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