1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #define DEBUG_TYPE "format-test"
11 
12 #include "clang/Format/Format.h"
13 #include "../Tooling/RewriterTestContext.h"
14 #include "clang/Lex/Lexer.h"
15 #include "llvm/Support/Debug.h"
16 #include "gtest/gtest.h"
17 
18 namespace clang {
19 namespace format {
20 
21 class FormatTest : public ::testing::Test {
22 protected:
23   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
24                      const FormatStyle &Style) {
25     DEBUG(llvm::errs() << "---\n");
26     RewriterTestContext Context;
27     FileID ID = Context.createInMemoryFile("input.cc", Code);
28     SourceLocation Start =
29         Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
30     std::vector<CharSourceRange> Ranges(
31         1,
32         CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
33     Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
34               getFormattingLangOpts());
35     tooling::Replacements Replace = reformat(
36         Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer());
37     ReplacementCount = Replace.size();
38     EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
39     DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
40     return Context.getRewrittenText(ID);
41   }
42 
43   std::string
44   format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
45     return format(Code, 0, Code.size(), Style);
46   }
47 
48   std::string messUp(llvm::StringRef Code) {
49     std::string MessedUp(Code.str());
50     bool InComment = false;
51     bool InPreprocessorDirective = false;
52     bool JustReplacedNewline = false;
53     for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
54       if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
55         if (JustReplacedNewline)
56           MessedUp[i - 1] = '\n';
57         InComment = true;
58       } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
59         if (i != 0)
60           MessedUp[i - 1] = '\n';
61         InPreprocessorDirective = true;
62       } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
63         MessedUp[i] = ' ';
64         MessedUp[i + 1] = ' ';
65       } else if (MessedUp[i] == '\n') {
66         if (InComment) {
67           InComment = false;
68         } else if (InPreprocessorDirective) {
69           InPreprocessorDirective = false;
70         } else {
71           JustReplacedNewline = true;
72           MessedUp[i] = ' ';
73         }
74       } else if (MessedUp[i] != ' ') {
75         JustReplacedNewline = false;
76       }
77     }
78     return MessedUp;
79   }
80 
81   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
82     FormatStyle Style = getLLVMStyle();
83     Style.ColumnLimit = ColumnLimit;
84     return Style;
85   }
86 
87   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
88     FormatStyle Style = getGoogleStyle();
89     Style.ColumnLimit = ColumnLimit;
90     return Style;
91   }
92 
93   void verifyFormat(llvm::StringRef Code,
94                     const FormatStyle &Style = getLLVMStyle()) {
95     EXPECT_EQ(Code.str(), format(messUp(Code), Style));
96   }
97 
98   void verifyGoogleFormat(llvm::StringRef Code) {
99     verifyFormat(Code, getGoogleStyle());
100   }
101 
102   void verifyIndependentOfContext(llvm::StringRef text) {
103     verifyFormat(text);
104     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
105   }
106 
107   int ReplacementCount;
108 };
109 
110 TEST_F(FormatTest, MessUp) {
111   EXPECT_EQ("1 2 3", messUp("1 2 3"));
112   EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
113   EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
114   EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
115   EXPECT_EQ("a\n#b  c  d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // Basic function tests.
120 //===----------------------------------------------------------------------===//
121 
122 TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
123   EXPECT_EQ(";", format(";"));
124 }
125 
126 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
127   EXPECT_EQ("int i;", format("  int i;"));
128   EXPECT_EQ("\nint i;", format(" \n\t \r  int i;"));
129   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
130   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
131 }
132 
133 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
134   EXPECT_EQ("int i;", format("int\ni;"));
135 }
136 
137 TEST_F(FormatTest, FormatsNestedBlockStatements) {
138   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
139 }
140 
141 TEST_F(FormatTest, FormatsNestedCall) {
142   verifyFormat("Method(f1, f2(f3));");
143   verifyFormat("Method(f1(f2, f3()));");
144   verifyFormat("Method(f1(f2, (f3())));");
145 }
146 
147 TEST_F(FormatTest, NestedNameSpecifiers) {
148   verifyFormat("vector< ::Type> v;");
149   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
150 }
151 
152 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
153   EXPECT_EQ("if (a) {\n"
154             "  f();\n"
155             "}",
156             format("if(a){f();}"));
157   EXPECT_EQ(4, ReplacementCount);
158   EXPECT_EQ("if (a) {\n"
159             "  f();\n"
160             "}",
161             format("if (a) {\n"
162                    "  f();\n"
163                    "}"));
164   EXPECT_EQ(0, ReplacementCount);
165 }
166 
167 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
168   EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
169   EXPECT_EQ("int a;", format("int a;         "));
170   EXPECT_EQ("int a;\n", format("int a;  \n   \n   \n "));
171   EXPECT_EQ("int a;\nint b;    ",
172             format("int a;  \nint b;    ", 0, 0, getLLVMStyle()));
173 }
174 
175 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
176   EXPECT_EQ("int b;\nint a;",
177             format("int b;\n   int a;", 7, 0, getLLVMStyle()));
178   EXPECT_EQ("int b;\n   int a;",
179             format("int b;\n   int a;", 6, 0, getLLVMStyle()));
180 
181   EXPECT_EQ("#define A  \\\n"
182             "  int a;   \\\n"
183             "  int b;",
184             format("#define A  \\\n"
185                    "  int a;   \\\n"
186                    "    int b;",
187                    26, 0, getLLVMStyleWithColumns(12)));
188   EXPECT_EQ("#define A  \\\n"
189             "  int a;   \\\n"
190             "    int b;",
191             format("#define A  \\\n"
192                    "  int a;   \\\n"
193                    "    int b;",
194                    25, 0, getLLVMStyleWithColumns(12)));
195 }
196 
197 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
198   EXPECT_EQ("int  a;\n\n int b;",
199             format("int  a;\n  \n\n int b;", 7, 0, getLLVMStyle()));
200   EXPECT_EQ("int  a;\n\n int b;",
201             format("int  a;\n  \n\n int b;", 9, 0, getLLVMStyle()));
202 }
203 
204 TEST_F(FormatTest, ReformatsMovedLines) {
205   EXPECT_EQ(
206       "template <typename T> T *getFETokenInfo() const {\n"
207       "  return static_cast<T *>(FETokenInfo);\n"
208       "}\n"
209       "  int a; // <- Should not be formatted",
210       format(
211           "template<typename T>\n"
212           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
213           "  int a; // <- Should not be formatted",
214           9, 5, getLLVMStyle()));
215 }
216 
217 //===----------------------------------------------------------------------===//
218 // Tests for control statements.
219 //===----------------------------------------------------------------------===//
220 
221 TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
222   verifyFormat("if (true)\n  f();\ng();");
223   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
224   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
225 
226   FormatStyle AllowsMergedIf = getGoogleStyle();
227   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
228   verifyFormat("if (a)\n"
229                "  // comment\n"
230                "  f();",
231                AllowsMergedIf);
232 
233   verifyFormat("if (a)  // Can't merge this\n"
234                "  f();\n",
235                AllowsMergedIf);
236   verifyFormat("if (a) /* still don't merge */\n"
237                "  f();",
238                AllowsMergedIf);
239   verifyFormat("if (a) {  // Never merge this\n"
240                "  f();\n"
241                "}",
242                AllowsMergedIf);
243   verifyFormat("if (a) { /* Never merge this */\n"
244                "  f();\n"
245                "}",
246                AllowsMergedIf);
247 
248   AllowsMergedIf.ColumnLimit = 14;
249   verifyFormat("if (a) return;", AllowsMergedIf);
250   verifyFormat("if (aaaaaaaaa)\n"
251                "  return;",
252                AllowsMergedIf);
253 
254   AllowsMergedIf.ColumnLimit = 13;
255   verifyFormat("if (a)\n  return;", AllowsMergedIf);
256 }
257 
258 TEST_F(FormatTest, ParseIfElse) {
259   verifyFormat("if (true)\n"
260                "  if (true)\n"
261                "    if (true)\n"
262                "      f();\n"
263                "    else\n"
264                "      g();\n"
265                "  else\n"
266                "    h();\n"
267                "else\n"
268                "  i();");
269   verifyFormat("if (true)\n"
270                "  if (true)\n"
271                "    if (true) {\n"
272                "      if (true)\n"
273                "        f();\n"
274                "    } else {\n"
275                "      g();\n"
276                "    }\n"
277                "  else\n"
278                "    h();\n"
279                "else {\n"
280                "  i();\n"
281                "}");
282 }
283 
284 TEST_F(FormatTest, ElseIf) {
285   verifyFormat("if (a) {\n} else if (b) {\n}");
286   verifyFormat("if (a)\n"
287                "  f();\n"
288                "else if (b)\n"
289                "  g();\n"
290                "else\n"
291                "  h();");
292 }
293 
294 TEST_F(FormatTest, FormatsForLoop) {
295   verifyFormat(
296       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
297       "     ++VeryVeryLongLoopVariable)\n"
298       "  ;");
299   verifyFormat("for (;;)\n"
300                "  f();");
301   verifyFormat("for (;;) {\n}");
302   verifyFormat("for (;;) {\n"
303                "  f();\n"
304                "}");
305   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
306 
307   verifyFormat(
308       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
309       "                                          E = UnwrappedLines.end();\n"
310       "     I != E; ++I) {\n}");
311 
312   verifyFormat(
313       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
314       "     ++IIIII) {\n}");
315   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
316                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
317                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
318   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
319                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
320                "         E = FD->getDeclsInPrototypeScope().end();\n"
321                "     I != E; ++I) {\n}");
322 
323   // FIXME: Not sure whether we want extra identation in line 3 here:
324   verifyFormat(
325       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
326       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
327       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
328       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
329       "     ++aaaaaaaaaaa) {\n}");
330   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
331                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
332                "}");
333   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
334                "         aaaaaaaaaa);\n"
335                "     iter; ++iter) {\n"
336                "}");
337 
338   FormatStyle NoBinPacking = getLLVMStyle();
339   NoBinPacking.BinPackParameters = false;
340   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
341                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
342                "                                           aaaaaaaaaaaaaaaa,\n"
343                "                                           aaaaaaaaaaaaaaaa,\n"
344                "                                           aaaaaaaaaaaaaaaa);\n"
345                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
346                "}",
347                NoBinPacking);
348   verifyFormat(
349       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
350       "                                          E = UnwrappedLines.end();\n"
351       "     I != E;\n"
352       "     ++I) {\n}",
353       NoBinPacking);
354 }
355 
356 TEST_F(FormatTest, RangeBasedForLoops) {
357   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
358                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
359   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
360                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
361   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
362                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
363 }
364 
365 TEST_F(FormatTest, FormatsWhileLoop) {
366   verifyFormat("while (true) {\n}");
367   verifyFormat("while (true)\n"
368                "  f();");
369   verifyFormat("while () {\n}");
370   verifyFormat("while () {\n"
371                "  f();\n"
372                "}");
373 }
374 
375 TEST_F(FormatTest, FormatsDoWhile) {
376   verifyFormat("do {\n"
377                "  do_something();\n"
378                "} while (something());");
379   verifyFormat("do\n"
380                "  do_something();\n"
381                "while (something());");
382 }
383 
384 TEST_F(FormatTest, FormatsSwitchStatement) {
385   verifyFormat("switch (x) {\n"
386                "case 1:\n"
387                "  f();\n"
388                "  break;\n"
389                "case kFoo:\n"
390                "case ns::kBar:\n"
391                "case kBaz:\n"
392                "  break;\n"
393                "default:\n"
394                "  g();\n"
395                "  break;\n"
396                "}");
397   verifyFormat("switch (x) {\n"
398                "case 1: {\n"
399                "  f();\n"
400                "  break;\n"
401                "}\n"
402                "}");
403   verifyFormat("switch (x) {\n"
404                "case 1: {\n"
405                "  f();\n"
406                "  {\n"
407                "    g();\n"
408                "    h();\n"
409                "  }\n"
410                "  break;\n"
411                "}\n"
412                "}");
413   verifyFormat("switch (x) {\n"
414                "case 1: {\n"
415                "  f();\n"
416                "  if (foo) {\n"
417                "    g();\n"
418                "    h();\n"
419                "  }\n"
420                "  break;\n"
421                "}\n"
422                "}");
423   verifyFormat("switch (x) {\n"
424                "case 1: {\n"
425                "  f();\n"
426                "  g();\n"
427                "} break;\n"
428                "}");
429   verifyFormat("switch (test)\n"
430                "  ;");
431   verifyFormat("switch (x) {\n"
432                "default: {\n"
433                "  // Do nothing.\n"
434                "}\n"
435                "}");
436   verifyFormat("switch (x) {\n"
437                "// comment\n"
438                "// if 1, do f()\n"
439                "case 1:\n"
440                "  f();\n"
441                "}");
442   verifyFormat("switch (x) {\n"
443                "case 1:\n"
444                "  // Do amazing stuff\n"
445                "  {\n"
446                "    f();\n"
447                "    g();\n"
448                "  }\n"
449                "  break;\n"
450                "}");
451   verifyFormat("#define A          \\\n"
452                "  switch (x) {     \\\n"
453                "  case a:          \\\n"
454                "    foo = b;       \\\n"
455                "  }", getLLVMStyleWithColumns(20));
456 
457   verifyGoogleFormat("switch (x) {\n"
458                      "  case 1:\n"
459                      "    f();\n"
460                      "    break;\n"
461                      "  case kFoo:\n"
462                      "  case ns::kBar:\n"
463                      "  case kBaz:\n"
464                      "    break;\n"
465                      "  default:\n"
466                      "    g();\n"
467                      "    break;\n"
468                      "}");
469   verifyGoogleFormat("switch (x) {\n"
470                      "  case 1: {\n"
471                      "    f();\n"
472                      "    break;\n"
473                      "  }\n"
474                      "}");
475   verifyGoogleFormat("switch (test)\n"
476                      "    ;");
477 }
478 
479 TEST_F(FormatTest, FormatsLabels) {
480   verifyFormat("void f() {\n"
481                "  some_code();\n"
482                "test_label:\n"
483                "  some_other_code();\n"
484                "  {\n"
485                "    some_more_code();\n"
486                "  another_label:\n"
487                "    some_more_code();\n"
488                "  }\n"
489                "}");
490   verifyFormat("some_code();\n"
491                "test_label:\n"
492                "some_other_code();");
493 }
494 
495 //===----------------------------------------------------------------------===//
496 // Tests for comments.
497 //===----------------------------------------------------------------------===//
498 
499 TEST_F(FormatTest, UnderstandsSingleLineComments) {
500   verifyFormat("//* */");
501   verifyFormat("// line 1\n"
502                "// line 2\n"
503                "void f() {}\n");
504 
505   verifyFormat("void f() {\n"
506                "  // Doesn't do anything\n"
507                "}");
508   verifyFormat("void f(int i,  // some comment (probably for i)\n"
509                "       int j,  // some comment (probably for j)\n"
510                "       int k); // some comment (probably for k)");
511   verifyFormat("void f(int i,\n"
512                "       // some comment (probably for j)\n"
513                "       int j,\n"
514                "       // some comment (probably for k)\n"
515                "       int k);");
516 
517   verifyFormat("int i    // This is a fancy variable\n"
518                "    = 5; // with nicely aligned comment.");
519 
520   verifyFormat("// Leading comment.\n"
521                "int a; // Trailing comment.");
522   verifyFormat("int a; // Trailing comment\n"
523                "       // on 2\n"
524                "       // or 3 lines.\n"
525                "int b;");
526   verifyFormat("int a; // Trailing comment\n"
527                "\n"
528                "// Leading comment.\n"
529                "int b;");
530   verifyFormat("int a;    // Comment.\n"
531                "          // More details.\n"
532                "int bbbb; // Another comment.");
533   verifyFormat(
534       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
535       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
536       "int cccccccccccccccccccccccccccccc;       // comment\n"
537       "int ddd;                     // looooooooooooooooooooooooong comment\n"
538       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
539       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
540       "int ccccccccccccccccccc;     // comment");
541 
542   verifyFormat("#include \"a\"     // comment\n"
543                "#include \"a/b/c\" // comment");
544   verifyFormat("#include <a>     // comment\n"
545                "#include <a/b/c> // comment");
546 
547   verifyFormat("enum E {\n"
548                "  // comment\n"
549                "  VAL_A, // comment\n"
550                "  VAL_B\n"
551                "};");
552 
553   verifyFormat(
554       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
555       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
556   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
557                "    // Comment inside a statement.\n"
558                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
559   verifyFormat(
560       "bool aaaaaaaaaaaaa = // comment\n"
561       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
562       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
563 
564   verifyFormat("int aaaa; // aaaaa\n"
565                "int aa;   // aaaaaaa",
566                getLLVMStyleWithColumns(20));
567 
568   EXPECT_EQ("void f() { // This does something ..\n"
569             "}\n"
570             "int a; // This is unrelated",
571             format("void f()    {     // This does something ..\n"
572                    "  }\n"
573                    "int   a;     // This is unrelated"));
574   EXPECT_EQ("void f() { // This does something ..\n"
575             "}          // awesome..\n"
576             "\n"
577             "int a; // This is unrelated",
578             format("void f()    { // This does something ..\n"
579                    "      } // awesome..\n"
580                    " \n"
581                    "int a;    // This is unrelated"));
582 
583   EXPECT_EQ("int i; // single line trailing comment",
584             format("int i;\\\n// single line trailing comment"));
585 
586   verifyGoogleFormat("int a;  // Trailing comment.");
587 
588   verifyFormat("someFunction(anotherFunction( // Force break.\n"
589                "    parameter));");
590 
591   verifyGoogleFormat("#endif  // HEADER_GUARD");
592 
593   verifyFormat("const char *test[] = {\n"
594                "  // A\n"
595                "  \"aaaa\",\n"
596                "  // B\n"
597                "  \"aaaaa\",\n"
598                "};");
599   verifyGoogleFormat(
600       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
601       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81 cols with this comment");
602   EXPECT_EQ("D(a, {\n"
603             "  // test\n"
604             "  int a;\n"
605             "});",
606             format("D(a, {\n"
607                    "// test\n"
608                    "int a;\n"
609                    "});"));
610 }
611 
612 TEST_F(FormatTest, CanFormatCommentsLocally) {
613   EXPECT_EQ("int a;    // comment\n"
614             "int    b; // comment",
615             format("int   a; // comment\n"
616                    "int    b; // comment",
617                    0, 0, getLLVMStyle()));
618   EXPECT_EQ("int   a; // comment\n"
619             "         // line 2\n"
620             "int b;",
621             format("int   a; // comment\n"
622                    "            // line 2\n"
623                    "int b;",
624                    28, 0, getLLVMStyle()));
625   EXPECT_EQ("int aaaaaa; // comment\n"
626             "int b;\n"
627             "int c; // unrelated comment",
628             format("int aaaaaa; // comment\n"
629                    "int b;\n"
630                    "int   c; // unrelated comment",
631                    31, 0, getLLVMStyle()));
632 }
633 
634 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
635   EXPECT_EQ("// comment", format("// comment  "));
636   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
637             format("int aaaaaaa, bbbbbbb; // comment                   ",
638                    getLLVMStyleWithColumns(33)));
639 }
640 
641 TEST_F(FormatTest, UnderstandsMultiLineComments) {
642   verifyFormat("f(/*test=*/ true);");
643   EXPECT_EQ(
644       "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
645       "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
646       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n/* Trailing comment for aa... */\n"
647              "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
648   EXPECT_EQ(
649       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
650       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
651       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
652              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
653 
654   FormatStyle NoBinPacking = getLLVMStyle();
655   NoBinPacking.BinPackParameters = false;
656   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
657                "         /* parameter 2 */ aaaaaa,\n"
658                "         /* parameter 3 */ aaaaaa,\n"
659                "         /* parameter 4 */ aaaaaa);",
660                NoBinPacking);
661 }
662 
663 TEST_F(FormatTest, AlignsMultiLineComments) {
664   EXPECT_EQ("/*\n"
665             " * Really multi-line\n"
666             " * comment.\n"
667             " */\n"
668             "void f() {}",
669             format("  /*\n"
670                    "   * Really multi-line\n"
671                    "   * comment.\n"
672                    "   */\n"
673                    "  void f() {}"));
674   EXPECT_EQ("class C {\n"
675             "  /*\n"
676             "   * Another multi-line\n"
677             "   * comment.\n"
678             "   */\n"
679             "  void f() {}\n"
680             "};",
681             format("class C {\n"
682                    "/*\n"
683                    " * Another multi-line\n"
684                    " * comment.\n"
685                    " */\n"
686                    "void f() {}\n"
687                    "};"));
688   EXPECT_EQ("/*\n"
689             "  1. This is a comment with non-trivial formatting.\n"
690             "     1.1. We have to indent/outdent all lines equally\n"
691             "         1.1.1. to keep the formatting.\n"
692             " */",
693             format("  /*\n"
694                    "    1. This is a comment with non-trivial formatting.\n"
695                    "       1.1. We have to indent/outdent all lines equally\n"
696                    "           1.1.1. to keep the formatting.\n"
697                    "   */"));
698   EXPECT_EQ("/*\n"
699             " Don't try to outdent if there's not enough inentation.\n"
700             " */",
701             format("  /*\n"
702                    " Don't try to outdent if there's not enough inentation.\n"
703                    " */"));
704 }
705 
706 TEST_F(FormatTest, SplitsLongCxxComments) {
707   EXPECT_EQ("// A comment that\n"
708             "// doesn't fit on\n"
709             "// one line",
710             format("// A comment that doesn't fit on one line",
711                    getLLVMStyleWithColumns(20)));
712   EXPECT_EQ("// a b c d\n"
713             "// e f  g\n"
714             "// h i j k",
715             format("// a b c d e f  g h i j k",
716                    getLLVMStyleWithColumns(10)));
717   EXPECT_EQ("// a b c d\n"
718             "// e f  g\n"
719             "// h i j k",
720             format("\\\n// a b c d e f  g h i j k",
721                    getLLVMStyleWithColumns(10)));
722   EXPECT_EQ("if (true) // A comment that\n"
723             "          // doesn't fit on\n"
724             "          // one line",
725             format("if (true) // A comment that doesn't fit on one line   ",
726                    getLLVMStyleWithColumns(30)));
727   EXPECT_EQ("//    Don't_touch_leading_whitespace",
728             format("//    Don't_touch_leading_whitespace",
729                    getLLVMStyleWithColumns(20)));
730   EXPECT_EQ(
731       "//Don't add leading\n"
732       "//whitespace",
733       format("//Don't add leading whitespace", getLLVMStyleWithColumns(20)));
734   EXPECT_EQ("// A comment before\n"
735             "// a macro\n"
736             "// definition\n"
737             "#define a b",
738             format("// A comment before a macro definition\n"
739                    "#define a b",
740                    getLLVMStyleWithColumns(20)));
741 }
742 
743 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
744   EXPECT_EQ("namespace {}\n// Test\n#define A",
745             format("namespace {}\n   // Test\n#define A"));
746   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
747             format("namespace {}\n   /* Test */\n#define A"));
748   EXPECT_EQ("namespace {}\n/* Test */ #define A",
749             format("namespace {}\n   /* Test */    #define A"));
750 }
751 
752 TEST_F(FormatTest, SplitsLongLinesInComments) {
753   EXPECT_EQ("/* This is a long\n"
754             " * comment that\n"
755             " * doesn't\n"
756             " * fit on one line.\n"
757             " */",
758             format("/* "
759                    "This is a long                                         "
760                    "comment that "
761                    "doesn't                                    "
762                    "fit on one line.  */",
763                    getLLVMStyleWithColumns(20)));
764   EXPECT_EQ("/* a b c d\n"
765             " * e f  g\n"
766             " * h i j k\n"
767             " */",
768             format("/* a b c d e f  g h i j k */",
769                    getLLVMStyleWithColumns(10)));
770   EXPECT_EQ("/* a b c d\n"
771             " * e f  g\n"
772             " * h i j k\n"
773             " */",
774             format("\\\n/* a b c d e f  g h i j k */",
775                    getLLVMStyleWithColumns(10)));
776   EXPECT_EQ("/*\n"
777             "This is a long\n"
778             "comment that doesn't\n"
779             "fit on one line.\n"
780             "*/",
781             format("/*\n"
782                    "This is a long                                         "
783                    "comment that doesn't                                    "
784                    "fit on one line.                                      \n"
785                    "*/", getLLVMStyleWithColumns(20)));
786   EXPECT_EQ("/*\n"
787             " * This is a long\n"
788             " * comment that\n"
789             " * doesn't fit on\n"
790             " * one line.\n"
791             " */",
792             format("/*      \n"
793                    " * This is a long "
794                    "   comment that     "
795                    "   doesn't fit on   "
796                    "   one line.                                            \n"
797                    " */", getLLVMStyleWithColumns(20)));
798   EXPECT_EQ("/*\n"
799             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
800             " * so_it_should_be_broken\n"
801             " * wherever_a_space_occurs\n"
802             " */",
803             format("/*\n"
804                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
805                    "   so_it_should_be_broken "
806                    "   wherever_a_space_occurs                             \n"
807                    " */",
808                    getLLVMStyleWithColumns(20)));
809   EXPECT_EQ("/*\n"
810             " *    This_comment_can_not_be_broken_into_lines\n"
811             " */",
812             format("/*\n"
813                    " *    This_comment_can_not_be_broken_into_lines\n"
814                    " */",
815                    getLLVMStyleWithColumns(20)));
816   EXPECT_EQ("{\n"
817             "  /*\n"
818             "  This is another\n"
819             "  long comment that\n"
820             "  doesn't fit on one\n"
821             "  line    1234567890\n"
822             "  */\n"
823             "}",
824             format("{\n"
825                    "/*\n"
826                    "This is another     "
827                    "  long comment that "
828                    "  doesn't fit on one"
829                    "  line    1234567890\n"
830                    "*/\n"
831                    "}", getLLVMStyleWithColumns(20)));
832   EXPECT_EQ("{\n"
833             "  /*\n"
834             "   * This        i s\n"
835             "   * another comment\n"
836             "   * t hat  doesn' t\n"
837             "   * fit on one l i\n"
838             "   * n e\n"
839             "   */\n"
840             "}",
841             format("{\n"
842                    "/*\n"
843                    " * This        i s"
844                    "   another comment"
845                    "   t hat  doesn' t"
846                    "   fit on one l i"
847                    "   n e\n"
848                    " */\n"
849                    "}", getLLVMStyleWithColumns(20)));
850   EXPECT_EQ("/*\n"
851             " * This is a long\n"
852             " * comment that\n"
853             " * doesn't fit on\n"
854             " * one line\n"
855             " */",
856             format("   /*\n"
857                    "    * This is a long comment that doesn't fit on one line\n"
858                    "    */", getLLVMStyleWithColumns(20)));
859   EXPECT_EQ("{\n"
860             "  if (something) /* This is a\n"
861             "long comment */\n"
862             "    ;\n"
863             "}",
864             format("{\n"
865                    "  if (something) /* This is a long comment */\n"
866                    "    ;\n"
867                    "}",
868                    getLLVMStyleWithColumns(30)));
869 }
870 
871 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
872   EXPECT_EQ("#define X          \\\n"
873             "  /*               \\\n"
874             "   Test            \\\n"
875             "   Macro comment   \\\n"
876             "   with a long     \\\n"
877             "   line            \\\n"
878             "   */              \\\n"
879             "  A + B",
880             format("#define X \\\n"
881                    "  /*\n"
882                    "   Test\n"
883                    "   Macro comment with a long  line\n"
884                    "   */ \\\n"
885                    "  A + B",
886                    getLLVMStyleWithColumns(20)));
887   EXPECT_EQ("#define X          \\\n"
888             "  /* Macro comment \\\n"
889             "     with a long   \\\n"
890             "     line */       \\\n"
891             "  A + B",
892             format("#define X \\\n"
893                    "  /* Macro comment with a long\n"
894                    "     line */ \\\n"
895                    "  A + B",
896                    getLLVMStyleWithColumns(20)));
897   EXPECT_EQ("#define X          \\\n"
898             "  /* Macro comment \\\n"
899             "   * with a long   \\\n"
900             "   * line */       \\\n"
901             "  A + B",
902             format("#define X \\\n"
903                    "  /* Macro comment with a long  line */ \\\n"
904                    "  A + B",
905                    getLLVMStyleWithColumns(20)));
906 }
907 
908 TEST_F(FormatTest, CommentsInStaticInitializers) {
909   EXPECT_EQ(
910       "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
911       "                         aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
912       "                         /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
913       "                         aaaaaaaaaaaaaaaaaaaa, // comment\n"
914       "                         aaaaaaaaaaaaaaaaaaaa };",
915       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
916              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
917              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
918              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
919              "                  aaaaaaaaaaaaaaaaaaaa };"));
920   verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
921                "                         bbbbbbbbbbb, ccccccccccc };");
922   verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
923                "                         // comment for bb....\n"
924                "                         bbbbbbbbbbb, ccccccccccc };");
925   verifyGoogleFormat(
926       "static SomeType type = { aaaaaaaaaaa,  // comment for aa...\n"
927       "                         bbbbbbbbbbb, ccccccccccc };");
928   verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
929                      "                         // comment for bb....\n"
930                      "                         bbbbbbbbbbb, ccccccccccc };");
931 
932   verifyFormat("S s = { { a, b, c },   // Group #1\n"
933                "        { d, e, f },   // Group #2\n"
934                "        { g, h, i } }; // Group #3");
935   verifyFormat("S s = { { // Group #1\n"
936                "          a, b, c },\n"
937                "        { // Group #2\n"
938                "          d, e, f },\n"
939                "        { // Group #3\n"
940                "          g, h, i } };");
941 
942   EXPECT_EQ("S s = {\n"
943             "  // Some comment\n"
944             "  a,\n"
945             "\n"
946             "  // Comment after empty line\n"
947             "  b\n"
948             "}",
949             format("S s =    {\n"
950                    "      // Some comment\n"
951                    "  a,\n"
952                    "  \n"
953                    "     // Comment after empty line\n"
954                    "      b\n"
955                    "}"));
956   EXPECT_EQ("S s = { a, b };", format("S s = {\n"
957                                       "  a,\n"
958                                       "\n"
959                                       "  b\n"
960                                       "};"));
961   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
962                "  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
963                "  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
964                "  0x00, 0x00, 0x00, 0x00              // comment\n"
965                "};");
966 }
967 
968 //===----------------------------------------------------------------------===//
969 // Tests for classes, namespaces, etc.
970 //===----------------------------------------------------------------------===//
971 
972 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
973   verifyFormat("class A {\n};");
974 }
975 
976 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
977   verifyFormat("class A {\n"
978                "public:\n"
979                "public: // comment\n"
980                "protected:\n"
981                "private:\n"
982                "  void f() {}\n"
983                "};");
984   verifyGoogleFormat("class A {\n"
985                      " public:\n"
986                      " protected:\n"
987                      " private:\n"
988                      "  void f() {}\n"
989                      "};");
990 }
991 
992 TEST_F(FormatTest, SeparatesLogicalBlocks) {
993   EXPECT_EQ("class A {\n"
994             "public:\n"
995             "  void f();\n"
996             "\n"
997             "private:\n"
998             "  void g() {}\n"
999             "  // test\n"
1000             "protected:\n"
1001             "  int h;\n"
1002             "};",
1003             format("class A {\n"
1004                    "public:\n"
1005                    "void f();\n"
1006                    "private:\n"
1007                    "void g() {}\n"
1008                    "// test\n"
1009                    "protected:\n"
1010                    "int h;\n"
1011                    "};"));
1012 }
1013 
1014 TEST_F(FormatTest, FormatsClasses) {
1015   verifyFormat("class A : public B {\n};");
1016   verifyFormat("class A : public ::B {\n};");
1017 
1018   verifyFormat(
1019       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1020       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
1021       "};\n");
1022   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1023                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1024                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
1025                "};\n");
1026   verifyFormat(
1027       "class A : public B, public C, public D, public E, public F, public G {\n"
1028       "};");
1029   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1030                "                     public C,\n"
1031                "                     public D,\n"
1032                "                     public E,\n"
1033                "                     public F,\n"
1034                "                     public G {\n"
1035                "};");
1036 
1037   verifyFormat("class\n"
1038                "    ReallyReallyLongClassName {\n};",
1039                getLLVMStyleWithColumns(32));
1040 }
1041 
1042 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1043   verifyFormat("class A {\n} a, b;");
1044   verifyFormat("struct A {\n} a, b;");
1045   verifyFormat("union A {\n} a;");
1046 }
1047 
1048 TEST_F(FormatTest, FormatsEnum) {
1049   verifyFormat("enum {\n"
1050                "  Zero,\n"
1051                "  One = 1,\n"
1052                "  Two = One + 1,\n"
1053                "  Three = (One + Two),\n"
1054                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1055                "  Five = (One, Two, Three, Four, 5)\n"
1056                "};");
1057   verifyFormat("enum Enum {\n"
1058                "};");
1059   verifyFormat("enum {\n"
1060                "};");
1061   verifyFormat("enum X E {\n} d;");
1062   verifyFormat("enum __attribute__((...)) E {\n} d;");
1063   verifyFormat("enum __declspec__((...)) E {\n} d;");
1064   verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
1065 }
1066 
1067 TEST_F(FormatTest, FormatsBitfields) {
1068   verifyFormat("struct Bitfields {\n"
1069                "  unsigned sClass : 8;\n"
1070                "  unsigned ValueKind : 2;\n"
1071                "};");
1072 }
1073 
1074 TEST_F(FormatTest, FormatsNamespaces) {
1075   verifyFormat("namespace some_namespace {\n"
1076                "class A {\n};\n"
1077                "void f() { f(); }\n"
1078                "}");
1079   verifyFormat("namespace {\n"
1080                "class A {\n};\n"
1081                "void f() { f(); }\n"
1082                "}");
1083   verifyFormat("inline namespace X {\n"
1084                "class A {\n};\n"
1085                "void f() { f(); }\n"
1086                "}");
1087   verifyFormat("using namespace some_namespace;\n"
1088                "class A {\n};\n"
1089                "void f() { f(); }");
1090 
1091   // This code is more common than we thought; if we
1092   // layout this correctly the semicolon will go into
1093   // its own line, which is undesireable.
1094   verifyFormat("namespace {\n};");
1095   verifyFormat("namespace {\n"
1096                "class A {\n"
1097                "};\n"
1098                "};");
1099 }
1100 
1101 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1102 
1103 TEST_F(FormatTest, FormatsInlineASM) {
1104   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1105   verifyFormat(
1106       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1107       "    \"cpuid\\n\\t\"\n"
1108       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1109       "    : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n"
1110       "    : \"a\"(value));");
1111 }
1112 
1113 TEST_F(FormatTest, FormatTryCatch) {
1114   // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
1115   // also not create single-line-blocks.
1116   verifyFormat("try {\n"
1117                "  throw a * b;\n"
1118                "}\n"
1119                "catch (int a) {\n"
1120                "  // Do nothing.\n"
1121                "}\n"
1122                "catch (...) {\n"
1123                "  exit(42);\n"
1124                "}");
1125 
1126   // Function-level try statements.
1127   verifyFormat("int f() try { return 4; }\n"
1128                "catch (...) {\n"
1129                "  return 5;\n"
1130                "}");
1131   verifyFormat("class A {\n"
1132                "  int a;\n"
1133                "  A() try : a(0) {}\n"
1134                "  catch (...) {\n"
1135                "    throw;\n"
1136                "  }\n"
1137                "};\n");
1138 }
1139 
1140 TEST_F(FormatTest, FormatObjCTryCatch) {
1141   verifyFormat("@try {\n"
1142                "  f();\n"
1143                "}\n"
1144                "@catch (NSException e) {\n"
1145                "  @throw;\n"
1146                "}\n"
1147                "@finally {\n"
1148                "  exit(42);\n"
1149                "}");
1150 }
1151 
1152 TEST_F(FormatTest, StaticInitializers) {
1153   verifyFormat("static SomeClass SC = { 1, 'a' };");
1154 
1155   // FIXME: Format like enums if the static initializer does not fit on a line.
1156   verifyFormat(
1157       "static SomeClass WithALoooooooooooooooooooongName = {\n"
1158       "  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1159       "};");
1160 
1161   verifyFormat(
1162       "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
1163       "                     looooooooooooooooooooooooooooooooooongname,\n"
1164       "                     looooooooooooooooooooooooooooooong };");
1165   // Allow bin-packing in static initializers as this would often lead to
1166   // terrible results, e.g.:
1167   verifyGoogleFormat(
1168       "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
1169       "                     looooooooooooooooooooooooooooooooooongname,\n"
1170       "                     looooooooooooooooooooooooooooooong };");
1171 }
1172 
1173 TEST_F(FormatTest, NestedStaticInitializers) {
1174   verifyFormat("static A x = { { {} } };\n");
1175   verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
1176                "                 { init1, init2, init3, init4 } } };");
1177 
1178   verifyFormat("somes Status::global_reps[3] = {\n"
1179                "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
1180                "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
1181                "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
1182                "};");
1183   verifyGoogleFormat("somes Status::global_reps[3] = {\n"
1184                      "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
1185                      "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
1186                      "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
1187                      "};");
1188   verifyFormat(
1189       "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
1190       "                   { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
1191       " } };");
1192 
1193   verifyFormat(
1194       "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
1195       "                            { 111111111111111111111111111111,\n"
1196       "                              222222222222222222222222222222,\n"
1197       "                              333333333333333333333333333333 },\n"
1198       "                            { 1, 2, 3 }, { 1, 2, 3 } } };");
1199   verifyFormat(
1200       "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
1201       "                          { { 111111111111111111111111111111,\n"
1202       "                              222222222222222222222222222222,\n"
1203       "                              333333333333333333333333333333 } },\n"
1204       "                          { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
1205 
1206   // FIXME: We might at some point want to handle this similar to parameter
1207   // lists, where we have an option to put each on a single line.
1208   verifyFormat(
1209       "struct {\n"
1210       "  unsigned bit;\n"
1211       "  const char *const name;\n"
1212       "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
1213       "                  { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
1214 }
1215 
1216 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1217   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1218                "                      \\\n"
1219                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1220 }
1221 
1222 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1223   verifyFormat(
1224       "virtual void write(ELFWriter *writerrr,\n"
1225       "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1226 }
1227 
1228 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1229   EXPECT_EQ("#123 \"A string literal\"",
1230             format("   #     123    \"A string literal\""));
1231   EXPECT_EQ("#;", format("#;"));
1232   verifyFormat("#\n;\n;\n;");
1233 }
1234 
1235 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1236   EXPECT_EQ("#line 42 \"test\"\n",
1237             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1238   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1239                                     getLLVMStyleWithColumns(12)));
1240 }
1241 
1242 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1243   EXPECT_EQ("#line 42 \"test\"",
1244             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1245   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1246 }
1247 
1248 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1249   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1250   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1251   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1252   // FIXME: We never break before the macro name.
1253   verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
1254 
1255   verifyFormat("#define A A\n#define A A");
1256   verifyFormat("#define A(X) A\n#define A A");
1257 
1258   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1259   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1260 }
1261 
1262 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1263   EXPECT_EQ("// somecomment\n"
1264             "#include \"a.h\"\n"
1265             "#define A(  \\\n"
1266             "    A, B)\n"
1267             "#include \"b.h\"\n"
1268             "// somecomment\n",
1269             format("  // somecomment\n"
1270                    "  #include \"a.h\"\n"
1271                    "#define A(A,\\\n"
1272                    "    B)\n"
1273                    "    #include \"b.h\"\n"
1274                    " // somecomment\n",
1275                    getLLVMStyleWithColumns(13)));
1276 }
1277 
1278 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1279 
1280 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1281   EXPECT_EQ("#define A    \\\n"
1282             "  c;         \\\n"
1283             "  e;\n"
1284             "f;",
1285             format("#define A c; e;\n"
1286                    "f;",
1287                    getLLVMStyleWithColumns(14)));
1288 }
1289 
1290 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1291 
1292 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
1293   EXPECT_EQ("int  i;\n"
1294             "#define A \\\n"
1295             "  int i;  \\\n"
1296             "  int j\n"
1297             "int  k;",
1298             format("int  i;\n"
1299                    "#define A  \\\n"
1300                    " int   i    ;  \\\n"
1301                    " int   j\n"
1302                    "int  k;",
1303                    8, 0, getGoogleStyle())); // 8: position of "#define".
1304   EXPECT_EQ("int  i;\n"
1305             "#define A \\\n"
1306             "  int i;  \\\n"
1307             "  int j\n"
1308             "int  k;",
1309             format("int  i;\n"
1310                    "#define A  \\\n"
1311                    " int   i    ;  \\\n"
1312                    " int   j\n"
1313                    "int  k;",
1314                    45, 0, getGoogleStyle())); // 45: position of "j".
1315 }
1316 
1317 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1318   EXPECT_EQ("int x,\n"
1319             "#define A\n"
1320             "    y;",
1321             format("int x,\n#define A\ny;"));
1322 }
1323 
1324 TEST_F(FormatTest, HashInMacroDefinition) {
1325   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1326   verifyFormat("#define A \\\n"
1327                "  {       \\\n"
1328                "    f(#c);\\\n"
1329                "  }",
1330                getLLVMStyleWithColumns(11));
1331 
1332   verifyFormat("#define A(X)         \\\n"
1333                "  void function##X()",
1334                getLLVMStyleWithColumns(22));
1335 
1336   verifyFormat("#define A(a, b, c)   \\\n"
1337                "  void a##b##c()",
1338                getLLVMStyleWithColumns(22));
1339 
1340   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1341 }
1342 
1343 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1344   verifyFormat("#define A (1)");
1345 }
1346 
1347 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1348   EXPECT_EQ("#define A b;", format("#define A \\\n"
1349                                    "          \\\n"
1350                                    "  b;",
1351                                    getLLVMStyleWithColumns(25)));
1352   EXPECT_EQ("#define A \\\n"
1353             "          \\\n"
1354             "  a;      \\\n"
1355             "  b;",
1356             format("#define A \\\n"
1357                    "          \\\n"
1358                    "  a;      \\\n"
1359                    "  b;",
1360                    getLLVMStyleWithColumns(11)));
1361   EXPECT_EQ("#define A \\\n"
1362             "  a;      \\\n"
1363             "          \\\n"
1364             "  b;",
1365             format("#define A \\\n"
1366                    "  a;      \\\n"
1367                    "          \\\n"
1368                    "  b;",
1369                    getLLVMStyleWithColumns(11)));
1370 }
1371 
1372 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1373   verifyFormat("#define A :");
1374 
1375   // FIXME: Improve formatting of case labels in macros.
1376   verifyFormat("#define SOMECASES  \\\n"
1377                "  case 1:          \\\n"
1378                "  case 2\n",
1379                getLLVMStyleWithColumns(20));
1380 
1381   verifyFormat("#define A template <typename T>");
1382   verifyFormat("#define STR(x) #x\n"
1383                "f(STR(this_is_a_string_literal{));");
1384 }
1385 
1386 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
1387   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1388             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1389             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1390             "class X {\n"
1391             "};\n"
1392             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1393             "int *createScopDetectionPass() { return 0; }",
1394             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1395                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1396                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1397                    "  class X {};\n"
1398                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1399                    "  int *createScopDetectionPass() { return 0; }"));
1400   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
1401   // braces, so that inner block is indented one level more.
1402   EXPECT_EQ("int q() {\n"
1403             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1404             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1405             "  IPC_END_MESSAGE_MAP()\n"
1406             "}",
1407             format("int q() {\n"
1408                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1409                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1410                    "  IPC_END_MESSAGE_MAP()\n"
1411                    "}"));
1412   EXPECT_EQ("int q() {\n"
1413             "  f(x);\n"
1414             "  f(x) {}\n"
1415             "  f(x)->g();\n"
1416             "  f(x)->*g();\n"
1417             "  f(x).g();\n"
1418             "  f(x) = x;\n"
1419             "  f(x) += x;\n"
1420             "  f(x) -= x;\n"
1421             "  f(x) *= x;\n"
1422             "  f(x) /= x;\n"
1423             "  f(x) %= x;\n"
1424             "  f(x) &= x;\n"
1425             "  f(x) |= x;\n"
1426             "  f(x) ^= x;\n"
1427             "  f(x) >>= x;\n"
1428             "  f(x) <<= x;\n"
1429             "  f(x)[y].z();\n"
1430             "  LOG(INFO) << x;\n"
1431             "  ifstream(x) >> x;\n"
1432             "}\n",
1433             format("int q() {\n"
1434                    "  f(x)\n;\n"
1435                    "  f(x)\n {}\n"
1436                    "  f(x)\n->g();\n"
1437                    "  f(x)\n->*g();\n"
1438                    "  f(x)\n.g();\n"
1439                    "  f(x)\n = x;\n"
1440                    "  f(x)\n += x;\n"
1441                    "  f(x)\n -= x;\n"
1442                    "  f(x)\n *= x;\n"
1443                    "  f(x)\n /= x;\n"
1444                    "  f(x)\n %= x;\n"
1445                    "  f(x)\n &= x;\n"
1446                    "  f(x)\n |= x;\n"
1447                    "  f(x)\n ^= x;\n"
1448                    "  f(x)\n >>= x;\n"
1449                    "  f(x)\n <<= x;\n"
1450                    "  f(x)\n[y].z();\n"
1451                    "  LOG(INFO)\n << x;\n"
1452                    "  ifstream(x)\n >> x;\n"
1453                    "}\n"));
1454   EXPECT_EQ("int q() {\n"
1455             "  f(x)\n"
1456             "  if (1) {\n"
1457             "  }\n"
1458             "  f(x)\n"
1459             "  while (1) {\n"
1460             "  }\n"
1461             "  f(x)\n"
1462             "  g(x);\n"
1463             "  f(x)\n"
1464             "  try {\n"
1465             "    q();\n"
1466             "  }\n"
1467             "  catch (...) {\n"
1468             "  }\n"
1469             "}\n",
1470             format("int q() {\n"
1471                    "f(x)\n"
1472                    "if (1) {}\n"
1473                    "f(x)\n"
1474                    "while (1) {}\n"
1475                    "f(x)\n"
1476                    "g(x);\n"
1477                    "f(x)\n"
1478                    "try { q(); } catch (...) {}\n"
1479                    "}\n"));
1480   EXPECT_EQ("class A {\n"
1481             "  A() : t(0) {}\n"
1482             "  A(X x)\n" // FIXME: function-level try blocks are broken.
1483             "  try : t(0) {\n"
1484             "  }\n"
1485             "  catch (...) {\n"
1486             "  }\n"
1487             "};",
1488             format("class A {\n"
1489                    "  A()\n : t(0) {}\n"
1490                    "  A(X x)\n"
1491                    "  try : t(0) {} catch (...) {}\n"
1492                    "};"));
1493 }
1494 
1495 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
1496   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
1497 }
1498 
1499 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
1500   verifyFormat("{\n  { a #c; }\n}");
1501 }
1502 
1503 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
1504   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
1505             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
1506   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
1507             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
1508 }
1509 
1510 TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
1511   EXPECT_EQ(
1512       "#define A \\\n  int i;  \\\n  int j;",
1513       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
1514 }
1515 
1516 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
1517   verifyFormat("#define A \\\n"
1518                "  int v(  \\\n"
1519                "      a); \\\n"
1520                "  int i;",
1521                getLLVMStyleWithColumns(11));
1522 }
1523 
1524 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
1525   EXPECT_EQ(
1526       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1527       "                      \\\n"
1528       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
1529       "\n"
1530       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
1531       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
1532       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
1533              "\\\n"
1534              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
1535              "  \n"
1536              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
1537              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
1538 }
1539 
1540 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
1541   EXPECT_EQ("int\n"
1542             "#define A\n"
1543             "    a;",
1544             format("int\n#define A\na;"));
1545   verifyFormat("functionCallTo(\n"
1546                "    someOtherFunction(\n"
1547                "        withSomeParameters, whichInSequence,\n"
1548                "        areLongerThanALine(andAnotherCall,\n"
1549                "#define A B\n"
1550                "                           withMoreParamters,\n"
1551                "                           whichStronglyInfluenceTheLayout),\n"
1552                "        andMoreParameters),\n"
1553                "    trailing);",
1554                getLLVMStyleWithColumns(69));
1555 }
1556 
1557 TEST_F(FormatTest, LayoutBlockInsideParens) {
1558   EXPECT_EQ("functionCall({\n"
1559             "  int i;\n"
1560             "});",
1561             format(" functionCall ( {int i;} );"));
1562 }
1563 
1564 TEST_F(FormatTest, LayoutBlockInsideStatement) {
1565   EXPECT_EQ("SOME_MACRO { int i; }\n"
1566             "int i;",
1567             format("  SOME_MACRO  {int i;}  int i;"));
1568 }
1569 
1570 TEST_F(FormatTest, LayoutNestedBlocks) {
1571   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
1572                "  struct s {\n"
1573                "    int i;\n"
1574                "  };\n"
1575                "  s kBitsToOs[] = { { 10 } };\n"
1576                "  for (int i = 0; i < 10; ++i)\n"
1577                "    return;\n"
1578                "}");
1579 }
1580 
1581 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
1582   EXPECT_EQ("{}", format("{}"));
1583 
1584   // Negative test for enum.
1585   verifyFormat("enum E {\n};");
1586 
1587   // Note that when there's a missing ';', we still join...
1588   verifyFormat("enum E {}");
1589 }
1590 
1591 //===----------------------------------------------------------------------===//
1592 // Line break tests.
1593 //===----------------------------------------------------------------------===//
1594 
1595 TEST_F(FormatTest, FormatsFunctionDefinition) {
1596   verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
1597                " int h, int j, int f,\n"
1598                "       int c, int ddddddddddddd) {}");
1599 }
1600 
1601 TEST_F(FormatTest, FormatsAwesomeMethodCall) {
1602   verifyFormat(
1603       "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
1604       "                       parameter, parameter, parameter)),\n"
1605       "                   SecondLongCall(parameter));");
1606 }
1607 
1608 TEST_F(FormatTest, PreventConfusingIndents) {
1609   verifyFormat(
1610       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1611       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
1612       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1613       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
1614   verifyFormat(
1615       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
1616       "    aaaaaaaaaaaaaaaaaaaaaaaa[\n"
1617       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n"
1618       "    aaaaaaaaaaaaaaaaaaaaaaaa];");
1619   verifyFormat(
1620       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
1621       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
1622       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
1623       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
1624   verifyFormat("int a = bbbb && ccc && fffff(\n"
1625                "#define A Just forcing a new line\n"
1626                "                           ddd);");
1627 }
1628 
1629 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
1630   verifyFormat(
1631       "bool aaaaaaa =\n"
1632       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
1633       "    bbbbbbbb();");
1634   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
1635                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
1636                "    ccccccccc == ddddddddddd;");
1637 
1638   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
1639                "                 aaaaaa) &&\n"
1640                "         bbbbbb && cccccc;");
1641   verifyFormat("Whitespaces.addUntouchableComment(\n"
1642                "    SourceMgr.getSpellingColumnNumber(\n"
1643                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
1644                "    1);");
1645 }
1646 
1647 TEST_F(FormatTest, ExpressionIndentation) {
1648   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1649                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1650                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
1651                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
1652                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
1653                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
1654                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
1655                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
1656                "                 ccccccccccccccccccccccccccccccccccccccccc;");
1657   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
1658                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1659                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
1660                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1661   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1662                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
1663                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
1664                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1665   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
1666                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
1667                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1668                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1669 }
1670 
1671 TEST_F(FormatTest, ConstructorInitializers) {
1672   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
1673   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
1674                getLLVMStyleWithColumns(45));
1675   verifyFormat("Constructor()\n"
1676                "    : Inttializer(FitsOnTheLine) {}",
1677                getLLVMStyleWithColumns(44));
1678   verifyFormat("Constructor()\n"
1679                "    : Inttializer(FitsOnTheLine) {}",
1680                getLLVMStyleWithColumns(43));
1681 
1682   verifyFormat(
1683       "SomeClass::Constructor()\n"
1684       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
1685 
1686   verifyFormat(
1687       "SomeClass::Constructor()\n"
1688       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1689       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
1690   verifyFormat(
1691       "SomeClass::Constructor()\n"
1692       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1693       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
1694 
1695   verifyFormat("Constructor()\n"
1696                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1697                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1698                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1699                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
1700 
1701   verifyFormat("Constructor()\n"
1702                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1703                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
1704 
1705   verifyFormat("Constructor(int Parameter = 0)\n"
1706                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
1707                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
1708 
1709   // Here a line could be saved by splitting the second initializer onto two
1710   // lines, but that is not desireable.
1711   verifyFormat("Constructor()\n"
1712                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
1713                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
1714                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
1715 
1716   FormatStyle OnePerLine = getLLVMStyle();
1717   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
1718   verifyFormat("SomeClass::Constructor()\n"
1719                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1720                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1721                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
1722                OnePerLine);
1723   verifyFormat("SomeClass::Constructor()\n"
1724                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
1725                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1726                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
1727                OnePerLine);
1728   verifyFormat("MyClass::MyClass(int var)\n"
1729                "    : some_var_(var),            // 4 space indent\n"
1730                "      some_other_var_(var + 1) { // lined up\n"
1731                "}",
1732                OnePerLine);
1733   verifyFormat("Constructor()\n"
1734                "    : aaaaa(aaaaaa),\n"
1735                "      aaaaa(aaaaaa),\n"
1736                "      aaaaa(aaaaaa),\n"
1737                "      aaaaa(aaaaaa),\n"
1738                "      aaaaa(aaaaaa) {}",
1739                OnePerLine);
1740   verifyFormat("Constructor()\n"
1741                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
1742                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
1743                OnePerLine);
1744 }
1745 
1746 TEST_F(FormatTest, MemoizationTests) {
1747   // This breaks if the memoization lookup does not take \c Indent and
1748   // \c LastSpace into account.
1749   verifyFormat(
1750       "extern CFRunLoopTimerRef\n"
1751       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
1752       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
1753       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
1754       "                     CFRunLoopTimerContext *context) {}");
1755 
1756   // Deep nesting somewhat works around our memoization.
1757   verifyFormat(
1758       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
1759       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
1760       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
1761       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
1762       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
1763       getLLVMStyleWithColumns(65));
1764 
1765   // This test takes VERY long when memoization is broken.
1766   FormatStyle OnePerLine = getLLVMStyle();
1767   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
1768   OnePerLine.BinPackParameters = false;
1769   std::string input = "Constructor()\n"
1770                       "    : aaaa(a,\n";
1771   for (unsigned i = 0, e = 80; i != e; ++i) {
1772     input += "           a,\n";
1773   }
1774   input += "           a) {}";
1775   verifyFormat(input, OnePerLine);
1776 }
1777 
1778 TEST_F(FormatTest, BreaksAsHighAsPossible) {
1779   verifyFormat(
1780       "void f() {\n"
1781       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
1782       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
1783       "    f();\n"
1784       "}");
1785   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
1786                "    Intervals[i - 1].getRange().getLast()) {\n}");
1787 }
1788 
1789 TEST_F(FormatTest, BreaksFunctionDeclarations) {
1790   // Principially, we break function declarations in a certain order:
1791   // 1) break amongst arguments.
1792   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
1793                "                              Cccccccccccccc cccccccccccccc);");
1794 
1795   // 2) break after return type.
1796   verifyFormat(
1797       "Aaaaaaaaaaaaaaaaaaaaaaaa\n"
1798       "    bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
1799 
1800   // 3) break after (.
1801   verifyFormat(
1802       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
1803       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
1804 
1805   // 4) break before after nested name specifiers.
1806   verifyFormat(
1807       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1808       "    SomeClasssssssssssssssssssssssssssssssssssssss::\n"
1809       "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
1810 
1811   // However, there are exceptions, if a sufficient amount of lines can be
1812   // saved.
1813   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
1814   // more adjusting.
1815   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
1816                "                                  Cccccccccccccc cccccccccc,\n"
1817                "                                  Cccccccccccccc cccccccccc,\n"
1818                "                                  Cccccccccccccc cccccccccc,\n"
1819                "                                  Cccccccccccccc cccccccccc);");
1820   verifyFormat(
1821       "Aaaaaaaaaaaaaaaaaa\n"
1822       "    bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
1823       "                Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
1824       "                Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
1825   verifyFormat(
1826       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
1827       "                                          Cccccccccccccc cccccccccc,\n"
1828       "                                          Cccccccccccccc cccccccccc,\n"
1829       "                                          Cccccccccccccc cccccccccc,\n"
1830       "                                          Cccccccccccccc cccccccccc,\n"
1831       "                                          Cccccccccccccc cccccccccc,\n"
1832       "                                          Cccccccccccccc cccccccccc);");
1833   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
1834                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
1835                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
1836                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
1837                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
1838 
1839   // Break after multi-line parameters.
1840   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1841                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1842                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1843                "    bbbb bbbb);");
1844 }
1845 
1846 TEST_F(FormatTest, BreaksDesireably) {
1847   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
1848                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
1849                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
1850   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1851                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1852                "}");
1853 
1854   verifyFormat(
1855       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1856       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
1857 
1858   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1859                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1860                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1861 
1862   verifyFormat(
1863       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1864       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1865       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1866       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
1867 
1868   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1869                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1870 
1871   verifyFormat(
1872       "void f() {\n"
1873       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1874       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
1875       "}");
1876   verifyFormat(
1877       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1878       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1879   verifyFormat(
1880       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1881       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1882   verifyFormat(
1883       "aaaaaaaaaaaaaaaaa(\n"
1884       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1885       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1886 
1887   // This test case breaks on an incorrect memoization, i.e. an optimization not
1888   // taking into account the StopAt value.
1889   verifyFormat(
1890       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1891       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1892       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1893       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1894 
1895   verifyFormat("{\n  {\n    {\n"
1896                "      Annotation.SpaceRequiredBefore =\n"
1897                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
1898                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
1899                "    }\n  }\n}");
1900 }
1901 
1902 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
1903   FormatStyle NoBinPacking = getGoogleStyle();
1904   NoBinPacking.BinPackParameters = false;
1905   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
1906                "  aaaaaaaaaaaaaaaaaaaa,\n"
1907                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
1908                NoBinPacking);
1909   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
1910                "        aaaaaaaaaaaaa,\n"
1911                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
1912                NoBinPacking);
1913   verifyFormat(
1914       "aaaaaaaa(aaaaaaaaaaaaa,\n"
1915       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1916       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1917       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1918       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
1919       NoBinPacking);
1920   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
1921                "    .aaaaaaaaaaaaaaaaaa();",
1922                NoBinPacking);
1923   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1924                "    aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
1925                NoBinPacking);
1926 
1927   verifyFormat(
1928       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1929       "             aaaaaaaaaaaa,\n"
1930       "             aaaaaaaaaaaa);",
1931       NoBinPacking);
1932   verifyFormat(
1933       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
1934       "                               ddddddddddddddddddddddddddddd),\n"
1935       "             test);",
1936       NoBinPacking);
1937 
1938   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
1939                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
1940                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
1941                NoBinPacking);
1942   verifyFormat("a(\"a\"\n"
1943                "  \"a\",\n"
1944                "  a);");
1945 
1946   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
1947   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
1948                "                aaaaaaaaa,\n"
1949                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
1950                NoBinPacking);
1951   verifyFormat(
1952       "void f() {\n"
1953       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
1954       "      .aaaaaaa();\n"
1955       "}",
1956       NoBinPacking);
1957 }
1958 
1959 TEST_F(FormatTest, FormatsBuilderPattern) {
1960   verifyFormat(
1961       "return llvm::StringSwitch<Reference::Kind>(name)\n"
1962       "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
1963       "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
1964       "    .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
1965       "    .Default(ORDER_TEXT);\n");
1966 
1967   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
1968                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
1969   verifyFormat(
1970       "aaaaaaa->aaaaaaa\n"
1971       "    ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1972       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
1973   verifyFormat(
1974       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
1975       "    aaaaaaaaaaaaaa);");
1976   verifyFormat(
1977       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
1978       "    ->aaaaaaaaaaaaaaaa(\n"
1979       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1980       "    ->aaaaaaaaaaaaaaaaa();");
1981 }
1982 
1983 TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
1984   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1985                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
1986   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1987                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
1988   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1989                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
1990   verifyFormat(
1991       "void aaaaaaaaaaaaaaaaaa()\n"
1992       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
1993       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
1994   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1995                "    __attribute__((unused));");
1996   verifyFormat(
1997       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1998       "    GUARDED_BY(aaaaaaaaaaaa);");
1999 }
2000 
2001 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
2002   verifyFormat(
2003       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2004       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
2005   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
2006                "    ccccccccccccccccccccccccc) {\n}");
2007   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
2008                "    ccccccccccccccccccccccccc) {\n}");
2009   verifyFormat(
2010       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
2011       "    ccccccccccccccccccccccccc) {\n}");
2012   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
2013                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
2014                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
2015                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
2016   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
2017                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
2018                "    aaaaaaaaaaaaaaa != aa) {\n}");
2019 }
2020 
2021 TEST_F(FormatTest, BreaksAfterAssignments) {
2022   verifyFormat(
2023       "unsigned Cost =\n"
2024       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
2025       "                        SI->getPointerAddressSpaceee());\n");
2026   verifyFormat(
2027       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
2028       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
2029 
2030   verifyFormat(
2031       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n"
2032       "    .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
2033 }
2034 
2035 TEST_F(FormatTest, AlignsAfterAssignments) {
2036   verifyFormat(
2037       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2038       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
2039   verifyFormat(
2040       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2041       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
2042   verifyFormat(
2043       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2044       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
2045   verifyFormat(
2046       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2047       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
2048   verifyFormat("double LooooooooooooooooooooooooongResult =\n"
2049                "    aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
2050                "    aaaaaaaaaaaaaaaaaaaaaaaa;");
2051 }
2052 
2053 TEST_F(FormatTest, AlignsAfterReturn) {
2054   verifyFormat(
2055       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2056       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
2057   verifyFormat(
2058       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2059       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
2060   verifyFormat(
2061       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
2062       "       aaaaaaaaaaaaaaaaaaaaaa();");
2063   verifyFormat(
2064       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
2065       "        aaaaaaaaaaaaaaaaaaaaaa());");
2066   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2067                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2068   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2069                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
2070                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2071 }
2072 
2073 TEST_F(FormatTest, BreaksConditionalExpressions) {
2074   verifyFormat(
2075       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
2076       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2077       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2078   verifyFormat(
2079       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2080       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2081   verifyFormat(
2082       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
2083       "                                                    : aaaaaaaaaaaaa);");
2084   verifyFormat(
2085       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2086       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2087       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2088       "                   aaaaaaaaaaaaa);");
2089   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2090                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2091                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2092                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2093                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2094   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2095                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2096                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2097                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2098                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2099                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2100                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2101 
2102   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2103                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2104                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2105   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
2106                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2107                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2108                "        : aaaaaaaaaaaaaaaa;");
2109   verifyFormat(
2110       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2111       "    ? aaaaaaaaaaaaaaa\n"
2112       "    : aaaaaaaaaaaaaaa;");
2113   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
2114                "          aaaaaaaaa\n"
2115                "      ? b\n"
2116                "      : c);");
2117   verifyFormat(
2118       "unsigned Indent =\n"
2119       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
2120       "                              ? IndentForLevel[TheLine.Level]\n"
2121       "                              : TheLine * 2,\n"
2122       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
2123       getLLVMStyleWithColumns(70));
2124 
2125   FormatStyle NoBinPacking = getLLVMStyle();
2126   NoBinPacking.BinPackParameters = false;
2127   verifyFormat(
2128       "void f() {\n"
2129       "  g(aaa,\n"
2130       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
2131       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2132       "        ? aaaaaaaaaaaaaaa\n"
2133       "        : aaaaaaaaaaaaaaa);\n"
2134       "}",
2135       NoBinPacking);
2136 }
2137 
2138 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
2139   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
2140                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
2141   verifyFormat("bool a = true, b = false;");
2142 
2143   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2144                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
2145                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
2146                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
2147   verifyFormat(
2148       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2149       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
2150       "     d = e && f;");
2151   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
2152                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
2153   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
2154                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
2155   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
2156                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
2157   // FIXME: If multiple variables are defined, the "*" needs to move to the new
2158   // line. Also fix indent for breaking after the type, this looks bad.
2159   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2160                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
2161                "   *b = bbbbbbbbbbbbbbbbbbb;");
2162 
2163   // Not ideal, but pointer-with-type does not allow much here.
2164   verifyGoogleFormat(
2165       "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
2166       "           *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
2167 }
2168 
2169 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
2170   verifyFormat("arr[foo ? bar : baz];");
2171   verifyFormat("f()[foo ? bar : baz];");
2172   verifyFormat("(a + b)[foo ? bar : baz];");
2173   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
2174 }
2175 
2176 TEST_F(FormatTest, AlignsStringLiterals) {
2177   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
2178                "                                      \"short literal\");");
2179   verifyFormat(
2180       "looooooooooooooooooooooooongFunction(\n"
2181       "    \"short literal\"\n"
2182       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
2183   verifyFormat("someFunction(\"Always break between multi-line\"\n"
2184                "             \" string literals\",\n"
2185                "             and, other, parameters);");
2186   EXPECT_EQ("fun + \"1243\" /* comment */\n"
2187             "      \"5678\";",
2188             format("fun + \"1243\" /* comment */\n"
2189                    "      \"5678\";",
2190                    getLLVMStyleWithColumns(28)));
2191   EXPECT_EQ(
2192       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
2193       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
2194       "         \"aaaaaaaaaaaaaaaa\";",
2195       format("aaaaaa ="
2196              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
2197              "aaaaaaaaaaaaaaaaaaaaa\" "
2198              "\"aaaaaaaaaaaaaaaa\";"));
2199   verifyFormat("a = a + \"a\"\n"
2200                "        \"a\"\n"
2201                "        \"a\";");
2202 
2203   verifyFormat(
2204       "#define LL_FORMAT \"ll\"\n"
2205       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
2206       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
2207 }
2208 
2209 TEST_F(FormatTest, AlignsPipes) {
2210   verifyFormat(
2211       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2212       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2213       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2214   verifyFormat(
2215       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
2216       "                     << aaaaaaaaaaaaaaaaaaaa;");
2217   verifyFormat(
2218       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2219       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2220   verifyFormat(
2221       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
2222       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
2223       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
2224   verifyFormat(
2225       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2226       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2227       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2228 
2229   verifyFormat("return out << \"somepacket = {\\n\"\n"
2230                "           << \"  aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
2231                "           << \"  bbbb = \" << pkt.bbbb << \"\\n\"\n"
2232                "           << \"  cccccc = \" << pkt.cccccc << \"\\n\"\n"
2233                "           << \"  ddd = [\" << pkt.ddd << \"]\\n\"\n"
2234                "           << \"}\";");
2235 
2236   verifyFormat(
2237       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
2238       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
2239       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
2240       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
2241       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
2242   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
2243                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
2244 
2245   verifyFormat(
2246       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2247       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2248 }
2249 
2250 TEST_F(FormatTest, UnderstandsEquals) {
2251   verifyFormat(
2252       "aaaaaaaaaaaaaaaaa =\n"
2253       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2254   verifyFormat(
2255       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2256       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2257   verifyFormat(
2258       "if (a) {\n"
2259       "  f();\n"
2260       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2261       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2262       "}");
2263 
2264   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2265                "        100000000 + 10000000) {\n}");
2266 }
2267 
2268 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
2269   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
2270                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
2271 
2272   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
2273                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
2274 
2275   verifyFormat(
2276       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
2277       "                                                          Parameter2);");
2278 
2279   verifyFormat(
2280       "ShortObject->shortFunction(\n"
2281       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
2282       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
2283 
2284   verifyFormat("loooooooooooooongFunction(\n"
2285                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
2286 
2287   verifyFormat(
2288       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
2289       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
2290 
2291   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
2292                "    .WillRepeatedly(Return(SomeValue));");
2293   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
2294                "    .insert(ccccccccccccccccccccccc);");
2295   verifyFormat(
2296       "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2297       "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2298       "    .aaaaaaaaaaaaaaa(\n"
2299       "         aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2300       "            aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
2301   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2302                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2303                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2304                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
2305                "}");
2306 
2307   // Here, it is not necessary to wrap at "." or "->".
2308   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
2309                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2310   verifyFormat(
2311       "aaaaaaaaaaa->aaaaaaaaa(\n"
2312       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2313       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
2314 
2315   verifyFormat(
2316       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2317       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
2318   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
2319                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
2320   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
2321                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
2322 
2323   // FIXME: Should we break before .a()?
2324   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2325                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
2326 
2327   FormatStyle NoBinPacking = getLLVMStyle();
2328   NoBinPacking.BinPackParameters = false;
2329   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
2330                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
2331                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
2332                "                         aaaaaaaaaaaaaaaaaaa,\n"
2333                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2334                NoBinPacking);
2335 }
2336 
2337 TEST_F(FormatTest, WrapsTemplateDeclarations) {
2338   verifyFormat("template <typename T>\n"
2339                "virtual void loooooooooooongFunction(int Param1, int Param2);");
2340   verifyFormat(
2341       "template <typename T>\n"
2342       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
2343   verifyFormat("template <typename T>\n"
2344                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
2345                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
2346   verifyFormat(
2347       "template <typename T>\n"
2348       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
2349       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
2350   verifyFormat(
2351       "template <typename T>\n"
2352       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
2353       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
2354       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2355   verifyFormat("template <typename T>\n"
2356                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2357                "    int aaaaaaaaaaaaaaaaaaaaaa);");
2358   verifyFormat(
2359       "template <typename T1, typename T2 = char, typename T3 = char,\n"
2360       "          typename T4 = char>\n"
2361       "void f();");
2362   verifyFormat(
2363       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
2364       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2365 
2366   verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
2367                "    a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
2368 }
2369 
2370 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
2371   verifyFormat(
2372       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2373       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2374   verifyFormat(
2375       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2376       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2377       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
2378 
2379   // FIXME: Should we have the extra indent after the second break?
2380   verifyFormat(
2381       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2382       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2383       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2384 
2385   // FIXME: Look into whether we should indent 4 from the start or 4 from
2386   // "bbbbb..." here instead of what we are doing now.
2387   verifyFormat(
2388       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
2389       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
2390 
2391   // Breaking at nested name specifiers is generally not desirable.
2392   verifyFormat(
2393       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2394       "    aaaaaaaaaaaaaaaaaaaaaaa);");
2395 
2396   verifyFormat(
2397       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2398       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2399       "                   aaaaaaaaaaaaaaaaaaaaa);",
2400       getLLVMStyleWithColumns(74));
2401 
2402   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2403                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2404                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2405 }
2406 
2407 TEST_F(FormatTest, UnderstandsTemplateParameters) {
2408   verifyFormat("A<int> a;");
2409   verifyFormat("A<A<A<int> > > a;");
2410   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
2411   verifyFormat("bool x = a < 1 || 2 > a;");
2412   verifyFormat("bool x = 5 < f<int>();");
2413   verifyFormat("bool x = f<int>() > 5;");
2414   verifyFormat("bool x = 5 < a<int>::x;");
2415   verifyFormat("bool x = a < 4 ? a > 2 : false;");
2416   verifyFormat("bool x = f() ? a < 2 : a > 2;");
2417 
2418   verifyGoogleFormat("A<A<int>> a;");
2419   verifyGoogleFormat("A<A<A<int>>> a;");
2420   verifyGoogleFormat("A<A<A<A<int>>>> a;");
2421   verifyGoogleFormat("A<A<int> > a;");
2422   verifyGoogleFormat("A<A<A<int> > > a;");
2423   verifyGoogleFormat("A<A<A<A<int> > > > a;");
2424   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
2425   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
2426 
2427   verifyFormat("test >> a >> b;");
2428   verifyFormat("test << a >> b;");
2429 
2430   verifyFormat("f<int>();");
2431   verifyFormat("template <typename T> void f() {}");
2432 }
2433 
2434 TEST_F(FormatTest, UnderstandsBinaryOperators) {
2435   verifyFormat("COMPARE(a, ==, b);");
2436 }
2437 
2438 TEST_F(FormatTest, UnderstandsPointersToMembers) {
2439   verifyFormat("int A::*x;");
2440   verifyFormat("int (S::*func)(void *);");
2441   verifyFormat("typedef bool *(Class::*Member)() const;");
2442   verifyFormat("void f() {\n"
2443                "  (a->*f)();\n"
2444                "  a->*x;\n"
2445                "  (a.*f)();\n"
2446                "  ((*a).*f)();\n"
2447                "  a.*x;\n"
2448                "}");
2449   FormatStyle Style = getLLVMStyle();
2450   Style.PointerBindsToType = true;
2451   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
2452 }
2453 
2454 TEST_F(FormatTest, UnderstandsUnaryOperators) {
2455   verifyFormat("int a = -2;");
2456   verifyFormat("f(-1, -2, -3);");
2457   verifyFormat("a[-1] = 5;");
2458   verifyFormat("int a = 5 + -2;");
2459   verifyFormat("if (i == -1) {\n}");
2460   verifyFormat("if (i != -1) {\n}");
2461   verifyFormat("if (i > -1) {\n}");
2462   verifyFormat("if (i < -1) {\n}");
2463   verifyFormat("++(a->f());");
2464   verifyFormat("--(a->f());");
2465   verifyFormat("(a->f())++;");
2466   verifyFormat("a[42]++;");
2467   verifyFormat("if (!(a->f())) {\n}");
2468 
2469   verifyFormat("a-- > b;");
2470   verifyFormat("b ? -a : c;");
2471   verifyFormat("n * sizeof char16;");
2472   verifyFormat("n * alignof char16;");
2473   verifyFormat("sizeof(char);");
2474   verifyFormat("alignof(char);");
2475 
2476   verifyFormat("return -1;");
2477   verifyFormat("switch (a) {\n"
2478                "case -1:\n"
2479                "  break;\n"
2480                "}");
2481   verifyFormat("#define X -1");
2482   verifyFormat("#define X -kConstant");
2483 
2484   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
2485   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
2486 
2487   verifyFormat("int a = /* confusing comment */ -1;");
2488   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
2489   verifyFormat("int a = i /* confusing comment */++;");
2490 }
2491 
2492 TEST_F(FormatTest, UndestandsOverloadedOperators) {
2493   verifyFormat("bool operator<();");
2494   verifyFormat("bool operator>();");
2495   verifyFormat("bool operator=();");
2496   verifyFormat("bool operator==();");
2497   verifyFormat("bool operator!=();");
2498   verifyFormat("int operator+();");
2499   verifyFormat("int operator++();");
2500   verifyFormat("bool operator();");
2501   verifyFormat("bool operator()();");
2502   verifyFormat("bool operator[]();");
2503   verifyFormat("operator bool();");
2504   verifyFormat("operator int();");
2505   verifyFormat("operator void *();");
2506   verifyFormat("operator SomeType<int>();");
2507   verifyFormat("operator SomeType<int, int>();");
2508   verifyFormat("operator SomeType<SomeType<int> >();");
2509   verifyFormat("void *operator new(std::size_t size);");
2510   verifyFormat("void *operator new[](std::size_t size);");
2511   verifyFormat("void operator delete(void *ptr);");
2512   verifyFormat("void operator delete[](void *ptr);");
2513   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
2514                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
2515 
2516   verifyFormat(
2517       "ostream &operator<<(ostream &OutputStream,\n"
2518       "                    SomeReallyLongType WithSomeReallyLongValue);");
2519   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
2520                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
2521                "  return left.group < right.group;\n"
2522                "}");
2523   verifyFormat("SomeType &operator=(const SomeType &S);");
2524 
2525   verifyGoogleFormat("operator void*();");
2526   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
2527 }
2528 
2529 TEST_F(FormatTest, UnderstandsNewAndDelete) {
2530   verifyFormat("void f() {\n"
2531                "  A *a = new A;\n"
2532                "  A *a = new (placement) A;\n"
2533                "  delete a;\n"
2534                "  delete (A *)a;\n"
2535                "}");
2536 }
2537 
2538 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
2539   verifyFormat("int *f(int *a) {}");
2540   verifyFormat("int main(int argc, char **argv) {}");
2541   verifyFormat("Test::Test(int b) : a(b * b) {}");
2542   verifyIndependentOfContext("f(a, *a);");
2543   verifyFormat("void g() { f(*a); }");
2544   verifyIndependentOfContext("int a = b * 10;");
2545   verifyIndependentOfContext("int a = 10 * b;");
2546   verifyIndependentOfContext("int a = b * c;");
2547   verifyIndependentOfContext("int a += b * c;");
2548   verifyIndependentOfContext("int a -= b * c;");
2549   verifyIndependentOfContext("int a *= b * c;");
2550   verifyIndependentOfContext("int a /= b * c;");
2551   verifyIndependentOfContext("int a = *b;");
2552   verifyIndependentOfContext("int a = *b * c;");
2553   verifyIndependentOfContext("int a = b * *c;");
2554   verifyIndependentOfContext("return 10 * b;");
2555   verifyIndependentOfContext("return *b * *c;");
2556   verifyIndependentOfContext("return a & ~b;");
2557   verifyIndependentOfContext("f(b ? *c : *d);");
2558   verifyIndependentOfContext("int a = b ? *c : *d;");
2559   verifyIndependentOfContext("*b = a;");
2560   verifyIndependentOfContext("a * ~b;");
2561   verifyIndependentOfContext("a * !b;");
2562   verifyIndependentOfContext("a * +b;");
2563   verifyIndependentOfContext("a * -b;");
2564   verifyIndependentOfContext("a * ++b;");
2565   verifyIndependentOfContext("a * --b;");
2566   verifyIndependentOfContext("a[4] * b;");
2567   verifyIndependentOfContext("a[a * a] = 1;");
2568   verifyIndependentOfContext("f() * b;");
2569   verifyIndependentOfContext("a * [self dostuff];");
2570   verifyIndependentOfContext("int x = a * (a + b);");
2571   verifyIndependentOfContext("(a *)(a + b);");
2572   verifyIndependentOfContext("int *pa = (int *)&a;");
2573   verifyIndependentOfContext("return sizeof(int **);");
2574   verifyIndependentOfContext("return sizeof(int ******);");
2575   verifyIndependentOfContext("return (int **&)a;");
2576   verifyFormat("void f(Type (*parameter)[10]) {}");
2577   verifyGoogleFormat("return sizeof(int**);");
2578   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
2579   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
2580   // FIXME: The newline is wrong.
2581   verifyFormat("auto a = [](int **&, int ***) {}\n;");
2582 
2583   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
2584 
2585   verifyIndependentOfContext("A<int *> a;");
2586   verifyIndependentOfContext("A<int **> a;");
2587   verifyIndependentOfContext("A<int *, int *> a;");
2588   verifyIndependentOfContext(
2589       "const char *const p = reinterpret_cast<const char *const>(q);");
2590   verifyIndependentOfContext("A<int **, int **> a;");
2591   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
2592   verifyFormat("for (char **a = b; *a; ++a) {\n}");
2593   verifyFormat("for (; a && b;) {\n}");
2594 
2595   verifyFormat(
2596       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2597       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2598 
2599   verifyGoogleFormat("int main(int argc, char** argv) {}");
2600   verifyGoogleFormat("A<int*> a;");
2601   verifyGoogleFormat("A<int**> a;");
2602   verifyGoogleFormat("A<int*, int*> a;");
2603   verifyGoogleFormat("A<int**, int**> a;");
2604   verifyGoogleFormat("f(b ? *c : *d);");
2605   verifyGoogleFormat("int a = b ? *c : *d;");
2606   verifyGoogleFormat("Type* t = **x;");
2607   verifyGoogleFormat("Type* t = *++*x;");
2608   verifyGoogleFormat("*++*x;");
2609   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
2610   verifyGoogleFormat("Type* t = x++ * y;");
2611   verifyGoogleFormat(
2612       "const char* const p = reinterpret_cast<const char* const>(q);");
2613 
2614   verifyIndependentOfContext("a = *(x + y);");
2615   verifyIndependentOfContext("a = &(x + y);");
2616   verifyIndependentOfContext("*(x + y).call();");
2617   verifyIndependentOfContext("&(x + y)->call();");
2618   verifyFormat("void f() { &(*I).first; }");
2619 
2620   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
2621   verifyFormat(
2622       "int *MyValues = {\n"
2623       "  *A, // Operator detection might be confused by the '{'\n"
2624       "  *BB // Operator detection might be confused by previous comment\n"
2625       "};");
2626 
2627   verifyIndependentOfContext("if (int *a = &b)");
2628   verifyIndependentOfContext("if (int &a = *b)");
2629   verifyIndependentOfContext("if (a & b[i])");
2630   verifyIndependentOfContext("if (a::b::c::d & b[i])");
2631   verifyIndependentOfContext("if (*b[i])");
2632   verifyIndependentOfContext("if (int *a = (&b))");
2633   verifyIndependentOfContext("while (int *a = &b)");
2634   verifyFormat("void f() {\n"
2635                "  for (const int &v : Values) {\n"
2636                "  }\n"
2637                "}");
2638   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
2639   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
2640 
2641   verifyIndependentOfContext("A = new SomeType *[Length];");
2642   verifyIndependentOfContext("A = new SomeType *[Length]();");
2643   verifyGoogleFormat("A = new SomeType* [Length]();");
2644   verifyGoogleFormat("A = new SomeType* [Length];");
2645   FormatStyle PointerLeft = getLLVMStyle();
2646   PointerLeft.PointerBindsToType = true;
2647   verifyFormat("delete *x;", PointerLeft);
2648 }
2649 
2650 TEST_F(FormatTest, UnderstandsEllipsis) {
2651   verifyFormat("int printf(const char *fmt, ...);");
2652   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
2653 }
2654 
2655 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
2656   EXPECT_EQ("int *a;\n"
2657             "int *a;\n"
2658             "int *a;",
2659             format("int *a;\n"
2660                    "int* a;\n"
2661                    "int *a;",
2662                    getGoogleStyle()));
2663   EXPECT_EQ("int* a;\n"
2664             "int* a;\n"
2665             "int* a;",
2666             format("int* a;\n"
2667                    "int* a;\n"
2668                    "int *a;",
2669                    getGoogleStyle()));
2670   EXPECT_EQ("int *a;\n"
2671             "int *a;\n"
2672             "int *a;",
2673             format("int *a;\n"
2674                    "int * a;\n"
2675                    "int *  a;",
2676                    getGoogleStyle()));
2677 }
2678 
2679 TEST_F(FormatTest, UnderstandsRvalueReferences) {
2680   verifyFormat("int f(int &&a) {}");
2681   verifyFormat("int f(int a, char &&b) {}");
2682   verifyFormat("void f() { int &&a = b; }");
2683   verifyGoogleFormat("int f(int a, char&& b) {}");
2684   verifyGoogleFormat("void f() { int&& a = b; }");
2685 
2686   // FIXME: These require somewhat deeper changes in template arguments
2687   // formatting.
2688   //  verifyIndependentOfContext("A<int &&> a;");
2689   //  verifyIndependentOfContext("A<int &&, int &&> a;");
2690   //  verifyGoogleFormat("A<int&&> a;");
2691   //  verifyGoogleFormat("A<int&&, int&&> a;");
2692 }
2693 
2694 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
2695   verifyFormat("void f() {\n"
2696                "  x[aaaaaaaaa -\n"
2697                "    b] = 23;\n"
2698                "}",
2699                getLLVMStyleWithColumns(15));
2700 }
2701 
2702 TEST_F(FormatTest, FormatsCasts) {
2703   verifyFormat("Type *A = static_cast<Type *>(P);");
2704   verifyFormat("Type *A = (Type *)P;");
2705   verifyFormat("Type *A = (vector<Type *, int *>)P;");
2706   verifyFormat("int a = (int)(2.0f);");
2707 
2708   // FIXME: These also need to be identified.
2709   verifyFormat("int a = (int) 2.0f;");
2710   verifyFormat("int a = (int) * b;");
2711 
2712   // These are not casts.
2713   verifyFormat("void f(int *) {}");
2714   verifyFormat("f(foo)->b;");
2715   verifyFormat("f(foo).b;");
2716   verifyFormat("f(foo)(b);");
2717   verifyFormat("f(foo)[b];");
2718   verifyFormat("[](foo) { return 4; }(bar)];");
2719   verifyFormat("(*funptr)(foo)[4];");
2720   verifyFormat("funptrs[4](foo)[4];");
2721   verifyFormat("void f(int *);");
2722   verifyFormat("void f(int *) = 0;");
2723   verifyFormat("void f(SmallVector<int>) {}");
2724   verifyFormat("void f(SmallVector<int>);");
2725   verifyFormat("void f(SmallVector<int>) = 0;");
2726   verifyFormat("void f(int i = (kValue) * kMask) {}");
2727   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
2728   verifyFormat("int a = sizeof(int) * b;");
2729   verifyFormat("int a = alignof(int) * b;");
2730 
2731   // These are not casts, but at some point were confused with casts.
2732   verifyFormat("virtual void foo(int *) override;");
2733   verifyFormat("virtual void foo(char &) const;");
2734   verifyFormat("virtual void foo(int *a, char *) const;");
2735   verifyFormat("int a = sizeof(int *) + b;");
2736   verifyFormat("int a = alignof(int *) + b;");
2737 }
2738 
2739 TEST_F(FormatTest, FormatsFunctionTypes) {
2740   verifyFormat("A<bool()> a;");
2741   verifyFormat("A<SomeType()> a;");
2742   verifyFormat("A<void(*)(int, std::string)> a;");
2743   verifyFormat("A<void *(int)>;");
2744   verifyFormat("void *(*a)(int *, SomeType *);");
2745 
2746   // FIXME: Inconsistent.
2747   verifyFormat("int (*func)(void *);");
2748   verifyFormat("void f() { int(*func)(void *); }");
2749 
2750   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
2751   verifyGoogleFormat("void* (*a)(int);");
2752 }
2753 
2754 TEST_F(FormatTest, BreaksLongDeclarations) {
2755   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
2756                "    AnotherNameForTheLongType;");
2757   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
2758                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
2759   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
2760                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
2761   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
2762                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
2763 
2764   // FIXME: Without the comment, this breaks after "(".
2765   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
2766                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
2767 
2768   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
2769                "                  int LoooooooooooooooooooongParam2) {}");
2770   verifyFormat(
2771       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
2772       "                                   SourceLocation L, IdentifierIn *II,\n"
2773       "                                   Type *T) {}");
2774   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
2775                "ReallyReallyLongFunctionName(\n"
2776                "    const std::string &SomeParameter,\n"
2777                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
2778                "        ReallyReallyLongParameterName,\n"
2779                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
2780                "        AnotherLongParameterName) {}");
2781   verifyFormat(
2782       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
2783       "    aaaaaaaaaaaaaaaaaaaaaaa;");
2784 
2785   verifyGoogleFormat(
2786       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
2787       "                                   SourceLocation L) {}");
2788   verifyGoogleFormat(
2789       "some_namespace::LongReturnType\n"
2790       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
2791       "    int first_long_parameter, int second_parameter) {}");
2792 
2793   verifyGoogleFormat("template <typename T>\n"
2794                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
2795                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
2796   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2797                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
2798 }
2799 
2800 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
2801   verifyFormat("(a)->b();");
2802   verifyFormat("--a;");
2803 }
2804 
2805 TEST_F(FormatTest, HandlesIncludeDirectives) {
2806   verifyFormat("#include <string>\n"
2807                "#include <a/b/c.h>\n"
2808                "#include \"a/b/string\"\n"
2809                "#include \"string.h\"\n"
2810                "#include \"string.h\"\n"
2811                "#include <a-a>\n"
2812                "#include < path with space >\n"
2813                "#include \"abc.h\" // this is included for ABC\n"
2814                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
2815                getLLVMStyleWithColumns(35));
2816 
2817   verifyFormat("#import <string>");
2818   verifyFormat("#import <a/b/c.h>");
2819   verifyFormat("#import \"a/b/string\"");
2820   verifyFormat("#import \"string.h\"");
2821   verifyFormat("#import \"string.h\"");
2822 }
2823 
2824 //===----------------------------------------------------------------------===//
2825 // Error recovery tests.
2826 //===----------------------------------------------------------------------===//
2827 
2828 TEST_F(FormatTest, IncompleteParameterLists) {
2829   FormatStyle NoBinPacking = getLLVMStyle();
2830   NoBinPacking.BinPackParameters = false;
2831   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
2832                "                        double *min_x,\n"
2833                "                        double *max_x,\n"
2834                "                        double *min_y,\n"
2835                "                        double *max_y,\n"
2836                "                        double *min_z,\n"
2837                "                        double *max_z, ) {}",
2838                NoBinPacking);
2839 }
2840 
2841 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
2842   verifyFormat("void f() { return; }\n42");
2843   verifyFormat("void f() {\n"
2844                "  if (0)\n"
2845                "    return;\n"
2846                "}\n"
2847                "42");
2848   verifyFormat("void f() { return }\n42");
2849   verifyFormat("void f() {\n"
2850                "  if (0)\n"
2851                "    return\n"
2852                "}\n"
2853                "42");
2854 }
2855 
2856 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
2857   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
2858   EXPECT_EQ("void f() {\n"
2859             "  if (a)\n"
2860             "    return\n"
2861             "}",
2862             format("void  f  (  )  {  if  ( a )  return  }"));
2863   EXPECT_EQ("namespace N { void f() }", format("namespace  N  {  void f()  }"));
2864   EXPECT_EQ("namespace N {\n"
2865             "void f() {}\n"
2866             "void g()\n"
2867             "}",
2868             format("namespace N  { void f( ) { } void g( ) }"));
2869 }
2870 
2871 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
2872   verifyFormat("int aaaaaaaa =\n"
2873                "    // Overlylongcomment\n"
2874                "    b;",
2875                getLLVMStyleWithColumns(20));
2876   verifyFormat("function(\n"
2877                "    ShortArgument,\n"
2878                "    LoooooooooooongArgument);\n",
2879                getLLVMStyleWithColumns(20));
2880 }
2881 
2882 TEST_F(FormatTest, IncorrectAccessSpecifier) {
2883   verifyFormat("public:");
2884   verifyFormat("class A {\n"
2885                "public\n"
2886                "  void f() {}\n"
2887                "};");
2888   verifyFormat("public\n"
2889                "int qwerty;");
2890   verifyFormat("public\n"
2891                "B {}");
2892   verifyFormat("public\n"
2893                "{}");
2894   verifyFormat("public\n"
2895                "B { int x; }");
2896 }
2897 
2898 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
2899   verifyFormat("{");
2900   verifyFormat("#})");
2901 }
2902 
2903 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
2904   verifyFormat("do {\n}");
2905   verifyFormat("do {\n}\n"
2906                "f();");
2907   verifyFormat("do {\n}\n"
2908                "wheeee(fun);");
2909   verifyFormat("do {\n"
2910                "  f();\n"
2911                "}");
2912 }
2913 
2914 TEST_F(FormatTest, IncorrectCodeMissingParens) {
2915   verifyFormat("if {\n  foo;\n  foo();\n}");
2916   verifyFormat("switch {\n  foo;\n  foo();\n}");
2917   verifyFormat("for {\n  foo;\n  foo();\n}");
2918   verifyFormat("while {\n  foo;\n  foo();\n}");
2919   verifyFormat("do {\n  foo;\n  foo();\n} while;");
2920 }
2921 
2922 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
2923   verifyFormat("namespace {\n"
2924                "class Foo {  Foo  ( }; }  // comment");
2925 }
2926 
2927 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
2928   EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
2929   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
2930   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
2931   EXPECT_EQ("{\n  {}\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
2932 
2933   EXPECT_EQ("{\n"
2934             "    {\n"
2935             " breakme(\n"
2936             "     qwe);\n"
2937             "}\n",
2938             format("{\n"
2939                    "    {\n"
2940                    " breakme(qwe);\n"
2941                    "}\n",
2942                    getLLVMStyleWithColumns(10)));
2943 }
2944 
2945 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
2946   verifyFormat("int x = {\n"
2947                "  avariable,\n"
2948                "  b(alongervariable)\n"
2949                "};",
2950                getLLVMStyleWithColumns(25));
2951 }
2952 
2953 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
2954   verifyFormat("return (a)(b) { 1, 2, 3 };");
2955 }
2956 
2957 TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
2958   // FIXME: This is bad, find a better and more generic solution.
2959   verifyFormat(
2960       "Aaa({\n"
2961       "  int i;\n"
2962       "},\n"
2963       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2964       "                                     ccccccccccccccccc));");
2965 }
2966 
2967 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
2968   verifyFormat("void f() { return 42; }");
2969   verifyFormat("void f() {\n"
2970                "  // Comment\n"
2971                "}");
2972   verifyFormat("{\n"
2973                "#error {\n"
2974                "  int a;\n"
2975                "}");
2976   verifyFormat("{\n"
2977                "  int a;\n"
2978                "#error {\n"
2979                "}");
2980 
2981   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
2982   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
2983 
2984   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
2985   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
2986 }
2987 
2988 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
2989   // Elaborate type variable declarations.
2990   verifyFormat("struct foo a = { bar };\nint n;");
2991   verifyFormat("class foo a = { bar };\nint n;");
2992   verifyFormat("union foo a = { bar };\nint n;");
2993 
2994   // Elaborate types inside function definitions.
2995   verifyFormat("struct foo f() {}\nint n;");
2996   verifyFormat("class foo f() {}\nint n;");
2997   verifyFormat("union foo f() {}\nint n;");
2998 
2999   // Templates.
3000   verifyFormat("template <class X> void f() {}\nint n;");
3001   verifyFormat("template <struct X> void f() {}\nint n;");
3002   verifyFormat("template <union X> void f() {}\nint n;");
3003 
3004   // Actual definitions...
3005   verifyFormat("struct {\n} n;");
3006   verifyFormat(
3007       "template <template <class T, class Y>, class Z> class X {\n} n;");
3008   verifyFormat("union Z {\n  int n;\n} x;");
3009   verifyFormat("class MACRO Z {\n} n;");
3010   verifyFormat("class MACRO(X) Z {\n} n;");
3011   verifyFormat("class __attribute__(X) Z {\n} n;");
3012   verifyFormat("class __declspec(X) Z {\n} n;");
3013   verifyFormat("class A##B##C {\n} n;");
3014 
3015   // Redefinition from nested context:
3016   verifyFormat("class A::B::C {\n} n;");
3017 
3018   // Template definitions.
3019   // FIXME: This is still incorrectly handled at the formatter side.
3020   verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
3021 
3022   // FIXME:
3023   // This now gets parsed incorrectly as class definition.
3024   // verifyFormat("class A<int> f() {\n}\nint n;");
3025 
3026   // Elaborate types where incorrectly parsing the structural element would
3027   // break the indent.
3028   verifyFormat("if (true)\n"
3029                "  class X x;\n"
3030                "else\n"
3031                "  f();\n");
3032 
3033   // This is simply incomplete. Formatting is not important, but must not crash.
3034   verifyFormat("class A:");
3035 }
3036 
3037 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
3038   verifyFormat("#error Leave     all         white!!!!! space* alone!\n");
3039   verifyFormat("#warning Leave     all         white!!!!! space* alone!\n");
3040   EXPECT_EQ("#error 1", format("  #  error   1"));
3041   EXPECT_EQ("#warning 1", format("  #  warning 1"));
3042 }
3043 
3044 TEST_F(FormatTest, FormatHashIfExpressions) {
3045   // FIXME: Come up with a better indentation for #elif.
3046   verifyFormat(
3047       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
3048       "    defined(BBBBBBBB)\n"
3049       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
3050       "    defined(BBBBBBBB)\n"
3051       "#endif",
3052       getLLVMStyleWithColumns(65));
3053 }
3054 
3055 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
3056   FormatStyle AllowsMergedIf = getGoogleStyle();
3057   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
3058   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
3059   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
3060   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
3061   EXPECT_EQ("if (true) return 42;",
3062             format("if (true)\nreturn 42;", AllowsMergedIf));
3063   FormatStyle ShortMergedIf = AllowsMergedIf;
3064   ShortMergedIf.ColumnLimit = 25;
3065   verifyFormat("#define A \\\n"
3066                "  if (true) return 42;",
3067                ShortMergedIf);
3068   verifyFormat("#define A \\\n"
3069                "  f();    \\\n"
3070                "  if (true)\n"
3071                "#define B",
3072                ShortMergedIf);
3073   verifyFormat("#define A \\\n"
3074                "  f();    \\\n"
3075                "  if (true)\n"
3076                "g();",
3077                ShortMergedIf);
3078   verifyFormat("{\n"
3079                "#ifdef A\n"
3080                "  // Comment\n"
3081                "  if (true) continue;\n"
3082                "#endif\n"
3083                "  // Comment\n"
3084                "  if (true) continue;",
3085                ShortMergedIf);
3086 }
3087 
3088 TEST_F(FormatTest, BlockCommentsInControlLoops) {
3089   verifyFormat("if (0) /* a comment in a strange place */ {\n"
3090                "  f();\n"
3091                "}");
3092   verifyFormat("if (0) /* a comment in a strange place */ {\n"
3093                "  f();\n"
3094                "} /* another comment */ else /* comment #3 */ {\n"
3095                "  g();\n"
3096                "}");
3097   verifyFormat("while (0) /* a comment in a strange place */ {\n"
3098                "  f();\n"
3099                "}");
3100   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
3101                "  f();\n"
3102                "}");
3103   verifyFormat("do /* a comment in a strange place */ {\n"
3104                "  f();\n"
3105                "} /* another comment */ while (0);");
3106 }
3107 
3108 TEST_F(FormatTest, BlockComments) {
3109   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
3110             format("/* *//* */  /* */\n/* *//* */  /* */"));
3111   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
3112   EXPECT_EQ("#define A /*123*/\\\n"
3113             "  b\n"
3114             "/* */\n"
3115             "someCall(\n"
3116             "    parameter);",
3117             format("#define A /*123*/ b\n"
3118                    "/* */\n"
3119                    "someCall(parameter);",
3120                    getLLVMStyleWithColumns(15)));
3121 
3122   EXPECT_EQ("#define A\n"
3123             "/* */ someCall(\n"
3124             "    parameter);",
3125             format("#define A\n"
3126                    "/* */someCall(parameter);",
3127                    getLLVMStyleWithColumns(15)));
3128 
3129   FormatStyle NoBinPacking = getLLVMStyle();
3130   NoBinPacking.BinPackParameters = false;
3131   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
3132             "             2, /* comment 2 */\n"
3133             "             3, /* comment 3 */\n"
3134             "             aaaa,\n"
3135             "             bbbb);",
3136             format("someFunction (1,   /* comment 1 */\n"
3137                    "                2, /* comment 2 */  \n"
3138                    "               3,   /* comment 3 */\n"
3139                    "aaaa, bbbb );",
3140                    NoBinPacking));
3141   verifyFormat(
3142       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3143       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3144   EXPECT_EQ(
3145       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
3146       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3147       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
3148       format(
3149           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
3150           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
3151           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
3152   EXPECT_EQ(
3153       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
3154       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
3155       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
3156       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
3157              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
3158              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
3159 }
3160 
3161 TEST_F(FormatTest, BlockCommentsInMacros) {
3162   EXPECT_EQ("#define A          \\\n"
3163             "  {                \\\n"
3164             "    /* one line */ \\\n"
3165             "    someCall();",
3166             format("#define A {        \\\n"
3167                    "  /* one line */   \\\n"
3168                    "  someCall();",
3169                    getLLVMStyleWithColumns(20)));
3170   EXPECT_EQ("#define A          \\\n"
3171             "  {                \\\n"
3172             "    /* previous */ \\\n"
3173             "    /* one line */ \\\n"
3174             "    someCall();",
3175             format("#define A {        \\\n"
3176                    "  /* previous */   \\\n"
3177                    "  /* one line */   \\\n"
3178                    "  someCall();",
3179                    getLLVMStyleWithColumns(20)));
3180 }
3181 
3182 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
3183   // FIXME: This is not what we want...
3184   verifyFormat("{\n"
3185                "// a"
3186                "// b");
3187 }
3188 
3189 TEST_F(FormatTest, FormatStarDependingOnContext) {
3190   verifyFormat("void f(int *a);");
3191   verifyFormat("void f() { f(fint * b); }");
3192   verifyFormat("class A {\n  void f(int *a);\n};");
3193   verifyFormat("class A {\n  int *a;\n};");
3194   verifyFormat("namespace a {\n"
3195                "namespace b {\n"
3196                "class A {\n"
3197                "  void f() {}\n"
3198                "  int *a;\n"
3199                "};\n"
3200                "}\n"
3201                "}");
3202 }
3203 
3204 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
3205   verifyFormat("while");
3206   verifyFormat("operator");
3207 }
3208 
3209 //===----------------------------------------------------------------------===//
3210 // Objective-C tests.
3211 //===----------------------------------------------------------------------===//
3212 
3213 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
3214   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
3215   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
3216             format("-(NSUInteger)indexOfObject:(id)anObject;"));
3217   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
3218   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
3219   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
3220             format("-(NSInteger)Method3:(id)anObject;"));
3221   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
3222             format("-(NSInteger)Method4:(id)anObject;"));
3223   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
3224             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
3225   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
3226             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
3227   EXPECT_EQ(
3228       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
3229       format(
3230           "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
3231 
3232   // Very long objectiveC method declaration.
3233   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
3234                "                    inRange:(NSRange)range\n"
3235                "                   outRange:(NSRange)out_range\n"
3236                "                  outRange1:(NSRange)out_range1\n"
3237                "                  outRange2:(NSRange)out_range2\n"
3238                "                  outRange3:(NSRange)out_range3\n"
3239                "                  outRange4:(NSRange)out_range4\n"
3240                "                  outRange5:(NSRange)out_range5\n"
3241                "                  outRange6:(NSRange)out_range6\n"
3242                "                  outRange7:(NSRange)out_range7\n"
3243                "                  outRange8:(NSRange)out_range8\n"
3244                "                  outRange9:(NSRange)out_range9;");
3245 
3246   verifyFormat("- (int)sum:(vector<int>)numbers;");
3247   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
3248   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
3249   // protocol lists (but not for template classes):
3250   //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
3251 
3252   verifyFormat("- (int(*)())foo:(int(*)())f;");
3253   verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
3254 
3255   // If there's no return type (very rare in practice!), LLVM and Google style
3256   // agree.
3257   verifyFormat("- foo;");
3258   verifyFormat("- foo:(int)f;");
3259   verifyGoogleFormat("- foo:(int)foo;");
3260 }
3261 
3262 TEST_F(FormatTest, FormatObjCBlocks) {
3263   verifyFormat("int (^Block)(int, int);");
3264   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
3265 }
3266 
3267 TEST_F(FormatTest, FormatObjCInterface) {
3268   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
3269                "@public\n"
3270                "  int field1;\n"
3271                "@protected\n"
3272                "  int field2;\n"
3273                "@private\n"
3274                "  int field3;\n"
3275                "@package\n"
3276                "  int field4;\n"
3277                "}\n"
3278                "+ (id)init;\n"
3279                "@end");
3280 
3281   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
3282                      " @public\n"
3283                      "  int field1;\n"
3284                      " @protected\n"
3285                      "  int field2;\n"
3286                      " @private\n"
3287                      "  int field3;\n"
3288                      " @package\n"
3289                      "  int field4;\n"
3290                      "}\n"
3291                      "+ (id)init;\n"
3292                      "@end");
3293 
3294   verifyFormat("@interface /* wait for it */ Foo\n"
3295                "+ (id)init;\n"
3296                "// Look, a comment!\n"
3297                "- (int)answerWith:(int)i;\n"
3298                "@end");
3299 
3300   verifyFormat("@interface Foo\n"
3301                "@end\n"
3302                "@interface Bar\n"
3303                "@end");
3304 
3305   verifyFormat("@interface Foo : Bar\n"
3306                "+ (id)init;\n"
3307                "@end");
3308 
3309   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
3310                "+ (id)init;\n"
3311                "@end");
3312 
3313   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
3314                      "+ (id)init;\n"
3315                      "@end");
3316 
3317   verifyFormat("@interface Foo (HackStuff)\n"
3318                "+ (id)init;\n"
3319                "@end");
3320 
3321   verifyFormat("@interface Foo ()\n"
3322                "+ (id)init;\n"
3323                "@end");
3324 
3325   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
3326                "+ (id)init;\n"
3327                "@end");
3328 
3329   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
3330                      "+ (id)init;\n"
3331                      "@end");
3332 
3333   verifyFormat("@interface Foo {\n"
3334                "  int _i;\n"
3335                "}\n"
3336                "+ (id)init;\n"
3337                "@end");
3338 
3339   verifyFormat("@interface Foo : Bar {\n"
3340                "  int _i;\n"
3341                "}\n"
3342                "+ (id)init;\n"
3343                "@end");
3344 
3345   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
3346                "  int _i;\n"
3347                "}\n"
3348                "+ (id)init;\n"
3349                "@end");
3350 
3351   verifyFormat("@interface Foo (HackStuff) {\n"
3352                "  int _i;\n"
3353                "}\n"
3354                "+ (id)init;\n"
3355                "@end");
3356 
3357   verifyFormat("@interface Foo () {\n"
3358                "  int _i;\n"
3359                "}\n"
3360                "+ (id)init;\n"
3361                "@end");
3362 
3363   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
3364                "  int _i;\n"
3365                "}\n"
3366                "+ (id)init;\n"
3367                "@end");
3368 }
3369 
3370 TEST_F(FormatTest, FormatObjCImplementation) {
3371   verifyFormat("@implementation Foo : NSObject {\n"
3372                "@public\n"
3373                "  int field1;\n"
3374                "@protected\n"
3375                "  int field2;\n"
3376                "@private\n"
3377                "  int field3;\n"
3378                "@package\n"
3379                "  int field4;\n"
3380                "}\n"
3381                "+ (id)init {\n}\n"
3382                "@end");
3383 
3384   verifyGoogleFormat("@implementation Foo : NSObject {\n"
3385                      " @public\n"
3386                      "  int field1;\n"
3387                      " @protected\n"
3388                      "  int field2;\n"
3389                      " @private\n"
3390                      "  int field3;\n"
3391                      " @package\n"
3392                      "  int field4;\n"
3393                      "}\n"
3394                      "+ (id)init {\n}\n"
3395                      "@end");
3396 
3397   verifyFormat("@implementation Foo\n"
3398                "+ (id)init {\n"
3399                "  if (true)\n"
3400                "    return nil;\n"
3401                "}\n"
3402                "// Look, a comment!\n"
3403                "- (int)answerWith:(int)i {\n"
3404                "  return i;\n"
3405                "}\n"
3406                "+ (int)answerWith:(int)i {\n"
3407                "  return i;\n"
3408                "}\n"
3409                "@end");
3410 
3411   verifyFormat("@implementation Foo\n"
3412                "@end\n"
3413                "@implementation Bar\n"
3414                "@end");
3415 
3416   verifyFormat("@implementation Foo : Bar\n"
3417                "+ (id)init {\n}\n"
3418                "- (void)foo {\n}\n"
3419                "@end");
3420 
3421   verifyFormat("@implementation Foo {\n"
3422                "  int _i;\n"
3423                "}\n"
3424                "+ (id)init {\n}\n"
3425                "@end");
3426 
3427   verifyFormat("@implementation Foo : Bar {\n"
3428                "  int _i;\n"
3429                "}\n"
3430                "+ (id)init {\n}\n"
3431                "@end");
3432 
3433   verifyFormat("@implementation Foo (HackStuff)\n"
3434                "+ (id)init {\n}\n"
3435                "@end");
3436 }
3437 
3438 TEST_F(FormatTest, FormatObjCProtocol) {
3439   verifyFormat("@protocol Foo\n"
3440                "@property(weak) id delegate;\n"
3441                "- (NSUInteger)numberOfThings;\n"
3442                "@end");
3443 
3444   verifyFormat("@protocol MyProtocol <NSObject>\n"
3445                "- (NSUInteger)numberOfThings;\n"
3446                "@end");
3447 
3448   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
3449                      "- (NSUInteger)numberOfThings;\n"
3450                      "@end");
3451 
3452   verifyFormat("@protocol Foo;\n"
3453                "@protocol Bar;\n");
3454 
3455   verifyFormat("@protocol Foo\n"
3456                "@end\n"
3457                "@protocol Bar\n"
3458                "@end");
3459 
3460   verifyFormat("@protocol myProtocol\n"
3461                "- (void)mandatoryWithInt:(int)i;\n"
3462                "@optional\n"
3463                "- (void)optional;\n"
3464                "@required\n"
3465                "- (void)required;\n"
3466                "@optional\n"
3467                "@property(assign) int madProp;\n"
3468                "@end\n");
3469 }
3470 
3471 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
3472   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
3473                "                   rect:(NSRect)theRect\n"
3474                "               interval:(float)theInterval {\n"
3475                "}");
3476   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
3477                "          longKeyword:(NSRect)theRect\n"
3478                "    evenLongerKeyword:(float)theInterval\n"
3479                "                error:(NSError **)theError {\n"
3480                "}");
3481 }
3482 
3483 TEST_F(FormatTest, FormatObjCMethodExpr) {
3484   verifyFormat("[foo bar:baz];");
3485   verifyFormat("return [foo bar:baz];");
3486   verifyFormat("f([foo bar:baz]);");
3487   verifyFormat("f(2, [foo bar:baz]);");
3488   verifyFormat("f(2, a ? b : c);");
3489   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
3490 
3491   // Unary operators.
3492   verifyFormat("int a = +[foo bar:baz];");
3493   verifyFormat("int a = -[foo bar:baz];");
3494   verifyFormat("int a = ![foo bar:baz];");
3495   verifyFormat("int a = ~[foo bar:baz];");
3496   verifyFormat("int a = ++[foo bar:baz];");
3497   verifyFormat("int a = --[foo bar:baz];");
3498   verifyFormat("int a = sizeof [foo bar:baz];");
3499   verifyFormat("int a = alignof [foo bar:baz];");
3500   verifyFormat("int a = &[foo bar:baz];");
3501   verifyFormat("int a = *[foo bar:baz];");
3502   // FIXME: Make casts work, without breaking f()[4].
3503   //verifyFormat("int a = (int)[foo bar:baz];");
3504   //verifyFormat("return (int)[foo bar:baz];");
3505   //verifyFormat("(void)[foo bar:baz];");
3506   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
3507 
3508   // Binary operators.
3509   verifyFormat("[foo bar:baz], [foo bar:baz];");
3510   verifyFormat("[foo bar:baz] = [foo bar:baz];");
3511   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
3512   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
3513   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
3514   verifyFormat("[foo bar:baz] += [foo bar:baz];");
3515   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
3516   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
3517   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
3518   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
3519   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
3520   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
3521   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
3522   verifyFormat("[foo bar:baz] || [foo bar:baz];");
3523   verifyFormat("[foo bar:baz] && [foo bar:baz];");
3524   verifyFormat("[foo bar:baz] | [foo bar:baz];");
3525   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
3526   verifyFormat("[foo bar:baz] & [foo bar:baz];");
3527   verifyFormat("[foo bar:baz] == [foo bar:baz];");
3528   verifyFormat("[foo bar:baz] != [foo bar:baz];");
3529   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
3530   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
3531   verifyFormat("[foo bar:baz] > [foo bar:baz];");
3532   verifyFormat("[foo bar:baz] < [foo bar:baz];");
3533   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
3534   verifyFormat("[foo bar:baz] << [foo bar:baz];");
3535   verifyFormat("[foo bar:baz] - [foo bar:baz];");
3536   verifyFormat("[foo bar:baz] + [foo bar:baz];");
3537   verifyFormat("[foo bar:baz] * [foo bar:baz];");
3538   verifyFormat("[foo bar:baz] / [foo bar:baz];");
3539   verifyFormat("[foo bar:baz] % [foo bar:baz];");
3540   // Whew!
3541 
3542   verifyFormat("return in[42];");
3543   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
3544                "}");
3545 
3546   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
3547   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
3548   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
3549   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
3550   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
3551   verifyFormat("[button setAction:@selector(zoomOut:)];");
3552   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
3553 
3554   verifyFormat("arr[[self indexForFoo:a]];");
3555   verifyFormat("throw [self errorFor:a];");
3556   verifyFormat("@throw [self errorFor:a];");
3557 
3558   // This tests that the formatter doesn't break after "backing" but before ":",
3559   // which would be at 80 columns.
3560   verifyFormat(
3561       "void f() {\n"
3562       "  if ((self = [super initWithContentRect:contentRect\n"
3563       "                               styleMask:styleMask\n"
3564       "                                 backing:NSBackingStoreBuffered\n"
3565       "                                   defer:YES]))");
3566 
3567   verifyFormat(
3568       "[foo checkThatBreakingAfterColonWorksOk:\n"
3569       "        [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
3570 
3571   verifyFormat("[myObj short:arg1 // Force line break\n"
3572                "          longKeyword:arg2\n"
3573                "    evenLongerKeyword:arg3\n"
3574                "                error:arg4];");
3575   verifyFormat(
3576       "void f() {\n"
3577       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
3578       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
3579       "                                     pos.width(), pos.height())\n"
3580       "                styleMask:NSBorderlessWindowMask\n"
3581       "                  backing:NSBackingStoreBuffered\n"
3582       "                    defer:NO]);\n"
3583       "}");
3584   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
3585                "                             with:contentsNativeView];");
3586 
3587   verifyFormat(
3588       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
3589       "           owner:nillllll];");
3590 
3591   verifyFormat(
3592       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
3593       "        forType:kBookmarkButtonDragType];");
3594 
3595   verifyFormat("[defaultCenter addObserver:self\n"
3596                "                  selector:@selector(willEnterFullscreen)\n"
3597                "                      name:kWillEnterFullscreenNotification\n"
3598                "                    object:nil];");
3599   verifyFormat("[image_rep drawInRect:drawRect\n"
3600                "             fromRect:NSZeroRect\n"
3601                "            operation:NSCompositeCopy\n"
3602                "             fraction:1.0\n"
3603                "       respectFlipped:NO\n"
3604                "                hints:nil];");
3605 
3606   verifyFormat(
3607       "scoped_nsobject<NSTextField> message(\n"
3608       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
3609       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
3610 }
3611 
3612 TEST_F(FormatTest, ObjCAt) {
3613   verifyFormat("@autoreleasepool");
3614   verifyFormat("@catch");
3615   verifyFormat("@class");
3616   verifyFormat("@compatibility_alias");
3617   verifyFormat("@defs");
3618   verifyFormat("@dynamic");
3619   verifyFormat("@encode");
3620   verifyFormat("@end");
3621   verifyFormat("@finally");
3622   verifyFormat("@implementation");
3623   verifyFormat("@import");
3624   verifyFormat("@interface");
3625   verifyFormat("@optional");
3626   verifyFormat("@package");
3627   verifyFormat("@private");
3628   verifyFormat("@property");
3629   verifyFormat("@protected");
3630   verifyFormat("@protocol");
3631   verifyFormat("@public");
3632   verifyFormat("@required");
3633   verifyFormat("@selector");
3634   verifyFormat("@synchronized");
3635   verifyFormat("@synthesize");
3636   verifyFormat("@throw");
3637   verifyFormat("@try");
3638 
3639   EXPECT_EQ("@interface", format("@ interface"));
3640 
3641   // The precise formatting of this doesn't matter, nobody writes code like
3642   // this.
3643   verifyFormat("@ /*foo*/ interface");
3644 }
3645 
3646 TEST_F(FormatTest, ObjCSnippets) {
3647   verifyFormat("@autoreleasepool {\n"
3648                "  foo();\n"
3649                "}");
3650   verifyFormat("@class Foo, Bar;");
3651   verifyFormat("@compatibility_alias AliasName ExistingClass;");
3652   verifyFormat("@dynamic textColor;");
3653   verifyFormat("char *buf1 = @encode(int *);");
3654   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
3655   verifyFormat("char *buf1 = @encode(int **);");
3656   verifyFormat("Protocol *proto = @protocol(p1);");
3657   verifyFormat("SEL s = @selector(foo:);");
3658   verifyFormat("@synchronized(self) {\n"
3659                "  f();\n"
3660                "}");
3661 
3662   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
3663   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
3664 
3665   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
3666   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
3667   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
3668 }
3669 
3670 TEST_F(FormatTest, ObjCLiterals) {
3671   verifyFormat("@\"String\"");
3672   verifyFormat("@1");
3673   verifyFormat("@+4.8");
3674   verifyFormat("@-4");
3675   verifyFormat("@1LL");
3676   verifyFormat("@.5");
3677   verifyFormat("@'c'");
3678   verifyFormat("@true");
3679 
3680   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
3681   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
3682   verifyFormat("NSNumber *favoriteColor = @(Green);");
3683   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
3684 
3685   verifyFormat("@[");
3686   verifyFormat("@[]");
3687   verifyFormat(
3688       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
3689   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
3690 
3691   verifyFormat("@{");
3692   verifyFormat("@{}");
3693   verifyFormat("@{ @\"one\" : @1 }");
3694   verifyFormat("return @{ @\"one\" : @1 };");
3695   verifyFormat("@{ @\"one\" : @1, }");
3696 
3697   // FIXME: Breaking in cases where we think there's a structural error
3698   // showed that we're incorrectly parsing this code. We need to fix the
3699   // parsing here.
3700   verifyFormat("@{ @\"one\" : @\n"
3701                "{ @2 : @1 }\n"
3702                "}");
3703   verifyFormat("@{ @\"one\" : @\n"
3704                "{ @2 : @1 }\n"
3705                ",\n"
3706                "}");
3707 
3708   verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
3709   verifyFormat("[self setDict:@{}");
3710   verifyFormat("[self setDict:@{ @1 : @2 }");
3711   verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
3712   verifyFormat(
3713       "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
3714   verifyFormat(
3715       "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
3716 
3717   // FIXME: Nested and multi-line array and dictionary literals need more work.
3718   verifyFormat(
3719       "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
3720       "                     @\"processInfo\" : [NSProcessInfo processInfo] };");
3721 }
3722 
3723 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
3724   EXPECT_EQ("{\n"
3725             "{\n"
3726             "a;\n"
3727             "b;\n"
3728             "}\n"
3729             "}",
3730             format("{\n"
3731                    "{\n"
3732                    "a;\n"
3733                    "     b;\n"
3734                    "}\n"
3735                    "}",
3736                    13, 2, getLLVMStyle()));
3737   EXPECT_EQ("{\n"
3738             "{\n"
3739             "  a;\n"
3740             "b;\n"
3741             "}\n"
3742             "}",
3743             format("{\n"
3744                    "{\n"
3745                    "     a;\n"
3746                    "b;\n"
3747                    "}\n"
3748                    "}",
3749                    9, 2, getLLVMStyle()));
3750   EXPECT_EQ("{\n"
3751             "{\n"
3752             "public:\n"
3753             "  b;\n"
3754             "}\n"
3755             "}",
3756             format("{\n"
3757                    "{\n"
3758                    "public:\n"
3759                    "     b;\n"
3760                    "}\n"
3761                    "}",
3762                    17, 2, getLLVMStyle()));
3763   EXPECT_EQ("{\n"
3764             "{\n"
3765             "a;\n"
3766             "}\n"
3767             "{\n"
3768             "  b;\n"
3769             "}\n"
3770             "}",
3771             format("{\n"
3772                    "{\n"
3773                    "a;\n"
3774                    "}\n"
3775                    "{\n"
3776                    "           b;\n"
3777                    "}\n"
3778                    "}",
3779                    22, 2, getLLVMStyle()));
3780   EXPECT_EQ("  {\n"
3781             "    a;\n"
3782             "  }",
3783             format("  {\n"
3784                    "a;\n"
3785                    "  }",
3786                    4, 2, getLLVMStyle()));
3787   EXPECT_EQ("void f() {}\n"
3788             "void g() {}",
3789             format("void f() {}\n"
3790                    "void g() {}",
3791                    13, 0, getLLVMStyle()));
3792   EXPECT_EQ("int a; // comment\n"
3793             "       // line 2\n"
3794             "int b;",
3795             format("int a; // comment\n"
3796                    "       // line 2\n"
3797                    "  int b;",
3798                    35, 0, getLLVMStyle()));
3799 }
3800 
3801 TEST_F(FormatTest, BreakStringLiterals) {
3802   EXPECT_EQ("\"some text \"\n"
3803             "\"other\";",
3804             format("\"some text other\";", getLLVMStyleWithColumns(12)));
3805   EXPECT_EQ("\"some text \"\n"
3806             "\"other\";",
3807             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
3808   EXPECT_EQ(
3809       "#define A  \\\n"
3810       "  \"some \"  \\\n"
3811       "  \"text \"  \\\n"
3812       "  \"other\";",
3813       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
3814   EXPECT_EQ(
3815       "#define A  \\\n"
3816       "  \"so \"    \\\n"
3817       "  \"text \"  \\\n"
3818       "  \"other\";",
3819       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
3820 
3821   EXPECT_EQ("\"some text\"",
3822             format("\"some text\"", getLLVMStyleWithColumns(1)));
3823   EXPECT_EQ("\"some text\"",
3824             format("\"some text\"", getLLVMStyleWithColumns(11)));
3825   EXPECT_EQ("\"some \"\n"
3826             "\"text\"",
3827             format("\"some text\"", getLLVMStyleWithColumns(10)));
3828   EXPECT_EQ("\"some \"\n"
3829             "\"text\"",
3830             format("\"some text\"", getLLVMStyleWithColumns(7)));
3831   EXPECT_EQ("\"some\"\n"
3832             "\" tex\"\n"
3833             "\"t\"",
3834             format("\"some text\"", getLLVMStyleWithColumns(6)));
3835   EXPECT_EQ("\"some\"\n"
3836             "\" tex\"\n"
3837             "\" and\"",
3838             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
3839   EXPECT_EQ("\"some\"\n"
3840             "\"/tex\"\n"
3841             "\"/and\"",
3842             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
3843 
3844   EXPECT_EQ("variable =\n"
3845             "    \"long string \"\n"
3846             "    \"literal\";",
3847             format("variable = \"long string literal\";",
3848                    getLLVMStyleWithColumns(20)));
3849 
3850   EXPECT_EQ("variable = f(\n"
3851             "    \"long string \"\n"
3852             "    \"literal\",\n"
3853             "    short,\n"
3854             "    loooooooooooooooooooong);",
3855             format("variable = f(\"long string literal\", short, "
3856                    "loooooooooooooooooooong);",
3857                    getLLVMStyleWithColumns(20)));
3858   EXPECT_EQ(
3859       "f(\"one two\".split(\n"
3860       "    variable));",
3861       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
3862   EXPECT_EQ("f(\"one two three four five six \"\n"
3863             "  \"seven\".split(\n"
3864             "      really_looooong_variable));",
3865             format("f(\"one two three four five six seven\"."
3866                    "split(really_looooong_variable));",
3867                    getLLVMStyleWithColumns(33)));
3868 
3869   EXPECT_EQ("f(\"some \"\n"
3870             "  \"text\",\n"
3871             "  other);",
3872             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
3873 
3874   // Only break as a last resort.
3875   verifyFormat(
3876       "aaaaaaaaaaaaaaaaaaaa(\n"
3877       "    aaaaaaaaaaaaaaaaaaaa,\n"
3878       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
3879 
3880   EXPECT_EQ(
3881       "\"splitmea\"\n"
3882       "\"trandomp\"\n"
3883       "\"oint\"",
3884       format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
3885 
3886   EXPECT_EQ(
3887       "\"split/\"\n"
3888       "\"pathat/\"\n"
3889       "\"slashes\"",
3890       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
3891 
3892   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
3893   AlignLeft.AlignEscapedNewlinesLeft = true;
3894   EXPECT_EQ(
3895       "#define A \\\n"
3896       "  \"some \" \\\n"
3897       "  \"text \" \\\n"
3898       "  \"other\";",
3899       format("#define A \"some text other\";", AlignLeft));
3900 }
3901 
3902 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
3903   EXPECT_EQ("\"\\a\"",
3904             format("\"\\a\"", getLLVMStyleWithColumns(3)));
3905   EXPECT_EQ("\"\\\"",
3906             format("\"\\\"", getLLVMStyleWithColumns(2)));
3907   EXPECT_EQ("\"test\"\n"
3908             "\"\\n\"",
3909             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
3910   EXPECT_EQ("\"tes\\\\\"\n"
3911             "\"n\"",
3912             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
3913   EXPECT_EQ("\"\\\\\\\\\"\n"
3914             "\"\\n\"",
3915             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
3916   EXPECT_EQ("\"\\uff01\"",
3917             format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
3918   EXPECT_EQ("\"\\uff01\"\n"
3919             "\"test\"",
3920             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
3921   EXPECT_EQ("\"\\Uff01ff02\"",
3922             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
3923   EXPECT_EQ("\"\\x000000000001\"\n"
3924             "\"next\"",
3925             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
3926   EXPECT_EQ("\"\\x000000000001next\"",
3927             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
3928   EXPECT_EQ("\"\\x000000000001\"",
3929             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
3930   EXPECT_EQ("\"test\"\n"
3931             "\"\\000000\"\n"
3932             "\"000001\"",
3933             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
3934   EXPECT_EQ("\"test\\000\"\n"
3935             "\"00000000\"\n"
3936             "\"1\"",
3937             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
3938   EXPECT_EQ("R\"(\\x\\x00)\"\n",
3939             format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7)));
3940 }
3941 
3942 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
3943   verifyFormat("void f() {\n"
3944                "  return g() {}\n"
3945                "  void h() {}");
3946   verifyFormat("if (foo)\n"
3947                "  return { forgot_closing_brace();\n"
3948                "test();");
3949   verifyFormat("int a[] = { void forgot_closing_brace()\n"
3950                "{\n"
3951                "  f();\n"
3952                "  g();\n"
3953                "}");
3954 }
3955 
3956 bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
3957   for (size_t i = 1; i < Styles.size(); ++i)
3958     if (!(Styles[0] == Styles[i]))
3959       return false;
3960   return true;
3961 }
3962 
3963 TEST_F(FormatTest, GetsPredefinedStyleByName) {
3964   FormatStyle LLVMStyles[] = { getLLVMStyle(), getPredefinedStyle("LLVM"),
3965                                getPredefinedStyle("llvm"),
3966                                getPredefinedStyle("lLvM") };
3967   EXPECT_TRUE(allStylesEqual(LLVMStyles));
3968 
3969   FormatStyle GoogleStyles[] = { getGoogleStyle(), getPredefinedStyle("Google"),
3970                                  getPredefinedStyle("google"),
3971                                  getPredefinedStyle("gOOgle") };
3972   EXPECT_TRUE(allStylesEqual(GoogleStyles));
3973 
3974   FormatStyle ChromiumStyles[] = { getChromiumStyle(),
3975                                    getPredefinedStyle("Chromium"),
3976                                    getPredefinedStyle("chromium"),
3977                                    getPredefinedStyle("chROmiUM") };
3978   EXPECT_TRUE(allStylesEqual(ChromiumStyles));
3979 
3980   FormatStyle MozillaStyles[] = { getMozillaStyle(),
3981                                   getPredefinedStyle("Mozilla"),
3982                                   getPredefinedStyle("mozilla"),
3983                                   getPredefinedStyle("moZilla") };
3984   EXPECT_TRUE(allStylesEqual(MozillaStyles));
3985 }
3986 
3987 TEST_F(FormatTest, ParsesConfiguration) {
3988   FormatStyle Style = {};
3989 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
3990   EXPECT_NE(VALUE, Style.FIELD);                                               \
3991   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
3992   EXPECT_EQ(VALUE, Style.FIELD)
3993 
3994 #define CHECK_PARSE_BOOL(FIELD)                                                \
3995   Style.FIELD = false;                                                         \
3996   EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value());           \
3997   EXPECT_EQ(true, Style.FIELD);                                                \
3998   EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value());          \
3999   EXPECT_EQ(false, Style.FIELD);
4000 
4001   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
4002   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
4003   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
4004   CHECK_PARSE_BOOL(BinPackParameters);
4005   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
4006   CHECK_PARSE_BOOL(DerivePointerBinding);
4007   CHECK_PARSE_BOOL(IndentCaseLabels);
4008   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
4009   CHECK_PARSE_BOOL(PointerBindsToType);
4010 
4011   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
4012   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
4013   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
4014   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
4015   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
4016               PenaltyReturnTypeOnItsOwnLine, 1234u);
4017   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
4018               SpacesBeforeTrailingComments, 1234u);
4019 
4020   Style.Standard = FormatStyle::LS_Auto;
4021   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
4022   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
4023   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
4024 
4025   Style.ColumnLimit = 123;
4026   FormatStyle BaseStyle = getLLVMStyle();
4027   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
4028   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
4029 
4030 #undef CHECK_PARSE
4031 #undef CHECK_PARSE_BOOL
4032 }
4033 
4034 TEST_F(FormatTest, ConfigurationRoundTripTest) {
4035   FormatStyle Style = getLLVMStyle();
4036   std::string YAML = configurationAsText(Style);
4037   FormatStyle ParsedStyle = {};
4038   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
4039   EXPECT_EQ(Style, ParsedStyle);
4040 }
4041 
4042 } // end namespace tooling
4043 } // end namespace clang
4044