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