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