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