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