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