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