FormatTest.cpp revision 9bb0d280faaedd528d2af7eeaf06c9f262345ad2
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    Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
33    tooling::Replacements Replace =
34        reformat(Style, Lex, Context.Sources, Ranges);
35    EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
36    return Context.getRewrittenText(ID);
37  }
38
39  std::string format(llvm::StringRef Code,
40                     const FormatStyle &Style = getLLVMStyle()) {
41    return format(Code, 0, Code.size(), Style);
42  }
43
44  std::string messUp(llvm::StringRef Code) {
45    std::string MessedUp(Code.str());
46    bool InComment = false;
47    bool JustReplacedNewline = false;
48    for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
49      if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
50        if (JustReplacedNewline)
51          MessedUp[i - 1] = '\n';
52        InComment = true;
53      } else if (MessedUp[i] != ' ') {
54        JustReplacedNewline = false;
55      } else if (MessedUp[i] == '\n') {
56        if (InComment) {
57          InComment = false;
58        } else {
59          JustReplacedNewline = true;
60          MessedUp[i] = ' ';
61        }
62      }
63    }
64    return MessedUp;
65  }
66
67  FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
68    FormatStyle Style = getLLVMStyle();
69    Style.ColumnLimit = ColumnLimit;
70    return Style;
71  }
72
73  void verifyFormat(llvm::StringRef Code,
74                    const FormatStyle &Style = getLLVMStyle()) {
75    EXPECT_EQ(Code.str(), format(messUp(Code), Style));
76  }
77
78  void verifyGoogleFormat(llvm::StringRef Code) {
79    verifyFormat(Code, getGoogleStyle());
80  }
81};
82
83//===----------------------------------------------------------------------===//
84// Basic function tests.
85//===----------------------------------------------------------------------===//
86
87TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
88  EXPECT_EQ(";", format(";"));
89}
90
91TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
92  EXPECT_EQ("int i;", format("  int i;"));
93  EXPECT_EQ("\nint i;", format(" \n\t \r  int i;"));
94  EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
95  EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
96}
97
98TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
99  EXPECT_EQ("int i;", format("int\ni;"));
100}
101
102TEST_F(FormatTest, FormatsNestedBlockStatements) {
103  EXPECT_EQ("{\n  {\n    {\n    }\n  }\n}", format("{{{}}}"));
104}
105
106TEST_F(FormatTest, FormatsNestedCall) {
107  verifyFormat("Method(f1, f2(f3));");
108  verifyFormat("Method(f1(f2, f3()));");
109}
110
111
112//===----------------------------------------------------------------------===//
113// Tests for control statements.
114//===----------------------------------------------------------------------===//
115
116TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
117  verifyFormat("if (true)\n  f();\ng();");
118  verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
119  verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
120  verifyFormat("if (a)\n"
121               "  // comment\n"
122               "  f();");
123}
124
125TEST_F(FormatTest, ParseIfElse) {
126  verifyFormat("if (true)\n"
127               "  if (true)\n"
128               "    if (true)\n"
129               "      f();\n"
130               "    else\n"
131               "      g();\n"
132               "  else\n"
133               "    h();\n"
134               "else\n"
135               "  i();");
136  verifyFormat("if (true)\n"
137               "  if (true)\n"
138               "    if (true) {\n"
139               "      if (true)\n"
140               "        f();\n"
141               "    } else {\n"
142               "      g();\n"
143               "    }\n"
144               "  else\n"
145               "    h();\n"
146               "else {\n"
147               "  i();\n"
148               "}");
149}
150
151TEST_F(FormatTest, ElseIf) {
152  verifyFormat("if (a) {\n"
153               "} else if (b) {\n"
154               "}");
155  verifyFormat("if (a)\n"
156               "  f();\n"
157               "else if (b)\n"
158               "  g();\n"
159               "else\n"
160               "  h();");
161}
162
163TEST_F(FormatTest, FormatsForLoop) {
164  verifyFormat(
165      "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
166      "     ++VeryVeryLongLoopVariable)\n"
167      "  ;");
168  verifyFormat("for (;;)\n"
169               "  f();");
170  verifyFormat("for (;;) {\n"
171               "}");
172  verifyFormat("for (;;) {\n"
173               "  f();\n"
174               "}");
175
176  verifyFormat(
177      "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
178      "                                          E = UnwrappedLines.end();\n"
179      "     I != E; ++I) {\n}");
180
181  verifyFormat(
182      "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
183      "     ++IIIII) {\n}");
184}
185
186TEST_F(FormatTest, FormatsWhileLoop) {
187  verifyFormat("while (true) {\n}");
188  verifyFormat("while (true)\n"
189               "  f();");
190  verifyFormat("while () {\n"
191               "}");
192  verifyFormat("while () {\n"
193               "  f();\n"
194               "}");
195}
196
197TEST_F(FormatTest, FormatsDoWhile) {
198  verifyFormat("do {\n"
199               "  do_something();\n"
200               "} while (something());");
201  verifyFormat("do\n"
202               "  do_something();\n"
203               "while (something());");
204}
205
206TEST_F(FormatTest, FormatsSwitchStatement) {
207  verifyFormat("switch (x) {\n"
208               "case 1:\n"
209               "  f();\n"
210               "  break;\n"
211               "case kFoo:\n"
212               "case ns::kBar:\n"
213               "case kBaz:\n"
214               "  break;\n"
215               "default:\n"
216               "  g();\n"
217               "  break;\n"
218               "}");
219  verifyFormat("switch (x) {\n"
220               "case 1: {\n"
221               "  f();\n"
222               "  break;\n"
223               "}\n"
224               "}");
225  verifyFormat("switch (test)\n"
226               "  ;");
227  verifyGoogleFormat("switch (x) {\n"
228                     "  case 1:\n"
229                     "    f();\n"
230                     "    break;\n"
231                     "  case kFoo:\n"
232                     "  case ns::kBar:\n"
233                     "  case kBaz:\n"
234                     "    break;\n"
235                     "  default:\n"
236                     "    g();\n"
237                     "    break;\n"
238                     "}");
239  verifyGoogleFormat("switch (x) {\n"
240                     "  case 1: {\n"
241                     "    f();\n"
242                     "    break;\n"
243                     "  }\n"
244                     "}");
245  verifyGoogleFormat("switch (test)\n"
246                     "    ;");
247}
248
249TEST_F(FormatTest, FormatsLabels) {
250  verifyFormat("void f() {\n"
251               "  some_code();\n"
252               "test_label:\n"
253               "  some_other_code();\n"
254               "  {\n"
255               "    some_more_code();\n"
256               "  another_label:\n"
257               "    some_more_code();\n"
258               "  }\n"
259               "}");
260  verifyFormat("some_code();\n"
261               "test_label:\n"
262               "some_other_code();");
263}
264
265
266//===----------------------------------------------------------------------===//
267// Tests for comments.
268//===----------------------------------------------------------------------===//
269
270TEST_F(FormatTest, UnderstandsSingleLineComments) {
271  verifyFormat("// line 1\n"
272               "// line 2\n"
273               "void f() {\n}\n");
274
275  verifyFormat("void f() {\n"
276               "  // Doesn't do anything\n"
277               "}");
278
279  verifyFormat("int i  // This is a fancy variable\n"
280               "    = 5;");
281
282  verifyFormat("enum E {\n"
283               "  // comment\n"
284               "  VAL_A,  // comment\n"
285               "  VAL_B\n"
286               "};");
287
288  verifyFormat(
289      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
290      "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;  // Trailing comment");
291}
292
293TEST_F(FormatTest, UnderstandsMultiLineComments) {
294  verifyFormat("f(/*test=*/ true);");
295}
296
297
298//===----------------------------------------------------------------------===//
299// Tests for classes, namespaces, etc.
300//===----------------------------------------------------------------------===//
301
302TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
303  verifyFormat("class A {\n};");
304}
305
306TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
307  verifyFormat("class A {\n"
308               "public:\n"
309               "protected:\n"
310               "private:\n"
311               "  void f() {\n"
312               "  }\n"
313               "};");
314  verifyGoogleFormat("class A {\n"
315                     " public:\n"
316                     " protected:\n"
317                     " private:\n"
318                     "  void f() {\n"
319                     "  }\n"
320                     "};");
321}
322
323TEST_F(FormatTest, FormatsDerivedClass) {
324  verifyFormat("class A : public B {\n"
325               "};");
326  verifyFormat("class A : public ::B {\n"
327               "};");
328}
329
330TEST_F(FormatTest, FormatsEnum) {
331  verifyFormat("enum {\n"
332               "  Zero,\n"
333               "  One = 1,\n"
334               "  Two = One + 1,\n"
335               "  Three = (One + Two),\n"
336               "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
337               "  Five = (One, Two, Three, Four, 5)\n"
338               "};");
339  verifyFormat("enum Enum {\n"
340               "};");
341  verifyFormat("enum {\n"
342               "};");
343}
344
345TEST_F(FormatTest, FormatsNamespaces) {
346  verifyFormat("namespace some_namespace {\n"
347               "class A {\n"
348               "};\n"
349               "void f() {\n"
350               "  f();\n"
351               "}\n"
352               "}");
353  verifyFormat("namespace {\n"
354               "class A {\n"
355               "};\n"
356               "void f() {\n"
357               "  f();\n"
358               "}\n"
359               "}");
360  verifyFormat("inline namespace X {\n"
361               "class A {\n"
362               "};\n"
363               "void f() {\n"
364               "  f();\n"
365               "}\n"
366               "}");
367  verifyFormat("using namespace some_namespace;\n"
368               "class A {\n"
369               "};\n"
370               "void f() {\n"
371               "  f();\n"
372               "}");
373}
374
375TEST_F(FormatTest, StaticInitializers) {
376  verifyFormat("static SomeClass SC = { 1, 'a' };");
377
378  // FIXME: Format like enums if the static initializer does not fit on a line.
379  verifyFormat(
380      "static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n"
381      "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
382}
383
384TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
385  verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
386               "                      \\\n"
387               "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
388}
389
390TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
391  EXPECT_EQ("#\n;", format("#;"));
392}
393
394TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
395  EXPECT_EQ("#line 42 \"test\"\n",
396            format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
397  EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n    B\n"));
398}
399
400TEST_F(FormatTest, EndOfFileEndsPPDirective) {
401  EXPECT_EQ("#line 42 \"test\"",
402            format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
403  EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n    B"));
404}
405
406TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
407  // If the macro fits in one line, we have the full width.
408  verifyFormat("#define A(B)", getLLVMStyleWithColumns(12));
409
410  verifyFormat("#define A(\\\n    B)", getLLVMStyleWithColumns(11));
411  verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(11));
412  verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
413}
414
415TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
416  verifyFormat(
417      "// some comment\n"
418      "\n"
419      "#include \"a.h\"\n"
420      "#define A(A,\\\n"
421      "          B)\n"
422      "#include \"b.h\"\n"
423      "\n"
424      "// some comment\n", getLLVMStyleWithColumns(13));
425}
426
427TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
428  verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
429               "                      \\\n"
430               "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
431               "\n"
432               "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
433               "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n");
434}
435
436//===----------------------------------------------------------------------===//
437// Line break tests.
438//===----------------------------------------------------------------------===//
439
440TEST_F(FormatTest, FormatsFunctionDefinition) {
441  verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
442               " int h, int j, int f,\n"
443               "       int c, int ddddddddddddd) {\n"
444               "}");
445}
446
447TEST_F(FormatTest, FormatsAwesomeMethodCall) {
448  verifyFormat(
449      "SomeLongMethodName(SomeReallyLongMethod(\n"
450      "    CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
451      "                   SecondLongCall(parameter));");
452}
453
454TEST_F(FormatTest, ConstructorInitializers) {
455  verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
456
457  verifyFormat(
458      "SomeClass::Constructor()\n"
459      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
460      "}");
461
462  verifyFormat(
463      "SomeClass::Constructor()\n"
464      "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
465      "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
466      "}");
467
468  verifyFormat("Constructor()\n"
469               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
470               "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
471               "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
472               "      aaaaaaaaaaaaaaaaaaaaaaa() {\n"
473               "}");
474
475  // Here a line could be saved by splitting the second initializer onto two
476  // lines, but that is not desireable.
477  verifyFormat("Constructor()\n"
478               "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
479               "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
480               "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
481               "}");
482
483  verifyGoogleFormat("MyClass::MyClass(int var)\n"
484                     "    : some_var_(var),  // 4 space indent\n"
485                     "      some_other_var_(var + 1) {  // lined up\n"
486                     "}");
487}
488
489TEST_F(FormatTest, BreaksAsHighAsPossible) {
490  verifyFormat(
491      "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
492      "    (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
493      "  f();");
494}
495
496TEST_F(FormatTest, BreaksDesireably) {
497  verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
498               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
499               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};");
500
501  verifyFormat(
502      "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
503      "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
504
505  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
506               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
507               "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
508
509  verifyFormat(
510      "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
511      "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
512      "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
513      "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
514
515  verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
516               "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
517
518  verifyFormat(
519      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
520      "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
521
522  // This test case breaks on an incorrect memoization, i.e. an optimization not
523  // taking into account the StopAt value.
524  verifyFormat(
525      "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
526      "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
527      "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
528      "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
529
530  verifyFormat(
531      "{\n  {\n    {\n"
532      "      Annotation.SpaceRequiredBefore =\n"
533      "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
534      "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
535      "    }\n  }\n}");
536}
537
538TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
539  verifyFormat(
540      "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
541      "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
542  verifyFormat(
543      "if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
544      "    ccccccccccccccccccccccccc) {\n}");
545  verifyFormat(
546      "if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
547      "    ccccccccccccccccccccccccc) {\n}");
548  verifyFormat(
549      "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
550      "    ccccccccccccccccccccccccc) {\n}");
551}
552
553TEST_F(FormatTest, AlignsAfterAssignments) {
554  verifyFormat(
555      "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
556      "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
557  verifyFormat(
558      "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
559      "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
560  verifyFormat(
561      "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
562      "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
563  verifyFormat(
564      "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
565      "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
566  verifyFormat(
567      "double LooooooooooooooooooooooooongResult =\n"
568      "    aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
569      "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
570}
571
572TEST_F(FormatTest, AlignsAfterReturn) {
573  verifyFormat(
574      "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
575      "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
576  verifyFormat(
577      "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
578      "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
579}
580
581TEST_F(FormatTest, AlignsStringLiterals) {
582  verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
583               "                                      \"short literal\");");
584  verifyFormat(
585      "looooooooooooooooooooooooongFunction(\n"
586      "    \"short literal\"\n"
587      "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
588}
589
590TEST_F(FormatTest, AlignsPipes) {
591  verifyFormat(
592      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
593      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
594      "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
595  verifyFormat(
596      "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
597      "                     << aaaaaaaaaaaaaaaaaaaa;");
598  verifyFormat(
599      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
600      "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
601  verifyFormat(
602      "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
603      "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
604      "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
605  verifyFormat(
606      "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
607      "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
608      "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
609}
610
611TEST_F(FormatTest, UnderstandsEquals) {
612  verifyFormat(
613      "aaaaaaaaaaaaaaaaa =\n"
614      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
615  verifyFormat(
616      "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
617      "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
618      "}");
619  verifyFormat(
620      "if (a) {\n"
621      "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
622      "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
623      "}");
624
625  verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
626               "        100000000 + 100000000) {\n}");
627}
628
629TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
630  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
631               "    .looooooooooooooooooooooooooooooooooooooongFunction();");
632
633  verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
634               "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
635
636  verifyFormat(
637      "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
638      "                                                          Parameter2);");
639
640  verifyFormat(
641      "ShortObject->shortFunction(\n"
642      "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
643      "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
644
645  verifyFormat("loooooooooooooongFunction(\n"
646               "    LoooooooooooooongObject->looooooooooooooooongFunction());");
647
648  verifyFormat(
649      "function(LoooooooooooooooooooooooooooooooooooongObject\n"
650      "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
651
652  verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
653               "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
654               "}");
655}
656
657TEST_F(FormatTest, WrapsTemplateDeclarations) {
658  verifyFormat("template <typename T>\n"
659               "virtual void loooooooooooongFunction(int Param1, int Param2);");
660  verifyFormat(
661      "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
662      "                             int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
663  verifyFormat(
664      "template <typename T>\n"
665      "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
666      "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
667  verifyFormat(
668      "template <typename T>\n"
669      "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
670      "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
671      "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
672
673}
674
675TEST_F(FormatTest, UnderstandsTemplateParameters) {
676  verifyFormat("A<int> a;");
677  verifyFormat("A<A<A<int> > > a;");
678  verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
679  verifyFormat("bool x = a < 1 || 2 > a;");
680  verifyFormat("bool x = 5 < f<int>();");
681  verifyFormat("bool x = f<int>() > 5;");
682  verifyFormat("bool x = 5 < a<int>::x;");
683  verifyFormat("bool x = a < 4 ? a > 2 : false;");
684  verifyFormat("bool x = f() ? a < 2 : a > 2;");
685
686  verifyGoogleFormat("A<A<int>> a;");
687  verifyGoogleFormat("A<A<A<int>>> a;");
688  verifyGoogleFormat("A<A<A<A<int>>>> a;");
689
690  verifyFormat("test >> a >> b;");
691  verifyFormat("test << a >> b;");
692
693  verifyFormat("f<int>();");
694  verifyFormat("template <typename T> void f() {\n}");
695}
696
697TEST_F(FormatTest, UnderstandsUnaryOperators) {
698  verifyFormat("int a = -2;");
699  verifyFormat("f(-1, -2, -3);");
700  verifyFormat("a[-1] = 5;");
701  verifyFormat("int a = 5 + -2;");
702  verifyFormat("if (i == -1) {\n}");
703  verifyFormat("if (i != -1) {\n}");
704  verifyFormat("if (i > -1) {\n}");
705  verifyFormat("if (i < -1) {\n}");
706  verifyFormat("++(a->f());");
707  verifyFormat("--(a->f());");
708  verifyFormat("if (!(a->f())) {\n}");
709
710  verifyFormat("a-- > b;");
711  verifyFormat("b ? -a : c;");
712  verifyFormat("n * sizeof char16;");
713  verifyFormat("n * alignof char16;");
714  verifyFormat("sizeof(char);");
715  verifyFormat("alignof(char);");
716
717  verifyFormat("return -1;");
718  verifyFormat("switch (a) {\n"
719               "case -1:\n"
720               "  break;\n"
721               "}");
722}
723
724TEST_F(FormatTest, UndestandsOverloadedOperators) {
725  verifyFormat("bool operator<();");
726  verifyFormat("bool operator>();");
727  verifyFormat("bool operator=();");
728  verifyFormat("bool operator==();");
729  verifyFormat("bool operator!=();");
730  verifyFormat("int operator+();");
731  verifyFormat("int operator++();");
732  verifyFormat("bool operator();");
733  verifyFormat("bool operator()();");
734  verifyFormat("bool operator[]();");
735  verifyFormat("operator bool();");
736  verifyFormat("operator SomeType<int>();");
737  verifyFormat("void *operator new(std::size_t size);");
738  verifyFormat("void *operator new[](std::size_t size);");
739  verifyFormat("void operator delete(void *ptr);");
740  verifyFormat("void operator delete[](void *ptr);");
741}
742
743TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
744  verifyFormat("int *f(int *a) {\n}");
745  verifyFormat("f(a, *a);");
746  verifyFormat("f(*a);");
747  verifyFormat("int a = b * 10;");
748  verifyFormat("int a = 10 * b;");
749  verifyFormat("int a = b * c;");
750  verifyFormat("int a += b * c;");
751  verifyFormat("int a -= b * c;");
752  verifyFormat("int a *= b * c;");
753  verifyFormat("int a /= b * c;");
754  verifyFormat("int a = *b;");
755  verifyFormat("int a = *b * c;");
756  verifyFormat("int a = b * *c;");
757  verifyFormat("int main(int argc, char **argv) {\n}");
758  verifyFormat("return 10 * b;");
759  verifyFormat("return *b * *c;");
760  verifyFormat("return a & ~b;");
761  verifyFormat("f(b ? *c : *d);");
762  verifyFormat("int a = b ? *c : *d;");
763  verifyFormat("*b = a;");
764  verifyFormat("a * ~b;");
765  verifyFormat("a * !b;");
766  verifyFormat("a * +b;");
767  verifyFormat("a * -b;");
768  verifyFormat("a * ++b;");
769  verifyFormat("a * --b;");
770
771  verifyFormat("InvalidRegions[*R] = 0;");
772
773  // FIXME: Is this desired for LLVM? Fix if not.
774  verifyFormat("A<int *> a;");
775  verifyFormat("A<int **> a;");
776  verifyFormat("A<int *, int *> a;");
777  verifyFormat("A<int **, int **> a;");
778  verifyFormat("Type *A = static_cast<Type *>(P);");
779  verifyFormat("Type *A = (Type *) P;");
780  verifyFormat("Type *A = (vector<Type *, int *>) P;");
781
782  verifyGoogleFormat("int main(int argc, char** argv) {\n}");
783  verifyGoogleFormat("A<int*> a;");
784  verifyGoogleFormat("A<int**> a;");
785  verifyGoogleFormat("A<int*, int*> a;");
786  verifyGoogleFormat("A<int**, int**> a;");
787  verifyGoogleFormat("f(b ? *c : *d);");
788  verifyGoogleFormat("int a = b ? *c : *d;");
789}
790
791TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
792  verifyFormat(
793      "int *someFunction(int LoooooooooooooooongParam1,\n"
794      "                  int LoooooooooooooooongParam2) {\n}");
795  verifyFormat(
796      "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
797      "                                   SourceLocation L, IdentifierIn *II,\n"
798      "                                   Type *T) {\n}");
799}
800
801TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
802  verifyFormat("(a)->b();");
803  verifyFormat("--a;");
804}
805
806TEST_F(FormatTest, HandlesIncludeDirectives) {
807  EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
808  EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
809  EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
810  EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
811  EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
812
813  EXPECT_EQ("#import <string>\n", format("#import <string>\n"));
814  EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n"));
815  EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n"));
816  EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
817  EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
818}
819
820
821//===----------------------------------------------------------------------===//
822// Error recovery tests.
823//===----------------------------------------------------------------------===//
824
825TEST_F(FormatTest, IncorrectAccessSpecifier) {
826  verifyFormat("public:");
827  verifyFormat("class A {\n"
828               "public\n"
829               "  void f() {\n"
830               "  }\n"
831               "};");
832  verifyFormat("public\n"
833               "int qwerty;");
834  verifyFormat("public\n"
835               "B {\n"
836               "};");
837  verifyFormat("public\n"
838               "{\n"
839               "};");
840  verifyFormat("public\n"
841               "B {\n"
842               "  int x;\n"
843               "};");
844}
845
846TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
847  verifyFormat("{");
848}
849
850TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
851  verifyFormat("do {\n"
852               "};");
853  verifyFormat("do {\n"
854               "};\n"
855               "f();");
856  verifyFormat("do {\n"
857               "}\n"
858               "wheeee(fun);");
859  verifyFormat("do {\n"
860               "  f();\n"
861               "};");
862}
863
864TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
865  verifyFormat("namespace {\n"
866               "class Foo {\n"
867               "  Foo  ( };\n"
868               "}  // comment");
869}
870
871TEST_F(FormatTest, IncorrectCodeErrorDetection) {
872  EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n"));
873  EXPECT_EQ("{\n  {\n}\n", format("{\n  {\n}\n"));
874  EXPECT_EQ("{\n  {\n  }\n", format("{\n  {\n  }\n"));
875  EXPECT_EQ("{\n  {\n    }\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
876
877  FormatStyle Style = getLLVMStyle();
878  Style.ColumnLimit = 10;
879  EXPECT_EQ("{\n"
880            "    {\n"
881            " breakme(\n"
882            "     qwe);\n"
883            "}\n", format("{\n"
884                          "    {\n"
885                          " breakme(qwe);\n"
886                          "}\n", Style));
887
888}
889
890TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
891  verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
892  EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
893            format("-(NSUInteger)indexOfObject:(id)anObject;"));
894  EXPECT_EQ("- (NSInteger)Mthod1;",
895            format("-(NSInteger)Mthod1;"));
896  EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
897  EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
898            format("-(NSInteger)Method3:(id)anObject;"));
899  EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
900            format("-(NSInteger)Method4:(id)anObject;"));
901  EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
902            format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
903  EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
904            format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
905  EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
906            format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
907
908  // Very long objectiveC method declaration.
909  EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n    "
910            "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n    "
911            "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n    "
912            "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n    "
913            "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n    "
914            "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
915
916            format("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
917                   "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
918                   "outRange2:(NSRange) out_range2  outRange3:(NSRange) out_range3  "
919                   "outRange4:(NSRange) out_range4  outRange5:(NSRange) out_range5 "
920                   "outRange6:(NSRange) out_range6  outRange7:(NSRange) out_range7  "
921                   "outRange8:(NSRange) out_range8  outRange9:(NSRange) out_range9;"));
922}
923
924}  // end namespace tooling
925}  // end namespace clang
926