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