xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision dc7d5817e2932b295007c2951a11fe021df15b92)
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 }
170 
171 TEST_F(FormatTest, ReformatsMovedLines) {
172   EXPECT_EQ(
173       "template <typename T> T *getFETokenInfo() const {\n"
174       "  return static_cast<T *>(FETokenInfo);\n"
175       "}\n"
176       "  int a; // <- Should not be formatted",
177       format(
178           "template<typename T>\n"
179           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
180           "  int a; // <- Should not be formatted",
181           9, 5, getLLVMStyle()));
182 }
183 
184 //===----------------------------------------------------------------------===//
185 // Tests for control statements.
186 //===----------------------------------------------------------------------===//
187 
188 TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
189   verifyFormat("if (true)\n  f();\ng();");
190   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
191   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
192 
193   FormatStyle AllowsMergedIf = getGoogleStyle();
194   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
195   verifyFormat("if (a)\n"
196                "  // comment\n"
197                "  f();",
198                AllowsMergedIf);
199 
200   verifyFormat("if (a)  // Can't merge this\n"
201                "  f();\n",
202                AllowsMergedIf);
203   verifyFormat("if (a) /* still don't merge */\n"
204                "  f();",
205                AllowsMergedIf);
206   verifyFormat("if (a) {  // Never merge this\n"
207                "  f();\n"
208                "}",
209                AllowsMergedIf);
210   verifyFormat("if (a) { /* Never merge this */\n"
211                "  f();\n"
212                "}",
213                AllowsMergedIf);
214 
215   AllowsMergedIf.ColumnLimit = 14;
216   verifyFormat("if (a) return;", AllowsMergedIf);
217   verifyFormat("if (aaaaaaaaa)\n"
218                "  return;",
219                AllowsMergedIf);
220 
221   AllowsMergedIf.ColumnLimit = 13;
222   verifyFormat("if (a)\n  return;", AllowsMergedIf);
223 }
224 
225 TEST_F(FormatTest, ParseIfElse) {
226   verifyFormat("if (true)\n"
227                "  if (true)\n"
228                "    if (true)\n"
229                "      f();\n"
230                "    else\n"
231                "      g();\n"
232                "  else\n"
233                "    h();\n"
234                "else\n"
235                "  i();");
236   verifyFormat("if (true)\n"
237                "  if (true)\n"
238                "    if (true) {\n"
239                "      if (true)\n"
240                "        f();\n"
241                "    } else {\n"
242                "      g();\n"
243                "    }\n"
244                "  else\n"
245                "    h();\n"
246                "else {\n"
247                "  i();\n"
248                "}");
249 }
250 
251 TEST_F(FormatTest, ElseIf) {
252   verifyFormat("if (a) {\n} else if (b) {\n}");
253   verifyFormat("if (a)\n"
254                "  f();\n"
255                "else if (b)\n"
256                "  g();\n"
257                "else\n"
258                "  h();");
259 }
260 
261 TEST_F(FormatTest, FormatsForLoop) {
262   verifyFormat(
263       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
264       "     ++VeryVeryLongLoopVariable)\n"
265       "  ;");
266   verifyFormat("for (;;)\n"
267                "  f();");
268   verifyFormat("for (;;) {\n}");
269   verifyFormat("for (;;) {\n"
270                "  f();\n"
271                "}");
272 
273   verifyFormat(
274       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
275       "                                          E = UnwrappedLines.end();\n"
276       "     I != E; ++I) {\n}");
277 
278   verifyFormat(
279       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
280       "     ++IIIII) {\n}");
281   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
282                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
283                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
284 
285   // FIXME: Not sure whether we want extra identation in line 3 here:
286   verifyFormat(
287       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
288       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
289       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
290       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
291       "     ++aaaaaaaaaaa) {\n}");
292 
293   verifyGoogleFormat(
294       "for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
295       "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
296       "}");
297   verifyGoogleFormat(
298       "for (int aaaaaaaaaaa = 1;\n"
299       "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
300       "                                           aaaaaaaaaaaaaaaa,\n"
301       "                                           aaaaaaaaaaaaaaaa,\n"
302       "                                           aaaaaaaaaaaaaaaa);\n"
303       "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
304       "}");
305 }
306 
307 TEST_F(FormatTest, RangeBasedForLoops) {
308   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
309                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
310   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
311                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
312 }
313 
314 TEST_F(FormatTest, FormatsWhileLoop) {
315   verifyFormat("while (true) {\n}");
316   verifyFormat("while (true)\n"
317                "  f();");
318   verifyFormat("while () {\n}");
319   verifyFormat("while () {\n"
320                "  f();\n"
321                "}");
322 }
323 
324 TEST_F(FormatTest, FormatsDoWhile) {
325   verifyFormat("do {\n"
326                "  do_something();\n"
327                "} while (something());");
328   verifyFormat("do\n"
329                "  do_something();\n"
330                "while (something());");
331 }
332 
333 TEST_F(FormatTest, FormatsSwitchStatement) {
334   verifyFormat("switch (x) {\n"
335                "case 1:\n"
336                "  f();\n"
337                "  break;\n"
338                "case kFoo:\n"
339                "case ns::kBar:\n"
340                "case kBaz:\n"
341                "  break;\n"
342                "default:\n"
343                "  g();\n"
344                "  break;\n"
345                "}");
346   verifyFormat("switch (x) {\n"
347                "case 1: {\n"
348                "  f();\n"
349                "  break;\n"
350                "}\n"
351                "}");
352   verifyFormat("switch (x) {\n"
353                "case 1: {\n"
354                "  f();\n"
355                "  {\n"
356                "    g();\n"
357                "    h();\n"
358                "  }\n"
359                "  break;\n"
360                "}\n"
361                "}");
362   verifyFormat("switch (x) {\n"
363                "case 1: {\n"
364                "  f();\n"
365                "  if (foo) {\n"
366                "    g();\n"
367                "    h();\n"
368                "  }\n"
369                "  break;\n"
370                "}\n"
371                "}");
372   verifyFormat("switch (x) {\n"
373                "case 1: {\n"
374                "  f();\n"
375                "  g();\n"
376                "} break;\n"
377                "}");
378   verifyFormat("switch (test)\n"
379                "  ;");
380 
381   verifyGoogleFormat("switch (x) {\n"
382                      "  case 1:\n"
383                      "    f();\n"
384                      "    break;\n"
385                      "  case kFoo:\n"
386                      "  case ns::kBar:\n"
387                      "  case kBaz:\n"
388                      "    break;\n"
389                      "  default:\n"
390                      "    g();\n"
391                      "    break;\n"
392                      "}");
393   verifyGoogleFormat("switch (x) {\n"
394                      "  case 1: {\n"
395                      "    f();\n"
396                      "    break;\n"
397                      "  }\n"
398                      "}");
399   verifyGoogleFormat("switch (test)\n"
400                      "    ;");
401 }
402 
403 TEST_F(FormatTest, FormatsLabels) {
404   verifyFormat("void f() {\n"
405                "  some_code();\n"
406                "test_label:\n"
407                "  some_other_code();\n"
408                "  {\n"
409                "    some_more_code();\n"
410                "  another_label:\n"
411                "    some_more_code();\n"
412                "  }\n"
413                "}");
414   verifyFormat("some_code();\n"
415                "test_label:\n"
416                "some_other_code();");
417 }
418 
419 //===----------------------------------------------------------------------===//
420 // Tests for comments.
421 //===----------------------------------------------------------------------===//
422 
423 TEST_F(FormatTest, UnderstandsSingleLineComments) {
424   verifyFormat("// line 1\n"
425                "// line 2\n"
426                "void f() {}\n");
427 
428   verifyFormat("void f() {\n"
429                "  // Doesn't do anything\n"
430                "}");
431   verifyFormat("void f(int i,  // some comment (probably for i)\n"
432                "       int j,  // some comment (probably for j)\n"
433                "       int k); // some comment (probably for k)");
434   verifyFormat("void f(int i,\n"
435                "       // some comment (probably for j)\n"
436                "       int j,\n"
437                "       // some comment (probably for k)\n"
438                "       int k);");
439 
440   verifyFormat("int i    // This is a fancy variable\n"
441                "    = 5; // with nicely aligned comment.");
442 
443   verifyFormat("// Leading comment.\n"
444                "int a; // Trailing comment.");
445   verifyFormat("int a; // Trailing comment\n"
446                "       // on 2\n"
447                "       // or 3 lines.\n"
448                "int b;");
449   verifyFormat("int a; // Trailing comment\n"
450                "\n"
451                "// Leading comment.\n"
452                "int b;");
453   verifyFormat("int a;    // Comment.\n"
454                "          // More details.\n"
455                "int bbbb; // Another comment.");
456   verifyFormat(
457       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
458       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
459       "int cccccccccccccccccccccccccccccc;       // comment\n"
460       "int ddd;                     // looooooooooooooooooooooooong comment\n"
461       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
462       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
463       "int ccccccccccccccccccc;     // comment");
464 
465   verifyFormat("#include \"a\"     // comment\n"
466                "#include \"a/b/c\" // comment");
467   verifyFormat("#include <a>     // comment\n"
468                "#include <a/b/c> // comment");
469 
470   verifyFormat("enum E {\n"
471                "  // comment\n"
472                "  VAL_A, // comment\n"
473                "  VAL_B\n"
474                "};");
475 
476   verifyFormat(
477       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
478       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
479   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
480                "    // Comment inside a statement.\n"
481                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
482   verifyFormat(
483       "bool aaaaaaaaaaaaa = // comment\n"
484       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
485       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
486 
487   verifyFormat("int aaaa; // aaaaa\n"
488                "int aa;   // aaaaaaa",
489                getLLVMStyleWithColumns(20));
490 
491   EXPECT_EQ("void f() { // This does something ..\n"
492             "}\n"
493             "int a; // This is unrelated",
494             format("void f()    {     // This does something ..\n"
495                    "  }\n"
496                    "int   a;     // This is unrelated"));
497   EXPECT_EQ("void f() { // This does something ..\n"
498             "}          // awesome..\n"
499             "\n"
500             "int a; // This is unrelated",
501             format("void f()    { // This does something ..\n"
502                    "      } // awesome..\n"
503                    " \n"
504                    "int a;    // This is unrelated"));
505 
506   EXPECT_EQ("int i; // single line trailing comment",
507             format("int i;\\\n// single line trailing comment"));
508 
509   verifyGoogleFormat("int a;  // Trailing comment.");
510 
511   verifyFormat("someFunction(anotherFunction( // Force break.\n"
512                "    parameter));");
513 
514   verifyGoogleFormat("#endif  // HEADER_GUARD");
515 
516   verifyFormat("const char *test[] = {\n"
517                "  // A\n"
518                "  \"aaaa\",\n"
519                "  // B\n"
520                "  \"aaaaa\",\n"
521                "};");
522   verifyGoogleFormat(
523       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
524       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81 cols with this comment");
525 }
526 
527 TEST_F(FormatTest, UnderstandsMultiLineComments) {
528   verifyFormat("f(/*test=*/ true);");
529   EXPECT_EQ(
530       "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
531       "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
532       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,  /* Trailing comment for aa... */\n"
533              "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
534   EXPECT_EQ(
535       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
536       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
537       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
538              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
539 
540   verifyGoogleFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
541                      "         /* parameter 2 */ aaaaaa,\n"
542                      "         /* parameter 3 */ aaaaaa,\n"
543                      "         /* parameter 4 */ aaaaaa);");
544 }
545 
546 TEST_F(FormatTest, CommentsInStaticInitializers) {
547   EXPECT_EQ(
548       "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
549       "                         aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
550       "                         /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
551       "                         aaaaaaaaaaaaaaaaaaaa, // comment\n"
552       "                         aaaaaaaaaaaaaaaaaaaa };",
553       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
554              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
555              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
556              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
557              "                  aaaaaaaaaaaaaaaaaaaa };"));
558   verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
559                "                         bbbbbbbbbbb, ccccccccccc };");
560   verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
561                "                         // comment for bb....\n"
562                "                         bbbbbbbbbbb, ccccccccccc };");
563   verifyGoogleFormat(
564       "static SomeType type = { aaaaaaaaaaa,  // comment for aa...\n"
565       "                         bbbbbbbbbbb, ccccccccccc };");
566   verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n"
567                      "                         // comment for bb....\n"
568                      "                         bbbbbbbbbbb, ccccccccccc };");
569 
570   verifyFormat("S s = { { a, b, c },   // Group #1\n"
571                "        { d, e, f },   // Group #2\n"
572                "        { g, h, i } }; // Group #3");
573   verifyFormat("S s = { { // Group #1\n"
574                "          a, b, c },\n"
575                "        { // Group #2\n"
576                "          d, e, f },\n"
577                "        { // Group #3\n"
578                "          g, h, i } };");
579 
580   EXPECT_EQ("S s = {\n"
581             "  // Some comment\n"
582             "  a\n"
583             "\n"
584             "  // Comment after empty line\n"
585             "  b\n"
586             "}", format("S s =    {\n"
587                         "      // Some comment\n"
588                         "  a\n"
589                         "  \n"
590                         "     // Comment after empty line\n"
591                         "      b\n"
592                         "}"));
593 }
594 
595 //===----------------------------------------------------------------------===//
596 // Tests for classes, namespaces, etc.
597 //===----------------------------------------------------------------------===//
598 
599 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
600   verifyFormat("class A {\n};");
601 }
602 
603 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
604   verifyFormat("class A {\n"
605                "public:\n"
606                "protected:\n"
607                "private:\n"
608                "  void f() {}\n"
609                "};");
610   verifyGoogleFormat("class A {\n"
611                      " public:\n"
612                      " protected:\n"
613                      " private:\n"
614                      "  void f() {}\n"
615                      "};");
616 }
617 
618 TEST_F(FormatTest, FormatsDerivedClass) {
619   verifyFormat("class A : public B {\n};");
620   verifyFormat("class A : public ::B {\n};");
621 
622   verifyFormat(
623       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
624       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
625       "};\n");
626   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n"
627                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
628                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n"
629                "};\n");
630   verifyFormat(
631       "class A : public B, public C, public D, public E, public F, public G {\n"
632       "};");
633   verifyFormat("class AAAAAAAAAAAA : public B,\n"
634                "                     public C,\n"
635                "                     public D,\n"
636                "                     public E,\n"
637                "                     public F,\n"
638                "                     public G {\n"
639                "};");
640 }
641 
642 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
643   verifyFormat("class A {\n} a, b;");
644   verifyFormat("struct A {\n} a, b;");
645   verifyFormat("union A {\n} a;");
646 }
647 
648 TEST_F(FormatTest, FormatsEnum) {
649   verifyFormat("enum {\n"
650                "  Zero,\n"
651                "  One = 1,\n"
652                "  Two = One + 1,\n"
653                "  Three = (One + Two),\n"
654                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
655                "  Five = (One, Two, Three, Four, 5)\n"
656                "};");
657   verifyFormat("enum Enum {\n"
658                "};");
659   verifyFormat("enum {\n"
660                "};");
661   verifyFormat("enum X E {\n} d;");
662   verifyFormat("enum __attribute__((...)) E {\n} d;");
663   verifyFormat("enum __declspec__((...)) E {\n} d;");
664   verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
665 }
666 
667 TEST_F(FormatTest, FormatsBitfields) {
668   verifyFormat("struct Bitfields {\n"
669                "  unsigned sClass : 8;\n"
670                "  unsigned ValueKind : 2;\n"
671                "};");
672 }
673 
674 TEST_F(FormatTest, FormatsNamespaces) {
675   verifyFormat("namespace some_namespace {\n"
676                "class A {\n};\n"
677                "void f() { f(); }\n"
678                "}");
679   verifyFormat("namespace {\n"
680                "class A {\n};\n"
681                "void f() { f(); }\n"
682                "}");
683   verifyFormat("inline namespace X {\n"
684                "class A {\n};\n"
685                "void f() { f(); }\n"
686                "}");
687   verifyFormat("using namespace some_namespace;\n"
688                "class A {\n};\n"
689                "void f() { f(); }");
690 
691   // This code is more common than we thought; if we
692   // layout this correctly the semicolon will go into
693   // its own line, which is undesireable.
694   verifyFormat("namespace {\n};");
695   verifyFormat("namespace {\n"
696                "class A {\n"
697                "};\n"
698                "};");
699 }
700 
701 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
702 
703 TEST_F(FormatTest, FormatTryCatch) {
704   // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
705   // also not create single-line-blocks.
706   verifyFormat("try {\n"
707                "  throw a * b;\n"
708                "}\n"
709                "catch (int a) {\n"
710                "  // Do nothing.\n"
711                "}\n"
712                "catch (...) {\n"
713                "  exit(42);\n"
714                "}");
715 
716   // Function-level try statements.
717   verifyFormat("int f() try { return 4; }\n"
718                "catch (...) {\n"
719                "  return 5;\n"
720                "}");
721   verifyFormat("class A {\n"
722                "  int a;\n"
723                "  A() try : a(0) {}\n"
724                "  catch (...) {\n"
725                "    throw;\n"
726                "  }\n"
727                "};\n");
728 }
729 
730 TEST_F(FormatTest, FormatObjCTryCatch) {
731   verifyFormat("@try {\n"
732                "  f();\n"
733                "}\n"
734                "@catch (NSException e) {\n"
735                "  @throw;\n"
736                "}\n"
737                "@finally {\n"
738                "  exit(42);\n"
739                "}");
740 }
741 
742 TEST_F(FormatTest, StaticInitializers) {
743   verifyFormat("static SomeClass SC = { 1, 'a' };");
744 
745   // FIXME: Format like enums if the static initializer does not fit on a line.
746   verifyFormat(
747       "static SomeClass WithALoooooooooooooooooooongName = {\n"
748       "  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
749       "};");
750 
751   verifyFormat(
752       "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
753       "                     looooooooooooooooooooooooooooooooooongname,\n"
754       "                     looooooooooooooooooooooooooooooong };");
755   // Allow bin-packing in static initializers as this would often lead to
756   // terrible results, e.g.:
757   verifyGoogleFormat(
758       "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
759       "                     looooooooooooooooooooooooooooooooooongname,\n"
760       "                     looooooooooooooooooooooooooooooong };");
761 }
762 
763 TEST_F(FormatTest, NestedStaticInitializers) {
764   verifyFormat("static A x = { { {} } };\n");
765   verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
766                "                 { init1, init2, init3, init4 } } };");
767 
768   verifyFormat("somes Status::global_reps[3] = {\n"
769                "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
770                "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
771                "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
772                "};");
773   verifyGoogleFormat("somes Status::global_reps[3] = {\n"
774                      "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
775                      "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
776                      "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
777                      "};");
778   verifyFormat(
779       "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
780       "                   { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
781       " } };");
782 
783   verifyFormat(
784       "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
785       "                            { 111111111111111111111111111111,\n"
786       "                              222222222222222222222222222222,\n"
787       "                              333333333333333333333333333333 },\n"
788       "                            { 1, 2, 3 }, { 1, 2, 3 } } };");
789   verifyFormat(
790       "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
791       "                          { { 111111111111111111111111111111,\n"
792       "                              222222222222222222222222222222,\n"
793       "                              333333333333333333333333333333 } },\n"
794       "                          { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
795 
796   // FIXME: We might at some point want to handle this similar to parameter
797   // lists, where we have an option to put each on a single line.
798   verifyFormat(
799       "struct {\n"
800       "  unsigned bit;\n"
801       "  const char *const name;\n"
802       "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
803       "                  { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
804 }
805 
806 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
807   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
808                "                      \\\n"
809                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
810 }
811 
812 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
813   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
814                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
815 }
816 
817 TEST_F(FormatTest, LayoutUnknownPPDirective) {
818   EXPECT_EQ("#123 \"A string literal\"",
819             format("   #     123    \"A string literal\""));
820   EXPECT_EQ("#;", format("#;"));
821   verifyFormat("#\n;\n;\n;");
822 }
823 
824 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
825   EXPECT_EQ("#line 42 \"test\"\n",
826             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
827   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
828                                     getLLVMStyleWithColumns(12)));
829 }
830 
831 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
832   EXPECT_EQ("#line 42 \"test\"",
833             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
834   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
835 }
836 
837 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
838   // If the macro fits in one line, we still do not get the full
839   // line, as only the next line decides whether we need an escaped newline and
840   // thus use the last column.
841   verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
842 
843   verifyFormat("#define A( \\\n    B)", getLLVMStyleWithColumns(12));
844   verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
845   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
846 
847   verifyFormat("#define A A\n#define A A");
848   verifyFormat("#define A(X) A\n#define A A");
849 
850   verifyFormat("#define Something Other", getLLVMStyleWithColumns(24));
851   verifyFormat("#define Something     \\\n"
852                "  Other",
853                getLLVMStyleWithColumns(23));
854 }
855 
856 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
857   EXPECT_EQ("// some comment\n"
858             "#include \"a.h\"\n"
859             "#define A(A,\\\n"
860             "          B)\n"
861             "#include \"b.h\"\n"
862             "// some comment\n",
863             format("  // some comment\n"
864                    "  #include \"a.h\"\n"
865                    "#define A(A,\\\n"
866                    "    B)\n"
867                    "    #include \"b.h\"\n"
868                    " // some comment\n",
869                    getLLVMStyleWithColumns(13)));
870 }
871 
872 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
873 
874 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
875   EXPECT_EQ("#define A    \\\n"
876             "  c;         \\\n"
877             "  e;\n"
878             "f;",
879             format("#define A c; e;\n"
880                    "f;",
881                    getLLVMStyleWithColumns(14)));
882 }
883 
884 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
885 
886 TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
887   EXPECT_EQ("# define A\\\n  b;",
888             format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
889 }
890 
891 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
892   EXPECT_EQ("int x,\n"
893             "#define A\n"
894             "    y;",
895             format("int x,\n#define A\ny;"));
896 }
897 
898 TEST_F(FormatTest, HashInMacroDefinition) {
899   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
900   verifyFormat("#define A \\\n"
901                "  {       \\\n"
902                "    f(#c);\\\n"
903                "  }",
904                getLLVMStyleWithColumns(11));
905 
906   verifyFormat("#define A(X)         \\\n"
907                "  void function##X()",
908                getLLVMStyleWithColumns(22));
909 
910   verifyFormat("#define A(a, b, c)   \\\n"
911                "  void a##b##c()",
912                getLLVMStyleWithColumns(22));
913 
914   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
915 }
916 
917 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
918   verifyFormat("#define A (1)");
919 }
920 
921 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
922   EXPECT_EQ("#define A b;", format("#define A \\\n"
923                                    "          \\\n"
924                                    "  b;",
925                                    getLLVMStyleWithColumns(25)));
926   EXPECT_EQ("#define A \\\n"
927             "          \\\n"
928             "  a;      \\\n"
929             "  b;",
930             format("#define A \\\n"
931                    "          \\\n"
932                    "  a;      \\\n"
933                    "  b;",
934                    getLLVMStyleWithColumns(11)));
935   EXPECT_EQ("#define A \\\n"
936             "  a;      \\\n"
937             "          \\\n"
938             "  b;",
939             format("#define A \\\n"
940                    "  a;      \\\n"
941                    "          \\\n"
942                    "  b;",
943                    getLLVMStyleWithColumns(11)));
944 }
945 
946 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
947   // FIXME: Improve formatting of case labels in macros.
948   verifyFormat("#define SOMECASES  \\\n"
949                "case 1:            \\\n"
950                "  case 2\n",
951                getLLVMStyleWithColumns(20));
952 
953   verifyFormat("#define A template <typename T>");
954   verifyFormat("#define STR(x) #x\n"
955                "f(STR(this_is_a_string_literal{));");
956 }
957 
958 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
959   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
960 }
961 
962 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
963   verifyFormat("{\n  { a #c; }\n}");
964 }
965 
966 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
967   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
968             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
969   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
970             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
971 }
972 
973 TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
974   EXPECT_EQ(
975       "#define A \\\n  int i;  \\\n  int j;",
976       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
977 }
978 
979 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
980   verifyFormat("#define A \\\n"
981                "  int v(  \\\n"
982                "      a); \\\n"
983                "  int i;",
984                getLLVMStyleWithColumns(11));
985 }
986 
987 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
988   EXPECT_EQ(
989       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
990       "                      \\\n"
991       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
992       "\n"
993       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
994       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
995       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
996              "\\\n"
997              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
998              "  \n"
999              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
1000              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
1001 }
1002 
1003 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
1004   EXPECT_EQ("int\n"
1005             "#define A\n"
1006             "    a;",
1007             format("int\n#define A\na;"));
1008   verifyFormat("functionCallTo(\n"
1009                "    someOtherFunction(\n"
1010                "        withSomeParameters, whichInSequence,\n"
1011                "        areLongerThanALine(andAnotherCall,\n"
1012                "#define A B\n"
1013                "                           withMoreParamters,\n"
1014                "                           whichStronglyInfluenceTheLayout),\n"
1015                "        andMoreParameters), trailing);",
1016                getLLVMStyleWithColumns(69));
1017 }
1018 
1019 TEST_F(FormatTest, LayoutBlockInsideParens) {
1020   EXPECT_EQ("functionCall({\n"
1021             "  int i;\n"
1022             "});",
1023             format(" functionCall ( {int i;} );"));
1024 }
1025 
1026 TEST_F(FormatTest, LayoutBlockInsideStatement) {
1027   EXPECT_EQ("SOME_MACRO { int i; }\n"
1028             "int i;",
1029             format("  SOME_MACRO  {int i;}  int i;"));
1030 }
1031 
1032 TEST_F(FormatTest, LayoutNestedBlocks) {
1033   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
1034                "  struct s {\n"
1035                "    int i;\n"
1036                "  };\n"
1037                "  s kBitsToOs[] = { { 10 } };\n"
1038                "  for (int i = 0; i < 10; ++i)\n"
1039                "    return;\n"
1040                "}");
1041 }
1042 
1043 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
1044   EXPECT_EQ("{}", format("{}"));
1045 
1046   // Negative test for enum.
1047   verifyFormat("enum E {\n};");
1048 
1049   // Note that when there's a missing ';', we still join...
1050   verifyFormat("enum E {}");
1051 }
1052 
1053 //===----------------------------------------------------------------------===//
1054 // Line break tests.
1055 //===----------------------------------------------------------------------===//
1056 
1057 TEST_F(FormatTest, FormatsFunctionDefinition) {
1058   verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
1059                " int h, int j, int f,\n"
1060                "       int c, int ddddddddddddd) {\n}");
1061 }
1062 
1063 TEST_F(FormatTest, FormatsAwesomeMethodCall) {
1064   verifyFormat(
1065       "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
1066       "                       parameter, parameter, parameter)),\n"
1067       "                   SecondLongCall(parameter));");
1068 }
1069 
1070 TEST_F(FormatTest, PreventConfusingIndents) {
1071   verifyFormat(
1072       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1073       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
1074       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1075       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
1076   verifyFormat(
1077       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
1078       "    aaaaaaaaaaaaaaaaaaaaaaaa[\n"
1079       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n"
1080       "    aaaaaaaaaaaaaaaaaaaaaaaa];");
1081   verifyFormat(
1082       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
1083       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
1084       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
1085       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
1086   verifyFormat("int a = bbbb && ccc && fffff(\n"
1087                "#define A Just forcing a new line\n"
1088                "                           ddd);");
1089 }
1090 
1091 TEST_F(FormatTest, ConstructorInitializers) {
1092   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
1093   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
1094                getLLVMStyleWithColumns(45));
1095   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}",
1096                getLLVMStyleWithColumns(44));
1097   verifyFormat("Constructor()\n"
1098                "    : Inttializer(FitsOnTheLine) {\n}",
1099                getLLVMStyleWithColumns(43));
1100 
1101   verifyFormat(
1102       "SomeClass::Constructor()\n"
1103       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
1104 
1105   verifyFormat(
1106       "SomeClass::Constructor()\n"
1107       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1108       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}");
1109   verifyFormat(
1110       "SomeClass::Constructor()\n"
1111       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1112       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}");
1113 
1114   verifyFormat("Constructor()\n"
1115                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1116                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1117                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1118                "      aaaaaaaaaaaaaaaaaaaaaaa() {\n}");
1119 
1120   verifyFormat("Constructor()\n"
1121                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1122                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1123 
1124   // Here a line could be saved by splitting the second initializer onto two
1125   // lines, but that is not desireable.
1126   verifyFormat(
1127       "Constructor()\n"
1128       "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
1129       "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
1130       "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1131 
1132   FormatStyle OnePerLine = getLLVMStyle();
1133   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
1134   verifyFormat("SomeClass::Constructor()\n"
1135                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1136                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1137                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}",
1138                OnePerLine);
1139   verifyFormat("SomeClass::Constructor()\n"
1140                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
1141                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1142                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}",
1143                OnePerLine);
1144   verifyFormat("MyClass::MyClass(int var)\n"
1145                "    : some_var_(var),            // 4 space indent\n"
1146                "      some_other_var_(var + 1) { // lined up\n"
1147                "}",
1148                OnePerLine);
1149 
1150   // This test takes VERY long when memoization is broken.
1151   std::string input = "Constructor()\n"
1152                       "    : aaaa(a,\n";
1153   for (unsigned i = 0, e = 80; i != e; ++i) {
1154     input += "           a,\n";
1155   }
1156   input += "           a) {\n}";
1157   verifyGoogleFormat(input);
1158 }
1159 
1160 TEST_F(FormatTest, BreaksAsHighAsPossible) {
1161   verifyFormat(
1162       "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
1163       "    (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
1164       "  f();");
1165 }
1166 
1167 TEST_F(FormatTest, BreaksDesireably) {
1168   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
1169                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
1170                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
1171   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1172                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1173                "}");
1174 
1175   verifyFormat(
1176       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1177       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1178 
1179   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1180                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1181                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1182 
1183   verifyFormat(
1184       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1185       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1186       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1187       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
1188 
1189   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1190                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1191 
1192   verifyFormat(
1193       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1194       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1195   verifyFormat(
1196       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1197       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1198   verifyFormat(
1199       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1200       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1201 
1202   // This test case breaks on an incorrect memoization, i.e. an optimization not
1203   // taking into account the StopAt value.
1204   verifyFormat(
1205       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1206       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1207       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1208       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1209 
1210   verifyFormat("{\n  {\n    {\n"
1211                "      Annotation.SpaceRequiredBefore =\n"
1212                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
1213                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
1214                "    }\n  }\n}");
1215 }
1216 
1217 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
1218   verifyGoogleFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
1219                      "  aaaaaaaaaaaaaaaaaaaa,\n"
1220                      "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);");
1221   verifyGoogleFormat(
1222       "aaaaaaa(aaaaaaaaaaaaa,\n"
1223       "        aaaaaaaaaaaaa,\n"
1224       "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
1225   verifyGoogleFormat(
1226       "aaaaaaaa(aaaaaaaaaaaaa,\n"
1227       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1228       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1229       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1230       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
1231   verifyGoogleFormat(
1232       "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
1233       "    .aaaaaaaaaaaaaaaaaa();");
1234   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1235                      "    aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);");
1236 
1237   verifyGoogleFormat(
1238       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1239       "             aaaaaaaaaaaa,\n"
1240       "             aaaaaaaaaaaa);");
1241   verifyGoogleFormat(
1242       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
1243       "                               ddddddddddddddddddddddddddddd),\n"
1244       "             test);");
1245 
1246   verifyGoogleFormat(
1247       "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
1248       "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
1249       "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;");
1250   verifyGoogleFormat("a(\"a\"\n"
1251                      "  \"a\",\n"
1252                      "  a);");
1253 
1254   FormatStyle Style = getGoogleStyle();
1255   Style.AllowAllParametersOfDeclarationOnNextLine = false;
1256   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
1257                "                aaaaaaaaa,\n"
1258                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
1259                Style);
1260   verifyFormat(
1261       "void f() {\n"
1262       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
1263       "      .aaaaaaa();\n"
1264       "}",
1265       Style);
1266 }
1267 
1268 TEST_F(FormatTest, FormatsBuilderPattern) {
1269   verifyFormat(
1270       "return llvm::StringSwitch<Reference::Kind>(name)\n"
1271       "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
1272       "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
1273       "    .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
1274       "    .Default(ORDER_TEXT);\n");
1275 
1276   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
1277                "       aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
1278   verifyFormat(
1279       "aaaaaaa->aaaaaaa\n"
1280       "    ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1281       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
1282   verifyFormat(
1283       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
1284       "                                        aaaaaaaaaaaaaa);");
1285   verifyFormat(
1286       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
1287       "    ->aaaaaaaaaaaaaaaa(\n"
1288       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1289       "    ->aaaaaaaaaaaaaaaaa();");
1290 }
1291 
1292 TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
1293   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1294                "    GUARDED_BY(aaaaaaaaaaaaa);");
1295   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1296                "    GUARDED_BY(aaaaaaaaaaaaa);");
1297   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1298                "    GUARDED_BY(aaaaaaaaaaaaa) {\n}");
1299 }
1300 
1301 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
1302   verifyFormat(
1303       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1304       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
1305   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
1306                "    ccccccccccccccccccccccccc) {\n}");
1307   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
1308                "    ccccccccccccccccccccccccc) {\n}");
1309   verifyFormat(
1310       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
1311       "    ccccccccccccccccccccccccc) {\n}");
1312   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
1313                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
1314                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
1315                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
1316   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
1317                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
1318                "    aaaaaaaaaaaaaaa != aa) {\n}");
1319 }
1320 
1321 TEST_F(FormatTest, BreaksAfterAssignments) {
1322   verifyFormat(
1323       "unsigned Cost =\n"
1324       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
1325       "                        SI->getPointerAddressSpaceee());\n");
1326   verifyFormat(
1327       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1328       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
1329 
1330   verifyFormat(
1331       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n"
1332       "    .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
1333 }
1334 
1335 TEST_F(FormatTest, AlignsAfterAssignments) {
1336   verifyFormat(
1337       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1338       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
1339   verifyFormat(
1340       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1341       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
1342   verifyFormat(
1343       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1344       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
1345   verifyFormat(
1346       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1347       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
1348   verifyFormat("double LooooooooooooooooooooooooongResult =\n"
1349                "    aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1350                "    aaaaaaaaaaaaaaaaaaaaaaaa;");
1351 }
1352 
1353 TEST_F(FormatTest, AlignsAfterReturn) {
1354   verifyFormat(
1355       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1356       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
1357   verifyFormat(
1358       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1359       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
1360 }
1361 
1362 TEST_F(FormatTest, BreaksConditionalExpressions) {
1363   verifyFormat(
1364       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1365       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1366       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1367   verifyFormat(
1368       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1369       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1370   verifyFormat(
1371       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
1372       "                                                    : aaaaaaaaaaaaa);");
1373   verifyFormat(
1374       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1375       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n"
1376       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1377       "                   aaaaaaaaaaaaa);");
1378   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1379                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1380                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1381                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1382                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1383   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1384                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1385                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1386                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1387                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1388                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1389                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1390 
1391   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1392                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1393                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1394 
1395   // FIXME: The trailing third parameter here is kind of hidden. Prefer putting
1396   // it on the next line.
1397   verifyFormat(
1398       "unsigned Indent =\n"
1399       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
1400       "                              ? IndentForLevel[TheLine.Level]\n"
1401       "                              : TheLine * 2, TheLine.InPPDirective,\n"
1402       "           PreviousEndOfLineColumn);",
1403       getLLVMStyleWithColumns(70));
1404 
1405 }
1406 
1407 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
1408   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
1409                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
1410   verifyFormat("bool a = true, b = false;");
1411 
1412   // FIXME: Indentation looks weird.
1413   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1414                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
1415                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
1416                "     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
1417 
1418   // FIXME: This is bad as we hide "d".
1419   verifyFormat(
1420       "bool aaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
1421       "                             cccccccccccccccccccccccccccc, d = e && f;");
1422 
1423 }
1424 
1425 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1426   verifyFormat("arr[foo ? bar : baz];");
1427   verifyFormat("f()[foo ? bar : baz];");
1428   verifyFormat("(a + b)[foo ? bar : baz];");
1429   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1430 }
1431 
1432 TEST_F(FormatTest, AlignsStringLiterals) {
1433   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1434                "                                      \"short literal\");");
1435   verifyFormat(
1436       "looooooooooooooooooooooooongFunction(\n"
1437       "    \"short literal\"\n"
1438       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1439   verifyFormat("someFunction(\"Always break between multi-line\"\n"
1440                "             \" string literals\",\n"
1441                "             and, other, parameters);");
1442 }
1443 
1444 TEST_F(FormatTest, AlignsPipes) {
1445   verifyFormat(
1446       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1447       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1448       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1449   verifyFormat(
1450       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1451       "                     << aaaaaaaaaaaaaaaaaaaa;");
1452   verifyFormat(
1453       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1454       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1455   verifyFormat(
1456       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1457       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1458       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1459   verifyFormat(
1460       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1461       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1462       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1463 
1464   verifyFormat("return out << \"somepacket = {\\n\"\n"
1465                "           << \"  aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
1466                "           << \"  bbbb = \" << pkt.bbbb << \"\\n\"\n"
1467                "           << \"  cccccc = \" << pkt.cccccc << \"\\n\"\n"
1468                "           << \"  ddd = [\" << pkt.ddd << \"]\\n\"\n"
1469                "           << \"}\";");
1470 
1471   verifyFormat(
1472       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
1473       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
1474       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
1475       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
1476       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
1477 }
1478 
1479 TEST_F(FormatTest, UnderstandsEquals) {
1480   verifyFormat(
1481       "aaaaaaaaaaaaaaaaa =\n"
1482       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1483   verifyFormat(
1484       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1485       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1486   verifyFormat(
1487       "if (a) {\n"
1488       "  f();\n"
1489       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1490       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1491       "}");
1492 
1493   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1494                "        100000000 + 10000000) {\n}");
1495 }
1496 
1497 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
1498   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1499                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
1500 
1501   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1502                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
1503 
1504   verifyFormat(
1505       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1506       "                                                          Parameter2);");
1507 
1508   verifyFormat(
1509       "ShortObject->shortFunction(\n"
1510       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1511       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1512 
1513   verifyFormat("loooooooooooooongFunction(\n"
1514                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
1515 
1516   verifyFormat(
1517       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1518       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1519 
1520   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
1521                "    .WillRepeatedly(Return(SomeValue));");
1522   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
1523                "    .insert(ccccccccccccccccccccccc);");
1524 
1525   verifyGoogleFormat(
1526       "aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
1527       "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
1528       "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
1529       "                         aaaaaaaaaaaaaaaaaaa,\n"
1530       "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1531 
1532   // Here, it is not necessary to wrap at "." or "->".
1533   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
1534                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1535   verifyFormat(
1536       "aaaaaaaaaaa->aaaaaaaaa(\n"
1537       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1538       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
1539 
1540   verifyFormat(
1541       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1542       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
1543 }
1544 
1545 TEST_F(FormatTest, WrapsTemplateDeclarations) {
1546   verifyFormat("template <typename T>\n"
1547                "virtual void loooooooooooongFunction(int Param1, int Param2);");
1548   verifyFormat(
1549       "template <typename T>\n"
1550       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
1551   verifyFormat("template <typename T>\n"
1552                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
1553                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
1554   verifyFormat(
1555       "template <typename T>\n"
1556       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
1557       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
1558   verifyFormat(
1559       "template <typename T>\n"
1560       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
1561       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
1562       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1563   verifyFormat("template <typename T>\n"
1564                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1565                "    int aaaaaaaaaaaaaaaaa);");
1566   verifyFormat(
1567       "template <typename T1, typename T2 = char, typename T3 = char,\n"
1568       "          typename T4 = char>\n"
1569       "void f();");
1570   verifyFormat(
1571       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
1572       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1573 
1574   verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
1575                "    a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
1576 }
1577 
1578 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
1579   verifyFormat(
1580       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1581       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
1582   verifyFormat(
1583       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1584       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1585       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
1586 
1587   // FIXME: Should we have an extra indent after the second break?
1588   verifyFormat(
1589       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1590       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1591       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
1592 
1593   // FIXME: Look into whether we should indent 4 from the start or 4 from
1594   // "bbbbb..." here instead of what we are doing now.
1595   verifyFormat(
1596       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
1597       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
1598 
1599   // Breaking at nested name specifiers is generally not desirable.
1600   verifyFormat(
1601       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1602       "    aaaaaaaaaaaaaaaaaaaaaaa);");
1603 
1604   verifyFormat(
1605       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1606       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1607       "                   aaaaaaaaaaaaaaaaaaaaa);",
1608       getLLVMStyleWithColumns(74));
1609 }
1610 
1611 TEST_F(FormatTest, UnderstandsTemplateParameters) {
1612   verifyFormat("A<int> a;");
1613   verifyFormat("A<A<A<int> > > a;");
1614   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
1615   verifyFormat("bool x = a < 1 || 2 > a;");
1616   verifyFormat("bool x = 5 < f<int>();");
1617   verifyFormat("bool x = f<int>() > 5;");
1618   verifyFormat("bool x = 5 < a<int>::x;");
1619   verifyFormat("bool x = a < 4 ? a > 2 : false;");
1620   verifyFormat("bool x = f() ? a < 2 : a > 2;");
1621 
1622   verifyGoogleFormat("A<A<int>> a;");
1623   verifyGoogleFormat("A<A<A<int>>> a;");
1624   verifyGoogleFormat("A<A<A<A<int>>>> a;");
1625   verifyGoogleFormat("A<A<int> > a;");
1626   verifyGoogleFormat("A<A<A<int> > > a;");
1627   verifyGoogleFormat("A<A<A<A<int> > > > a;");
1628   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
1629   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
1630 
1631   verifyFormat("test >> a >> b;");
1632   verifyFormat("test << a >> b;");
1633 
1634   verifyFormat("f<int>();");
1635   verifyFormat("template <typename T> void f() {}");
1636 }
1637 
1638 TEST_F(FormatTest, UnderstandsUnaryOperators) {
1639   verifyFormat("int a = -2;");
1640   verifyFormat("f(-1, -2, -3);");
1641   verifyFormat("a[-1] = 5;");
1642   verifyFormat("int a = 5 + -2;");
1643   verifyFormat("if (i == -1) {\n}");
1644   verifyFormat("if (i != -1) {\n}");
1645   verifyFormat("if (i > -1) {\n}");
1646   verifyFormat("if (i < -1) {\n}");
1647   verifyFormat("++(a->f());");
1648   verifyFormat("--(a->f());");
1649   verifyFormat("(a->f())++;");
1650   verifyFormat("a[42]++;");
1651   verifyFormat("if (!(a->f())) {\n}");
1652 
1653   verifyFormat("a-- > b;");
1654   verifyFormat("b ? -a : c;");
1655   verifyFormat("n * sizeof char16;");
1656   verifyFormat("n * alignof char16;");
1657   verifyFormat("sizeof(char);");
1658   verifyFormat("alignof(char);");
1659 
1660   verifyFormat("return -1;");
1661   verifyFormat("switch (a) {\n"
1662                "case -1:\n"
1663                "  break;\n"
1664                "}");
1665 
1666   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
1667   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
1668 
1669   verifyFormat("int a = /* confusing comment */ -1;");
1670   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
1671   verifyFormat("int a = i /* confusing comment */++;");
1672 }
1673 
1674 TEST_F(FormatTest, UndestandsOverloadedOperators) {
1675   verifyFormat("bool operator<();");
1676   verifyFormat("bool operator>();");
1677   verifyFormat("bool operator=();");
1678   verifyFormat("bool operator==();");
1679   verifyFormat("bool operator!=();");
1680   verifyFormat("int operator+();");
1681   verifyFormat("int operator++();");
1682   verifyFormat("bool operator();");
1683   verifyFormat("bool operator()();");
1684   verifyFormat("bool operator[]();");
1685   verifyFormat("operator bool();");
1686   verifyFormat("operator int();");
1687   verifyFormat("operator void *();");
1688   verifyFormat("operator SomeType<int>();");
1689   verifyFormat("operator SomeType<int, int>();");
1690   verifyFormat("operator SomeType<SomeType<int> >();");
1691   verifyFormat("void *operator new(std::size_t size);");
1692   verifyFormat("void *operator new[](std::size_t size);");
1693   verifyFormat("void operator delete(void *ptr);");
1694   verifyFormat("void operator delete[](void *ptr);");
1695 
1696   verifyFormat(
1697       "ostream &operator<<(ostream &OutputStream,\n"
1698       "                    SomeReallyLongType WithSomeReallyLongValue);");
1699 
1700   verifyGoogleFormat("operator void*();");
1701   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
1702 }
1703 
1704 TEST_F(FormatTest, UnderstandsNewAndDelete) {
1705   verifyFormat("A *a = new A;");
1706   verifyFormat("A *a = new (placement) A;");
1707   verifyFormat("delete a;");
1708   verifyFormat("delete (A *)a;");
1709 }
1710 
1711 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
1712   verifyFormat("int *f(int *a) {}");
1713   verifyFormat("int main(int argc, char **argv) {}");
1714   verifyFormat("Test::Test(int b) : a(b * b) {}");
1715   verifyIndependentOfContext("f(a, *a);");
1716   verifyIndependentOfContext("f(*a);");
1717   verifyIndependentOfContext("int a = b * 10;");
1718   verifyIndependentOfContext("int a = 10 * b;");
1719   verifyIndependentOfContext("int a = b * c;");
1720   verifyIndependentOfContext("int a += b * c;");
1721   verifyIndependentOfContext("int a -= b * c;");
1722   verifyIndependentOfContext("int a *= b * c;");
1723   verifyIndependentOfContext("int a /= b * c;");
1724   verifyIndependentOfContext("int a = *b;");
1725   verifyIndependentOfContext("int a = *b * c;");
1726   verifyIndependentOfContext("int a = b * *c;");
1727   verifyIndependentOfContext("return 10 * b;");
1728   verifyIndependentOfContext("return *b * *c;");
1729   verifyIndependentOfContext("return a & ~b;");
1730   verifyIndependentOfContext("f(b ? *c : *d);");
1731   verifyIndependentOfContext("int a = b ? *c : *d;");
1732   verifyIndependentOfContext("*b = a;");
1733   verifyIndependentOfContext("a * ~b;");
1734   verifyIndependentOfContext("a * !b;");
1735   verifyIndependentOfContext("a * +b;");
1736   verifyIndependentOfContext("a * -b;");
1737   verifyIndependentOfContext("a * ++b;");
1738   verifyIndependentOfContext("a * --b;");
1739   verifyIndependentOfContext("a[4] * b;");
1740   verifyIndependentOfContext("f() * b;");
1741   verifyIndependentOfContext("a * [self dostuff];");
1742   verifyIndependentOfContext("a * (a + b);");
1743   verifyIndependentOfContext("(a *)(a + b);");
1744   verifyIndependentOfContext("int *pa = (int *)&a;");
1745   verifyIndependentOfContext("return sizeof(int **);");
1746   verifyIndependentOfContext("return sizeof(int ******);");
1747   verifyIndependentOfContext("return (int **&)a;");
1748   verifyGoogleFormat("return sizeof(int**);");
1749   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
1750   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
1751   // FIXME: The newline is wrong.
1752   verifyFormat("auto a = [](int **&, int ***) {}\n;");
1753 
1754   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
1755 
1756   verifyIndependentOfContext("A<int *> a;");
1757   verifyIndependentOfContext("A<int **> a;");
1758   verifyIndependentOfContext("A<int *, int *> a;");
1759   verifyIndependentOfContext(
1760       "const char *const p = reinterpret_cast<const char *const>(q);");
1761   verifyIndependentOfContext("A<int **, int **> a;");
1762   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
1763 
1764   verifyFormat(
1765       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1766       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1767 
1768   verifyGoogleFormat("int main(int argc, char** argv) {}");
1769   verifyGoogleFormat("A<int*> a;");
1770   verifyGoogleFormat("A<int**> a;");
1771   verifyGoogleFormat("A<int*, int*> a;");
1772   verifyGoogleFormat("A<int**, int**> a;");
1773   verifyGoogleFormat("f(b ? *c : *d);");
1774   verifyGoogleFormat("int a = b ? *c : *d;");
1775   verifyGoogleFormat("Type* t = **x;");
1776   verifyGoogleFormat("Type* t = *++*x;");
1777   verifyGoogleFormat("*++*x;");
1778   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
1779   verifyGoogleFormat("Type* t = x++ * y;");
1780   verifyGoogleFormat(
1781       "const char* const p = reinterpret_cast<const char* const>(q);");
1782 
1783   verifyIndependentOfContext("a = *(x + y);");
1784   verifyIndependentOfContext("a = &(x + y);");
1785   verifyIndependentOfContext("*(x + y).call();");
1786   verifyIndependentOfContext("&(x + y)->call();");
1787   verifyIndependentOfContext("&(*I).first");
1788 
1789   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
1790   verifyFormat(
1791       "int *MyValues = {\n"
1792       "  *A, // Operator detection might be confused by the '{'\n"
1793       "  *BB // Operator detection might be confused by previous comment\n"
1794       "};");
1795 
1796   verifyIndependentOfContext("if (int *a = &b)");
1797   verifyIndependentOfContext("if (int &a = *b)");
1798   verifyIndependentOfContext("if (a & b[i])");
1799   verifyIndependentOfContext("if (a::b::c::d & b[i])");
1800   verifyIndependentOfContext("if (*b[i])");
1801   verifyIndependentOfContext("if (int *a = (&b))");
1802   verifyIndependentOfContext("while (int *a = &b)");
1803   verifyFormat("void f() {\n"
1804                "  for (const int &v : Values) {\n"
1805                "  }\n"
1806                "}");
1807   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
1808   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
1809 
1810   verifyIndependentOfContext("A = new SomeType *[Length]();");
1811   verifyGoogleFormat("A = new SomeType* [Length]();");
1812 
1813   EXPECT_EQ("int *a;\n"
1814             "int *a;\n"
1815             "int *a;",
1816             format("int *a;\n"
1817                    "int* a;\n"
1818                    "int *a;",
1819                    getGoogleStyle()));
1820   EXPECT_EQ("int* a;\n"
1821             "int* a;\n"
1822             "int* a;",
1823             format("int* a;\n"
1824                    "int* a;\n"
1825                    "int *a;",
1826                    getGoogleStyle()));
1827   EXPECT_EQ("int *a;\n"
1828             "int *a;\n"
1829             "int *a;",
1830             format("int *a;\n"
1831                    "int * a;\n"
1832                    "int *  a;",
1833                    getGoogleStyle()));
1834 }
1835 
1836 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
1837   verifyFormat("void f() {\n"
1838                "  x[aaaaaaaaa -\n"
1839                "      b] = 23;\n"
1840                "}",
1841                getLLVMStyleWithColumns(15));
1842 }
1843 
1844 TEST_F(FormatTest, FormatsCasts) {
1845   verifyFormat("Type *A = static_cast<Type *>(P);");
1846   verifyFormat("Type *A = (Type *)P;");
1847   verifyFormat("Type *A = (vector<Type *, int *>)P;");
1848   verifyFormat("int a = (int)(2.0f);");
1849 
1850   // FIXME: These also need to be identified.
1851   verifyFormat("int a = (int) 2.0f;");
1852   verifyFormat("int a = (int) * b;");
1853 
1854   // These are not casts.
1855   verifyFormat("void f(int *) {}");
1856   verifyFormat("f(foo)->b;");
1857   verifyFormat("f(foo).b;");
1858   verifyFormat("f(foo)(b);");
1859   verifyFormat("f(foo)[b];");
1860   verifyFormat("[](foo) { return 4; }(bar)];");
1861   verifyFormat("(*funptr)(foo)[4];");
1862   verifyFormat("funptrs[4](foo)[4];");
1863   verifyFormat("void f(int *);");
1864   verifyFormat("void f(int *) = 0;");
1865   verifyFormat("void f(SmallVector<int>) {}");
1866   verifyFormat("void f(SmallVector<int>);");
1867   verifyFormat("void f(SmallVector<int>) = 0;");
1868   verifyFormat("void f(int i = (kValue) * kMask) {}");
1869   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
1870   verifyFormat("int a = sizeof(int) * b;");
1871   verifyFormat("int a = alignof(int) * b;");
1872 }
1873 
1874 TEST_F(FormatTest, FormatsFunctionTypes) {
1875   // FIXME: Determine the cases that need a space after the return type and fix.
1876   verifyFormat("A<bool()> a;");
1877   verifyFormat("A<SomeType()> a;");
1878   verifyFormat("A<void(*)(int, std::string)> a;");
1879 
1880   verifyFormat("int(*func)(void *);");
1881 }
1882 
1883 TEST_F(FormatTest, BreaksFunctionDeclarations) {
1884   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
1885                "                  int LoooooooooooooooooooongParam2) {\n}");
1886   verifyFormat(
1887       "TypeSpecDecl *\n"
1888       "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n"
1889       "                     IdentifierIn *II, Type *T) {\n}");
1890   verifyGoogleFormat(
1891       "TypeSpecDecl* TypeSpecDecl::Create(\n"
1892       "    ASTContext& C, DeclContext* DC, SourceLocation L) {\n}");
1893   verifyGoogleFormat(
1894       "some_namespace::LongReturnType\n"
1895       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
1896       "    int first_long_parameter, int second_parameter) {\n}");
1897 
1898   verifyGoogleFormat("template <typename T>\n"
1899                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
1900                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {\n}");
1901 }
1902 
1903 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1904   verifyFormat("(a)->b();");
1905   verifyFormat("--a;");
1906 }
1907 
1908 TEST_F(FormatTest, HandlesIncludeDirectives) {
1909   verifyFormat("#include <string>\n"
1910                "#include <a/b/c.h>\n"
1911                "#include \"a/b/string\"\n"
1912                "#include \"string.h\"\n"
1913                "#include \"string.h\"\n"
1914                "#include <a-a>\n"
1915                "#include < path with space >\n");
1916 
1917   verifyFormat("#import <string>");
1918   verifyFormat("#import <a/b/c.h>");
1919   verifyFormat("#import \"a/b/string\"");
1920   verifyFormat("#import \"string.h\"");
1921   verifyFormat("#import \"string.h\"");
1922 }
1923 
1924 //===----------------------------------------------------------------------===//
1925 // Error recovery tests.
1926 //===----------------------------------------------------------------------===//
1927 
1928 TEST_F(FormatTest, IncompleteParameterLists) {
1929   verifyGoogleFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
1930                      "                        double *min_x,\n"
1931                      "                        double *max_x,\n"
1932                      "                        double *min_y,\n"
1933                      "                        double *max_y,\n"
1934                      "                        double *min_z,\n"
1935                      "                        double *max_z, ) {\n"
1936                      "}");
1937 }
1938 
1939 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
1940   verifyFormat("void f() { return; }\n42");
1941   verifyFormat("void f() {\n"
1942                "  if (0)\n"
1943                "    return;\n"
1944                "}\n"
1945                "42");
1946   verifyFormat("void f() { return }\n42");
1947   verifyFormat("void f() {\n"
1948                "  if (0)\n"
1949                "    return\n"
1950                "}\n"
1951                "42");
1952 }
1953 
1954 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
1955   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
1956   EXPECT_EQ("void f() {\n"
1957             "  if (a)\n"
1958             "    return\n"
1959             "}",
1960             format("void  f  (  )  {  if  ( a )  return  }"));
1961   EXPECT_EQ("namespace N { void f() }", format("namespace  N  {  void f()  }"));
1962   EXPECT_EQ("namespace N {\n"
1963             "void f() {}\n"
1964             "void g()\n"
1965             "}",
1966             format("namespace N  { void f( ) { } void g( ) }"));
1967 }
1968 
1969 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1970   verifyFormat("int aaaaaaaa =\n"
1971                "    // Overly long comment\n"
1972                "    b;",
1973                getLLVMStyleWithColumns(20));
1974   verifyFormat("function(\n"
1975                "    ShortArgument,\n"
1976                "    LoooooooooooongArgument);\n",
1977                getLLVMStyleWithColumns(20));
1978 }
1979 
1980 TEST_F(FormatTest, IncorrectAccessSpecifier) {
1981   verifyFormat("public:");
1982   verifyFormat("class A {\n"
1983                "public\n"
1984                "  void f() {}\n"
1985                "};");
1986   verifyFormat("public\n"
1987                "int qwerty;");
1988   verifyFormat("public\n"
1989                "B {}");
1990   verifyFormat("public\n"
1991                "{}");
1992   verifyFormat("public\n"
1993                "B { int x; }");
1994 }
1995 
1996 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); }
1997 
1998 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
1999   verifyFormat("do {\n}");
2000   verifyFormat("do {\n}\n"
2001                "f();");
2002   verifyFormat("do {\n}\n"
2003                "wheeee(fun);");
2004   verifyFormat("do {\n"
2005                "  f();\n"
2006                "}");
2007 }
2008 
2009 TEST_F(FormatTest, IncorrectCodeMissingParens) {
2010   verifyFormat("if {\n  foo;\n  foo();\n}");
2011   verifyFormat("switch {\n  foo;\n  foo();\n}");
2012   verifyFormat("for {\n  foo;\n  foo();\n}");
2013   verifyFormat("while {\n  foo;\n  foo();\n}");
2014   verifyFormat("do {\n  foo;\n  foo();\n} while;");
2015 }
2016 
2017 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
2018   verifyFormat("namespace {\n"
2019                "class Foo {  Foo  ( }; }  // comment");
2020 }
2021 
2022 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
2023   EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
2024   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
2025   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
2026   EXPECT_EQ("{\n  {}\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
2027 
2028   EXPECT_EQ("{\n"
2029             "    {\n"
2030             " breakme(\n"
2031             "     qwe);\n"
2032             "}\n",
2033             format("{\n"
2034                    "    {\n"
2035                    " breakme(qwe);\n"
2036                    "}\n",
2037                    getLLVMStyleWithColumns(10)));
2038 }
2039 
2040 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
2041   verifyFormat("int x = {\n"
2042                "  avariable,\n"
2043                "  b(alongervariable)\n"
2044                "};",
2045                getLLVMStyleWithColumns(25));
2046 }
2047 
2048 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
2049   verifyFormat("return (a)(b) { 1, 2, 3 };");
2050 }
2051 
2052 TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
2053   verifyFormat(
2054       "Aaa({\n"
2055       "  int i;\n"
2056       "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2057       "                                    ccccccccccccccccc));");
2058 }
2059 
2060 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
2061   verifyFormat("void f() { return 42; }");
2062   verifyFormat("void f() {\n"
2063                "  // Comment\n"
2064                "}");
2065   verifyFormat("{\n"
2066                "#error {\n"
2067                "  int a;\n"
2068                "}");
2069   verifyFormat("{\n"
2070                "  int a;\n"
2071                "#error {\n"
2072                "}");
2073 
2074   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
2075   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
2076 
2077   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
2078   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
2079 }
2080 
2081 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
2082   // Elaborate type variable declarations.
2083   verifyFormat("struct foo a = { bar };\nint n;");
2084   verifyFormat("class foo a = { bar };\nint n;");
2085   verifyFormat("union foo a = { bar };\nint n;");
2086 
2087   // Elaborate types inside function definitions.
2088   verifyFormat("struct foo f() {}\nint n;");
2089   verifyFormat("class foo f() {}\nint n;");
2090   verifyFormat("union foo f() {}\nint n;");
2091 
2092   // Templates.
2093   verifyFormat("template <class X> void f() {}\nint n;");
2094   verifyFormat("template <struct X> void f() {}\nint n;");
2095   verifyFormat("template <union X> void f() {}\nint n;");
2096 
2097   // Actual definitions...
2098   verifyFormat("struct {\n} n;");
2099   verifyFormat(
2100       "template <template <class T, class Y>, class Z> class X {\n} n;");
2101   verifyFormat("union Z {\n  int n;\n} x;");
2102   verifyFormat("class MACRO Z {\n} n;");
2103   verifyFormat("class MACRO(X) Z {\n} n;");
2104   verifyFormat("class __attribute__(X) Z {\n} n;");
2105   verifyFormat("class __declspec(X) Z {\n} n;");
2106   verifyFormat("class A##B##C {\n} n;");
2107 
2108   // Redefinition from nested context:
2109   verifyFormat("class A::B::C {\n} n;");
2110 
2111   // Template definitions.
2112   // FIXME: This is still incorrectly handled at the formatter side.
2113   verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
2114 
2115   // FIXME:
2116   // This now gets parsed incorrectly as class definition.
2117   // verifyFormat("class A<int> f() {\n}\nint n;");
2118 
2119   // Elaborate types where incorrectly parsing the structural element would
2120   // break the indent.
2121   verifyFormat("if (true)\n"
2122                "  class X x;\n"
2123                "else\n"
2124                "  f();\n");
2125 }
2126 
2127 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
2128   verifyFormat("#error Leave     all         white!!!!! space* alone!\n");
2129   verifyFormat("#warning Leave     all         white!!!!! space* alone!\n");
2130   EXPECT_EQ("#error 1", format("  #  error   1"));
2131   EXPECT_EQ("#warning 1", format("  #  warning 1"));
2132 }
2133 
2134 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
2135   FormatStyle AllowsMergedIf = getGoogleStyle();
2136   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
2137   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
2138   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
2139   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
2140   EXPECT_EQ("if (true) return 42;",
2141             format("if (true)\nreturn 42;", AllowsMergedIf));
2142   FormatStyle ShortMergedIf = AllowsMergedIf;
2143   ShortMergedIf.ColumnLimit = 25;
2144   verifyFormat("#define A               \\\n"
2145                "  if (true) return 42;",
2146                ShortMergedIf);
2147   verifyFormat("#define A               \\\n"
2148                "  f();                  \\\n"
2149                "  if (true)\n"
2150                "#define B",
2151                ShortMergedIf);
2152   verifyFormat("#define A               \\\n"
2153                "  f();                  \\\n"
2154                "  if (true)\n"
2155                "g();",
2156                ShortMergedIf);
2157   verifyFormat("{\n"
2158                "#ifdef A\n"
2159                "  // Comment\n"
2160                "  if (true) continue;\n"
2161                "#endif\n"
2162                "  // Comment\n"
2163                "  if (true) continue;",
2164                ShortMergedIf);
2165 }
2166 
2167 TEST_F(FormatTest, BlockCommentsInControlLoops) {
2168   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2169                "  f();\n"
2170                "}");
2171   verifyFormat("if (0) /* a comment in a strange place */ {\n"
2172                "  f();\n"
2173                "} /* another comment */ else /* comment #3 */ {\n"
2174                "  g();\n"
2175                "}");
2176   verifyFormat("while (0) /* a comment in a strange place */ {\n"
2177                "  f();\n"
2178                "}");
2179   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2180                "  f();\n"
2181                "}");
2182   verifyFormat("do /* a comment in a strange place */ {\n"
2183                "  f();\n"
2184                "} /* another comment */ while (0);");
2185 }
2186 
2187 TEST_F(FormatTest, BlockComments) {
2188   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2189             format("/* *//* */  /* */\n/* *//* */  /* */"));
2190   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
2191   EXPECT_EQ("#define A /*   */\\\n"
2192             "  b\n"
2193             "/* */\n"
2194             "someCall(\n"
2195             "    parameter);",
2196             format("#define A /*   */ b\n"
2197                    "/* */\n"
2198                    "someCall(parameter);",
2199                    getLLVMStyleWithColumns(15)));
2200 
2201   EXPECT_EQ("#define A\n"
2202             "/* */ someCall(\n"
2203             "    parameter);",
2204             format("#define A\n"
2205                    "/* */someCall(parameter);",
2206                    getLLVMStyleWithColumns(15)));
2207 
2208   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2209             "             2, /* comment 2 */\n"
2210             "             3, /* comment 3 */\n"
2211             "             aaaa,\n"
2212             "             bbbb);",
2213             format("someFunction (1,   /* comment 1 */\n"
2214                    "                2, /* comment 2 */  \n"
2215                    "               3,   /* comment 3 */\n"
2216                    "aaaa, bbbb );",
2217                    getGoogleStyle()));
2218   verifyFormat(
2219       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2220       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2221   EXPECT_EQ(
2222       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2223       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2224       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2225       format(
2226           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
2227           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
2228           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2229   EXPECT_EQ(
2230       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2231       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
2232       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
2233       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2234              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2235              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
2236 }
2237 
2238 TEST_F(FormatTest, BlockCommentsInMacros) {
2239   EXPECT_EQ("#define A          \\\n"
2240             "  {                \\\n"
2241             "    /* one line */ \\\n"
2242             "    someCall();",
2243             format("#define A {        \\\n"
2244                    "  /* one line */   \\\n"
2245                    "  someCall();",
2246                    getLLVMStyleWithColumns(20)));
2247   EXPECT_EQ("#define A          \\\n"
2248             "  {                \\\n"
2249             "    /* previous */ \\\n"
2250             "    /* one line */ \\\n"
2251             "    someCall();",
2252             format("#define A {        \\\n"
2253                    "  /* previous */   \\\n"
2254                    "  /* one line */   \\\n"
2255                    "  someCall();",
2256                    getLLVMStyleWithColumns(20)));
2257 }
2258 
2259 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2260   // FIXME: This is not what we want...
2261   verifyFormat("{\n"
2262                "// a"
2263                "// b");
2264 }
2265 
2266 TEST_F(FormatTest, FormatStarDependingOnContext) {
2267   verifyFormat("void f(int *a);");
2268   verifyFormat("void f() { f(fint * b); }");
2269   verifyFormat("class A {\n  void f(int *a);\n};");
2270   verifyFormat("class A {\n  int *a;\n};");
2271   verifyFormat("namespace a {\n"
2272                "namespace b {\n"
2273                "class A {\n"
2274                "  void f() {}\n"
2275                "  int *a;\n"
2276                "};\n"
2277                "}\n"
2278                "}");
2279 }
2280 
2281 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
2282   verifyFormat("while");
2283   verifyFormat("operator");
2284 }
2285 
2286 //===----------------------------------------------------------------------===//
2287 // Objective-C tests.
2288 //===----------------------------------------------------------------------===//
2289 
2290 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
2291   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
2292   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
2293             format("-(NSUInteger)indexOfObject:(id)anObject;"));
2294   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
2295   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
2296   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
2297             format("-(NSInteger)Method3:(id)anObject;"));
2298   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
2299             format("-(NSInteger)Method4:(id)anObject;"));
2300   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
2301             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
2302   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
2303             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
2304   EXPECT_EQ(
2305       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
2306       format(
2307           "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
2308 
2309   // Very long objectiveC method declaration.
2310   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
2311                "                    inRange:(NSRange)range\n"
2312                "                   outRange:(NSRange)out_range\n"
2313                "                  outRange1:(NSRange)out_range1\n"
2314                "                  outRange2:(NSRange)out_range2\n"
2315                "                  outRange3:(NSRange)out_range3\n"
2316                "                  outRange4:(NSRange)out_range4\n"
2317                "                  outRange5:(NSRange)out_range5\n"
2318                "                  outRange6:(NSRange)out_range6\n"
2319                "                  outRange7:(NSRange)out_range7\n"
2320                "                  outRange8:(NSRange)out_range8\n"
2321                "                  outRange9:(NSRange)out_range9;");
2322 
2323   verifyFormat("- (int)sum:(vector<int>)numbers;");
2324   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
2325   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
2326   // protocol lists (but not for template classes):
2327   //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
2328 
2329   verifyFormat("- (int(*)())foo:(int(*)())f;");
2330   verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
2331 
2332   // If there's no return type (very rare in practice!), LLVM and Google style
2333   // agree.
2334   verifyFormat("- foo:(int)f;");
2335   verifyGoogleFormat("- foo:(int)foo;");
2336 }
2337 
2338 TEST_F(FormatTest, FormatObjCBlocks) {
2339   verifyFormat("int (^Block)(int, int);");
2340   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
2341 }
2342 
2343 TEST_F(FormatTest, FormatObjCInterface) {
2344   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
2345                "@public\n"
2346                "  int field1;\n"
2347                "@protected\n"
2348                "  int field2;\n"
2349                "@private\n"
2350                "  int field3;\n"
2351                "@package\n"
2352                "  int field4;\n"
2353                "}\n"
2354                "+ (id)init;\n"
2355                "@end");
2356 
2357   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
2358                      " @public\n"
2359                      "  int field1;\n"
2360                      " @protected\n"
2361                      "  int field2;\n"
2362                      " @private\n"
2363                      "  int field3;\n"
2364                      " @package\n"
2365                      "  int field4;\n"
2366                      "}\n"
2367                      "+ (id)init;\n"
2368                      "@end");
2369 
2370   verifyFormat("@interface /* wait for it */ Foo\n"
2371                "+ (id)init;\n"
2372                "// Look, a comment!\n"
2373                "- (int)answerWith:(int)i;\n"
2374                "@end");
2375 
2376   verifyFormat("@interface Foo\n"
2377                "@end\n"
2378                "@interface Bar\n"
2379                "@end");
2380 
2381   verifyFormat("@interface Foo : Bar\n"
2382                "+ (id)init;\n"
2383                "@end");
2384 
2385   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
2386                "+ (id)init;\n"
2387                "@end");
2388 
2389   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
2390                      "+ (id)init;\n"
2391                      "@end");
2392 
2393   verifyFormat("@interface Foo (HackStuff)\n"
2394                "+ (id)init;\n"
2395                "@end");
2396 
2397   verifyFormat("@interface Foo ()\n"
2398                "+ (id)init;\n"
2399                "@end");
2400 
2401   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
2402                "+ (id)init;\n"
2403                "@end");
2404 
2405   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
2406                      "+ (id)init;\n"
2407                      "@end");
2408 
2409   verifyFormat("@interface Foo {\n"
2410                "  int _i;\n"
2411                "}\n"
2412                "+ (id)init;\n"
2413                "@end");
2414 
2415   verifyFormat("@interface Foo : Bar {\n"
2416                "  int _i;\n"
2417                "}\n"
2418                "+ (id)init;\n"
2419                "@end");
2420 
2421   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
2422                "  int _i;\n"
2423                "}\n"
2424                "+ (id)init;\n"
2425                "@end");
2426 
2427   verifyFormat("@interface Foo (HackStuff) {\n"
2428                "  int _i;\n"
2429                "}\n"
2430                "+ (id)init;\n"
2431                "@end");
2432 
2433   verifyFormat("@interface Foo () {\n"
2434                "  int _i;\n"
2435                "}\n"
2436                "+ (id)init;\n"
2437                "@end");
2438 
2439   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
2440                "  int _i;\n"
2441                "}\n"
2442                "+ (id)init;\n"
2443                "@end");
2444 }
2445 
2446 TEST_F(FormatTest, FormatObjCImplementation) {
2447   verifyFormat("@implementation Foo : NSObject {\n"
2448                "@public\n"
2449                "  int field1;\n"
2450                "@protected\n"
2451                "  int field2;\n"
2452                "@private\n"
2453                "  int field3;\n"
2454                "@package\n"
2455                "  int field4;\n"
2456                "}\n"
2457                "+ (id)init {\n}\n"
2458                "@end");
2459 
2460   verifyGoogleFormat("@implementation Foo : NSObject {\n"
2461                      " @public\n"
2462                      "  int field1;\n"
2463                      " @protected\n"
2464                      "  int field2;\n"
2465                      " @private\n"
2466                      "  int field3;\n"
2467                      " @package\n"
2468                      "  int field4;\n"
2469                      "}\n"
2470                      "+ (id)init {\n}\n"
2471                      "@end");
2472 
2473   verifyFormat("@implementation Foo\n"
2474                "+ (id)init {\n"
2475                "  if (true)\n"
2476                "    return nil;\n"
2477                "}\n"
2478                "// Look, a comment!\n"
2479                "- (int)answerWith:(int)i {\n"
2480                "  return i;\n"
2481                "}\n"
2482                "+ (int)answerWith:(int)i {\n"
2483                "  return i;\n"
2484                "}\n"
2485                "@end");
2486 
2487   verifyFormat("@implementation Foo\n"
2488                "@end\n"
2489                "@implementation Bar\n"
2490                "@end");
2491 
2492   verifyFormat("@implementation Foo : Bar\n"
2493                "+ (id)init {\n}\n"
2494                "- (void)foo {\n}\n"
2495                "@end");
2496 
2497   verifyFormat("@implementation Foo {\n"
2498                "  int _i;\n"
2499                "}\n"
2500                "+ (id)init {\n}\n"
2501                "@end");
2502 
2503   verifyFormat("@implementation Foo : Bar {\n"
2504                "  int _i;\n"
2505                "}\n"
2506                "+ (id)init {\n}\n"
2507                "@end");
2508 
2509   verifyFormat("@implementation Foo (HackStuff)\n"
2510                "+ (id)init {\n}\n"
2511                "@end");
2512 }
2513 
2514 TEST_F(FormatTest, FormatObjCProtocol) {
2515   verifyFormat("@protocol Foo\n"
2516                "@property(weak) id delegate;\n"
2517                "- (NSUInteger)numberOfThings;\n"
2518                "@end");
2519 
2520   verifyFormat("@protocol MyProtocol <NSObject>\n"
2521                "- (NSUInteger)numberOfThings;\n"
2522                "@end");
2523 
2524   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
2525                      "- (NSUInteger)numberOfThings;\n"
2526                      "@end");
2527 
2528   verifyFormat("@protocol Foo;\n"
2529                "@protocol Bar;\n");
2530 
2531   verifyFormat("@protocol Foo\n"
2532                "@end\n"
2533                "@protocol Bar\n"
2534                "@end");
2535 
2536   verifyFormat("@protocol myProtocol\n"
2537                "- (void)mandatoryWithInt:(int)i;\n"
2538                "@optional\n"
2539                "- (void)optional;\n"
2540                "@required\n"
2541                "- (void)required;\n"
2542                "@optional\n"
2543                "@property(assign) int madProp;\n"
2544                "@end\n");
2545 }
2546 
2547 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
2548   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
2549                "                   rect:(NSRect)theRect\n"
2550                "               interval:(float)theInterval {\n"
2551                "}");
2552   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
2553                "          longKeyword:(NSRect)theRect\n"
2554                "    evenLongerKeyword:(float)theInterval\n"
2555                "                error:(NSError **)theError {\n"
2556                "}");
2557 }
2558 
2559 TEST_F(FormatTest, FormatObjCMethodExpr) {
2560   verifyFormat("[foo bar:baz];");
2561   verifyFormat("return [foo bar:baz];");
2562   verifyFormat("f([foo bar:baz]);");
2563   verifyFormat("f(2, [foo bar:baz]);");
2564   verifyFormat("f(2, a ? b : c);");
2565   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
2566 
2567   // Unary operators.
2568   verifyFormat("int a = +[foo bar:baz];");
2569   verifyFormat("int a = -[foo bar:baz];");
2570   verifyFormat("int a = ![foo bar:baz];");
2571   verifyFormat("int a = ~[foo bar:baz];");
2572   verifyFormat("int a = ++[foo bar:baz];");
2573   verifyFormat("int a = --[foo bar:baz];");
2574   verifyFormat("int a = sizeof [foo bar:baz];");
2575   verifyFormat("int a = alignof [foo bar:baz];");
2576   verifyFormat("int a = &[foo bar:baz];");
2577   verifyFormat("int a = *[foo bar:baz];");
2578   // FIXME: Make casts work, without breaking f()[4].
2579   //verifyFormat("int a = (int)[foo bar:baz];");
2580   //verifyFormat("return (int)[foo bar:baz];");
2581   //verifyFormat("(void)[foo bar:baz];");
2582   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
2583 
2584   // Binary operators.
2585   verifyFormat("[foo bar:baz], [foo bar:baz];");
2586   verifyFormat("[foo bar:baz] = [foo bar:baz];");
2587   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
2588   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
2589   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
2590   verifyFormat("[foo bar:baz] += [foo bar:baz];");
2591   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
2592   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
2593   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
2594   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
2595   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
2596   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
2597   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
2598   verifyFormat("[foo bar:baz] || [foo bar:baz];");
2599   verifyFormat("[foo bar:baz] && [foo bar:baz];");
2600   verifyFormat("[foo bar:baz] | [foo bar:baz];");
2601   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
2602   verifyFormat("[foo bar:baz] & [foo bar:baz];");
2603   verifyFormat("[foo bar:baz] == [foo bar:baz];");
2604   verifyFormat("[foo bar:baz] != [foo bar:baz];");
2605   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
2606   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
2607   verifyFormat("[foo bar:baz] > [foo bar:baz];");
2608   verifyFormat("[foo bar:baz] < [foo bar:baz];");
2609   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
2610   verifyFormat("[foo bar:baz] << [foo bar:baz];");
2611   verifyFormat("[foo bar:baz] - [foo bar:baz];");
2612   verifyFormat("[foo bar:baz] + [foo bar:baz];");
2613   verifyFormat("[foo bar:baz] * [foo bar:baz];");
2614   verifyFormat("[foo bar:baz] / [foo bar:baz];");
2615   verifyFormat("[foo bar:baz] % [foo bar:baz];");
2616   // Whew!
2617 
2618   verifyFormat("return in[42];");
2619   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
2620                "}");
2621 
2622   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
2623   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
2624   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
2625   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
2626   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
2627   verifyFormat("[button setAction:@selector(zoomOut:)];");
2628   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
2629 
2630   verifyFormat("arr[[self indexForFoo:a]];");
2631   verifyFormat("throw [self errorFor:a];");
2632   verifyFormat("@throw [self errorFor:a];");
2633 
2634   // This tests that the formatter doesn't break after "backing" but before ":",
2635   // which would be at 80 columns.
2636   verifyFormat(
2637       "void f() {\n"
2638       "  if ((self = [super initWithContentRect:contentRect\n"
2639       "                               styleMask:styleMask\n"
2640       "                                 backing:NSBackingStoreBuffered\n"
2641       "                                   defer:YES]))");
2642 
2643   verifyFormat(
2644       "[foo checkThatBreakingAfterColonWorksOk:\n"
2645       "        [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
2646 
2647   verifyFormat("[myObj short:arg1 // Force line break\n"
2648                "          longKeyword:arg2\n"
2649                "    evenLongerKeyword:arg3\n"
2650                "                error:arg4];");
2651   verifyFormat(
2652       "void f() {\n"
2653       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
2654       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
2655       "                                     pos.width(), pos.height())\n"
2656       "                styleMask:NSBorderlessWindowMask\n"
2657       "                  backing:NSBackingStoreBuffered\n"
2658       "                    defer:NO]);\n"
2659       "}");
2660   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
2661                "                             with:contentsNativeView];");
2662 
2663   verifyFormat(
2664       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
2665       "           owner:nillllll];");
2666 
2667   verifyFormat(
2668       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
2669       "        forType:kBookmarkButtonDragType];");
2670 
2671   verifyFormat("[defaultCenter addObserver:self\n"
2672                "                  selector:@selector(willEnterFullscreen)\n"
2673                "                      name:kWillEnterFullscreenNotification\n"
2674                "                    object:nil];");
2675   verifyFormat("[image_rep drawInRect:drawRect\n"
2676                "             fromRect:NSZeroRect\n"
2677                "            operation:NSCompositeCopy\n"
2678                "             fraction:1.0\n"
2679                "       respectFlipped:NO\n"
2680                "                hints:nil];");
2681 
2682   verifyFormat(
2683       "scoped_nsobject<NSTextField> message(\n"
2684       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
2685       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
2686 }
2687 
2688 TEST_F(FormatTest, ObjCAt) {
2689   verifyFormat("@autoreleasepool");
2690   verifyFormat("@catch");
2691   verifyFormat("@class");
2692   verifyFormat("@compatibility_alias");
2693   verifyFormat("@defs");
2694   verifyFormat("@dynamic");
2695   verifyFormat("@encode");
2696   verifyFormat("@end");
2697   verifyFormat("@finally");
2698   verifyFormat("@implementation");
2699   verifyFormat("@import");
2700   verifyFormat("@interface");
2701   verifyFormat("@optional");
2702   verifyFormat("@package");
2703   verifyFormat("@private");
2704   verifyFormat("@property");
2705   verifyFormat("@protected");
2706   verifyFormat("@protocol");
2707   verifyFormat("@public");
2708   verifyFormat("@required");
2709   verifyFormat("@selector");
2710   verifyFormat("@synchronized");
2711   verifyFormat("@synthesize");
2712   verifyFormat("@throw");
2713   verifyFormat("@try");
2714 
2715   EXPECT_EQ("@interface", format("@ interface"));
2716 
2717   // The precise formatting of this doesn't matter, nobody writes code like
2718   // this.
2719   verifyFormat("@ /*foo*/ interface");
2720 }
2721 
2722 TEST_F(FormatTest, ObjCSnippets) {
2723   verifyFormat("@autoreleasepool {\n"
2724                "  foo();\n"
2725                "}");
2726   verifyFormat("@class Foo, Bar;");
2727   verifyFormat("@compatibility_alias AliasName ExistingClass;");
2728   verifyFormat("@dynamic textColor;");
2729   verifyFormat("char *buf1 = @encode(int *);");
2730   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
2731   verifyFormat("char *buf1 = @encode(int **);");
2732   verifyFormat("Protocol *proto = @protocol(p1);");
2733   verifyFormat("SEL s = @selector(foo:);");
2734   verifyFormat("@synchronized(self) {\n"
2735                "  f();\n"
2736                "}");
2737 
2738   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2739   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
2740 
2741   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
2742   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
2743   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
2744 }
2745 
2746 TEST_F(FormatTest, ObjCLiterals) {
2747   verifyFormat("@\"String\"");
2748   verifyFormat("@1");
2749   verifyFormat("@+4.8");
2750   verifyFormat("@-4");
2751   verifyFormat("@1LL");
2752   verifyFormat("@.5");
2753   verifyFormat("@'c'");
2754   verifyFormat("@true");
2755 
2756   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
2757   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
2758   verifyFormat("NSNumber *favoriteColor = @(Green);");
2759   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
2760 
2761   verifyFormat("@[");
2762   verifyFormat("@[]");
2763   verifyFormat(
2764       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
2765   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
2766 
2767   verifyFormat("@{");
2768   verifyFormat("@{}");
2769   verifyFormat("@{ @\"one\" : @1 }");
2770   verifyFormat("return @{ @\"one\" : @1 };");
2771   verifyFormat("@{ @\"one\" : @1, }");
2772   verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
2773   verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
2774   verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
2775   verifyFormat("[self setDict:@{}");
2776   verifyFormat("[self setDict:@{ @1 : @2 }");
2777   verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
2778   verifyFormat(
2779       "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
2780   verifyFormat(
2781       "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
2782 
2783   // FIXME: Nested and multi-line array and dictionary literals need more work.
2784   verifyFormat(
2785       "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
2786       "                     @\"processInfo\" : [NSProcessInfo processInfo] };");
2787 }
2788 
2789 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
2790   EXPECT_EQ("{\n"
2791             "{\n"
2792             "a;\n"
2793             "b;\n"
2794             "}\n"
2795             "}",
2796             format("{\n"
2797                    "{\n"
2798                    "a;\n"
2799                    "     b;\n"
2800                    "}\n"
2801                    "}",
2802                    13, 2, getLLVMStyle()));
2803   EXPECT_EQ("{\n"
2804             "{\n"
2805             "  a;\n"
2806             "b;\n"
2807             "}\n"
2808             "}",
2809             format("{\n"
2810                    "{\n"
2811                    "     a;\n"
2812                    "b;\n"
2813                    "}\n"
2814                    "}",
2815                    9, 2, getLLVMStyle()));
2816   EXPECT_EQ("{\n"
2817             "{\n"
2818             "public:\n"
2819             "  b;\n"
2820             "}\n"
2821             "}",
2822             format("{\n"
2823                    "{\n"
2824                    "public:\n"
2825                    "     b;\n"
2826                    "}\n"
2827                    "}",
2828                    17, 2, getLLVMStyle()));
2829   EXPECT_EQ("{\n"
2830             "{\n"
2831             "a;\n"
2832             "}\n"
2833             "{\n"
2834             "  b;\n"
2835             "}\n"
2836             "}",
2837             format("{\n"
2838                    "{\n"
2839                    "a;\n"
2840                    "}\n"
2841                    "{\n"
2842                    "           b;\n"
2843                    "}\n"
2844                    "}",
2845                    22, 2, getLLVMStyle()));
2846   EXPECT_EQ("  {\n"
2847             "    a;\n"
2848             "  }",
2849             format("  {\n"
2850                    "a;\n"
2851                    "  }",
2852                    4, 2, getLLVMStyle()));
2853   EXPECT_EQ("void f() {}\n"
2854             "void g() {}",
2855             format("void f() {}\n"
2856                    "void g() {}",
2857                    13, 0, getLLVMStyle()));
2858 }
2859 
2860 TEST_F(FormatTest, BreakStringLiterals) {
2861   EXPECT_EQ("\"some text \"\n"
2862             "\"other\";",
2863             format("\"some text other\";", getLLVMStyleWithColumns(12)));
2864   EXPECT_EQ(
2865       "#define A  \\\n"
2866       "  \"some \"  \\\n"
2867       "  \"text \"  \\\n"
2868       "  \"other\";",
2869       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
2870   EXPECT_EQ(
2871       "#define A  \\\n"
2872       "  \"so \"    \\\n"
2873       "  \"text \"  \\\n"
2874       "  \"other\";",
2875       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
2876 
2877   EXPECT_EQ("\"some text\"",
2878             format("\"some text\"", getLLVMStyleWithColumns(1)));
2879   EXPECT_EQ("\"some text\"",
2880             format("\"some text\"", getLLVMStyleWithColumns(11)));
2881   EXPECT_EQ("\"some \"\n"
2882             "\"text\"",
2883             format("\"some text\"", getLLVMStyleWithColumns(10)));
2884   EXPECT_EQ("\"some \"\n"
2885             "\"text\"",
2886             format("\"some text\"", getLLVMStyleWithColumns(7)));
2887   EXPECT_EQ("\"some text\"",
2888             format("\"some text\"", getLLVMStyleWithColumns(6)));
2889 
2890   EXPECT_EQ("variable =\n"
2891             "    \"long string \"\n"
2892             "    \"literal\";",
2893             format("variable = \"long string literal\";",
2894                    getLLVMStyleWithColumns(20)));
2895 
2896   EXPECT_EQ("variable = f(\n"
2897             "    \"long string \"\n"
2898             "    \"literal\", short,\n"
2899             "    loooooooooooooooooooong);",
2900             format("variable = f(\"long string literal\", short, "
2901                    "loooooooooooooooooooong);",
2902                    getLLVMStyleWithColumns(20)));
2903   EXPECT_EQ(
2904       "f(\"one two\".split(\n"
2905       "    variable));",
2906       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
2907   EXPECT_EQ("f(\"one two three four five six \"\n"
2908             "  \"seven\".split(\n"
2909             "      really_looooong_variable));",
2910             format("f(\"one two three four five six seven\"."
2911                    "split(really_looooong_variable));",
2912                    getLLVMStyleWithColumns(33)));
2913 
2914   EXPECT_EQ("f(\"some \"\n"
2915             "  \"text\",\n"
2916             "  other);",
2917             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
2918 }
2919 
2920 } // end namespace tooling
2921 } // end namespace clang
2922