FormatTest.cpp revision 880e538536d1a7597eb5805d67a08e3941577c7f
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#include "clang/Format/Format.h"
11#include "../Tooling/RewriterTestContext.h"
12#include "clang/Lex/Lexer.h"
13#include "gtest/gtest.h"
14
15namespace clang {
16namespace format {
17
18class FormatTest : public ::testing::Test {
19protected:
20  std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
21                     const FormatStyle &Style) {
22    RewriterTestContext Context;
23    FileID ID = Context.createInMemoryFile("input.cc", Code);
24    SourceLocation Start =
25        Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
26    std::vector<CharSourceRange> Ranges(
27        1,
28        CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
29    LangOptions LangOpts;
30    LangOpts.CPlusPlus = 1;
31    LangOpts.CPlusPlus11 = 1;
32    LangOpts.ObjC1 = 1;
33    LangOpts.ObjC2 = 1;
34    Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
35    tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
36                                             Ranges);
37    EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
38    return Context.getRewrittenText(ID);
39  }
40
41  std::string format(llvm::StringRef Code,
42                     const FormatStyle &Style = getLLVMStyle()) {
43    return format(Code, 0, Code.size(), Style);
44  }
45
46  std::string messUp(llvm::StringRef Code) {
47    std::string MessedUp(Code.str());
48    bool InComment = false;
49    bool InPreprocessorDirective = false;
50    bool JustReplacedNewline = false;
51    for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
52      if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
53        if (JustReplacedNewline)
54          MessedUp[i - 1] = '\n';
55        InComment = true;
56      } else if (MessedUp[i] == '#' && JustReplacedNewline) {
57        MessedUp[i - 1] = '\n';
58        InPreprocessorDirective = true;
59      } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
60        MessedUp[i] = ' ';
61        MessedUp[i + 1] = ' ';
62      } else if (MessedUp[i] == '\n') {
63        if (InComment) {
64          InComment = false;
65        } else if (InPreprocessorDirective) {
66          InPreprocessorDirective = false;
67        } else {
68          JustReplacedNewline = true;
69          MessedUp[i] = ' ';
70        }
71      } else if (MessedUp[i] != ' ') {
72        JustReplacedNewline = false;
73      }
74    }
75    return MessedUp;
76  }
77
78  FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
79    FormatStyle Style = getLLVMStyle();
80    Style.ColumnLimit = ColumnLimit;
81    return Style;
82  }
83
84  void verifyFormat(llvm::StringRef Code,
85                    const FormatStyle &Style = getLLVMStyle()) {
86    EXPECT_EQ(Code.str(), format(messUp(Code), Style));
87  }
88
89  void verifyGoogleFormat(llvm::StringRef Code) {
90    verifyFormat(Code, getGoogleStyle());
91  }
92};
93
94TEST_F(FormatTest, MessUp) {
95  EXPECT_EQ("1 2 3", messUp("1 2 3"));
96  EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
97  EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
98  EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
99  EXPECT_EQ("a\n#b  c  d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
100}
101
102//===----------------------------------------------------------------------===//
103// Basic function tests.
104//===----------------------------------------------------------------------===//
105
106TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
107  EXPECT_EQ(";", format(";"));
108}
109
110TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
111  EXPECT_EQ("int i;", format("  int i;"));
112  EXPECT_EQ("\nint i;", format(" \n\t \r  int i;"));
113  EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
114  EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
115}
116
117TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
118  EXPECT_EQ("int i;", format("int\ni;"));
119}
120
121TEST_F(FormatTest, FormatsNestedBlockStatements) {
122  EXPECT_EQ("{\n  {\n    {\n    }\n  }\n}", format("{{{}}}"));
123}
124
125TEST_F(FormatTest, FormatsNestedCall) {
126  verifyFormat("Method(f1, f2(f3));");
127  verifyFormat("Method(f1(f2, f3()));");
128}
129
130//===----------------------------------------------------------------------===//
131// Tests for control statements.
132//===----------------------------------------------------------------------===//
133
134TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
135  verifyFormat("if (true)\n  f();\ng();");
136  verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
137  verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
138  verifyFormat("if (a)\n"
139               "  // comment\n"
140               "  f();");
141}
142
143TEST_F(FormatTest, ParseIfElse) {
144  verifyFormat("if (true)\n"
145               "  if (true)\n"
146               "    if (true)\n"
147               "      f();\n"
148               "    else\n"
149               "      g();\n"
150               "  else\n"
151               "    h();\n"
152               "else\n"
153               "  i();");
154  verifyFormat("if (true)\n"
155               "  if (true)\n"
156               "    if (true) {\n"
157               "      if (true)\n"
158               "        f();\n"
159               "    } else {\n"
160               "      g();\n"
161               "    }\n"
162               "  else\n"
163               "    h();\n"
164               "else {\n"
165               "  i();\n"
166               "}");
167}
168
169TEST_F(FormatTest, ElseIf) {
170  verifyFormat("if (a) {\n"
171               "} else if (b) {\n"
172               "}");
173  verifyFormat("if (a)\n"
174               "  f();\n"
175               "else if (b)\n"
176               "  g();\n"
177               "else\n"
178               "  h();");
179}
180
181TEST_F(FormatTest, FormatsForLoop) {
182  verifyFormat(
183      "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
184      "     ++VeryVeryLongLoopVariable)\n"
185      "  ;");
186  verifyFormat("for (;;)\n"
187               "  f();");
188  verifyFormat("for (;;) {\n"
189               "}");
190  verifyFormat("for (;;) {\n"
191               "  f();\n"
192               "}");
193
194  verifyFormat(
195      "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
196      "                                          E = UnwrappedLines.end();\n"
197      "     I != E; ++I) {\n}");
198
199  verifyFormat(
200      "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
201      "     ++IIIII) {\n}");
202}
203
204TEST_F(FormatTest, FormatsWhileLoop) {
205  verifyFormat("while (true) {\n}");
206  verifyFormat("while (true)\n"
207               "  f();");
208  verifyFormat("while () {\n"
209               "}");
210  verifyFormat("while () {\n"
211               "  f();\n"
212               "}");
213}
214
215TEST_F(FormatTest, FormatsDoWhile) {
216  verifyFormat("do {\n"
217               "  do_something();\n"
218               "} while (something());");
219  verifyFormat("do\n"
220               "  do_something();\n"
221               "while (something());");
222}
223
224TEST_F(FormatTest, FormatsSwitchStatement) {
225  verifyFormat("switch (x) {\n"
226               "case 1:\n"
227               "  f();\n"
228               "  break;\n"
229               "case kFoo:\n"
230               "case ns::kBar:\n"
231               "case kBaz:\n"
232               "  break;\n"
233               "default:\n"
234               "  g();\n"
235               "  break;\n"
236               "}");
237  verifyFormat("switch (x) {\n"
238               "case 1: {\n"
239               "  f();\n"
240               "  break;\n"
241               "}\n"
242               "}");
243  verifyFormat("switch (test)\n"
244               "  ;");
245  verifyGoogleFormat("switch (x) {\n"
246                     "  case 1:\n"
247                     "    f();\n"
248                     "    break;\n"
249                     "  case kFoo:\n"
250                     "  case ns::kBar:\n"
251                     "  case kBaz:\n"
252                     "    break;\n"
253                     "  default:\n"
254                     "    g();\n"
255                     "    break;\n"
256                     "}");
257  verifyGoogleFormat("switch (x) {\n"
258                     "  case 1: {\n"
259                     "    f();\n"
260                     "    break;\n"
261                     "  }\n"
262                     "}");
263  verifyGoogleFormat("switch (test)\n"
264                     "    ;");
265}
266
267TEST_F(FormatTest, FormatsLabels) {
268  verifyFormat("void f() {\n"
269               "  some_code();\n"
270               "test_label:\n"
271               "  some_other_code();\n"
272               "  {\n"
273               "    some_more_code();\n"
274               "  another_label:\n"
275               "    some_more_code();\n"
276               "  }\n"
277               "}");
278  verifyFormat("some_code();\n"
279               "test_label:\n"
280               "some_other_code();");
281}
282
283//===----------------------------------------------------------------------===//
284// Tests for comments.
285//===----------------------------------------------------------------------===//
286
287TEST_F(FormatTest, UnderstandsSingleLineComments) {
288  verifyFormat("// line 1\n"
289               "// line 2\n"
290               "void f() {\n}\n");
291
292  verifyFormat("void f() {\n"
293               "  // Doesn't do anything\n"
294               "}");
295
296  verifyFormat("int i // This is a fancy variable\n"
297               "    = 5;");
298
299  verifyFormat("enum E {\n"
300               "  // comment\n"
301               "  VAL_A, // comment\n"
302               "  VAL_B\n"
303               "};");
304
305  verifyFormat(
306      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
307      "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
308  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
309               "    // Comment inside a statement.\n"
310               "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
311
312  EXPECT_EQ("int i; // single line trailing comment",
313            format("int i;\\\n// single line trailing comment"));
314
315  verifyGoogleFormat("int a;  // Trailing comment.");
316}
317
318TEST_F(FormatTest, UnderstandsMultiLineComments) {
319  verifyFormat("f(/*test=*/ true);");
320}
321
322//===----------------------------------------------------------------------===//
323// Tests for classes, namespaces, etc.
324//===----------------------------------------------------------------------===//
325
326TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
327  verifyFormat("class A {\n};");
328}
329
330TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
331  verifyFormat("class A {\n"
332               "public:\n"
333               "protected:\n"
334               "private:\n"
335               "  void f() {\n"
336               "  }\n"
337               "};");
338  verifyGoogleFormat("class A {\n"
339                     " public:\n"
340                     " protected:\n"
341                     " private:\n"
342                     "  void f() {\n"
343                     "  }\n"
344                     "};");
345}
346
347TEST_F(FormatTest, FormatsDerivedClass) {
348  verifyFormat("class A : public B {\n"
349               "};");
350  verifyFormat("class A : public ::B {\n"
351               "};");
352}
353
354TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
355  verifyFormat("class A {\n"
356               "} a, b;");
357  verifyFormat("struct A {\n"
358               "} a, b;");
359}
360
361TEST_F(FormatTest, FormatsEnum) {
362  verifyFormat("enum {\n"
363               "  Zero,\n"
364               "  One = 1,\n"
365               "  Two = One + 1,\n"
366               "  Three = (One + Two),\n"
367               "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
368               "  Five = (One, Two, Three, Four, 5)\n"
369               "};");
370  verifyFormat("enum Enum {\n"
371               "};");
372  verifyFormat("enum {\n"
373               "};");
374}
375
376TEST_F(FormatTest, FormatsNamespaces) {
377  verifyFormat("namespace some_namespace {\n"
378               "class A {\n"
379               "};\n"
380               "void f() {\n"
381               "  f();\n"
382               "}\n"
383               "}");
384  verifyFormat("namespace {\n"
385               "class A {\n"
386               "};\n"
387               "void f() {\n"
388               "  f();\n"
389               "}\n"
390               "}");
391  verifyFormat("inline namespace X {\n"
392               "class A {\n"
393               "};\n"
394               "void f() {\n"
395               "  f();\n"
396               "}\n"
397               "}");
398  verifyFormat("using namespace some_namespace;\n"
399               "class A {\n"
400               "};\n"
401               "void f() {\n"
402               "  f();\n"
403               "}");
404}
405
406TEST_F(FormatTest, FormatTryCatch) {
407  verifyFormat("try {\n"
408               "  throw a * b;\n"
409               "}\n"
410               "catch (int a) {\n"
411               "  // Do nothing.\n"
412               "}\n"
413               "catch (...) {\n"
414               "  exit(42);\n"
415               "}");
416
417  // Function-level try statements.
418  verifyFormat("int f() try {\n"
419               "  return 4;\n"
420               "}\n"
421               "catch (...) {\n"
422               "  return 5;\n"
423               "}");
424  verifyFormat("class A {\n"
425               "  int a;\n"
426               "  A() try : a(0) {\n"
427               "  }\n"
428               "  catch (...) {\n"
429               "    throw;\n"
430               "  }\n"
431               "};\n");
432}
433
434TEST_F(FormatTest, FormatObjCTryCatch) {
435  verifyFormat("@try {\n"
436               "  f();\n"
437               "}\n"
438               "@catch (NSException e) {\n"
439               "  @throw;\n"
440               "}\n"
441               "@finally {\n"
442               "  exit(42);\n"
443               "}");
444}
445
446TEST_F(FormatTest, StaticInitializers) {
447  verifyFormat("static SomeClass SC = { 1, 'a' };");
448
449  // FIXME: Format like enums if the static initializer does not fit on a line.
450  verifyFormat(
451      "static SomeClass WithALoooooooooooooooooooongName = {\n"
452      "  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
453}
454
455TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
456  verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
457               "                      \\\n"
458               "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
459}
460
461TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
462  verifyFormat("virtual void write(ELFWriter *writerrr,\n"
463               "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
464}
465
466TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
467  EXPECT_EQ("#\n;", format("#;"));
468  verifyFormat("#\n;\n;\n;");
469}
470
471TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
472  EXPECT_EQ("#line 42 \"test\"\n",
473            format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
474  EXPECT_EQ("#define A  \\\n  B\n",
475            format("#  \\\n define  \\\n    A  \\\n       B\n",
476                   getLLVMStyleWithColumns(12)));
477}
478
479TEST_F(FormatTest, EndOfFileEndsPPDirective) {
480  EXPECT_EQ("#line 42 \"test\"",
481            format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
482  EXPECT_EQ("#define A  \\\n  B",
483            format("#  \\\n define  \\\n    A  \\\n       B",
484                   getLLVMStyleWithColumns(12)));
485}
486
487TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
488  // If the macro fits in one line, we still do not get the full
489  // line, as only the next line decides whether we need an escaped newline and
490  // thus use the last column.
491  verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
492
493  verifyFormat("#define A( \\\n    B)", getLLVMStyleWithColumns(12));
494  verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
495  verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
496}
497
498TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
499  EXPECT_EQ("// some comment\n"
500            "#include \"a.h\"\n"
501            "#define A(A,\\\n"
502            "          B)\n"
503            "#include \"b.h\"\n"
504            "// some comment\n",
505            format("  // some comment\n"
506                   "  #include \"a.h\"\n"
507                   "#define A(A,\\\n"
508                   "    B)\n"
509                   "    #include \"b.h\"\n"
510                   " // some comment\n", getLLVMStyleWithColumns(13)));
511}
512
513TEST_F(FormatTest, LayoutSingleHash) {
514  EXPECT_EQ("#\na;", format("#\na;"));
515}
516
517TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
518  EXPECT_EQ("#define A    \\\n"
519            "  c;         \\\n"
520            "  e;\n"
521            "f;", format("#define A c; e;\n"
522                         "f;", getLLVMStyleWithColumns(14)));
523}
524
525TEST_F(FormatTest, LayoutRemainingTokens) {
526  EXPECT_EQ("{\n}", format("{}"));
527}
528
529TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
530  EXPECT_EQ("# define A\\\n  b;",
531            format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
532}
533
534TEST_F(FormatTest, MacroDefinitionInsideStatement) {
535  EXPECT_EQ("int x,\n"
536            "#define A\n"
537            "    y;", format("int x,\n#define A\ny;"));
538}
539
540TEST_F(FormatTest, HashInMacroDefinition) {
541  verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
542  verifyFormat("#define A \\\n"
543               "  {       \\\n"
544               "    f(#c);\\\n"
545               "  }", getLLVMStyleWithColumns(11));
546
547  verifyFormat("#define A(X)         \\\n"
548               "  void function##X()", getLLVMStyleWithColumns(22));
549
550  verifyFormat("#define A(a, b, c)   \\\n"
551               "  void a##b##c()", getLLVMStyleWithColumns(22));
552
553  verifyFormat("#define A            \\\n"
554               "  void # ## #", getLLVMStyleWithColumns(22));
555}
556
557TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
558  EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
559}
560
561TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
562  verifyFormat("{\n  {\n    a #c;\n  }\n}");
563}
564
565TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
566  EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
567            format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
568  EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
569            format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
570}
571
572TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
573  EXPECT_EQ(
574      "#define A \\\n  int i;  \\\n  int j;",
575      format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
576}
577
578TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
579  verifyFormat("#define A \\\n"
580               "  int v(  \\\n"
581               "      a); \\\n"
582               "  int i;", getLLVMStyleWithColumns(11));
583}
584
585TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
586  EXPECT_EQ(
587      "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
588      "                      \\\n"
589      "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
590      "\n"
591      "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
592      "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
593      format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
594             "\\\n"
595             "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
596             "  \n"
597             "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
598             "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
599}
600
601TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
602  EXPECT_EQ("int\n"
603            "#define A\n"
604            "    a;",
605            format("int\n#define A\na;"));
606  verifyFormat(
607      "functionCallTo(someOtherFunction(\n"
608      "    withSomeParameters, whichInSequence,\n"
609      "    areLongerThanALine(andAnotherCall,\n"
610      "#define A                                                           \\\n"
611      "  B\n"
612      "                       withMoreParamters,\n"
613      "                       whichStronglyInfluenceTheLayout),\n"
614      "    andMoreParameters),\n"
615      "               trailing);", getLLVMStyleWithColumns(69));
616}
617
618//===----------------------------------------------------------------------===//
619// Line break tests.
620//===----------------------------------------------------------------------===//
621
622TEST_F(FormatTest, FormatsFunctionDefinition) {
623  verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
624               " int h, int j, int f,\n"
625               "       int c, int ddddddddddddd) {\n"
626               "}");
627}
628
629TEST_F(FormatTest, FormatsAwesomeMethodCall) {
630  verifyFormat(
631      "SomeLongMethodName(SomeReallyLongMethod(\n"
632      "    CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
633      "                   SecondLongCall(parameter));");
634}
635
636TEST_F(FormatTest, ConstructorInitializers) {
637  verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
638
639  verifyFormat(
640      "SomeClass::Constructor()\n"
641      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
642      "}");
643
644  verifyFormat(
645      "SomeClass::Constructor()\n"
646      "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
647      "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
648      "}");
649
650  verifyFormat("Constructor()\n"
651               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
652               "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
653               "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
654               "      aaaaaaaaaaaaaaaaaaaaaaa() {\n"
655               "}");
656
657  // Here a line could be saved by splitting the second initializer onto two
658  // lines, but that is not desireable.
659  verifyFormat("Constructor()\n"
660               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
661               "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
662               "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
663               "}");
664
665  verifyGoogleFormat("MyClass::MyClass(int var)\n"
666                     "    : some_var_(var),  // 4 space indent\n"
667                     "      some_other_var_(var + 1) {  // lined up\n"
668                     "}");
669}
670
671TEST_F(FormatTest, BreaksAsHighAsPossible) {
672  verifyFormat(
673      "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
674      "    (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
675      "  f();");
676}
677
678TEST_F(FormatTest, BreaksDesireably) {
679  verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
680               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
681               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
682
683  verifyFormat(
684      "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
685      "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
686
687  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
688               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
689               "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
690
691  verifyFormat(
692      "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
693      "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
694      "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
695      "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
696
697  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
698               "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
699
700  verifyFormat(
701      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
702      "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
703
704  // This test case breaks on an incorrect memoization, i.e. an optimization not
705  // taking into account the StopAt value.
706  verifyFormat(
707      "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
708      "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
709      "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
710      "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
711
712  verifyFormat("{\n  {\n    {\n"
713               "      Annotation.SpaceRequiredBefore =\n"
714               "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
715               "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
716               "    }\n  }\n}");
717}
718
719TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
720  verifyFormat(
721      "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
722      "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
723  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
724               "    ccccccccccccccccccccccccc) {\n}");
725  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
726               "    ccccccccccccccccccccccccc) {\n}");
727  verifyFormat(
728      "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
729      "    ccccccccccccccccccccccccc) {\n}");
730}
731
732TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
733  verifyFormat(
734      "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
735      "                                    SI->getAlignment(),\n"
736      "                                    SI->getPointerAddressSpaceee());\n");
737  verifyFormat(
738      "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
739      "                                Line.Tokens.front().Tok.getLocation(),\n"
740      "                                Line.Tokens.back().Tok.getLocation());");
741}
742
743TEST_F(FormatTest, AlignsAfterAssignments) {
744  verifyFormat(
745      "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
746      "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
747  verifyFormat(
748      "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
749      "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
750  verifyFormat(
751      "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
752      "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
753  verifyFormat(
754      "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
755      "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
756  verifyFormat(
757      "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
758      "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
759      "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
760}
761
762TEST_F(FormatTest, AlignsAfterReturn) {
763  verifyFormat(
764      "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
765      "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
766  verifyFormat(
767      "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
768      "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
769}
770
771TEST_F(FormatTest, BreaksConditionalExpressions) {
772  verifyFormat(
773      "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
774      "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
775      "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
776  verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
777               "         aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);");
778}
779
780TEST_F(FormatTest, AlignsStringLiterals) {
781  verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
782               "                                      \"short literal\");");
783  verifyFormat(
784      "looooooooooooooooooooooooongFunction(\n"
785      "    \"short literal\"\n"
786      "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
787}
788
789TEST_F(FormatTest, AlignsPipes) {
790  verifyFormat(
791      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
792      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
793      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
794  verifyFormat(
795      "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
796      "                     << aaaaaaaaaaaaaaaaaaaa;");
797  verifyFormat(
798      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
799      "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
800  verifyFormat(
801      "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
802      "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
803      "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
804  verifyFormat(
805      "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
806      "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
807      "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
808}
809
810TEST_F(FormatTest, UnderstandsEquals) {
811  verifyFormat(
812      "aaaaaaaaaaaaaaaaa =\n"
813      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
814  verifyFormat(
815      "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
816      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
817      "}");
818  verifyFormat(
819      "if (a) {\n"
820      "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
821      "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
822      "}");
823
824  verifyFormat(
825      // FIXME: Does an expression like this ever make sense? If yes, fix.
826      "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
827      "    10000000) {\n"
828      "}");
829}
830
831TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
832  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
833               "    .looooooooooooooooooooooooooooooooooooooongFunction();");
834
835  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
836               "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
837
838  verifyFormat(
839      "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
840      "                                                          Parameter2);");
841
842  verifyFormat(
843      "ShortObject->shortFunction(\n"
844      "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
845      "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
846
847  verifyFormat("loooooooooooooongFunction(\n"
848               "    LoooooooooooooongObject->looooooooooooooooongFunction());");
849
850  verifyFormat(
851      "function(LoooooooooooooooooooooooooooooooooooongObject\n"
852      "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
853
854  // Here, it is not necessary to wrap at "." or "->".
855  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
856               "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
857               "}");
858  verifyFormat(
859      "aaaaaaaaaaa->aaaaaaaaa(\n"
860      "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
861      "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
862}
863
864TEST_F(FormatTest, WrapsTemplateDeclarations) {
865  verifyFormat("template <typename T>\n"
866               "virtual void loooooooooooongFunction(int Param1, int Param2);");
867  verifyFormat(
868      "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
869      "                             int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
870  verifyFormat(
871      "template <typename T>\n"
872      "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
873      "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
874  verifyFormat(
875      "template <typename T>\n"
876      "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
877      "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
878      "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
879  verifyFormat("template <typename T>\n"
880               "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
881               "    int aaaaaaaaaaaaaaaaa);");
882  verifyFormat(
883      "template <typename T1, typename T2 = char, typename T3 = char,\n"
884      "          typename T4 = char>\n"
885      "void f();");
886}
887
888TEST_F(FormatTest, UnderstandsTemplateParameters) {
889  verifyFormat("A<int> a;");
890  verifyFormat("A<A<A<int> > > a;");
891  verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
892  verifyFormat("bool x = a < 1 || 2 > a;");
893  verifyFormat("bool x = 5 < f<int>();");
894  verifyFormat("bool x = f<int>() > 5;");
895  verifyFormat("bool x = 5 < a<int>::x;");
896  verifyFormat("bool x = a < 4 ? a > 2 : false;");
897  verifyFormat("bool x = f() ? a < 2 : a > 2;");
898
899  verifyGoogleFormat("A<A<int>> a;");
900  verifyGoogleFormat("A<A<A<int>>> a;");
901  verifyGoogleFormat("A<A<A<A<int>>>> a;");
902
903  verifyFormat("test >> a >> b;");
904  verifyFormat("test << a >> b;");
905
906  verifyFormat("f<int>();");
907  verifyFormat("template <typename T> void f() {\n}");
908}
909
910TEST_F(FormatTest, UnderstandsUnaryOperators) {
911  verifyFormat("int a = -2;");
912  verifyFormat("f(-1, -2, -3);");
913  verifyFormat("a[-1] = 5;");
914  verifyFormat("int a = 5 + -2;");
915  verifyFormat("if (i == -1) {\n}");
916  verifyFormat("if (i != -1) {\n}");
917  verifyFormat("if (i > -1) {\n}");
918  verifyFormat("if (i < -1) {\n}");
919  verifyFormat("++(a->f());");
920  verifyFormat("--(a->f());");
921  verifyFormat("if (!(a->f())) {\n}");
922
923  verifyFormat("a-- > b;");
924  verifyFormat("b ? -a : c;");
925  verifyFormat("n * sizeof char16;");
926  verifyFormat("n * alignof char16;");
927  verifyFormat("sizeof(char);");
928  verifyFormat("alignof(char);");
929
930  verifyFormat("return -1;");
931  verifyFormat("switch (a) {\n"
932               "case -1:\n"
933               "  break;\n"
934               "}");
935}
936
937TEST_F(FormatTest, UndestandsOverloadedOperators) {
938  verifyFormat("bool operator<();");
939  verifyFormat("bool operator>();");
940  verifyFormat("bool operator=();");
941  verifyFormat("bool operator==();");
942  verifyFormat("bool operator!=();");
943  verifyFormat("int operator+();");
944  verifyFormat("int operator++();");
945  verifyFormat("bool operator();");
946  verifyFormat("bool operator()();");
947  verifyFormat("bool operator[]();");
948  verifyFormat("operator bool();");
949  verifyFormat("operator SomeType<int>();");
950  verifyFormat("void *operator new(std::size_t size);");
951  verifyFormat("void *operator new[](std::size_t size);");
952  verifyFormat("void operator delete(void *ptr);");
953  verifyFormat("void operator delete[](void *ptr);");
954}
955
956TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
957  verifyFormat("int *f(int *a) {\n}");
958  verifyFormat("f(a, *a);");
959  verifyFormat("f(*a);");
960  verifyFormat("int a = b * 10;");
961  verifyFormat("int a = 10 * b;");
962  verifyFormat("int a = b * c;");
963  verifyFormat("int a += b * c;");
964  verifyFormat("int a -= b * c;");
965  verifyFormat("int a *= b * c;");
966  verifyFormat("int a /= b * c;");
967  verifyFormat("int a = *b;");
968  verifyFormat("int a = *b * c;");
969  verifyFormat("int a = b * *c;");
970  verifyFormat("int main(int argc, char **argv) {\n}");
971  verifyFormat("return 10 * b;");
972  verifyFormat("return *b * *c;");
973  verifyFormat("return a & ~b;");
974  verifyFormat("f(b ? *c : *d);");
975  verifyFormat("int a = b ? *c : *d;");
976  verifyFormat("*b = a;");
977  verifyFormat("a * ~b;");
978  verifyFormat("a * !b;");
979  verifyFormat("a * +b;");
980  verifyFormat("a * -b;");
981  verifyFormat("a * ++b;");
982  verifyFormat("a * --b;");
983
984  verifyFormat("InvalidRegions[*R] = 0;");
985
986  verifyFormat("A<int *> a;");
987  verifyFormat("A<int **> a;");
988  verifyFormat("A<int *, int *> a;");
989  verifyFormat("A<int **, int **> a;");
990  verifyFormat("Type *A = static_cast<Type *>(P);");
991  verifyFormat("Type *A = (Type *) P;");
992  verifyFormat("Type *A = (vector<Type *, int *>) P;");
993
994  verifyFormat(
995      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
996      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
997
998  verifyGoogleFormat("int main(int argc, char** argv) {\n}");
999  verifyGoogleFormat("A<int*> a;");
1000  verifyGoogleFormat("A<int**> a;");
1001  verifyGoogleFormat("A<int*, int*> a;");
1002  verifyGoogleFormat("A<int**, int**> a;");
1003  verifyGoogleFormat("f(b ? *c : *d);");
1004  verifyGoogleFormat("int a = b ? *c : *d;");
1005}
1006
1007TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
1008  verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
1009               "                  int LoooooooooooooooongParam2) {\n}");
1010  verifyFormat(
1011      "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
1012      "                                   SourceLocation L, IdentifierIn *II,\n"
1013      "                                   Type *T) {\n}");
1014}
1015
1016TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
1017  verifyFormat("(a)->b();");
1018  verifyFormat("--a;");
1019}
1020
1021TEST_F(FormatTest, HandlesIncludeDirectives) {
1022  EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
1023  EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
1024  EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
1025  EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
1026  EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
1027
1028  EXPECT_EQ("#import <string>\n", format("#import <string>\n"));
1029  EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n"));
1030  EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n"));
1031  EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1032  EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1033}
1034
1035//===----------------------------------------------------------------------===//
1036// Error recovery tests.
1037//===----------------------------------------------------------------------===//
1038
1039TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
1040  verifyFormat("int aaaaaaaa =\n"
1041               "    // Overly long comment\n"
1042               "    b;", getLLVMStyleWithColumns(20));
1043  verifyFormat("function(\n"
1044               "    ShortArgument,\n"
1045               "    LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20));
1046}
1047
1048TEST_F(FormatTest, IncorrectAccessSpecifier) {
1049  verifyFormat("public:");
1050  verifyFormat("class A {\n"
1051               "public\n"
1052               "  void f() {\n"
1053               "  }\n"
1054               "};");
1055  verifyFormat("public\n"
1056               "int qwerty;");
1057  verifyFormat("public\n"
1058               "B {\n"
1059               "}");
1060  verifyFormat("public\n"
1061               "{\n"
1062               "}");
1063  verifyFormat("public\n"
1064               "B {\n"
1065               "  int x;\n"
1066               "}");
1067}
1068
1069TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1070  verifyFormat("{");
1071}
1072
1073TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
1074  verifyFormat("do {\n"
1075               "}");
1076  verifyFormat("do {\n"
1077               "}\n"
1078               "f();");
1079  verifyFormat("do {\n"
1080               "}\n"
1081               "wheeee(fun);");
1082  verifyFormat("do {\n"
1083               "  f();\n"
1084               "}");
1085}
1086
1087TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1088  verifyFormat("namespace {\n"
1089               "class Foo {  Foo  ( }; }  // comment");
1090}
1091
1092TEST_F(FormatTest, IncorrectCodeErrorDetection) {
1093  EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n"));
1094  EXPECT_EQ("{\n  {\n}\n", format("{\n  {\n}\n"));
1095  EXPECT_EQ("{\n  {\n  }\n", format("{\n  {\n  }\n"));
1096  EXPECT_EQ("{\n  {\n    }\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
1097
1098  FormatStyle Style = getLLVMStyle();
1099  Style.ColumnLimit = 10;
1100  EXPECT_EQ("{\n"
1101            "    {\n"
1102            " breakme(\n"
1103            "     qwe);\n"
1104            "}\n", format("{\n"
1105                          "    {\n"
1106                          " breakme(qwe);\n"
1107                          "}\n", Style));
1108
1109}
1110
1111//===----------------------------------------------------------------------===//
1112// Objective-C tests.
1113//===----------------------------------------------------------------------===//
1114
1115TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1116  verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1117  EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1118            format("-(NSUInteger)indexOfObject:(id)anObject;"));
1119  EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
1120  EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1121  EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1122            format("-(NSInteger)Method3:(id)anObject;"));
1123  EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1124            format("-(NSInteger)Method4:(id)anObject;"));
1125  EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1126            format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1127  EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1128            format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
1129  EXPECT_EQ(
1130      "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1131      format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
1132
1133  // Very long objectiveC method declaration.
1134  EXPECT_EQ(
1135      "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n    "
1136      "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n    "
1137      "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n    "
1138      "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n    "
1139      "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n    "
1140      "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1141      format(
1142          "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1143          "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1144          "outRange2:(NSRange) out_range2  outRange3:(NSRange) out_range3  "
1145          "outRange4:(NSRange) out_range4  outRange5:(NSRange) out_range5 "
1146          "outRange6:(NSRange) out_range6  outRange7:(NSRange) out_range7  "
1147          "outRange8:(NSRange) out_range8  outRange9:(NSRange) out_range9;"));
1148}
1149
1150TEST_F(FormatTest, FormatObjCBlocks) {
1151  verifyFormat("int (^Block) (int, int);");
1152  verifyFormat("int (^Block1) (int, int) = ^(int i, int j)");
1153}
1154
1155TEST_F(FormatTest, FormatObjCInterface) {
1156  // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875
1157  // FIXME: In google style, it's "+(id) init", not "+ (id)init".
1158  verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1159               "@public\n"
1160               "  int field1;\n"
1161               "@protected\n"
1162               "  int field2;\n"
1163               "@private\n"
1164               "  int field3;\n"
1165               "@package\n"
1166               "  int field4;\n"
1167               "}\n"
1168               "+ (id)init;\n"
1169               "@end");
1170
1171  // FIXME: In LLVM style, there should be a space before '<' for protocols.
1172  verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
1173                     " @public\n"
1174                     "  int field1;\n"
1175                     " @protected\n"
1176                     "  int field2;\n"
1177                     " @private\n"
1178                     "  int field3;\n"
1179                     " @package\n"
1180                     "  int field4;\n"
1181                     "}\n"
1182                     "+ (id)init;\n"
1183                     "@end");
1184
1185  verifyFormat("@interface Foo\n"
1186               "+ (id)init;\n"
1187               "// Look, a comment!\n"
1188               "- (int)answerWith:(int)i;\n"
1189               "@end");
1190
1191  verifyFormat("@interface Foo\n"
1192               "@end\n"
1193               "@interface Bar\n"
1194               "@end");
1195
1196  verifyFormat("@interface Foo : Bar\n"
1197               "+ (id)init;\n"
1198               "@end");
1199
1200  verifyFormat("@interface Foo : Bar<Baz, Quux>\n"
1201               "+ (id)init;\n"
1202               "@end");
1203
1204  // FIXME: there should be a space before '(' for categories.
1205  verifyFormat("@interface Foo(HackStuff)\n"
1206               "+ (id)init;\n"
1207               "@end");
1208
1209  verifyFormat("@interface Foo()\n"
1210               "+ (id)init;\n"
1211               "@end");
1212
1213  verifyFormat("@interface Foo(HackStuff)<MyProtocol>\n"
1214               "+ (id)init;\n"
1215               "@end");
1216
1217  verifyFormat("@interface Foo {\n"
1218               "  int _i;\n"
1219               "}\n"
1220               "+ (id)init;\n"
1221               "@end");
1222
1223  verifyFormat("@interface Foo : Bar {\n"
1224               "  int _i;\n"
1225               "}\n"
1226               "+ (id)init;\n"
1227               "@end");
1228
1229  verifyFormat("@interface Foo : Bar<Baz, Quux> {\n"
1230               "  int _i;\n"
1231               "}\n"
1232               "+ (id)init;\n"
1233               "@end");
1234
1235  verifyFormat("@interface Foo(HackStuff) {\n"
1236               "  int _i;\n"
1237               "}\n"
1238               "+ (id)init;\n"
1239               "@end");
1240
1241  verifyFormat("@interface Foo() {\n"
1242               "  int _i;\n"
1243               "}\n"
1244               "+ (id)init;\n"
1245               "@end");
1246
1247  verifyFormat("@interface Foo(HackStuff)<MyProtocol> {\n"
1248               "  int _i;\n"
1249               "}\n"
1250               "+ (id)init;\n"
1251               "@end");
1252}
1253
1254TEST_F(FormatTest, FormatObjCImplementation) {
1255  verifyFormat("@implementation Foo : NSObject {\n"
1256               "@public\n"
1257               "  int field1;\n"
1258               "@protected\n"
1259               "  int field2;\n"
1260               "@private\n"
1261               "  int field3;\n"
1262               "@package\n"
1263               "  int field4;\n"
1264               "}\n"
1265               "+ (id)init {\n"
1266               "}\n"
1267               "@end");
1268
1269  verifyGoogleFormat("@implementation Foo : NSObject {\n"
1270                     " @public\n"
1271                     "  int field1;\n"
1272                     " @protected\n"
1273                     "  int field2;\n"
1274                     " @private\n"
1275                     "  int field3;\n"
1276                     " @package\n"
1277                     "  int field4;\n"
1278                     "}\n"
1279                     "+ (id)init {\n"
1280                     "}\n"
1281                     "@end");
1282
1283  verifyFormat("@implementation Foo\n"
1284               "+ (id)init {\n"
1285               "  if (true)\n"
1286               "    return nil;\n"
1287               "}\n"
1288               "// Look, a comment!\n"
1289               "- (int)answerWith:(int)i {\n"
1290               "  return i;\n"
1291               "}\n"
1292               "@end");
1293
1294  verifyFormat("@implementation Foo\n"
1295               "@end\n"
1296               "@implementation Bar\n"
1297               "@end");
1298
1299  verifyFormat("@implementation Foo : Bar\n"
1300               "+ (id)init {\n"
1301               "}\n"
1302               "@end");
1303
1304  verifyFormat("@implementation Foo {\n"
1305               "  int _i;\n"
1306               "}\n"
1307               "+ (id)init {\n"
1308               "}\n"
1309               "@end");
1310
1311  verifyFormat("@implementation Foo : Bar {\n"
1312               "  int _i;\n"
1313               "}\n"
1314               "+ (id)init {\n"
1315               "}\n"
1316               "@end");
1317
1318  // FIXME: there should be a space before '(' for categories.
1319  verifyFormat("@implementation Foo(HackStuff)\n"
1320               "+ (id)init {\n"
1321               "}\n"
1322               "@end");
1323}
1324
1325TEST_F(FormatTest, FormatObjCProtocol) {
1326  verifyFormat("@protocol Foo\n"
1327               "@property(weak) id delegate;\n"
1328               "- (NSUInteger)numberOfThings;\n"
1329               "@end");
1330
1331  // FIXME: In LLVM style, there should be a space before '<' for protocols.
1332  verifyFormat("@protocol MyProtocol<NSObject>\n"
1333               "- (NSUInteger)numberOfThings;\n"
1334               "@end");
1335
1336  verifyFormat("@protocol Foo;\n"
1337               "@protocol Bar;\n");
1338
1339  verifyFormat("@protocol Foo\n"
1340               "@end\n"
1341               "@protocol Bar\n"
1342               "@end");
1343
1344  verifyFormat("@protocol myProtocol\n"
1345               "- (void)mandatoryWithInt:(int)i;\n"
1346               "@optional\n"
1347               "- (void)optional;\n"
1348               "@required\n"
1349               "- (void)required;\n"
1350               "@optional\n"
1351               "@property(assign) int madProp;\n"
1352               "@end\n");
1353}
1354
1355TEST_F(FormatTest, ObjCAt) {
1356  verifyFormat("@autoreleasepool");
1357  verifyFormat("@catch");
1358  verifyFormat("@class");
1359  verifyFormat("@compatibility_alias");
1360  verifyFormat("@defs");
1361  verifyFormat("@dynamic");
1362  verifyFormat("@encode");
1363  verifyFormat("@end");
1364  verifyFormat("@finally");
1365  verifyFormat("@implementation");
1366  verifyFormat("@import");
1367  verifyFormat("@interface");
1368  verifyFormat("@optional");
1369  verifyFormat("@package");
1370  verifyFormat("@private");
1371  verifyFormat("@property");
1372  verifyFormat("@protected");
1373  verifyFormat("@protocol");
1374  verifyFormat("@public");
1375  verifyFormat("@required");
1376  verifyFormat("@selector");
1377  verifyFormat("@synchronized");
1378  verifyFormat("@synthesize");
1379  verifyFormat("@throw");
1380  verifyFormat("@try");
1381
1382  // FIXME: Make the uncommented lines below pass.
1383  verifyFormat("@\"String\"");
1384  verifyFormat("@1");
1385  //verifyFormat("@+4.8");
1386  //verifyFormat("@-4");
1387  verifyFormat("@1LL");
1388  verifyFormat("@.5");
1389  verifyFormat("@'c'");
1390  verifyFormat("@true");
1391  verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1392  verifyFormat("@[");
1393  verifyFormat("@{");
1394
1395
1396  EXPECT_EQ("@interface", format("@ interface"));
1397
1398  // The precise formatting of this doesn't matter, nobody writes code like
1399  // this.
1400  verifyFormat("@ /*foo*/ interface");
1401}
1402
1403TEST_F(FormatTest, ObjCSnippets) {
1404  // FIXME: Make the uncommented lines below pass.
1405  verifyFormat("@autoreleasepool {\n"
1406               "  foo();\n"
1407               "}");
1408  verifyFormat("@class Foo, Bar;");
1409  verifyFormat("@compatibility_alias AliasName ExistingClass;");
1410  verifyFormat("@dynamic textColor;");
1411  //verifyFormat("char *buf1 = @encode(int **);");
1412  verifyFormat("Protocol *proto = @protocol(p1);");
1413  //verifyFormat("SEL s = @selector(foo:);");
1414  verifyFormat("@synchronized(self) {\n"
1415               "  f();\n"
1416               "}");
1417  verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1418
1419  // FIXME: "getter=bar" should not be surround by spaces in @property.
1420  verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1421}
1422
1423} // end namespace tooling
1424} // end namespace clang
1425