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