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 "clang/Lex/Lexer.h"
14 #include "llvm/Support/Debug.h"
15 #include "gtest/gtest.h"
16 
17 namespace clang {
18 namespace format {
19 
20 class FormatTest : public ::testing::Test {
21 protected:
22   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
23                      const FormatStyle &Style) {
24     DEBUG(llvm::errs() << "---\n");
25     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
26     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
27     ReplacementCount = Replaces.size();
28     std::string Result = applyAllReplacements(Code, Replaces);
29     EXPECT_NE("", Result);
30     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31     return Result;
32   }
33 
34   std::string
35   format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
36     return format(Code, 0, Code.size(), Style);
37   }
38 
39   std::string messUp(llvm::StringRef Code) {
40     std::string MessedUp(Code.str());
41     bool InComment = false;
42     bool InPreprocessorDirective = false;
43     bool JustReplacedNewline = false;
44     for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
45       if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
46         if (JustReplacedNewline)
47           MessedUp[i - 1] = '\n';
48         InComment = true;
49       } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
50         if (i != 0)
51           MessedUp[i - 1] = '\n';
52         InPreprocessorDirective = true;
53       } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
54         MessedUp[i] = ' ';
55         MessedUp[i + 1] = ' ';
56       } else if (MessedUp[i] == '\n') {
57         if (InComment) {
58           InComment = false;
59         } else if (InPreprocessorDirective) {
60           InPreprocessorDirective = false;
61         } else {
62           JustReplacedNewline = true;
63           MessedUp[i] = ' ';
64         }
65       } else if (MessedUp[i] != ' ') {
66         JustReplacedNewline = false;
67       }
68     }
69     return MessedUp;
70   }
71 
72   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
73     FormatStyle Style = getLLVMStyle();
74     Style.ColumnLimit = ColumnLimit;
75     return Style;
76   }
77 
78   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
79     FormatStyle Style = getGoogleStyle();
80     Style.ColumnLimit = ColumnLimit;
81     return Style;
82   }
83 
84   void verifyFormat(llvm::StringRef Code,
85                     const FormatStyle &Style = getLLVMStyle()) {
86     EXPECT_EQ(Code.str(), format(messUp(Code), Style));
87   }
88 
89   void verifyGoogleFormat(llvm::StringRef Code) {
90     verifyFormat(Code, getGoogleStyle());
91   }
92 
93   void verifyIndependentOfContext(llvm::StringRef text) {
94     verifyFormat(text);
95     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
96   }
97 
98   int ReplacementCount;
99 };
100 
101 TEST_F(FormatTest, MessUp) {
102   EXPECT_EQ("1 2 3", messUp("1 2 3"));
103   EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
104   EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
105   EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
106   EXPECT_EQ("a\n#b  c  d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
107 }
108 
109 //===----------------------------------------------------------------------===//
110 // Basic function tests.
111 //===----------------------------------------------------------------------===//
112 
113 TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
114   EXPECT_EQ(";", format(";"));
115 }
116 
117 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
118   EXPECT_EQ("int i;", format("  int i;"));
119   EXPECT_EQ("\nint i;", format(" \n\t \r  int i;"));
120   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
121   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
122 }
123 
124 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
125   EXPECT_EQ("int i;", format("int\ni;"));
126 }
127 
128 TEST_F(FormatTest, FormatsNestedBlockStatements) {
129   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
130 }
131 
132 TEST_F(FormatTest, FormatsNestedCall) {
133   verifyFormat("Method(f1, f2(f3));");
134   verifyFormat("Method(f1(f2, f3()));");
135   verifyFormat("Method(f1(f2, (f3())));");
136 }
137 
138 TEST_F(FormatTest, NestedNameSpecifiers) {
139   verifyFormat("vector< ::Type> v;");
140   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
141 }
142 
143 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
144   EXPECT_EQ("if (a) {\n"
145             "  f();\n"
146             "}",
147             format("if(a){f();}"));
148   EXPECT_EQ(4, ReplacementCount);
149   EXPECT_EQ("if (a) {\n"
150             "  f();\n"
151             "}",
152             format("if (a) {\n"
153                    "  f();\n"
154                    "}"));
155   EXPECT_EQ(0, ReplacementCount);
156 }
157 
158 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
159   EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
160   EXPECT_EQ("int a;", format("int a;         "));
161   EXPECT_EQ("int a;\n", format("int a;  \n   \n   \n "));
162   EXPECT_EQ("int a;\nint b;    ",
163             format("int a;  \nint b;    ", 0, 0, getLLVMStyle()));
164 }
165 
166 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
167   EXPECT_EQ("int b;\nint a;",
168             format("int b;\n   int a;", 7, 0, getLLVMStyle()));
169   EXPECT_EQ("int b;\n   int a;",
170             format("int b;\n   int a;", 6, 0, getLLVMStyle()));
171 
172   EXPECT_EQ("#define A  \\\n"
173             "  int a;   \\\n"
174             "  int b;",
175             format("#define A  \\\n"
176                    "  int a;   \\\n"
177                    "    int b;",
178                    26, 0, getLLVMStyleWithColumns(12)));
179   EXPECT_EQ("#define A  \\\n"
180             "  int a;   \\\n"
181             "    int b;",
182             format("#define A  \\\n"
183                    "  int a;   \\\n"
184                    "    int b;",
185                    25, 0, getLLVMStyleWithColumns(12)));
186 }
187 
188 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
189   EXPECT_EQ("int  a;\n\n int b;",
190             format("int  a;\n  \n\n int b;", 7, 0, getLLVMStyle()));
191   EXPECT_EQ("int  a;\n\n int b;",
192             format("int  a;\n  \n\n int b;", 9, 0, getLLVMStyle()));
193 }
194 
195 TEST_F(FormatTest, RemovesEmptyLines) {
196   EXPECT_EQ("class C {\n"
197             "  int i;\n"
198             "};",
199             format("class C {\n"
200                    " int i;\n"
201                    "\n"
202                    "};"));
203 
204   // Don't remove empty lines in more complex control statements.
205   EXPECT_EQ("void f() {\n"
206             "  if (a) {\n"
207             "    f();\n"
208             "\n"
209             "  } else if (b) {\n"
210             "    f();\n"
211             "  }\n"
212             "}",
213             format("void f() {\n"
214                    "  if (a) {\n"
215                    "    f();\n"
216                    "\n"
217                    "  } else if (b) {\n"
218                    "    f();\n"
219                    "\n"
220                    "  }\n"
221                    "\n"
222                    "}"));
223 
224   // FIXME: This is slightly inconsistent.
225   EXPECT_EQ("namespace {\n"
226             "int i;\n"
227             "}",
228             format("namespace {\n"
229                    "int i;\n"
230                    "\n"
231                    "}"));
232   EXPECT_EQ("namespace {\n"
233             "int i;\n"
234             "\n"
235             "} // namespace",
236             format("namespace {\n"
237                    "int i;\n"
238                    "\n"
239                    "}  // namespace"));
240 }
241 
242 TEST_F(FormatTest, ReformatsMovedLines) {
243   EXPECT_EQ(
244       "template <typename T> T *getFETokenInfo() const {\n"
245       "  return static_cast<T *>(FETokenInfo);\n"
246       "}\n"
247       "  int a; // <- Should not be formatted",
248       format(
249           "template<typename T>\n"
250           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
251           "  int a; // <- Should not be formatted",
252           9, 5, getLLVMStyle()));
253 }
254 
255 //===----------------------------------------------------------------------===//
256 // Tests for control statements.
257 //===----------------------------------------------------------------------===//
258 
259 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
260   verifyFormat("if (true)\n  f();\ng();");
261   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
262   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
263 
264   FormatStyle AllowsMergedIf = getLLVMStyle();
265   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
266   verifyFormat("if (a)\n"
267                "  // comment\n"
268                "  f();",
269                AllowsMergedIf);
270   verifyFormat("if (a)\n"
271                "  ;",
272                AllowsMergedIf);
273   verifyFormat("if (a)\n"
274                "  if (b) return;",
275                AllowsMergedIf);
276 
277   verifyFormat("if (a) // Can't merge this\n"
278                "  f();\n",
279                AllowsMergedIf);
280   verifyFormat("if (a) /* still don't merge */\n"
281                "  f();",
282                AllowsMergedIf);
283   verifyFormat("if (a) { // Never merge this\n"
284                "  f();\n"
285                "}",
286                AllowsMergedIf);
287   verifyFormat("if (a) { /* Never merge this */\n"
288                "  f();\n"
289                "}",
290                AllowsMergedIf);
291 
292   EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
293   EXPECT_EQ("if (a) return; // comment",
294             format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
295 
296   AllowsMergedIf.ColumnLimit = 14;
297   verifyFormat("if (a) return;", AllowsMergedIf);
298   verifyFormat("if (aaaaaaaaa)\n"
299                "  return;",
300                AllowsMergedIf);
301 
302   AllowsMergedIf.ColumnLimit = 13;
303   verifyFormat("if (a)\n  return;", AllowsMergedIf);
304 }
305 
306 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
307   FormatStyle AllowsMergedLoops = getLLVMStyle();
308   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
309   verifyFormat("while (true) continue;", AllowsMergedLoops);
310   verifyFormat("for (;;) continue;", AllowsMergedLoops);
311   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
312   verifyFormat("while (true)\n"
313                "  ;",
314                AllowsMergedLoops);
315   verifyFormat("for (;;)\n"
316                "  ;",
317                AllowsMergedLoops);
318   verifyFormat("for (;;)\n"
319                "  for (;;) continue;",
320                AllowsMergedLoops);
321   verifyFormat("for (;;) // Can't merge this\n"
322                "  continue;",
323                AllowsMergedLoops);
324   verifyFormat("for (;;) /* still don't merge */\n"
325                "  continue;",
326                AllowsMergedLoops);
327 }
328 
329 TEST_F(FormatTest, ParseIfElse) {
330   verifyFormat("if (true)\n"
331                "  if (true)\n"
332                "    if (true)\n"
333                "      f();\n"
334                "    else\n"
335                "      g();\n"
336                "  else\n"
337                "    h();\n"
338                "else\n"
339                "  i();");
340   verifyFormat("if (true)\n"
341                "  if (true)\n"
342                "    if (true) {\n"
343                "      if (true)\n"
344                "        f();\n"
345                "    } else {\n"
346                "      g();\n"
347                "    }\n"
348                "  else\n"
349                "    h();\n"
350                "else {\n"
351                "  i();\n"
352                "}");
353 }
354 
355 TEST_F(FormatTest, ElseIf) {
356   verifyFormat("if (a) {\n} else if (b) {\n}");
357   verifyFormat("if (a)\n"
358                "  f();\n"
359                "else if (b)\n"
360                "  g();\n"
361                "else\n"
362                "  h();");
363 }
364 
365 TEST_F(FormatTest, FormatsForLoop) {
366   verifyFormat(
367       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
368       "     ++VeryVeryLongLoopVariable)\n"
369       "  ;");
370   verifyFormat("for (;;)\n"
371                "  f();");
372   verifyFormat("for (;;) {\n}");
373   verifyFormat("for (;;) {\n"
374                "  f();\n"
375                "}");
376   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
377 
378   verifyFormat(
379       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
380       "                                          E = UnwrappedLines.end();\n"
381       "     I != E; ++I) {\n}");
382 
383   verifyFormat(
384       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
385       "     ++IIIII) {\n}");
386   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
387                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
388                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
389   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
390                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
391                "         E = FD->getDeclsInPrototypeScope().end();\n"
392                "     I != E; ++I) {\n}");
393 
394   // FIXME: Not sure whether we want extra identation in line 3 here:
395   verifyFormat(
396       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
397       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
398       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
399       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
400       "     ++aaaaaaaaaaa) {\n}");
401   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
402                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
403                "}");
404   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
405                "         aaaaaaaaaa);\n"
406                "     iter; ++iter) {\n"
407                "}");
408 
409   FormatStyle NoBinPacking = getLLVMStyle();
410   NoBinPacking.BinPackParameters = false;
411   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
412                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
413                "                                           aaaaaaaaaaaaaaaa,\n"
414                "                                           aaaaaaaaaaaaaaaa,\n"
415                "                                           aaaaaaaaaaaaaaaa);\n"
416                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
417                "}",
418                NoBinPacking);
419   verifyFormat(
420       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
421       "                                          E = UnwrappedLines.end();\n"
422       "     I != E;\n"
423       "     ++I) {\n}",
424       NoBinPacking);
425 }
426 
427 TEST_F(FormatTest, RangeBasedForLoops) {
428   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
429                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
430   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
431                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
432   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
433                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
434 }
435 
436 TEST_F(FormatTest, FormatsWhileLoop) {
437   verifyFormat("while (true) {\n}");
438   verifyFormat("while (true)\n"
439                "  f();");
440   verifyFormat("while () {\n}");
441   verifyFormat("while () {\n"
442                "  f();\n"
443                "}");
444 }
445 
446 TEST_F(FormatTest, FormatsDoWhile) {
447   verifyFormat("do {\n"
448                "  do_something();\n"
449                "} while (something());");
450   verifyFormat("do\n"
451                "  do_something();\n"
452                "while (something());");
453 }
454 
455 TEST_F(FormatTest, FormatsSwitchStatement) {
456   verifyFormat("switch (x) {\n"
457                "case 1:\n"
458                "  f();\n"
459                "  break;\n"
460                "case kFoo:\n"
461                "case ns::kBar:\n"
462                "case kBaz:\n"
463                "  break;\n"
464                "default:\n"
465                "  g();\n"
466                "  break;\n"
467                "}");
468   verifyFormat("switch (x) {\n"
469                "case 1: {\n"
470                "  f();\n"
471                "  break;\n"
472                "}\n"
473                "}");
474   verifyFormat("switch (x) {\n"
475                "case 1: {\n"
476                "  f();\n"
477                "  {\n"
478                "    g();\n"
479                "    h();\n"
480                "  }\n"
481                "  break;\n"
482                "}\n"
483                "}");
484   verifyFormat("switch (x) {\n"
485                "case 1: {\n"
486                "  f();\n"
487                "  if (foo) {\n"
488                "    g();\n"
489                "    h();\n"
490                "  }\n"
491                "  break;\n"
492                "}\n"
493                "}");
494   verifyFormat("switch (x) {\n"
495                "case 1: {\n"
496                "  f();\n"
497                "  g();\n"
498                "} break;\n"
499                "}");
500   verifyFormat("switch (test)\n"
501                "  ;");
502   verifyFormat("switch (x) {\n"
503                "default: {\n"
504                "  // Do nothing.\n"
505                "}\n"
506                "}");
507   verifyFormat("switch (x) {\n"
508                "// comment\n"
509                "// if 1, do f()\n"
510                "case 1:\n"
511                "  f();\n"
512                "}");
513   verifyFormat("switch (x) {\n"
514                "case 1:\n"
515                "  // Do amazing stuff\n"
516                "  {\n"
517                "    f();\n"
518                "    g();\n"
519                "  }\n"
520                "  break;\n"
521                "}");
522   verifyFormat("#define A          \\\n"
523                "  switch (x) {     \\\n"
524                "  case a:          \\\n"
525                "    foo = b;       \\\n"
526                "  }", getLLVMStyleWithColumns(20));
527 
528   verifyGoogleFormat("switch (x) {\n"
529                      "  case 1:\n"
530                      "    f();\n"
531                      "    break;\n"
532                      "  case kFoo:\n"
533                      "  case ns::kBar:\n"
534                      "  case kBaz:\n"
535                      "    break;\n"
536                      "  default:\n"
537                      "    g();\n"
538                      "    break;\n"
539                      "}");
540   verifyGoogleFormat("switch (x) {\n"
541                      "  case 1: {\n"
542                      "    f();\n"
543                      "    break;\n"
544                      "  }\n"
545                      "}");
546   verifyGoogleFormat("switch (test)\n"
547                      "    ;");
548 }
549 
550 TEST_F(FormatTest, FormatsLabels) {
551   verifyFormat("void f() {\n"
552                "  some_code();\n"
553                "test_label:\n"
554                "  some_other_code();\n"
555                "  {\n"
556                "    some_more_code();\n"
557                "  another_label:\n"
558                "    some_more_code();\n"
559                "  }\n"
560                "}");
561   verifyFormat("some_code();\n"
562                "test_label:\n"
563                "some_other_code();");
564 }
565 
566 //===----------------------------------------------------------------------===//
567 // Tests for comments.
568 //===----------------------------------------------------------------------===//
569 
570 TEST_F(FormatTest, UnderstandsSingleLineComments) {
571   verifyFormat("//* */");
572   verifyFormat("// line 1\n"
573                "// line 2\n"
574                "void f() {}\n");
575 
576   verifyFormat("void f() {\n"
577                "  // Doesn't do anything\n"
578                "}");
579   verifyFormat("void f(int i,  // some comment (probably for i)\n"
580                "       int j,  // some comment (probably for j)\n"
581                "       int k); // some comment (probably for k)");
582   verifyFormat("void f(int i,\n"
583                "       // some comment (probably for j)\n"
584                "       int j,\n"
585                "       // some comment (probably for k)\n"
586                "       int k);");
587 
588   verifyFormat("int i    // This is a fancy variable\n"
589                "    = 5; // with nicely aligned comment.");
590 
591   verifyFormat("// Leading comment.\n"
592                "int a; // Trailing comment.");
593   verifyFormat("int a; // Trailing comment\n"
594                "       // on 2\n"
595                "       // or 3 lines.\n"
596                "int b;");
597   verifyFormat("int a; // Trailing comment\n"
598                "\n"
599                "// Leading comment.\n"
600                "int b;");
601   verifyFormat("int a;    // Comment.\n"
602                "          // More details.\n"
603                "int bbbb; // Another comment.");
604   verifyFormat(
605       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
606       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
607       "int cccccccccccccccccccccccccccccc;       // comment\n"
608       "int ddd;                     // looooooooooooooooooooooooong comment\n"
609       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
610       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
611       "int ccccccccccccccccccc;     // comment");
612 
613   verifyFormat("#include \"a\"     // comment\n"
614                "#include \"a/b/c\" // comment");
615   verifyFormat("#include <a>     // comment\n"
616                "#include <a/b/c> // comment");
617   EXPECT_EQ("#include \\\n"
618             "  \"a\"            // comment\n"
619             "#include \"a/b/c\" // comment",
620             format("#include \\\n"
621                    "  \"a\" // comment\n"
622                    "#include \"a/b/c\" // comment"));
623 
624   verifyFormat("enum E {\n"
625                "  // comment\n"
626                "  VAL_A, // comment\n"
627                "  VAL_B\n"
628                "};");
629 
630   verifyFormat(
631       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
632       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
633   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
634                "    // Comment inside a statement.\n"
635                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
636   verifyFormat(
637       "bool aaaaaaaaaaaaa = // comment\n"
638       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
639       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
640 
641   verifyFormat("int aaaa; // aaaaa\n"
642                "int aa;   // aaaaaaa",
643                getLLVMStyleWithColumns(20));
644 
645   EXPECT_EQ("void f() { // This does something ..\n"
646             "}\n"
647             "int a; // This is unrelated",
648             format("void f()    {     // This does something ..\n"
649                    "  }\n"
650                    "int   a;     // This is unrelated"));
651   EXPECT_EQ("void f() { // This does something ..\n"
652             "}          // awesome..\n"
653             "\n"
654             "int a; // This is unrelated",
655             format("void f()    { // This does something ..\n"
656                    "      } // awesome..\n"
657                    " \n"
658                    "int a;    // This is unrelated"));
659 
660   EXPECT_EQ("int i; // single line trailing comment",
661             format("int i;\\\n// single line trailing comment"));
662 
663   verifyGoogleFormat("int a;  // Trailing comment.");
664 
665   verifyFormat("someFunction(anotherFunction( // Force break.\n"
666                "    parameter));");
667 
668   verifyGoogleFormat("#endif  // HEADER_GUARD");
669 
670   verifyFormat("const char *test[] = {\n"
671                "  // A\n"
672                "  \"aaaa\",\n"
673                "  // B\n"
674                "  \"aaaaa\",\n"
675                "};");
676   verifyGoogleFormat(
677       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
678       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
679   EXPECT_EQ("D(a, {\n"
680             "  // test\n"
681             "  int a;\n"
682             "});",
683             format("D(a, {\n"
684                    "// test\n"
685                    "int a;\n"
686                    "});"));
687 
688   EXPECT_EQ("lineWith(); // comment\n"
689             "// at start\n"
690             "otherLine();",
691             format("lineWith();   // comment\n"
692                    "// at start\n"
693                    "otherLine();"));
694   EXPECT_EQ("lineWith(); // comment\n"
695             "            // at start\n"
696             "otherLine();",
697             format("lineWith();   // comment\n"
698                    " // at start\n"
699                    "otherLine();"));
700 
701   EXPECT_EQ("lineWith(); // comment\n"
702             "// at start\n"
703             "otherLine(); // comment",
704             format("lineWith();   // comment\n"
705                    "// at start\n"
706                    "otherLine();   // comment"));
707   EXPECT_EQ("lineWith();\n"
708             "// at start\n"
709             "otherLine(); // comment",
710             format("lineWith();\n"
711                    " // at start\n"
712                    "otherLine();   // comment"));
713   EXPECT_EQ("// first\n"
714             "// at start\n"
715             "otherLine(); // comment",
716             format("// first\n"
717                    " // at start\n"
718                    "otherLine();   // comment"));
719   EXPECT_EQ("f();\n"
720             "// first\n"
721             "// at start\n"
722             "otherLine(); // comment",
723             format("f();\n"
724                    "// first\n"
725                    " // at start\n"
726                    "otherLine();   // comment"));
727 }
728 
729 TEST_F(FormatTest, CanFormatCommentsLocally) {
730   EXPECT_EQ("int a;    // comment\n"
731             "int    b; // comment",
732             format("int   a; // comment\n"
733                    "int    b; // comment",
734                    0, 0, getLLVMStyle()));
735   EXPECT_EQ("int   a; // comment\n"
736             "         // line 2\n"
737             "int b;",
738             format("int   a; // comment\n"
739                    "            // line 2\n"
740                    "int b;",
741                    28, 0, getLLVMStyle()));
742   EXPECT_EQ("int aaaaaa; // comment\n"
743             "int b;\n"
744             "int c; // unrelated comment",
745             format("int aaaaaa; // comment\n"
746                    "int b;\n"
747                    "int   c; // unrelated comment",
748                    31, 0, getLLVMStyle()));
749 }
750 
751 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
752   EXPECT_EQ("// comment", format("// comment  "));
753   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
754             format("int aaaaaaa, bbbbbbb; // comment                   ",
755                    getLLVMStyleWithColumns(33)));
756 }
757 
758 TEST_F(FormatTest, UnderstandsBlockComments) {
759   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
760   EXPECT_EQ(
761       "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
762       "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
763       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n/* Trailing comment for aa... */\n"
764              "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
765   EXPECT_EQ(
766       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
767       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
768       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
769              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
770   EXPECT_EQ(
771       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
772       "    aaaaaaaaaaaaaaaaaa,\n"
773       "    aaaaaaaaaaaaaaaaaa) { /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n"
774       "}",
775       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
776              "                      aaaaaaaaaaaaaaaaaa  ,\n"
777              "    aaaaaaaaaaaaaaaaaa) {   /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa */\n"
778              "}"));
779 
780   FormatStyle NoBinPacking = getLLVMStyle();
781   NoBinPacking.BinPackParameters = false;
782   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
783                "         /* parameter 2 */ aaaaaa,\n"
784                "         /* parameter 3 */ aaaaaa,\n"
785                "         /* parameter 4 */ aaaaaa);",
786                NoBinPacking);
787 }
788 
789 TEST_F(FormatTest, AlignsBlockComments) {
790   EXPECT_EQ("/*\n"
791             " * Really multi-line\n"
792             " * comment.\n"
793             " */\n"
794             "void f() {}",
795             format("  /*\n"
796                    "   * Really multi-line\n"
797                    "   * comment.\n"
798                    "   */\n"
799                    "  void f() {}"));
800   EXPECT_EQ("class C {\n"
801             "  /*\n"
802             "   * Another multi-line\n"
803             "   * comment.\n"
804             "   */\n"
805             "  void f() {}\n"
806             "};",
807             format("class C {\n"
808                    "/*\n"
809                    " * Another multi-line\n"
810                    " * comment.\n"
811                    " */\n"
812                    "void f() {}\n"
813                    "};"));
814   EXPECT_EQ("/*\n"
815             "  1. This is a comment with non-trivial formatting.\n"
816             "     1.1. We have to indent/outdent all lines equally\n"
817             "         1.1.1. to keep the formatting.\n"
818             " */",
819             format("  /*\n"
820                    "    1. This is a comment with non-trivial formatting.\n"
821                    "       1.1. We have to indent/outdent all lines equally\n"
822                    "           1.1.1. to keep the formatting.\n"
823                    "   */"));
824   EXPECT_EQ("/*\n"
825             "Don't try to outdent if there's not enough inentation.\n"
826             "*/",
827             format("  /*\n"
828                    " Don't try to outdent if there's not enough inentation.\n"
829                    " */"));
830 
831   EXPECT_EQ("int i; /* Comment with empty...\n"
832             "        *\n"
833             "        * line. */",
834             format("int i; /* Comment with empty...\n"
835                    "        *\n"
836                    "        * line. */"));
837 }
838 
839 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
840   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
841             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
842             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
843                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
844 }
845 
846 TEST_F(FormatTest, SplitsLongCxxComments) {
847   EXPECT_EQ("// A comment that\n"
848             "// doesn't fit on\n"
849             "// one line",
850             format("// A comment that doesn't fit on one line",
851                    getLLVMStyleWithColumns(20)));
852   EXPECT_EQ("// a b c d\n"
853             "// e f  g\n"
854             "// h i j k",
855             format("// a b c d e f  g h i j k",
856                    getLLVMStyleWithColumns(10)));
857   EXPECT_EQ("// a b c d\n"
858             "// e f  g\n"
859             "// h i j k",
860             format("\\\n// a b c d e f  g h i j k",
861                    getLLVMStyleWithColumns(10)));
862   EXPECT_EQ("if (true) // A comment that\n"
863             "          // doesn't fit on\n"
864             "          // one line",
865             format("if (true) // A comment that doesn't fit on one line   ",
866                    getLLVMStyleWithColumns(30)));
867   EXPECT_EQ("//    Don't_touch_leading_whitespace",
868             format("//    Don't_touch_leading_whitespace",
869                    getLLVMStyleWithColumns(20)));
870   EXPECT_EQ("// Add leading\n"
871             "// whitespace",
872             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
873   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
874   EXPECT_EQ("// Even if it makes the line exceed the column\n"
875             "// limit",
876             format("//Even if it makes the line exceed the column limit",
877                    getLLVMStyleWithColumns(51)));
878   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
879   EXPECT_EQ("// A comment before\n"
880             "// a macro\n"
881             "// definition\n"
882             "#define a b",
883             format("// A comment before a macro definition\n"
884                    "#define a b",
885                    getLLVMStyleWithColumns(20)));
886   EXPECT_EQ("void ffffff(int aaaaaaaaa,  // wwww\n"
887             "            int a, int bbb, // xxxxxxx\n"
888             "                            // yyyyyyyyy\n"
889             "            int c, int d, int e) {}",
890             format("void ffffff(\n"
891                    "    int aaaaaaaaa, // wwww\n"
892                    "    int a,\n"
893                    "    int bbb, // xxxxxxx yyyyyyyyy\n"
894                    "    int c, int d, int e) {}",
895                    getLLVMStyleWithColumns(40)));
896   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
897             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
898                    getLLVMStyleWithColumns(20)));
899 }
900 
901 TEST_F(FormatTest, PriorityOfCommentBreaking) {
902   EXPECT_EQ("if (xxx == yyy && // aaaaaaaaaaaa\n"
903             "                  // bbbbbbbbb\n"
904             "    zzz)\n"
905             "  q();",
906             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
907                    "    zzz) q();",
908                    getLLVMStyleWithColumns(40)));
909   EXPECT_EQ("if (xxxxxxxxxx ==\n"
910             "        yyy && // aaaaaa bbbbbbbb cccc\n"
911             "    zzz)\n"
912             "  q();",
913             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
914                    "    zzz) q();",
915                    getLLVMStyleWithColumns(40)));
916   EXPECT_EQ("if (xxxxxxxxxx &&\n"
917             "        yyy || // aaaaaa bbbbbbbb cccc\n"
918             "    zzz)\n"
919             "  q();",
920             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
921                    "    zzz) q();",
922                    getLLVMStyleWithColumns(40)));
923   EXPECT_EQ("fffffffff(&xxx, // aaaaaaaaaaaa\n"
924             "                // bbbbbbbbbbb\n"
925             "          zzz);",
926             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
927                    " zzz);",
928                    getLLVMStyleWithColumns(40)));
929 }
930 
931 TEST_F(FormatTest, MultiLineCommentsInDefines) {
932   EXPECT_EQ("#define A(x) /* \\\n"
933             "  a comment     \\\n"
934             "  inside */     \\\n"
935             "  f();",
936             format("#define A(x) /* \\\n"
937                    "  a comment     \\\n"
938                    "  inside */     \\\n"
939                    "  f();",
940                    getLLVMStyleWithColumns(17)));
941   EXPECT_EQ("#define A(      \\\n"
942             "    x) /*       \\\n"
943             "  a comment     \\\n"
944             "  inside */     \\\n"
945             "  f();",
946             format("#define A(      \\\n"
947                    "    x) /*       \\\n"
948                    "  a comment     \\\n"
949                    "  inside */     \\\n"
950                    "  f();",
951                    getLLVMStyleWithColumns(17)));
952 }
953 
954 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
955   EXPECT_EQ("namespace {}\n// Test\n#define A",
956             format("namespace {}\n   // Test\n#define A"));
957   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
958             format("namespace {}\n   /* Test */\n#define A"));
959   EXPECT_EQ("namespace {}\n/* Test */ #define A",
960             format("namespace {}\n   /* Test */    #define A"));
961 }
962 
963 TEST_F(FormatTest, SplitsLongLinesInComments) {
964   EXPECT_EQ("/* This is a long\n"
965             " * comment that\n"
966             " * doesn't\n"
967             " * fit on one line.\n"
968             " */",
969             format("/* "
970                    "This is a long                                         "
971                    "comment that "
972                    "doesn't                                    "
973                    "fit on one line.  */",
974                    getLLVMStyleWithColumns(20)));
975   EXPECT_EQ("/* a b c d\n"
976             " * e f  g\n"
977             " * h i j k\n"
978             " */",
979             format("/* a b c d e f  g h i j k */",
980                    getLLVMStyleWithColumns(10)));
981   EXPECT_EQ("/* a b c d\n"
982             " * e f  g\n"
983             " * h i j k\n"
984             " */",
985             format("\\\n/* a b c d e f  g h i j k */",
986                    getLLVMStyleWithColumns(10)));
987   EXPECT_EQ("/*\n"
988             "This is a long\n"
989             "comment that doesn't\n"
990             "fit on one line.\n"
991             "*/",
992             format("/*\n"
993                    "This is a long                                         "
994                    "comment that doesn't                                    "
995                    "fit on one line.                                      \n"
996                    "*/", getLLVMStyleWithColumns(20)));
997   EXPECT_EQ("/*\n"
998             " * This is a long\n"
999             " * comment that\n"
1000             " * doesn't fit on\n"
1001             " * one line.\n"
1002             " */",
1003             format("/*      \n"
1004                    " * This is a long "
1005                    "   comment that     "
1006                    "   doesn't fit on   "
1007                    "   one line.                                            \n"
1008                    " */", getLLVMStyleWithColumns(20)));
1009   EXPECT_EQ("/*\n"
1010             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1011             " * so_it_should_be_broken\n"
1012             " * wherever_a_space_occurs\n"
1013             " */",
1014             format("/*\n"
1015                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1016                    "   so_it_should_be_broken "
1017                    "   wherever_a_space_occurs                             \n"
1018                    " */",
1019                    getLLVMStyleWithColumns(20)));
1020   EXPECT_EQ("/*\n"
1021             " *    This_comment_can_not_be_broken_into_lines\n"
1022             " */",
1023             format("/*\n"
1024                    " *    This_comment_can_not_be_broken_into_lines\n"
1025                    " */",
1026                    getLLVMStyleWithColumns(20)));
1027   EXPECT_EQ("{\n"
1028             "  /*\n"
1029             "  This is another\n"
1030             "  long comment that\n"
1031             "  doesn't fit on one\n"
1032             "  line    1234567890\n"
1033             "  */\n"
1034             "}",
1035             format("{\n"
1036                    "/*\n"
1037                    "This is another     "
1038                    "  long comment that "
1039                    "  doesn't fit on one"
1040                    "  line    1234567890\n"
1041                    "*/\n"
1042                    "}", getLLVMStyleWithColumns(20)));
1043   EXPECT_EQ("{\n"
1044             "  /*\n"
1045             "   * This        i s\n"
1046             "   * another comment\n"
1047             "   * t hat  doesn' t\n"
1048             "   * fit on one l i\n"
1049             "   * n e\n"
1050             "   */\n"
1051             "}",
1052             format("{\n"
1053                    "/*\n"
1054                    " * This        i s"
1055                    "   another comment"
1056                    "   t hat  doesn' t"
1057                    "   fit on one l i"
1058                    "   n e\n"
1059                    " */\n"
1060                    "}", getLLVMStyleWithColumns(20)));
1061   EXPECT_EQ("/*\n"
1062             " * This is a long\n"
1063             " * comment that\n"
1064             " * doesn't fit on\n"
1065             " * one line\n"
1066             " */",
1067             format("   /*\n"
1068                    "    * This is a long comment that doesn't fit on one line\n"
1069                    "    */", getLLVMStyleWithColumns(20)));
1070   EXPECT_EQ("{\n"
1071             "  if (something) /* This is a\n"
1072             "                    long\n"
1073             "                    comment */\n"
1074             "    ;\n"
1075             "}",
1076             format("{\n"
1077                    "  if (something) /* This is a long comment */\n"
1078                    "    ;\n"
1079                    "}",
1080                    getLLVMStyleWithColumns(30)));
1081 
1082   EXPECT_EQ("/* A comment before\n"
1083             " * a macro\n"
1084             " * definition */\n"
1085             "#define a b",
1086             format("/* A comment before a macro definition */\n"
1087                    "#define a b",
1088                    getLLVMStyleWithColumns(20)));
1089 
1090   EXPECT_EQ("/* some comment\n"
1091             "     *   a comment\n"
1092             "* that we break\n"
1093             " * another comment\n"
1094             "* we have to break\n"
1095             "* a left comment\n"
1096             " */",
1097             format("  /* some comment\n"
1098                    "       *   a comment that we break\n"
1099                    "   * another comment we have to break\n"
1100                    "* a left comment\n"
1101                    "   */",
1102                    getLLVMStyleWithColumns(20)));
1103 
1104   EXPECT_EQ("/*\n"
1105             "\n"
1106             "\n"
1107             "    */\n",
1108             format("  /*       \n"
1109                    "      \n"
1110                    "               \n"
1111                    "      */\n"));
1112 }
1113 
1114 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1115   EXPECT_EQ("#define X          \\\n"
1116             "  /*               \\\n"
1117             "   Test            \\\n"
1118             "   Macro comment   \\\n"
1119             "   with a long     \\\n"
1120             "   line            \\\n"
1121             "   */              \\\n"
1122             "  A + B",
1123             format("#define X \\\n"
1124                    "  /*\n"
1125                    "   Test\n"
1126                    "   Macro comment with a long  line\n"
1127                    "   */ \\\n"
1128                    "  A + B",
1129                    getLLVMStyleWithColumns(20)));
1130   EXPECT_EQ("#define X          \\\n"
1131             "  /* Macro comment \\\n"
1132             "     with a long   \\\n"
1133             "     line */       \\\n"
1134             "  A + B",
1135             format("#define X \\\n"
1136                    "  /* Macro comment with a long\n"
1137                    "     line */ \\\n"
1138                    "  A + B",
1139                    getLLVMStyleWithColumns(20)));
1140   EXPECT_EQ("#define X          \\\n"
1141             "  /* Macro comment \\\n"
1142             "   * with a long   \\\n"
1143             "   * line */       \\\n"
1144             "  A + B",
1145             format("#define X \\\n"
1146                    "  /* Macro comment with a long  line */ \\\n"
1147                    "  A + B",
1148                    getLLVMStyleWithColumns(20)));
1149 }
1150 
1151 TEST_F(FormatTest, CommentsInStaticInitializers) {
1152   EXPECT_EQ(
1153       "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1154       "                         aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1155       "                         /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1156       "                         aaaaaaaaaaaaaaaaaaaa, // comment\n"
1157       "                         aaaaaaaaaaaaaaaaaaaa };",
1158       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1159              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1160              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1161              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1162              "                  aaaaaaaaaaaaaaaaaaaa };"));
1163   verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n"
1164                "                         bbbbbbbbbbb, ccccccccccc };");
1165   verifyFormat("static SomeType type = { aaaaaaaaaaa,\n"
1166                "                         // comment for bb....\n"
1167                "                         bbbbbbbbbbb, ccccccccccc };");
1168   verifyGoogleFormat(
1169       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1170       "                        bbbbbbbbbbb, ccccccccccc};");
1171   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1172                      "                        // comment for bb....\n"
1173                      "                        bbbbbbbbbbb, ccccccccccc};");
1174 
1175   verifyFormat("S s = { { a, b, c },   // Group #1\n"
1176                "        { d, e, f },   // Group #2\n"
1177                "        { g, h, i } }; // Group #3");
1178   verifyFormat("S s = { { // Group #1\n"
1179                "          a, b, c },\n"
1180                "        { // Group #2\n"
1181                "          d, e, f },\n"
1182                "        { // Group #3\n"
1183                "          g, h, i } };");
1184 
1185   EXPECT_EQ("S s = {\n"
1186             "  // Some comment\n"
1187             "  a,\n"
1188             "\n"
1189             "  // Comment after empty line\n"
1190             "  b\n"
1191             "}",
1192             format("S s =    {\n"
1193                    "      // Some comment\n"
1194                    "  a,\n"
1195                    "  \n"
1196                    "     // Comment after empty line\n"
1197                    "      b\n"
1198                    "}"));
1199   EXPECT_EQ("S s = {\n"
1200             "  /* Some comment */\n"
1201             "  a,\n"
1202             "\n"
1203             "  /* Comment after empty line */\n"
1204             "  b\n"
1205             "}",
1206             format("S s =    {\n"
1207                    "      /* Some comment */\n"
1208                    "  a,\n"
1209                    "  \n"
1210                    "     /* Comment after empty line */\n"
1211                    "      b\n"
1212                    "}"));
1213   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1214                "  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1215                "  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1216                "  0x00, 0x00, 0x00, 0x00              // comment\n"
1217                "};");
1218 }
1219 
1220 TEST_F(FormatTest, IgnoresIf0Contents) {
1221   EXPECT_EQ("#if 0\n"
1222             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1223             "#endif\n"
1224             "void f() {}",
1225             format("#if 0\n"
1226                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1227                    "#endif\n"
1228                    "void f(  ) {  }"));
1229   EXPECT_EQ("#if false\n"
1230             "void f(  ) {  }\n"
1231             "#endif\n"
1232             "void g() {}\n",
1233             format("#if false\n"
1234                    "void f(  ) {  }\n"
1235                    "#endif\n"
1236                    "void g(  ) {  }\n"));
1237   EXPECT_EQ("enum E {\n"
1238             "  One,\n"
1239             "  Two,\n"
1240             "#if 0\n"
1241             "Three,\n"
1242             "      Four,\n"
1243             "#endif\n"
1244             "  Five\n"
1245             "};",
1246             format("enum E {\n"
1247                    "  One,Two,\n"
1248                    "#if 0\n"
1249                    "Three,\n"
1250                    "      Four,\n"
1251                    "#endif\n"
1252                    "  Five};"));
1253   EXPECT_EQ("enum F {\n"
1254             "  One,\n"
1255             "#if 1\n"
1256             "  Two,\n"
1257             "#if 0\n"
1258             "Three,\n"
1259             "      Four,\n"
1260             "#endif\n"
1261             "  Five\n"
1262             "#endif\n"
1263             "};",
1264             format("enum F {\n"
1265                    "One,\n"
1266                    "#if 1\n"
1267                    "Two,\n"
1268                    "#if 0\n"
1269                    "Three,\n"
1270                    "      Four,\n"
1271                    "#endif\n"
1272                    "Five\n"
1273                    "#endif\n"
1274                    "};"));
1275   EXPECT_EQ("enum G {\n"
1276             "  One,\n"
1277             "#if 0\n"
1278             "Two,\n"
1279             "#else\n"
1280             "  Three,\n"
1281             "#endif\n"
1282             "  Four\n"
1283             "};",
1284             format("enum G {\n"
1285                    "One,\n"
1286                    "#if 0\n"
1287                    "Two,\n"
1288                    "#else\n"
1289                    "Three,\n"
1290                    "#endif\n"
1291                    "Four\n"
1292                    "};"));
1293   EXPECT_EQ("enum H {\n"
1294             "  One,\n"
1295             "#if 0\n"
1296             "#ifdef Q\n"
1297             "Two,\n"
1298             "#else\n"
1299             "Three,\n"
1300             "#endif\n"
1301             "#endif\n"
1302             "  Four\n"
1303             "};",
1304             format("enum H {\n"
1305                    "One,\n"
1306                    "#if 0\n"
1307                    "#ifdef Q\n"
1308                    "Two,\n"
1309                    "#else\n"
1310                    "Three,\n"
1311                    "#endif\n"
1312                    "#endif\n"
1313                    "Four\n"
1314                    "};"));
1315   EXPECT_EQ("enum I {\n"
1316             "  One,\n"
1317             "#if /* test */ 0 || 1\n"
1318             "Two,\n"
1319             "Three,\n"
1320             "#endif\n"
1321             "  Four\n"
1322             "};",
1323             format("enum I {\n"
1324                    "One,\n"
1325                    "#if /* test */ 0 || 1\n"
1326                    "Two,\n"
1327                    "Three,\n"
1328                    "#endif\n"
1329                    "Four\n"
1330                    "};"));
1331   EXPECT_EQ("enum J {\n"
1332             "  One,\n"
1333             "#if 0\n"
1334             "#if 0\n"
1335             "Two,\n"
1336             "#else\n"
1337             "Three,\n"
1338             "#endif\n"
1339             "Four,\n"
1340             "#endif\n"
1341             "  Five\n"
1342             "};",
1343             format("enum J {\n"
1344                    "One,\n"
1345                    "#if 0\n"
1346                    "#if 0\n"
1347                    "Two,\n"
1348                    "#else\n"
1349                    "Three,\n"
1350                    "#endif\n"
1351                    "Four,\n"
1352                    "#endif\n"
1353                    "Five\n"
1354                    "};"));
1355 
1356 }
1357 
1358 //===----------------------------------------------------------------------===//
1359 // Tests for classes, namespaces, etc.
1360 //===----------------------------------------------------------------------===//
1361 
1362 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1363   verifyFormat("class A {};");
1364 }
1365 
1366 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1367   verifyFormat("class A {\n"
1368                "public:\n"
1369                "public: // comment\n"
1370                "protected:\n"
1371                "private:\n"
1372                "  void f() {}\n"
1373                "};");
1374   verifyGoogleFormat("class A {\n"
1375                      " public:\n"
1376                      " protected:\n"
1377                      " private:\n"
1378                      "  void f() {}\n"
1379                      "};");
1380 }
1381 
1382 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1383   EXPECT_EQ("class A {\n"
1384             "public:\n"
1385             "  void f();\n"
1386             "\n"
1387             "private:\n"
1388             "  void g() {}\n"
1389             "  // test\n"
1390             "protected:\n"
1391             "  int h;\n"
1392             "};",
1393             format("class A {\n"
1394                    "public:\n"
1395                    "void f();\n"
1396                    "private:\n"
1397                    "void g() {}\n"
1398                    "// test\n"
1399                    "protected:\n"
1400                    "int h;\n"
1401                    "};"));
1402 }
1403 
1404 TEST_F(FormatTest, FormatsClasses) {
1405   verifyFormat("class A : public B {};");
1406   verifyFormat("class A : public ::B {};");
1407 
1408   verifyFormat(
1409       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1410       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1411   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1412                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1413                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1414   verifyFormat(
1415       "class A : public B, public C, public D, public E, public F {};");
1416   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1417                "                     public C,\n"
1418                "                     public D,\n"
1419                "                     public E,\n"
1420                "                     public F,\n"
1421                "                     public G {};");
1422 
1423   verifyFormat("class\n"
1424                "    ReallyReallyLongClassName {\n};",
1425                getLLVMStyleWithColumns(32));
1426 }
1427 
1428 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1429   verifyFormat("class A {\n} a, b;");
1430   verifyFormat("struct A {\n} a, b;");
1431   verifyFormat("union A {\n} a;");
1432 }
1433 
1434 TEST_F(FormatTest, FormatsEnum) {
1435   verifyFormat("enum {\n"
1436                "  Zero,\n"
1437                "  One = 1,\n"
1438                "  Two = One + 1,\n"
1439                "  Three = (One + Two),\n"
1440                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1441                "  Five = (One, Two, Three, Four, 5)\n"
1442                "};");
1443   verifyFormat("enum Enum {};");
1444   verifyFormat("enum {};");
1445   verifyFormat("enum X E {\n} d;");
1446   verifyFormat("enum __attribute__((...)) E {\n} d;");
1447   verifyFormat("enum __declspec__((...)) E {\n} d;");
1448   verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
1449 }
1450 
1451 TEST_F(FormatTest, FormatsBitfields) {
1452   verifyFormat("struct Bitfields {\n"
1453                "  unsigned sClass : 8;\n"
1454                "  unsigned ValueKind : 2;\n"
1455                "};");
1456 }
1457 
1458 TEST_F(FormatTest, FormatsNamespaces) {
1459   verifyFormat("namespace some_namespace {\n"
1460                "class A {};\n"
1461                "void f() { f(); }\n"
1462                "}");
1463   verifyFormat("namespace {\n"
1464                "class A {};\n"
1465                "void f() { f(); }\n"
1466                "}");
1467   verifyFormat("inline namespace X {\n"
1468                "class A {};\n"
1469                "void f() { f(); }\n"
1470                "}");
1471   verifyFormat("using namespace some_namespace;\n"
1472                "class A {};\n"
1473                "void f() { f(); }");
1474 
1475   // This code is more common than we thought; if we
1476   // layout this correctly the semicolon will go into
1477   // its own line, which is undesireable.
1478   verifyFormat("namespace {};");
1479   verifyFormat("namespace {\n"
1480                "class A {};\n"
1481                "};");
1482 }
1483 
1484 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1485 
1486 TEST_F(FormatTest, FormatsInlineASM) {
1487   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1488   verifyFormat(
1489       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1490       "    \"cpuid\\n\\t\"\n"
1491       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1492       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1493       "    : \"a\"(value));");
1494 }
1495 
1496 TEST_F(FormatTest, FormatTryCatch) {
1497   // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
1498   // also not create single-line-blocks.
1499   verifyFormat("try {\n"
1500                "  throw a * b;\n"
1501                "}\n"
1502                "catch (int a) {\n"
1503                "  // Do nothing.\n"
1504                "}\n"
1505                "catch (...) {\n"
1506                "  exit(42);\n"
1507                "}");
1508 
1509   // Function-level try statements.
1510   verifyFormat("int f() try { return 4; }\n"
1511                "catch (...) {\n"
1512                "  return 5;\n"
1513                "}");
1514   verifyFormat("class A {\n"
1515                "  int a;\n"
1516                "  A() try : a(0) {}\n"
1517                "  catch (...) {\n"
1518                "    throw;\n"
1519                "  }\n"
1520                "};\n");
1521 }
1522 
1523 TEST_F(FormatTest, FormatObjCTryCatch) {
1524   verifyFormat("@try {\n"
1525                "  f();\n"
1526                "}\n"
1527                "@catch (NSException e) {\n"
1528                "  @throw;\n"
1529                "}\n"
1530                "@finally {\n"
1531                "  exit(42);\n"
1532                "}");
1533 }
1534 
1535 TEST_F(FormatTest, StaticInitializers) {
1536   verifyFormat("static SomeClass SC = { 1, 'a' };");
1537 
1538   verifyFormat(
1539       "static SomeClass WithALoooooooooooooooooooongName = {\n"
1540       "  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1541       "};");
1542 
1543   verifyFormat(
1544       "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
1545       "                     looooooooooooooooooooooooooooooooooongname,\n"
1546       "                     looooooooooooooooooooooooooooooong };");
1547   // Allow bin-packing in static initializers as this would often lead to
1548   // terrible results, e.g.:
1549   verifyGoogleFormat(
1550       "static SomeClass = {a, b, c, d, e, f, g, h, i, j,\n"
1551       "                    looooooooooooooooooooooooooooooooooongname,\n"
1552       "                    looooooooooooooooooooooooooooooong};");
1553   // Here, everything other than the "}" would fit on a line.
1554   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1555                "  100000000000000000000000\n"
1556                "};");
1557   EXPECT_EQ("S s = { a, b };", format("S s = {\n"
1558                                       "  a,\n"
1559                                       "\n"
1560                                       "  b\n"
1561                                       "};"));
1562 
1563   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1564   // line. However, the formatting looks a bit off and this probably doesn't
1565   // happen often in practice.
1566   verifyFormat("static int Variable[1] = {\n"
1567                "  { 1000000000000000000000000000000000000 }\n"
1568                "};",
1569                getLLVMStyleWithColumns(40));
1570 }
1571 
1572 TEST_F(FormatTest, DesignatedInitializers) {
1573   verifyFormat("const struct A a = { .a = 1, .b = 2 };");
1574   verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n"
1575                "                     .bbbbbbbbbb = 2,\n"
1576                "                     .cccccccccc = 3,\n"
1577                "                     .dddddddddd = 4,\n"
1578                "                     .eeeeeeeeee = 5 };");
1579   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1580                "  .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1581                "  .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1582                "  .ccccccccccccccccccccccccccc = 3,\n"
1583                "  .ddddddddddddddddddddddddddd = 4,\n"
1584                "  .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
1585                "};");
1586 
1587   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1588 }
1589 
1590 TEST_F(FormatTest, NestedStaticInitializers) {
1591   verifyFormat("static A x = { { {} } };\n");
1592   verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
1593                "                 { init1, init2, init3, init4 } } };");
1594 
1595   verifyFormat("somes Status::global_reps[3] = {\n"
1596                "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
1597                "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
1598                "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
1599                "};");
1600   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1601                      "  {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1602                      "  {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1603                      "  {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}\n"
1604                      "};");
1605   verifyFormat(
1606       "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
1607       "                   { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
1608       " } };");
1609 
1610   verifyFormat(
1611       "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
1612       "                            { 111111111111111111111111111111,\n"
1613       "                              222222222222222222222222222222,\n"
1614       "                              333333333333333333333333333333 },\n"
1615       "                            { 1, 2, 3 }, { 1, 2, 3 } } };");
1616   verifyFormat(
1617       "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
1618       "                          { { 111111111111111111111111111111,\n"
1619       "                              222222222222222222222222222222,\n"
1620       "                              333333333333333333333333333333 } },\n"
1621       "                          { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
1622   verifyGoogleFormat(
1623       "SomeArrayOfSomeType a = {{{1, 2, 3}}, {{1, 2, 3}},\n"
1624       "                         {{111111111111111111111111111111,\n"
1625       "                           222222222222222222222222222222,\n"
1626       "                           333333333333333333333333333333}},\n"
1627       "                         {{1, 2, 3}}, {{1, 2, 3}}};");
1628 
1629   // FIXME: We might at some point want to handle this similar to parameter
1630   // lists, where we have an option to put each on a single line.
1631   verifyFormat(
1632       "struct {\n"
1633       "  unsigned bit;\n"
1634       "  const char *const name;\n"
1635       "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
1636       "                  { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
1637 }
1638 
1639 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1640   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1641                "                      \\\n"
1642                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1643 }
1644 
1645 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1646   verifyFormat(
1647       "virtual void write(ELFWriter *writerrr,\n"
1648       "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1649 }
1650 
1651 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1652   EXPECT_EQ("#123 \"A string literal\"",
1653             format("   #     123    \"A string literal\""));
1654   EXPECT_EQ("#;", format("#;"));
1655   verifyFormat("#\n;\n;\n;");
1656 }
1657 
1658 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1659   EXPECT_EQ("#line 42 \"test\"\n",
1660             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1661   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1662                                     getLLVMStyleWithColumns(12)));
1663 }
1664 
1665 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1666   EXPECT_EQ("#line 42 \"test\"",
1667             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1668   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1669 }
1670 
1671 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1672   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1673   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1674   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1675   // FIXME: We never break before the macro name.
1676   verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
1677 
1678   verifyFormat("#define A A\n#define A A");
1679   verifyFormat("#define A(X) A\n#define A A");
1680 
1681   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1682   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1683 }
1684 
1685 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1686   EXPECT_EQ("// somecomment\n"
1687             "#include \"a.h\"\n"
1688             "#define A(  \\\n"
1689             "    A, B)\n"
1690             "#include \"b.h\"\n"
1691             "// somecomment\n",
1692             format("  // somecomment\n"
1693                    "  #include \"a.h\"\n"
1694                    "#define A(A,\\\n"
1695                    "    B)\n"
1696                    "    #include \"b.h\"\n"
1697                    " // somecomment\n",
1698                    getLLVMStyleWithColumns(13)));
1699 }
1700 
1701 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1702 
1703 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1704   EXPECT_EQ("#define A    \\\n"
1705             "  c;         \\\n"
1706             "  e;\n"
1707             "f;",
1708             format("#define A c; e;\n"
1709                    "f;",
1710                    getLLVMStyleWithColumns(14)));
1711 }
1712 
1713 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1714 
1715 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
1716   EXPECT_EQ("int  i;\n"
1717             "#define A \\\n"
1718             "  int i;  \\\n"
1719             "  int j\n"
1720             "int  k;",
1721             format("int  i;\n"
1722                    "#define A  \\\n"
1723                    " int   i    ;  \\\n"
1724                    " int   j\n"
1725                    "int  k;",
1726                    8, 0, getGoogleStyle())); // 8: position of "#define".
1727   EXPECT_EQ("int  i;\n"
1728             "#define A \\\n"
1729             "  int i;  \\\n"
1730             "  int j\n"
1731             "int  k;",
1732             format("int  i;\n"
1733                    "#define A  \\\n"
1734                    " int   i    ;  \\\n"
1735                    " int   j\n"
1736                    "int  k;",
1737                    45, 0, getGoogleStyle())); // 45: position of "j".
1738 }
1739 
1740 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1741   EXPECT_EQ("int x,\n"
1742             "#define A\n"
1743             "    y;",
1744             format("int x,\n#define A\ny;"));
1745 }
1746 
1747 TEST_F(FormatTest, HashInMacroDefinition) {
1748   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1749   verifyFormat("#define A \\\n"
1750                "  {       \\\n"
1751                "    f(#c);\\\n"
1752                "  }",
1753                getLLVMStyleWithColumns(11));
1754 
1755   verifyFormat("#define A(X)         \\\n"
1756                "  void function##X()",
1757                getLLVMStyleWithColumns(22));
1758 
1759   verifyFormat("#define A(a, b, c)   \\\n"
1760                "  void a##b##c()",
1761                getLLVMStyleWithColumns(22));
1762 
1763   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1764 }
1765 
1766 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1767   verifyFormat("#define A (1)");
1768 }
1769 
1770 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1771   EXPECT_EQ("#define A b;", format("#define A \\\n"
1772                                    "          \\\n"
1773                                    "  b;",
1774                                    getLLVMStyleWithColumns(25)));
1775   EXPECT_EQ("#define A \\\n"
1776             "          \\\n"
1777             "  a;      \\\n"
1778             "  b;",
1779             format("#define A \\\n"
1780                    "          \\\n"
1781                    "  a;      \\\n"
1782                    "  b;",
1783                    getLLVMStyleWithColumns(11)));
1784   EXPECT_EQ("#define A \\\n"
1785             "  a;      \\\n"
1786             "          \\\n"
1787             "  b;",
1788             format("#define A \\\n"
1789                    "  a;      \\\n"
1790                    "          \\\n"
1791                    "  b;",
1792                    getLLVMStyleWithColumns(11)));
1793 }
1794 
1795 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1796   verifyFormat("#define A :");
1797   verifyFormat("#define SOMECASES  \\\n"
1798                "  case 1:          \\\n"
1799                "  case 2\n",
1800                getLLVMStyleWithColumns(20));
1801   verifyFormat("#define A template <typename T>");
1802   verifyFormat("#define STR(x) #x\n"
1803                "f(STR(this_is_a_string_literal{));");
1804 }
1805 
1806 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
1807   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
1808   EXPECT_EQ("class A : public QObject {\n"
1809             "  Q_OBJECT\n"
1810             "\n"
1811             "  A() {}\n"
1812             "};",
1813             format("class A  :  public QObject {\n"
1814                    "     Q_OBJECT\n"
1815                    "\n"
1816                    "  A() {\n}\n"
1817                    "}  ;"));
1818   EXPECT_EQ("SOME_MACRO\n"
1819             "namespace {\n"
1820             "void f();\n"
1821             "}",
1822             format("SOME_MACRO\n"
1823                    "  namespace    {\n"
1824                    "void   f(  );\n"
1825                    "}"));
1826   // Only if the identifier contains at least 5 characters.
1827   EXPECT_EQ("HTTP f();",
1828             format("HTTP\nf();"));
1829   EXPECT_EQ("MACRO\nf();",
1830             format("MACRO\nf();"));
1831   // Only if everything is upper case.
1832   EXPECT_EQ("class A : public QObject {\n"
1833             "  Q_Object A() {}\n"
1834             "};",
1835             format("class A  :  public QObject {\n"
1836                    "     Q_Object\n"
1837                    "\n"
1838                    "  A() {\n}\n"
1839                    "}  ;"));
1840 }
1841 
1842 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
1843   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1844             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1845             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1846             "class X {};\n"
1847             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1848             "int *createScopDetectionPass() { return 0; }",
1849             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1850                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1851                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1852                    "  class X {};\n"
1853                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1854                    "  int *createScopDetectionPass() { return 0; }"));
1855   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
1856   // braces, so that inner block is indented one level more.
1857   EXPECT_EQ("int q() {\n"
1858             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1859             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1860             "  IPC_END_MESSAGE_MAP()\n"
1861             "}",
1862             format("int q() {\n"
1863                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1864                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1865                    "  IPC_END_MESSAGE_MAP()\n"
1866                    "}"));
1867   EXPECT_EQ("int q() {\n"
1868             "  f(x);\n"
1869             "  f(x) {}\n"
1870             "  f(x)->g();\n"
1871             "  f(x)->*g();\n"
1872             "  f(x).g();\n"
1873             "  f(x) = x;\n"
1874             "  f(x) += x;\n"
1875             "  f(x) -= x;\n"
1876             "  f(x) *= x;\n"
1877             "  f(x) /= x;\n"
1878             "  f(x) %= x;\n"
1879             "  f(x) &= x;\n"
1880             "  f(x) |= x;\n"
1881             "  f(x) ^= x;\n"
1882             "  f(x) >>= x;\n"
1883             "  f(x) <<= x;\n"
1884             "  f(x)[y].z();\n"
1885             "  LOG(INFO) << x;\n"
1886             "  ifstream(x) >> x;\n"
1887             "}\n",
1888             format("int q() {\n"
1889                    "  f(x)\n;\n"
1890                    "  f(x)\n {}\n"
1891                    "  f(x)\n->g();\n"
1892                    "  f(x)\n->*g();\n"
1893                    "  f(x)\n.g();\n"
1894                    "  f(x)\n = x;\n"
1895                    "  f(x)\n += x;\n"
1896                    "  f(x)\n -= x;\n"
1897                    "  f(x)\n *= x;\n"
1898                    "  f(x)\n /= x;\n"
1899                    "  f(x)\n %= x;\n"
1900                    "  f(x)\n &= x;\n"
1901                    "  f(x)\n |= x;\n"
1902                    "  f(x)\n ^= x;\n"
1903                    "  f(x)\n >>= x;\n"
1904                    "  f(x)\n <<= x;\n"
1905                    "  f(x)\n[y].z();\n"
1906                    "  LOG(INFO)\n << x;\n"
1907                    "  ifstream(x)\n >> x;\n"
1908                    "}\n"));
1909   EXPECT_EQ("int q() {\n"
1910             "  f(x)\n"
1911             "  if (1) {\n"
1912             "  }\n"
1913             "  f(x)\n"
1914             "  while (1) {\n"
1915             "  }\n"
1916             "  f(x)\n"
1917             "  g(x);\n"
1918             "  f(x)\n"
1919             "  try {\n"
1920             "    q();\n"
1921             "  }\n"
1922             "  catch (...) {\n"
1923             "  }\n"
1924             "}\n",
1925             format("int q() {\n"
1926                    "f(x)\n"
1927                    "if (1) {}\n"
1928                    "f(x)\n"
1929                    "while (1) {}\n"
1930                    "f(x)\n"
1931                    "g(x);\n"
1932                    "f(x)\n"
1933                    "try { q(); } catch (...) {}\n"
1934                    "}\n"));
1935   EXPECT_EQ("class A {\n"
1936             "  A() : t(0) {}\n"
1937             "  A(X x)\n" // FIXME: function-level try blocks are broken.
1938             "  try : t(0) {\n"
1939             "  }\n"
1940             "  catch (...) {\n"
1941             "  }\n"
1942             "};",
1943             format("class A {\n"
1944                    "  A()\n : t(0) {}\n"
1945                    "  A(X x)\n"
1946                    "  try : t(0) {} catch (...) {}\n"
1947                    "};"));
1948 }
1949 
1950 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
1951   verifyFormat("#define A \\\n"
1952                "  f({     \\\n"
1953                "    g();  \\\n"
1954                "  });", getLLVMStyleWithColumns(11));
1955 }
1956 
1957 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
1958   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
1959 }
1960 
1961 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
1962   verifyFormat("{\n  { a #c; }\n}");
1963 }
1964 
1965 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
1966   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
1967             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
1968   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
1969             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
1970 }
1971 
1972 TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
1973   EXPECT_EQ(
1974       "#define A \\\n  int i;  \\\n  int j;",
1975       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
1976   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
1977 }
1978 
1979 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
1980   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
1981 }
1982 
1983 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
1984   verifyFormat("#define A \\\n"
1985                "  int v(  \\\n"
1986                "      a); \\\n"
1987                "  int i;",
1988                getLLVMStyleWithColumns(11));
1989 }
1990 
1991 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
1992   EXPECT_EQ(
1993       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1994       "                      \\\n"
1995       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
1996       "\n"
1997       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
1998       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
1999       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2000              "\\\n"
2001              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2002              "  \n"
2003              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2004              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2005 }
2006 
2007 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2008   EXPECT_EQ("int\n"
2009             "#define A\n"
2010             "    a;",
2011             format("int\n#define A\na;", getGoogleStyle()));
2012   verifyFormat("functionCallTo(\n"
2013                "    someOtherFunction(\n"
2014                "        withSomeParameters, whichInSequence,\n"
2015                "        areLongerThanALine(andAnotherCall,\n"
2016                "#define A B\n"
2017                "                           withMoreParamters,\n"
2018                "                           whichStronglyInfluenceTheLayout),\n"
2019                "        andMoreParameters),\n"
2020                "    trailing);",
2021                getLLVMStyleWithColumns(69));
2022 }
2023 
2024 TEST_F(FormatTest, LayoutBlockInsideParens) {
2025   EXPECT_EQ("functionCall({\n"
2026             "  int i;\n"
2027             "});",
2028             format(" functionCall ( {int i;} );"));
2029 
2030   // FIXME: This is bad, find a better and more generic solution.
2031   EXPECT_EQ("functionCall({\n"
2032             "  int i;\n"
2033             "},\n"
2034             "             aaaa, bbbb, cccc);",
2035             format(" functionCall ( {int i;},  aaaa,   bbbb, cccc);"));
2036   verifyFormat(
2037       "Aaa({\n"
2038       "  int i;\n"
2039       "},\n"
2040       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2041       "                                     ccccccccccccccccc));");
2042 }
2043 
2044 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2045   EXPECT_EQ("SOME_MACRO { int i; }\n"
2046             "int i;",
2047             format("  SOME_MACRO  {int i;}  int i;"));
2048 }
2049 
2050 TEST_F(FormatTest, LayoutNestedBlocks) {
2051   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2052                "  struct s {\n"
2053                "    int i;\n"
2054                "  };\n"
2055                "  s kBitsToOs[] = { { 10 } };\n"
2056                "  for (int i = 0; i < 10; ++i)\n"
2057                "    return;\n"
2058                "}");
2059 }
2060 
2061 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2062   EXPECT_EQ("{}", format("{}"));
2063   verifyFormat("enum E {};");
2064   verifyFormat("enum E {}");
2065 }
2066 
2067 //===----------------------------------------------------------------------===//
2068 // Line break tests.
2069 //===----------------------------------------------------------------------===//
2070 
2071 TEST_F(FormatTest, FormatsAwesomeMethodCall) {
2072   verifyFormat(
2073       "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2074       "                       parameter, parameter, parameter)),\n"
2075       "                   SecondLongCall(parameter));");
2076 }
2077 
2078 TEST_F(FormatTest, PreventConfusingIndents) {
2079   verifyFormat(
2080       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2081       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2082       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2083       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2084   verifyFormat(
2085       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
2086       "    aaaaaaaaaaaaaaaaaaaaaaaa[\n"
2087       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n"
2088       "    aaaaaaaaaaaaaaaaaaaaaaaa];");
2089   verifyFormat(
2090       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2091       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2092       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2093       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2094   verifyFormat("int a = bbbb && ccc && fffff(\n"
2095                "#define A Just forcing a new line\n"
2096                "                           ddd);");
2097 }
2098 
2099 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2100   verifyFormat(
2101       "bool aaaaaaa =\n"
2102       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2103       "    bbbbbbbb();");
2104   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2105                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2106                "    ccccccccc == ddddddddddd;");
2107 
2108   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2109                "                 aaaaaa) &&\n"
2110                "         bbbbbb && cccccc;");
2111   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2112                "                 aaaaaa) >>\n"
2113                "         bbbbbb;");
2114   verifyFormat("Whitespaces.addUntouchableComment(\n"
2115                "    SourceMgr.getSpellingColumnNumber(\n"
2116                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2117                "    1);");
2118 
2119   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2120                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2121                "    cccccc) {\n}");
2122 
2123   FormatStyle OnePerLine = getLLVMStyle();
2124   OnePerLine.BinPackParameters = false;
2125   verifyFormat(
2126       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2127       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2128       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2129       OnePerLine);
2130 }
2131 
2132 TEST_F(FormatTest, ExpressionIndentation) {
2133   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2134                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2135                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2136                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2137                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2138                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2139                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2140                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2141                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2142   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2143                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2144                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2145                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2146   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2147                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2148                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2149                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2150   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2151                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2152                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2153                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2154   verifyFormat("if () {\n"
2155                "} else if (aaaaa && bbbbb > // break\n"
2156                "                        ccccc) {\n"
2157                "}");
2158 
2159   // Presence of a trailing comment used to change indentation of b.
2160   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2161                "       b;\n"
2162                "return aaaaaaaaaaaaaaaaaaa +\n"
2163                "       b; //",
2164                getLLVMStyleWithColumns(30));
2165 }
2166 
2167 TEST_F(FormatTest, ConstructorInitializers) {
2168   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2169   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2170                getLLVMStyleWithColumns(45));
2171   verifyFormat("Constructor()\n"
2172                "    : Inttializer(FitsOnTheLine) {}",
2173                getLLVMStyleWithColumns(44));
2174   verifyFormat("Constructor()\n"
2175                "    : Inttializer(FitsOnTheLine) {}",
2176                getLLVMStyleWithColumns(43));
2177 
2178   verifyFormat(
2179       "SomeClass::Constructor()\n"
2180       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2181 
2182   verifyFormat(
2183       "SomeClass::Constructor()\n"
2184       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2185       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2186   verifyFormat(
2187       "SomeClass::Constructor()\n"
2188       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2189       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2190 
2191   verifyFormat("Constructor()\n"
2192                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2193                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2194                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2195                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2196 
2197   verifyFormat("Constructor()\n"
2198                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2199                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2200 
2201   verifyFormat("Constructor(int Parameter = 0)\n"
2202                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2203                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2204 
2205   // Here a line could be saved by splitting the second initializer onto two
2206   // lines, but that is not desireable.
2207   verifyFormat("Constructor()\n"
2208                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2209                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
2210                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2211 
2212   FormatStyle OnePerLine = getLLVMStyle();
2213   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2214   verifyFormat("SomeClass::Constructor()\n"
2215                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2216                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2217                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2218                OnePerLine);
2219   verifyFormat("SomeClass::Constructor()\n"
2220                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2221                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2222                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2223                OnePerLine);
2224   verifyFormat("MyClass::MyClass(int var)\n"
2225                "    : some_var_(var),            // 4 space indent\n"
2226                "      some_other_var_(var + 1) { // lined up\n"
2227                "}",
2228                OnePerLine);
2229   verifyFormat("Constructor()\n"
2230                "    : aaaaa(aaaaaa),\n"
2231                "      aaaaa(aaaaaa),\n"
2232                "      aaaaa(aaaaaa),\n"
2233                "      aaaaa(aaaaaa),\n"
2234                "      aaaaa(aaaaaa) {}",
2235                OnePerLine);
2236   verifyFormat("Constructor()\n"
2237                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2238                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
2239                OnePerLine);
2240 }
2241 
2242 TEST_F(FormatTest, MemoizationTests) {
2243   // This breaks if the memoization lookup does not take \c Indent and
2244   // \c LastSpace into account.
2245   verifyFormat(
2246       "extern CFRunLoopTimerRef\n"
2247       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
2248       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
2249       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
2250       "                     CFRunLoopTimerContext *context) {}");
2251 
2252   // Deep nesting somewhat works around our memoization.
2253   verifyFormat(
2254       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2255       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2256       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2257       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2258       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
2259       getLLVMStyleWithColumns(65));
2260   verifyFormat(
2261       "aaaaa(\n"
2262       "    aaaaa,\n"
2263       "    aaaaa(\n"
2264       "        aaaaa,\n"
2265       "        aaaaa(\n"
2266       "            aaaaa,\n"
2267       "            aaaaa(\n"
2268       "                aaaaa,\n"
2269       "                aaaaa(\n"
2270       "                    aaaaa,\n"
2271       "                    aaaaa(\n"
2272       "                        aaaaa,\n"
2273       "                        aaaaa(\n"
2274       "                            aaaaa,\n"
2275       "                            aaaaa(\n"
2276       "                                aaaaa,\n"
2277       "                                aaaaa(\n"
2278       "                                    aaaaa,\n"
2279       "                                    aaaaa(\n"
2280       "                                        aaaaa,\n"
2281       "                                        aaaaa(\n"
2282       "                                            aaaaa,\n"
2283       "                                            aaaaa(\n"
2284       "                                                aaaaa,\n"
2285       "                                                aaaaa))))))))))));",
2286       getLLVMStyleWithColumns(65));
2287   verifyFormat(
2288       "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
2289       "                                  a),\n"
2290       "                                a),\n"
2291       "                              a),\n"
2292       "                            a),\n"
2293       "                          a),\n"
2294       "                        a),\n"
2295       "                      a),\n"
2296       "                    a),\n"
2297       "                  a),\n"
2298       "                a),\n"
2299       "              a),\n"
2300       "            a),\n"
2301       "          a),\n"
2302       "        a),\n"
2303       "      a),\n"
2304       "    a),\n"
2305       "  a)",
2306       getLLVMStyleWithColumns(65));
2307 
2308   // This test takes VERY long when memoization is broken.
2309   FormatStyle OnePerLine = getLLVMStyle();
2310   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2311   OnePerLine.BinPackParameters = false;
2312   std::string input = "Constructor()\n"
2313                       "    : aaaa(a,\n";
2314   for (unsigned i = 0, e = 80; i != e; ++i) {
2315     input += "           a,\n";
2316   }
2317   input += "           a) {}";
2318   verifyFormat(input, OnePerLine);
2319 }
2320 
2321 TEST_F(FormatTest, BreaksAsHighAsPossible) {
2322   verifyFormat(
2323       "void f() {\n"
2324       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
2325       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
2326       "    f();\n"
2327       "}");
2328   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
2329                "    Intervals[i - 1].getRange().getLast()) {\n}");
2330 }
2331 
2332 TEST_F(FormatTest, BreaksFunctionDeclarations) {
2333   // Principially, we break function declarations in a certain order:
2334   // 1) break amongst arguments.
2335   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
2336                "                              Cccccccccccccc cccccccccccccc);");
2337 
2338   // 2) break after return type.
2339   verifyFormat(
2340       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2341       "    bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
2342       getGoogleStyle());
2343 
2344   // 3) break after (.
2345   verifyFormat(
2346       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
2347       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
2348       getGoogleStyle());
2349 
2350   // 4) break before after nested name specifiers.
2351   verifyFormat(
2352       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2353       "    SomeClasssssssssssssssssssssssssssssssssssssss::\n"
2354       "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
2355       getGoogleStyle());
2356 
2357   // However, there are exceptions, if a sufficient amount of lines can be
2358   // saved.
2359   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
2360   // more adjusting.
2361   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
2362                "                                  Cccccccccccccc cccccccccc,\n"
2363                "                                  Cccccccccccccc cccccccccc,\n"
2364                "                                  Cccccccccccccc cccccccccc,\n"
2365                "                                  Cccccccccccccc cccccccccc);");
2366   verifyFormat(
2367       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2368       "    bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2369       "                Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2370       "                Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
2371       getGoogleStyle());
2372   verifyFormat(
2373       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
2374       "                                          Cccccccccccccc cccccccccc,\n"
2375       "                                          Cccccccccccccc cccccccccc,\n"
2376       "                                          Cccccccccccccc cccccccccc,\n"
2377       "                                          Cccccccccccccc cccccccccc,\n"
2378       "                                          Cccccccccccccc cccccccccc,\n"
2379       "                                          Cccccccccccccc cccccccccc);");
2380   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
2381                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2382                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2383                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2384                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
2385 
2386   // Break after multi-line parameters.
2387   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2388                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2389                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2390                "    bbbb bbbb);");
2391 }
2392 
2393 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
2394   verifyFormat("void someLongFunction(\n"
2395                "    int someLongParameter) const {}",
2396                getLLVMStyleWithColumns(46));
2397   FormatStyle Style = getGoogleStyle();
2398   Style.ColumnLimit = 47;
2399   verifyFormat("void\n"
2400                "someLongFunction(int someLongParameter) const {\n}",
2401                getLLVMStyleWithColumns(47));
2402   verifyFormat("void someLongFunction(\n"
2403                "    int someLongParameter) const {}",
2404                Style);
2405   verifyFormat("LoooooongReturnType\n"
2406                "someLoooooooongFunction() const {}",
2407                getLLVMStyleWithColumns(47));
2408   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
2409                "    const {}",
2410                Style);
2411 
2412   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2413                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
2414   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
2415                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
2416   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
2417                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
2418 
2419   verifyFormat(
2420       "void aaaaaaaaaaaaaaaaaa()\n"
2421       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
2422       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
2423   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2424                "    __attribute__((unused));");
2425   verifyFormat(
2426       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2427       "    GUARDED_BY(aaaaaaaaaaaa);",
2428       getGoogleStyle());
2429 }
2430 
2431 TEST_F(FormatTest, BreaksDesireably) {
2432   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
2433                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
2434                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
2435   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2436                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2437                "}");
2438 
2439   verifyFormat(
2440       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2441       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2442 
2443   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2444                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2445                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
2446 
2447   verifyFormat(
2448       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2449       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
2450       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2451       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
2452 
2453   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2454                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2455 
2456   verifyFormat(
2457       "void f() {\n"
2458       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
2459       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2460       "}");
2461   verifyFormat(
2462       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2463       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
2464   verifyFormat(
2465       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2466       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
2467   verifyFormat(
2468       "aaaaaaaaaaaaaaaaa(\n"
2469       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2470       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2471 
2472   // This test case breaks on an incorrect memoization, i.e. an optimization not
2473   // taking into account the StopAt value.
2474   verifyFormat(
2475       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
2476       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
2477       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
2478       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2479 
2480   verifyFormat("{\n  {\n    {\n"
2481                "      Annotation.SpaceRequiredBefore =\n"
2482                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
2483                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
2484                "    }\n  }\n}");
2485 
2486   // Break on an outer level if there was a break on an inner level.
2487   EXPECT_EQ("f(g(h(a, // comment\n"
2488             "      b, c),\n"
2489             "    d, e),\n"
2490             "  x, y);",
2491             format("f(g(h(a, // comment\n"
2492                    "    b, c), d, e), x, y);"));
2493 }
2494 
2495 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
2496   FormatStyle NoBinPacking = getGoogleStyle();
2497   NoBinPacking.BinPackParameters = false;
2498   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
2499                "  aaaaaaaaaaaaaaaaaaaa,\n"
2500                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
2501                NoBinPacking);
2502   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
2503                "        aaaaaaaaaaaaa,\n"
2504                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
2505                NoBinPacking);
2506   verifyFormat(
2507       "aaaaaaaa(aaaaaaaaaaaaa,\n"
2508       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2509       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
2510       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2511       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
2512       NoBinPacking);
2513   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
2514                "    .aaaaaaaaaaaaaaaaaa();",
2515                NoBinPacking);
2516   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2517                "    aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
2518                NoBinPacking);
2519 
2520   verifyFormat(
2521       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2522       "             aaaaaaaaaaaa,\n"
2523       "             aaaaaaaaaaaa);",
2524       NoBinPacking);
2525   verifyFormat(
2526       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
2527       "                               ddddddddddddddddddddddddddddd),\n"
2528       "             test);",
2529       NoBinPacking);
2530 
2531   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
2532                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
2533                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
2534                NoBinPacking);
2535   verifyFormat("a(\"a\"\n"
2536                "  \"a\",\n"
2537                "  a);");
2538 
2539   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
2540   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
2541                "                aaaaaaaaa,\n"
2542                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2543                NoBinPacking);
2544   verifyFormat(
2545       "void f() {\n"
2546       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
2547       "      .aaaaaaa();\n"
2548       "}",
2549       NoBinPacking);
2550   verifyFormat(
2551       "template <class SomeType, class SomeOtherType>\n"
2552       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
2553       NoBinPacking);
2554 }
2555 
2556 TEST_F(FormatTest, FormatsBuilderPattern) {
2557   verifyFormat(
2558       "return llvm::StringSwitch<Reference::Kind>(name)\n"
2559       "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
2560       "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
2561       "    .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
2562       "    .Default(ORDER_TEXT);\n");
2563 
2564   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
2565                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
2566   verifyFormat(
2567       "aaaaaaa->aaaaaaa\n"
2568       "    ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2569       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
2570   verifyFormat(
2571       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
2572       "    aaaaaaaaaaaaaa);");
2573   verifyFormat(
2574       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
2575       "    ->aaaaaaaaaaaaaaaa(\n"
2576       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2577       "    ->aaaaaaaaaaaaaaaaa();");
2578 }
2579 
2580 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
2581   verifyFormat(
2582       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2583       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
2584   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
2585                "    ccccccccccccccccccccccccc) {\n}");
2586   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
2587                "    ccccccccccccccccccccccccc) {\n}");
2588   verifyFormat(
2589       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
2590       "    ccccccccccccccccccccccccc) {\n}");
2591   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
2592                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
2593                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
2594                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
2595   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
2596                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
2597                "    aaaaaaaaaaaaaaa != aa) {\n}");
2598 }
2599 
2600 TEST_F(FormatTest, BreaksAfterAssignments) {
2601   verifyFormat(
2602       "unsigned Cost =\n"
2603       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
2604       "                        SI->getPointerAddressSpaceee());\n");
2605   verifyFormat(
2606       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
2607       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
2608 
2609   verifyFormat(
2610       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n"
2611       "    .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
2612   verifyFormat("unsigned OriginalStartColumn =\n"
2613                "    SourceMgr.getSpellingColumnNumber(\n"
2614                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
2615                "    1;");
2616 }
2617 
2618 TEST_F(FormatTest, AlignsAfterAssignments) {
2619   verifyFormat(
2620       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2621       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
2622   verifyFormat(
2623       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2624       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
2625   verifyFormat(
2626       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2627       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
2628   verifyFormat(
2629       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2630       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
2631   verifyFormat("double LooooooooooooooooooooooooongResult =\n"
2632                "    aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
2633                "    aaaaaaaaaaaaaaaaaaaaaaaa;");
2634 }
2635 
2636 TEST_F(FormatTest, AlignsAfterReturn) {
2637   verifyFormat(
2638       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2639       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
2640   verifyFormat(
2641       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2642       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
2643   verifyFormat(
2644       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
2645       "       aaaaaaaaaaaaaaaaaaaaaa();");
2646   verifyFormat(
2647       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
2648       "        aaaaaaaaaaaaaaaaaaaaaa());");
2649   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2650                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2651   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2652                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
2653                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2654 }
2655 
2656 TEST_F(FormatTest, BreaksConditionalExpressions) {
2657   verifyFormat(
2658       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
2659       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2660       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2661   verifyFormat(
2662       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2663       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2664   verifyFormat(
2665       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
2666       "                                                    : aaaaaaaaaaaaa);");
2667   verifyFormat(
2668       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2669       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2670       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2671       "                   aaaaaaaaaaaaa);");
2672   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2673                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2674                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2675                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2676                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2677   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2678                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2679                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2680                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2681                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2682                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2683                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2684 
2685   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2686                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2687                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2688   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
2689                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2690                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2691                "        : aaaaaaaaaaaaaaaa;");
2692   verifyFormat(
2693       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2694       "    ? aaaaaaaaaaaaaaa\n"
2695       "    : aaaaaaaaaaaaaaa;");
2696   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
2697                "          aaaaaaaaa\n"
2698                "      ? b\n"
2699                "      : c);");
2700   verifyFormat(
2701       "unsigned Indent =\n"
2702       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
2703       "                              ? IndentForLevel[TheLine.Level]\n"
2704       "                              : TheLine * 2,\n"
2705       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
2706       getLLVMStyleWithColumns(70));
2707   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
2708                "                  ? aaaaaaaaaaaaaaa\n"
2709                "                  : bbbbbbbbbbbbbbb //\n"
2710                "                        ? ccccccccccccccc\n"
2711                "                        : ddddddddddddddd;");
2712   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
2713                "                  ? aaaaaaaaaaaaaaa\n"
2714                "                  : (bbbbbbbbbbbbbbb //\n"
2715                "                         ? ccccccccccccccc\n"
2716                "                         : ddddddddddddddd);");
2717 
2718   FormatStyle NoBinPacking = getLLVMStyle();
2719   NoBinPacking.BinPackParameters = false;
2720   verifyFormat(
2721       "void f() {\n"
2722       "  g(aaa,\n"
2723       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
2724       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2725       "        ? aaaaaaaaaaaaaaa\n"
2726       "        : aaaaaaaaaaaaaaa);\n"
2727       "}",
2728       NoBinPacking);
2729 }
2730 
2731 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
2732   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
2733                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
2734   verifyFormat("bool a = true, b = false;");
2735 
2736   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2737                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
2738                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
2739                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
2740   verifyFormat(
2741       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2742       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
2743       "     d = e && f;");
2744   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
2745                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
2746   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
2747                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
2748   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
2749                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
2750   // FIXME: If multiple variables are defined, the "*" needs to move to the new
2751   // line. Also fix indent for breaking after the type, this looks bad.
2752   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
2753                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
2754                "    *b = bbbbbbbbbbbbbbbbbbb;",
2755                getGoogleStyle());
2756 
2757   // Not ideal, but pointer-with-type does not allow much here.
2758   verifyGoogleFormat(
2759       "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
2760       "           *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
2761 }
2762 
2763 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
2764   verifyFormat("arr[foo ? bar : baz];");
2765   verifyFormat("f()[foo ? bar : baz];");
2766   verifyFormat("(a + b)[foo ? bar : baz];");
2767   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
2768 }
2769 
2770 TEST_F(FormatTest, AlignsStringLiterals) {
2771   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
2772                "                                      \"short literal\");");
2773   verifyFormat(
2774       "looooooooooooooooooooooooongFunction(\n"
2775       "    \"short literal\"\n"
2776       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
2777   verifyFormat("someFunction(\"Always break between multi-line\"\n"
2778                "             \" string literals\",\n"
2779                "             and, other, parameters);");
2780   EXPECT_EQ("fun + \"1243\" /* comment */\n"
2781             "      \"5678\";",
2782             format("fun + \"1243\" /* comment */\n"
2783                    "      \"5678\";",
2784                    getLLVMStyleWithColumns(28)));
2785   EXPECT_EQ(
2786       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
2787       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
2788       "         \"aaaaaaaaaaaaaaaa\";",
2789       format("aaaaaa ="
2790              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
2791              "aaaaaaaaaaaaaaaaaaaaa\" "
2792              "\"aaaaaaaaaaaaaaaa\";"));
2793   verifyFormat("a = a + \"a\"\n"
2794                "        \"a\"\n"
2795                "        \"a\";");
2796   verifyFormat("f(\"a\", \"b\"\n"
2797                "       \"c\");");
2798 
2799   verifyFormat(
2800       "#define LL_FORMAT \"ll\"\n"
2801       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
2802       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
2803 
2804   verifyFormat("#define A(X)          \\\n"
2805                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
2806                "  \"ccccc\"",
2807                getLLVMStyleWithColumns(23));
2808   verifyFormat("#define A \"def\"\n"
2809                "f(\"abc\" A \"ghi\"\n"
2810                "  \"jkl\");");
2811 }
2812 
2813 TEST_F(FormatTest, AlignsPipes) {
2814   verifyFormat(
2815       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2816       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2817       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2818   verifyFormat(
2819       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
2820       "                     << aaaaaaaaaaaaaaaaaaaa;");
2821   verifyFormat(
2822       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2823       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2824   verifyFormat(
2825       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
2826       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
2827       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
2828   verifyFormat(
2829       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2830       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2831       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2832 
2833   verifyFormat("return out << \"somepacket = {\\n\"\n"
2834                "           << \"  aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
2835                "           << \"  bbbb = \" << pkt.bbbb << \"\\n\"\n"
2836                "           << \"  cccccc = \" << pkt.cccccc << \"\\n\"\n"
2837                "           << \"  ddd = [\" << pkt.ddd << \"]\\n\"\n"
2838                "           << \"}\";");
2839 
2840   verifyFormat(
2841       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
2842       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
2843       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
2844       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
2845       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
2846   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
2847                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
2848   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaa: \"\n"
2849                "             << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2850 
2851   verifyFormat(
2852       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2853       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2854 }
2855 
2856 TEST_F(FormatTest, UnderstandsEquals) {
2857   verifyFormat(
2858       "aaaaaaaaaaaaaaaaa =\n"
2859       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2860   verifyFormat(
2861       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2862       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2863   verifyFormat(
2864       "if (a) {\n"
2865       "  f();\n"
2866       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2867       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2868       "}");
2869 
2870   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2871                "        100000000 + 10000000) {\n}");
2872 }
2873 
2874 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
2875   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
2876                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
2877 
2878   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
2879                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
2880 
2881   verifyFormat(
2882       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
2883       "                                                          Parameter2);");
2884 
2885   verifyFormat(
2886       "ShortObject->shortFunction(\n"
2887       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
2888       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
2889 
2890   verifyFormat("loooooooooooooongFunction(\n"
2891                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
2892 
2893   verifyFormat(
2894       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
2895       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
2896 
2897   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
2898                "    .WillRepeatedly(Return(SomeValue));");
2899   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
2900                "    .insert(ccccccccccccccccccccccc);");
2901   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2902                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
2903                "      aaaaaaaaaaaaaaaaaaaaa);");
2904   verifyFormat(
2905       "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2906       "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2907       "    .aaaaaaaaaaaaaaa(\n"
2908       "         aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2909       "            aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
2910   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2911                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2912                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2913                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
2914                "}");
2915 
2916   // Here, it is not necessary to wrap at "." or "->".
2917   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
2918                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2919   verifyFormat(
2920       "aaaaaaaaaaa->aaaaaaaaa(\n"
2921       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2922       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
2923 
2924   verifyFormat(
2925       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2926       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
2927   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
2928                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
2929   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
2930                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
2931 
2932   // FIXME: Should we break before .a()?
2933   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2934                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
2935 
2936   FormatStyle NoBinPacking = getLLVMStyle();
2937   NoBinPacking.BinPackParameters = false;
2938   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
2939                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
2940                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
2941                "                         aaaaaaaaaaaaaaaaaaa,\n"
2942                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2943                NoBinPacking);
2944 }
2945 
2946 TEST_F(FormatTest, WrapsTemplateDeclarations) {
2947   verifyFormat("template <typename T>\n"
2948                "virtual void loooooooooooongFunction(int Param1, int Param2);");
2949   verifyFormat(
2950       "template <typename T>\n"
2951       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
2952   verifyFormat("template <typename T>\n"
2953                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
2954                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
2955   verifyFormat(
2956       "template <typename T>\n"
2957       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
2958       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
2959   verifyFormat(
2960       "template <typename T>\n"
2961       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
2962       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
2963       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2964   verifyFormat("template <typename T>\n"
2965                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2966                "    int aaaaaaaaaaaaaaaaaaaaaa);");
2967   verifyFormat(
2968       "template <typename T1, typename T2 = char, typename T3 = char,\n"
2969       "          typename T4 = char>\n"
2970       "void f();");
2971   verifyFormat(
2972       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
2973       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2974 
2975   verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
2976                "    a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
2977 
2978   verifyFormat("template <typename T> class C {};");
2979   verifyFormat("template <typename T> void f();");
2980   verifyFormat("template <typename T> void f() {}");
2981 
2982   FormatStyle AlwaysBreak = getLLVMStyle();
2983   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
2984   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
2985   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
2986   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
2987   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2988                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
2989                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
2990 }
2991 
2992 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
2993   verifyFormat(
2994       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2995       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2996   verifyFormat(
2997       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2998       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2999       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
3000 
3001   // FIXME: Should we have the extra indent after the second break?
3002   verifyFormat(
3003       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
3004       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
3005       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3006 
3007   verifyFormat(
3008       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
3009       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
3010 
3011   // Breaking at nested name specifiers is generally not desirable.
3012   verifyFormat(
3013       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3014       "    aaaaaaaaaaaaaaaaaaaaaaa);");
3015 
3016   verifyFormat(
3017       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
3018       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3019       "                   aaaaaaaaaaaaaaaaaaaaa);",
3020       getLLVMStyleWithColumns(74));
3021 
3022   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
3023                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3024                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3025 }
3026 
3027 TEST_F(FormatTest, UnderstandsTemplateParameters) {
3028   verifyFormat("A<int> a;");
3029   verifyFormat("A<A<A<int> > > a;");
3030   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
3031   verifyFormat("bool x = a < 1 || 2 > a;");
3032   verifyFormat("bool x = 5 < f<int>();");
3033   verifyFormat("bool x = f<int>() > 5;");
3034   verifyFormat("bool x = 5 < a<int>::x;");
3035   verifyFormat("bool x = a < 4 ? a > 2 : false;");
3036   verifyFormat("bool x = f() ? a < 2 : a > 2;");
3037 
3038   verifyGoogleFormat("A<A<int>> a;");
3039   verifyGoogleFormat("A<A<A<int>>> a;");
3040   verifyGoogleFormat("A<A<A<A<int>>>> a;");
3041   verifyGoogleFormat("A<A<int> > a;");
3042   verifyGoogleFormat("A<A<A<int> > > a;");
3043   verifyGoogleFormat("A<A<A<A<int> > > > a;");
3044   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
3045   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
3046 
3047   verifyFormat("test >> a >> b;");
3048   verifyFormat("test << a >> b;");
3049 
3050   verifyFormat("f<int>();");
3051   verifyFormat("template <typename T> void f() {}");
3052 
3053   // Not template parameters.
3054   verifyFormat("return a < b && c > d;");
3055   verifyFormat("void f() {\n"
3056                "  while (a < b && c > d) {\n"
3057                "  }\n"
3058                "}");
3059 }
3060 
3061 TEST_F(FormatTest, UnderstandsBinaryOperators) {
3062   verifyFormat("COMPARE(a, ==, b);");
3063 }
3064 
3065 TEST_F(FormatTest, UnderstandsPointersToMembers) {
3066   verifyFormat("int A::*x;");
3067   verifyFormat("int (S::*func)(void *);");
3068   verifyFormat("void f() { int (S::*func)(void *); }");
3069   verifyFormat("typedef bool *(Class::*Member)() const;");
3070   verifyFormat("void f() {\n"
3071                "  (a->*f)();\n"
3072                "  a->*x;\n"
3073                "  (a.*f)();\n"
3074                "  ((*a).*f)();\n"
3075                "  a.*x;\n"
3076                "}");
3077   FormatStyle Style = getLLVMStyle();
3078   Style.PointerBindsToType = true;
3079   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
3080 }
3081 
3082 TEST_F(FormatTest, UnderstandsUnaryOperators) {
3083   verifyFormat("int a = -2;");
3084   verifyFormat("f(-1, -2, -3);");
3085   verifyFormat("a[-1] = 5;");
3086   verifyFormat("int a = 5 + -2;");
3087   verifyFormat("if (i == -1) {\n}");
3088   verifyFormat("if (i != -1) {\n}");
3089   verifyFormat("if (i > -1) {\n}");
3090   verifyFormat("if (i < -1) {\n}");
3091   verifyFormat("++(a->f());");
3092   verifyFormat("--(a->f());");
3093   verifyFormat("(a->f())++;");
3094   verifyFormat("a[42]++;");
3095   verifyFormat("if (!(a->f())) {\n}");
3096 
3097   verifyFormat("a-- > b;");
3098   verifyFormat("b ? -a : c;");
3099   verifyFormat("n * sizeof char16;");
3100   verifyFormat("n * alignof char16;");
3101   verifyFormat("sizeof(char);");
3102   verifyFormat("alignof(char);");
3103 
3104   verifyFormat("return -1;");
3105   verifyFormat("switch (a) {\n"
3106                "case -1:\n"
3107                "  break;\n"
3108                "}");
3109   verifyFormat("#define X -1");
3110   verifyFormat("#define X -kConstant");
3111 
3112   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
3113   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
3114 
3115   verifyFormat("int a = /* confusing comment */ -1;");
3116   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
3117   verifyFormat("int a = i /* confusing comment */++;");
3118 }
3119 
3120 TEST_F(FormatTest, UndestandsOverloadedOperators) {
3121   verifyFormat("bool operator<();");
3122   verifyFormat("bool operator>();");
3123   verifyFormat("bool operator=();");
3124   verifyFormat("bool operator==();");
3125   verifyFormat("bool operator!=();");
3126   verifyFormat("int operator+();");
3127   verifyFormat("int operator++();");
3128   verifyFormat("bool operator();");
3129   verifyFormat("bool operator()();");
3130   verifyFormat("bool operator[]();");
3131   verifyFormat("operator bool();");
3132   verifyFormat("operator int();");
3133   verifyFormat("operator void *();");
3134   verifyFormat("operator SomeType<int>();");
3135   verifyFormat("operator SomeType<int, int>();");
3136   verifyFormat("operator SomeType<SomeType<int> >();");
3137   verifyFormat("void *operator new(std::size_t size);");
3138   verifyFormat("void *operator new[](std::size_t size);");
3139   verifyFormat("void operator delete(void *ptr);");
3140   verifyFormat("void operator delete[](void *ptr);");
3141   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
3142                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
3143 
3144   verifyFormat(
3145       "ostream &operator<<(ostream &OutputStream,\n"
3146       "                    SomeReallyLongType WithSomeReallyLongValue);");
3147   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
3148                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
3149                "  return left.group < right.group;\n"
3150                "}");
3151   verifyFormat("SomeType &operator=(const SomeType &S);");
3152 
3153   verifyGoogleFormat("operator void*();");
3154   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
3155 }
3156 
3157 TEST_F(FormatTest, UnderstandsNewAndDelete) {
3158   verifyFormat("void f() {\n"
3159                "  A *a = new A;\n"
3160                "  A *a = new (placement) A;\n"
3161                "  delete a;\n"
3162                "  delete (A *)a;\n"
3163                "}");
3164 }
3165 
3166 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
3167   verifyFormat("int *f(int *a) {}");
3168   verifyFormat("int main(int argc, char **argv) {}");
3169   verifyFormat("Test::Test(int b) : a(b * b) {}");
3170   verifyIndependentOfContext("f(a, *a);");
3171   verifyFormat("void g() { f(*a); }");
3172   verifyIndependentOfContext("int a = b * 10;");
3173   verifyIndependentOfContext("int a = 10 * b;");
3174   verifyIndependentOfContext("int a = b * c;");
3175   verifyIndependentOfContext("int a += b * c;");
3176   verifyIndependentOfContext("int a -= b * c;");
3177   verifyIndependentOfContext("int a *= b * c;");
3178   verifyIndependentOfContext("int a /= b * c;");
3179   verifyIndependentOfContext("int a = *b;");
3180   verifyIndependentOfContext("int a = *b * c;");
3181   verifyIndependentOfContext("int a = b * *c;");
3182   verifyIndependentOfContext("return 10 * b;");
3183   verifyIndependentOfContext("return *b * *c;");
3184   verifyIndependentOfContext("return a & ~b;");
3185   verifyIndependentOfContext("f(b ? *c : *d);");
3186   verifyIndependentOfContext("int a = b ? *c : *d;");
3187   verifyIndependentOfContext("*b = a;");
3188   verifyIndependentOfContext("a * ~b;");
3189   verifyIndependentOfContext("a * !b;");
3190   verifyIndependentOfContext("a * +b;");
3191   verifyIndependentOfContext("a * -b;");
3192   verifyIndependentOfContext("a * ++b;");
3193   verifyIndependentOfContext("a * --b;");
3194   verifyIndependentOfContext("a[4] * b;");
3195   verifyIndependentOfContext("a[a * a] = 1;");
3196   verifyIndependentOfContext("f() * b;");
3197   verifyIndependentOfContext("a * [self dostuff];");
3198   verifyIndependentOfContext("int x = a * (a + b);");
3199   verifyIndependentOfContext("(a *)(a + b);");
3200   verifyIndependentOfContext("int *pa = (int *)&a;");
3201   verifyIndependentOfContext("return sizeof(int **);");
3202   verifyIndependentOfContext("return sizeof(int ******);");
3203   verifyIndependentOfContext("return (int **&)a;");
3204   verifyIndependentOfContext("f((*PointerToArray)[10]);");
3205   verifyFormat("void f(Type (*parameter)[10]) {}");
3206   verifyGoogleFormat("return sizeof(int**);");
3207   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
3208   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
3209   verifyFormat("auto a = [](int **&, int ***) {};");
3210 
3211   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
3212 
3213   verifyIndependentOfContext("A<int *> a;");
3214   verifyIndependentOfContext("A<int **> a;");
3215   verifyIndependentOfContext("A<int *, int *> a;");
3216   verifyIndependentOfContext(
3217       "const char *const p = reinterpret_cast<const char *const>(q);");
3218   verifyIndependentOfContext("A<int **, int **> a;");
3219   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
3220   verifyFormat("for (char **a = b; *a; ++a) {\n}");
3221   verifyFormat("for (; a && b;) {\n}");
3222 
3223   verifyFormat(
3224       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3225       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3226 
3227   verifyGoogleFormat("int main(int argc, char** argv) {}");
3228   verifyGoogleFormat("A<int*> a;");
3229   verifyGoogleFormat("A<int**> a;");
3230   verifyGoogleFormat("A<int*, int*> a;");
3231   verifyGoogleFormat("A<int**, int**> a;");
3232   verifyGoogleFormat("f(b ? *c : *d);");
3233   verifyGoogleFormat("int a = b ? *c : *d;");
3234   verifyGoogleFormat("Type* t = **x;");
3235   verifyGoogleFormat("Type* t = *++*x;");
3236   verifyGoogleFormat("*++*x;");
3237   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
3238   verifyGoogleFormat("Type* t = x++ * y;");
3239   verifyGoogleFormat(
3240       "const char* const p = reinterpret_cast<const char* const>(q);");
3241 
3242   verifyIndependentOfContext("a = *(x + y);");
3243   verifyIndependentOfContext("a = &(x + y);");
3244   verifyIndependentOfContext("*(x + y).call();");
3245   verifyIndependentOfContext("&(x + y)->call();");
3246   verifyFormat("void f() { &(*I).first; }");
3247 
3248   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
3249   verifyFormat(
3250       "int *MyValues = {\n"
3251       "  *A, // Operator detection might be confused by the '{'\n"
3252       "  *BB // Operator detection might be confused by previous comment\n"
3253       "};");
3254 
3255   verifyIndependentOfContext("if (int *a = &b)");
3256   verifyIndependentOfContext("if (int &a = *b)");
3257   verifyIndependentOfContext("if (a & b[i])");
3258   verifyIndependentOfContext("if (a::b::c::d & b[i])");
3259   verifyIndependentOfContext("if (*b[i])");
3260   verifyIndependentOfContext("if (int *a = (&b))");
3261   verifyIndependentOfContext("while (int *a = &b)");
3262   verifyFormat("void f() {\n"
3263                "  for (const int &v : Values) {\n"
3264                "  }\n"
3265                "}");
3266   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
3267   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
3268 
3269   verifyFormat("#define MACRO     \\\n"
3270                "  int *i = a * b; \\\n"
3271                "  void f(a *b);",
3272                getLLVMStyleWithColumns(19));
3273 
3274   verifyIndependentOfContext("A = new SomeType *[Length];");
3275   verifyIndependentOfContext("A = new SomeType *[Length]();");
3276   verifyGoogleFormat("A = new SomeType* [Length]();");
3277   verifyGoogleFormat("A = new SomeType* [Length];");
3278 
3279   FormatStyle PointerLeft = getLLVMStyle();
3280   PointerLeft.PointerBindsToType = true;
3281   verifyFormat("delete *x;", PointerLeft);
3282 }
3283 
3284 TEST_F(FormatTest, UnderstandsEllipsis) {
3285   verifyFormat("int printf(const char *fmt, ...);");
3286   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
3287 }
3288 
3289 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
3290   EXPECT_EQ("int *a;\n"
3291             "int *a;\n"
3292             "int *a;",
3293             format("int *a;\n"
3294                    "int* a;\n"
3295                    "int *a;",
3296                    getGoogleStyle()));
3297   EXPECT_EQ("int* a;\n"
3298             "int* a;\n"
3299             "int* a;",
3300             format("int* a;\n"
3301                    "int* a;\n"
3302                    "int *a;",
3303                    getGoogleStyle()));
3304   EXPECT_EQ("int *a;\n"
3305             "int *a;\n"
3306             "int *a;",
3307             format("int *a;\n"
3308                    "int * a;\n"
3309                    "int *  a;",
3310                    getGoogleStyle()));
3311 }
3312 
3313 TEST_F(FormatTest, UnderstandsRvalueReferences) {
3314   verifyFormat("int f(int &&a) {}");
3315   verifyFormat("int f(int a, char &&b) {}");
3316   verifyFormat("void f() { int &&a = b; }");
3317   verifyGoogleFormat("int f(int a, char&& b) {}");
3318   verifyGoogleFormat("void f() { int&& a = b; }");
3319 
3320   verifyIndependentOfContext("A<int &&> a;");
3321   verifyIndependentOfContext("A<int &&, int &&> a;");
3322   verifyGoogleFormat("A<int&&> a;");
3323   verifyGoogleFormat("A<int&&, int&&> a;");
3324 }
3325 
3326 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
3327   verifyFormat("void f() {\n"
3328                "  x[aaaaaaaaa -\n"
3329                "    b] = 23;\n"
3330                "}",
3331                getLLVMStyleWithColumns(15));
3332 }
3333 
3334 TEST_F(FormatTest, FormatsCasts) {
3335   verifyFormat("Type *A = static_cast<Type *>(P);");
3336   verifyFormat("Type *A = (Type *)P;");
3337   verifyFormat("Type *A = (vector<Type *, int *>)P;");
3338   verifyFormat("int a = (int)(2.0f);");
3339   verifyFormat("int a = (int)2.0f;");
3340   verifyFormat("x[(int32)y];");
3341   verifyFormat("x = (int32)y;");
3342   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
3343   verifyFormat("int a = (int)*b;");
3344   verifyFormat("int a = (int)2.0f;");
3345   verifyFormat("int a = (int)~0;");
3346   verifyFormat("int a = (int)++a;");
3347   verifyFormat("int a = (int)sizeof(int);");
3348   verifyFormat("int a = (int)+2;");
3349   verifyFormat("my_int a = (my_int)2.0f;");
3350   verifyFormat("my_int a = (my_int)sizeof(int);");
3351   verifyFormat("return (my_int)aaa;");
3352 
3353   // FIXME: Without type knowledge, this can still fall apart miserably.
3354   verifyFormat("void f() { my_int a = (my_int) * b; }");
3355   verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
3356   verifyFormat("my_int a = (my_int) ~0;");
3357   verifyFormat("my_int a = (my_int)++ a;");
3358   verifyFormat("my_int a = (my_int) + 2;");
3359 
3360   // These are not casts.
3361   verifyFormat("void f(int *) {}");
3362   verifyFormat("f(foo)->b;");
3363   verifyFormat("f(foo).b;");
3364   verifyFormat("f(foo)(b);");
3365   verifyFormat("f(foo)[b];");
3366   verifyFormat("[](foo) { return 4; }(bar)];");
3367   verifyFormat("(*funptr)(foo)[4];");
3368   verifyFormat("funptrs[4](foo)[4];");
3369   verifyFormat("void f(int *);");
3370   verifyFormat("void f(int *) = 0;");
3371   verifyFormat("void f(SmallVector<int>) {}");
3372   verifyFormat("void f(SmallVector<int>);");
3373   verifyFormat("void f(SmallVector<int>) = 0;");
3374   verifyFormat("void f(int i = (kValue) * kMask) {}");
3375   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
3376   verifyFormat("int a = sizeof(int) * b;");
3377   verifyFormat("int a = alignof(int) * b;");
3378   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
3379   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
3380 
3381   // These are not casts, but at some point were confused with casts.
3382   verifyFormat("virtual void foo(int *) override;");
3383   verifyFormat("virtual void foo(char &) const;");
3384   verifyFormat("virtual void foo(int *a, char *) const;");
3385   verifyFormat("int a = sizeof(int *) + b;");
3386   verifyFormat("int a = alignof(int *) + b;");
3387 }
3388 
3389 TEST_F(FormatTest, FormatsFunctionTypes) {
3390   verifyFormat("A<bool()> a;");
3391   verifyFormat("A<SomeType()> a;");
3392   verifyFormat("A<void (*)(int, std::string)> a;");
3393   verifyFormat("A<void *(int)>;");
3394   verifyFormat("void *(*a)(int *, SomeType *);");
3395   verifyFormat("int (*func)(void *);");
3396   verifyFormat("void f() { int (*func)(void *); }");
3397 
3398   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
3399   verifyGoogleFormat("void* (*a)(int);");
3400 
3401   // Other constructs can look somewhat like function types:
3402   verifyFormat("A<sizeof(*x)> a;");
3403   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
3404 }
3405 
3406 TEST_F(FormatTest, BreaksLongDeclarations) {
3407   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
3408                "    AnotherNameForTheLongType;",
3409                getGoogleStyle());
3410   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
3411                "    LoooooooooooooooooooooooooooooooooooooooongVariable;",
3412                getGoogleStyle());
3413   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
3414                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
3415                getGoogleStyle());
3416   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
3417                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
3418 
3419   // FIXME: Without the comment, this breaks after "(".
3420   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
3421                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
3422                getGoogleStyle());
3423 
3424   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
3425                "                  int LoooooooooooooooooooongParam2) {}");
3426   verifyFormat(
3427       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
3428       "                                   SourceLocation L, IdentifierIn *II,\n"
3429       "                                   Type *T) {}");
3430   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
3431                "ReallyReallyLongFunctionName(\n"
3432                "    const std::string &SomeParameter,\n"
3433                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
3434                "        ReallyReallyLongParameterName,\n"
3435                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
3436                "        AnotherLongParameterName) {}");
3437   verifyFormat("template <typename A>\n"
3438                "SomeLoooooooooooooooooooooongType<\n"
3439                "    typename some_namespace::SomeOtherType<A>::Type>\n"
3440                "Function() {}");
3441   verifyFormat(
3442       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
3443       "    aaaaaaaaaaaaaaaaaaaaaaa;",
3444       getGoogleStyle());
3445 
3446   verifyGoogleFormat(
3447       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
3448       "                                   SourceLocation L) {}");
3449   verifyGoogleFormat(
3450       "some_namespace::LongReturnType\n"
3451       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
3452       "    int first_long_parameter, int second_parameter) {}");
3453 
3454   verifyGoogleFormat("template <typename T>\n"
3455                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3456                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
3457   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3458                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
3459 }
3460 
3461 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
3462   verifyFormat("(a)->b();");
3463   verifyFormat("--a;");
3464 }
3465 
3466 TEST_F(FormatTest, HandlesIncludeDirectives) {
3467   verifyFormat("#include <string>\n"
3468                "#include <a/b/c.h>\n"
3469                "#include \"a/b/string\"\n"
3470                "#include \"string.h\"\n"
3471                "#include \"string.h\"\n"
3472                "#include <a-a>\n"
3473                "#include < path with space >\n"
3474                "#include \"abc.h\" // this is included for ABC\n"
3475                "#include \"some long include\" // with a comment\n"
3476                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
3477                getLLVMStyleWithColumns(35));
3478 
3479   verifyFormat("#import <string>");
3480   verifyFormat("#import <a/b/c.h>");
3481   verifyFormat("#import \"a/b/string\"");
3482   verifyFormat("#import \"string.h\"");
3483   verifyFormat("#import \"string.h\"");
3484 }
3485 
3486 //===----------------------------------------------------------------------===//
3487 // Error recovery tests.
3488 //===----------------------------------------------------------------------===//
3489 
3490 TEST_F(FormatTest, IncompleteParameterLists) {
3491   FormatStyle NoBinPacking = getLLVMStyle();
3492   NoBinPacking.BinPackParameters = false;
3493   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
3494                "                        double *min_x,\n"
3495                "                        double *max_x,\n"
3496                "                        double *min_y,\n"
3497                "                        double *max_y,\n"
3498                "                        double *min_z,\n"
3499                "                        double *max_z, ) {}",
3500                NoBinPacking);
3501 }
3502 
3503 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
3504   verifyFormat("void f() { return; }\n42");
3505   verifyFormat("void f() {\n"
3506                "  if (0)\n"
3507                "    return;\n"
3508                "}\n"
3509                "42");
3510   verifyFormat("void f() { return }\n42");
3511   verifyFormat("void f() {\n"
3512                "  if (0)\n"
3513                "    return\n"
3514                "}\n"
3515                "42");
3516 }
3517 
3518 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
3519   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
3520   EXPECT_EQ("void f() {\n"
3521             "  if (a)\n"
3522             "    return\n"
3523             "}",
3524             format("void  f  (  )  {  if  ( a )  return  }"));
3525   EXPECT_EQ("namespace N {\n"
3526             "void f()\n"
3527             "}",
3528             format("namespace  N  {  void f()  }"));
3529   EXPECT_EQ("namespace N {\n"
3530             "void f() {}\n"
3531             "void g()\n"
3532             "}",
3533             format("namespace N  { void f( ) { } void g( ) }"));
3534 }
3535 
3536 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
3537   verifyFormat("int aaaaaaaa =\n"
3538                "    // Overlylongcomment\n"
3539                "    b;",
3540                getLLVMStyleWithColumns(20));
3541   verifyFormat("function(\n"
3542                "    ShortArgument,\n"
3543                "    LoooooooooooongArgument);\n",
3544                getLLVMStyleWithColumns(20));
3545 }
3546 
3547 TEST_F(FormatTest, IncorrectAccessSpecifier) {
3548   verifyFormat("public:");
3549   verifyFormat("class A {\n"
3550                "public\n"
3551                "  void f() {}\n"
3552                "};");
3553   verifyFormat("public\n"
3554                "int qwerty;");
3555   verifyFormat("public\n"
3556                "B {}");
3557   verifyFormat("public\n"
3558                "{}");
3559   verifyFormat("public\n"
3560                "B { int x; }");
3561 }
3562 
3563 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
3564   verifyFormat("{");
3565   verifyFormat("#})");
3566 }
3567 
3568 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
3569   verifyFormat("do {\n}");
3570   verifyFormat("do {\n}\n"
3571                "f();");
3572   verifyFormat("do {\n}\n"
3573                "wheeee(fun);");
3574   verifyFormat("do {\n"
3575                "  f();\n"
3576                "}");
3577 }
3578 
3579 TEST_F(FormatTest, IncorrectCodeMissingParens) {
3580   verifyFormat("if {\n  foo;\n  foo();\n}");
3581   verifyFormat("switch {\n  foo;\n  foo();\n}");
3582   verifyFormat("for {\n  foo;\n  foo();\n}");
3583   verifyFormat("while {\n  foo;\n  foo();\n}");
3584   verifyFormat("do {\n  foo;\n  foo();\n} while;");
3585 }
3586 
3587 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
3588   verifyFormat("namespace {\n"
3589                "class Foo {  Foo  (\n"
3590                "};\n"
3591                "} // comment");
3592 }
3593 
3594 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
3595   EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
3596   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
3597   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
3598   EXPECT_EQ("{\n  {}\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
3599 
3600   EXPECT_EQ("{\n"
3601             "    {\n"
3602             " breakme(\n"
3603             "     qwe);\n"
3604             "}\n",
3605             format("{\n"
3606                    "    {\n"
3607                    " breakme(qwe);\n"
3608                    "}\n",
3609                    getLLVMStyleWithColumns(10)));
3610 }
3611 
3612 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
3613   verifyFormat("int x = {\n"
3614                "  avariable,\n"
3615                "  b(alongervariable)\n"
3616                "};",
3617                getLLVMStyleWithColumns(25));
3618 }
3619 
3620 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
3621   verifyFormat("return (a)(b) { 1, 2, 3 };");
3622 }
3623 
3624 TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
3625     verifyFormat("vector<int> x{ 1, 2, 3, 4 };");
3626     verifyFormat("vector<T> x{ {}, {}, {}, {} };");
3627     verifyFormat("f({ 1, 2 });");
3628     verifyFormat("auto v = Foo{ 1 };");
3629     verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });");
3630     verifyFormat("Class::Class : member{ 1, 2, 3 } {}");
3631     verifyFormat("new vector<int>{ 1, 2, 3 };");
3632     verifyFormat("new int[3]{ 1, 2, 3 };");
3633     verifyFormat("return { arg1, arg2 };");
3634     verifyFormat("return { arg1, SomeType{ parameter } };");
3635     verifyFormat("new T{ arg1, arg2 };");
3636     verifyFormat("class Class {\n"
3637                  "  T member = { arg1, arg2 };\n"
3638                  "};");
3639     verifyFormat(
3640         "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3641         "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
3642         "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3643         "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };");
3644 
3645     FormatStyle NoSpaces = getLLVMStyle();
3646     NoSpaces.SpacesInBracedLists = false;
3647     verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
3648     verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
3649     verifyFormat("f({1, 2});", NoSpaces);
3650     verifyFormat("auto v = Foo{-1};", NoSpaces);
3651     verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces);
3652     verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces);
3653     verifyFormat("new vector<int>{1, 2, 3};", NoSpaces);
3654     verifyFormat("new int[3]{1, 2, 3};", NoSpaces);
3655     verifyFormat("return {arg1, arg2};", NoSpaces);
3656     verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces);
3657     verifyFormat("new T{arg1, arg2};", NoSpaces);
3658     verifyFormat("class Class {\n"
3659                  "  T member = {arg1, arg2};\n"
3660                  "};",
3661                  NoSpaces);
3662 }
3663 
3664 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
3665   verifyFormat("void f() { return 42; }");
3666   verifyFormat("void f() {\n"
3667                "  // Comment\n"
3668                "}");
3669   verifyFormat("{\n"
3670                "#error {\n"
3671                "  int a;\n"
3672                "}");
3673   verifyFormat("{\n"
3674                "  int a;\n"
3675                "#error {\n"
3676                "}");
3677   verifyFormat("void f() {} // comment");
3678   verifyFormat("void f() { int a; } // comment");
3679   verifyFormat("void f() {\n"
3680                "} // comment",
3681                getLLVMStyleWithColumns(15));
3682 
3683   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
3684   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
3685 
3686   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
3687   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
3688 }
3689 
3690 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
3691   // Elaborate type variable declarations.
3692   verifyFormat("struct foo a = { bar };\nint n;");
3693   verifyFormat("class foo a = { bar };\nint n;");
3694   verifyFormat("union foo a = { bar };\nint n;");
3695 
3696   // Elaborate types inside function definitions.
3697   verifyFormat("struct foo f() {}\nint n;");
3698   verifyFormat("class foo f() {}\nint n;");
3699   verifyFormat("union foo f() {}\nint n;");
3700 
3701   // Templates.
3702   verifyFormat("template <class X> void f() {}\nint n;");
3703   verifyFormat("template <struct X> void f() {}\nint n;");
3704   verifyFormat("template <union X> void f() {}\nint n;");
3705 
3706   // Actual definitions...
3707   verifyFormat("struct {\n} n;");
3708   verifyFormat(
3709       "template <template <class T, class Y>, class Z> class X {\n} n;");
3710   verifyFormat("union Z {\n  int n;\n} x;");
3711   verifyFormat("class MACRO Z {\n} n;");
3712   verifyFormat("class MACRO(X) Z {\n} n;");
3713   verifyFormat("class __attribute__(X) Z {\n} n;");
3714   verifyFormat("class __declspec(X) Z {\n} n;");
3715   verifyFormat("class A##B##C {\n} n;");
3716 
3717   // Redefinition from nested context:
3718   verifyFormat("class A::B::C {\n} n;");
3719 
3720   // Template definitions.
3721   verifyFormat(
3722       "template <typename F>\n"
3723       "Matcher(const Matcher<F> &Other,\n"
3724       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
3725       "                             !is_same<F, T>::value>::type * = 0)\n"
3726       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
3727 
3728   // FIXME: This is still incorrectly handled at the formatter side.
3729   verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {};");
3730 
3731   // FIXME:
3732   // This now gets parsed incorrectly as class definition.
3733   // verifyFormat("class A<int> f() {\n}\nint n;");
3734 
3735   // Elaborate types where incorrectly parsing the structural element would
3736   // break the indent.
3737   verifyFormat("if (true)\n"
3738                "  class X x;\n"
3739                "else\n"
3740                "  f();\n");
3741 
3742   // This is simply incomplete. Formatting is not important, but must not crash.
3743   verifyFormat("class A:");
3744 }
3745 
3746 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
3747   verifyFormat("#error Leave     all         white!!!!! space* alone!\n");
3748   verifyFormat("#warning Leave     all         white!!!!! space* alone!\n");
3749   EXPECT_EQ("#error 1", format("  #  error   1"));
3750   EXPECT_EQ("#warning 1", format("  #  warning 1"));
3751 }
3752 
3753 TEST_F(FormatTest, FormatHashIfExpressions) {
3754   // FIXME: Come up with a better indentation for #elif.
3755   verifyFormat(
3756       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
3757       "    defined(BBBBBBBB)\n"
3758       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
3759       "    defined(BBBBBBBB)\n"
3760       "#endif",
3761       getLLVMStyleWithColumns(65));
3762 }
3763 
3764 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
3765   FormatStyle AllowsMergedIf = getGoogleStyle();
3766   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
3767   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
3768   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
3769   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
3770   EXPECT_EQ("if (true) return 42;",
3771             format("if (true)\nreturn 42;", AllowsMergedIf));
3772   FormatStyle ShortMergedIf = AllowsMergedIf;
3773   ShortMergedIf.ColumnLimit = 25;
3774   verifyFormat("#define A \\\n"
3775                "  if (true) return 42;",
3776                ShortMergedIf);
3777   verifyFormat("#define A \\\n"
3778                "  f();    \\\n"
3779                "  if (true)\n"
3780                "#define B",
3781                ShortMergedIf);
3782   verifyFormat("#define A \\\n"
3783                "  f();    \\\n"
3784                "  if (true)\n"
3785                "g();",
3786                ShortMergedIf);
3787   verifyFormat("{\n"
3788                "#ifdef A\n"
3789                "  // Comment\n"
3790                "  if (true) continue;\n"
3791                "#endif\n"
3792                "  // Comment\n"
3793                "  if (true) continue;",
3794                ShortMergedIf);
3795 }
3796 
3797 TEST_F(FormatTest, BlockCommentsInControlLoops) {
3798   verifyFormat("if (0) /* a comment in a strange place */ {\n"
3799                "  f();\n"
3800                "}");
3801   verifyFormat("if (0) /* a comment in a strange place */ {\n"
3802                "  f();\n"
3803                "} /* another comment */ else /* comment #3 */ {\n"
3804                "  g();\n"
3805                "}");
3806   verifyFormat("while (0) /* a comment in a strange place */ {\n"
3807                "  f();\n"
3808                "}");
3809   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
3810                "  f();\n"
3811                "}");
3812   verifyFormat("do /* a comment in a strange place */ {\n"
3813                "  f();\n"
3814                "} /* another comment */ while (0);");
3815 }
3816 
3817 TEST_F(FormatTest, BlockComments) {
3818   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
3819             format("/* *//* */  /* */\n/* *//* */  /* */"));
3820   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
3821   EXPECT_EQ("#define A /*123*/\\\n"
3822             "  b\n"
3823             "/* */\n"
3824             "someCall(\n"
3825             "    parameter);",
3826             format("#define A /*123*/ b\n"
3827                    "/* */\n"
3828                    "someCall(parameter);",
3829                    getLLVMStyleWithColumns(15)));
3830 
3831   EXPECT_EQ("#define A\n"
3832             "/* */ someCall(\n"
3833             "    parameter);",
3834             format("#define A\n"
3835                    "/* */someCall(parameter);",
3836                    getLLVMStyleWithColumns(15)));
3837   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
3838   EXPECT_EQ("/*\n"
3839             "*\n"
3840             " * aaaaaa\n"
3841             "* aaaaaa\n"
3842             "*/",
3843             format("/*\n"
3844                    "*\n"
3845                    " * aaaaaa aaaaaa\n"
3846                    "*/",
3847                    getLLVMStyleWithColumns(10)));
3848   EXPECT_EQ("/*\n"
3849             "**\n"
3850             "* aaaaaa\n"
3851             "* aaaaaa\n"
3852             "*/",
3853             format("/*\n"
3854                    "**\n"
3855                    "* aaaaaa aaaaaa\n"
3856                    "*/",
3857                    getLLVMStyleWithColumns(10)));
3858 
3859   FormatStyle NoBinPacking = getLLVMStyle();
3860   NoBinPacking.BinPackParameters = false;
3861   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
3862             "             2, /* comment 2 */\n"
3863             "             3, /* comment 3 */\n"
3864             "             aaaa,\n"
3865             "             bbbb);",
3866             format("someFunction (1,   /* comment 1 */\n"
3867                    "                2, /* comment 2 */  \n"
3868                    "               3,   /* comment 3 */\n"
3869                    "aaaa, bbbb );",
3870                    NoBinPacking));
3871   verifyFormat(
3872       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3873       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3874   EXPECT_EQ(
3875       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
3876       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3877       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
3878       format(
3879           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
3880           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
3881           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
3882   EXPECT_EQ(
3883       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
3884       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
3885       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
3886       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
3887              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
3888              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
3889 }
3890 
3891 TEST_F(FormatTest, BlockCommentsInMacros) {
3892   EXPECT_EQ("#define A          \\\n"
3893             "  {                \\\n"
3894             "    /* one line */ \\\n"
3895             "    someCall();",
3896             format("#define A {        \\\n"
3897                    "  /* one line */   \\\n"
3898                    "  someCall();",
3899                    getLLVMStyleWithColumns(20)));
3900   EXPECT_EQ("#define A          \\\n"
3901             "  {                \\\n"
3902             "    /* previous */ \\\n"
3903             "    /* one line */ \\\n"
3904             "    someCall();",
3905             format("#define A {        \\\n"
3906                    "  /* previous */   \\\n"
3907                    "  /* one line */   \\\n"
3908                    "  someCall();",
3909                    getLLVMStyleWithColumns(20)));
3910 }
3911 
3912 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
3913   EXPECT_EQ("a = {\n"
3914             "  1111 /*    */\n"
3915             "};",
3916             format("a = {1111 /*    */\n"
3917                    "};",
3918                    getLLVMStyleWithColumns(15)));
3919   EXPECT_EQ("a = {\n"
3920             "  1111 /*      */\n"
3921             "};",
3922             format("a = {1111 /*      */\n"
3923                    "};",
3924                    getLLVMStyleWithColumns(15)));
3925 
3926   // FIXME: The formatting is still wrong here.
3927   EXPECT_EQ("a = {\n"
3928             "  1111 /*      a\n"
3929             "          */\n"
3930             "};",
3931             format("a = {1111 /*      a */\n"
3932                    "};",
3933                    getLLVMStyleWithColumns(15)));
3934 }
3935 
3936 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
3937   // FIXME: This is not what we want...
3938   verifyFormat("{\n"
3939                "// a"
3940                "// b");
3941 }
3942 
3943 TEST_F(FormatTest, FormatStarDependingOnContext) {
3944   verifyFormat("void f(int *a);");
3945   verifyFormat("void f() { f(fint * b); }");
3946   verifyFormat("class A {\n  void f(int *a);\n};");
3947   verifyFormat("class A {\n  int *a;\n};");
3948   verifyFormat("namespace a {\n"
3949                "namespace b {\n"
3950                "class A {\n"
3951                "  void f() {}\n"
3952                "  int *a;\n"
3953                "};\n"
3954                "}\n"
3955                "}");
3956 }
3957 
3958 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
3959   verifyFormat("while");
3960   verifyFormat("operator");
3961 }
3962 
3963 //===----------------------------------------------------------------------===//
3964 // Objective-C tests.
3965 //===----------------------------------------------------------------------===//
3966 
3967 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
3968   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
3969   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
3970             format("-(NSUInteger)indexOfObject:(id)anObject;"));
3971   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
3972   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
3973   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
3974             format("-(NSInteger)Method3:(id)anObject;"));
3975   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
3976             format("-(NSInteger)Method4:(id)anObject;"));
3977   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
3978             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
3979   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
3980             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
3981   EXPECT_EQ(
3982       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
3983       format(
3984           "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
3985 
3986   // Very long objectiveC method declaration.
3987   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
3988                "                    inRange:(NSRange)range\n"
3989                "                   outRange:(NSRange)out_range\n"
3990                "                  outRange1:(NSRange)out_range1\n"
3991                "                  outRange2:(NSRange)out_range2\n"
3992                "                  outRange3:(NSRange)out_range3\n"
3993                "                  outRange4:(NSRange)out_range4\n"
3994                "                  outRange5:(NSRange)out_range5\n"
3995                "                  outRange6:(NSRange)out_range6\n"
3996                "                  outRange7:(NSRange)out_range7\n"
3997                "                  outRange8:(NSRange)out_range8\n"
3998                "                  outRange9:(NSRange)out_range9;");
3999 
4000   verifyFormat("- (int)sum:(vector<int>)numbers;");
4001   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
4002   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
4003   // protocol lists (but not for template classes):
4004   //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
4005 
4006   verifyFormat("- (int (*)())foo:(int (*)())f;");
4007   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
4008 
4009   // If there's no return type (very rare in practice!), LLVM and Google style
4010   // agree.
4011   verifyFormat("- foo;");
4012   verifyFormat("- foo:(int)f;");
4013   verifyGoogleFormat("- foo:(int)foo;");
4014 }
4015 
4016 TEST_F(FormatTest, FormatObjCBlocks) {
4017   verifyFormat("int (^Block)(int, int);");
4018   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
4019 }
4020 
4021 TEST_F(FormatTest, FormatObjCInterface) {
4022   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
4023                "@public\n"
4024                "  int field1;\n"
4025                "@protected\n"
4026                "  int field2;\n"
4027                "@private\n"
4028                "  int field3;\n"
4029                "@package\n"
4030                "  int field4;\n"
4031                "}\n"
4032                "+ (id)init;\n"
4033                "@end");
4034 
4035   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
4036                      " @public\n"
4037                      "  int field1;\n"
4038                      " @protected\n"
4039                      "  int field2;\n"
4040                      " @private\n"
4041                      "  int field3;\n"
4042                      " @package\n"
4043                      "  int field4;\n"
4044                      "}\n"
4045                      "+ (id)init;\n"
4046                      "@end");
4047 
4048   verifyFormat("@interface /* wait for it */ Foo\n"
4049                "+ (id)init;\n"
4050                "// Look, a comment!\n"
4051                "- (int)answerWith:(int)i;\n"
4052                "@end");
4053 
4054   verifyFormat("@interface Foo\n"
4055                "@end\n"
4056                "@interface Bar\n"
4057                "@end");
4058 
4059   verifyFormat("@interface Foo : Bar\n"
4060                "+ (id)init;\n"
4061                "@end");
4062 
4063   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
4064                "+ (id)init;\n"
4065                "@end");
4066 
4067   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
4068                      "+ (id)init;\n"
4069                      "@end");
4070 
4071   verifyFormat("@interface Foo (HackStuff)\n"
4072                "+ (id)init;\n"
4073                "@end");
4074 
4075   verifyFormat("@interface Foo ()\n"
4076                "+ (id)init;\n"
4077                "@end");
4078 
4079   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
4080                "+ (id)init;\n"
4081                "@end");
4082 
4083   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
4084                      "+ (id)init;\n"
4085                      "@end");
4086 
4087   verifyFormat("@interface Foo {\n"
4088                "  int _i;\n"
4089                "}\n"
4090                "+ (id)init;\n"
4091                "@end");
4092 
4093   verifyFormat("@interface Foo : Bar {\n"
4094                "  int _i;\n"
4095                "}\n"
4096                "+ (id)init;\n"
4097                "@end");
4098 
4099   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
4100                "  int _i;\n"
4101                "}\n"
4102                "+ (id)init;\n"
4103                "@end");
4104 
4105   verifyFormat("@interface Foo (HackStuff) {\n"
4106                "  int _i;\n"
4107                "}\n"
4108                "+ (id)init;\n"
4109                "@end");
4110 
4111   verifyFormat("@interface Foo () {\n"
4112                "  int _i;\n"
4113                "}\n"
4114                "+ (id)init;\n"
4115                "@end");
4116 
4117   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
4118                "  int _i;\n"
4119                "}\n"
4120                "+ (id)init;\n"
4121                "@end");
4122 }
4123 
4124 TEST_F(FormatTest, FormatObjCImplementation) {
4125   verifyFormat("@implementation Foo : NSObject {\n"
4126                "@public\n"
4127                "  int field1;\n"
4128                "@protected\n"
4129                "  int field2;\n"
4130                "@private\n"
4131                "  int field3;\n"
4132                "@package\n"
4133                "  int field4;\n"
4134                "}\n"
4135                "+ (id)init {\n}\n"
4136                "@end");
4137 
4138   verifyGoogleFormat("@implementation Foo : NSObject {\n"
4139                      " @public\n"
4140                      "  int field1;\n"
4141                      " @protected\n"
4142                      "  int field2;\n"
4143                      " @private\n"
4144                      "  int field3;\n"
4145                      " @package\n"
4146                      "  int field4;\n"
4147                      "}\n"
4148                      "+ (id)init {\n}\n"
4149                      "@end");
4150 
4151   verifyFormat("@implementation Foo\n"
4152                "+ (id)init {\n"
4153                "  if (true)\n"
4154                "    return nil;\n"
4155                "}\n"
4156                "// Look, a comment!\n"
4157                "- (int)answerWith:(int)i {\n"
4158                "  return i;\n"
4159                "}\n"
4160                "+ (int)answerWith:(int)i {\n"
4161                "  return i;\n"
4162                "}\n"
4163                "@end");
4164 
4165   verifyFormat("@implementation Foo\n"
4166                "@end\n"
4167                "@implementation Bar\n"
4168                "@end");
4169 
4170   verifyFormat("@implementation Foo : Bar\n"
4171                "+ (id)init {\n}\n"
4172                "- (void)foo {\n}\n"
4173                "@end");
4174 
4175   verifyFormat("@implementation Foo {\n"
4176                "  int _i;\n"
4177                "}\n"
4178                "+ (id)init {\n}\n"
4179                "@end");
4180 
4181   verifyFormat("@implementation Foo : Bar {\n"
4182                "  int _i;\n"
4183                "}\n"
4184                "+ (id)init {\n}\n"
4185                "@end");
4186 
4187   verifyFormat("@implementation Foo (HackStuff)\n"
4188                "+ (id)init {\n}\n"
4189                "@end");
4190 }
4191 
4192 TEST_F(FormatTest, FormatObjCProtocol) {
4193   verifyFormat("@protocol Foo\n"
4194                "@property(weak) id delegate;\n"
4195                "- (NSUInteger)numberOfThings;\n"
4196                "@end");
4197 
4198   verifyFormat("@protocol MyProtocol <NSObject>\n"
4199                "- (NSUInteger)numberOfThings;\n"
4200                "@end");
4201 
4202   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
4203                      "- (NSUInteger)numberOfThings;\n"
4204                      "@end");
4205 
4206   verifyFormat("@protocol Foo;\n"
4207                "@protocol Bar;\n");
4208 
4209   verifyFormat("@protocol Foo\n"
4210                "@end\n"
4211                "@protocol Bar\n"
4212                "@end");
4213 
4214   verifyFormat("@protocol myProtocol\n"
4215                "- (void)mandatoryWithInt:(int)i;\n"
4216                "@optional\n"
4217                "- (void)optional;\n"
4218                "@required\n"
4219                "- (void)required;\n"
4220                "@optional\n"
4221                "@property(assign) int madProp;\n"
4222                "@end\n");
4223 }
4224 
4225 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
4226   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
4227                "                   rect:(NSRect)theRect\n"
4228                "               interval:(float)theInterval {\n"
4229                "}");
4230   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
4231                "          longKeyword:(NSRect)theRect\n"
4232                "    evenLongerKeyword:(float)theInterval\n"
4233                "                error:(NSError **)theError {\n"
4234                "}");
4235 }
4236 
4237 TEST_F(FormatTest, FormatObjCMethodExpr) {
4238   verifyFormat("[foo bar:baz];");
4239   verifyFormat("return [foo bar:baz];");
4240   verifyFormat("f([foo bar:baz]);");
4241   verifyFormat("f(2, [foo bar:baz]);");
4242   verifyFormat("f(2, a ? b : c);");
4243   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
4244 
4245   // Unary operators.
4246   verifyFormat("int a = +[foo bar:baz];");
4247   verifyFormat("int a = -[foo bar:baz];");
4248   verifyFormat("int a = ![foo bar:baz];");
4249   verifyFormat("int a = ~[foo bar:baz];");
4250   verifyFormat("int a = ++[foo bar:baz];");
4251   verifyFormat("int a = --[foo bar:baz];");
4252   verifyFormat("int a = sizeof [foo bar:baz];");
4253   verifyFormat("int a = alignof [foo bar:baz];");
4254   verifyFormat("int a = &[foo bar:baz];");
4255   verifyFormat("int a = *[foo bar:baz];");
4256   // FIXME: Make casts work, without breaking f()[4].
4257   //verifyFormat("int a = (int)[foo bar:baz];");
4258   //verifyFormat("return (int)[foo bar:baz];");
4259   //verifyFormat("(void)[foo bar:baz];");
4260   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
4261 
4262   // Binary operators.
4263   verifyFormat("[foo bar:baz], [foo bar:baz];");
4264   verifyFormat("[foo bar:baz] = [foo bar:baz];");
4265   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
4266   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
4267   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
4268   verifyFormat("[foo bar:baz] += [foo bar:baz];");
4269   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
4270   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
4271   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
4272   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
4273   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
4274   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
4275   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
4276   verifyFormat("[foo bar:baz] || [foo bar:baz];");
4277   verifyFormat("[foo bar:baz] && [foo bar:baz];");
4278   verifyFormat("[foo bar:baz] | [foo bar:baz];");
4279   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
4280   verifyFormat("[foo bar:baz] & [foo bar:baz];");
4281   verifyFormat("[foo bar:baz] == [foo bar:baz];");
4282   verifyFormat("[foo bar:baz] != [foo bar:baz];");
4283   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
4284   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
4285   verifyFormat("[foo bar:baz] > [foo bar:baz];");
4286   verifyFormat("[foo bar:baz] < [foo bar:baz];");
4287   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
4288   verifyFormat("[foo bar:baz] << [foo bar:baz];");
4289   verifyFormat("[foo bar:baz] - [foo bar:baz];");
4290   verifyFormat("[foo bar:baz] + [foo bar:baz];");
4291   verifyFormat("[foo bar:baz] * [foo bar:baz];");
4292   verifyFormat("[foo bar:baz] / [foo bar:baz];");
4293   verifyFormat("[foo bar:baz] % [foo bar:baz];");
4294   // Whew!
4295 
4296   verifyFormat("return in[42];");
4297   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
4298                "}");
4299 
4300   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
4301   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
4302   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
4303   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
4304   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
4305   verifyFormat("[button setAction:@selector(zoomOut:)];");
4306   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
4307 
4308   verifyFormat("arr[[self indexForFoo:a]];");
4309   verifyFormat("throw [self errorFor:a];");
4310   verifyFormat("@throw [self errorFor:a];");
4311 
4312   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
4313   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
4314   verifyFormat("4 > 4 ? (id)a : (id)baz;");
4315 
4316   // This tests that the formatter doesn't break after "backing" but before ":",
4317   // which would be at 80 columns.
4318   verifyFormat(
4319       "void f() {\n"
4320       "  if ((self = [super initWithContentRect:contentRect\n"
4321       "                               styleMask:styleMask\n"
4322       "                                 backing:NSBackingStoreBuffered\n"
4323       "                                   defer:YES]))");
4324 
4325   verifyFormat(
4326       "[foo checkThatBreakingAfterColonWorksOk:\n"
4327       "        [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
4328 
4329   verifyFormat("[myObj short:arg1 // Force line break\n"
4330                "          longKeyword:arg2\n"
4331                "    evenLongerKeyword:arg3\n"
4332                "                error:arg4];");
4333   verifyFormat(
4334       "void f() {\n"
4335       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
4336       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
4337       "                                     pos.width(), pos.height())\n"
4338       "                styleMask:NSBorderlessWindowMask\n"
4339       "                  backing:NSBackingStoreBuffered\n"
4340       "                    defer:NO]);\n"
4341       "}");
4342   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
4343                "                             with:contentsNativeView];");
4344 
4345   verifyFormat(
4346       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
4347       "           owner:nillllll];");
4348 
4349   verifyFormat(
4350       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
4351       "        forType:kBookmarkButtonDragType];");
4352 
4353   verifyFormat("[defaultCenter addObserver:self\n"
4354                "                  selector:@selector(willEnterFullscreen)\n"
4355                "                      name:kWillEnterFullscreenNotification\n"
4356                "                    object:nil];");
4357   verifyFormat("[image_rep drawInRect:drawRect\n"
4358                "             fromRect:NSZeroRect\n"
4359                "            operation:NSCompositeCopy\n"
4360                "             fraction:1.0\n"
4361                "       respectFlipped:NO\n"
4362                "                hints:nil];");
4363 
4364   verifyFormat(
4365       "scoped_nsobject<NSTextField> message(\n"
4366       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
4367       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
4368 }
4369 
4370 TEST_F(FormatTest, ObjCAt) {
4371   verifyFormat("@autoreleasepool");
4372   verifyFormat("@catch");
4373   verifyFormat("@class");
4374   verifyFormat("@compatibility_alias");
4375   verifyFormat("@defs");
4376   verifyFormat("@dynamic");
4377   verifyFormat("@encode");
4378   verifyFormat("@end");
4379   verifyFormat("@finally");
4380   verifyFormat("@implementation");
4381   verifyFormat("@import");
4382   verifyFormat("@interface");
4383   verifyFormat("@optional");
4384   verifyFormat("@package");
4385   verifyFormat("@private");
4386   verifyFormat("@property");
4387   verifyFormat("@protected");
4388   verifyFormat("@protocol");
4389   verifyFormat("@public");
4390   verifyFormat("@required");
4391   verifyFormat("@selector");
4392   verifyFormat("@synchronized");
4393   verifyFormat("@synthesize");
4394   verifyFormat("@throw");
4395   verifyFormat("@try");
4396 
4397   EXPECT_EQ("@interface", format("@ interface"));
4398 
4399   // The precise formatting of this doesn't matter, nobody writes code like
4400   // this.
4401   verifyFormat("@ /*foo*/ interface");
4402 }
4403 
4404 TEST_F(FormatTest, ObjCSnippets) {
4405   verifyFormat("@autoreleasepool {\n"
4406                "  foo();\n"
4407                "}");
4408   verifyFormat("@class Foo, Bar;");
4409   verifyFormat("@compatibility_alias AliasName ExistingClass;");
4410   verifyFormat("@dynamic textColor;");
4411   verifyFormat("char *buf1 = @encode(int *);");
4412   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
4413   verifyFormat("char *buf1 = @encode(int **);");
4414   verifyFormat("Protocol *proto = @protocol(p1);");
4415   verifyFormat("SEL s = @selector(foo:);");
4416   verifyFormat("@synchronized(self) {\n"
4417                "  f();\n"
4418                "}");
4419 
4420   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
4421   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
4422 
4423   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
4424   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
4425   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
4426 
4427   verifyFormat("@import foo.bar;\n"
4428                "@import baz;");
4429 }
4430 
4431 TEST_F(FormatTest, ObjCLiterals) {
4432   verifyFormat("@\"String\"");
4433   verifyFormat("@1");
4434   verifyFormat("@+4.8");
4435   verifyFormat("@-4");
4436   verifyFormat("@1LL");
4437   verifyFormat("@.5");
4438   verifyFormat("@'c'");
4439   verifyFormat("@true");
4440 
4441   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
4442   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
4443   verifyFormat("NSNumber *favoriteColor = @(Green);");
4444   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
4445 
4446   verifyFormat("@[");
4447   verifyFormat("@[]");
4448   verifyFormat(
4449       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
4450   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
4451 
4452   verifyFormat("@{");
4453   verifyFormat("@{}");
4454   verifyFormat("@{ @\"one\" : @1 }");
4455   verifyFormat("return @{ @\"one\" : @1 };");
4456   verifyFormat("@{ @\"one\" : @1, }");
4457 
4458   verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
4459   verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
4460 
4461   verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
4462   verifyFormat("[self setDict:@{}");
4463   verifyFormat("[self setDict:@{ @1 : @2 }");
4464   verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
4465   verifyFormat(
4466       "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
4467   verifyFormat(
4468       "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
4469 
4470   // FIXME: Nested and multi-line array and dictionary literals need more work.
4471   verifyFormat(
4472       "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
4473       "                     @\"processInfo\" : [NSProcessInfo processInfo] };");
4474   verifyFormat(
4475       "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
4476       "   regularFont, };");
4477 
4478 }
4479 
4480 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
4481   EXPECT_EQ("{\n"
4482             "{\n"
4483             "a;\n"
4484             "b;\n"
4485             "}\n"
4486             "}",
4487             format("{\n"
4488                    "{\n"
4489                    "a;\n"
4490                    "     b;\n"
4491                    "}\n"
4492                    "}",
4493                    13, 2, getLLVMStyle()));
4494   EXPECT_EQ("{\n"
4495             "{\n"
4496             "  a;\n"
4497             "b;\n"
4498             "}\n"
4499             "}",
4500             format("{\n"
4501                    "{\n"
4502                    "     a;\n"
4503                    "b;\n"
4504                    "}\n"
4505                    "}",
4506                    9, 2, getLLVMStyle()));
4507   EXPECT_EQ("{\n"
4508             "{\n"
4509             "public:\n"
4510             "  b;\n"
4511             "}\n"
4512             "}",
4513             format("{\n"
4514                    "{\n"
4515                    "public:\n"
4516                    "     b;\n"
4517                    "}\n"
4518                    "}",
4519                    17, 2, getLLVMStyle()));
4520   EXPECT_EQ("{\n"
4521             "{\n"
4522             "a;\n"
4523             "}\n"
4524             "{\n"
4525             "  b; //\n"
4526             "}\n"
4527             "}",
4528             format("{\n"
4529                    "{\n"
4530                    "a;\n"
4531                    "}\n"
4532                    "{\n"
4533                    "           b; //\n"
4534                    "}\n"
4535                    "}",
4536                    22, 2, getLLVMStyle()));
4537   EXPECT_EQ("  {\n"
4538             "    a; //\n"
4539             "  }",
4540             format("  {\n"
4541                    "a; //\n"
4542                    "  }",
4543                    4, 2, getLLVMStyle()));
4544   EXPECT_EQ("void f() {}\n"
4545             "void g() {}",
4546             format("void f() {}\n"
4547                    "void g() {}",
4548                    13, 0, getLLVMStyle()));
4549   EXPECT_EQ("int a; // comment\n"
4550             "       // line 2\n"
4551             "int b;",
4552             format("int a; // comment\n"
4553                    "       // line 2\n"
4554                    "  int b;",
4555                    35, 0, getLLVMStyle()));
4556 }
4557 
4558 TEST_F(FormatTest, BreakStringLiterals) {
4559   EXPECT_EQ("\"some text \"\n"
4560             "\"other\";",
4561             format("\"some text other\";", getLLVMStyleWithColumns(12)));
4562   EXPECT_EQ("\"some text \"\n"
4563             "\"other\";",
4564             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
4565   EXPECT_EQ(
4566       "#define A  \\\n"
4567       "  \"some \"  \\\n"
4568       "  \"text \"  \\\n"
4569       "  \"other\";",
4570       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
4571   EXPECT_EQ(
4572       "#define A  \\\n"
4573       "  \"so \"    \\\n"
4574       "  \"text \"  \\\n"
4575       "  \"other\";",
4576       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
4577 
4578   EXPECT_EQ("\"some text\"",
4579             format("\"some text\"", getLLVMStyleWithColumns(1)));
4580   EXPECT_EQ("\"some text\"",
4581             format("\"some text\"", getLLVMStyleWithColumns(11)));
4582   EXPECT_EQ("\"some \"\n"
4583             "\"text\"",
4584             format("\"some text\"", getLLVMStyleWithColumns(10)));
4585   EXPECT_EQ("\"some \"\n"
4586             "\"text\"",
4587             format("\"some text\"", getLLVMStyleWithColumns(7)));
4588   EXPECT_EQ("\"some\"\n"
4589             "\" tex\"\n"
4590             "\"t\"",
4591             format("\"some text\"", getLLVMStyleWithColumns(6)));
4592   EXPECT_EQ("\"some\"\n"
4593             "\" tex\"\n"
4594             "\" and\"",
4595             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
4596   EXPECT_EQ("\"some\"\n"
4597             "\"/tex\"\n"
4598             "\"/and\"",
4599             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
4600 
4601   EXPECT_EQ("variable =\n"
4602             "    \"long string \"\n"
4603             "    \"literal\";",
4604             format("variable = \"long string literal\";",
4605                    getLLVMStyleWithColumns(20)));
4606 
4607   EXPECT_EQ("variable = f(\n"
4608             "    \"long string \"\n"
4609             "    \"literal\",\n"
4610             "    short,\n"
4611             "    loooooooooooooooooooong);",
4612             format("variable = f(\"long string literal\", short, "
4613                    "loooooooooooooooooooong);",
4614                    getLLVMStyleWithColumns(20)));
4615 
4616   EXPECT_EQ("f(g(\"long string \"\n"
4617             "    \"literal\"),\n"
4618             "  b);",
4619             format("f(g(\"long string literal\"), b);",
4620                    getLLVMStyleWithColumns(20)));
4621   EXPECT_EQ("f(g(\"long string \"\n"
4622             "    \"literal\",\n"
4623             "    a),\n"
4624             "  b);",
4625             format("f(g(\"long string literal\", a), b);",
4626                    getLLVMStyleWithColumns(20)));
4627   EXPECT_EQ(
4628       "f(\"one two\".split(\n"
4629       "    variable));",
4630       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
4631   EXPECT_EQ("f(\"one two three four five six \"\n"
4632             "  \"seven\".split(\n"
4633             "      really_looooong_variable));",
4634             format("f(\"one two three four five six seven\"."
4635                    "split(really_looooong_variable));",
4636                    getLLVMStyleWithColumns(33)));
4637 
4638   EXPECT_EQ("f(\"some \"\n"
4639             "  \"text\",\n"
4640             "  other);",
4641             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
4642 
4643   // Only break as a last resort.
4644   verifyFormat(
4645       "aaaaaaaaaaaaaaaaaaaa(\n"
4646       "    aaaaaaaaaaaaaaaaaaaa,\n"
4647       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
4648 
4649   EXPECT_EQ(
4650       "\"splitmea\"\n"
4651       "\"trandomp\"\n"
4652       "\"oint\"",
4653       format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
4654 
4655   EXPECT_EQ(
4656       "\"split/\"\n"
4657       "\"pathat/\"\n"
4658       "\"slashes\"",
4659       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
4660 
4661   EXPECT_EQ(
4662       "\"split/\"\n"
4663       "\"pathat/\"\n"
4664       "\"slashes\"",
4665       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
4666   EXPECT_EQ("\"split at \"\n"
4667             "\"spaces/at/\"\n"
4668             "\"slashes.at.any$\"\n"
4669             "\"non-alphanumeric%\"\n"
4670             "\"1111111111characte\"\n"
4671             "\"rs\"",
4672             format("\"split at "
4673                    "spaces/at/"
4674                    "slashes.at."
4675                    "any$non-"
4676                    "alphanumeric%"
4677                    "1111111111characte"
4678                    "rs\"",
4679                    getLLVMStyleWithColumns(20)));
4680 
4681   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
4682   AlignLeft.AlignEscapedNewlinesLeft = true;
4683   EXPECT_EQ(
4684       "#define A \\\n"
4685       "  \"some \" \\\n"
4686       "  \"text \" \\\n"
4687       "  \"other\";",
4688       format("#define A \"some text other\";", AlignLeft));
4689 }
4690 
4691 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
4692   EXPECT_EQ("u8\"unsupported literal\";",
4693             format("u8\"unsupported literal\";", getLLVMStyleWithColumns(15)));
4694   EXPECT_EQ("u\"unsupported literal\";",
4695             format("u\"unsupported literal\";", getLLVMStyleWithColumns(15)));
4696   EXPECT_EQ("U\"unsupported literal\";",
4697             format("U\"unsupported literal\";", getLLVMStyleWithColumns(15)));
4698   EXPECT_EQ("L\"unsupported literal\";",
4699             format("L\"unsupported literal\";", getLLVMStyleWithColumns(15)));
4700   EXPECT_EQ("R\"x(raw literal)x\";",
4701             format("R\"x(raw literal)x\";", getLLVMStyleWithColumns(15)));
4702 }
4703 
4704 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
4705   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
4706             "             \"ddeeefff\");",
4707             format("someFunction(\"aaabbbcccdddeeefff\");",
4708                    getLLVMStyleWithColumns(25)));
4709   EXPECT_EQ("someFunction1234567890(\n"
4710             "    \"aaabbbcccdddeeefff\");",
4711             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
4712                    getLLVMStyleWithColumns(26)));
4713   EXPECT_EQ("someFunction1234567890(\n"
4714             "    \"aaabbbcccdddeeeff\"\n"
4715             "    \"f\");",
4716             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
4717                    getLLVMStyleWithColumns(25)));
4718   EXPECT_EQ("someFunction1234567890(\n"
4719             "    \"aaabbbcccdddeeeff\"\n"
4720             "    \"f\");",
4721             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
4722                    getLLVMStyleWithColumns(24)));
4723   EXPECT_EQ("someFunction(\n"
4724             "    \"aaabbbcc \"\n"
4725             "    \"dddeeefff\");",
4726             format("someFunction(\"aaabbbcc dddeeefff\");",
4727                    getLLVMStyleWithColumns(25)));
4728   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
4729             "             \"ddeeefff\");",
4730             format("someFunction(\"aaabbbccc ddeeefff\");",
4731                    getLLVMStyleWithColumns(25)));
4732   EXPECT_EQ("someFunction1234567890(\n"
4733             "    \"aaabb \"\n"
4734             "    \"cccdddeeefff\");",
4735             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
4736                    getLLVMStyleWithColumns(25)));
4737   EXPECT_EQ("#define A          \\\n"
4738             "  string s =       \\\n"
4739             "      \"123456789\"  \\\n"
4740             "      \"0\";         \\\n"
4741             "  int i;",
4742             format("#define A string s = \"1234567890\"; int i;",
4743                    getLLVMStyleWithColumns(20)));
4744 }
4745 
4746 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
4747   EXPECT_EQ("\"\\a\"",
4748             format("\"\\a\"", getLLVMStyleWithColumns(3)));
4749   EXPECT_EQ("\"\\\"",
4750             format("\"\\\"", getLLVMStyleWithColumns(2)));
4751   EXPECT_EQ("\"test\"\n"
4752             "\"\\n\"",
4753             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
4754   EXPECT_EQ("\"tes\\\\\"\n"
4755             "\"n\"",
4756             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
4757   EXPECT_EQ("\"\\\\\\\\\"\n"
4758             "\"\\n\"",
4759             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
4760   EXPECT_EQ("\"\\uff01\"",
4761             format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
4762   EXPECT_EQ("\"\\uff01\"\n"
4763             "\"test\"",
4764             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
4765   EXPECT_EQ("\"\\Uff01ff02\"",
4766             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
4767   EXPECT_EQ("\"\\x000000000001\"\n"
4768             "\"next\"",
4769             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
4770   EXPECT_EQ("\"\\x000000000001next\"",
4771             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
4772   EXPECT_EQ("\"\\x000000000001\"",
4773             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
4774   EXPECT_EQ("\"test\"\n"
4775             "\"\\000000\"\n"
4776             "\"000001\"",
4777             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
4778   EXPECT_EQ("\"test\\000\"\n"
4779             "\"00000000\"\n"
4780             "\"1\"",
4781             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
4782   EXPECT_EQ("R\"(\\x\\x00)\"\n",
4783             format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7)));
4784 }
4785 
4786 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
4787   verifyFormat("void f() {\n"
4788                "  return g() {}\n"
4789                "  void h() {}");
4790   verifyFormat("if (foo)\n"
4791                "  return { forgot_closing_brace();\n"
4792                "test();");
4793   verifyFormat("int a[] = { void forgot_closing_brace() { f();\n"
4794                "g();\n"
4795                "}");
4796 }
4797 
4798 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
4799   verifyFormat("class X {\n"
4800                "  void f() {\n"
4801                "  }\n"
4802                "};",
4803                getLLVMStyleWithColumns(12));
4804 }
4805 
4806 TEST_F(FormatTest, ConfigurableIndentWidth) {
4807   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
4808   EightIndent.IndentWidth = 8;
4809   verifyFormat("void f() {\n"
4810                "        someFunction();\n"
4811                "        if (true) {\n"
4812                "                f();\n"
4813                "        }\n"
4814                "}",
4815                EightIndent);
4816   verifyFormat("class X {\n"
4817                "        void f() {\n"
4818                "        }\n"
4819                "};",
4820                EightIndent);
4821   verifyFormat("int x[] = {\n"
4822                "        call(),\n"
4823                "        call(),\n"
4824                "};",
4825                EightIndent);
4826 }
4827 
4828 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
4829   verifyFormat("void\n"
4830                "f();",
4831                getLLVMStyleWithColumns(8));
4832 }
4833 
4834 TEST_F(FormatTest, ConfigurableUseOfTab) {
4835   FormatStyle Tab = getLLVMStyleWithColumns(42);
4836   Tab.IndentWidth = 8;
4837   Tab.UseTab = true;
4838   Tab.AlignEscapedNewlinesLeft = true;
4839   verifyFormat("class X {\n"
4840                "\tvoid f() {\n"
4841                "\t\tsomeFunction(parameter1,\n"
4842                "\t\t\t     parameter2);\n"
4843                "\t}\n"
4844                "};",
4845                Tab);
4846   verifyFormat("#define A                        \\\n"
4847                "\tvoid f() {               \\\n"
4848                "\t\tsomeFunction(    \\\n"
4849                "\t\t    parameter1,  \\\n"
4850                "\t\t    parameter2); \\\n"
4851                "\t}",
4852                Tab);
4853 
4854 
4855   // FIXME: To correctly count mixed whitespace we need to
4856   // also correctly count mixed whitespace in front of the comment.
4857   //
4858   // EXPECT_EQ("/*\n"
4859   //           "\t      a\t\tcomment\n"
4860   //           "\t      in multiple lines\n"
4861   //           "       */",
4862   //           format("   /*\t \t \n"
4863   //                  " \t \t a\t\tcomment\t \t\n"
4864   //                  " \t \t in multiple lines\t\n"
4865   //                  " \t  */",
4866   //                  Tab));
4867   // Tab.UseTab = false;
4868   // EXPECT_EQ("/*\n"
4869   //           "              a\t\tcomment\n"
4870   //           "              in multiple lines\n"
4871   //           "       */",
4872   //           format("   /*\t \t \n"
4873   //                  " \t \t a\t\tcomment\t \t\n"
4874   //                  " \t \t in multiple lines\t\n"
4875   //                  " \t  */",
4876   //                  Tab));
4877   // EXPECT_EQ("/* some\n"
4878   //           "   comment */",
4879   //          format(" \t \t /* some\n"
4880   //                 " \t \t    comment */",
4881   //                 Tab));
4882 
4883   EXPECT_EQ("{\n"
4884             "  /*\n"
4885             "   * Comment\n"
4886             "   */\n"
4887             "  int i;\n"
4888             "}",
4889             format("{\n"
4890                    "\t/*\n"
4891                    "\t * Comment\n"
4892                    "\t */\n"
4893                    "\t int i;\n"
4894                    "}"));
4895 }
4896 
4897 TEST_F(FormatTest, LinuxBraceBreaking) {
4898   FormatStyle BreakBeforeBrace = getLLVMStyle();
4899   BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
4900   verifyFormat("namespace a\n"
4901                "{\n"
4902                "class A\n"
4903                "{\n"
4904                "  void f()\n"
4905                "  {\n"
4906                "    if (true) {\n"
4907                "      a();\n"
4908                "      b();\n"
4909                "    }\n"
4910                "  }\n"
4911                "  void g()\n"
4912                "  {\n"
4913                "    return;\n"
4914                "  }\n"
4915                "}\n"
4916                "}",
4917                BreakBeforeBrace);
4918 }
4919 
4920 TEST_F(FormatTest, StroustrupBraceBreaking) {
4921   FormatStyle BreakBeforeBrace = getLLVMStyle();
4922   BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4923   verifyFormat("namespace a {\n"
4924                "class A {\n"
4925                "  void f()\n"
4926                "  {\n"
4927                "    if (true) {\n"
4928                "      a();\n"
4929                "      b();\n"
4930                "    }\n"
4931                "  }\n"
4932                "  void g()\n"
4933                "  {\n"
4934                "    return;\n"
4935                "  }\n"
4936                "}\n"
4937                "}",
4938                BreakBeforeBrace);
4939 }
4940 
4941 bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
4942   for (size_t i = 1; i < Styles.size(); ++i)
4943     if (!(Styles[0] == Styles[i]))
4944       return false;
4945   return true;
4946 }
4947 
4948 TEST_F(FormatTest, GetsPredefinedStyleByName) {
4949   FormatStyle Styles[3];
4950 
4951   Styles[0] = getLLVMStyle();
4952   EXPECT_TRUE(getPredefinedStyle("LLVM", &Styles[1]));
4953   EXPECT_TRUE(getPredefinedStyle("lLvM", &Styles[2]));
4954   EXPECT_TRUE(allStylesEqual(Styles));
4955 
4956   Styles[0] = getGoogleStyle();
4957   EXPECT_TRUE(getPredefinedStyle("Google", &Styles[1]));
4958   EXPECT_TRUE(getPredefinedStyle("gOOgle", &Styles[2]));
4959   EXPECT_TRUE(allStylesEqual(Styles));
4960 
4961   Styles[0] = getChromiumStyle();
4962   EXPECT_TRUE(getPredefinedStyle("Chromium", &Styles[1]));
4963   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", &Styles[2]));
4964   EXPECT_TRUE(allStylesEqual(Styles));
4965 
4966   Styles[0] = getMozillaStyle();
4967   EXPECT_TRUE(getPredefinedStyle("Mozilla", &Styles[1]));
4968   EXPECT_TRUE(getPredefinedStyle("moZILla", &Styles[2]));
4969   EXPECT_TRUE(allStylesEqual(Styles));
4970 
4971   EXPECT_FALSE(getPredefinedStyle("qwerty", &Styles[0]));
4972 }
4973 
4974 TEST_F(FormatTest, ParsesConfiguration) {
4975   FormatStyle Style = {};
4976 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
4977   EXPECT_NE(VALUE, Style.FIELD);                                               \
4978   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
4979   EXPECT_EQ(VALUE, Style.FIELD)
4980 
4981 #define CHECK_PARSE_BOOL(FIELD)                                                \
4982   Style.FIELD = false;                                                         \
4983   EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value());           \
4984   EXPECT_TRUE(Style.FIELD);                                                    \
4985   EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value());          \
4986   EXPECT_FALSE(Style.FIELD);
4987 
4988   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
4989   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
4990   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
4991   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
4992   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
4993   CHECK_PARSE_BOOL(BinPackParameters);
4994   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
4995   CHECK_PARSE_BOOL(DerivePointerBinding);
4996   CHECK_PARSE_BOOL(IndentCaseLabels);
4997   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
4998   CHECK_PARSE_BOOL(PointerBindsToType);
4999   CHECK_PARSE_BOOL(SpacesInBracedLists);
5000   CHECK_PARSE_BOOL(UseTab);
5001   CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
5002 
5003   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
5004   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
5005   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
5006   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
5007   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
5008               PenaltyReturnTypeOnItsOwnLine, 1234u);
5009   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
5010               SpacesBeforeTrailingComments, 1234u);
5011   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
5012 
5013   Style.Standard = FormatStyle::LS_Auto;
5014   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
5015   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
5016   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
5017 
5018   Style.ColumnLimit = 123;
5019   FormatStyle BaseStyle = getLLVMStyle();
5020   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
5021   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
5022 
5023   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5024   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
5025               FormatStyle::BS_Attach);
5026   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
5027               FormatStyle::BS_Linux);
5028   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
5029               FormatStyle::BS_Stroustrup);
5030 
5031 #undef CHECK_PARSE
5032 #undef CHECK_PARSE_BOOL
5033 }
5034 
5035 TEST_F(FormatTest, ConfigurationRoundTripTest) {
5036   FormatStyle Style = getLLVMStyle();
5037   std::string YAML = configurationAsText(Style);
5038   FormatStyle ParsedStyle = {};
5039   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
5040   EXPECT_EQ(Style, ParsedStyle);
5041 }
5042 
5043 TEST_F(FormatTest, WorksFor8bitEncodings) {
5044   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
5045             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
5046             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
5047             "\"\xef\xee\xf0\xf3...\"",
5048             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
5049                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
5050                    "\xef\xee\xf0\xf3...\"",
5051                    getLLVMStyleWithColumns(12)));
5052 }
5053 
5054 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
5055 #if !defined(_MSC_VER)
5056 
5057 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
5058   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
5059                getLLVMStyleWithColumns(35));
5060   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
5061                getLLVMStyleWithColumns(21));
5062   verifyFormat("// Однажды в студёную зимнюю пору...",
5063                getLLVMStyleWithColumns(36));
5064   verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
5065                getLLVMStyleWithColumns(22));
5066   verifyFormat("/* Однажды в студёную зимнюю пору... */",
5067                getLLVMStyleWithColumns(39));
5068   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
5069                getLLVMStyleWithColumns(25));
5070 }
5071 
5072 TEST_F(FormatTest, SplitsUTF8Strings) {
5073   EXPECT_EQ(
5074       "\"Однажды, в \"\n"
5075       "\"студёную \"\n"
5076       "\"зимнюю \"\n"
5077       "\"пору,\"",
5078       format("\"Однажды, в студёную зимнюю пору,\"",
5079              getLLVMStyleWithColumns(13)));
5080   EXPECT_EQ("\"一 二 三 四 \"\n"
5081             "\"五 六 七 八 \"\n"
5082             "\"九 十\"",
5083             format("\"一 二 三 四 五 六 七 八 九 十\"",
5084                    getLLVMStyleWithColumns(10)));
5085 }
5086 
5087 TEST_F(FormatTest, SplitsUTF8LineComments) {
5088   EXPECT_EQ("// Я из лесу\n"
5089             "// вышел; был\n"
5090             "// сильный\n"
5091             "// мороз.",
5092             format("// Я из лесу вышел; был сильный мороз.",
5093                    getLLVMStyleWithColumns(13)));
5094   EXPECT_EQ("// 一二三\n"
5095             "// 四五六七\n"
5096             "// 八\n"
5097             "// 九 十",
5098             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(6)));
5099 }
5100 
5101 TEST_F(FormatTest, SplitsUTF8BlockComments) {
5102   EXPECT_EQ("/* Гляжу,\n"
5103             " * поднимается\n"
5104             " * медленно в\n"
5105             " * гору\n"
5106             " * Лошадка,\n"
5107             " * везущая\n"
5108             " * хворосту\n"
5109             " * воз. */",
5110             format("/* Гляжу, поднимается медленно в гору\n"
5111                    " * Лошадка, везущая хворосту воз. */",
5112                    getLLVMStyleWithColumns(13)));
5113   EXPECT_EQ("/* 一二三\n"
5114             " * 四五六七\n"
5115             " * 八\n"
5116             " * 九 十\n"
5117             " */",
5118             format("/* 一二三 四五六七 八  九 十 */", getLLVMStyleWithColumns(6)));
5119   EXPECT_EQ("/* �������� ��������\n"
5120             " * ��������\n"
5121             " * ������-�� */",
5122             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
5123 }
5124 
5125 #endif
5126 
5127 } // end namespace tooling
5128 } // end namespace clang
5129