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