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 
15 namespace clang {
16 namespace format {
17 
18 class FormatTest : public ::testing::Test {
19 protected:
20   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
21                      const FormatStyle &Style) {
22     RewriterTestContext Context;
23     FileID ID = Context.createInMemoryFile("input.cc", Code);
24     SourceLocation Start =
25         Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset);
26     std::vector<CharSourceRange> Ranges(
27         1,
28         CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length)));
29     LangOptions LangOpts;
30     LangOpts.CPlusPlus = 1;
31     LangOpts.CPlusPlus11 = 1;
32     LangOpts.ObjC1 = 1;
33     LangOpts.ObjC2 = 1;
34     Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts);
35     tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
36                                              Ranges);
37     EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
38     return Context.getRewrittenText(ID);
39   }
40 
41   std::string format(llvm::StringRef Code,
42                      const FormatStyle &Style = getLLVMStyle()) {
43     return format(Code, 0, Code.size(), Style);
44   }
45 
46   std::string messUp(llvm::StringRef Code) {
47     std::string MessedUp(Code.str());
48     bool InComment = false;
49     bool JustReplacedNewline = false;
50     for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
51       if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
52         if (JustReplacedNewline)
53           MessedUp[i - 1] = '\n';
54         InComment = true;
55       } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
56         MessedUp[i] = ' ';
57       } else if (MessedUp[i] == '\n') {
58         if (InComment) {
59           InComment = false;
60         } else {
61           JustReplacedNewline = true;
62           MessedUp[i] = ' ';
63         }
64       } else if (MessedUp[i] != ' ') {
65         JustReplacedNewline = false;
66       }
67     }
68     return MessedUp;
69   }
70 
71   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
72     FormatStyle Style = getLLVMStyle();
73     Style.ColumnLimit = ColumnLimit;
74     return Style;
75   }
76 
77   void verifyFormat(llvm::StringRef Code,
78                     const FormatStyle &Style = getLLVMStyle()) {
79     EXPECT_EQ(Code.str(), format(messUp(Code), Style));
80   }
81 
82   void verifyGoogleFormat(llvm::StringRef Code) {
83     verifyFormat(Code, getGoogleStyle());
84   }
85 };
86 
87 //===----------------------------------------------------------------------===//
88 // Basic function tests.
89 //===----------------------------------------------------------------------===//
90 
91 TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) {
92   EXPECT_EQ(";", format(";"));
93 }
94 
95 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
96   EXPECT_EQ("int i;", format("  int i;"));
97   EXPECT_EQ("\nint i;", format(" \n\t \r  int i;"));
98   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
99   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
100 }
101 
102 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
103   EXPECT_EQ("int i;", format("int\ni;"));
104 }
105 
106 TEST_F(FormatTest, FormatsNestedBlockStatements) {
107   EXPECT_EQ("{\n  {\n    {\n    }\n  }\n}", format("{{{}}}"));
108 }
109 
110 TEST_F(FormatTest, FormatsNestedCall) {
111   verifyFormat("Method(f1, f2(f3));");
112   verifyFormat("Method(f1(f2, f3()));");
113 }
114 
115 //===----------------------------------------------------------------------===//
116 // Tests for control statements.
117 //===----------------------------------------------------------------------===//
118 
119 TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
120   verifyFormat("if (true)\n  f();\ng();");
121   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
122   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
123   verifyFormat("if (a)\n"
124                "  // comment\n"
125                "  f();");
126 }
127 
128 TEST_F(FormatTest, ParseIfElse) {
129   verifyFormat("if (true)\n"
130                "  if (true)\n"
131                "    if (true)\n"
132                "      f();\n"
133                "    else\n"
134                "      g();\n"
135                "  else\n"
136                "    h();\n"
137                "else\n"
138                "  i();");
139   verifyFormat("if (true)\n"
140                "  if (true)\n"
141                "    if (true) {\n"
142                "      if (true)\n"
143                "        f();\n"
144                "    } else {\n"
145                "      g();\n"
146                "    }\n"
147                "  else\n"
148                "    h();\n"
149                "else {\n"
150                "  i();\n"
151                "}");
152 }
153 
154 TEST_F(FormatTest, ElseIf) {
155   verifyFormat("if (a) {\n"
156                "} else if (b) {\n"
157                "}");
158   verifyFormat("if (a)\n"
159                "  f();\n"
160                "else if (b)\n"
161                "  g();\n"
162                "else\n"
163                "  h();");
164 }
165 
166 TEST_F(FormatTest, FormatsForLoop) {
167   verifyFormat(
168       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
169       "     ++VeryVeryLongLoopVariable)\n"
170       "  ;");
171   verifyFormat("for (;;)\n"
172                "  f();");
173   verifyFormat("for (;;) {\n"
174                "}");
175   verifyFormat("for (;;) {\n"
176                "  f();\n"
177                "}");
178 
179   verifyFormat(
180       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
181       "                                          E = UnwrappedLines.end();\n"
182       "     I != E; ++I) {\n}");
183 
184   verifyFormat(
185       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
186       "     ++IIIII) {\n}");
187 }
188 
189 TEST_F(FormatTest, FormatsWhileLoop) {
190   verifyFormat("while (true) {\n}");
191   verifyFormat("while (true)\n"
192                "  f();");
193   verifyFormat("while () {\n"
194                "}");
195   verifyFormat("while () {\n"
196                "  f();\n"
197                "}");
198 }
199 
200 TEST_F(FormatTest, FormatsDoWhile) {
201   verifyFormat("do {\n"
202                "  do_something();\n"
203                "} while (something());");
204   verifyFormat("do\n"
205                "  do_something();\n"
206                "while (something());");
207 }
208 
209 TEST_F(FormatTest, FormatsSwitchStatement) {
210   verifyFormat("switch (x) {\n"
211                "case 1:\n"
212                "  f();\n"
213                "  break;\n"
214                "case kFoo:\n"
215                "case ns::kBar:\n"
216                "case kBaz:\n"
217                "  break;\n"
218                "default:\n"
219                "  g();\n"
220                "  break;\n"
221                "}");
222   verifyFormat("switch (x) {\n"
223                "case 1: {\n"
224                "  f();\n"
225                "  break;\n"
226                "}\n"
227                "}");
228   verifyFormat("switch (test)\n"
229                "  ;");
230   verifyGoogleFormat("switch (x) {\n"
231                      "  case 1:\n"
232                      "    f();\n"
233                      "    break;\n"
234                      "  case kFoo:\n"
235                      "  case ns::kBar:\n"
236                      "  case kBaz:\n"
237                      "    break;\n"
238                      "  default:\n"
239                      "    g();\n"
240                      "    break;\n"
241                      "}");
242   verifyGoogleFormat("switch (x) {\n"
243                      "  case 1: {\n"
244                      "    f();\n"
245                      "    break;\n"
246                      "  }\n"
247                      "}");
248   verifyGoogleFormat("switch (test)\n"
249                      "    ;");
250 }
251 
252 TEST_F(FormatTest, FormatsLabels) {
253   verifyFormat("void f() {\n"
254                "  some_code();\n"
255                "test_label:\n"
256                "  some_other_code();\n"
257                "  {\n"
258                "    some_more_code();\n"
259                "  another_label:\n"
260                "    some_more_code();\n"
261                "  }\n"
262                "}");
263   verifyFormat("some_code();\n"
264                "test_label:\n"
265                "some_other_code();");
266 }
267 
268 //===----------------------------------------------------------------------===//
269 // Tests for comments.
270 //===----------------------------------------------------------------------===//
271 
272 TEST_F(FormatTest, UnderstandsSingleLineComments) {
273   verifyFormat("// line 1\n"
274                "// line 2\n"
275                "void f() {\n}\n");
276 
277   verifyFormat("void f() {\n"
278                "  // Doesn't do anything\n"
279                "}");
280 
281   verifyFormat("int i // This is a fancy variable\n"
282                "    = 5;");
283 
284   verifyFormat("enum E {\n"
285                "  // comment\n"
286                "  VAL_A, // comment\n"
287                "  VAL_B\n"
288                "};");
289 
290   verifyFormat(
291       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
292       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
293 
294   EXPECT_EQ("int i; // single line trailing comment",
295             format("int i;\\\n// single line trailing comment"));
296 
297   verifyGoogleFormat("int a;  // Trailing comment.");
298 }
299 
300 TEST_F(FormatTest, UnderstandsMultiLineComments) {
301   verifyFormat("f(/*test=*/ true);");
302 }
303 
304 //===----------------------------------------------------------------------===//
305 // Tests for classes, namespaces, etc.
306 //===----------------------------------------------------------------------===//
307 
308 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
309   verifyFormat("class A {\n};");
310 }
311 
312 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
313   verifyFormat("class A {\n"
314                "public:\n"
315                "protected:\n"
316                "private:\n"
317                "  void f() {\n"
318                "  }\n"
319                "};");
320   verifyGoogleFormat("class A {\n"
321                      " public:\n"
322                      " protected:\n"
323                      " private:\n"
324                      "  void f() {\n"
325                      "  }\n"
326                      "};");
327 }
328 
329 TEST_F(FormatTest, FormatsDerivedClass) {
330   verifyFormat("class A : public B {\n"
331                "};");
332   verifyFormat("class A : public ::B {\n"
333                "};");
334 }
335 
336 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
337   verifyFormat("class A {\n"
338                "} a, b;");
339   verifyFormat("struct A {\n"
340                "} a, b;");
341 }
342 
343 TEST_F(FormatTest, FormatsEnum) {
344   verifyFormat("enum {\n"
345                "  Zero,\n"
346                "  One = 1,\n"
347                "  Two = One + 1,\n"
348                "  Three = (One + Two),\n"
349                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
350                "  Five = (One, Two, Three, Four, 5)\n"
351                "};");
352   verifyFormat("enum Enum {\n"
353                "};");
354   verifyFormat("enum {\n"
355                "};");
356 }
357 
358 TEST_F(FormatTest, FormatsNamespaces) {
359   verifyFormat("namespace some_namespace {\n"
360                "class A {\n"
361                "};\n"
362                "void f() {\n"
363                "  f();\n"
364                "}\n"
365                "}");
366   verifyFormat("namespace {\n"
367                "class A {\n"
368                "};\n"
369                "void f() {\n"
370                "  f();\n"
371                "}\n"
372                "}");
373   verifyFormat("inline namespace X {\n"
374                "class A {\n"
375                "};\n"
376                "void f() {\n"
377                "  f();\n"
378                "}\n"
379                "}");
380   verifyFormat("using namespace some_namespace;\n"
381                "class A {\n"
382                "};\n"
383                "void f() {\n"
384                "  f();\n"
385                "}");
386 }
387 
388 TEST_F(FormatTest, FormatTryCatch) {
389   verifyFormat("try {\n"
390                "  throw a * b;\n"
391                "}\n"
392                "catch (int a) {\n"
393                "  // Do nothing.\n"
394                "}\n"
395                "catch (...) {\n"
396                "  exit(42);\n"
397                "}");
398 
399   // Function-level try statements.
400   verifyFormat("int f() try {\n"
401                "  return 4;\n"
402                "}\n"
403                "catch (...) {\n"
404                "  return 5;\n"
405                "}");
406   verifyFormat("class A {\n"
407                "  int a;\n"
408                "  A() try : a(0) {\n"
409                "  }\n"
410                "  catch (...) {\n"
411                "    throw;\n"
412                "  }\n"
413                "};\n");
414 }
415 
416 TEST_F(FormatTest, FormatObjCTryCatch) {
417   verifyFormat("@try {\n"
418                "  f();\n"
419                "}\n"
420                "@catch (NSException e) {\n"
421                "  @throw;\n"
422                "}\n"
423                "@finally {\n"
424                "  exit(42);\n"
425                "}");
426 }
427 
428 TEST_F(FormatTest, FormatObjCInterface) {
429   verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
430                "@public\n"
431                "  int field1;\n"
432                "@protected\n"
433                "  int field2;\n"
434                "@private\n"
435                "  int field3;\n"
436                "@package\n"
437                "  int field4;\n"
438                "}\n"
439                "+ (id)init;\n"
440                "@end");
441 
442   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
443                      " @public\n"
444                      "  int field1;\n"
445                      " @protected\n"
446                      "  int field2;\n"
447                      " @private\n"
448                      "  int field3;\n"
449                      " @package\n"
450                      "  int field4;\n"
451                      "}\n"
452                      "+ (id)init;\n"
453                      "@end");
454 }
455 
456 TEST_F(FormatTest, StaticInitializers) {
457   verifyFormat("static SomeClass SC = { 1, 'a' };");
458 
459   // FIXME: Format like enums if the static initializer does not fit on a line.
460   verifyFormat(
461       "static SomeClass WithALoooooooooooooooooooongName = {\n"
462       "  100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
463 }
464 
465 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
466   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
467                "                      \\\n"
468                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
469 }
470 
471 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
472   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
473                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
474 }
475 
476 TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) {
477   EXPECT_EQ("#\n;", format("#;"));
478   verifyFormat("#\n;\n;\n;");
479 }
480 
481 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
482   EXPECT_EQ("#line 42 \"test\"\n",
483             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
484   EXPECT_EQ("#define A  \\\n  B\n",
485             format("#  \\\n define  \\\n    A  \\\n       B\n",
486                    getLLVMStyleWithColumns(12)));
487 }
488 
489 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
490   EXPECT_EQ("#line 42 \"test\"",
491             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
492   EXPECT_EQ("#define A  \\\n  B",
493             format("#  \\\n define  \\\n    A  \\\n       B",
494                    getLLVMStyleWithColumns(12)));
495 }
496 
497 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
498   // If the macro fits in one line, we still do not get the full
499   // line, as only the next line decides whether we need an escaped newline and
500   // thus use the last column.
501   verifyFormat("#define A(B)", getLLVMStyleWithColumns(13));
502 
503   verifyFormat("#define A( \\\n    B)", getLLVMStyleWithColumns(12));
504   verifyFormat("#define AA(\\\n    B)", getLLVMStyleWithColumns(12));
505   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
506 }
507 
508 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
509   EXPECT_EQ("// 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",
515             format("  // some comment\n"
516                    "  #include \"a.h\"\n"
517                    "#define A(A,\\\n"
518                    "    B)\n"
519                    "    #include \"b.h\"\n"
520                    " // some comment\n", getLLVMStyleWithColumns(13)));
521 }
522 
523 TEST_F(FormatTest, LayoutSingleHash) {
524   EXPECT_EQ("#\na;", format("#\na;"));
525 }
526 
527 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
528   EXPECT_EQ("#define A    \\\n"
529             "  c;         \\\n"
530             "  e;\n"
531             "f;", format("#define A c; e;\n"
532                          "f;", getLLVMStyleWithColumns(14)));
533 }
534 
535 TEST_F(FormatTest, LayoutRemainingTokens) {
536   EXPECT_EQ("{\n}", format("{}"));
537 }
538 
539 TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
540   EXPECT_EQ("# define A\\\n  b;",
541             format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
542 }
543 
544 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
545   EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;"));
546 }
547 
548 TEST_F(FormatTest, HashInMacroDefinition) {
549   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
550   verifyFormat("#define A \\\n"
551                "  {       \\\n"
552                "    f(#c);\\\n"
553                "  }", getLLVMStyleWithColumns(11));
554 
555   verifyFormat("#define A(X)         \\\n"
556                "  void function##X()", getLLVMStyleWithColumns(22));
557 
558   verifyFormat("#define A(a, b, c)   \\\n"
559                "  void a##b##c()", getLLVMStyleWithColumns(22));
560 
561   verifyFormat("#define A            \\\n"
562                "  void # ## #", getLLVMStyleWithColumns(22));
563 }
564 
565 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
566   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
567 }
568 
569 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
570   verifyFormat("{\n  {\n    a #c;\n  }\n}");
571 }
572 
573 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
574   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
575             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
576   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
577             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
578 }
579 
580 TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) {
581   EXPECT_EQ(
582       "#define A \\\n  int i;  \\\n  int j;",
583       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
584 }
585 
586 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
587   verifyFormat("#define A \\\n"
588                "  int v(  \\\n"
589                "      a); \\\n"
590                "  int i;", getLLVMStyleWithColumns(11));
591 }
592 
593 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
594   EXPECT_EQ(
595       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
596       "                      \\\n"
597       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
598       "\n"
599       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
600       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
601       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
602              "\\\n"
603              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
604              "  \n"
605              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
606              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
607 }
608 
609 //===----------------------------------------------------------------------===//
610 // Line break tests.
611 //===----------------------------------------------------------------------===//
612 
613 TEST_F(FormatTest, FormatsFunctionDefinition) {
614   verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g,"
615                " int h, int j, int f,\n"
616                "       int c, int ddddddddddddd) {\n"
617                "}");
618 }
619 
620 TEST_F(FormatTest, FormatsAwesomeMethodCall) {
621   verifyFormat(
622       "SomeLongMethodName(SomeReallyLongMethod(\n"
623       "    CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
624       "                   SecondLongCall(parameter));");
625 }
626 
627 TEST_F(FormatTest, ConstructorInitializers) {
628   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}");
629 
630   verifyFormat(
631       "SomeClass::Constructor()\n"
632       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
633       "}");
634 
635   verifyFormat(
636       "SomeClass::Constructor()\n"
637       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
638       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n"
639       "}");
640 
641   verifyFormat("Constructor()\n"
642                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
643                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
644                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
645                "      aaaaaaaaaaaaaaaaaaaaaaa() {\n"
646                "}");
647 
648   // Here a line could be saved by splitting the second initializer onto two
649   // lines, but that is not desireable.
650   verifyFormat("Constructor()\n"
651                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
652                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
653                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
654                "}");
655 
656   verifyGoogleFormat("MyClass::MyClass(int var)\n"
657                      "    : some_var_(var),  // 4 space indent\n"
658                      "      some_other_var_(var + 1) {  // lined up\n"
659                      "}");
660 }
661 
662 TEST_F(FormatTest, BreaksAsHighAsPossible) {
663   verifyFormat(
664       "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
665       "    (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
666       "  f();");
667 }
668 
669 TEST_F(FormatTest, BreaksDesireably) {
670   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
671                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
672                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
673 
674   verifyFormat(
675       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
676       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
677 
678   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
679                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
680                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
681 
682   verifyFormat(
683       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
684       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
685       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
686       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
687 
688   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
689                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
690 
691   verifyFormat(
692       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
693       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
694 
695   // This test case breaks on an incorrect memoization, i.e. an optimization not
696   // taking into account the StopAt value.
697   verifyFormat(
698       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
699       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
700       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
701       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
702 
703   verifyFormat("{\n  {\n    {\n"
704                "      Annotation.SpaceRequiredBefore =\n"
705                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
706                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
707                "    }\n  }\n}");
708 }
709 
710 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
711   verifyFormat(
712       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
713       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
714   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
715                "    ccccccccccccccccccccccccc) {\n}");
716   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
717                "    ccccccccccccccccccccccccc) {\n}");
718   verifyFormat(
719       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
720       "    ccccccccccccccccccccccccc) {\n}");
721 }
722 
723 TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
724   verifyFormat(
725       "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
726       "                                    SI->getAlignment(),\n"
727       "                                    SI->getPointerAddressSpaceee());\n");
728   verifyFormat(
729       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
730       "                                Line.Tokens.front().Tok.getLocation(),\n"
731       "                                Line.Tokens.back().Tok.getLocation());");
732 }
733 
734 TEST_F(FormatTest, AlignsAfterAssignments) {
735   verifyFormat(
736       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
737       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
738   verifyFormat(
739       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
740       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
741   verifyFormat(
742       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
743       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
744   verifyFormat(
745       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
746       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
747   verifyFormat(
748       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
749       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
750       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
751 }
752 
753 TEST_F(FormatTest, AlignsAfterReturn) {
754   verifyFormat(
755       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
756       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
757   verifyFormat(
758       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
759       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
760 }
761 
762 TEST_F(FormatTest, AlignsStringLiterals) {
763   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
764                "                                      \"short literal\");");
765   verifyFormat(
766       "looooooooooooooooooooooooongFunction(\n"
767       "    \"short literal\"\n"
768       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
769 }
770 
771 TEST_F(FormatTest, AlignsPipes) {
772   verifyFormat(
773       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
774       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
775       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
776   verifyFormat(
777       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
778       "                     << aaaaaaaaaaaaaaaaaaaa;");
779   verifyFormat(
780       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
781       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
782   verifyFormat(
783       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
784       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
785       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
786   verifyFormat(
787       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
788       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
789       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
790 }
791 
792 TEST_F(FormatTest, UnderstandsEquals) {
793   verifyFormat(
794       "aaaaaaaaaaaaaaaaa =\n"
795       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
796   verifyFormat(
797       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
798       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
799       "}");
800   verifyFormat(
801       "if (a) {\n"
802       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
803       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
804       "}");
805 
806   verifyFormat(
807       "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
808       "                                                           10000000) {\n"
809       "}");
810 }
811 
812 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
813   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
814                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
815 
816   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
817                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
818 
819   verifyFormat(
820       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
821       "                                                          Parameter2);");
822 
823   verifyFormat(
824       "ShortObject->shortFunction(\n"
825       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
826       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
827 
828   verifyFormat("loooooooooooooongFunction(\n"
829                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
830 
831   verifyFormat(
832       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
833       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
834 
835   // Here, it is not necessary to wrap at "." or "->".
836   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
837                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
838                "}");
839   verifyFormat(
840       "aaaaaaaaaaa->aaaaaaaaa(\n"
841       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
842       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
843 }
844 
845 TEST_F(FormatTest, WrapsTemplateDeclarations) {
846   verifyFormat("template <typename T>\n"
847                "virtual void loooooooooooongFunction(int Param1, int Param2);");
848   verifyFormat(
849       "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
850       "                             int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
851   verifyFormat(
852       "template <typename T>\n"
853       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
854       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
855   verifyFormat(
856       "template <typename T>\n"
857       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
858       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
859       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
860 
861 }
862 
863 TEST_F(FormatTest, UnderstandsTemplateParameters) {
864   verifyFormat("A<int> a;");
865   verifyFormat("A<A<A<int> > > a;");
866   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
867   verifyFormat("bool x = a < 1 || 2 > a;");
868   verifyFormat("bool x = 5 < f<int>();");
869   verifyFormat("bool x = f<int>() > 5;");
870   verifyFormat("bool x = 5 < a<int>::x;");
871   verifyFormat("bool x = a < 4 ? a > 2 : false;");
872   verifyFormat("bool x = f() ? a < 2 : a > 2;");
873 
874   verifyGoogleFormat("A<A<int>> a;");
875   verifyGoogleFormat("A<A<A<int>>> a;");
876   verifyGoogleFormat("A<A<A<A<int>>>> a;");
877 
878   verifyFormat("test >> a >> b;");
879   verifyFormat("test << a >> b;");
880 
881   verifyFormat("f<int>();");
882   verifyFormat("template <typename T> void f() {\n}");
883 }
884 
885 TEST_F(FormatTest, UnderstandsUnaryOperators) {
886   verifyFormat("int a = -2;");
887   verifyFormat("f(-1, -2, -3);");
888   verifyFormat("a[-1] = 5;");
889   verifyFormat("int a = 5 + -2;");
890   verifyFormat("if (i == -1) {\n}");
891   verifyFormat("if (i != -1) {\n}");
892   verifyFormat("if (i > -1) {\n}");
893   verifyFormat("if (i < -1) {\n}");
894   verifyFormat("++(a->f());");
895   verifyFormat("--(a->f());");
896   verifyFormat("if (!(a->f())) {\n}");
897 
898   verifyFormat("a-- > b;");
899   verifyFormat("b ? -a : c;");
900   verifyFormat("n * sizeof char16;");
901   verifyFormat("n * alignof char16;");
902   verifyFormat("sizeof(char);");
903   verifyFormat("alignof(char);");
904 
905   verifyFormat("return -1;");
906   verifyFormat("switch (a) {\n"
907                "case -1:\n"
908                "  break;\n"
909                "}");
910 }
911 
912 TEST_F(FormatTest, UndestandsOverloadedOperators) {
913   verifyFormat("bool operator<();");
914   verifyFormat("bool operator>();");
915   verifyFormat("bool operator=();");
916   verifyFormat("bool operator==();");
917   verifyFormat("bool operator!=();");
918   verifyFormat("int operator+();");
919   verifyFormat("int operator++();");
920   verifyFormat("bool operator();");
921   verifyFormat("bool operator()();");
922   verifyFormat("bool operator[]();");
923   verifyFormat("operator bool();");
924   verifyFormat("operator SomeType<int>();");
925   verifyFormat("void *operator new(std::size_t size);");
926   verifyFormat("void *operator new[](std::size_t size);");
927   verifyFormat("void operator delete(void *ptr);");
928   verifyFormat("void operator delete[](void *ptr);");
929 }
930 
931 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
932   verifyFormat("int *f(int *a) {\n}");
933   verifyFormat("f(a, *a);");
934   verifyFormat("f(*a);");
935   verifyFormat("int a = b * 10;");
936   verifyFormat("int a = 10 * b;");
937   verifyFormat("int a = b * c;");
938   verifyFormat("int a += b * c;");
939   verifyFormat("int a -= b * c;");
940   verifyFormat("int a *= b * c;");
941   verifyFormat("int a /= b * c;");
942   verifyFormat("int a = *b;");
943   verifyFormat("int a = *b * c;");
944   verifyFormat("int a = b * *c;");
945   verifyFormat("int main(int argc, char **argv) {\n}");
946   verifyFormat("return 10 * b;");
947   verifyFormat("return *b * *c;");
948   verifyFormat("return a & ~b;");
949   verifyFormat("f(b ? *c : *d);");
950   verifyFormat("int a = b ? *c : *d;");
951   verifyFormat("*b = a;");
952   verifyFormat("a * ~b;");
953   verifyFormat("a * !b;");
954   verifyFormat("a * +b;");
955   verifyFormat("a * -b;");
956   verifyFormat("a * ++b;");
957   verifyFormat("a * --b;");
958 
959   verifyFormat("InvalidRegions[*R] = 0;");
960 
961   // FIXME: Is this desired for LLVM? Fix if not.
962   verifyFormat("A<int *> a;");
963   verifyFormat("A<int **> a;");
964   verifyFormat("A<int *, int *> a;");
965   verifyFormat("A<int **, int **> a;");
966   verifyFormat("Type *A = static_cast<Type *>(P);");
967   verifyFormat("Type *A = (Type *) P;");
968   verifyFormat("Type *A = (vector<Type *, int *>) P;");
969 
970   verifyGoogleFormat("int main(int argc, char** argv) {\n}");
971   verifyGoogleFormat("A<int*> a;");
972   verifyGoogleFormat("A<int**> a;");
973   verifyGoogleFormat("A<int*, int*> a;");
974   verifyGoogleFormat("A<int**, int**> a;");
975   verifyGoogleFormat("f(b ? *c : *d);");
976   verifyGoogleFormat("int a = b ? *c : *d;");
977 }
978 
979 TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) {
980   verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n"
981                "                  int LoooooooooooooooongParam2) {\n}");
982   verifyFormat(
983       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
984       "                                   SourceLocation L, IdentifierIn *II,\n"
985       "                                   Type *T) {\n}");
986 }
987 
988 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
989   verifyFormat("(a)->b();");
990   verifyFormat("--a;");
991 }
992 
993 TEST_F(FormatTest, HandlesIncludeDirectives) {
994   EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
995   EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
996   EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
997   EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
998   EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
999 
1000   EXPECT_EQ("#import <string>\n", format("#import <string>\n"));
1001   EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n"));
1002   EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n"));
1003   EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1004   EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n"));
1005 }
1006 
1007 //===----------------------------------------------------------------------===//
1008 // Error recovery tests.
1009 //===----------------------------------------------------------------------===//
1010 
1011 TEST_F(FormatTest, IncorrectAccessSpecifier) {
1012   verifyFormat("public:");
1013   verifyFormat("class A {\n"
1014                "public\n"
1015                "  void f() {\n"
1016                "  }\n"
1017                "};");
1018   verifyFormat("public\n"
1019                "int qwerty;");
1020   verifyFormat("public\n"
1021                "B {\n"
1022                "}");
1023   verifyFormat("public\n"
1024                "{\n"
1025                "}");
1026   verifyFormat("public\n"
1027                "B {\n"
1028                "  int x;\n"
1029                "}");
1030 }
1031 
1032 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
1033   verifyFormat("{");
1034 }
1035 
1036 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
1037   verifyFormat("do {\n"
1038                "}");
1039   verifyFormat("do {\n"
1040                "}\n"
1041                "f();");
1042   verifyFormat("do {\n"
1043                "}\n"
1044                "wheeee(fun);");
1045   verifyFormat("do {\n"
1046                "  f();\n"
1047                "}");
1048 }
1049 
1050 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
1051   verifyFormat("namespace {\n"
1052                "class Foo {  Foo  ( }; }  // comment");
1053 }
1054 
1055 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
1056   EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n"));
1057   EXPECT_EQ("{\n  {\n}\n", format("{\n  {\n}\n"));
1058   EXPECT_EQ("{\n  {\n  }\n", format("{\n  {\n  }\n"));
1059   EXPECT_EQ("{\n  {\n    }\n  }\n}\n", format("{\n  {\n    }\n  }\n}\n"));
1060 
1061   FormatStyle Style = getLLVMStyle();
1062   Style.ColumnLimit = 10;
1063   EXPECT_EQ("{\n"
1064             "    {\n"
1065             " breakme(\n"
1066             "     qwe);\n"
1067             "}\n", format("{\n"
1068                           "    {\n"
1069                           " breakme(qwe);\n"
1070                           "}\n", Style));
1071 
1072 }
1073 
1074 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
1075   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
1076   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
1077             format("-(NSUInteger)indexOfObject:(id)anObject;"));
1078   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
1079   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
1080   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
1081             format("-(NSInteger)Method3:(id)anObject;"));
1082   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
1083             format("-(NSInteger)Method4:(id)anObject;"));
1084   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
1085             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
1086   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
1087             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
1088   EXPECT_EQ(
1089       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
1090       format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
1091 
1092   // Very long objectiveC method declaration.
1093   EXPECT_EQ(
1094       "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n    "
1095       "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n    "
1096       "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n    "
1097       "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n    "
1098       "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n    "
1099       "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;",
1100       format(
1101           "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range "
1102           "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 "
1103           "outRange2:(NSRange) out_range2  outRange3:(NSRange) out_range3  "
1104           "outRange4:(NSRange) out_range4  outRange5:(NSRange) out_range5 "
1105           "outRange6:(NSRange) out_range6  outRange7:(NSRange) out_range7  "
1106           "outRange8:(NSRange) out_range8  outRange9:(NSRange) out_range9;"));
1107 }
1108 
1109 TEST_F(FormatTest, ObjCAt) {
1110   verifyFormat("@autoreleasepool");
1111   verifyFormat("@catch");
1112   verifyFormat("@class");
1113   verifyFormat("@compatibility_alias");
1114   verifyFormat("@defs");
1115   verifyFormat("@dynamic");
1116   verifyFormat("@encode");
1117   verifyFormat("@end");
1118   verifyFormat("@finally");
1119   verifyFormat("@implementation");
1120   verifyFormat("@import");
1121   verifyFormat("@interface");
1122   verifyFormat("@optional");
1123   verifyFormat("@package");
1124   verifyFormat("@private");
1125   verifyFormat("@property");
1126   verifyFormat("@protected");
1127   verifyFormat("@protocol");
1128   verifyFormat("@public");
1129   verifyFormat("@required");
1130   verifyFormat("@selector");
1131   verifyFormat("@synchronized");
1132   verifyFormat("@synthesize");
1133   verifyFormat("@throw");
1134   verifyFormat("@try");
1135 
1136   EXPECT_EQ("@interface", format("@ interface"));
1137 
1138   // The precise formatting of this doesn't matter, nobody writes code like
1139   // this.
1140   verifyFormat("@ /*foo*/ interface");
1141 }
1142 
1143 } // end namespace tooling
1144 } // end namespace clang
1145