FormatTest.cpp revision 8ed9f2b25f082a1643ab5310f9eec33cf31a7577
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, FormatsDerivedClass) {
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
1014TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1015  verifyFormat("class A {\n} a, b;");
1016  verifyFormat("struct A {\n} a, b;");
1017  verifyFormat("union A {\n} a;");
1018}
1019
1020TEST_F(FormatTest, FormatsEnum) {
1021  verifyFormat("enum {\n"
1022               "  Zero,\n"
1023               "  One = 1,\n"
1024               "  Two = One + 1,\n"
1025               "  Three = (One + Two),\n"
1026               "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1027               "  Five = (One, Two, Three, Four, 5)\n"
1028               "};");
1029  verifyFormat("enum Enum {\n"
1030               "};");
1031  verifyFormat("enum {\n"
1032               "};");
1033  verifyFormat("enum X E {\n} d;");
1034  verifyFormat("enum __attribute__((...)) E {\n} d;");
1035  verifyFormat("enum __declspec__((...)) E {\n} d;");
1036  verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
1037}
1038
1039TEST_F(FormatTest, FormatsBitfields) {
1040  verifyFormat("struct Bitfields {\n"
1041               "  unsigned sClass : 8;\n"
1042               "  unsigned ValueKind : 2;\n"
1043               "};");
1044}
1045
1046TEST_F(FormatTest, FormatsNamespaces) {
1047  verifyFormat("namespace some_namespace {\n"
1048               "class A {\n};\n"
1049               "void f() { f(); }\n"
1050               "}");
1051  verifyFormat("namespace {\n"
1052               "class A {\n};\n"
1053               "void f() { f(); }\n"
1054               "}");
1055  verifyFormat("inline namespace X {\n"
1056               "class A {\n};\n"
1057               "void f() { f(); }\n"
1058               "}");
1059  verifyFormat("using namespace some_namespace;\n"
1060               "class A {\n};\n"
1061               "void f() { f(); }");
1062
1063  // This code is more common than we thought; if we
1064  // layout this correctly the semicolon will go into
1065  // its own line, which is undesireable.
1066  verifyFormat("namespace {\n};");
1067  verifyFormat("namespace {\n"
1068               "class A {\n"
1069               "};\n"
1070               "};");
1071}
1072
1073TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1074
1075TEST_F(FormatTest, FormatsInlineASM) {
1076  verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1077  verifyFormat(
1078      "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1079      "    \"cpuid\\n\\t\"\n"
1080      "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1081      "    : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n"
1082      "    : \"a\"(value));");
1083}
1084
1085TEST_F(FormatTest, FormatTryCatch) {
1086  // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll
1087  // also not create single-line-blocks.
1088  verifyFormat("try {\n"
1089               "  throw a * b;\n"
1090               "}\n"
1091               "catch (int a) {\n"
1092               "  // Do nothing.\n"
1093               "}\n"
1094               "catch (...) {\n"
1095               "  exit(42);\n"
1096               "}");
1097
1098  // Function-level try statements.
1099  verifyFormat("int f() try { return 4; }\n"
1100               "catch (...) {\n"
1101               "  return 5;\n"
1102               "}");
1103  verifyFormat("class A {\n"
1104               "  int a;\n"
1105               "  A() try : a(0) {}\n"
1106               "  catch (...) {\n"
1107               "    throw;\n"
1108               "  }\n"
1109               "};\n");
1110}
1111
1112TEST_F(FormatTest, FormatObjCTryCatch) {
1113  verifyFormat("@try {\n"
1114               "  f();\n"
1115               "}\n"
1116               "@catch (NSException e) {\n"
1117               "  @throw;\n"
1118               "}\n"
1119               "@finally {\n"
1120               "  exit(42);\n"
1121               "}");
1122}
1123
1124TEST_F(FormatTest, StaticInitializers) {
1125  verifyFormat("static SomeClass SC = { 1, 'a' };");
1126
1127  // FIXME: Format like enums if the static initializer does not fit on a line.
1128  verifyFormat(
1129      "static SomeClass WithALoooooooooooooooooooongName = {\n"
1130      "  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1131      "};");
1132
1133  verifyFormat(
1134      "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
1135      "                     looooooooooooooooooooooooooooooooooongname,\n"
1136      "                     looooooooooooooooooooooooooooooong };");
1137  // Allow bin-packing in static initializers as this would often lead to
1138  // terrible results, e.g.:
1139  verifyGoogleFormat(
1140      "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
1141      "                     looooooooooooooooooooooooooooooooooongname,\n"
1142      "                     looooooooooooooooooooooooooooooong };");
1143}
1144
1145TEST_F(FormatTest, NestedStaticInitializers) {
1146  verifyFormat("static A x = { { {} } };\n");
1147  verifyFormat("static A x = { { { init1, init2, init3, init4 },\n"
1148               "                 { init1, init2, init3, init4 } } };");
1149
1150  verifyFormat("somes Status::global_reps[3] = {\n"
1151               "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
1152               "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
1153               "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
1154               "};");
1155  verifyGoogleFormat("somes Status::global_reps[3] = {\n"
1156                     "  { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n"
1157                     "  { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n"
1158                     "  { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
1159                     "};");
1160  verifyFormat(
1161      "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
1162      "                   { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
1163      " } };");
1164
1165  verifyFormat(
1166      "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n"
1167      "                            { 111111111111111111111111111111,\n"
1168      "                              222222222222222222222222222222,\n"
1169      "                              333333333333333333333333333333 },\n"
1170      "                            { 1, 2, 3 }, { 1, 2, 3 } } };");
1171  verifyFormat(
1172      "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n"
1173      "                          { { 111111111111111111111111111111,\n"
1174      "                              222222222222222222222222222222,\n"
1175      "                              333333333333333333333333333333 } },\n"
1176      "                          { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
1177
1178  // FIXME: We might at some point want to handle this similar to parameter
1179  // lists, where we have an option to put each on a single line.
1180  verifyFormat(
1181      "struct {\n"
1182      "  unsigned bit;\n"
1183      "  const char *const name;\n"
1184      "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
1185      "                  { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
1186}
1187
1188TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1189  verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1190               "                      \\\n"
1191               "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1192}
1193
1194TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1195  verifyFormat(
1196      "virtual void\n"
1197      "write(ELFWriter *writerrr, OwningPtr<FileOutputBuffer> &buffer) = 0;");
1198}
1199
1200TEST_F(FormatTest, LayoutUnknownPPDirective) {
1201  EXPECT_EQ("#123 \"A string literal\"",
1202            format("   #     123    \"A string literal\""));
1203  EXPECT_EQ("#;", format("#;"));
1204  verifyFormat("#\n;\n;\n;");
1205}
1206
1207TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1208  EXPECT_EQ("#line 42 \"test\"\n",
1209            format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1210  EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1211                                    getLLVMStyleWithColumns(12)));
1212}
1213
1214TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1215  EXPECT_EQ("#line 42 \"test\"",
1216            format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1217  EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1218}
1219
1220TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1221  verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1222  verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1223  verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1224  // FIXME: We never break before the macro name.
1225  verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
1226
1227  verifyFormat("#define A A\n#define A A");
1228  verifyFormat("#define A(X) A\n#define A A");
1229
1230  verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1231  verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1232}
1233
1234TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1235  EXPECT_EQ("// somecomment\n"
1236            "#include \"a.h\"\n"
1237            "#define A(  \\\n"
1238            "    A, B)\n"
1239            "#include \"b.h\"\n"
1240            "// somecomment\n",
1241            format("  // somecomment\n"
1242                   "  #include \"a.h\"\n"
1243                   "#define A(A,\\\n"
1244                   "    B)\n"
1245                   "    #include \"b.h\"\n"
1246                   " // somecomment\n",
1247                   getLLVMStyleWithColumns(13)));
1248}
1249
1250TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1251
1252TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1253  EXPECT_EQ("#define A    \\\n"
1254            "  c;         \\\n"
1255            "  e;\n"
1256            "f;",
1257            format("#define A c; e;\n"
1258                   "f;",
1259                   getLLVMStyleWithColumns(14)));
1260}
1261
1262TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1263
1264TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
1265  EXPECT_EQ("# define A\\\n  b;",
1266            format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
1267}
1268
1269TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1270  EXPECT_EQ("int x,\n"
1271            "#define A\n"
1272            "    y;",
1273            format("int x,\n#define A\ny;"));
1274}
1275
1276TEST_F(FormatTest, HashInMacroDefinition) {
1277  verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1278  verifyFormat("#define A \\\n"
1279               "  {       \\\n"
1280               "    f(#c);\\\n"
1281               "  }",
1282               getLLVMStyleWithColumns(11));
1283
1284  verifyFormat("#define A(X)         \\\n"
1285               "  void function##X()",
1286               getLLVMStyleWithColumns(22));
1287
1288  verifyFormat("#define A(a, b, c)   \\\n"
1289               "  void a##b##c()",
1290               getLLVMStyleWithColumns(22));
1291
1292  verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1293}
1294
1295TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1296  verifyFormat("#define A (1)");
1297}
1298
1299TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1300  EXPECT_EQ("#define A b;", format("#define A \\\n"
1301                                   "          \\\n"
1302                                   "  b;",
1303                                   getLLVMStyleWithColumns(25)));
1304  EXPECT_EQ("#define A \\\n"
1305            "          \\\n"
1306            "  a;      \\\n"
1307            "  b;",
1308            format("#define A \\\n"
1309                   "          \\\n"
1310                   "  a;      \\\n"
1311                   "  b;",
1312                   getLLVMStyleWithColumns(11)));
1313  EXPECT_EQ("#define A \\\n"
1314            "  a;      \\\n"
1315            "          \\\n"
1316            "  b;",
1317            format("#define A \\\n"
1318                   "  a;      \\\n"
1319                   "          \\\n"
1320                   "  b;",
1321                   getLLVMStyleWithColumns(11)));
1322}
1323
1324TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1325  verifyFormat("#define A :");
1326
1327  // FIXME: Improve formatting of case labels in macros.
1328  verifyFormat("#define SOMECASES  \\\n"
1329               "  case 1:          \\\n"
1330               "  case 2\n",
1331               getLLVMStyleWithColumns(20));
1332
1333  verifyFormat("#define A template <typename T>");
1334  verifyFormat("#define STR(x) #x\n"
1335               "f(STR(this_is_a_string_literal{));");
1336}
1337
1338TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
1339  EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
1340}
1341
1342TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
1343  verifyFormat("{\n  { a #c; }\n}");
1344}
1345
1346TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
1347  EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
1348            format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
1349  EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
1350            format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
1351}
1352
1353TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
1354  EXPECT_EQ(
1355      "#define A \\\n  int i;  \\\n  int j;",
1356      format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
1357}
1358
1359TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
1360  verifyFormat("#define A \\\n"
1361               "  int v(  \\\n"
1362               "      a); \\\n"
1363               "  int i;",
1364               getLLVMStyleWithColumns(11));
1365}
1366
1367TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
1368  EXPECT_EQ(
1369      "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1370      "                      \\\n"
1371      "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
1372      "\n"
1373      "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
1374      "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
1375      format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
1376             "\\\n"
1377             "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
1378             "  \n"
1379             "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
1380             "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
1381}
1382
1383TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
1384  EXPECT_EQ("int\n"
1385            "#define A\n"
1386            "a;",
1387            format("int\n#define A\na;"));
1388  verifyFormat("functionCallTo(\n"
1389               "    someOtherFunction(\n"
1390               "        withSomeParameters, whichInSequence,\n"
1391               "        areLongerThanALine(andAnotherCall,\n"
1392               "#define A B\n"
1393               "                           withMoreParamters,\n"
1394               "                           whichStronglyInfluenceTheLayout),\n"
1395               "        andMoreParameters),\n"
1396               "    trailing);",
1397               getLLVMStyleWithColumns(69));
1398}
1399
1400TEST_F(FormatTest, LayoutBlockInsideParens) {
1401  EXPECT_EQ("functionCall({\n"
1402            "  int i;\n"
1403            "});",
1404            format(" functionCall ( {int i;} );"));
1405}
1406
1407TEST_F(FormatTest, LayoutBlockInsideStatement) {
1408  EXPECT_EQ("SOME_MACRO { int i; }\n"
1409            "int i;",
1410            format("  SOME_MACRO  {int i;}  int i;"));
1411}
1412
1413TEST_F(FormatTest, LayoutNestedBlocks) {
1414  verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
1415               "  struct s {\n"
1416               "    int i;\n"
1417               "  };\n"
1418               "  s kBitsToOs[] = { { 10 } };\n"
1419               "  for (int i = 0; i < 10; ++i)\n"
1420               "    return;\n"
1421               "}");
1422}
1423
1424TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
1425  EXPECT_EQ("{}", format("{}"));
1426
1427  // Negative test for enum.
1428  verifyFormat("enum E {\n};");
1429
1430  // Note that when there's a missing ';', we still join...
1431  verifyFormat("enum E {}");
1432}
1433
1434//===----------------------------------------------------------------------===//
1435// Line break tests.
1436//===----------------------------------------------------------------------===//
1437
1438TEST_F(FormatTest, FormatsFunctionDefinition) {
1439  verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
1440               " int h, int j, int f,\n"
1441               "       int c, int ddddddddddddd) {}");
1442}
1443
1444TEST_F(FormatTest, FormatsAwesomeMethodCall) {
1445  verifyFormat(
1446      "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
1447      "                       parameter, parameter, parameter)),\n"
1448      "                   SecondLongCall(parameter));");
1449}
1450
1451TEST_F(FormatTest, PreventConfusingIndents) {
1452  verifyFormat(
1453      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1454      "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
1455      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1456      "    aaaaaaaaaaaaaaaaaaaaaaaa);");
1457  verifyFormat(
1458      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n"
1459      "    aaaaaaaaaaaaaaaaaaaaaaaa[\n"
1460      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n"
1461      "    aaaaaaaaaaaaaaaaaaaaaaaa];");
1462  verifyFormat(
1463      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
1464      "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
1465      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
1466      "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
1467  verifyFormat("int a = bbbb && ccc && fffff(\n"
1468               "#define A Just forcing a new line\n"
1469               "                           ddd);");
1470}
1471
1472TEST_F(FormatTest, ConstructorInitializers) {
1473  verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
1474  verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
1475               getLLVMStyleWithColumns(45));
1476  verifyFormat("Constructor()\n"
1477               "    : Inttializer(FitsOnTheLine) {}",
1478               getLLVMStyleWithColumns(44));
1479  verifyFormat("Constructor()\n"
1480               "    : Inttializer(FitsOnTheLine) {}",
1481               getLLVMStyleWithColumns(43));
1482
1483  verifyFormat(
1484      "SomeClass::Constructor()\n"
1485      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
1486
1487  verifyFormat(
1488      "SomeClass::Constructor()\n"
1489      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1490      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
1491  verifyFormat(
1492      "SomeClass::Constructor()\n"
1493      "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1494      "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
1495
1496  verifyFormat("Constructor()\n"
1497               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1498               "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1499               "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1500               "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
1501
1502  verifyFormat("Constructor()\n"
1503               "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1504               "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
1505
1506  verifyFormat("Constructor(int Parameter = 0)\n"
1507               "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
1508               "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
1509
1510  // Here a line could be saved by splitting the second initializer onto two
1511  // lines, but that is not desireable.
1512  verifyFormat("Constructor()\n"
1513               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
1514               "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
1515               "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
1516
1517  FormatStyle OnePerLine = getLLVMStyle();
1518  OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
1519  verifyFormat("SomeClass::Constructor()\n"
1520               "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1521               "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1522               "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
1523               OnePerLine);
1524  verifyFormat("SomeClass::Constructor()\n"
1525               "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
1526               "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
1527               "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
1528               OnePerLine);
1529  verifyFormat("MyClass::MyClass(int var)\n"
1530               "    : some_var_(var),            // 4 space indent\n"
1531               "      some_other_var_(var + 1) { // lined up\n"
1532               "}",
1533               OnePerLine);
1534  verifyFormat("Constructor()\n"
1535               "    : aaaaa(aaaaaa),\n"
1536               "      aaaaa(aaaaaa),\n"
1537               "      aaaaa(aaaaaa),\n"
1538               "      aaaaa(aaaaaa),\n"
1539               "      aaaaa(aaaaaa) {}",
1540               OnePerLine);
1541
1542  // This test takes VERY long when memoization is broken.
1543  OnePerLine.BinPackParameters = false;
1544  std::string input = "Constructor()\n"
1545                      "    : aaaa(a,\n";
1546  for (unsigned i = 0, e = 80; i != e; ++i) {
1547    input += "           a,\n";
1548  }
1549  input += "           a) {}";
1550  verifyFormat(input, OnePerLine);
1551}
1552
1553TEST_F(FormatTest, BreaksAsHighAsPossible) {
1554  verifyFormat(
1555      "void f() {\n"
1556      "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
1557      "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
1558      "    f();\n"
1559      "}");
1560  verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
1561               "        Intervals[i - 1].getRange().getLast()) {\n}");
1562}
1563
1564TEST_F(FormatTest, BreaksDesireably) {
1565  verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
1566               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
1567               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
1568  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1569               "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1570               "}");
1571
1572  verifyFormat(
1573      "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1574      "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
1575
1576  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1577               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1578               "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1579
1580  verifyFormat(
1581      "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1582      "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1583      "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1584      "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
1585
1586  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1587               "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1588
1589  verifyFormat(
1590      "void f() {\n"
1591      "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1592      "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
1593      "}");
1594  verifyFormat(
1595      "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1596      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1597  verifyFormat(
1598      "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1599      "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
1600  verifyFormat(
1601      "aaaaaaaaaaaaaaaaa(\n"
1602      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1603      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1604
1605  // This test case breaks on an incorrect memoization, i.e. an optimization not
1606  // taking into account the StopAt value.
1607  verifyFormat(
1608      "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1609      "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1610      "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
1611      "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1612
1613  verifyFormat("{\n  {\n    {\n"
1614               "      Annotation.SpaceRequiredBefore =\n"
1615               "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
1616               "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
1617               "    }\n  }\n}");
1618}
1619
1620TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
1621  FormatStyle NoBinPacking = getGoogleStyle();
1622  NoBinPacking.BinPackParameters = false;
1623  verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
1624               "  aaaaaaaaaaaaaaaaaaaa,\n"
1625               "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
1626               NoBinPacking);
1627  verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
1628               "        aaaaaaaaaaaaa,\n"
1629               "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
1630               NoBinPacking);
1631  verifyFormat(
1632      "aaaaaaaa(aaaaaaaaaaaaa,\n"
1633      "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1634      "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
1635      "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1636      "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
1637      NoBinPacking);
1638  verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
1639               "    .aaaaaaaaaaaaaaaaaa();",
1640               NoBinPacking);
1641  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1642               "    aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);",
1643               NoBinPacking);
1644
1645  verifyFormat(
1646      "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1647      "             aaaaaaaaaaaa,\n"
1648      "             aaaaaaaaaaaa);",
1649      NoBinPacking);
1650  verifyFormat(
1651      "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
1652      "                               ddddddddddddddddddddddddddddd),\n"
1653      "             test);",
1654      NoBinPacking);
1655
1656  verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
1657               "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
1658               "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
1659               NoBinPacking);
1660  verifyFormat("a(\"a\"\n"
1661               "  \"a\",\n"
1662               "  a);");
1663
1664  NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
1665  verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
1666               "                aaaaaaaaa,\n"
1667               "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
1668               NoBinPacking);
1669  verifyFormat(
1670      "void f() {\n"
1671      "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
1672      "      .aaaaaaa();\n"
1673      "}",
1674      NoBinPacking);
1675}
1676
1677TEST_F(FormatTest, FormatsBuilderPattern) {
1678  verifyFormat(
1679      "return llvm::StringSwitch<Reference::Kind>(name)\n"
1680      "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
1681      "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n"
1682      "    .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n"
1683      "    .Default(ORDER_TEXT);\n");
1684
1685  verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
1686               "           aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
1687  verifyFormat(
1688      "aaaaaaa->aaaaaaa\n"
1689      "    ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1690      "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
1691  verifyFormat(
1692      "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
1693      "    aaaaaaaaaaaaaa);");
1694  verifyFormat(
1695      "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
1696      "    ->aaaaaaaaaaaaaaaa(\n"
1697      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1698      "    ->aaaaaaaaaaaaaaaaa();");
1699}
1700
1701TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) {
1702  verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1703               "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
1704  verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1705               "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
1706  verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
1707               "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
1708  verifyFormat(
1709      "void aaaaaaaaaaaaaaaaaa()\n"
1710      "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
1711      "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
1712  verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1713               "    __attribute__((unused));");
1714
1715  // FIXME: This is bad indentation, but generally hard to distinguish from a
1716  // function declaration.
1717  verifyFormat(
1718      "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1719      "GUARDED_BY(aaaaaaaaaaaa);");
1720}
1721
1722TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
1723  verifyFormat(
1724      "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1725      "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
1726  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
1727               "    ccccccccccccccccccccccccc) {\n}");
1728  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
1729               "    ccccccccccccccccccccccccc) {\n}");
1730  verifyFormat(
1731      "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
1732      "    ccccccccccccccccccccccccc) {\n}");
1733  verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
1734               "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
1735               "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
1736               "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
1737  verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
1738               "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
1739               "    aaaaaaaaaaaaaaa != aa) {\n}");
1740}
1741
1742TEST_F(FormatTest, BreaksAfterAssignments) {
1743  verifyFormat(
1744      "unsigned Cost =\n"
1745      "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
1746      "                        SI->getPointerAddressSpaceee());\n");
1747  verifyFormat(
1748      "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
1749      "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
1750
1751  verifyFormat(
1752      "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n"
1753      "    .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
1754}
1755
1756TEST_F(FormatTest, AlignsAfterAssignments) {
1757  verifyFormat(
1758      "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1759      "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
1760  verifyFormat(
1761      "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1762      "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
1763  verifyFormat(
1764      "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1765      "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
1766  verifyFormat(
1767      "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1768      "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
1769  verifyFormat("double LooooooooooooooooooooooooongResult =\n"
1770               "    aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n"
1771               "    aaaaaaaaaaaaaaaaaaaaaaaa;");
1772}
1773
1774TEST_F(FormatTest, AlignsAfterReturn) {
1775  verifyFormat(
1776      "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1777      "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
1778  verifyFormat(
1779      "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
1780      "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
1781  verifyFormat(
1782      "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
1783      "           aaaaaaaaaaaaaaaaaaaaaa();");
1784  verifyFormat(
1785      "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
1786      "            aaaaaaaaaaaaaaaaaaaaaa());");
1787}
1788
1789TEST_F(FormatTest, BreaksConditionalExpressions) {
1790  verifyFormat(
1791      "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
1792      "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1793      "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1794  verifyFormat(
1795      "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1796      "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1797  verifyFormat(
1798      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
1799      "                                                    : aaaaaaaaaaaaa);");
1800  verifyFormat(
1801      "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1802      "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1803      "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1804      "                   aaaaaaaaaaaaa);");
1805  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1806               "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1807               "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1808               "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1809               "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1810  verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1811               "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1812               "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1813               "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1814               "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1815               "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
1816               "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1817
1818  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1819               "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1820               "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1821  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
1822               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1823               "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1824               "        : aaaaaaaaaaaaaaaa;");
1825  verifyFormat(
1826      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1827      "    ? aaaaaaaaaaaaaaa\n"
1828      "    : aaaaaaaaaaaaaaa;");
1829  verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
1830               "  aaaaaaaaa\n"
1831               "      ? b\n"
1832               "      : c);");
1833  verifyFormat(
1834      "unsigned Indent =\n"
1835      "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
1836      "                              ? IndentForLevel[TheLine.Level]\n"
1837      "                              : TheLine * 2,\n"
1838      "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
1839      getLLVMStyleWithColumns(70));
1840
1841  FormatStyle NoBinPacking = getLLVMStyle();
1842  NoBinPacking.BinPackParameters = false;
1843  verifyFormat(
1844      "void f() {\n"
1845      "  g(aaa,\n"
1846      "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
1847      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1848      "        ? aaaaaaaaaaaaaaa\n"
1849      "        : aaaaaaaaaaaaaaa);\n"
1850      "}",
1851      NoBinPacking);
1852}
1853
1854TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
1855  verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
1856               "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
1857  verifyFormat("bool a = true, b = false;");
1858
1859  verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1860               "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
1861               "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
1862               "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
1863  verifyFormat(
1864      "bool aaaaaaaaaaaaaaaaaaaaa =\n"
1865      "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
1866      "     d = e && f;");
1867
1868}
1869
1870TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
1871  verifyFormat("arr[foo ? bar : baz];");
1872  verifyFormat("f()[foo ? bar : baz];");
1873  verifyFormat("(a + b)[foo ? bar : baz];");
1874  verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
1875}
1876
1877TEST_F(FormatTest, AlignsStringLiterals) {
1878  verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
1879               "                                      \"short literal\");");
1880  verifyFormat(
1881      "looooooooooooooooooooooooongFunction(\n"
1882      "    \"short literal\"\n"
1883      "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
1884  verifyFormat("someFunction(\"Always break between multi-line\"\n"
1885               "             \" string literals\",\n"
1886               "             and, other, parameters);");
1887  EXPECT_EQ("fun + \"1243\" /* comment */\n"
1888            "      \"5678\";",
1889            format("fun + \"1243\" /* comment */\n"
1890                   "      \"5678\";",
1891                   getLLVMStyleWithColumns(28)));
1892  EXPECT_EQ(
1893      "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
1894      "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
1895      "         \"aaaaaaaaaaaaaaaa\";",
1896      format("aaaaaa ="
1897             "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
1898             "aaaaaaaaaaaaaaaaaaaaa\" "
1899             "\"aaaaaaaaaaaaaaaa\";"));
1900  verifyFormat("a = a + \"a\"\n"
1901               "        \"a\"\n"
1902               "        \"a\";");
1903
1904  verifyFormat(
1905      "#define LL_FORMAT \"ll\"\n"
1906      "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
1907      "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
1908}
1909
1910TEST_F(FormatTest, AlignsPipes) {
1911  verifyFormat(
1912      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1913      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1914      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1915  verifyFormat(
1916      "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
1917      "                     << aaaaaaaaaaaaaaaaaaaa;");
1918  verifyFormat(
1919      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1920      "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1921  verifyFormat(
1922      "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1923      "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
1924      "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
1925  verifyFormat(
1926      "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1927      "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1928      "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1929
1930  verifyFormat("return out << \"somepacket = {\\n\"\n"
1931               "           << \"  aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
1932               "           << \"  bbbb = \" << pkt.bbbb << \"\\n\"\n"
1933               "           << \"  cccccc = \" << pkt.cccccc << \"\\n\"\n"
1934               "           << \"  ddd = [\" << pkt.ddd << \"]\\n\"\n"
1935               "           << \"}\";");
1936
1937  verifyFormat(
1938      "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
1939      "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
1940      "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
1941      "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
1942      "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
1943  verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
1944               "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
1945
1946  verifyFormat(
1947      "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1948      "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
1949}
1950
1951TEST_F(FormatTest, UnderstandsEquals) {
1952  verifyFormat(
1953      "aaaaaaaaaaaaaaaaa =\n"
1954      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1955  verifyFormat(
1956      "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1957      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1958  verifyFormat(
1959      "if (a) {\n"
1960      "  f();\n"
1961      "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1962      "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1963      "}");
1964
1965  verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
1966               "        100000000 + 10000000) {\n}");
1967}
1968
1969TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
1970  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1971               "    .looooooooooooooooooooooooooooooooooooooongFunction();");
1972
1973  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
1974               "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
1975
1976  verifyFormat(
1977      "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
1978      "                                                          Parameter2);");
1979
1980  verifyFormat(
1981      "ShortObject->shortFunction(\n"
1982      "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
1983      "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
1984
1985  verifyFormat("loooooooooooooongFunction(\n"
1986               "    LoooooooooooooongObject->looooooooooooooooongFunction());");
1987
1988  verifyFormat(
1989      "function(LoooooooooooooooooooooooooooooooooooongObject\n"
1990      "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
1991
1992  verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
1993               "    .WillRepeatedly(Return(SomeValue));");
1994  verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n"
1995               "    .insert(ccccccccccccccccccccccc);");
1996  verifyFormat(
1997      "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1998      "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
1999      "    .aaaaaaaaaaaaaaa(\n"
2000      "        aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2001      "           aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
2002  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2003               "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2004               "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2005               "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
2006               "}");
2007
2008  // Here, it is not necessary to wrap at "." or "->".
2009  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
2010               "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2011  verifyFormat(
2012      "aaaaaaaaaaa->aaaaaaaaa(\n"
2013      "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2014      "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
2015
2016  verifyFormat(
2017      "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2018      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
2019  verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
2020               "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
2021  verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
2022               "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
2023
2024  // FIXME: Should we break before .a()?
2025  verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2026               "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
2027
2028  FormatStyle NoBinPacking = getLLVMStyle();
2029  NoBinPacking.BinPackParameters = false;
2030  verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
2031               "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
2032               "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
2033               "                         aaaaaaaaaaaaaaaaaaa,\n"
2034               "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2035               NoBinPacking);
2036}
2037
2038TEST_F(FormatTest, WrapsTemplateDeclarations) {
2039  verifyFormat("template <typename T>\n"
2040               "virtual void loooooooooooongFunction(int Param1, int Param2);");
2041  verifyFormat(
2042      "template <typename T>\n"
2043      "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
2044  verifyFormat("template <typename T>\n"
2045               "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
2046               "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
2047  verifyFormat(
2048      "template <typename T>\n"
2049      "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
2050      "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
2051  verifyFormat(
2052      "template <typename T>\n"
2053      "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
2054      "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
2055      "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2056  verifyFormat("template <typename T>\n"
2057               "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2058               "    int aaaaaaaaaaaaaaaaaaaaaa);");
2059  verifyFormat(
2060      "template <typename T1, typename T2 = char, typename T3 = char,\n"
2061      "          typename T4 = char>\n"
2062      "void f();");
2063  verifyFormat(
2064      "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
2065      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2066
2067  verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
2068               "    a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));");
2069}
2070
2071TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
2072  verifyFormat(
2073      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2074      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2075  verifyFormat(
2076      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2077      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2078      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
2079
2080  // FIXME: Should we have an extra indent after the second break?
2081  verifyFormat(
2082      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2083      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2084      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2085
2086  // FIXME: Look into whether we should indent 4 from the start or 4 from
2087  // "bbbbb..." here instead of what we are doing now.
2088  verifyFormat(
2089      "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
2090      "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
2091
2092  // Breaking at nested name specifiers is generally not desirable.
2093  verifyFormat(
2094      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2095      "    aaaaaaaaaaaaaaaaaaaaaaa);");
2096
2097  verifyFormat(
2098      "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2099      "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2100      "                   aaaaaaaaaaaaaaaaaaaaa);",
2101      getLLVMStyleWithColumns(74));
2102
2103  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
2104               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2105               "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
2106}
2107
2108TEST_F(FormatTest, UnderstandsTemplateParameters) {
2109  verifyFormat("A<int> a;");
2110  verifyFormat("A<A<A<int> > > a;");
2111  verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
2112  verifyFormat("bool x = a < 1 || 2 > a;");
2113  verifyFormat("bool x = 5 < f<int>();");
2114  verifyFormat("bool x = f<int>() > 5;");
2115  verifyFormat("bool x = 5 < a<int>::x;");
2116  verifyFormat("bool x = a < 4 ? a > 2 : false;");
2117  verifyFormat("bool x = f() ? a < 2 : a > 2;");
2118
2119  verifyGoogleFormat("A<A<int>> a;");
2120  verifyGoogleFormat("A<A<A<int>>> a;");
2121  verifyGoogleFormat("A<A<A<A<int>>>> a;");
2122  verifyGoogleFormat("A<A<int> > a;");
2123  verifyGoogleFormat("A<A<A<int> > > a;");
2124  verifyGoogleFormat("A<A<A<A<int> > > > a;");
2125  EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
2126  EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
2127
2128  verifyFormat("test >> a >> b;");
2129  verifyFormat("test << a >> b;");
2130
2131  verifyFormat("f<int>();");
2132  verifyFormat("template <typename T> void f() {}");
2133}
2134
2135TEST_F(FormatTest, UnderstandsBinaryOperators) {
2136  verifyFormat("COMPARE(a, ==, b);");
2137}
2138
2139TEST_F(FormatTest, UnderstandsPointersToMembers) {
2140  verifyFormat("int A::*x;");
2141  // FIXME: Recognize pointers to member functions.
2142  //verifyFormat("int (S::*func)(void *);");
2143  verifyFormat("int(S::*func)(void *);");
2144  verifyFormat("(a->*f)();");
2145  verifyFormat("a->*x;");
2146  verifyFormat("(a.*f)();");
2147  verifyFormat("((*a).*f)();");
2148  verifyFormat("a.*x;");
2149}
2150
2151TEST_F(FormatTest, UnderstandsUnaryOperators) {
2152  verifyFormat("int a = -2;");
2153  verifyFormat("f(-1, -2, -3);");
2154  verifyFormat("a[-1] = 5;");
2155  verifyFormat("int a = 5 + -2;");
2156  verifyFormat("if (i == -1) {\n}");
2157  verifyFormat("if (i != -1) {\n}");
2158  verifyFormat("if (i > -1) {\n}");
2159  verifyFormat("if (i < -1) {\n}");
2160  verifyFormat("++(a->f());");
2161  verifyFormat("--(a->f());");
2162  verifyFormat("(a->f())++;");
2163  verifyFormat("a[42]++;");
2164  verifyFormat("if (!(a->f())) {\n}");
2165
2166  verifyFormat("a-- > b;");
2167  verifyFormat("b ? -a : c;");
2168  verifyFormat("n * sizeof char16;");
2169  verifyFormat("n * alignof char16;");
2170  verifyFormat("sizeof(char);");
2171  verifyFormat("alignof(char);");
2172
2173  verifyFormat("return -1;");
2174  verifyFormat("switch (a) {\n"
2175               "case -1:\n"
2176               "  break;\n"
2177               "}");
2178  verifyFormat("#define X -1");
2179  verifyFormat("#define X -kConstant");
2180
2181  verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };");
2182  verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };");
2183
2184  verifyFormat("int a = /* confusing comment */ -1;");
2185  // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
2186  verifyFormat("int a = i /* confusing comment */++;");
2187}
2188
2189TEST_F(FormatTest, UndestandsOverloadedOperators) {
2190  verifyFormat("bool operator<();");
2191  verifyFormat("bool operator>();");
2192  verifyFormat("bool operator=();");
2193  verifyFormat("bool operator==();");
2194  verifyFormat("bool operator!=();");
2195  verifyFormat("int operator+();");
2196  verifyFormat("int operator++();");
2197  verifyFormat("bool operator();");
2198  verifyFormat("bool operator()();");
2199  verifyFormat("bool operator[]();");
2200  verifyFormat("operator bool();");
2201  verifyFormat("operator int();");
2202  verifyFormat("operator void *();");
2203  verifyFormat("operator SomeType<int>();");
2204  verifyFormat("operator SomeType<int, int>();");
2205  verifyFormat("operator SomeType<SomeType<int> >();");
2206  verifyFormat("void *operator new(std::size_t size);");
2207  verifyFormat("void *operator new[](std::size_t size);");
2208  verifyFormat("void operator delete(void *ptr);");
2209  verifyFormat("void operator delete[](void *ptr);");
2210
2211  verifyFormat(
2212      "ostream &operator<<(ostream &OutputStream,\n"
2213      "                    SomeReallyLongType WithSomeReallyLongValue);");
2214
2215  verifyGoogleFormat("operator void*();");
2216  verifyGoogleFormat("operator SomeType<SomeType<int>>();");
2217}
2218
2219TEST_F(FormatTest, UnderstandsNewAndDelete) {
2220  verifyFormat("void f() {\n"
2221               "  A *a = new A;\n"
2222               "  A *a = new (placement) A;\n"
2223               "  delete a;\n"
2224               "  delete (A *)a;\n"
2225               "}");
2226}
2227
2228TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
2229  verifyFormat("int *f(int *a) {}");
2230  verifyFormat("int main(int argc, char **argv) {}");
2231  verifyFormat("Test::Test(int b) : a(b * b) {}");
2232  verifyIndependentOfContext("f(a, *a);");
2233  verifyFormat("void g() { f(*a); }");
2234  verifyIndependentOfContext("int a = b * 10;");
2235  verifyIndependentOfContext("int a = 10 * b;");
2236  verifyIndependentOfContext("int a = b * c;");
2237  verifyIndependentOfContext("int a += b * c;");
2238  verifyIndependentOfContext("int a -= b * c;");
2239  verifyIndependentOfContext("int a *= b * c;");
2240  verifyIndependentOfContext("int a /= b * c;");
2241  verifyIndependentOfContext("int a = *b;");
2242  verifyIndependentOfContext("int a = *b * c;");
2243  verifyIndependentOfContext("int a = b * *c;");
2244  verifyIndependentOfContext("return 10 * b;");
2245  verifyIndependentOfContext("return *b * *c;");
2246  verifyIndependentOfContext("return a & ~b;");
2247  verifyIndependentOfContext("f(b ? *c : *d);");
2248  verifyIndependentOfContext("int a = b ? *c : *d;");
2249  verifyIndependentOfContext("*b = a;");
2250  verifyIndependentOfContext("a * ~b;");
2251  verifyIndependentOfContext("a * !b;");
2252  verifyIndependentOfContext("a * +b;");
2253  verifyIndependentOfContext("a * -b;");
2254  verifyIndependentOfContext("a * ++b;");
2255  verifyIndependentOfContext("a * --b;");
2256  verifyIndependentOfContext("a[4] * b;");
2257  verifyIndependentOfContext("a[a * a] = 1;");
2258  verifyIndependentOfContext("f() * b;");
2259  verifyIndependentOfContext("a * [self dostuff];");
2260  verifyIndependentOfContext("int x = a * (a + b);");
2261  verifyIndependentOfContext("(a *)(a + b);");
2262  verifyIndependentOfContext("int *pa = (int *)&a;");
2263  verifyIndependentOfContext("return sizeof(int **);");
2264  verifyIndependentOfContext("return sizeof(int ******);");
2265  verifyIndependentOfContext("return (int **&)a;");
2266  verifyFormat("void f(Type (*parameter)[10]) {}");
2267  verifyGoogleFormat("return sizeof(int**);");
2268  verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
2269  verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
2270  // FIXME: The newline is wrong.
2271  verifyFormat("auto a = [](int **&, int ***) {}\n;");
2272
2273  verifyIndependentOfContext("InvalidRegions[*R] = 0;");
2274
2275  verifyIndependentOfContext("A<int *> a;");
2276  verifyIndependentOfContext("A<int **> a;");
2277  verifyIndependentOfContext("A<int *, int *> a;");
2278  verifyIndependentOfContext(
2279      "const char *const p = reinterpret_cast<const char *const>(q);");
2280  verifyIndependentOfContext("A<int **, int **> a;");
2281  verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
2282  verifyFormat("for (char **a = b; *a; ++a) {\n}");
2283
2284  verifyFormat(
2285      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2286      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
2287
2288  verifyGoogleFormat("int main(int argc, char** argv) {}");
2289  verifyGoogleFormat("A<int*> a;");
2290  verifyGoogleFormat("A<int**> a;");
2291  verifyGoogleFormat("A<int*, int*> a;");
2292  verifyGoogleFormat("A<int**, int**> a;");
2293  verifyGoogleFormat("f(b ? *c : *d);");
2294  verifyGoogleFormat("int a = b ? *c : *d;");
2295  verifyGoogleFormat("Type* t = **x;");
2296  verifyGoogleFormat("Type* t = *++*x;");
2297  verifyGoogleFormat("*++*x;");
2298  verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
2299  verifyGoogleFormat("Type* t = x++ * y;");
2300  verifyGoogleFormat(
2301      "const char* const p = reinterpret_cast<const char* const>(q);");
2302
2303  verifyIndependentOfContext("a = *(x + y);");
2304  verifyIndependentOfContext("a = &(x + y);");
2305  verifyIndependentOfContext("*(x + y).call();");
2306  verifyIndependentOfContext("&(x + y)->call();");
2307  verifyFormat("void f() { &(*I).first; }");
2308
2309  verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
2310  verifyFormat(
2311      "int *MyValues = {\n"
2312      "  *A, // Operator detection might be confused by the '{'\n"
2313      "  *BB // Operator detection might be confused by previous comment\n"
2314      "};");
2315
2316  verifyIndependentOfContext("if (int *a = &b)");
2317  verifyIndependentOfContext("if (int &a = *b)");
2318  verifyIndependentOfContext("if (a & b[i])");
2319  verifyIndependentOfContext("if (a::b::c::d & b[i])");
2320  verifyIndependentOfContext("if (*b[i])");
2321  verifyIndependentOfContext("if (int *a = (&b))");
2322  verifyIndependentOfContext("while (int *a = &b)");
2323  verifyFormat("void f() {\n"
2324               "  for (const int &v : Values) {\n"
2325               "  }\n"
2326               "}");
2327  verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
2328  verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
2329
2330  verifyIndependentOfContext("A = new SomeType *[Length];");
2331  verifyIndependentOfContext("A = new SomeType *[Length]();");
2332  verifyGoogleFormat("A = new SomeType* [Length]();");
2333  verifyGoogleFormat("A = new SomeType* [Length];");
2334}
2335
2336TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
2337  EXPECT_EQ("int *a;\n"
2338            "int *a;\n"
2339            "int *a;",
2340            format("int *a;\n"
2341                   "int* a;\n"
2342                   "int *a;",
2343                   getGoogleStyle()));
2344  EXPECT_EQ("int* a;\n"
2345            "int* a;\n"
2346            "int* a;",
2347            format("int* a;\n"
2348                   "int* a;\n"
2349                   "int *a;",
2350                   getGoogleStyle()));
2351  EXPECT_EQ("int *a;\n"
2352            "int *a;\n"
2353            "int *a;",
2354            format("int *a;\n"
2355                   "int * a;\n"
2356                   "int *  a;",
2357                   getGoogleStyle()));
2358}
2359
2360TEST_F(FormatTest, UnderstandsRvalueReferences) {
2361  verifyFormat("int f(int &&a) {}");
2362  verifyFormat("int f(int a, char &&b) {}");
2363  verifyFormat("void f() { int &&a = b; }");
2364  verifyGoogleFormat("int f(int a, char&& b) {}");
2365  verifyGoogleFormat("void f() { int&& a = b; }");
2366
2367  // FIXME: These require somewhat deeper changes in template arguments
2368  // formatting.
2369  //  verifyIndependentOfContext("A<int &&> a;");
2370  //  verifyIndependentOfContext("A<int &&, int &&> a;");
2371  //  verifyGoogleFormat("A<int&&> a;");
2372  //  verifyGoogleFormat("A<int&&, int&&> a;");
2373}
2374
2375TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
2376  verifyFormat("void f() {\n"
2377               "  x[aaaaaaaaa -\n"
2378               "      b] = 23;\n"
2379               "}",
2380               getLLVMStyleWithColumns(15));
2381}
2382
2383TEST_F(FormatTest, FormatsCasts) {
2384  verifyFormat("Type *A = static_cast<Type *>(P);");
2385  verifyFormat("Type *A = (Type *)P;");
2386  verifyFormat("Type *A = (vector<Type *, int *>)P;");
2387  verifyFormat("int a = (int)(2.0f);");
2388
2389  // FIXME: These also need to be identified.
2390  verifyFormat("int a = (int) 2.0f;");
2391  verifyFormat("int a = (int) * b;");
2392
2393  // These are not casts.
2394  verifyFormat("void f(int *) {}");
2395  verifyFormat("f(foo)->b;");
2396  verifyFormat("f(foo).b;");
2397  verifyFormat("f(foo)(b);");
2398  verifyFormat("f(foo)[b];");
2399  verifyFormat("[](foo) { return 4; }(bar)];");
2400  verifyFormat("(*funptr)(foo)[4];");
2401  verifyFormat("funptrs[4](foo)[4];");
2402  verifyFormat("void f(int *);");
2403  verifyFormat("void f(int *) = 0;");
2404  verifyFormat("void f(SmallVector<int>) {}");
2405  verifyFormat("void f(SmallVector<int>);");
2406  verifyFormat("void f(SmallVector<int>) = 0;");
2407  verifyFormat("void f(int i = (kValue) * kMask) {}");
2408  verifyFormat("void f(int i = (kA * kB) & kMask) {}");
2409  verifyFormat("int a = sizeof(int) * b;");
2410  verifyFormat("int a = alignof(int) * b;");
2411
2412  // These are not casts, but at some point were confused with casts.
2413  verifyFormat("virtual void foo(int *) override;");
2414  verifyFormat("virtual void foo(char &) const;");
2415  verifyFormat("virtual void foo(int *a, char *) const;");
2416  verifyFormat("int a = sizeof(int *) + b;");
2417  verifyFormat("int a = alignof(int *) + b;");
2418}
2419
2420TEST_F(FormatTest, FormatsFunctionTypes) {
2421  verifyFormat("A<bool()> a;");
2422  verifyFormat("A<SomeType()> a;");
2423  verifyFormat("A<void(*)(int, std::string)> a;");
2424  verifyFormat("A<void *(int)>;");
2425  verifyFormat("void *(*a)(int *, SomeType *);");
2426
2427  // FIXME: Inconsistent.
2428  verifyFormat("int (*func)(void *);");
2429  verifyFormat("void f() { int(*func)(void *); }");
2430
2431  verifyGoogleFormat("A<void*(int*, SomeType*)>;");
2432  verifyGoogleFormat("void* (*a)(int);");
2433}
2434
2435TEST_F(FormatTest, BreaksLongDeclarations) {
2436  verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
2437               "                  int LoooooooooooooooooooongParam2) {}");
2438  verifyFormat(
2439      "TypeSpecDecl *\n"
2440      "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n"
2441      "                     IdentifierIn *II, Type *T) {}");
2442  verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
2443               "ReallyReallyLongFunctionName(\n"
2444               "    const std::string &SomeParameter,\n"
2445               "    const SomeType<string, SomeOtherTemplateParameter> &\n"
2446               "        ReallyReallyLongParameterName,\n"
2447               "    const SomeType<string, SomeOtherTemplateParameter> &\n"
2448               "        AnotherLongParameterName) {}");
2449  verifyFormat(
2450      "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
2451      "aaaaaaaaaaaaaaaaaaaaaaa;");
2452
2453  verifyGoogleFormat(
2454      "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
2455      "                                   SourceLocation L) {}");
2456  verifyGoogleFormat(
2457      "some_namespace::LongReturnType\n"
2458      "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
2459      "    int first_long_parameter, int second_parameter) {}");
2460
2461  verifyGoogleFormat("template <typename T>\n"
2462                     "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
2463                     "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
2464  verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2465                     "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
2466}
2467
2468TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
2469  verifyFormat("(a)->b();");
2470  verifyFormat("--a;");
2471}
2472
2473TEST_F(FormatTest, HandlesIncludeDirectives) {
2474  verifyFormat("#include <string>\n"
2475               "#include <a/b/c.h>\n"
2476               "#include \"a/b/string\"\n"
2477               "#include \"string.h\"\n"
2478               "#include \"string.h\"\n"
2479               "#include <a-a>\n"
2480               "#include < path with space >\n"
2481               "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
2482               getLLVMStyleWithColumns(35));
2483
2484  verifyFormat("#import <string>");
2485  verifyFormat("#import <a/b/c.h>");
2486  verifyFormat("#import \"a/b/string\"");
2487  verifyFormat("#import \"string.h\"");
2488  verifyFormat("#import \"string.h\"");
2489}
2490
2491//===----------------------------------------------------------------------===//
2492// Error recovery tests.
2493//===----------------------------------------------------------------------===//
2494
2495TEST_F(FormatTest, IncompleteParameterLists) {
2496  FormatStyle NoBinPacking = getLLVMStyle();
2497  NoBinPacking.BinPackParameters = false;
2498  verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
2499               "                        double *min_x,\n"
2500               "                        double *max_x,\n"
2501               "                        double *min_y,\n"
2502               "                        double *max_y,\n"
2503               "                        double *min_z,\n"
2504               "                        double *max_z, ) {}",
2505               NoBinPacking);
2506}
2507
2508TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
2509  verifyFormat("void f() { return; }\n42");
2510  verifyFormat("void f() {\n"
2511               "  if (0)\n"
2512               "    return;\n"
2513               "}\n"
2514               "42");
2515  verifyFormat("void f() { return }\n42");
2516  verifyFormat("void f() {\n"
2517               "  if (0)\n"
2518               "    return\n"
2519               "}\n"
2520               "42");
2521}
2522
2523TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
2524  EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
2525  EXPECT_EQ("void f() {\n"
2526            "  if (a)\n"
2527            "    return\n"
2528            "}",
2529            format("void  f  (  )  {  if  ( a )  return  }"));
2530  EXPECT_EQ("namespace N { void f() }", format("namespace  N  {  void f()  }"));
2531  EXPECT_EQ("namespace N {\n"
2532            "void f() {}\n"
2533            "void g()\n"
2534            "}",
2535            format("namespace N  { void f( ) { } void g( ) }"));
2536}
2537
2538TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
2539  verifyFormat("int aaaaaaaa =\n"
2540               "    // Overlylongcomment\n"
2541               "    b;",
2542               getLLVMStyleWithColumns(20));
2543  verifyFormat("function(\n"
2544               "    ShortArgument,\n"
2545               "    LoooooooooooongArgument);\n",
2546               getLLVMStyleWithColumns(20));
2547}
2548
2549TEST_F(FormatTest, IncorrectAccessSpecifier) {
2550  verifyFormat("public:");
2551  verifyFormat("class A {\n"
2552               "public\n"
2553               "  void f() {}\n"
2554               "};");
2555  verifyFormat("public\n"
2556               "int qwerty;");
2557  verifyFormat("public\n"
2558               "B {}");
2559  verifyFormat("public\n"
2560               "{}");
2561  verifyFormat("public\n"
2562               "B { int x; }");
2563}
2564
2565TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
2566  verifyFormat("{");
2567  verifyFormat("#})");
2568}
2569
2570TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
2571  verifyFormat("do {\n}");
2572  verifyFormat("do {\n}\n"
2573               "f();");
2574  verifyFormat("do {\n}\n"
2575               "wheeee(fun);");
2576  verifyFormat("do {\n"
2577               "  f();\n"
2578               "}");
2579}
2580
2581TEST_F(FormatTest, IncorrectCodeMissingParens) {
2582  verifyFormat("if {\n  foo;\n  foo();\n}");
2583  verifyFormat("switch {\n  foo;\n  foo();\n}");
2584  verifyFormat("for {\n  foo;\n  foo();\n}");
2585  verifyFormat("while {\n  foo;\n  foo();\n}");
2586  verifyFormat("do {\n  foo;\n  foo();\n} while;");
2587}
2588
2589TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
2590  verifyFormat("namespace {\n"
2591               "class Foo {  Foo  ( }; }  // comment");
2592}
2593
2594TEST_F(FormatTest, IncorrectCodeErrorDetection) {
2595  EXPECT_EQ("{\n{}\n", format("{\n{\n}\n"));
2596  EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
2597  EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
2598  EXPECT_EQ("{\n  {}\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
2599
2600  EXPECT_EQ("{\n"
2601            "    {\n"
2602            " breakme(\n"
2603            "     qwe);\n"
2604            "}\n",
2605            format("{\n"
2606                   "    {\n"
2607                   " breakme(qwe);\n"
2608                   "}\n",
2609                   getLLVMStyleWithColumns(10)));
2610}
2611
2612TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
2613  verifyFormat("int x = {\n"
2614               "  avariable,\n"
2615               "  b(alongervariable)\n"
2616               "};",
2617               getLLVMStyleWithColumns(25));
2618}
2619
2620TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
2621  verifyFormat("return (a)(b) { 1, 2, 3 };");
2622}
2623
2624TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
2625  // FIXME: This is bad, find a better and more generic solution.
2626  verifyFormat(
2627      "Aaa({\n"
2628      "  int i;\n"
2629      "},\n"
2630      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2631      "                                     ccccccccccccccccc));");
2632}
2633
2634TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
2635  verifyFormat("void f() { return 42; }");
2636  verifyFormat("void f() {\n"
2637               "  // Comment\n"
2638               "}");
2639  verifyFormat("{\n"
2640               "#error {\n"
2641               "  int a;\n"
2642               "}");
2643  verifyFormat("{\n"
2644               "  int a;\n"
2645               "#error {\n"
2646               "}");
2647
2648  verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
2649  verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
2650
2651  verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
2652  verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
2653}
2654
2655TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
2656  // Elaborate type variable declarations.
2657  verifyFormat("struct foo a = { bar };\nint n;");
2658  verifyFormat("class foo a = { bar };\nint n;");
2659  verifyFormat("union foo a = { bar };\nint n;");
2660
2661  // Elaborate types inside function definitions.
2662  verifyFormat("struct foo f() {}\nint n;");
2663  verifyFormat("class foo f() {}\nint n;");
2664  verifyFormat("union foo f() {}\nint n;");
2665
2666  // Templates.
2667  verifyFormat("template <class X> void f() {}\nint n;");
2668  verifyFormat("template <struct X> void f() {}\nint n;");
2669  verifyFormat("template <union X> void f() {}\nint n;");
2670
2671  // Actual definitions...
2672  verifyFormat("struct {\n} n;");
2673  verifyFormat(
2674      "template <template <class T, class Y>, class Z> class X {\n} n;");
2675  verifyFormat("union Z {\n  int n;\n} x;");
2676  verifyFormat("class MACRO Z {\n} n;");
2677  verifyFormat("class MACRO(X) Z {\n} n;");
2678  verifyFormat("class __attribute__(X) Z {\n} n;");
2679  verifyFormat("class __declspec(X) Z {\n} n;");
2680  verifyFormat("class A##B##C {\n} n;");
2681
2682  // Redefinition from nested context:
2683  verifyFormat("class A::B::C {\n} n;");
2684
2685  // Template definitions.
2686  // FIXME: This is still incorrectly handled at the formatter side.
2687  verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};");
2688
2689  // FIXME:
2690  // This now gets parsed incorrectly as class definition.
2691  // verifyFormat("class A<int> f() {\n}\nint n;");
2692
2693  // Elaborate types where incorrectly parsing the structural element would
2694  // break the indent.
2695  verifyFormat("if (true)\n"
2696               "  class X x;\n"
2697               "else\n"
2698               "  f();\n");
2699
2700  // This is simply incomplete. Formatting is not important, but must not crash.
2701  verifyFormat("class A:");
2702}
2703
2704TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
2705  verifyFormat("#error Leave     all         white!!!!! space* alone!\n");
2706  verifyFormat("#warning Leave     all         white!!!!! space* alone!\n");
2707  EXPECT_EQ("#error 1", format("  #  error   1"));
2708  EXPECT_EQ("#warning 1", format("  #  warning 1"));
2709}
2710
2711TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
2712  FormatStyle AllowsMergedIf = getGoogleStyle();
2713  AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
2714  verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
2715  verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
2716  verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
2717  EXPECT_EQ("if (true) return 42;",
2718            format("if (true)\nreturn 42;", AllowsMergedIf));
2719  FormatStyle ShortMergedIf = AllowsMergedIf;
2720  ShortMergedIf.ColumnLimit = 25;
2721  verifyFormat("#define A               \\\n"
2722               "  if (true) return 42;",
2723               ShortMergedIf);
2724  verifyFormat("#define A               \\\n"
2725               "  f();                  \\\n"
2726               "  if (true)\n"
2727               "#define B",
2728               ShortMergedIf);
2729  verifyFormat("#define A               \\\n"
2730               "  f();                  \\\n"
2731               "  if (true)\n"
2732               "g();",
2733               ShortMergedIf);
2734  verifyFormat("{\n"
2735               "#ifdef A\n"
2736               "  // Comment\n"
2737               "  if (true) continue;\n"
2738               "#endif\n"
2739               "  // Comment\n"
2740               "  if (true) continue;",
2741               ShortMergedIf);
2742}
2743
2744TEST_F(FormatTest, BlockCommentsInControlLoops) {
2745  verifyFormat("if (0) /* a comment in a strange place */ {\n"
2746               "  f();\n"
2747               "}");
2748  verifyFormat("if (0) /* a comment in a strange place */ {\n"
2749               "  f();\n"
2750               "} /* another comment */ else /* comment #3 */ {\n"
2751               "  g();\n"
2752               "}");
2753  verifyFormat("while (0) /* a comment in a strange place */ {\n"
2754               "  f();\n"
2755               "}");
2756  verifyFormat("for (;;) /* a comment in a strange place */ {\n"
2757               "  f();\n"
2758               "}");
2759  verifyFormat("do /* a comment in a strange place */ {\n"
2760               "  f();\n"
2761               "} /* another comment */ while (0);");
2762}
2763
2764TEST_F(FormatTest, BlockComments) {
2765  EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
2766            format("/* *//* */  /* */\n/* *//* */  /* */"));
2767  EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
2768  EXPECT_EQ("#define A /*123*/\\\n"
2769            "  b\n"
2770            "/* */\n"
2771            "someCall(\n"
2772            "    parameter);",
2773            format("#define A /*123*/ b\n"
2774                   "/* */\n"
2775                   "someCall(parameter);",
2776                   getLLVMStyleWithColumns(15)));
2777
2778  EXPECT_EQ("#define A\n"
2779            "/* */ someCall(\n"
2780            "    parameter);",
2781            format("#define A\n"
2782                   "/* */someCall(parameter);",
2783                   getLLVMStyleWithColumns(15)));
2784
2785  FormatStyle NoBinPacking = getLLVMStyle();
2786  NoBinPacking.BinPackParameters = false;
2787  EXPECT_EQ("someFunction(1, /* comment 1 */\n"
2788            "             2, /* comment 2 */\n"
2789            "             3, /* comment 3 */\n"
2790            "             aaaa,\n"
2791            "             bbbb);",
2792            format("someFunction (1,   /* comment 1 */\n"
2793                   "                2, /* comment 2 */  \n"
2794                   "               3,   /* comment 3 */\n"
2795                   "aaaa, bbbb );",
2796                   NoBinPacking));
2797  verifyFormat(
2798      "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2799      "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2800  EXPECT_EQ(
2801      "bool aaaaaaaaaaaaa = /* trailing comment */\n"
2802      "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2803      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
2804      format(
2805          "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
2806          "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
2807          "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
2808  EXPECT_EQ(
2809      "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2810      "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
2811      "int cccccccccccccccccccccccccccccc;       /* comment */\n",
2812      format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
2813             "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
2814             "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
2815}
2816
2817TEST_F(FormatTest, BlockCommentsInMacros) {
2818  EXPECT_EQ("#define A          \\\n"
2819            "  {                \\\n"
2820            "    /* one line */ \\\n"
2821            "    someCall();",
2822            format("#define A {        \\\n"
2823                   "  /* one line */   \\\n"
2824                   "  someCall();",
2825                   getLLVMStyleWithColumns(20)));
2826  EXPECT_EQ("#define A          \\\n"
2827            "  {                \\\n"
2828            "    /* previous */ \\\n"
2829            "    /* one line */ \\\n"
2830            "    someCall();",
2831            format("#define A {        \\\n"
2832                   "  /* previous */   \\\n"
2833                   "  /* one line */   \\\n"
2834                   "  someCall();",
2835                   getLLVMStyleWithColumns(20)));
2836}
2837
2838TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
2839  // FIXME: This is not what we want...
2840  verifyFormat("{\n"
2841               "// a"
2842               "// b");
2843}
2844
2845TEST_F(FormatTest, FormatStarDependingOnContext) {
2846  verifyFormat("void f(int *a);");
2847  verifyFormat("void f() { f(fint * b); }");
2848  verifyFormat("class A {\n  void f(int *a);\n};");
2849  verifyFormat("class A {\n  int *a;\n};");
2850  verifyFormat("namespace a {\n"
2851               "namespace b {\n"
2852               "class A {\n"
2853               "  void f() {}\n"
2854               "  int *a;\n"
2855               "};\n"
2856               "}\n"
2857               "}");
2858}
2859
2860TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
2861  verifyFormat("while");
2862  verifyFormat("operator");
2863}
2864
2865//===----------------------------------------------------------------------===//
2866// Objective-C tests.
2867//===----------------------------------------------------------------------===//
2868
2869TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
2870  verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
2871  EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
2872            format("-(NSUInteger)indexOfObject:(id)anObject;"));
2873  EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
2874  EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
2875  EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
2876            format("-(NSInteger)Method3:(id)anObject;"));
2877  EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
2878            format("-(NSInteger)Method4:(id)anObject;"));
2879  EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
2880            format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
2881  EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
2882            format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
2883  EXPECT_EQ(
2884      "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
2885      format(
2886          "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
2887
2888  // Very long objectiveC method declaration.
2889  verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
2890               "                    inRange:(NSRange)range\n"
2891               "                   outRange:(NSRange)out_range\n"
2892               "                  outRange1:(NSRange)out_range1\n"
2893               "                  outRange2:(NSRange)out_range2\n"
2894               "                  outRange3:(NSRange)out_range3\n"
2895               "                  outRange4:(NSRange)out_range4\n"
2896               "                  outRange5:(NSRange)out_range5\n"
2897               "                  outRange6:(NSRange)out_range6\n"
2898               "                  outRange7:(NSRange)out_range7\n"
2899               "                  outRange8:(NSRange)out_range8\n"
2900               "                  outRange9:(NSRange)out_range9;");
2901
2902  verifyFormat("- (int)sum:(vector<int>)numbers;");
2903  verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
2904  // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
2905  // protocol lists (but not for template classes):
2906  //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
2907
2908  verifyFormat("- (int(*)())foo:(int(*)())f;");
2909  verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;");
2910
2911  // If there's no return type (very rare in practice!), LLVM and Google style
2912  // agree.
2913  verifyFormat("- foo;");
2914  verifyFormat("- foo:(int)f;");
2915  verifyGoogleFormat("- foo:(int)foo;");
2916}
2917
2918TEST_F(FormatTest, FormatObjCBlocks) {
2919  verifyFormat("int (^Block)(int, int);");
2920  verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
2921}
2922
2923TEST_F(FormatTest, FormatObjCInterface) {
2924  verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
2925               "@public\n"
2926               "  int field1;\n"
2927               "@protected\n"
2928               "  int field2;\n"
2929               "@private\n"
2930               "  int field3;\n"
2931               "@package\n"
2932               "  int field4;\n"
2933               "}\n"
2934               "+ (id)init;\n"
2935               "@end");
2936
2937  verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
2938                     " @public\n"
2939                     "  int field1;\n"
2940                     " @protected\n"
2941                     "  int field2;\n"
2942                     " @private\n"
2943                     "  int field3;\n"
2944                     " @package\n"
2945                     "  int field4;\n"
2946                     "}\n"
2947                     "+ (id)init;\n"
2948                     "@end");
2949
2950  verifyFormat("@interface /* wait for it */ Foo\n"
2951               "+ (id)init;\n"
2952               "// Look, a comment!\n"
2953               "- (int)answerWith:(int)i;\n"
2954               "@end");
2955
2956  verifyFormat("@interface Foo\n"
2957               "@end\n"
2958               "@interface Bar\n"
2959               "@end");
2960
2961  verifyFormat("@interface Foo : Bar\n"
2962               "+ (id)init;\n"
2963               "@end");
2964
2965  verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
2966               "+ (id)init;\n"
2967               "@end");
2968
2969  verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
2970                     "+ (id)init;\n"
2971                     "@end");
2972
2973  verifyFormat("@interface Foo (HackStuff)\n"
2974               "+ (id)init;\n"
2975               "@end");
2976
2977  verifyFormat("@interface Foo ()\n"
2978               "+ (id)init;\n"
2979               "@end");
2980
2981  verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
2982               "+ (id)init;\n"
2983               "@end");
2984
2985  verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
2986                     "+ (id)init;\n"
2987                     "@end");
2988
2989  verifyFormat("@interface Foo {\n"
2990               "  int _i;\n"
2991               "}\n"
2992               "+ (id)init;\n"
2993               "@end");
2994
2995  verifyFormat("@interface Foo : Bar {\n"
2996               "  int _i;\n"
2997               "}\n"
2998               "+ (id)init;\n"
2999               "@end");
3000
3001  verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
3002               "  int _i;\n"
3003               "}\n"
3004               "+ (id)init;\n"
3005               "@end");
3006
3007  verifyFormat("@interface Foo (HackStuff) {\n"
3008               "  int _i;\n"
3009               "}\n"
3010               "+ (id)init;\n"
3011               "@end");
3012
3013  verifyFormat("@interface Foo () {\n"
3014               "  int _i;\n"
3015               "}\n"
3016               "+ (id)init;\n"
3017               "@end");
3018
3019  verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
3020               "  int _i;\n"
3021               "}\n"
3022               "+ (id)init;\n"
3023               "@end");
3024}
3025
3026TEST_F(FormatTest, FormatObjCImplementation) {
3027  verifyFormat("@implementation Foo : NSObject {\n"
3028               "@public\n"
3029               "  int field1;\n"
3030               "@protected\n"
3031               "  int field2;\n"
3032               "@private\n"
3033               "  int field3;\n"
3034               "@package\n"
3035               "  int field4;\n"
3036               "}\n"
3037               "+ (id)init {\n}\n"
3038               "@end");
3039
3040  verifyGoogleFormat("@implementation Foo : NSObject {\n"
3041                     " @public\n"
3042                     "  int field1;\n"
3043                     " @protected\n"
3044                     "  int field2;\n"
3045                     " @private\n"
3046                     "  int field3;\n"
3047                     " @package\n"
3048                     "  int field4;\n"
3049                     "}\n"
3050                     "+ (id)init {\n}\n"
3051                     "@end");
3052
3053  verifyFormat("@implementation Foo\n"
3054               "+ (id)init {\n"
3055               "  if (true)\n"
3056               "    return nil;\n"
3057               "}\n"
3058               "// Look, a comment!\n"
3059               "- (int)answerWith:(int)i {\n"
3060               "  return i;\n"
3061               "}\n"
3062               "+ (int)answerWith:(int)i {\n"
3063               "  return i;\n"
3064               "}\n"
3065               "@end");
3066
3067  verifyFormat("@implementation Foo\n"
3068               "@end\n"
3069               "@implementation Bar\n"
3070               "@end");
3071
3072  verifyFormat("@implementation Foo : Bar\n"
3073               "+ (id)init {\n}\n"
3074               "- (void)foo {\n}\n"
3075               "@end");
3076
3077  verifyFormat("@implementation Foo {\n"
3078               "  int _i;\n"
3079               "}\n"
3080               "+ (id)init {\n}\n"
3081               "@end");
3082
3083  verifyFormat("@implementation Foo : Bar {\n"
3084               "  int _i;\n"
3085               "}\n"
3086               "+ (id)init {\n}\n"
3087               "@end");
3088
3089  verifyFormat("@implementation Foo (HackStuff)\n"
3090               "+ (id)init {\n}\n"
3091               "@end");
3092}
3093
3094TEST_F(FormatTest, FormatObjCProtocol) {
3095  verifyFormat("@protocol Foo\n"
3096               "@property(weak) id delegate;\n"
3097               "- (NSUInteger)numberOfThings;\n"
3098               "@end");
3099
3100  verifyFormat("@protocol MyProtocol <NSObject>\n"
3101               "- (NSUInteger)numberOfThings;\n"
3102               "@end");
3103
3104  verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
3105                     "- (NSUInteger)numberOfThings;\n"
3106                     "@end");
3107
3108  verifyFormat("@protocol Foo;\n"
3109               "@protocol Bar;\n");
3110
3111  verifyFormat("@protocol Foo\n"
3112               "@end\n"
3113               "@protocol Bar\n"
3114               "@end");
3115
3116  verifyFormat("@protocol myProtocol\n"
3117               "- (void)mandatoryWithInt:(int)i;\n"
3118               "@optional\n"
3119               "- (void)optional;\n"
3120               "@required\n"
3121               "- (void)required;\n"
3122               "@optional\n"
3123               "@property(assign) int madProp;\n"
3124               "@end\n");
3125}
3126
3127TEST_F(FormatTest, FormatObjCMethodDeclarations) {
3128  verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
3129               "                   rect:(NSRect)theRect\n"
3130               "               interval:(float)theInterval {\n"
3131               "}");
3132  verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
3133               "          longKeyword:(NSRect)theRect\n"
3134               "    evenLongerKeyword:(float)theInterval\n"
3135               "                error:(NSError **)theError {\n"
3136               "}");
3137}
3138
3139TEST_F(FormatTest, FormatObjCMethodExpr) {
3140  verifyFormat("[foo bar:baz];");
3141  verifyFormat("return [foo bar:baz];");
3142  verifyFormat("f([foo bar:baz]);");
3143  verifyFormat("f(2, [foo bar:baz]);");
3144  verifyFormat("f(2, a ? b : c);");
3145  verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
3146
3147  // Unary operators.
3148  verifyFormat("int a = +[foo bar:baz];");
3149  verifyFormat("int a = -[foo bar:baz];");
3150  verifyFormat("int a = ![foo bar:baz];");
3151  verifyFormat("int a = ~[foo bar:baz];");
3152  verifyFormat("int a = ++[foo bar:baz];");
3153  verifyFormat("int a = --[foo bar:baz];");
3154  verifyFormat("int a = sizeof [foo bar:baz];");
3155  verifyFormat("int a = alignof [foo bar:baz];");
3156  verifyFormat("int a = &[foo bar:baz];");
3157  verifyFormat("int a = *[foo bar:baz];");
3158  // FIXME: Make casts work, without breaking f()[4].
3159  //verifyFormat("int a = (int)[foo bar:baz];");
3160  //verifyFormat("return (int)[foo bar:baz];");
3161  //verifyFormat("(void)[foo bar:baz];");
3162  verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
3163
3164  // Binary operators.
3165  verifyFormat("[foo bar:baz], [foo bar:baz];");
3166  verifyFormat("[foo bar:baz] = [foo bar:baz];");
3167  verifyFormat("[foo bar:baz] *= [foo bar:baz];");
3168  verifyFormat("[foo bar:baz] /= [foo bar:baz];");
3169  verifyFormat("[foo bar:baz] %= [foo bar:baz];");
3170  verifyFormat("[foo bar:baz] += [foo bar:baz];");
3171  verifyFormat("[foo bar:baz] -= [foo bar:baz];");
3172  verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
3173  verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
3174  verifyFormat("[foo bar:baz] &= [foo bar:baz];");
3175  verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
3176  verifyFormat("[foo bar:baz] |= [foo bar:baz];");
3177  verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
3178  verifyFormat("[foo bar:baz] || [foo bar:baz];");
3179  verifyFormat("[foo bar:baz] && [foo bar:baz];");
3180  verifyFormat("[foo bar:baz] | [foo bar:baz];");
3181  verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
3182  verifyFormat("[foo bar:baz] & [foo bar:baz];");
3183  verifyFormat("[foo bar:baz] == [foo bar:baz];");
3184  verifyFormat("[foo bar:baz] != [foo bar:baz];");
3185  verifyFormat("[foo bar:baz] >= [foo bar:baz];");
3186  verifyFormat("[foo bar:baz] <= [foo bar:baz];");
3187  verifyFormat("[foo bar:baz] > [foo bar:baz];");
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  // Whew!
3197
3198  verifyFormat("return in[42];");
3199  verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
3200               "}");
3201
3202  verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
3203  verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
3204  verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
3205  verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
3206  verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
3207  verifyFormat("[button setAction:@selector(zoomOut:)];");
3208  verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
3209
3210  verifyFormat("arr[[self indexForFoo:a]];");
3211  verifyFormat("throw [self errorFor:a];");
3212  verifyFormat("@throw [self errorFor:a];");
3213
3214  // This tests that the formatter doesn't break after "backing" but before ":",
3215  // which would be at 80 columns.
3216  verifyFormat(
3217      "void f() {\n"
3218      "  if ((self = [super initWithContentRect:contentRect\n"
3219      "                               styleMask:styleMask\n"
3220      "                                 backing:NSBackingStoreBuffered\n"
3221      "                                   defer:YES]))");
3222
3223  verifyFormat(
3224      "[foo checkThatBreakingAfterColonWorksOk:\n"
3225      "        [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
3226
3227  verifyFormat("[myObj short:arg1 // Force line break\n"
3228               "          longKeyword:arg2\n"
3229               "    evenLongerKeyword:arg3\n"
3230               "                error:arg4];");
3231  verifyFormat(
3232      "void f() {\n"
3233      "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
3234      "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
3235      "                                     pos.width(), pos.height())\n"
3236      "                styleMask:NSBorderlessWindowMask\n"
3237      "                  backing:NSBackingStoreBuffered\n"
3238      "                    defer:NO]);\n"
3239      "}");
3240  verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
3241               "                             with:contentsNativeView];");
3242
3243  verifyFormat(
3244      "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
3245      "           owner:nillllll];");
3246
3247  verifyFormat(
3248      "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
3249      "        forType:kBookmarkButtonDragType];");
3250
3251  verifyFormat("[defaultCenter addObserver:self\n"
3252               "                  selector:@selector(willEnterFullscreen)\n"
3253               "                      name:kWillEnterFullscreenNotification\n"
3254               "                    object:nil];");
3255  verifyFormat("[image_rep drawInRect:drawRect\n"
3256               "             fromRect:NSZeroRect\n"
3257               "            operation:NSCompositeCopy\n"
3258               "             fraction:1.0\n"
3259               "       respectFlipped:NO\n"
3260               "                hints:nil];");
3261
3262  verifyFormat(
3263      "scoped_nsobject<NSTextField> message(\n"
3264      "    // The frame will be fixed up when |-setMessageText:| is called.\n"
3265      "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
3266}
3267
3268TEST_F(FormatTest, ObjCAt) {
3269  verifyFormat("@autoreleasepool");
3270  verifyFormat("@catch");
3271  verifyFormat("@class");
3272  verifyFormat("@compatibility_alias");
3273  verifyFormat("@defs");
3274  verifyFormat("@dynamic");
3275  verifyFormat("@encode");
3276  verifyFormat("@end");
3277  verifyFormat("@finally");
3278  verifyFormat("@implementation");
3279  verifyFormat("@import");
3280  verifyFormat("@interface");
3281  verifyFormat("@optional");
3282  verifyFormat("@package");
3283  verifyFormat("@private");
3284  verifyFormat("@property");
3285  verifyFormat("@protected");
3286  verifyFormat("@protocol");
3287  verifyFormat("@public");
3288  verifyFormat("@required");
3289  verifyFormat("@selector");
3290  verifyFormat("@synchronized");
3291  verifyFormat("@synthesize");
3292  verifyFormat("@throw");
3293  verifyFormat("@try");
3294
3295  EXPECT_EQ("@interface", format("@ interface"));
3296
3297  // The precise formatting of this doesn't matter, nobody writes code like
3298  // this.
3299  verifyFormat("@ /*foo*/ interface");
3300}
3301
3302TEST_F(FormatTest, ObjCSnippets) {
3303  verifyFormat("@autoreleasepool {\n"
3304               "  foo();\n"
3305               "}");
3306  verifyFormat("@class Foo, Bar;");
3307  verifyFormat("@compatibility_alias AliasName ExistingClass;");
3308  verifyFormat("@dynamic textColor;");
3309  verifyFormat("char *buf1 = @encode(int *);");
3310  verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
3311  verifyFormat("char *buf1 = @encode(int **);");
3312  verifyFormat("Protocol *proto = @protocol(p1);");
3313  verifyFormat("SEL s = @selector(foo:);");
3314  verifyFormat("@synchronized(self) {\n"
3315               "  f();\n"
3316               "}");
3317
3318  verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
3319  verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
3320
3321  verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
3322  verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
3323  verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
3324}
3325
3326TEST_F(FormatTest, ObjCLiterals) {
3327  verifyFormat("@\"String\"");
3328  verifyFormat("@1");
3329  verifyFormat("@+4.8");
3330  verifyFormat("@-4");
3331  verifyFormat("@1LL");
3332  verifyFormat("@.5");
3333  verifyFormat("@'c'");
3334  verifyFormat("@true");
3335
3336  verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
3337  verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
3338  verifyFormat("NSNumber *favoriteColor = @(Green);");
3339  verifyFormat("NSString *path = @(getenv(\"PATH\"));");
3340
3341  verifyFormat("@[");
3342  verifyFormat("@[]");
3343  verifyFormat(
3344      "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
3345  verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
3346
3347  verifyFormat("@{");
3348  verifyFormat("@{}");
3349  verifyFormat("@{ @\"one\" : @1 }");
3350  verifyFormat("return @{ @\"one\" : @1 };");
3351  verifyFormat("@{ @\"one\" : @1, }");
3352  verifyFormat("@{ @\"one\" : @{ @2 : @1 } }");
3353  verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }");
3354  verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }");
3355  verifyFormat("[self setDict:@{}");
3356  verifyFormat("[self setDict:@{ @1 : @2 }");
3357  verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);");
3358  verifyFormat(
3359      "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };");
3360  verifyFormat(
3361      "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };");
3362
3363  // FIXME: Nested and multi-line array and dictionary literals need more work.
3364  verifyFormat(
3365      "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
3366      "                     @\"processInfo\" : [NSProcessInfo processInfo] };");
3367}
3368
3369TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
3370  EXPECT_EQ("{\n"
3371            "{\n"
3372            "a;\n"
3373            "b;\n"
3374            "}\n"
3375            "}",
3376            format("{\n"
3377                   "{\n"
3378                   "a;\n"
3379                   "     b;\n"
3380                   "}\n"
3381                   "}",
3382                   13, 2, getLLVMStyle()));
3383  EXPECT_EQ("{\n"
3384            "{\n"
3385            "  a;\n"
3386            "b;\n"
3387            "}\n"
3388            "}",
3389            format("{\n"
3390                   "{\n"
3391                   "     a;\n"
3392                   "b;\n"
3393                   "}\n"
3394                   "}",
3395                   9, 2, getLLVMStyle()));
3396  EXPECT_EQ("{\n"
3397            "{\n"
3398            "public:\n"
3399            "  b;\n"
3400            "}\n"
3401            "}",
3402            format("{\n"
3403                   "{\n"
3404                   "public:\n"
3405                   "     b;\n"
3406                   "}\n"
3407                   "}",
3408                   17, 2, getLLVMStyle()));
3409  EXPECT_EQ("{\n"
3410            "{\n"
3411            "a;\n"
3412            "}\n"
3413            "{\n"
3414            "  b;\n"
3415            "}\n"
3416            "}",
3417            format("{\n"
3418                   "{\n"
3419                   "a;\n"
3420                   "}\n"
3421                   "{\n"
3422                   "           b;\n"
3423                   "}\n"
3424                   "}",
3425                   22, 2, getLLVMStyle()));
3426  EXPECT_EQ("  {\n"
3427            "    a;\n"
3428            "  }",
3429            format("  {\n"
3430                   "a;\n"
3431                   "  }",
3432                   4, 2, getLLVMStyle()));
3433  EXPECT_EQ("void f() {}\n"
3434            "void g() {}",
3435            format("void f() {}\n"
3436                   "void g() {}",
3437                   13, 0, getLLVMStyle()));
3438  EXPECT_EQ("int a; // comment\n"
3439            "       // line 2\n"
3440            "int b;",
3441            format("int a; // comment\n"
3442                   "       // line 2\n"
3443                   "  int b;",
3444                   35, 0, getLLVMStyle()));
3445}
3446
3447TEST_F(FormatTest, BreakStringLiterals) {
3448  EXPECT_EQ("\"some text \"\n"
3449            "\"other\";",
3450            format("\"some text other\";", getLLVMStyleWithColumns(12)));
3451  EXPECT_EQ(
3452      "#define A  \\\n"
3453      "  \"some \"  \\\n"
3454      "  \"text \"  \\\n"
3455      "  \"other\";",
3456      format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
3457  EXPECT_EQ(
3458      "#define A  \\\n"
3459      "  \"so \"    \\\n"
3460      "  \"text \"  \\\n"
3461      "  \"other\";",
3462      format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
3463
3464  EXPECT_EQ("\"some text\"",
3465            format("\"some text\"", getLLVMStyleWithColumns(1)));
3466  EXPECT_EQ("\"some text\"",
3467            format("\"some text\"", getLLVMStyleWithColumns(11)));
3468  EXPECT_EQ("\"some \"\n"
3469            "\"text\"",
3470            format("\"some text\"", getLLVMStyleWithColumns(10)));
3471  EXPECT_EQ("\"some \"\n"
3472            "\"text\"",
3473            format("\"some text\"", getLLVMStyleWithColumns(7)));
3474  EXPECT_EQ("\"some\"\n"
3475            "\" text\"",
3476            format("\"some text\"", getLLVMStyleWithColumns(6)));
3477  EXPECT_EQ("\"some\"\n"
3478            "\" tex\"\n"
3479            "\" and\"",
3480            format("\"some tex and\"", getLLVMStyleWithColumns(6)));
3481  EXPECT_EQ("\"some\"\n"
3482            "\"/tex\"\n"
3483            "\"/and\"",
3484            format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
3485
3486  EXPECT_EQ("variable =\n"
3487            "    \"long string \"\n"
3488            "    \"literal\";",
3489            format("variable = \"long string literal\";",
3490                   getLLVMStyleWithColumns(20)));
3491
3492  EXPECT_EQ("variable = f(\n"
3493            "    \"long string \"\n"
3494            "    \"literal\",\n"
3495            "    short,\n"
3496            "    loooooooooooooooooooong);",
3497            format("variable = f(\"long string literal\", short, "
3498                   "loooooooooooooooooooong);",
3499                   getLLVMStyleWithColumns(20)));
3500  EXPECT_EQ(
3501      "f(\"one two\".split(\n"
3502      "    variable));",
3503      format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
3504  EXPECT_EQ("f(\"one two three four five six \"\n"
3505            "  \"seven\".split(\n"
3506            "      really_looooong_variable));",
3507            format("f(\"one two three four five six seven\"."
3508                   "split(really_looooong_variable));",
3509                   getLLVMStyleWithColumns(33)));
3510
3511  EXPECT_EQ("f(\"some \"\n"
3512            "  \"text\",\n"
3513            "  other);",
3514            format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
3515
3516  // Only break as a last resort.
3517  verifyFormat(
3518      "aaaaaaaaaaaaaaaaaaaa(\n"
3519      "    aaaaaaaaaaaaaaaaaaaa,\n"
3520      "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
3521
3522  EXPECT_EQ(
3523      "\"splitmea\"\n"
3524      "\"trandomp\"\n"
3525      "\"oint\"",
3526      format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
3527
3528  EXPECT_EQ(
3529      "\"split/\"\n"
3530      "\"pathat/\"\n"
3531      "\"slashes\"",
3532      format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
3533}
3534
3535TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
3536  EXPECT_EQ("\"\\a\"",
3537            format("\"\\a\"", getLLVMStyleWithColumns(3)));
3538  EXPECT_EQ("\"\\\"",
3539            format("\"\\\"", getLLVMStyleWithColumns(2)));
3540  EXPECT_EQ("\"test\"\n"
3541            "\"\\n\"",
3542            format("\"test\\n\"", getLLVMStyleWithColumns(7)));
3543  EXPECT_EQ("\"tes\\\\\"\n"
3544            "\"n\"",
3545            format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
3546  EXPECT_EQ("\"\\\\\\\\\"\n"
3547            "\"\\n\"",
3548            format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
3549  EXPECT_EQ("\"\\uff01\"",
3550            format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
3551  EXPECT_EQ("\"\\uff01\"\n"
3552            "\"test\"",
3553            format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
3554  EXPECT_EQ("\"\\Uff01ff02\"",
3555            format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
3556  EXPECT_EQ("\"\\x000000000001\"\n"
3557            "\"next\"",
3558            format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
3559  EXPECT_EQ("\"\\x000000000001next\"",
3560            format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
3561  EXPECT_EQ("\"\\x000000000001\"",
3562            format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
3563  EXPECT_EQ("\"test\"\n"
3564            "\"\\000000\"\n"
3565            "\"000001\"",
3566            format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
3567  EXPECT_EQ("\"test\\000\"\n"
3568            "\"000000001\"",
3569            format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
3570  EXPECT_EQ("R\"(\\x\\x00)\"\n",
3571            format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7)));
3572}
3573
3574} // end namespace tooling
3575} // end namespace clang
3576