Home | History | Annotate | Download | only in Format
      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 "FormatTestUtils.h"
     11 #include "clang/Format/Format.h"
     12 #include "llvm/Support/Debug.h"
     13 #include "gtest/gtest.h"
     14 
     15 #define DEBUG_TYPE "format-test"
     16 
     17 namespace clang {
     18 namespace format {
     19 namespace {
     20 
     21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
     22 
     23 class FormatTest : public ::testing::Test {
     24 protected:
     25   enum IncompleteCheck {
     26     IC_ExpectComplete,
     27     IC_ExpectIncomplete,
     28     IC_DoNotCheck
     29   };
     30 
     31   std::string format(llvm::StringRef Code,
     32                      const FormatStyle &Style = getLLVMStyle(),
     33                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
     34     DEBUG(llvm::errs() << "---\n");
     35     DEBUG(llvm::errs() << Code << "\n\n");
     36     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
     37     bool IncompleteFormat = false;
     38     tooling::Replacements Replaces =
     39         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
     40     if (CheckIncomplete != IC_DoNotCheck) {
     41       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
     42       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
     43     }
     44     ReplacementCount = Replaces.size();
     45     std::string Result = applyAllReplacements(Code, Replaces);
     46     EXPECT_NE("", Result);
     47     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
     48     return Result;
     49   }
     50 
     51   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
     52     FormatStyle Style = getLLVMStyle();
     53     Style.ColumnLimit = ColumnLimit;
     54     return Style;
     55   }
     56 
     57   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
     58     FormatStyle Style = getGoogleStyle();
     59     Style.ColumnLimit = ColumnLimit;
     60     return Style;
     61   }
     62 
     63   void verifyFormat(llvm::StringRef Code,
     64                     const FormatStyle &Style = getLLVMStyle()) {
     65     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
     66   }
     67 
     68   void verifyIncompleteFormat(llvm::StringRef Code,
     69                               const FormatStyle &Style = getLLVMStyle()) {
     70     EXPECT_EQ(Code.str(),
     71               format(test::messUp(Code), Style, IC_ExpectIncomplete));
     72   }
     73 
     74   void verifyGoogleFormat(llvm::StringRef Code) {
     75     verifyFormat(Code, getGoogleStyle());
     76   }
     77 
     78   void verifyIndependentOfContext(llvm::StringRef text) {
     79     verifyFormat(text);
     80     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
     81   }
     82 
     83   /// \brief Verify that clang-format does not crash on the given input.
     84   void verifyNoCrash(llvm::StringRef Code,
     85                      const FormatStyle &Style = getLLVMStyle()) {
     86     format(Code, Style, IC_DoNotCheck);
     87   }
     88 
     89   int ReplacementCount;
     90 };
     91 
     92 TEST_F(FormatTest, MessUp) {
     93   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
     94   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
     95   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
     96   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
     97   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
     98 }
     99 
    100 //===----------------------------------------------------------------------===//
    101 // Basic function tests.
    102 //===----------------------------------------------------------------------===//
    103 
    104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
    105   EXPECT_EQ(";", format(";"));
    106 }
    107 
    108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
    109   EXPECT_EQ("int i;", format("  int i;"));
    110   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
    111   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
    112   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
    113 }
    114 
    115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
    116   EXPECT_EQ("int i;", format("int\ni;"));
    117 }
    118 
    119 TEST_F(FormatTest, FormatsNestedBlockStatements) {
    120   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
    121 }
    122 
    123 TEST_F(FormatTest, FormatsNestedCall) {
    124   verifyFormat("Method(f1, f2(f3));");
    125   verifyFormat("Method(f1(f2, f3()));");
    126   verifyFormat("Method(f1(f2, (f3())));");
    127 }
    128 
    129 TEST_F(FormatTest, NestedNameSpecifiers) {
    130   verifyFormat("vector<::Type> v;");
    131   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
    132   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
    133   verifyFormat("bool a = 2 < ::SomeFunction();");
    134 }
    135 
    136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
    137   EXPECT_EQ("if (a) {\n"
    138             "  f();\n"
    139             "}",
    140             format("if(a){f();}"));
    141   EXPECT_EQ(4, ReplacementCount);
    142   EXPECT_EQ("if (a) {\n"
    143             "  f();\n"
    144             "}",
    145             format("if (a) {\n"
    146                    "  f();\n"
    147                    "}"));
    148   EXPECT_EQ(0, ReplacementCount);
    149   EXPECT_EQ("/*\r\n"
    150             "\r\n"
    151             "*/\r\n",
    152             format("/*\r\n"
    153                    "\r\n"
    154                    "*/\r\n"));
    155   EXPECT_EQ(0, ReplacementCount);
    156 }
    157 
    158 TEST_F(FormatTest, RemovesEmptyLines) {
    159   EXPECT_EQ("class C {\n"
    160             "  int i;\n"
    161             "};",
    162             format("class C {\n"
    163                    " int i;\n"
    164                    "\n"
    165                    "};"));
    166 
    167   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
    168   EXPECT_EQ("namespace N {\n"
    169             "\n"
    170             "int i;\n"
    171             "}",
    172             format("namespace N {\n"
    173                    "\n"
    174                    "int    i;\n"
    175                    "}",
    176                    getGoogleStyle()));
    177   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
    178             "\n"
    179             "int i;\n"
    180             "}",
    181             format("extern /**/ \"C\" /**/ {\n"
    182                    "\n"
    183                    "int    i;\n"
    184                    "}",
    185                    getGoogleStyle()));
    186 
    187   // ...but do keep inlining and removing empty lines for non-block extern "C"
    188   // functions.
    189   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
    190   EXPECT_EQ("extern \"C\" int f() {\n"
    191             "  int i = 42;\n"
    192             "  return i;\n"
    193             "}",
    194             format("extern \"C\" int f() {\n"
    195                    "\n"
    196                    "  int i = 42;\n"
    197                    "  return i;\n"
    198                    "}",
    199                    getGoogleStyle()));
    200 
    201   // Remove empty lines at the beginning and end of blocks.
    202   EXPECT_EQ("void f() {\n"
    203             "\n"
    204             "  if (a) {\n"
    205             "\n"
    206             "    f();\n"
    207             "  }\n"
    208             "}",
    209             format("void f() {\n"
    210                    "\n"
    211                    "  if (a) {\n"
    212                    "\n"
    213                    "    f();\n"
    214                    "\n"
    215                    "  }\n"
    216                    "\n"
    217                    "}",
    218                    getLLVMStyle()));
    219   EXPECT_EQ("void f() {\n"
    220             "  if (a) {\n"
    221             "    f();\n"
    222             "  }\n"
    223             "}",
    224             format("void f() {\n"
    225                    "\n"
    226                    "  if (a) {\n"
    227                    "\n"
    228                    "    f();\n"
    229                    "\n"
    230                    "  }\n"
    231                    "\n"
    232                    "}",
    233                    getGoogleStyle()));
    234 
    235   // Don't remove empty lines in more complex control statements.
    236   EXPECT_EQ("void f() {\n"
    237             "  if (a) {\n"
    238             "    f();\n"
    239             "\n"
    240             "  } else if (b) {\n"
    241             "    f();\n"
    242             "  }\n"
    243             "}",
    244             format("void f() {\n"
    245                    "  if (a) {\n"
    246                    "    f();\n"
    247                    "\n"
    248                    "  } else if (b) {\n"
    249                    "    f();\n"
    250                    "\n"
    251                    "  }\n"
    252                    "\n"
    253                    "}"));
    254 
    255   // FIXME: This is slightly inconsistent.
    256   EXPECT_EQ("namespace {\n"
    257             "int i;\n"
    258             "}",
    259             format("namespace {\n"
    260                    "int i;\n"
    261                    "\n"
    262                    "}"));
    263   EXPECT_EQ("namespace {\n"
    264             "int i;\n"
    265             "\n"
    266             "} // namespace",
    267             format("namespace {\n"
    268                    "int i;\n"
    269                    "\n"
    270                    "}  // namespace"));
    271 }
    272 
    273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
    274   verifyFormat("x = (a) and (b);");
    275   verifyFormat("x = (a) or (b);");
    276   verifyFormat("x = (a) bitand (b);");
    277   verifyFormat("x = (a) bitor (b);");
    278   verifyFormat("x = (a) not_eq (b);");
    279   verifyFormat("x = (a) and_eq (b);");
    280   verifyFormat("x = (a) or_eq (b);");
    281   verifyFormat("x = (a) xor (b);");
    282 }
    283 
    284 //===----------------------------------------------------------------------===//
    285 // Tests for control statements.
    286 //===----------------------------------------------------------------------===//
    287 
    288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
    289   verifyFormat("if (true)\n  f();\ng();");
    290   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
    291   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
    292 
    293   FormatStyle AllowsMergedIf = getLLVMStyle();
    294   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
    295   verifyFormat("if (a)\n"
    296                "  // comment\n"
    297                "  f();",
    298                AllowsMergedIf);
    299   verifyFormat("if (a)\n"
    300                "  ;",
    301                AllowsMergedIf);
    302   verifyFormat("if (a)\n"
    303                "  if (b) return;",
    304                AllowsMergedIf);
    305 
    306   verifyFormat("if (a) // Can't merge this\n"
    307                "  f();\n",
    308                AllowsMergedIf);
    309   verifyFormat("if (a) /* still don't merge */\n"
    310                "  f();",
    311                AllowsMergedIf);
    312   verifyFormat("if (a) { // Never merge this\n"
    313                "  f();\n"
    314                "}",
    315                AllowsMergedIf);
    316   verifyFormat("if (a) { /* Never merge this */\n"
    317                "  f();\n"
    318                "}",
    319                AllowsMergedIf);
    320 
    321   AllowsMergedIf.ColumnLimit = 14;
    322   verifyFormat("if (a) return;", AllowsMergedIf);
    323   verifyFormat("if (aaaaaaaaa)\n"
    324                "  return;",
    325                AllowsMergedIf);
    326 
    327   AllowsMergedIf.ColumnLimit = 13;
    328   verifyFormat("if (a)\n  return;", AllowsMergedIf);
    329 }
    330 
    331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
    332   FormatStyle AllowsMergedLoops = getLLVMStyle();
    333   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
    334   verifyFormat("while (true) continue;", AllowsMergedLoops);
    335   verifyFormat("for (;;) continue;", AllowsMergedLoops);
    336   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
    337   verifyFormat("while (true)\n"
    338                "  ;",
    339                AllowsMergedLoops);
    340   verifyFormat("for (;;)\n"
    341                "  ;",
    342                AllowsMergedLoops);
    343   verifyFormat("for (;;)\n"
    344                "  for (;;) continue;",
    345                AllowsMergedLoops);
    346   verifyFormat("for (;;) // Can't merge this\n"
    347                "  continue;",
    348                AllowsMergedLoops);
    349   verifyFormat("for (;;) /* still don't merge */\n"
    350                "  continue;",
    351                AllowsMergedLoops);
    352 }
    353 
    354 TEST_F(FormatTest, FormatShortBracedStatements) {
    355   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
    356   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
    357 
    358   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
    359   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
    360 
    361   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
    362   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
    363   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
    364   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
    365   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
    366   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
    367   verifyFormat("if (true) { //\n"
    368                "  f();\n"
    369                "}",
    370                AllowSimpleBracedStatements);
    371   verifyFormat("if (true) {\n"
    372                "  f();\n"
    373                "  f();\n"
    374                "}",
    375                AllowSimpleBracedStatements);
    376   verifyFormat("if (true) {\n"
    377                "  f();\n"
    378                "} else {\n"
    379                "  f();\n"
    380                "}",
    381                AllowSimpleBracedStatements);
    382 
    383   verifyFormat("template <int> struct A2 {\n"
    384                "  struct B {};\n"
    385                "};",
    386                AllowSimpleBracedStatements);
    387 
    388   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
    389   verifyFormat("if (true) {\n"
    390                "  f();\n"
    391                "}",
    392                AllowSimpleBracedStatements);
    393   verifyFormat("if (true) {\n"
    394                "  f();\n"
    395                "} else {\n"
    396                "  f();\n"
    397                "}",
    398                AllowSimpleBracedStatements);
    399 
    400   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
    401   verifyFormat("while (true) {\n"
    402                "  f();\n"
    403                "}",
    404                AllowSimpleBracedStatements);
    405   verifyFormat("for (;;) {\n"
    406                "  f();\n"
    407                "}",
    408                AllowSimpleBracedStatements);
    409 }
    410 
    411 TEST_F(FormatTest, ParseIfElse) {
    412   verifyFormat("if (true)\n"
    413                "  if (true)\n"
    414                "    if (true)\n"
    415                "      f();\n"
    416                "    else\n"
    417                "      g();\n"
    418                "  else\n"
    419                "    h();\n"
    420                "else\n"
    421                "  i();");
    422   verifyFormat("if (true)\n"
    423                "  if (true)\n"
    424                "    if (true) {\n"
    425                "      if (true)\n"
    426                "        f();\n"
    427                "    } else {\n"
    428                "      g();\n"
    429                "    }\n"
    430                "  else\n"
    431                "    h();\n"
    432                "else {\n"
    433                "  i();\n"
    434                "}");
    435   verifyFormat("void f() {\n"
    436                "  if (a) {\n"
    437                "  } else {\n"
    438                "  }\n"
    439                "}");
    440 }
    441 
    442 TEST_F(FormatTest, ElseIf) {
    443   verifyFormat("if (a) {\n} else if (b) {\n}");
    444   verifyFormat("if (a)\n"
    445                "  f();\n"
    446                "else if (b)\n"
    447                "  g();\n"
    448                "else\n"
    449                "  h();");
    450   verifyFormat("if (a) {\n"
    451                "  f();\n"
    452                "}\n"
    453                "// or else ..\n"
    454                "else {\n"
    455                "  g()\n"
    456                "}");
    457 
    458   verifyFormat("if (a) {\n"
    459                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
    460                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
    461                "}");
    462   verifyFormat("if (a) {\n"
    463                "} else if (\n"
    464                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
    465                "}",
    466                getLLVMStyleWithColumns(62));
    467 }
    468 
    469 TEST_F(FormatTest, FormatsForLoop) {
    470   verifyFormat(
    471       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
    472       "     ++VeryVeryLongLoopVariable)\n"
    473       "  ;");
    474   verifyFormat("for (;;)\n"
    475                "  f();");
    476   verifyFormat("for (;;) {\n}");
    477   verifyFormat("for (;;) {\n"
    478                "  f();\n"
    479                "}");
    480   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
    481 
    482   verifyFormat(
    483       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
    484       "                                          E = UnwrappedLines.end();\n"
    485       "     I != E; ++I) {\n}");
    486 
    487   verifyFormat(
    488       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
    489       "     ++IIIII) {\n}");
    490   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
    491                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
    492                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
    493   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
    494                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
    495                "         E = FD->getDeclsInPrototypeScope().end();\n"
    496                "     I != E; ++I) {\n}");
    497   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
    498                "         I = Container.begin(),\n"
    499                "         E = Container.end();\n"
    500                "     I != E; ++I) {\n}",
    501                getLLVMStyleWithColumns(76));
    502 
    503   verifyFormat(
    504       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
    505       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
    506       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
    507       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
    508       "     ++aaaaaaaaaaa) {\n}");
    509   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
    510                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
    511                "     ++i) {\n}");
    512   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
    513                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
    514                "}");
    515   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
    516                "         aaaaaaaaaa);\n"
    517                "     iter; ++iter) {\n"
    518                "}");
    519   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
    520                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
    521                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
    522                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
    523 
    524   FormatStyle NoBinPacking = getLLVMStyle();
    525   NoBinPacking.BinPackParameters = false;
    526   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
    527                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
    528                "                                           aaaaaaaaaaaaaaaa,\n"
    529                "                                           aaaaaaaaaaaaaaaa,\n"
    530                "                                           aaaaaaaaaaaaaaaa);\n"
    531                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
    532                "}",
    533                NoBinPacking);
    534   verifyFormat(
    535       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
    536       "                                          E = UnwrappedLines.end();\n"
    537       "     I != E;\n"
    538       "     ++I) {\n}",
    539       NoBinPacking);
    540 }
    541 
    542 TEST_F(FormatTest, RangeBasedForLoops) {
    543   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
    544                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
    545   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
    546                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
    547   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
    548                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
    549   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
    550                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
    551 }
    552 
    553 TEST_F(FormatTest, ForEachLoops) {
    554   verifyFormat("void f() {\n"
    555                "  foreach (Item *item, itemlist) {}\n"
    556                "  Q_FOREACH (Item *item, itemlist) {}\n"
    557                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
    558                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
    559                "}");
    560 
    561   // As function-like macros.
    562   verifyFormat("#define foreach(x, y)\n"
    563                "#define Q_FOREACH(x, y)\n"
    564                "#define BOOST_FOREACH(x, y)\n"
    565                "#define UNKNOWN_FOREACH(x, y)\n");
    566 
    567   // Not as function-like macros.
    568   verifyFormat("#define foreach (x, y)\n"
    569                "#define Q_FOREACH (x, y)\n"
    570                "#define BOOST_FOREACH (x, y)\n"
    571                "#define UNKNOWN_FOREACH (x, y)\n");
    572 }
    573 
    574 TEST_F(FormatTest, FormatsWhileLoop) {
    575   verifyFormat("while (true) {\n}");
    576   verifyFormat("while (true)\n"
    577                "  f();");
    578   verifyFormat("while () {\n}");
    579   verifyFormat("while () {\n"
    580                "  f();\n"
    581                "}");
    582 }
    583 
    584 TEST_F(FormatTest, FormatsDoWhile) {
    585   verifyFormat("do {\n"
    586                "  do_something();\n"
    587                "} while (something());");
    588   verifyFormat("do\n"
    589                "  do_something();\n"
    590                "while (something());");
    591 }
    592 
    593 TEST_F(FormatTest, FormatsSwitchStatement) {
    594   verifyFormat("switch (x) {\n"
    595                "case 1:\n"
    596                "  f();\n"
    597                "  break;\n"
    598                "case kFoo:\n"
    599                "case ns::kBar:\n"
    600                "case kBaz:\n"
    601                "  break;\n"
    602                "default:\n"
    603                "  g();\n"
    604                "  break;\n"
    605                "}");
    606   verifyFormat("switch (x) {\n"
    607                "case 1: {\n"
    608                "  f();\n"
    609                "  break;\n"
    610                "}\n"
    611                "case 2: {\n"
    612                "  break;\n"
    613                "}\n"
    614                "}");
    615   verifyFormat("switch (x) {\n"
    616                "case 1: {\n"
    617                "  f();\n"
    618                "  {\n"
    619                "    g();\n"
    620                "    h();\n"
    621                "  }\n"
    622                "  break;\n"
    623                "}\n"
    624                "}");
    625   verifyFormat("switch (x) {\n"
    626                "case 1: {\n"
    627                "  f();\n"
    628                "  if (foo) {\n"
    629                "    g();\n"
    630                "    h();\n"
    631                "  }\n"
    632                "  break;\n"
    633                "}\n"
    634                "}");
    635   verifyFormat("switch (x) {\n"
    636                "case 1: {\n"
    637                "  f();\n"
    638                "  g();\n"
    639                "} break;\n"
    640                "}");
    641   verifyFormat("switch (test)\n"
    642                "  ;");
    643   verifyFormat("switch (x) {\n"
    644                "default: {\n"
    645                "  // Do nothing.\n"
    646                "}\n"
    647                "}");
    648   verifyFormat("switch (x) {\n"
    649                "// comment\n"
    650                "// if 1, do f()\n"
    651                "case 1:\n"
    652                "  f();\n"
    653                "}");
    654   verifyFormat("switch (x) {\n"
    655                "case 1:\n"
    656                "  // Do amazing stuff\n"
    657                "  {\n"
    658                "    f();\n"
    659                "    g();\n"
    660                "  }\n"
    661                "  break;\n"
    662                "}");
    663   verifyFormat("#define A          \\\n"
    664                "  switch (x) {     \\\n"
    665                "  case a:          \\\n"
    666                "    foo = b;       \\\n"
    667                "  }",
    668                getLLVMStyleWithColumns(20));
    669   verifyFormat("#define OPERATION_CASE(name)           \\\n"
    670                "  case OP_name:                        \\\n"
    671                "    return operations::Operation##name\n",
    672                getLLVMStyleWithColumns(40));
    673   verifyFormat("switch (x) {\n"
    674                "case 1:;\n"
    675                "default:;\n"
    676                "  int i;\n"
    677                "}");
    678 
    679   verifyGoogleFormat("switch (x) {\n"
    680                      "  case 1:\n"
    681                      "    f();\n"
    682                      "    break;\n"
    683                      "  case kFoo:\n"
    684                      "  case ns::kBar:\n"
    685                      "  case kBaz:\n"
    686                      "    break;\n"
    687                      "  default:\n"
    688                      "    g();\n"
    689                      "    break;\n"
    690                      "}");
    691   verifyGoogleFormat("switch (x) {\n"
    692                      "  case 1: {\n"
    693                      "    f();\n"
    694                      "    break;\n"
    695                      "  }\n"
    696                      "}");
    697   verifyGoogleFormat("switch (test)\n"
    698                      "  ;");
    699 
    700   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
    701                      "  case OP_name:              \\\n"
    702                      "    return operations::Operation##name\n");
    703   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
    704                      "  // Get the correction operation class.\n"
    705                      "  switch (OpCode) {\n"
    706                      "    CASE(Add);\n"
    707                      "    CASE(Subtract);\n"
    708                      "    default:\n"
    709                      "      return operations::Unknown;\n"
    710                      "  }\n"
    711                      "#undef OPERATION_CASE\n"
    712                      "}");
    713   verifyFormat("DEBUG({\n"
    714                "  switch (x) {\n"
    715                "  case A:\n"
    716                "    f();\n"
    717                "    break;\n"
    718                "  // On B:\n"
    719                "  case B:\n"
    720                "    g();\n"
    721                "    break;\n"
    722                "  }\n"
    723                "});");
    724   verifyFormat("switch (a) {\n"
    725                "case (b):\n"
    726                "  return;\n"
    727                "}");
    728 
    729   verifyFormat("switch (a) {\n"
    730                "case some_namespace::\n"
    731                "    some_constant:\n"
    732                "  return;\n"
    733                "}",
    734                getLLVMStyleWithColumns(34));
    735 }
    736 
    737 TEST_F(FormatTest, CaseRanges) {
    738   verifyFormat("switch (x) {\n"
    739                "case 'A' ... 'Z':\n"
    740                "case 1 ... 5:\n"
    741                "  break;\n"
    742                "}");
    743 }
    744 
    745 TEST_F(FormatTest, ShortCaseLabels) {
    746   FormatStyle Style = getLLVMStyle();
    747   Style.AllowShortCaseLabelsOnASingleLine = true;
    748   verifyFormat("switch (a) {\n"
    749                "case 1: x = 1; break;\n"
    750                "case 2: return;\n"
    751                "case 3:\n"
    752                "case 4:\n"
    753                "case 5: return;\n"
    754                "case 6: // comment\n"
    755                "  return;\n"
    756                "case 7:\n"
    757                "  // comment\n"
    758                "  return;\n"
    759                "case 8:\n"
    760                "  x = 8; // comment\n"
    761                "  break;\n"
    762                "default: y = 1; break;\n"
    763                "}",
    764                Style);
    765   verifyFormat("switch (a) {\n"
    766                "#if FOO\n"
    767                "case 0: return 0;\n"
    768                "#endif\n"
    769                "}",
    770                Style);
    771   verifyFormat("switch (a) {\n"
    772                "case 1: {\n"
    773                "}\n"
    774                "case 2: {\n"
    775                "  return;\n"
    776                "}\n"
    777                "case 3: {\n"
    778                "  x = 1;\n"
    779                "  return;\n"
    780                "}\n"
    781                "case 4:\n"
    782                "  if (x)\n"
    783                "    return;\n"
    784                "}",
    785                Style);
    786   Style.ColumnLimit = 21;
    787   verifyFormat("switch (a) {\n"
    788                "case 1: x = 1; break;\n"
    789                "case 2: return;\n"
    790                "case 3:\n"
    791                "case 4:\n"
    792                "case 5: return;\n"
    793                "default:\n"
    794                "  y = 1;\n"
    795                "  break;\n"
    796                "}",
    797                Style);
    798 }
    799 
    800 TEST_F(FormatTest, FormatsLabels) {
    801   verifyFormat("void f() {\n"
    802                "  some_code();\n"
    803                "test_label:\n"
    804                "  some_other_code();\n"
    805                "  {\n"
    806                "    some_more_code();\n"
    807                "  another_label:\n"
    808                "    some_more_code();\n"
    809                "  }\n"
    810                "}");
    811   verifyFormat("{\n"
    812                "  some_code();\n"
    813                "test_label:\n"
    814                "  some_other_code();\n"
    815                "}");
    816   verifyFormat("{\n"
    817                "  some_code();\n"
    818                "test_label:;\n"
    819                "  int i = 0;\n"
    820                "}");
    821 }
    822 
    823 //===----------------------------------------------------------------------===//
    824 // Tests for comments.
    825 //===----------------------------------------------------------------------===//
    826 
    827 TEST_F(FormatTest, UnderstandsSingleLineComments) {
    828   verifyFormat("//* */");
    829   verifyFormat("// line 1\n"
    830                "// line 2\n"
    831                "void f() {}\n");
    832 
    833   verifyFormat("void f() {\n"
    834                "  // Doesn't do anything\n"
    835                "}");
    836   verifyFormat("SomeObject\n"
    837                "    // Calling someFunction on SomeObject\n"
    838                "    .someFunction();");
    839   verifyFormat("auto result = SomeObject\n"
    840                "                  // Calling someFunction on SomeObject\n"
    841                "                  .someFunction();");
    842   verifyFormat("void f(int i,  // some comment (probably for i)\n"
    843                "       int j,  // some comment (probably for j)\n"
    844                "       int k); // some comment (probably for k)");
    845   verifyFormat("void f(int i,\n"
    846                "       // some comment (probably for j)\n"
    847                "       int j,\n"
    848                "       // some comment (probably for k)\n"
    849                "       int k);");
    850 
    851   verifyFormat("int i    // This is a fancy variable\n"
    852                "    = 5; // with nicely aligned comment.");
    853 
    854   verifyFormat("// Leading comment.\n"
    855                "int a; // Trailing comment.");
    856   verifyFormat("int a; // Trailing comment\n"
    857                "       // on 2\n"
    858                "       // or 3 lines.\n"
    859                "int b;");
    860   verifyFormat("int a; // Trailing comment\n"
    861                "\n"
    862                "// Leading comment.\n"
    863                "int b;");
    864   verifyFormat("int a;    // Comment.\n"
    865                "          // More details.\n"
    866                "int bbbb; // Another comment.");
    867   verifyFormat(
    868       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
    869       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
    870       "int cccccccccccccccccccccccccccccc;       // comment\n"
    871       "int ddd;                     // looooooooooooooooooooooooong comment\n"
    872       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
    873       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
    874       "int ccccccccccccccccccc;     // comment");
    875 
    876   verifyFormat("#include \"a\"     // comment\n"
    877                "#include \"a/b/c\" // comment");
    878   verifyFormat("#include <a>     // comment\n"
    879                "#include <a/b/c> // comment");
    880   EXPECT_EQ("#include \"a\"     // comment\n"
    881             "#include \"a/b/c\" // comment",
    882             format("#include \\\n"
    883                    "  \"a\" // comment\n"
    884                    "#include \"a/b/c\" // comment"));
    885 
    886   verifyFormat("enum E {\n"
    887                "  // comment\n"
    888                "  VAL_A, // comment\n"
    889                "  VAL_B\n"
    890                "};");
    891 
    892   verifyFormat(
    893       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
    894       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
    895   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
    896                "    // Comment inside a statement.\n"
    897                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
    898   verifyFormat("SomeFunction(a,\n"
    899                "             // comment\n"
    900                "             b + x);");
    901   verifyFormat("SomeFunction(a, a,\n"
    902                "             // comment\n"
    903                "             b + x);");
    904   verifyFormat(
    905       "bool aaaaaaaaaaaaa = // comment\n"
    906       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
    907       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
    908 
    909   verifyFormat("int aaaa; // aaaaa\n"
    910                "int aa;   // aaaaaaa",
    911                getLLVMStyleWithColumns(20));
    912 
    913   EXPECT_EQ("void f() { // This does something ..\n"
    914             "}\n"
    915             "int a; // This is unrelated",
    916             format("void f()    {     // This does something ..\n"
    917                    "  }\n"
    918                    "int   a;     // This is unrelated"));
    919   EXPECT_EQ("class C {\n"
    920             "  void f() { // This does something ..\n"
    921             "  }          // awesome..\n"
    922             "\n"
    923             "  int a; // This is unrelated\n"
    924             "};",
    925             format("class C{void f()    { // This does something ..\n"
    926                    "      } // awesome..\n"
    927                    " \n"
    928                    "int a;    // This is unrelated\n"
    929                    "};"));
    930 
    931   EXPECT_EQ("int i; // single line trailing comment",
    932             format("int i;\\\n// single line trailing comment"));
    933 
    934   verifyGoogleFormat("int a;  // Trailing comment.");
    935 
    936   verifyFormat("someFunction(anotherFunction( // Force break.\n"
    937                "    parameter));");
    938 
    939   verifyGoogleFormat("#endif  // HEADER_GUARD");
    940 
    941   verifyFormat("const char *test[] = {\n"
    942                "    // A\n"
    943                "    \"aaaa\",\n"
    944                "    // B\n"
    945                "    \"aaaaa\"};");
    946   verifyGoogleFormat(
    947       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
    948       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
    949   EXPECT_EQ("D(a, {\n"
    950             "  // test\n"
    951             "  int a;\n"
    952             "});",
    953             format("D(a, {\n"
    954                    "// test\n"
    955                    "int a;\n"
    956                    "});"));
    957 
    958   EXPECT_EQ("lineWith(); // comment\n"
    959             "// at start\n"
    960             "otherLine();",
    961             format("lineWith();   // comment\n"
    962                    "// at start\n"
    963                    "otherLine();"));
    964   EXPECT_EQ("lineWith(); // comment\n"
    965             "            // at start\n"
    966             "otherLine();",
    967             format("lineWith();   // comment\n"
    968                    " // at start\n"
    969                    "otherLine();"));
    970 
    971   EXPECT_EQ("lineWith(); // comment\n"
    972             "// at start\n"
    973             "otherLine(); // comment",
    974             format("lineWith();   // comment\n"
    975                    "// at start\n"
    976                    "otherLine();   // comment"));
    977   EXPECT_EQ("lineWith();\n"
    978             "// at start\n"
    979             "otherLine(); // comment",
    980             format("lineWith();\n"
    981                    " // at start\n"
    982                    "otherLine();   // comment"));
    983   EXPECT_EQ("// first\n"
    984             "// at start\n"
    985             "otherLine(); // comment",
    986             format("// first\n"
    987                    " // at start\n"
    988                    "otherLine();   // comment"));
    989   EXPECT_EQ("f();\n"
    990             "// first\n"
    991             "// at start\n"
    992             "otherLine(); // comment",
    993             format("f();\n"
    994                    "// first\n"
    995                    " // at start\n"
    996                    "otherLine();   // comment"));
    997   verifyFormat("f(); // comment\n"
    998                "// first\n"
    999                "// at start\n"
   1000                "otherLine();");
   1001   EXPECT_EQ("f(); // comment\n"
   1002             "// first\n"
   1003             "// at start\n"
   1004             "otherLine();",
   1005             format("f();   // comment\n"
   1006                    "// first\n"
   1007                    " // at start\n"
   1008                    "otherLine();"));
   1009   EXPECT_EQ("f(); // comment\n"
   1010             "     // first\n"
   1011             "// at start\n"
   1012             "otherLine();",
   1013             format("f();   // comment\n"
   1014                    " // first\n"
   1015                    "// at start\n"
   1016                    "otherLine();"));
   1017   EXPECT_EQ("void f() {\n"
   1018             "  lineWith(); // comment\n"
   1019             "  // at start\n"
   1020             "}",
   1021             format("void              f() {\n"
   1022                    "  lineWith(); // comment\n"
   1023                    "  // at start\n"
   1024                    "}"));
   1025 
   1026   verifyFormat("#define A                                                  \\\n"
   1027                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
   1028                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
   1029                getLLVMStyleWithColumns(60));
   1030   verifyFormat(
   1031       "#define A                                                   \\\n"
   1032       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
   1033       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
   1034       getLLVMStyleWithColumns(61));
   1035 
   1036   verifyFormat("if ( // This is some comment\n"
   1037                "    x + 3) {\n"
   1038                "}");
   1039   EXPECT_EQ("if ( // This is some comment\n"
   1040             "     // spanning two lines\n"
   1041             "    x + 3) {\n"
   1042             "}",
   1043             format("if( // This is some comment\n"
   1044                    "     // spanning two lines\n"
   1045                    " x + 3) {\n"
   1046                    "}"));
   1047 
   1048   verifyNoCrash("/\\\n/");
   1049   verifyNoCrash("/\\\n* */");
   1050   // The 0-character somehow makes the lexer return a proper comment.
   1051   verifyNoCrash(StringRef("/*\\\0\n/", 6));
   1052 }
   1053 
   1054 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
   1055   EXPECT_EQ("SomeFunction(a,\n"
   1056             "             b, // comment\n"
   1057             "             c);",
   1058             format("SomeFunction(a,\n"
   1059                    "          b, // comment\n"
   1060                    "      c);"));
   1061   EXPECT_EQ("SomeFunction(a, b,\n"
   1062             "             // comment\n"
   1063             "             c);",
   1064             format("SomeFunction(a,\n"
   1065                    "          b,\n"
   1066                    "  // comment\n"
   1067                    "      c);"));
   1068   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
   1069             "             c);",
   1070             format("SomeFunction(a, b, // comment (unclear relation)\n"
   1071                    "      c);"));
   1072   EXPECT_EQ("SomeFunction(a, // comment\n"
   1073             "             b,\n"
   1074             "             c); // comment",
   1075             format("SomeFunction(a,     // comment\n"
   1076                    "          b,\n"
   1077                    "      c); // comment"));
   1078 }
   1079 
   1080 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
   1081   EXPECT_EQ("// comment", format("// comment  "));
   1082   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
   1083             format("int aaaaaaa, bbbbbbb; // comment                   ",
   1084                    getLLVMStyleWithColumns(33)));
   1085   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
   1086   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
   1087 }
   1088 
   1089 TEST_F(FormatTest, UnderstandsBlockComments) {
   1090   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
   1091   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
   1092   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
   1093             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
   1094             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
   1095                    "/* Trailing comment for aa... */\n"
   1096                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
   1097   EXPECT_EQ(
   1098       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   1099       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
   1100       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
   1101              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
   1102   EXPECT_EQ(
   1103       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   1104       "    aaaaaaaaaaaaaaaaaa,\n"
   1105       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
   1106       "}",
   1107       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   1108              "                      aaaaaaaaaaaaaaaaaa  ,\n"
   1109              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
   1110              "}"));
   1111 
   1112   FormatStyle NoBinPacking = getLLVMStyle();
   1113   NoBinPacking.BinPackParameters = false;
   1114   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
   1115                "         /* parameter 2 */ aaaaaa,\n"
   1116                "         /* parameter 3 */ aaaaaa,\n"
   1117                "         /* parameter 4 */ aaaaaa);",
   1118                NoBinPacking);
   1119 
   1120   // Aligning block comments in macros.
   1121   verifyGoogleFormat("#define A        \\\n"
   1122                      "  int i;   /*a*/ \\\n"
   1123                      "  int jjj; /*b*/");
   1124 }
   1125 
   1126 TEST_F(FormatTest, AlignsBlockComments) {
   1127   EXPECT_EQ("/*\n"
   1128             " * Really multi-line\n"
   1129             " * comment.\n"
   1130             " */\n"
   1131             "void f() {}",
   1132             format("  /*\n"
   1133                    "   * Really multi-line\n"
   1134                    "   * comment.\n"
   1135                    "   */\n"
   1136                    "  void f() {}"));
   1137   EXPECT_EQ("class C {\n"
   1138             "  /*\n"
   1139             "   * Another multi-line\n"
   1140             "   * comment.\n"
   1141             "   */\n"
   1142             "  void f() {}\n"
   1143             "};",
   1144             format("class C {\n"
   1145                    "/*\n"
   1146                    " * Another multi-line\n"
   1147                    " * comment.\n"
   1148                    " */\n"
   1149                    "void f() {}\n"
   1150                    "};"));
   1151   EXPECT_EQ("/*\n"
   1152             "  1. This is a comment with non-trivial formatting.\n"
   1153             "     1.1. We have to indent/outdent all lines equally\n"
   1154             "         1.1.1. to keep the formatting.\n"
   1155             " */",
   1156             format("  /*\n"
   1157                    "    1. This is a comment with non-trivial formatting.\n"
   1158                    "       1.1. We have to indent/outdent all lines equally\n"
   1159                    "           1.1.1. to keep the formatting.\n"
   1160                    "   */"));
   1161   EXPECT_EQ("/*\n"
   1162             "Don't try to outdent if there's not enough indentation.\n"
   1163             "*/",
   1164             format("  /*\n"
   1165                    " Don't try to outdent if there's not enough indentation.\n"
   1166                    " */"));
   1167 
   1168   EXPECT_EQ("int i; /* Comment with empty...\n"
   1169             "        *\n"
   1170             "        * line. */",
   1171             format("int i; /* Comment with empty...\n"
   1172                    "        *\n"
   1173                    "        * line. */"));
   1174   EXPECT_EQ("int foobar = 0; /* comment */\n"
   1175             "int bar = 0;    /* multiline\n"
   1176             "                   comment 1 */\n"
   1177             "int baz = 0;    /* multiline\n"
   1178             "                   comment 2 */\n"
   1179             "int bzz = 0;    /* multiline\n"
   1180             "                   comment 3 */",
   1181             format("int foobar = 0; /* comment */\n"
   1182                    "int bar = 0;    /* multiline\n"
   1183                    "                   comment 1 */\n"
   1184                    "int baz = 0; /* multiline\n"
   1185                    "                comment 2 */\n"
   1186                    "int bzz = 0;         /* multiline\n"
   1187                    "                        comment 3 */"));
   1188   EXPECT_EQ("int foobar = 0; /* comment */\n"
   1189             "int bar = 0;    /* multiline\n"
   1190             "   comment */\n"
   1191             "int baz = 0;    /* multiline\n"
   1192             "comment */",
   1193             format("int foobar = 0; /* comment */\n"
   1194                    "int bar = 0; /* multiline\n"
   1195                    "comment */\n"
   1196                    "int baz = 0;        /* multiline\n"
   1197                    "comment */"));
   1198 }
   1199 
   1200 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
   1201   FormatStyle Style = getLLVMStyleWithColumns(20);
   1202   Style.ReflowComments = false;
   1203   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
   1204   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
   1205 }
   1206 
   1207 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
   1208   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   1209             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
   1210             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   1211                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
   1212   EXPECT_EQ(
   1213       "void ffffffffffff(\n"
   1214       "    int aaaaaaaa, int bbbbbbbb,\n"
   1215       "    int cccccccccccc) { /*\n"
   1216       "                           aaaaaaaaaa\n"
   1217       "                           aaaaaaaaaaaaa\n"
   1218       "                           bbbbbbbbbbbbbb\n"
   1219       "                           bbbbbbbbbb\n"
   1220       "                         */\n"
   1221       "}",
   1222       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
   1223              "{ /*\n"
   1224              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
   1225              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
   1226              "   */\n"
   1227              "}",
   1228              getLLVMStyleWithColumns(40)));
   1229 }
   1230 
   1231 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
   1232   EXPECT_EQ("void ffffffffff(\n"
   1233             "    int aaaaa /* test */);",
   1234             format("void ffffffffff(int aaaaa /* test */);",
   1235                    getLLVMStyleWithColumns(35)));
   1236 }
   1237 
   1238 TEST_F(FormatTest, SplitsLongCxxComments) {
   1239   EXPECT_EQ("// A comment that\n"
   1240             "// doesn't fit on\n"
   1241             "// one line",
   1242             format("// A comment that doesn't fit on one line",
   1243                    getLLVMStyleWithColumns(20)));
   1244   EXPECT_EQ("/// A comment that\n"
   1245             "/// doesn't fit on\n"
   1246             "/// one line",
   1247             format("/// A comment that doesn't fit on one line",
   1248                    getLLVMStyleWithColumns(20)));
   1249   EXPECT_EQ("//! A comment that\n"
   1250             "//! doesn't fit on\n"
   1251             "//! one line",
   1252             format("//! A comment that doesn't fit on one line",
   1253                    getLLVMStyleWithColumns(20)));
   1254   EXPECT_EQ("// a b c d\n"
   1255             "// e f  g\n"
   1256             "// h i j k",
   1257             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
   1258   EXPECT_EQ(
   1259       "// a b c d\n"
   1260       "// e f  g\n"
   1261       "// h i j k",
   1262       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
   1263   EXPECT_EQ("if (true) // A comment that\n"
   1264             "          // doesn't fit on\n"
   1265             "          // one line",
   1266             format("if (true) // A comment that doesn't fit on one line   ",
   1267                    getLLVMStyleWithColumns(30)));
   1268   EXPECT_EQ("//    Don't_touch_leading_whitespace",
   1269             format("//    Don't_touch_leading_whitespace",
   1270                    getLLVMStyleWithColumns(20)));
   1271   EXPECT_EQ("// Add leading\n"
   1272             "// whitespace",
   1273             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
   1274   EXPECT_EQ("/// Add leading\n"
   1275             "/// whitespace",
   1276             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
   1277   EXPECT_EQ("//! Add leading\n"
   1278             "//! whitespace",
   1279             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
   1280   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
   1281   EXPECT_EQ("// Even if it makes the line exceed the column\n"
   1282             "// limit",
   1283             format("//Even if it makes the line exceed the column limit",
   1284                    getLLVMStyleWithColumns(51)));
   1285   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
   1286 
   1287   EXPECT_EQ("// aa bb cc dd",
   1288             format("// aa bb             cc dd                   ",
   1289                    getLLVMStyleWithColumns(15)));
   1290 
   1291   EXPECT_EQ("// A comment before\n"
   1292             "// a macro\n"
   1293             "// definition\n"
   1294             "#define a b",
   1295             format("// A comment before a macro definition\n"
   1296                    "#define a b",
   1297                    getLLVMStyleWithColumns(20)));
   1298   EXPECT_EQ("void ffffff(\n"
   1299             "    int aaaaaaaaa,  // wwww\n"
   1300             "    int bbbbbbbbbb, // xxxxxxx\n"
   1301             "                    // yyyyyyyyyy\n"
   1302             "    int c, int d, int e) {}",
   1303             format("void ffffff(\n"
   1304                    "    int aaaaaaaaa, // wwww\n"
   1305                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
   1306                    "    int c, int d, int e) {}",
   1307                    getLLVMStyleWithColumns(40)));
   1308   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   1309             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   1310                    getLLVMStyleWithColumns(20)));
   1311   EXPECT_EQ(
   1312       "#define XXX // a b c d\n"
   1313       "            // e f g h",
   1314       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
   1315   EXPECT_EQ(
   1316       "#define XXX // q w e r\n"
   1317       "            // t y u i",
   1318       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
   1319 }
   1320 
   1321 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
   1322   EXPECT_EQ("//     A comment\n"
   1323             "//     that doesn't\n"
   1324             "//     fit on one\n"
   1325             "//     line",
   1326             format("//     A comment that doesn't fit on one line",
   1327                    getLLVMStyleWithColumns(20)));
   1328   EXPECT_EQ("///     A comment\n"
   1329             "///     that doesn't\n"
   1330             "///     fit on one\n"
   1331             "///     line",
   1332             format("///     A comment that doesn't fit on one line",
   1333                    getLLVMStyleWithColumns(20)));
   1334 }
   1335 
   1336 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
   1337   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   1338             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   1339             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   1340             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   1341                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   1342                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
   1343   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1344             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1345             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
   1346             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1347                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1348                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
   1349                    getLLVMStyleWithColumns(50)));
   1350   // FIXME: One day we might want to implement adjustment of leading whitespace
   1351   // of the consecutive lines in this kind of comment:
   1352   EXPECT_EQ("double\n"
   1353             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1354             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1355             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
   1356             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1357                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
   1358                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
   1359                    getLLVMStyleWithColumns(49)));
   1360 }
   1361 
   1362 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
   1363   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
   1364   Pragmas.CommentPragmas = "^ IWYU pragma:";
   1365   EXPECT_EQ(
   1366       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
   1367       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
   1368   EXPECT_EQ(
   1369       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
   1370       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
   1371 }
   1372 
   1373 TEST_F(FormatTest, PriorityOfCommentBreaking) {
   1374   EXPECT_EQ("if (xxx ==\n"
   1375             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
   1376             "    zzz)\n"
   1377             "  q();",
   1378             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
   1379                    "    zzz) q();",
   1380                    getLLVMStyleWithColumns(40)));
   1381   EXPECT_EQ("if (xxxxxxxxxx ==\n"
   1382             "        yyy && // aaaaaa bbbbbbbb cccc\n"
   1383             "    zzz)\n"
   1384             "  q();",
   1385             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
   1386                    "    zzz) q();",
   1387                    getLLVMStyleWithColumns(40)));
   1388   EXPECT_EQ("if (xxxxxxxxxx &&\n"
   1389             "        yyy || // aaaaaa bbbbbbbb cccc\n"
   1390             "    zzz)\n"
   1391             "  q();",
   1392             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
   1393                    "    zzz) q();",
   1394                    getLLVMStyleWithColumns(40)));
   1395   EXPECT_EQ("fffffffff(\n"
   1396             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
   1397             "    zzz);",
   1398             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
   1399                    " zzz);",
   1400                    getLLVMStyleWithColumns(40)));
   1401 }
   1402 
   1403 TEST_F(FormatTest, MultiLineCommentsInDefines) {
   1404   EXPECT_EQ("#define A(x) /* \\\n"
   1405             "  a comment     \\\n"
   1406             "  inside */     \\\n"
   1407             "  f();",
   1408             format("#define A(x) /* \\\n"
   1409                    "  a comment     \\\n"
   1410                    "  inside */     \\\n"
   1411                    "  f();",
   1412                    getLLVMStyleWithColumns(17)));
   1413   EXPECT_EQ("#define A(      \\\n"
   1414             "    x) /*       \\\n"
   1415             "  a comment     \\\n"
   1416             "  inside */     \\\n"
   1417             "  f();",
   1418             format("#define A(      \\\n"
   1419                    "    x) /*       \\\n"
   1420                    "  a comment     \\\n"
   1421                    "  inside */     \\\n"
   1422                    "  f();",
   1423                    getLLVMStyleWithColumns(17)));
   1424 }
   1425 
   1426 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
   1427   EXPECT_EQ("namespace {}\n// Test\n#define A",
   1428             format("namespace {}\n   // Test\n#define A"));
   1429   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
   1430             format("namespace {}\n   /* Test */\n#define A"));
   1431   EXPECT_EQ("namespace {}\n/* Test */ #define A",
   1432             format("namespace {}\n   /* Test */    #define A"));
   1433 }
   1434 
   1435 TEST_F(FormatTest, SplitsLongLinesInComments) {
   1436   EXPECT_EQ("/* This is a long\n"
   1437             " * comment that\n"
   1438             " * doesn't\n"
   1439             " * fit on one line.\n"
   1440             " */",
   1441             format("/* "
   1442                    "This is a long                                         "
   1443                    "comment that "
   1444                    "doesn't                                    "
   1445                    "fit on one line.  */",
   1446                    getLLVMStyleWithColumns(20)));
   1447   EXPECT_EQ(
   1448       "/* a b c d\n"
   1449       " * e f  g\n"
   1450       " * h i j k\n"
   1451       " */",
   1452       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
   1453   EXPECT_EQ(
   1454       "/* a b c d\n"
   1455       " * e f  g\n"
   1456       " * h i j k\n"
   1457       " */",
   1458       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
   1459   EXPECT_EQ("/*\n"
   1460             "This is a long\n"
   1461             "comment that doesn't\n"
   1462             "fit on one line.\n"
   1463             "*/",
   1464             format("/*\n"
   1465                    "This is a long                                         "
   1466                    "comment that doesn't                                    "
   1467                    "fit on one line.                                      \n"
   1468                    "*/",
   1469                    getLLVMStyleWithColumns(20)));
   1470   EXPECT_EQ("/*\n"
   1471             " * This is a long\n"
   1472             " * comment that\n"
   1473             " * doesn't fit on\n"
   1474             " * one line.\n"
   1475             " */",
   1476             format("/*      \n"
   1477                    " * This is a long "
   1478                    "   comment that     "
   1479                    "   doesn't fit on   "
   1480                    "   one line.                                            \n"
   1481                    " */",
   1482                    getLLVMStyleWithColumns(20)));
   1483   EXPECT_EQ("/*\n"
   1484             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
   1485             " * so_it_should_be_broken\n"
   1486             " * wherever_a_space_occurs\n"
   1487             " */",
   1488             format("/*\n"
   1489                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
   1490                    "   so_it_should_be_broken "
   1491                    "   wherever_a_space_occurs                             \n"
   1492                    " */",
   1493                    getLLVMStyleWithColumns(20)));
   1494   EXPECT_EQ("/*\n"
   1495             " *    This_comment_can_not_be_broken_into_lines\n"
   1496             " */",
   1497             format("/*\n"
   1498                    " *    This_comment_can_not_be_broken_into_lines\n"
   1499                    " */",
   1500                    getLLVMStyleWithColumns(20)));
   1501   EXPECT_EQ("{\n"
   1502             "  /*\n"
   1503             "  This is another\n"
   1504             "  long comment that\n"
   1505             "  doesn't fit on one\n"
   1506             "  line    1234567890\n"
   1507             "  */\n"
   1508             "}",
   1509             format("{\n"
   1510                    "/*\n"
   1511                    "This is another     "
   1512                    "  long comment that "
   1513                    "  doesn't fit on one"
   1514                    "  line    1234567890\n"
   1515                    "*/\n"
   1516                    "}",
   1517                    getLLVMStyleWithColumns(20)));
   1518   EXPECT_EQ("{\n"
   1519             "  /*\n"
   1520             "   * This        i s\n"
   1521             "   * another comment\n"
   1522             "   * t hat  doesn' t\n"
   1523             "   * fit on one l i\n"
   1524             "   * n e\n"
   1525             "   */\n"
   1526             "}",
   1527             format("{\n"
   1528                    "/*\n"
   1529                    " * This        i s"
   1530                    "   another comment"
   1531                    "   t hat  doesn' t"
   1532                    "   fit on one l i"
   1533                    "   n e\n"
   1534                    " */\n"
   1535                    "}",
   1536                    getLLVMStyleWithColumns(20)));
   1537   EXPECT_EQ("/*\n"
   1538             " * This is a long\n"
   1539             " * comment that\n"
   1540             " * doesn't fit on\n"
   1541             " * one line\n"
   1542             " */",
   1543             format("   /*\n"
   1544                    "    * This is a long comment that doesn't fit on one line\n"
   1545                    "    */",
   1546                    getLLVMStyleWithColumns(20)));
   1547   EXPECT_EQ("{\n"
   1548             "  if (something) /* This is a\n"
   1549             "                    long\n"
   1550             "                    comment */\n"
   1551             "    ;\n"
   1552             "}",
   1553             format("{\n"
   1554                    "  if (something) /* This is a long comment */\n"
   1555                    "    ;\n"
   1556                    "}",
   1557                    getLLVMStyleWithColumns(30)));
   1558 
   1559   EXPECT_EQ("/* A comment before\n"
   1560             " * a macro\n"
   1561             " * definition */\n"
   1562             "#define a b",
   1563             format("/* A comment before a macro definition */\n"
   1564                    "#define a b",
   1565                    getLLVMStyleWithColumns(20)));
   1566 
   1567   EXPECT_EQ("/* some comment\n"
   1568             "     *   a comment\n"
   1569             "* that we break\n"
   1570             " * another comment\n"
   1571             "* we have to break\n"
   1572             "* a left comment\n"
   1573             " */",
   1574             format("  /* some comment\n"
   1575                    "       *   a comment that we break\n"
   1576                    "   * another comment we have to break\n"
   1577                    "* a left comment\n"
   1578                    "   */",
   1579                    getLLVMStyleWithColumns(20)));
   1580 
   1581   EXPECT_EQ("/**\n"
   1582             " * multiline block\n"
   1583             " * comment\n"
   1584             " *\n"
   1585             " */",
   1586             format("/**\n"
   1587                    " * multiline block comment\n"
   1588                    " *\n"
   1589                    " */",
   1590                    getLLVMStyleWithColumns(20)));
   1591 
   1592   EXPECT_EQ("/*\n"
   1593             "\n"
   1594             "\n"
   1595             "    */\n",
   1596             format("  /*       \n"
   1597                    "      \n"
   1598                    "               \n"
   1599                    "      */\n"));
   1600 
   1601   EXPECT_EQ("/* a a */",
   1602             format("/* a a            */", getLLVMStyleWithColumns(15)));
   1603   EXPECT_EQ("/* a a bc  */",
   1604             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
   1605   EXPECT_EQ("/* aaa aaa\n"
   1606             " * aaaaa */",
   1607             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
   1608   EXPECT_EQ("/* aaa aaa\n"
   1609             " * aaaaa     */",
   1610             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
   1611 }
   1612 
   1613 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
   1614   EXPECT_EQ("#define X          \\\n"
   1615             "  /*               \\\n"
   1616             "   Test            \\\n"
   1617             "   Macro comment   \\\n"
   1618             "   with a long     \\\n"
   1619             "   line            \\\n"
   1620             "   */              \\\n"
   1621             "  A + B",
   1622             format("#define X \\\n"
   1623                    "  /*\n"
   1624                    "   Test\n"
   1625                    "   Macro comment with a long  line\n"
   1626                    "   */ \\\n"
   1627                    "  A + B",
   1628                    getLLVMStyleWithColumns(20)));
   1629   EXPECT_EQ("#define X          \\\n"
   1630             "  /* Macro comment \\\n"
   1631             "     with a long   \\\n"
   1632             "     line */       \\\n"
   1633             "  A + B",
   1634             format("#define X \\\n"
   1635                    "  /* Macro comment with a long\n"
   1636                    "     line */ \\\n"
   1637                    "  A + B",
   1638                    getLLVMStyleWithColumns(20)));
   1639   EXPECT_EQ("#define X          \\\n"
   1640             "  /* Macro comment \\\n"
   1641             "   * with a long   \\\n"
   1642             "   * line */       \\\n"
   1643             "  A + B",
   1644             format("#define X \\\n"
   1645                    "  /* Macro comment with a long  line */ \\\n"
   1646                    "  A + B",
   1647                    getLLVMStyleWithColumns(20)));
   1648 }
   1649 
   1650 TEST_F(FormatTest, CommentsInStaticInitializers) {
   1651   EXPECT_EQ(
   1652       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
   1653       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
   1654       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
   1655       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
   1656       "                        aaaaaaaaaaaaaaaaaaaa};",
   1657       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
   1658              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
   1659              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
   1660              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
   1661              "                  aaaaaaaaaaaaaaaaaaaa };"));
   1662   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
   1663                "                        bbbbbbbbbbb, ccccccccccc};");
   1664   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
   1665                "                        // comment for bb....\n"
   1666                "                        bbbbbbbbbbb, ccccccccccc};");
   1667   verifyGoogleFormat(
   1668       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
   1669       "                        bbbbbbbbbbb, ccccccccccc};");
   1670   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
   1671                      "                        // comment for bb....\n"
   1672                      "                        bbbbbbbbbbb, ccccccccccc};");
   1673 
   1674   verifyFormat("S s = {{a, b, c},  // Group #1\n"
   1675                "       {d, e, f},  // Group #2\n"
   1676                "       {g, h, i}}; // Group #3");
   1677   verifyFormat("S s = {{// Group #1\n"
   1678                "        a, b, c},\n"
   1679                "       {// Group #2\n"
   1680                "        d, e, f},\n"
   1681                "       {// Group #3\n"
   1682                "        g, h, i}};");
   1683 
   1684   EXPECT_EQ("S s = {\n"
   1685             "    // Some comment\n"
   1686             "    a,\n"
   1687             "\n"
   1688             "    // Comment after empty line\n"
   1689             "    b}",
   1690             format("S s =    {\n"
   1691                    "      // Some comment\n"
   1692                    "  a,\n"
   1693                    "  \n"
   1694                    "     // Comment after empty line\n"
   1695                    "      b\n"
   1696                    "}"));
   1697   EXPECT_EQ("S s = {\n"
   1698             "    /* Some comment */\n"
   1699             "    a,\n"
   1700             "\n"
   1701             "    /* Comment after empty line */\n"
   1702             "    b}",
   1703             format("S s =    {\n"
   1704                    "      /* Some comment */\n"
   1705                    "  a,\n"
   1706                    "  \n"
   1707                    "     /* Comment after empty line */\n"
   1708                    "      b\n"
   1709                    "}"));
   1710   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
   1711                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
   1712                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
   1713                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
   1714 }
   1715 
   1716 TEST_F(FormatTest, IgnoresIf0Contents) {
   1717   EXPECT_EQ("#if 0\n"
   1718             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
   1719             "#endif\n"
   1720             "void f() {}",
   1721             format("#if 0\n"
   1722                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
   1723                    "#endif\n"
   1724                    "void f(  ) {  }"));
   1725   EXPECT_EQ("#if false\n"
   1726             "void f(  ) {  }\n"
   1727             "#endif\n"
   1728             "void g() {}\n",
   1729             format("#if false\n"
   1730                    "void f(  ) {  }\n"
   1731                    "#endif\n"
   1732                    "void g(  ) {  }\n"));
   1733   EXPECT_EQ("enum E {\n"
   1734             "  One,\n"
   1735             "  Two,\n"
   1736             "#if 0\n"
   1737             "Three,\n"
   1738             "      Four,\n"
   1739             "#endif\n"
   1740             "  Five\n"
   1741             "};",
   1742             format("enum E {\n"
   1743                    "  One,Two,\n"
   1744                    "#if 0\n"
   1745                    "Three,\n"
   1746                    "      Four,\n"
   1747                    "#endif\n"
   1748                    "  Five};"));
   1749   EXPECT_EQ("enum F {\n"
   1750             "  One,\n"
   1751             "#if 1\n"
   1752             "  Two,\n"
   1753             "#if 0\n"
   1754             "Three,\n"
   1755             "      Four,\n"
   1756             "#endif\n"
   1757             "  Five\n"
   1758             "#endif\n"
   1759             "};",
   1760             format("enum F {\n"
   1761                    "One,\n"
   1762                    "#if 1\n"
   1763                    "Two,\n"
   1764                    "#if 0\n"
   1765                    "Three,\n"
   1766                    "      Four,\n"
   1767                    "#endif\n"
   1768                    "Five\n"
   1769                    "#endif\n"
   1770                    "};"));
   1771   EXPECT_EQ("enum G {\n"
   1772             "  One,\n"
   1773             "#if 0\n"
   1774             "Two,\n"
   1775             "#else\n"
   1776             "  Three,\n"
   1777             "#endif\n"
   1778             "  Four\n"
   1779             "};",
   1780             format("enum G {\n"
   1781                    "One,\n"
   1782                    "#if 0\n"
   1783                    "Two,\n"
   1784                    "#else\n"
   1785                    "Three,\n"
   1786                    "#endif\n"
   1787                    "Four\n"
   1788                    "};"));
   1789   EXPECT_EQ("enum H {\n"
   1790             "  One,\n"
   1791             "#if 0\n"
   1792             "#ifdef Q\n"
   1793             "Two,\n"
   1794             "#else\n"
   1795             "Three,\n"
   1796             "#endif\n"
   1797             "#endif\n"
   1798             "  Four\n"
   1799             "};",
   1800             format("enum H {\n"
   1801                    "One,\n"
   1802                    "#if 0\n"
   1803                    "#ifdef Q\n"
   1804                    "Two,\n"
   1805                    "#else\n"
   1806                    "Three,\n"
   1807                    "#endif\n"
   1808                    "#endif\n"
   1809                    "Four\n"
   1810                    "};"));
   1811   EXPECT_EQ("enum I {\n"
   1812             "  One,\n"
   1813             "#if /* test */ 0 || 1\n"
   1814             "Two,\n"
   1815             "Three,\n"
   1816             "#endif\n"
   1817             "  Four\n"
   1818             "};",
   1819             format("enum I {\n"
   1820                    "One,\n"
   1821                    "#if /* test */ 0 || 1\n"
   1822                    "Two,\n"
   1823                    "Three,\n"
   1824                    "#endif\n"
   1825                    "Four\n"
   1826                    "};"));
   1827   EXPECT_EQ("enum J {\n"
   1828             "  One,\n"
   1829             "#if 0\n"
   1830             "#if 0\n"
   1831             "Two,\n"
   1832             "#else\n"
   1833             "Three,\n"
   1834             "#endif\n"
   1835             "Four,\n"
   1836             "#endif\n"
   1837             "  Five\n"
   1838             "};",
   1839             format("enum J {\n"
   1840                    "One,\n"
   1841                    "#if 0\n"
   1842                    "#if 0\n"
   1843                    "Two,\n"
   1844                    "#else\n"
   1845                    "Three,\n"
   1846                    "#endif\n"
   1847                    "Four,\n"
   1848                    "#endif\n"
   1849                    "Five\n"
   1850                    "};"));
   1851 }
   1852 
   1853 //===----------------------------------------------------------------------===//
   1854 // Tests for classes, namespaces, etc.
   1855 //===----------------------------------------------------------------------===//
   1856 
   1857 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
   1858   verifyFormat("class A {};");
   1859 }
   1860 
   1861 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
   1862   verifyFormat("class A {\n"
   1863                "public:\n"
   1864                "public: // comment\n"
   1865                "protected:\n"
   1866                "private:\n"
   1867                "  void f() {}\n"
   1868                "};");
   1869   verifyGoogleFormat("class A {\n"
   1870                      " public:\n"
   1871                      " protected:\n"
   1872                      " private:\n"
   1873                      "  void f() {}\n"
   1874                      "};");
   1875   verifyFormat("class A {\n"
   1876                "public slots:\n"
   1877                "  void f1() {}\n"
   1878                "public Q_SLOTS:\n"
   1879                "  void f2() {}\n"
   1880                "protected slots:\n"
   1881                "  void f3() {}\n"
   1882                "protected Q_SLOTS:\n"
   1883                "  void f4() {}\n"
   1884                "private slots:\n"
   1885                "  void f5() {}\n"
   1886                "private Q_SLOTS:\n"
   1887                "  void f6() {}\n"
   1888                "signals:\n"
   1889                "  void g1();\n"
   1890                "Q_SIGNALS:\n"
   1891                "  void g2();\n"
   1892                "};");
   1893 
   1894   // Don't interpret 'signals' the wrong way.
   1895   verifyFormat("signals.set();");
   1896   verifyFormat("for (Signals signals : f()) {\n}");
   1897   verifyFormat("{\n"
   1898                "  signals.set(); // This needs indentation.\n"
   1899                "}");
   1900 }
   1901 
   1902 TEST_F(FormatTest, SeparatesLogicalBlocks) {
   1903   EXPECT_EQ("class A {\n"
   1904             "public:\n"
   1905             "  void f();\n"
   1906             "\n"
   1907             "private:\n"
   1908             "  void g() {}\n"
   1909             "  // test\n"
   1910             "protected:\n"
   1911             "  int h;\n"
   1912             "};",
   1913             format("class A {\n"
   1914                    "public:\n"
   1915                    "void f();\n"
   1916                    "private:\n"
   1917                    "void g() {}\n"
   1918                    "// test\n"
   1919                    "protected:\n"
   1920                    "int h;\n"
   1921                    "};"));
   1922   EXPECT_EQ("class A {\n"
   1923             "protected:\n"
   1924             "public:\n"
   1925             "  void f();\n"
   1926             "};",
   1927             format("class A {\n"
   1928                    "protected:\n"
   1929                    "\n"
   1930                    "public:\n"
   1931                    "\n"
   1932                    "  void f();\n"
   1933                    "};"));
   1934 
   1935   // Even ensure proper spacing inside macros.
   1936   EXPECT_EQ("#define B     \\\n"
   1937             "  class A {   \\\n"
   1938             "   protected: \\\n"
   1939             "   public:    \\\n"
   1940             "    void f(); \\\n"
   1941             "  };",
   1942             format("#define B     \\\n"
   1943                    "  class A {   \\\n"
   1944                    "   protected: \\\n"
   1945                    "              \\\n"
   1946                    "   public:    \\\n"
   1947                    "              \\\n"
   1948                    "    void f(); \\\n"
   1949                    "  };",
   1950                    getGoogleStyle()));
   1951   // But don't remove empty lines after macros ending in access specifiers.
   1952   EXPECT_EQ("#define A private:\n"
   1953             "\n"
   1954             "int i;",
   1955             format("#define A         private:\n"
   1956                    "\n"
   1957                    "int              i;"));
   1958 }
   1959 
   1960 TEST_F(FormatTest, FormatsClasses) {
   1961   verifyFormat("class A : public B {};");
   1962   verifyFormat("class A : public ::B {};");
   1963 
   1964   verifyFormat(
   1965       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
   1966       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
   1967   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
   1968                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
   1969                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
   1970   verifyFormat(
   1971       "class A : public B, public C, public D, public E, public F {};");
   1972   verifyFormat("class AAAAAAAAAAAA : public B,\n"
   1973                "                     public C,\n"
   1974                "                     public D,\n"
   1975                "                     public E,\n"
   1976                "                     public F,\n"
   1977                "                     public G {};");
   1978 
   1979   verifyFormat("class\n"
   1980                "    ReallyReallyLongClassName {\n"
   1981                "  int i;\n"
   1982                "};",
   1983                getLLVMStyleWithColumns(32));
   1984   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
   1985                "                           aaaaaaaaaaaaaaaa> {};");
   1986   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
   1987                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
   1988                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
   1989   verifyFormat("template <class R, class C>\n"
   1990                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
   1991                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
   1992   verifyFormat("class ::A::B {};");
   1993 }
   1994 
   1995 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
   1996   verifyFormat("class A {\n} a, b;");
   1997   verifyFormat("struct A {\n} a, b;");
   1998   verifyFormat("union A {\n} a;");
   1999 }
   2000 
   2001 TEST_F(FormatTest, FormatsEnum) {
   2002   verifyFormat("enum {\n"
   2003                "  Zero,\n"
   2004                "  One = 1,\n"
   2005                "  Two = One + 1,\n"
   2006                "  Three = (One + Two),\n"
   2007                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
   2008                "  Five = (One, Two, Three, Four, 5)\n"
   2009                "};");
   2010   verifyGoogleFormat("enum {\n"
   2011                      "  Zero,\n"
   2012                      "  One = 1,\n"
   2013                      "  Two = One + 1,\n"
   2014                      "  Three = (One + Two),\n"
   2015                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
   2016                      "  Five = (One, Two, Three, Four, 5)\n"
   2017                      "};");
   2018   verifyFormat("enum Enum {};");
   2019   verifyFormat("enum {};");
   2020   verifyFormat("enum X E {} d;");
   2021   verifyFormat("enum __attribute__((...)) E {} d;");
   2022   verifyFormat("enum __declspec__((...)) E {} d;");
   2023   verifyFormat("enum {\n"
   2024                "  Bar = Foo<int, int>::value\n"
   2025                "};",
   2026                getLLVMStyleWithColumns(30));
   2027 
   2028   verifyFormat("enum ShortEnum { A, B, C };");
   2029   verifyGoogleFormat("enum ShortEnum { A, B, C };");
   2030 
   2031   EXPECT_EQ("enum KeepEmptyLines {\n"
   2032             "  ONE,\n"
   2033             "\n"
   2034             "  TWO,\n"
   2035             "\n"
   2036             "  THREE\n"
   2037             "}",
   2038             format("enum KeepEmptyLines {\n"
   2039                    "  ONE,\n"
   2040                    "\n"
   2041                    "  TWO,\n"
   2042                    "\n"
   2043                    "\n"
   2044                    "  THREE\n"
   2045                    "}"));
   2046   verifyFormat("enum E { // comment\n"
   2047                "  ONE,\n"
   2048                "  TWO\n"
   2049                "};\n"
   2050                "int i;");
   2051   // Not enums.
   2052   verifyFormat("enum X f() {\n"
   2053                "  a();\n"
   2054                "  return 42;\n"
   2055                "}");
   2056   verifyFormat("enum X Type::f() {\n"
   2057                "  a();\n"
   2058                "  return 42;\n"
   2059                "}");
   2060   verifyFormat("enum ::X f() {\n"
   2061                "  a();\n"
   2062                "  return 42;\n"
   2063                "}");
   2064   verifyFormat("enum ns::X f() {\n"
   2065                "  a();\n"
   2066                "  return 42;\n"
   2067                "}");
   2068 }
   2069 
   2070 TEST_F(FormatTest, FormatsEnumsWithErrors) {
   2071   verifyFormat("enum Type {\n"
   2072                "  One = 0; // These semicolons should be commas.\n"
   2073                "  Two = 1;\n"
   2074                "};");
   2075   verifyFormat("namespace n {\n"
   2076                "enum Type {\n"
   2077                "  One,\n"
   2078                "  Two, // missing };\n"
   2079                "  int i;\n"
   2080                "}\n"
   2081                "void g() {}");
   2082 }
   2083 
   2084 TEST_F(FormatTest, FormatsEnumStruct) {
   2085   verifyFormat("enum struct {\n"
   2086                "  Zero,\n"
   2087                "  One = 1,\n"
   2088                "  Two = One + 1,\n"
   2089                "  Three = (One + Two),\n"
   2090                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
   2091                "  Five = (One, Two, Three, Four, 5)\n"
   2092                "};");
   2093   verifyFormat("enum struct Enum {};");
   2094   verifyFormat("enum struct {};");
   2095   verifyFormat("enum struct X E {} d;");
   2096   verifyFormat("enum struct __attribute__((...)) E {} d;");
   2097   verifyFormat("enum struct __declspec__((...)) E {} d;");
   2098   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
   2099 }
   2100 
   2101 TEST_F(FormatTest, FormatsEnumClass) {
   2102   verifyFormat("enum class {\n"
   2103                "  Zero,\n"
   2104                "  One = 1,\n"
   2105                "  Two = One + 1,\n"
   2106                "  Three = (One + Two),\n"
   2107                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
   2108                "  Five = (One, Two, Three, Four, 5)\n"
   2109                "};");
   2110   verifyFormat("enum class Enum {};");
   2111   verifyFormat("enum class {};");
   2112   verifyFormat("enum class X E {} d;");
   2113   verifyFormat("enum class __attribute__((...)) E {} d;");
   2114   verifyFormat("enum class __declspec__((...)) E {} d;");
   2115   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
   2116 }
   2117 
   2118 TEST_F(FormatTest, FormatsEnumTypes) {
   2119   verifyFormat("enum X : int {\n"
   2120                "  A, // Force multiple lines.\n"
   2121                "  B\n"
   2122                "};");
   2123   verifyFormat("enum X : int { A, B };");
   2124   verifyFormat("enum X : std::uint32_t { A, B };");
   2125 }
   2126 
   2127 TEST_F(FormatTest, FormatsNSEnums) {
   2128   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
   2129   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
   2130                      "  // Information about someDecentlyLongValue.\n"
   2131                      "  someDecentlyLongValue,\n"
   2132                      "  // Information about anotherDecentlyLongValue.\n"
   2133                      "  anotherDecentlyLongValue,\n"
   2134                      "  // Information about aThirdDecentlyLongValue.\n"
   2135                      "  aThirdDecentlyLongValue\n"
   2136                      "};");
   2137   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
   2138                      "  a = 1,\n"
   2139                      "  b = 2,\n"
   2140                      "  c = 3,\n"
   2141                      "};");
   2142   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
   2143                      "  a = 1,\n"
   2144                      "  b = 2,\n"
   2145                      "  c = 3,\n"
   2146                      "};");
   2147   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
   2148                      "  a = 1,\n"
   2149                      "  b = 2,\n"
   2150                      "  c = 3,\n"
   2151                      "};");
   2152 }
   2153 
   2154 TEST_F(FormatTest, FormatsBitfields) {
   2155   verifyFormat("struct Bitfields {\n"
   2156                "  unsigned sClass : 8;\n"
   2157                "  unsigned ValueKind : 2;\n"
   2158                "};");
   2159   verifyFormat("struct A {\n"
   2160                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
   2161                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
   2162                "};");
   2163   verifyFormat("struct MyStruct {\n"
   2164                "  uchar data;\n"
   2165                "  uchar : 8;\n"
   2166                "  uchar : 8;\n"
   2167                "  uchar other;\n"
   2168                "};");
   2169 }
   2170 
   2171 TEST_F(FormatTest, FormatsNamespaces) {
   2172   verifyFormat("namespace some_namespace {\n"
   2173                "class A {};\n"
   2174                "void f() { f(); }\n"
   2175                "}");
   2176   verifyFormat("namespace {\n"
   2177                "class A {};\n"
   2178                "void f() { f(); }\n"
   2179                "}");
   2180   verifyFormat("inline namespace X {\n"
   2181                "class A {};\n"
   2182                "void f() { f(); }\n"
   2183                "}");
   2184   verifyFormat("using namespace some_namespace;\n"
   2185                "class A {};\n"
   2186                "void f() { f(); }");
   2187 
   2188   // This code is more common than we thought; if we
   2189   // layout this correctly the semicolon will go into
   2190   // its own line, which is undesirable.
   2191   verifyFormat("namespace {};");
   2192   verifyFormat("namespace {\n"
   2193                "class A {};\n"
   2194                "};");
   2195 
   2196   verifyFormat("namespace {\n"
   2197                "int SomeVariable = 0; // comment\n"
   2198                "} // namespace");
   2199   EXPECT_EQ("#ifndef HEADER_GUARD\n"
   2200             "#define HEADER_GUARD\n"
   2201             "namespace my_namespace {\n"
   2202             "int i;\n"
   2203             "} // my_namespace\n"
   2204             "#endif // HEADER_GUARD",
   2205             format("#ifndef HEADER_GUARD\n"
   2206                    " #define HEADER_GUARD\n"
   2207                    "   namespace my_namespace {\n"
   2208                    "int i;\n"
   2209                    "}    // my_namespace\n"
   2210                    "#endif    // HEADER_GUARD"));
   2211 
   2212   EXPECT_EQ("namespace A::B {\n"
   2213             "class C {};\n"
   2214             "}",
   2215             format("namespace A::B {\n"
   2216                    "class C {};\n"
   2217                    "}"));
   2218 
   2219   FormatStyle Style = getLLVMStyle();
   2220   Style.NamespaceIndentation = FormatStyle::NI_All;
   2221   EXPECT_EQ("namespace out {\n"
   2222             "  int i;\n"
   2223             "  namespace in {\n"
   2224             "    int i;\n"
   2225             "  } // namespace\n"
   2226             "} // namespace",
   2227             format("namespace out {\n"
   2228                    "int i;\n"
   2229                    "namespace in {\n"
   2230                    "int i;\n"
   2231                    "} // namespace\n"
   2232                    "} // namespace",
   2233                    Style));
   2234 
   2235   Style.NamespaceIndentation = FormatStyle::NI_Inner;
   2236   EXPECT_EQ("namespace out {\n"
   2237             "int i;\n"
   2238             "namespace in {\n"
   2239             "  int i;\n"
   2240             "} // namespace\n"
   2241             "} // namespace",
   2242             format("namespace out {\n"
   2243                    "int i;\n"
   2244                    "namespace in {\n"
   2245                    "int i;\n"
   2246                    "} // namespace\n"
   2247                    "} // namespace",
   2248                    Style));
   2249 }
   2250 
   2251 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
   2252 
   2253 TEST_F(FormatTest, FormatsInlineASM) {
   2254   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
   2255   verifyFormat("asm(\"nop\" ::: \"memory\");");
   2256   verifyFormat(
   2257       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
   2258       "    \"cpuid\\n\\t\"\n"
   2259       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
   2260       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
   2261       "    : \"a\"(value));");
   2262   EXPECT_EQ(
   2263       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
   2264       "  __asm {\n"
   2265       "        mov     edx,[that] // vtable in edx\n"
   2266       "        mov     eax,methodIndex\n"
   2267       "        call    [edx][eax*4] // stdcall\n"
   2268       "  }\n"
   2269       "}",
   2270       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
   2271              "    __asm {\n"
   2272              "        mov     edx,[that] // vtable in edx\n"
   2273              "        mov     eax,methodIndex\n"
   2274              "        call    [edx][eax*4] // stdcall\n"
   2275              "    }\n"
   2276              "}"));
   2277   EXPECT_EQ("_asm {\n"
   2278             "  xor eax, eax;\n"
   2279             "  cpuid;\n"
   2280             "}",
   2281             format("_asm {\n"
   2282                    "  xor eax, eax;\n"
   2283                    "  cpuid;\n"
   2284                    "}"));
   2285   verifyFormat("void function() {\n"
   2286                "  // comment\n"
   2287                "  asm(\"\");\n"
   2288                "}");
   2289   EXPECT_EQ("__asm {\n"
   2290             "}\n"
   2291             "int i;",
   2292             format("__asm   {\n"
   2293                    "}\n"
   2294                    "int   i;"));
   2295 }
   2296 
   2297 TEST_F(FormatTest, FormatTryCatch) {
   2298   verifyFormat("try {\n"
   2299                "  throw a * b;\n"
   2300                "} catch (int a) {\n"
   2301                "  // Do nothing.\n"
   2302                "} catch (...) {\n"
   2303                "  exit(42);\n"
   2304                "}");
   2305 
   2306   // Function-level try statements.
   2307   verifyFormat("int f() try { return 4; } catch (...) {\n"
   2308                "  return 5;\n"
   2309                "}");
   2310   verifyFormat("class A {\n"
   2311                "  int a;\n"
   2312                "  A() try : a(0) {\n"
   2313                "  } catch (...) {\n"
   2314                "    throw;\n"
   2315                "  }\n"
   2316                "};\n");
   2317 
   2318   // Incomplete try-catch blocks.
   2319   verifyIncompleteFormat("try {} catch (");
   2320 }
   2321 
   2322 TEST_F(FormatTest, FormatSEHTryCatch) {
   2323   verifyFormat("__try {\n"
   2324                "  int a = b * c;\n"
   2325                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
   2326                "  // Do nothing.\n"
   2327                "}");
   2328 
   2329   verifyFormat("__try {\n"
   2330                "  int a = b * c;\n"
   2331                "} __finally {\n"
   2332                "  // Do nothing.\n"
   2333                "}");
   2334 
   2335   verifyFormat("DEBUG({\n"
   2336                "  __try {\n"
   2337                "  } __finally {\n"
   2338                "  }\n"
   2339                "});\n");
   2340 }
   2341 
   2342 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
   2343   verifyFormat("try {\n"
   2344                "  f();\n"
   2345                "} catch {\n"
   2346                "  g();\n"
   2347                "}");
   2348   verifyFormat("try {\n"
   2349                "  f();\n"
   2350                "} catch (A a) MACRO(x) {\n"
   2351                "  g();\n"
   2352                "} catch (B b) MACRO(x) {\n"
   2353                "  g();\n"
   2354                "}");
   2355 }
   2356 
   2357 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
   2358   FormatStyle Style = getLLVMStyle();
   2359   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
   2360                           FormatStyle::BS_WebKit}) {
   2361     Style.BreakBeforeBraces = BraceStyle;
   2362     verifyFormat("try {\n"
   2363                  "  // something\n"
   2364                  "} catch (...) {\n"
   2365                  "  // something\n"
   2366                  "}",
   2367                  Style);
   2368   }
   2369   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
   2370   verifyFormat("try {\n"
   2371                "  // something\n"
   2372                "}\n"
   2373                "catch (...) {\n"
   2374                "  // something\n"
   2375                "}",
   2376                Style);
   2377   verifyFormat("__try {\n"
   2378                "  // something\n"
   2379                "}\n"
   2380                "__finally {\n"
   2381                "  // something\n"
   2382                "}",
   2383                Style);
   2384   verifyFormat("@try {\n"
   2385                "  // something\n"
   2386                "}\n"
   2387                "@finally {\n"
   2388                "  // something\n"
   2389                "}",
   2390                Style);
   2391   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
   2392   verifyFormat("try\n"
   2393                "{\n"
   2394                "  // something\n"
   2395                "}\n"
   2396                "catch (...)\n"
   2397                "{\n"
   2398                "  // something\n"
   2399                "}",
   2400                Style);
   2401   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
   2402   verifyFormat("try\n"
   2403                "  {\n"
   2404                "    // something\n"
   2405                "  }\n"
   2406                "catch (...)\n"
   2407                "  {\n"
   2408                "    // something\n"
   2409                "  }",
   2410                Style);
   2411   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   2412   Style.BraceWrapping.BeforeCatch = true;
   2413   verifyFormat("try {\n"
   2414                "  // something\n"
   2415                "}\n"
   2416                "catch (...) {\n"
   2417                "  // something\n"
   2418                "}",
   2419                Style);
   2420 }
   2421 
   2422 TEST_F(FormatTest, FormatObjCTryCatch) {
   2423   verifyFormat("@try {\n"
   2424                "  f();\n"
   2425                "} @catch (NSException e) {\n"
   2426                "  @throw;\n"
   2427                "} @finally {\n"
   2428                "  exit(42);\n"
   2429                "}");
   2430   verifyFormat("DEBUG({\n"
   2431                "  @try {\n"
   2432                "  } @finally {\n"
   2433                "  }\n"
   2434                "});\n");
   2435 }
   2436 
   2437 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
   2438   FormatStyle Style = getLLVMStyle();
   2439   verifyFormat("@autoreleasepool {\n"
   2440                "  f();\n"
   2441                "}\n"
   2442                "@autoreleasepool {\n"
   2443                "  f();\n"
   2444                "}\n",
   2445                Style);
   2446   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
   2447   verifyFormat("@autoreleasepool\n"
   2448                "{\n"
   2449                "  f();\n"
   2450                "}\n"
   2451                "@autoreleasepool\n"
   2452                "{\n"
   2453                "  f();\n"
   2454                "}\n",
   2455                Style);
   2456 }
   2457 
   2458 TEST_F(FormatTest, StaticInitializers) {
   2459   verifyFormat("static SomeClass SC = {1, 'a'};");
   2460 
   2461   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
   2462                "    100000000, "
   2463                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
   2464 
   2465   // Here, everything other than the "}" would fit on a line.
   2466   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
   2467                "    10000000000000000000000000};");
   2468   EXPECT_EQ("S s = {a,\n"
   2469             "\n"
   2470             "       b};",
   2471             format("S s = {\n"
   2472                    "  a,\n"
   2473                    "\n"
   2474                    "  b\n"
   2475                    "};"));
   2476 
   2477   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
   2478   // line. However, the formatting looks a bit off and this probably doesn't
   2479   // happen often in practice.
   2480   verifyFormat("static int Variable[1] = {\n"
   2481                "    {1000000000000000000000000000000000000}};",
   2482                getLLVMStyleWithColumns(40));
   2483 }
   2484 
   2485 TEST_F(FormatTest, DesignatedInitializers) {
   2486   verifyFormat("const struct A a = {.a = 1, .b = 2};");
   2487   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
   2488                "                    .bbbbbbbbbb = 2,\n"
   2489                "                    .cccccccccc = 3,\n"
   2490                "                    .dddddddddd = 4,\n"
   2491                "                    .eeeeeeeeee = 5};");
   2492   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
   2493                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
   2494                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
   2495                "    .ccccccccccccccccccccccccccc = 3,\n"
   2496                "    .ddddddddddddddddddddddddddd = 4,\n"
   2497                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
   2498 
   2499   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
   2500 }
   2501 
   2502 TEST_F(FormatTest, NestedStaticInitializers) {
   2503   verifyFormat("static A x = {{{}}};\n");
   2504   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
   2505                "               {init1, init2, init3, init4}}};",
   2506                getLLVMStyleWithColumns(50));
   2507 
   2508   verifyFormat("somes Status::global_reps[3] = {\n"
   2509                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
   2510                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
   2511                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
   2512                getLLVMStyleWithColumns(60));
   2513   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
   2514                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
   2515                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
   2516                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
   2517   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
   2518                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
   2519                "rect.fTop}};");
   2520 
   2521   verifyFormat(
   2522       "SomeArrayOfSomeType a = {\n"
   2523       "    {{1, 2, 3},\n"
   2524       "     {1, 2, 3},\n"
   2525       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
   2526       "      333333333333333333333333333333},\n"
   2527       "     {1, 2, 3},\n"
   2528       "     {1, 2, 3}}};");
   2529   verifyFormat(
   2530       "SomeArrayOfSomeType a = {\n"
   2531       "    {{1, 2, 3}},\n"
   2532       "    {{1, 2, 3}},\n"
   2533       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
   2534       "      333333333333333333333333333333}},\n"
   2535       "    {{1, 2, 3}},\n"
   2536       "    {{1, 2, 3}}};");
   2537 
   2538   verifyFormat("struct {\n"
   2539                "  unsigned bit;\n"
   2540                "  const char *const name;\n"
   2541                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
   2542                "                 {kOsWin, \"Windows\"},\n"
   2543                "                 {kOsLinux, \"Linux\"},\n"
   2544                "                 {kOsCrOS, \"Chrome OS\"}};");
   2545   verifyFormat("struct {\n"
   2546                "  unsigned bit;\n"
   2547                "  const char *const name;\n"
   2548                "} kBitsToOs[] = {\n"
   2549                "    {kOsMac, \"Mac\"},\n"
   2550                "    {kOsWin, \"Windows\"},\n"
   2551                "    {kOsLinux, \"Linux\"},\n"
   2552                "    {kOsCrOS, \"Chrome OS\"},\n"
   2553                "};");
   2554 }
   2555 
   2556 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
   2557   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
   2558                "                      \\\n"
   2559                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
   2560 }
   2561 
   2562 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
   2563   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
   2564                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
   2565 
   2566   // Do break defaulted and deleted functions.
   2567   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
   2568                "    default;",
   2569                getLLVMStyleWithColumns(40));
   2570   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
   2571                "    delete;",
   2572                getLLVMStyleWithColumns(40));
   2573 }
   2574 
   2575 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
   2576   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
   2577                getLLVMStyleWithColumns(40));
   2578   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
   2579                getLLVMStyleWithColumns(40));
   2580   EXPECT_EQ("#define Q                              \\\n"
   2581             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
   2582             "  \"aaaaaaaa.cpp\"",
   2583             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
   2584                    getLLVMStyleWithColumns(40)));
   2585 }
   2586 
   2587 TEST_F(FormatTest, UnderstandsLinePPDirective) {
   2588   EXPECT_EQ("# 123 \"A string literal\"",
   2589             format("   #     123    \"A string literal\""));
   2590 }
   2591 
   2592 TEST_F(FormatTest, LayoutUnknownPPDirective) {
   2593   EXPECT_EQ("#;", format("#;"));
   2594   verifyFormat("#\n;\n;\n;");
   2595 }
   2596 
   2597 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
   2598   EXPECT_EQ("#line 42 \"test\"\n",
   2599             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
   2600   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
   2601                                     getLLVMStyleWithColumns(12)));
   2602 }
   2603 
   2604 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
   2605   EXPECT_EQ("#line 42 \"test\"",
   2606             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
   2607   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
   2608 }
   2609 
   2610 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
   2611   verifyFormat("#define A \\x20");
   2612   verifyFormat("#define A \\ x20");
   2613   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
   2614   verifyFormat("#define A ''");
   2615   verifyFormat("#define A ''qqq");
   2616   verifyFormat("#define A `qqq");
   2617   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
   2618   EXPECT_EQ("const char *c = STRINGIFY(\n"
   2619             "\\na : b);",
   2620             format("const char * c = STRINGIFY(\n"
   2621                    "\\na : b);"));
   2622 
   2623   verifyFormat("a\r\\");
   2624   verifyFormat("a\v\\");
   2625   verifyFormat("a\f\\");
   2626 }
   2627 
   2628 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
   2629   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
   2630   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
   2631   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
   2632   // FIXME: We never break before the macro name.
   2633   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
   2634 
   2635   verifyFormat("#define A A\n#define A A");
   2636   verifyFormat("#define A(X) A\n#define A A");
   2637 
   2638   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
   2639   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
   2640 }
   2641 
   2642 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
   2643   EXPECT_EQ("// somecomment\n"
   2644             "#include \"a.h\"\n"
   2645             "#define A(  \\\n"
   2646             "    A, B)\n"
   2647             "#include \"b.h\"\n"
   2648             "// somecomment\n",
   2649             format("  // somecomment\n"
   2650                    "  #include \"a.h\"\n"
   2651                    "#define A(A,\\\n"
   2652                    "    B)\n"
   2653                    "    #include \"b.h\"\n"
   2654                    " // somecomment\n",
   2655                    getLLVMStyleWithColumns(13)));
   2656 }
   2657 
   2658 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
   2659 
   2660 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
   2661   EXPECT_EQ("#define A    \\\n"
   2662             "  c;         \\\n"
   2663             "  e;\n"
   2664             "f;",
   2665             format("#define A c; e;\n"
   2666                    "f;",
   2667                    getLLVMStyleWithColumns(14)));
   2668 }
   2669 
   2670 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
   2671 
   2672 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
   2673   EXPECT_EQ("int x,\n"
   2674             "#define A\n"
   2675             "    y;",
   2676             format("int x,\n#define A\ny;"));
   2677 }
   2678 
   2679 TEST_F(FormatTest, HashInMacroDefinition) {
   2680   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
   2681   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
   2682   verifyFormat("#define A  \\\n"
   2683                "  {        \\\n"
   2684                "    f(#c); \\\n"
   2685                "  }",
   2686                getLLVMStyleWithColumns(11));
   2687 
   2688   verifyFormat("#define A(X)         \\\n"
   2689                "  void function##X()",
   2690                getLLVMStyleWithColumns(22));
   2691 
   2692   verifyFormat("#define A(a, b, c)   \\\n"
   2693                "  void a##b##c()",
   2694                getLLVMStyleWithColumns(22));
   2695 
   2696   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
   2697 }
   2698 
   2699 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
   2700   EXPECT_EQ("#define A (x)", format("#define A (x)"));
   2701   EXPECT_EQ("#define A(x)", format("#define A(x)"));
   2702 }
   2703 
   2704 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
   2705   EXPECT_EQ("#define A b;", format("#define A \\\n"
   2706                                    "          \\\n"
   2707                                    "  b;",
   2708                                    getLLVMStyleWithColumns(25)));
   2709   EXPECT_EQ("#define A \\\n"
   2710             "          \\\n"
   2711             "  a;      \\\n"
   2712             "  b;",
   2713             format("#define A \\\n"
   2714                    "          \\\n"
   2715                    "  a;      \\\n"
   2716                    "  b;",
   2717                    getLLVMStyleWithColumns(11)));
   2718   EXPECT_EQ("#define A \\\n"
   2719             "  a;      \\\n"
   2720             "          \\\n"
   2721             "  b;",
   2722             format("#define A \\\n"
   2723                    "  a;      \\\n"
   2724                    "          \\\n"
   2725                    "  b;",
   2726                    getLLVMStyleWithColumns(11)));
   2727 }
   2728 
   2729 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
   2730   verifyIncompleteFormat("#define A :");
   2731   verifyFormat("#define SOMECASES  \\\n"
   2732                "  case 1:          \\\n"
   2733                "  case 2\n",
   2734                getLLVMStyleWithColumns(20));
   2735   verifyFormat("#define A template <typename T>");
   2736   verifyIncompleteFormat("#define STR(x) #x\n"
   2737                          "f(STR(this_is_a_string_literal{));");
   2738   verifyFormat("#pragma omp threadprivate( \\\n"
   2739                "    y)), // expected-warning",
   2740                getLLVMStyleWithColumns(28));
   2741   verifyFormat("#d, = };");
   2742   verifyFormat("#if \"a");
   2743   verifyIncompleteFormat("({\n"
   2744                          "#define b     \\\n"
   2745                          "  }           \\\n"
   2746                          "  a\n"
   2747                          "a",
   2748                          getLLVMStyleWithColumns(15));
   2749   verifyFormat("#define A     \\\n"
   2750                "  {           \\\n"
   2751                "    {\n"
   2752                "#define B     \\\n"
   2753                "  }           \\\n"
   2754                "  }",
   2755                getLLVMStyleWithColumns(15));
   2756   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
   2757   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
   2758   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
   2759   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
   2760 }
   2761 
   2762 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
   2763   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
   2764   EXPECT_EQ("class A : public QObject {\n"
   2765             "  Q_OBJECT\n"
   2766             "\n"
   2767             "  A() {}\n"
   2768             "};",
   2769             format("class A  :  public QObject {\n"
   2770                    "     Q_OBJECT\n"
   2771                    "\n"
   2772                    "  A() {\n}\n"
   2773                    "}  ;"));
   2774   EXPECT_EQ("MACRO\n"
   2775             "/*static*/ int i;",
   2776             format("MACRO\n"
   2777                    " /*static*/ int   i;"));
   2778   EXPECT_EQ("SOME_MACRO\n"
   2779             "namespace {\n"
   2780             "void f();\n"
   2781             "}",
   2782             format("SOME_MACRO\n"
   2783                    "  namespace    {\n"
   2784                    "void   f(  );\n"
   2785                    "}"));
   2786   // Only if the identifier contains at least 5 characters.
   2787   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
   2788   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
   2789   // Only if everything is upper case.
   2790   EXPECT_EQ("class A : public QObject {\n"
   2791             "  Q_Object A() {}\n"
   2792             "};",
   2793             format("class A  :  public QObject {\n"
   2794                    "     Q_Object\n"
   2795                    "  A() {\n}\n"
   2796                    "}  ;"));
   2797 
   2798   // Only if the next line can actually start an unwrapped line.
   2799   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
   2800             format("SOME_WEIRD_LOG_MACRO\n"
   2801                    "<< SomeThing;"));
   2802 
   2803   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
   2804                "(n, buffers))\n",
   2805                getChromiumStyle(FormatStyle::LK_Cpp));
   2806 }
   2807 
   2808 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
   2809   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
   2810             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
   2811             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
   2812             "class X {};\n"
   2813             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
   2814             "int *createScopDetectionPass() { return 0; }",
   2815             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
   2816                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
   2817                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
   2818                    "  class X {};\n"
   2819                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
   2820                    "  int *createScopDetectionPass() { return 0; }"));
   2821   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
   2822   // braces, so that inner block is indented one level more.
   2823   EXPECT_EQ("int q() {\n"
   2824             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
   2825             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
   2826             "  IPC_END_MESSAGE_MAP()\n"
   2827             "}",
   2828             format("int q() {\n"
   2829                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
   2830                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
   2831                    "  IPC_END_MESSAGE_MAP()\n"
   2832                    "}"));
   2833 
   2834   // Same inside macros.
   2835   EXPECT_EQ("#define LIST(L) \\\n"
   2836             "  L(A)          \\\n"
   2837             "  L(B)          \\\n"
   2838             "  L(C)",
   2839             format("#define LIST(L) \\\n"
   2840                    "  L(A) \\\n"
   2841                    "  L(B) \\\n"
   2842                    "  L(C)",
   2843                    getGoogleStyle()));
   2844 
   2845   // These must not be recognized as macros.
   2846   EXPECT_EQ("int q() {\n"
   2847             "  f(x);\n"
   2848             "  f(x) {}\n"
   2849             "  f(x)->g();\n"
   2850             "  f(x)->*g();\n"
   2851             "  f(x).g();\n"
   2852             "  f(x) = x;\n"
   2853             "  f(x) += x;\n"
   2854             "  f(x) -= x;\n"
   2855             "  f(x) *= x;\n"
   2856             "  f(x) /= x;\n"
   2857             "  f(x) %= x;\n"
   2858             "  f(x) &= x;\n"
   2859             "  f(x) |= x;\n"
   2860             "  f(x) ^= x;\n"
   2861             "  f(x) >>= x;\n"
   2862             "  f(x) <<= x;\n"
   2863             "  f(x)[y].z();\n"
   2864             "  LOG(INFO) << x;\n"
   2865             "  ifstream(x) >> x;\n"
   2866             "}\n",
   2867             format("int q() {\n"
   2868                    "  f(x)\n;\n"
   2869                    "  f(x)\n {}\n"
   2870                    "  f(x)\n->g();\n"
   2871                    "  f(x)\n->*g();\n"
   2872                    "  f(x)\n.g();\n"
   2873                    "  f(x)\n = x;\n"
   2874                    "  f(x)\n += x;\n"
   2875                    "  f(x)\n -= x;\n"
   2876                    "  f(x)\n *= x;\n"
   2877                    "  f(x)\n /= x;\n"
   2878                    "  f(x)\n %= x;\n"
   2879                    "  f(x)\n &= x;\n"
   2880                    "  f(x)\n |= x;\n"
   2881                    "  f(x)\n ^= x;\n"
   2882                    "  f(x)\n >>= x;\n"
   2883                    "  f(x)\n <<= x;\n"
   2884                    "  f(x)\n[y].z();\n"
   2885                    "  LOG(INFO)\n << x;\n"
   2886                    "  ifstream(x)\n >> x;\n"
   2887                    "}\n"));
   2888   EXPECT_EQ("int q() {\n"
   2889             "  F(x)\n"
   2890             "  if (1) {\n"
   2891             "  }\n"
   2892             "  F(x)\n"
   2893             "  while (1) {\n"
   2894             "  }\n"
   2895             "  F(x)\n"
   2896             "  G(x);\n"
   2897             "  F(x)\n"
   2898             "  try {\n"
   2899             "    Q();\n"
   2900             "  } catch (...) {\n"
   2901             "  }\n"
   2902             "}\n",
   2903             format("int q() {\n"
   2904                    "F(x)\n"
   2905                    "if (1) {}\n"
   2906                    "F(x)\n"
   2907                    "while (1) {}\n"
   2908                    "F(x)\n"
   2909                    "G(x);\n"
   2910                    "F(x)\n"
   2911                    "try { Q(); } catch (...) {}\n"
   2912                    "}\n"));
   2913   EXPECT_EQ("class A {\n"
   2914             "  A() : t(0) {}\n"
   2915             "  A(int i) noexcept() : {}\n"
   2916             "  A(X x)\n" // FIXME: function-level try blocks are broken.
   2917             "  try : t(0) {\n"
   2918             "  } catch (...) {\n"
   2919             "  }\n"
   2920             "};",
   2921             format("class A {\n"
   2922                    "  A()\n : t(0) {}\n"
   2923                    "  A(int i)\n noexcept() : {}\n"
   2924                    "  A(X x)\n"
   2925                    "  try : t(0) {} catch (...) {}\n"
   2926                    "};"));
   2927   EXPECT_EQ("class SomeClass {\n"
   2928             "public:\n"
   2929             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
   2930             "};",
   2931             format("class SomeClass {\n"
   2932                    "public:\n"
   2933                    "  SomeClass()\n"
   2934                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
   2935                    "};"));
   2936   EXPECT_EQ("class SomeClass {\n"
   2937             "public:\n"
   2938             "  SomeClass()\n"
   2939             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
   2940             "};",
   2941             format("class SomeClass {\n"
   2942                    "public:\n"
   2943                    "  SomeClass()\n"
   2944                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
   2945                    "};",
   2946                    getLLVMStyleWithColumns(40)));
   2947 
   2948   verifyFormat("MACRO(>)");
   2949 }
   2950 
   2951 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
   2952   verifyFormat("#define A \\\n"
   2953                "  f({     \\\n"
   2954                "    g();  \\\n"
   2955                "  });",
   2956                getLLVMStyleWithColumns(11));
   2957 }
   2958 
   2959 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
   2960   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
   2961 }
   2962 
   2963 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
   2964   verifyFormat("{\n  { a #c; }\n}");
   2965 }
   2966 
   2967 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
   2968   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
   2969             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
   2970   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
   2971             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
   2972 }
   2973 
   2974 TEST_F(FormatTest, EscapedNewlines) {
   2975   EXPECT_EQ(
   2976       "#define A \\\n  int i;  \\\n  int j;",
   2977       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
   2978   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
   2979   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
   2980   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
   2981   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
   2982 }
   2983 
   2984 TEST_F(FormatTest, DontCrashOnBlockComments) {
   2985   EXPECT_EQ(
   2986       "int xxxxxxxxx; /* "
   2987       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
   2988       "zzzzzz\n"
   2989       "0*/",
   2990       format("int xxxxxxxxx;                          /* "
   2991              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
   2992              "0*/"));
   2993 }
   2994 
   2995 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
   2996   verifyFormat("#define A \\\n"
   2997                "  int v(  \\\n"
   2998                "      a); \\\n"
   2999                "  int i;",
   3000                getLLVMStyleWithColumns(11));
   3001 }
   3002 
   3003 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
   3004   EXPECT_EQ(
   3005       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
   3006       "                      \\\n"
   3007       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
   3008       "\n"
   3009       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
   3010       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
   3011       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
   3012              "\\\n"
   3013              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
   3014              "  \n"
   3015              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
   3016              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
   3017 }
   3018 
   3019 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
   3020   EXPECT_EQ("int\n"
   3021             "#define A\n"
   3022             "    a;",
   3023             format("int\n#define A\na;"));
   3024   verifyFormat("functionCallTo(\n"
   3025                "    someOtherFunction(\n"
   3026                "        withSomeParameters, whichInSequence,\n"
   3027                "        areLongerThanALine(andAnotherCall,\n"
   3028                "#define A B\n"
   3029                "                           withMoreParamters,\n"
   3030                "                           whichStronglyInfluenceTheLayout),\n"
   3031                "        andMoreParameters),\n"
   3032                "    trailing);",
   3033                getLLVMStyleWithColumns(69));
   3034   verifyFormat("Foo::Foo()\n"
   3035                "#ifdef BAR\n"
   3036                "    : baz(0)\n"
   3037                "#endif\n"
   3038                "{\n"
   3039                "}");
   3040   verifyFormat("void f() {\n"
   3041                "  if (true)\n"
   3042                "#ifdef A\n"
   3043                "    f(42);\n"
   3044                "  x();\n"
   3045                "#else\n"
   3046                "    g();\n"
   3047                "  x();\n"
   3048                "#endif\n"
   3049                "}");
   3050   verifyFormat("void f(param1, param2,\n"
   3051                "       param3,\n"
   3052                "#ifdef A\n"
   3053                "       param4(param5,\n"
   3054                "#ifdef A1\n"
   3055                "              param6,\n"
   3056                "#ifdef A2\n"
   3057                "              param7),\n"
   3058                "#else\n"
   3059                "              param8),\n"
   3060                "       param9,\n"
   3061                "#endif\n"
   3062                "       param10,\n"
   3063                "#endif\n"
   3064                "       param11)\n"
   3065                "#else\n"
   3066                "       param12)\n"
   3067                "#endif\n"
   3068                "{\n"
   3069                "  x();\n"
   3070                "}",
   3071                getLLVMStyleWithColumns(28));
   3072   verifyFormat("#if 1\n"
   3073                "int i;");
   3074   verifyFormat("#if 1\n"
   3075                "#endif\n"
   3076                "#if 1\n"
   3077                "#else\n"
   3078                "#endif\n");
   3079   verifyFormat("DEBUG({\n"
   3080                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3081                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
   3082                "});\n"
   3083                "#if a\n"
   3084                "#else\n"
   3085                "#endif");
   3086 
   3087   verifyIncompleteFormat("void f(\n"
   3088                          "#if A\n"
   3089                          "    );\n"
   3090                          "#else\n"
   3091                          "#endif");
   3092 }
   3093 
   3094 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
   3095   verifyFormat("#endif\n"
   3096                "#if B");
   3097 }
   3098 
   3099 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
   3100   FormatStyle SingleLine = getLLVMStyle();
   3101   SingleLine.AllowShortIfStatementsOnASingleLine = true;
   3102   verifyFormat("#if 0\n"
   3103                "#elif 1\n"
   3104                "#endif\n"
   3105                "void foo() {\n"
   3106                "  if (test) foo2();\n"
   3107                "}",
   3108                SingleLine);
   3109 }
   3110 
   3111 TEST_F(FormatTest, LayoutBlockInsideParens) {
   3112   verifyFormat("functionCall({ int i; });");
   3113   verifyFormat("functionCall({\n"
   3114                "  int i;\n"
   3115                "  int j;\n"
   3116                "});");
   3117   verifyFormat("functionCall(\n"
   3118                "    {\n"
   3119                "      int i;\n"
   3120                "      int j;\n"
   3121                "    },\n"
   3122                "    aaaa, bbbb, cccc);");
   3123   verifyFormat("functionA(functionB({\n"
   3124                "            int i;\n"
   3125                "            int j;\n"
   3126                "          }),\n"
   3127                "          aaaa, bbbb, cccc);");
   3128   verifyFormat("functionCall(\n"
   3129                "    {\n"
   3130                "      int i;\n"
   3131                "      int j;\n"
   3132                "    },\n"
   3133                "    aaaa, bbbb, // comment\n"
   3134                "    cccc);");
   3135   verifyFormat("functionA(functionB({\n"
   3136                "            int i;\n"
   3137                "            int j;\n"
   3138                "          }),\n"
   3139                "          aaaa, bbbb, // comment\n"
   3140                "          cccc);");
   3141   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
   3142   verifyFormat("functionCall(aaaa, bbbb, {\n"
   3143                "  int i;\n"
   3144                "  int j;\n"
   3145                "});");
   3146   verifyFormat(
   3147       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
   3148       "    {\n"
   3149       "      int i; // break\n"
   3150       "    },\n"
   3151       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
   3152       "                                     ccccccccccccccccc));");
   3153   verifyFormat("DEBUG({\n"
   3154                "  if (a)\n"
   3155                "    f();\n"
   3156                "});");
   3157 }
   3158 
   3159 TEST_F(FormatTest, LayoutBlockInsideStatement) {
   3160   EXPECT_EQ("SOME_MACRO { int i; }\n"
   3161             "int i;",
   3162             format("  SOME_MACRO  {int i;}  int i;"));
   3163 }
   3164 
   3165 TEST_F(FormatTest, LayoutNestedBlocks) {
   3166   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
   3167                "  struct s {\n"
   3168                "    int i;\n"
   3169                "  };\n"
   3170                "  s kBitsToOs[] = {{10}};\n"
   3171                "  for (int i = 0; i < 10; ++i)\n"
   3172                "    return;\n"
   3173                "}");
   3174   verifyFormat("call(parameter, {\n"
   3175                "  something();\n"
   3176                "  // Comment using all columns.\n"
   3177                "  somethingelse();\n"
   3178                "});",
   3179                getLLVMStyleWithColumns(40));
   3180   verifyFormat("DEBUG( //\n"
   3181                "    { f(); }, a);");
   3182   verifyFormat("DEBUG( //\n"
   3183                "    {\n"
   3184                "      f(); //\n"
   3185                "    },\n"
   3186                "    a);");
   3187 
   3188   EXPECT_EQ("call(parameter, {\n"
   3189             "  something();\n"
   3190             "  // Comment too\n"
   3191             "  // looooooooooong.\n"
   3192             "  somethingElse();\n"
   3193             "});",
   3194             format("call(parameter, {\n"
   3195                    "  something();\n"
   3196                    "  // Comment too looooooooooong.\n"
   3197                    "  somethingElse();\n"
   3198                    "});",
   3199                    getLLVMStyleWithColumns(29)));
   3200   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
   3201   EXPECT_EQ("DEBUG({ // comment\n"
   3202             "  int i;\n"
   3203             "});",
   3204             format("DEBUG({ // comment\n"
   3205                    "int  i;\n"
   3206                    "});"));
   3207   EXPECT_EQ("DEBUG({\n"
   3208             "  int i;\n"
   3209             "\n"
   3210             "  // comment\n"
   3211             "  int j;\n"
   3212             "});",
   3213             format("DEBUG({\n"
   3214                    "  int  i;\n"
   3215                    "\n"
   3216                    "  // comment\n"
   3217                    "  int  j;\n"
   3218                    "});"));
   3219 
   3220   verifyFormat("DEBUG({\n"
   3221                "  if (a)\n"
   3222                "    return;\n"
   3223                "});");
   3224   verifyGoogleFormat("DEBUG({\n"
   3225                      "  if (a) return;\n"
   3226                      "});");
   3227   FormatStyle Style = getGoogleStyle();
   3228   Style.ColumnLimit = 45;
   3229   verifyFormat("Debug(aaaaa,\n"
   3230                "      {\n"
   3231                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
   3232                "      },\n"
   3233                "      a);",
   3234                Style);
   3235 
   3236   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
   3237 
   3238   verifyNoCrash("^{v^{a}}");
   3239 }
   3240 
   3241 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
   3242   EXPECT_EQ("#define MACRO()                     \\\n"
   3243             "  Debug(aaa, /* force line break */ \\\n"
   3244             "        {                           \\\n"
   3245             "          int i;                    \\\n"
   3246             "          int j;                    \\\n"
   3247             "        })",
   3248             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
   3249                    "          {  int   i;  int  j;   })",
   3250                    getGoogleStyle()));
   3251 
   3252   EXPECT_EQ("#define A                                       \\\n"
   3253             "  [] {                                          \\\n"
   3254             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
   3255             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
   3256             "  }",
   3257             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
   3258                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
   3259                    getGoogleStyle()));
   3260 }
   3261 
   3262 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
   3263   EXPECT_EQ("{}", format("{}"));
   3264   verifyFormat("enum E {};");
   3265   verifyFormat("enum E {}");
   3266 }
   3267 
   3268 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
   3269   FormatStyle Style = getLLVMStyle();
   3270   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
   3271   Style.MacroBlockEnd = "^[A-Z_]+_END$";
   3272   verifyFormat("FOO_BEGIN\n"
   3273                "  FOO_ENTRY\n"
   3274                "FOO_END", Style);
   3275   verifyFormat("FOO_BEGIN\n"
   3276                "  NESTED_FOO_BEGIN\n"
   3277                "    NESTED_FOO_ENTRY\n"
   3278                "  NESTED_FOO_END\n"
   3279                "FOO_END", Style);
   3280   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
   3281                "  int x;\n"
   3282                "  x = 1;\n"
   3283                "FOO_END(Baz)", Style);
   3284 }
   3285 
   3286 //===----------------------------------------------------------------------===//
   3287 // Line break tests.
   3288 //===----------------------------------------------------------------------===//
   3289 
   3290 TEST_F(FormatTest, PreventConfusingIndents) {
   3291   verifyFormat(
   3292       "void f() {\n"
   3293       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
   3294       "                         parameter, parameter, parameter)),\n"
   3295       "                     SecondLongCall(parameter));\n"
   3296       "}");
   3297   verifyFormat(
   3298       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3299       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3300       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   3301       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
   3302   verifyFormat(
   3303       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3304       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
   3305       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
   3306       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
   3307   verifyFormat(
   3308       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
   3309       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
   3310       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
   3311       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
   3312   verifyFormat("int a = bbbb && ccc && fffff(\n"
   3313                "#define A Just forcing a new line\n"
   3314                "                           ddd);");
   3315 }
   3316 
   3317 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
   3318   verifyFormat(
   3319       "bool aaaaaaa =\n"
   3320       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
   3321       "    bbbbbbbb();");
   3322   verifyFormat(
   3323       "bool aaaaaaa =\n"
   3324       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
   3325       "    bbbbbbbb();");
   3326 
   3327   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
   3328                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
   3329                "    ccccccccc == ddddddddddd;");
   3330   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
   3331                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
   3332                "    ccccccccc == ddddddddddd;");
   3333   verifyFormat(
   3334       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
   3335       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
   3336       "    ccccccccc == ddddddddddd;");
   3337 
   3338   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
   3339                "                 aaaaaa) &&\n"
   3340                "         bbbbbb && cccccc;");
   3341   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
   3342                "                 aaaaaa) >>\n"
   3343                "         bbbbbb;");
   3344   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
   3345                "    SourceMgr.getSpellingColumnNumber(\n"
   3346                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
   3347                "    1);");
   3348 
   3349   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   3350                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
   3351                "    cccccc) {\n}");
   3352   verifyFormat("b = a &&\n"
   3353                "    // Comment\n"
   3354                "    b.c && d;");
   3355 
   3356   // If the LHS of a comparison is not a binary expression itself, the
   3357   // additional linebreak confuses many people.
   3358   verifyFormat(
   3359       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3360       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
   3361       "}");
   3362   verifyFormat(
   3363       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3364       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
   3365       "}");
   3366   verifyFormat(
   3367       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
   3368       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
   3369       "}");
   3370   // Even explicit parentheses stress the precedence enough to make the
   3371   // additional break unnecessary.
   3372   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3373                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
   3374                "}");
   3375   // This cases is borderline, but with the indentation it is still readable.
   3376   verifyFormat(
   3377       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3378       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3379       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
   3380       "}",
   3381       getLLVMStyleWithColumns(75));
   3382 
   3383   // If the LHS is a binary expression, we should still use the additional break
   3384   // as otherwise the formatting hides the operator precedence.
   3385   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3386                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
   3387                "    5) {\n"
   3388                "}");
   3389 
   3390   FormatStyle OnePerLine = getLLVMStyle();
   3391   OnePerLine.BinPackParameters = false;
   3392   verifyFormat(
   3393       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   3394       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   3395       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
   3396       OnePerLine);
   3397 }
   3398 
   3399 TEST_F(FormatTest, ExpressionIndentation) {
   3400   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3401                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3402                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
   3403                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
   3404                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
   3405                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
   3406                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
   3407                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
   3408                "                 ccccccccccccccccccccccccccccccccccccccccc;");
   3409   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
   3410                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3411                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
   3412                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
   3413   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3414                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
   3415                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
   3416                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
   3417   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
   3418                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
   3419                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   3420                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
   3421   verifyFormat("if () {\n"
   3422                "} else if (aaaaa &&\n"
   3423                "           bbbbb > // break\n"
   3424                "               ccccc) {\n"
   3425                "}");
   3426 
   3427   // Presence of a trailing comment used to change indentation of b.
   3428   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
   3429                "       b;\n"
   3430                "return aaaaaaaaaaaaaaaaaaa +\n"
   3431                "       b; //",
   3432                getLLVMStyleWithColumns(30));
   3433 }
   3434 
   3435 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
   3436   // Not sure what the best system is here. Like this, the LHS can be found
   3437   // immediately above an operator (everything with the same or a higher
   3438   // indent). The RHS is aligned right of the operator and so compasses
   3439   // everything until something with the same indent as the operator is found.
   3440   // FIXME: Is this a good system?
   3441   FormatStyle Style = getLLVMStyle();
   3442   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
   3443   verifyFormat(
   3444       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3445       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3446       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3447       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3448       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3449       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3450       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3451       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3452       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
   3453       Style);
   3454   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3455                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3456                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3457                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
   3458                Style);
   3459   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3460                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3461                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3462                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
   3463                Style);
   3464   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3465                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3466                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3467                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
   3468                Style);
   3469   verifyFormat("if () {\n"
   3470                "} else if (aaaaa\n"
   3471                "           && bbbbb // break\n"
   3472                "                  > ccccc) {\n"
   3473                "}",
   3474                Style);
   3475   verifyFormat("return (a)\n"
   3476                "       // comment\n"
   3477                "       + b;",
   3478                Style);
   3479   verifyFormat(
   3480       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3481       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3482       "             + cc;",
   3483       Style);
   3484 
   3485   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3486                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
   3487                Style);
   3488 
   3489   // Forced by comments.
   3490   verifyFormat(
   3491       "unsigned ContentSize =\n"
   3492       "    sizeof(int16_t)   // DWARF ARange version number\n"
   3493       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
   3494       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
   3495       "    + sizeof(int8_t); // Segment Size (in bytes)");
   3496 
   3497   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
   3498                "       == boost::fusion::at_c<1>(iiii).second;",
   3499                Style);
   3500 
   3501   Style.ColumnLimit = 60;
   3502   verifyFormat("zzzzzzzzzz\n"
   3503                "    = bbbbbbbbbbbbbbbbb\n"
   3504                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
   3505                Style);
   3506 }
   3507 
   3508 TEST_F(FormatTest, NoOperandAlignment) {
   3509   FormatStyle Style = getLLVMStyle();
   3510   Style.AlignOperands = false;
   3511   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
   3512   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3513                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3514                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3515                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3516                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3517                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3518                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3519                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3520                "        > ccccccccccccccccccccccccccccccccccccccccc;",
   3521                Style);
   3522 
   3523   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3524                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3525                "    + cc;",
   3526                Style);
   3527   verifyFormat("int a = aa\n"
   3528                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
   3529                "        * cccccccccccccccccccccccccccccccccccc;",
   3530                Style);
   3531 
   3532   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
   3533   verifyFormat("return (a > b\n"
   3534                "    // comment1\n"
   3535                "    // comment2\n"
   3536                "    || c);",
   3537                Style);
   3538 }
   3539 
   3540 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
   3541   FormatStyle Style = getLLVMStyle();
   3542   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
   3543   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   3544                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3545                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
   3546                Style);
   3547 }
   3548 
   3549 TEST_F(FormatTest, ConstructorInitializers) {
   3550   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
   3551   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
   3552                getLLVMStyleWithColumns(45));
   3553   verifyFormat("Constructor()\n"
   3554                "    : Inttializer(FitsOnTheLine) {}",
   3555                getLLVMStyleWithColumns(44));
   3556   verifyFormat("Constructor()\n"
   3557                "    : Inttializer(FitsOnTheLine) {}",
   3558                getLLVMStyleWithColumns(43));
   3559 
   3560   verifyFormat("template <typename T>\n"
   3561                "Constructor() : Initializer(FitsOnTheLine) {}",
   3562                getLLVMStyleWithColumns(45));
   3563 
   3564   verifyFormat(
   3565       "SomeClass::Constructor()\n"
   3566       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
   3567 
   3568   verifyFormat(
   3569       "SomeClass::Constructor()\n"
   3570       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   3571       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
   3572   verifyFormat(
   3573       "SomeClass::Constructor()\n"
   3574       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   3575       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
   3576   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   3577                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   3578                "    : aaaaaaaaaa(aaaaaa) {}");
   3579 
   3580   verifyFormat("Constructor()\n"
   3581                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   3582                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   3583                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   3584                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
   3585 
   3586   verifyFormat("Constructor()\n"
   3587                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3588                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
   3589 
   3590   verifyFormat("Constructor(int Parameter = 0)\n"
   3591                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
   3592                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
   3593   verifyFormat("Constructor()\n"
   3594                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
   3595                "}",
   3596                getLLVMStyleWithColumns(60));
   3597   verifyFormat("Constructor()\n"
   3598                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3599                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
   3600 
   3601   // Here a line could be saved by splitting the second initializer onto two
   3602   // lines, but that is not desirable.
   3603   verifyFormat("Constructor()\n"
   3604                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
   3605                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
   3606                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
   3607 
   3608   FormatStyle OnePerLine = getLLVMStyle();
   3609   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   3610   verifyFormat("SomeClass::Constructor()\n"
   3611                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   3612                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   3613                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
   3614                OnePerLine);
   3615   verifyFormat("SomeClass::Constructor()\n"
   3616                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
   3617                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   3618                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
   3619                OnePerLine);
   3620   verifyFormat("MyClass::MyClass(int var)\n"
   3621                "    : some_var_(var),            // 4 space indent\n"
   3622                "      some_other_var_(var + 1) { // lined up\n"
   3623                "}",
   3624                OnePerLine);
   3625   verifyFormat("Constructor()\n"
   3626                "    : aaaaa(aaaaaa),\n"
   3627                "      aaaaa(aaaaaa),\n"
   3628                "      aaaaa(aaaaaa),\n"
   3629                "      aaaaa(aaaaaa),\n"
   3630                "      aaaaa(aaaaaa) {}",
   3631                OnePerLine);
   3632   verifyFormat("Constructor()\n"
   3633                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
   3634                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
   3635                OnePerLine);
   3636   OnePerLine.ColumnLimit = 60;
   3637   verifyFormat("Constructor()\n"
   3638                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
   3639                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
   3640                OnePerLine);
   3641 
   3642   EXPECT_EQ("Constructor()\n"
   3643             "    : // Comment forcing unwanted break.\n"
   3644             "      aaaa(aaaa) {}",
   3645             format("Constructor() :\n"
   3646                    "    // Comment forcing unwanted break.\n"
   3647                    "    aaaa(aaaa) {}"));
   3648 }
   3649 
   3650 TEST_F(FormatTest, MemoizationTests) {
   3651   // This breaks if the memoization lookup does not take \c Indent and
   3652   // \c LastSpace into account.
   3653   verifyFormat(
   3654       "extern CFRunLoopTimerRef\n"
   3655       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
   3656       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
   3657       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
   3658       "                     CFRunLoopTimerContext *context) {}");
   3659 
   3660   // Deep nesting somewhat works around our memoization.
   3661   verifyFormat(
   3662       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
   3663       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
   3664       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
   3665       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
   3666       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
   3667       getLLVMStyleWithColumns(65));
   3668   verifyFormat(
   3669       "aaaaa(\n"
   3670       "    aaaaa,\n"
   3671       "    aaaaa(\n"
   3672       "        aaaaa,\n"
   3673       "        aaaaa(\n"
   3674       "            aaaaa,\n"
   3675       "            aaaaa(\n"
   3676       "                aaaaa,\n"
   3677       "                aaaaa(\n"
   3678       "                    aaaaa,\n"
   3679       "                    aaaaa(\n"
   3680       "                        aaaaa,\n"
   3681       "                        aaaaa(\n"
   3682       "                            aaaaa,\n"
   3683       "                            aaaaa(\n"
   3684       "                                aaaaa,\n"
   3685       "                                aaaaa(\n"
   3686       "                                    aaaaa,\n"
   3687       "                                    aaaaa(\n"
   3688       "                                        aaaaa,\n"
   3689       "                                        aaaaa(\n"
   3690       "                                            aaaaa,\n"
   3691       "                                            aaaaa(\n"
   3692       "                                                aaaaa,\n"
   3693       "                                                aaaaa))))))))))));",
   3694       getLLVMStyleWithColumns(65));
   3695   verifyFormat(
   3696       "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
   3697       "                                  a),\n"
   3698       "                                a),\n"
   3699       "                              a),\n"
   3700       "                            a),\n"
   3701       "                          a),\n"
   3702       "                        a),\n"
   3703       "                      a),\n"
   3704       "                    a),\n"
   3705       "                  a),\n"
   3706       "                a),\n"
   3707       "              a),\n"
   3708       "            a),\n"
   3709       "          a),\n"
   3710       "        a),\n"
   3711       "      a),\n"
   3712       "    a),\n"
   3713       "  a)",
   3714       getLLVMStyleWithColumns(65));
   3715 
   3716   // This test takes VERY long when memoization is broken.
   3717   FormatStyle OnePerLine = getLLVMStyle();
   3718   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   3719   OnePerLine.BinPackParameters = false;
   3720   std::string input = "Constructor()\n"
   3721                       "    : aaaa(a,\n";
   3722   for (unsigned i = 0, e = 80; i != e; ++i) {
   3723     input += "           a,\n";
   3724   }
   3725   input += "           a) {}";
   3726   verifyFormat(input, OnePerLine);
   3727 }
   3728 
   3729 TEST_F(FormatTest, BreaksAsHighAsPossible) {
   3730   verifyFormat(
   3731       "void f() {\n"
   3732       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
   3733       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
   3734       "    f();\n"
   3735       "}");
   3736   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
   3737                "    Intervals[i - 1].getRange().getLast()) {\n}");
   3738 }
   3739 
   3740 TEST_F(FormatTest, BreaksFunctionDeclarations) {
   3741   // Principially, we break function declarations in a certain order:
   3742   // 1) break amongst arguments.
   3743   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
   3744                "                              Cccccccccccccc cccccccccccccc);");
   3745   verifyFormat("template <class TemplateIt>\n"
   3746                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
   3747                "                            TemplateIt *stop) {}");
   3748 
   3749   // 2) break after return type.
   3750   verifyFormat(
   3751       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3752       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
   3753       getGoogleStyle());
   3754 
   3755   // 3) break after (.
   3756   verifyFormat(
   3757       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
   3758       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
   3759       getGoogleStyle());
   3760 
   3761   // 4) break before after nested name specifiers.
   3762   verifyFormat(
   3763       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3764       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
   3765       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
   3766       getGoogleStyle());
   3767 
   3768   // However, there are exceptions, if a sufficient amount of lines can be
   3769   // saved.
   3770   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
   3771   // more adjusting.
   3772   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
   3773                "                                  Cccccccccccccc cccccccccc,\n"
   3774                "                                  Cccccccccccccc cccccccccc,\n"
   3775                "                                  Cccccccccccccc cccccccccc,\n"
   3776                "                                  Cccccccccccccc cccccccccc);");
   3777   verifyFormat(
   3778       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3779       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
   3780       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
   3781       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
   3782       getGoogleStyle());
   3783   verifyFormat(
   3784       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
   3785       "                                          Cccccccccccccc cccccccccc,\n"
   3786       "                                          Cccccccccccccc cccccccccc,\n"
   3787       "                                          Cccccccccccccc cccccccccc,\n"
   3788       "                                          Cccccccccccccc cccccccccc,\n"
   3789       "                                          Cccccccccccccc cccccccccc,\n"
   3790       "                                          Cccccccccccccc cccccccccc);");
   3791   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
   3792                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
   3793                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
   3794                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
   3795                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
   3796 
   3797   // Break after multi-line parameters.
   3798   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3799                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3800                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   3801                "    bbbb bbbb);");
   3802   verifyFormat("void SomeLoooooooooooongFunction(\n"
   3803                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
   3804                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   3805                "    int bbbbbbbbbbbbb);");
   3806 
   3807   // Treat overloaded operators like other functions.
   3808   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
   3809                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
   3810   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
   3811                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
   3812   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
   3813                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
   3814   verifyGoogleFormat(
   3815       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
   3816       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
   3817   verifyGoogleFormat(
   3818       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
   3819       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
   3820   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3821                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
   3822   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
   3823                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
   3824   verifyGoogleFormat(
   3825       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
   3826       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3827       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
   3828 
   3829   FormatStyle Style = getLLVMStyle();
   3830   Style.PointerAlignment = FormatStyle::PAS_Left;
   3831   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3832                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
   3833                Style);
   3834   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
   3835                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
   3836                Style);
   3837 }
   3838 
   3839 TEST_F(FormatTest, TrailingReturnType) {
   3840   verifyFormat("auto foo() -> int;\n");
   3841   verifyFormat("struct S {\n"
   3842                "  auto bar() const -> int;\n"
   3843                "};");
   3844   verifyFormat("template <size_t Order, typename T>\n"
   3845                "auto load_img(const std::string &filename)\n"
   3846                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
   3847   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
   3848                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
   3849   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
   3850   verifyFormat("template <typename T>\n"
   3851                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
   3852                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
   3853 
   3854   // Not trailing return types.
   3855   verifyFormat("void f() { auto a = b->c(); }");
   3856 }
   3857 
   3858 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
   3859   // Avoid breaking before trailing 'const' or other trailing annotations, if
   3860   // they are not function-like.
   3861   FormatStyle Style = getGoogleStyle();
   3862   Style.ColumnLimit = 47;
   3863   verifyFormat("void someLongFunction(\n"
   3864                "    int someLoooooooooooooongParameter) const {\n}",
   3865                getLLVMStyleWithColumns(47));
   3866   verifyFormat("LoooooongReturnType\n"
   3867                "someLoooooooongFunction() const {}",
   3868                getLLVMStyleWithColumns(47));
   3869   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
   3870                "    const {}",
   3871                Style);
   3872   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
   3873                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
   3874   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
   3875                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
   3876   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
   3877                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
   3878   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
   3879                "                   aaaaaaaaaaa aaaaa) const override;");
   3880   verifyGoogleFormat(
   3881       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   3882       "    const override;");
   3883 
   3884   // Even if the first parameter has to be wrapped.
   3885   verifyFormat("void someLongFunction(\n"
   3886                "    int someLongParameter) const {}",
   3887                getLLVMStyleWithColumns(46));
   3888   verifyFormat("void someLongFunction(\n"
   3889                "    int someLongParameter) const {}",
   3890                Style);
   3891   verifyFormat("void someLongFunction(\n"
   3892                "    int someLongParameter) override {}",
   3893                Style);
   3894   verifyFormat("void someLongFunction(\n"
   3895                "    int someLongParameter) OVERRIDE {}",
   3896                Style);
   3897   verifyFormat("void someLongFunction(\n"
   3898                "    int someLongParameter) final {}",
   3899                Style);
   3900   verifyFormat("void someLongFunction(\n"
   3901                "    int someLongParameter) FINAL {}",
   3902                Style);
   3903   verifyFormat("void someLongFunction(\n"
   3904                "    int parameter) const override {}",
   3905                Style);
   3906 
   3907   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
   3908   verifyFormat("void someLongFunction(\n"
   3909                "    int someLongParameter) const\n"
   3910                "{\n"
   3911                "}",
   3912                Style);
   3913 
   3914   // Unless these are unknown annotations.
   3915   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
   3916                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   3917                "    LONG_AND_UGLY_ANNOTATION;");
   3918 
   3919   // Breaking before function-like trailing annotations is fine to keep them
   3920   // close to their arguments.
   3921   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   3922                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
   3923   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
   3924                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
   3925   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
   3926                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
   3927   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
   3928                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
   3929   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
   3930 
   3931   verifyFormat(
   3932       "void aaaaaaaaaaaaaaaaaa()\n"
   3933       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
   3934       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
   3935   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3936                "    __attribute__((unused));");
   3937   verifyGoogleFormat(
   3938       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3939       "    GUARDED_BY(aaaaaaaaaaaa);");
   3940   verifyGoogleFormat(
   3941       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3942       "    GUARDED_BY(aaaaaaaaaaaa);");
   3943   verifyGoogleFormat(
   3944       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
   3945       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   3946   verifyGoogleFormat(
   3947       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
   3948       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
   3949 }
   3950 
   3951 TEST_F(FormatTest, FunctionAnnotations) {
   3952   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
   3953                "int OldFunction(const string &parameter) {}");
   3954   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
   3955                "string OldFunction(const string &parameter) {}");
   3956   verifyFormat("template <typename T>\n"
   3957                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
   3958                "string OldFunction(const string &parameter) {}");
   3959 
   3960   // Not function annotations.
   3961   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   3962                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
   3963   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
   3964                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
   3965 }
   3966 
   3967 TEST_F(FormatTest, BreaksDesireably) {
   3968   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
   3969                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
   3970                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
   3971   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3972                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
   3973                "}");
   3974 
   3975   verifyFormat(
   3976       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   3977       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
   3978 
   3979   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3980                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3981                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
   3982 
   3983   verifyFormat(
   3984       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3985       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
   3986       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3987       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
   3988 
   3989   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   3990                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   3991 
   3992   verifyFormat(
   3993       "void f() {\n"
   3994       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
   3995       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
   3996       "}");
   3997   verifyFormat(
   3998       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   3999       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
   4000   verifyFormat(
   4001       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4002       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
   4003   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4004                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4005                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4006 
   4007   // Indent consistently independent of call expression and unary operator.
   4008   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
   4009                "    dddddddddddddddddddddddddddddd));");
   4010   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
   4011                "    dddddddddddddddddddddddddddddd));");
   4012   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
   4013                "    dddddddddddddddddddddddddddddd));");
   4014 
   4015   // This test case breaks on an incorrect memoization, i.e. an optimization not
   4016   // taking into account the StopAt value.
   4017   verifyFormat(
   4018       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
   4019       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
   4020       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
   4021       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4022 
   4023   verifyFormat("{\n  {\n    {\n"
   4024                "      Annotation.SpaceRequiredBefore =\n"
   4025                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
   4026                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
   4027                "    }\n  }\n}");
   4028 
   4029   // Break on an outer level if there was a break on an inner level.
   4030   EXPECT_EQ("f(g(h(a, // comment\n"
   4031             "      b, c),\n"
   4032             "    d, e),\n"
   4033             "  x, y);",
   4034             format("f(g(h(a, // comment\n"
   4035                    "    b, c), d, e), x, y);"));
   4036 
   4037   // Prefer breaking similar line breaks.
   4038   verifyFormat(
   4039       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
   4040       "                             NSTrackingMouseEnteredAndExited |\n"
   4041       "                             NSTrackingActiveAlways;");
   4042 }
   4043 
   4044 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
   4045   FormatStyle NoBinPacking = getGoogleStyle();
   4046   NoBinPacking.BinPackParameters = false;
   4047   NoBinPacking.BinPackArguments = true;
   4048   verifyFormat("void f() {\n"
   4049                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
   4050                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
   4051                "}",
   4052                NoBinPacking);
   4053   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
   4054                "       int aaaaaaaaaaaaaaaaaaaa,\n"
   4055                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
   4056                NoBinPacking);
   4057 }
   4058 
   4059 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
   4060   FormatStyle NoBinPacking = getGoogleStyle();
   4061   NoBinPacking.BinPackParameters = false;
   4062   NoBinPacking.BinPackArguments = false;
   4063   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
   4064                "  aaaaaaaaaaaaaaaaaaaa,\n"
   4065                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
   4066                NoBinPacking);
   4067   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
   4068                "        aaaaaaaaaaaaa,\n"
   4069                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
   4070                NoBinPacking);
   4071   verifyFormat(
   4072       "aaaaaaaa(aaaaaaaaaaaaa,\n"
   4073       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4074       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
   4075       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4076       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
   4077       NoBinPacking);
   4078   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
   4079                "    .aaaaaaaaaaaaaaaaaa();",
   4080                NoBinPacking);
   4081   verifyFormat("void f() {\n"
   4082                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4083                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
   4084                "}",
   4085                NoBinPacking);
   4086 
   4087   verifyFormat(
   4088       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4089       "             aaaaaaaaaaaa,\n"
   4090       "             aaaaaaaaaaaa);",
   4091       NoBinPacking);
   4092   verifyFormat(
   4093       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
   4094       "                               ddddddddddddddddddddddddddddd),\n"
   4095       "             test);",
   4096       NoBinPacking);
   4097 
   4098   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
   4099                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
   4100                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
   4101                "    aaaaaaaaaaaaaaaaaa;",
   4102                NoBinPacking);
   4103   verifyFormat("a(\"a\"\n"
   4104                "  \"a\",\n"
   4105                "  a);");
   4106 
   4107   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
   4108   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
   4109                "                aaaaaaaaa,\n"
   4110                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4111                NoBinPacking);
   4112   verifyFormat(
   4113       "void f() {\n"
   4114       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
   4115       "      .aaaaaaa();\n"
   4116       "}",
   4117       NoBinPacking);
   4118   verifyFormat(
   4119       "template <class SomeType, class SomeOtherType>\n"
   4120       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
   4121       NoBinPacking);
   4122 }
   4123 
   4124 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
   4125   FormatStyle Style = getLLVMStyleWithColumns(15);
   4126   Style.ExperimentalAutoDetectBinPacking = true;
   4127   EXPECT_EQ("aaa(aaaa,\n"
   4128             "    aaaa,\n"
   4129             "    aaaa);\n"
   4130             "aaa(aaaa,\n"
   4131             "    aaaa,\n"
   4132             "    aaaa);",
   4133             format("aaa(aaaa,\n" // one-per-line
   4134                    "  aaaa,\n"
   4135                    "    aaaa  );\n"
   4136                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
   4137                    Style));
   4138   EXPECT_EQ("aaa(aaaa, aaaa,\n"
   4139             "    aaaa);\n"
   4140             "aaa(aaaa, aaaa,\n"
   4141             "    aaaa);",
   4142             format("aaa(aaaa,  aaaa,\n" // bin-packed
   4143                    "    aaaa  );\n"
   4144                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
   4145                    Style));
   4146 }
   4147 
   4148 TEST_F(FormatTest, FormatsBuilderPattern) {
   4149   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
   4150                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
   4151                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
   4152                "    .StartsWith(\".init\", ORDER_INIT)\n"
   4153                "    .StartsWith(\".fini\", ORDER_FINI)\n"
   4154                "    .StartsWith(\".hash\", ORDER_HASH)\n"
   4155                "    .Default(ORDER_TEXT);\n");
   4156 
   4157   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
   4158                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
   4159   verifyFormat(
   4160       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4161       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4162       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
   4163   verifyFormat(
   4164       "aaaaaaa->aaaaaaa\n"
   4165       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4166       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
   4167   verifyFormat(
   4168       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
   4169       "    aaaaaaaaaaaaaa);");
   4170   verifyFormat(
   4171       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
   4172       "    aaaaaa->aaaaaaaaaaaa()\n"
   4173       "        ->aaaaaaaaaaaaaaaa(\n"
   4174       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4175       "        ->aaaaaaaaaaaaaaaaa();");
   4176   verifyGoogleFormat(
   4177       "void f() {\n"
   4178       "  someo->Add((new util::filetools::Handler(dir))\n"
   4179       "                 ->OnEvent1(NewPermanentCallback(\n"
   4180       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
   4181       "                 ->OnEvent2(NewPermanentCallback(\n"
   4182       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
   4183       "                 ->OnEvent3(NewPermanentCallback(\n"
   4184       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
   4185       "                 ->OnEvent5(NewPermanentCallback(\n"
   4186       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
   4187       "                 ->OnEvent6(NewPermanentCallback(\n"
   4188       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
   4189       "}");
   4190 
   4191   verifyFormat(
   4192       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
   4193   verifyFormat("aaaaaaaaaaaaaaa()\n"
   4194                "    .aaaaaaaaaaaaaaa()\n"
   4195                "    .aaaaaaaaaaaaaaa()\n"
   4196                "    .aaaaaaaaaaaaaaa()\n"
   4197                "    .aaaaaaaaaaaaaaa();");
   4198   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
   4199                "    .aaaaaaaaaaaaaaa()\n"
   4200                "    .aaaaaaaaaaaaaaa()\n"
   4201                "    .aaaaaaaaaaaaaaa();");
   4202   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
   4203                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
   4204                "    .aaaaaaaaaaaaaaa();");
   4205   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4206                "    ->aaaaaaaaaaaaaae(0)\n"
   4207                "    ->aaaaaaaaaaaaaaa();");
   4208 
   4209   // Don't linewrap after very short segments.
   4210   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4211                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4212                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   4213   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4214                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4215                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   4216   verifyFormat("aaa()\n"
   4217                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4218                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4219                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   4220 
   4221   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
   4222                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
   4223                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
   4224   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
   4225                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
   4226                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
   4227 
   4228   // Prefer not to break after empty parentheses.
   4229   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
   4230                "    First->LastNewlineOffset);");
   4231 
   4232   // Prefer not to create "hanging" indents.
   4233   verifyFormat(
   4234       "return !soooooooooooooome_map\n"
   4235       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4236       "            .second;");
   4237 }
   4238 
   4239 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
   4240   verifyFormat(
   4241       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   4242       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
   4243   verifyFormat(
   4244       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
   4245       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
   4246 
   4247   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
   4248                "    ccccccccccccccccccccccccc) {\n}");
   4249   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
   4250                "    ccccccccccccccccccccccccc) {\n}");
   4251 
   4252   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
   4253                "    ccccccccccccccccccccccccc) {\n}");
   4254   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
   4255                "    ccccccccccccccccccccccccc) {\n}");
   4256 
   4257   verifyFormat(
   4258       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
   4259       "    ccccccccccccccccccccccccc) {\n}");
   4260   verifyFormat(
   4261       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
   4262       "    ccccccccccccccccccccccccc) {\n}");
   4263 
   4264   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
   4265                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
   4266                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
   4267                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
   4268   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
   4269                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
   4270                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
   4271                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
   4272 
   4273   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
   4274                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
   4275                "    aaaaaaaaaaaaaaa != aa) {\n}");
   4276   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
   4277                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
   4278                "    aaaaaaaaaaaaaaa != aa) {\n}");
   4279 }
   4280 
   4281 TEST_F(FormatTest, BreaksAfterAssignments) {
   4282   verifyFormat(
   4283       "unsigned Cost =\n"
   4284       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
   4285       "                        SI->getPointerAddressSpaceee());\n");
   4286   verifyFormat(
   4287       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
   4288       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
   4289 
   4290   verifyFormat(
   4291       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
   4292       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
   4293   verifyFormat("unsigned OriginalStartColumn =\n"
   4294                "    SourceMgr.getSpellingColumnNumber(\n"
   4295                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
   4296                "    1;");
   4297 }
   4298 
   4299 TEST_F(FormatTest, AlignsAfterAssignments) {
   4300   verifyFormat(
   4301       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4302       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
   4303   verifyFormat(
   4304       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4305       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
   4306   verifyFormat(
   4307       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4308       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
   4309   verifyFormat(
   4310       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4311       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
   4312   verifyFormat(
   4313       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4314       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4315       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
   4316 }
   4317 
   4318 TEST_F(FormatTest, AlignsAfterReturn) {
   4319   verifyFormat(
   4320       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4321       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
   4322   verifyFormat(
   4323       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4324       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
   4325   verifyFormat(
   4326       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
   4327       "       aaaaaaaaaaaaaaaaaaaaaa();");
   4328   verifyFormat(
   4329       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
   4330       "        aaaaaaaaaaaaaaaaaaaaaa());");
   4331   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4332                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4333   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4334                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
   4335                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   4336   verifyFormat("return\n"
   4337                "    // true if code is one of a or b.\n"
   4338                "    code == a || code == b;");
   4339 }
   4340 
   4341 TEST_F(FormatTest, AlignsAfterOpenBracket) {
   4342   verifyFormat(
   4343       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
   4344       "                                                aaaaaaaaa aaaaaaa) {}");
   4345   verifyFormat(
   4346       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
   4347       "                                               aaaaaaaaaaa aaaaaaaaa);");
   4348   verifyFormat(
   4349       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
   4350       "                                             aaaaaaaaaaaaaaaaaaaaa));");
   4351   FormatStyle Style = getLLVMStyle();
   4352   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
   4353   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4354                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
   4355                Style);
   4356   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
   4357                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
   4358                Style);
   4359   verifyFormat("SomeLongVariableName->someFunction(\n"
   4360                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
   4361                Style);
   4362   verifyFormat(
   4363       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
   4364       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
   4365       Style);
   4366   verifyFormat(
   4367       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
   4368       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4369       Style);
   4370   verifyFormat(
   4371       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
   4372       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
   4373       Style);
   4374 
   4375   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
   4376   Style.BinPackArguments = false;
   4377   Style.BinPackParameters = false;
   4378   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4379                "    aaaaaaaaaaa aaaaaaaa,\n"
   4380                "    aaaaaaaaa aaaaaaa,\n"
   4381                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
   4382                Style);
   4383   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
   4384                "    aaaaaaaaaaa aaaaaaaaa,\n"
   4385                "    aaaaaaaaaaa aaaaaaaaa,\n"
   4386                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4387                Style);
   4388   verifyFormat("SomeLongVariableName->someFunction(\n"
   4389                "    foooooooo(\n"
   4390                "        aaaaaaaaaaaaaaa,\n"
   4391                "        aaaaaaaaaaaaaaaaaaaaa,\n"
   4392                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
   4393                Style);
   4394 }
   4395 
   4396 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
   4397   FormatStyle Style = getLLVMStyleWithColumns(40);
   4398   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
   4399                "          bbbbbbbbbbbbbbbbbbbbbb);",
   4400                Style);
   4401   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
   4402   Style.AlignOperands = false;
   4403   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
   4404                "          bbbbbbbbbbbbbbbbbbbbbb);",
   4405                Style);
   4406   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
   4407   Style.AlignOperands = true;
   4408   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
   4409                "          bbbbbbbbbbbbbbbbbbbbbb);",
   4410                Style);
   4411   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
   4412   Style.AlignOperands = false;
   4413   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
   4414                "    bbbbbbbbbbbbbbbbbbbbbb);",
   4415                Style);
   4416 }
   4417 
   4418 TEST_F(FormatTest, BreaksConditionalExpressions) {
   4419   verifyFormat(
   4420       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4421       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4422       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4423   verifyFormat(
   4424       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4425       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4426   verifyFormat(
   4427       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
   4428       "                                                    : aaaaaaaaaaaaa);");
   4429   verifyFormat(
   4430       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4431       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4432       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4433       "                   aaaaaaaaaaaaa);");
   4434   verifyFormat(
   4435       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4436       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4437       "                   aaaaaaaaaaaaa);");
   4438   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4439                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4440                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4441                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4442                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4443   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4444                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4445                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4446                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4447                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4448                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   4449                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4450   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4451                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4452                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4453                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   4454                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4455   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4456                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4457                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   4458   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
   4459                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4460                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4461                "        : aaaaaaaaaaaaaaaa;");
   4462   verifyFormat(
   4463       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4464       "    ? aaaaaaaaaaaaaaa\n"
   4465       "    : aaaaaaaaaaaaaaa;");
   4466   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
   4467                "          aaaaaaaaa\n"
   4468                "      ? b\n"
   4469                "      : c);");
   4470   verifyFormat("return aaaa == bbbb\n"
   4471                "           // comment\n"
   4472                "           ? aaaa\n"
   4473                "           : bbbb;");
   4474   verifyFormat("unsigned Indent =\n"
   4475                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
   4476                "                              ? IndentForLevel[TheLine.Level]\n"
   4477                "                              : TheLine * 2,\n"
   4478                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
   4479                getLLVMStyleWithColumns(70));
   4480   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
   4481                "                  ? aaaaaaaaaaaaaaa\n"
   4482                "                  : bbbbbbbbbbbbbbb //\n"
   4483                "                        ? ccccccccccccccc\n"
   4484                "                        : ddddddddddddddd;");
   4485   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
   4486                "                  ? aaaaaaaaaaaaaaa\n"
   4487                "                  : (bbbbbbbbbbbbbbb //\n"
   4488                "                         ? ccccccccccccccc\n"
   4489                "                         : ddddddddddddddd);");
   4490   verifyFormat(
   4491       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4492       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
   4493       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
   4494       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
   4495       "                                      : aaaaaaaaaa;");
   4496   verifyFormat(
   4497       "aaaaaa = aaaaaaaaaaaa\n"
   4498       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4499       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
   4500       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   4501 
   4502   FormatStyle NoBinPacking = getLLVMStyle();
   4503   NoBinPacking.BinPackArguments = false;
   4504   verifyFormat(
   4505       "void f() {\n"
   4506       "  g(aaa,\n"
   4507       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
   4508       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4509       "        ? aaaaaaaaaaaaaaa\n"
   4510       "        : aaaaaaaaaaaaaaa);\n"
   4511       "}",
   4512       NoBinPacking);
   4513   verifyFormat(
   4514       "void f() {\n"
   4515       "  g(aaa,\n"
   4516       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
   4517       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4518       "        ?: aaaaaaaaaaaaaaa);\n"
   4519       "}",
   4520       NoBinPacking);
   4521 
   4522   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
   4523                "             // comment.\n"
   4524                "             ccccccccccccccccccccccccccccccccccccccc\n"
   4525                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4526                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
   4527 
   4528   // Assignments in conditional expressions. Apparently not uncommon :-(.
   4529   verifyFormat("return a != b\n"
   4530                "           // comment\n"
   4531                "           ? a = b\n"
   4532                "           : a = b;");
   4533   verifyFormat("return a != b\n"
   4534                "           // comment\n"
   4535                "           ? a = a != b\n"
   4536                "                     // comment\n"
   4537                "                     ? a = b\n"
   4538                "                     : a\n"
   4539                "           : a;\n");
   4540   verifyFormat("return a != b\n"
   4541                "           // comment\n"
   4542                "           ? a\n"
   4543                "           : a = a != b\n"
   4544                "                     // comment\n"
   4545                "                     ? a = b\n"
   4546                "                     : a;");
   4547 }
   4548 
   4549 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
   4550   FormatStyle Style = getLLVMStyle();
   4551   Style.BreakBeforeTernaryOperators = false;
   4552   Style.ColumnLimit = 70;
   4553   verifyFormat(
   4554       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
   4555       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
   4556       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4557       Style);
   4558   verifyFormat(
   4559       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
   4560       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4561       Style);
   4562   verifyFormat(
   4563       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
   4564       "                                                      aaaaaaaaaaaaa);",
   4565       Style);
   4566   verifyFormat(
   4567       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4568       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
   4569       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4570       "                   aaaaaaaaaaaaa);",
   4571       Style);
   4572   verifyFormat(
   4573       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4574       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4575       "                   aaaaaaaaaaaaa);",
   4576       Style);
   4577   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
   4578                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4579                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
   4580                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4581                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4582                Style);
   4583   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4584                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
   4585                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4586                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
   4587                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4588                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   4589                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4590                Style);
   4591   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4592                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
   4593                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4594                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
   4595                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   4596                Style);
   4597   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
   4598                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
   4599                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
   4600                Style);
   4601   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
   4602                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
   4603                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
   4604                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
   4605                Style);
   4606   verifyFormat(
   4607       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
   4608       "    aaaaaaaaaaaaaaa :\n"
   4609       "    aaaaaaaaaaaaaaa;",
   4610       Style);
   4611   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
   4612                "          aaaaaaaaa ?\n"
   4613                "      b :\n"
   4614                "      c);",
   4615                Style);
   4616   verifyFormat(
   4617       "unsigned Indent =\n"
   4618       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
   4619       "                              IndentForLevel[TheLine.Level] :\n"
   4620       "                              TheLine * 2,\n"
   4621       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
   4622       Style);
   4623   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
   4624                "                  aaaaaaaaaaaaaaa :\n"
   4625                "                  bbbbbbbbbbbbbbb ? //\n"
   4626                "                      ccccccccccccccc :\n"
   4627                "                      ddddddddddddddd;",
   4628                Style);
   4629   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
   4630                "                  aaaaaaaaaaaaaaa :\n"
   4631                "                  (bbbbbbbbbbbbbbb ? //\n"
   4632                "                       ccccccccccccccc :\n"
   4633                "                       ddddddddddddddd);",
   4634                Style);
   4635 }
   4636 
   4637 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
   4638   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
   4639                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
   4640   verifyFormat("bool a = true, b = false;");
   4641 
   4642   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   4643                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
   4644                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
   4645                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
   4646   verifyFormat(
   4647       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
   4648       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
   4649       "     d = e && f;");
   4650   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
   4651                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
   4652   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
   4653                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
   4654   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
   4655                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
   4656 
   4657   FormatStyle Style = getGoogleStyle();
   4658   Style.PointerAlignment = FormatStyle::PAS_Left;
   4659   Style.DerivePointerAlignment = false;
   4660   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4661                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
   4662                "    *b = bbbbbbbbbbbbbbbbbbb;",
   4663                Style);
   4664   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
   4665                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
   4666                Style);
   4667 }
   4668 
   4669 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
   4670   verifyFormat("arr[foo ? bar : baz];");
   4671   verifyFormat("f()[foo ? bar : baz];");
   4672   verifyFormat("(a + b)[foo ? bar : baz];");
   4673   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
   4674 }
   4675 
   4676 TEST_F(FormatTest, AlignsStringLiterals) {
   4677   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
   4678                "                                      \"short literal\");");
   4679   verifyFormat(
   4680       "looooooooooooooooooooooooongFunction(\n"
   4681       "    \"short literal\"\n"
   4682       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
   4683   verifyFormat("someFunction(\"Always break between multi-line\"\n"
   4684                "             \" string literals\",\n"
   4685                "             and, other, parameters);");
   4686   EXPECT_EQ("fun + \"1243\" /* comment */\n"
   4687             "      \"5678\";",
   4688             format("fun + \"1243\" /* comment */\n"
   4689                    "      \"5678\";",
   4690                    getLLVMStyleWithColumns(28)));
   4691   EXPECT_EQ(
   4692       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
   4693       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
   4694       "         \"aaaaaaaaaaaaaaaa\";",
   4695       format("aaaaaa ="
   4696              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
   4697              "aaaaaaaaaaaaaaaaaaaaa\" "
   4698              "\"aaaaaaaaaaaaaaaa\";"));
   4699   verifyFormat("a = a + \"a\"\n"
   4700                "        \"a\"\n"
   4701                "        \"a\";");
   4702   verifyFormat("f(\"a\", \"b\"\n"
   4703                "       \"c\");");
   4704 
   4705   verifyFormat(
   4706       "#define LL_FORMAT \"ll\"\n"
   4707       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
   4708       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
   4709 
   4710   verifyFormat("#define A(X)          \\\n"
   4711                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
   4712                "  \"ccccc\"",
   4713                getLLVMStyleWithColumns(23));
   4714   verifyFormat("#define A \"def\"\n"
   4715                "f(\"abc\" A \"ghi\"\n"
   4716                "  \"jkl\");");
   4717 
   4718   verifyFormat("f(L\"a\"\n"
   4719                "  L\"b\");");
   4720   verifyFormat("#define A(X)            \\\n"
   4721                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
   4722                "  L\"ccccc\"",
   4723                getLLVMStyleWithColumns(25));
   4724 
   4725   verifyFormat("f(@\"a\"\n"
   4726                "  @\"b\");");
   4727   verifyFormat("NSString s = @\"a\"\n"
   4728                "             @\"b\"\n"
   4729                "             @\"c\";");
   4730   verifyFormat("NSString s = @\"a\"\n"
   4731                "              \"b\"\n"
   4732                "              \"c\";");
   4733 }
   4734 
   4735 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
   4736   FormatStyle Style = getLLVMStyle();
   4737   // No declarations or definitions should be moved to own line.
   4738   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
   4739   verifyFormat("class A {\n"
   4740                "  int f() { return 1; }\n"
   4741                "  int g();\n"
   4742                "};\n"
   4743                "int f() { return 1; }\n"
   4744                "int g();\n",
   4745                Style);
   4746 
   4747   // All declarations and definitions should have the return type moved to its
   4748   // own
   4749   // line.
   4750   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
   4751   verifyFormat("class E {\n"
   4752                "  int\n"
   4753                "  f() {\n"
   4754                "    return 1;\n"
   4755                "  }\n"
   4756                "  int\n"
   4757                "  g();\n"
   4758                "};\n"
   4759                "int\n"
   4760                "f() {\n"
   4761                "  return 1;\n"
   4762                "}\n"
   4763                "int\n"
   4764                "g();\n",
   4765                Style);
   4766 
   4767   // Top-level definitions, and no kinds of declarations should have the
   4768   // return type moved to its own line.
   4769   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
   4770   verifyFormat("class B {\n"
   4771                "  int f() { return 1; }\n"
   4772                "  int g();\n"
   4773                "};\n"
   4774                "int\n"
   4775                "f() {\n"
   4776                "  return 1;\n"
   4777                "}\n"
   4778                "int g();\n",
   4779                Style);
   4780 
   4781   // Top-level definitions and declarations should have the return type moved
   4782   // to its own line.
   4783   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
   4784   verifyFormat("class C {\n"
   4785                "  int f() { return 1; }\n"
   4786                "  int g();\n"
   4787                "};\n"
   4788                "int\n"
   4789                "f() {\n"
   4790                "  return 1;\n"
   4791                "}\n"
   4792                "int\n"
   4793                "g();\n",
   4794                Style);
   4795 
   4796   // All definitions should have the return type moved to its own line, but no
   4797   // kinds of declarations.
   4798   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
   4799   verifyFormat("class D {\n"
   4800                "  int\n"
   4801                "  f() {\n"
   4802                "    return 1;\n"
   4803                "  }\n"
   4804                "  int g();\n"
   4805                "};\n"
   4806                "int\n"
   4807                "f() {\n"
   4808                "  return 1;\n"
   4809                "}\n"
   4810                "int g();\n",
   4811                Style);
   4812   verifyFormat("const char *\n"
   4813                "f(void) {\n" // Break here.
   4814                "  return \"\";\n"
   4815                "}\n"
   4816                "const char *bar(void);\n", // No break here.
   4817                Style);
   4818   verifyFormat("template <class T>\n"
   4819                "T *\n"
   4820                "f(T &c) {\n" // Break here.
   4821                "  return NULL;\n"
   4822                "}\n"
   4823                "template <class T> T *f(T &c);\n", // No break here.
   4824                Style);
   4825   verifyFormat("class C {\n"
   4826                "  int\n"
   4827                "  operator+() {\n"
   4828                "    return 1;\n"
   4829                "  }\n"
   4830                "  int\n"
   4831                "  operator()() {\n"
   4832                "    return 1;\n"
   4833                "  }\n"
   4834                "};\n",
   4835                Style);
   4836   verifyFormat("void\n"
   4837                "A::operator()() {}\n"
   4838                "void\n"
   4839                "A::operator>>() {}\n"
   4840                "void\n"
   4841                "A::operator+() {}\n",
   4842                Style);
   4843   verifyFormat("void *operator new(std::size_t s);", // No break here.
   4844                Style);
   4845   verifyFormat("void *\n"
   4846                "operator new(std::size_t s) {}",
   4847                Style);
   4848   verifyFormat("void *\n"
   4849                "operator delete[](void *ptr) {}",
   4850                Style);
   4851   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
   4852   verifyFormat("const char *\n"
   4853                "f(void)\n" // Break here.
   4854                "{\n"
   4855                "  return \"\";\n"
   4856                "}\n"
   4857                "const char *bar(void);\n", // No break here.
   4858                Style);
   4859   verifyFormat("template <class T>\n"
   4860                "T *\n"     // Problem here: no line break
   4861                "f(T &c)\n" // Break here.
   4862                "{\n"
   4863                "  return NULL;\n"
   4864                "}\n"
   4865                "template <class T> T *f(T &c);\n", // No break here.
   4866                Style);
   4867 }
   4868 
   4869 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
   4870   FormatStyle NoBreak = getLLVMStyle();
   4871   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
   4872   FormatStyle Break = getLLVMStyle();
   4873   Break.AlwaysBreakBeforeMultilineStrings = true;
   4874   verifyFormat("aaaa = \"bbbb\"\n"
   4875                "       \"cccc\";",
   4876                NoBreak);
   4877   verifyFormat("aaaa =\n"
   4878                "    \"bbbb\"\n"
   4879                "    \"cccc\";",
   4880                Break);
   4881   verifyFormat("aaaa(\"bbbb\"\n"
   4882                "     \"cccc\");",
   4883                NoBreak);
   4884   verifyFormat("aaaa(\n"
   4885                "    \"bbbb\"\n"
   4886                "    \"cccc\");",
   4887                Break);
   4888   verifyFormat("aaaa(qqq, \"bbbb\"\n"
   4889                "          \"cccc\");",
   4890                NoBreak);
   4891   verifyFormat("aaaa(qqq,\n"
   4892                "     \"bbbb\"\n"
   4893                "     \"cccc\");",
   4894                Break);
   4895   verifyFormat("aaaa(qqq,\n"
   4896                "     L\"bbbb\"\n"
   4897                "     L\"cccc\");",
   4898                Break);
   4899   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
   4900                "                      \"bbbb\"));",
   4901                Break);
   4902   verifyFormat("string s = someFunction(\n"
   4903                "    \"abc\"\n"
   4904                "    \"abc\");",
   4905                Break);
   4906 
   4907   // As we break before unary operators, breaking right after them is bad.
   4908   verifyFormat("string foo = abc ? \"x\"\n"
   4909                "                   \"blah blah blah blah blah blah\"\n"
   4910                "                 : \"y\";",
   4911                Break);
   4912 
   4913   // Don't break if there is no column gain.
   4914   verifyFormat("f(\"aaaa\"\n"
   4915                "  \"bbbb\");",
   4916                Break);
   4917 
   4918   // Treat literals with escaped newlines like multi-line string literals.
   4919   EXPECT_EQ("x = \"a\\\n"
   4920             "b\\\n"
   4921             "c\";",
   4922             format("x = \"a\\\n"
   4923                    "b\\\n"
   4924                    "c\";",
   4925                    NoBreak));
   4926   EXPECT_EQ("xxxx =\n"
   4927             "    \"a\\\n"
   4928             "b\\\n"
   4929             "c\";",
   4930             format("xxxx = \"a\\\n"
   4931                    "b\\\n"
   4932                    "c\";",
   4933                    Break));
   4934 
   4935   // Exempt ObjC strings for now.
   4936   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
   4937             "                          @\"bbbb\";",
   4938             format("NSString *const kString = @\"aaaa\"\n"
   4939                    "@\"bbbb\";",
   4940                    Break));
   4941 
   4942   Break.ColumnLimit = 0;
   4943   verifyFormat("const char *hello = \"hello llvm\";", Break);
   4944 }
   4945 
   4946 TEST_F(FormatTest, AlignsPipes) {
   4947   verifyFormat(
   4948       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4949       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4950       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   4951   verifyFormat(
   4952       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
   4953       "                     << aaaaaaaaaaaaaaaaaaaa;");
   4954   verifyFormat(
   4955       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4956       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   4957   verifyFormat(
   4958       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
   4959       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
   4960       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
   4961   verifyFormat(
   4962       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   4963       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4964       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   4965   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4966                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   4967                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   4968                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
   4969   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
   4970                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
   4971   verifyFormat(
   4972       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   4973       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   4974 
   4975   verifyFormat("return out << \"somepacket = {\\n\"\n"
   4976                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
   4977                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
   4978                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
   4979                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
   4980                "           << \"}\";");
   4981 
   4982   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
   4983                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
   4984                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
   4985   verifyFormat(
   4986       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
   4987       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
   4988       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
   4989       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
   4990       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
   4991   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
   4992                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
   4993   verifyFormat(
   4994       "void f() {\n"
   4995       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
   4996       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
   4997       "}");
   4998   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
   4999                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
   5000   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5001                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5002                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
   5003                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
   5004   verifyFormat("LOG_IF(aaa == //\n"
   5005                "       bbb)\n"
   5006                "    << a << b;");
   5007 
   5008   // Breaking before the first "<<" is generally not desirable.
   5009   verifyFormat(
   5010       "llvm::errs()\n"
   5011       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5012       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5013       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5014       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
   5015       getLLVMStyleWithColumns(70));
   5016   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
   5017                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5018                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
   5019                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5020                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
   5021                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
   5022                getLLVMStyleWithColumns(70));
   5023 
   5024   // But sometimes, breaking before the first "<<" is desirable.
   5025   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
   5026                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
   5027   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
   5028                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5029                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   5030   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
   5031                "    << BEF << IsTemplate << Description << E->getType();");
   5032 
   5033   verifyFormat(
   5034       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5035       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   5036 
   5037   // Incomplete string literal.
   5038   EXPECT_EQ("llvm::errs() << \"\n"
   5039             "             << a;",
   5040             format("llvm::errs() << \"\n<<a;"));
   5041 
   5042   verifyFormat("void f() {\n"
   5043                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
   5044                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
   5045                "}");
   5046 
   5047   // Handle 'endl'.
   5048   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
   5049                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
   5050   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
   5051 }
   5052 
   5053 TEST_F(FormatTest, UnderstandsEquals) {
   5054   verifyFormat(
   5055       "aaaaaaaaaaaaaaaaa =\n"
   5056       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   5057   verifyFormat(
   5058       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   5059       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
   5060   verifyFormat(
   5061       "if (a) {\n"
   5062       "  f();\n"
   5063       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   5064       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
   5065       "}");
   5066 
   5067   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   5068                "        100000000 + 10000000) {\n}");
   5069 }
   5070 
   5071 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
   5072   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
   5073                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
   5074 
   5075   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
   5076                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
   5077 
   5078   verifyFormat(
   5079       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
   5080       "                                                          Parameter2);");
   5081 
   5082   verifyFormat(
   5083       "ShortObject->shortFunction(\n"
   5084       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
   5085       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
   5086 
   5087   verifyFormat("loooooooooooooongFunction(\n"
   5088                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
   5089 
   5090   verifyFormat(
   5091       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
   5092       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
   5093 
   5094   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
   5095                "    .WillRepeatedly(Return(SomeValue));");
   5096   verifyFormat("void f() {\n"
   5097                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
   5098                "      .Times(2)\n"
   5099                "      .WillRepeatedly(Return(SomeValue));\n"
   5100                "}");
   5101   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
   5102                "    ccccccccccccccccccccccc);");
   5103   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5104                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   5105                "          .aaaaa(aaaaa),\n"
   5106                "      aaaaaaaaaaaaaaaaaaaaa);");
   5107   verifyFormat("void f() {\n"
   5108                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5109                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
   5110                "}");
   5111   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5112                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   5113                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5114                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5115                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
   5116   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5117                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5118                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5119                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
   5120                "}");
   5121 
   5122   // Here, it is not necessary to wrap at "." or "->".
   5123   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
   5124                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
   5125   verifyFormat(
   5126       "aaaaaaaaaaa->aaaaaaaaa(\n"
   5127       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5128       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
   5129 
   5130   verifyFormat(
   5131       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5132       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
   5133   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
   5134                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
   5135   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
   5136                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
   5137 
   5138   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5139                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   5140                "    .a();");
   5141 
   5142   FormatStyle NoBinPacking = getLLVMStyle();
   5143   NoBinPacking.BinPackParameters = false;
   5144   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
   5145                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
   5146                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
   5147                "                         aaaaaaaaaaaaaaaaaaa,\n"
   5148                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
   5149                NoBinPacking);
   5150 
   5151   // If there is a subsequent call, change to hanging indentation.
   5152   verifyFormat(
   5153       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5154       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
   5155       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   5156   verifyFormat(
   5157       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5158       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
   5159   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5160                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   5161                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   5162   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5163                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   5164                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
   5165 }
   5166 
   5167 TEST_F(FormatTest, WrapsTemplateDeclarations) {
   5168   verifyFormat("template <typename T>\n"
   5169                "virtual void loooooooooooongFunction(int Param1, int Param2);");
   5170   verifyFormat("template <typename T>\n"
   5171                "// T should be one of {A, B}.\n"
   5172                "virtual void loooooooooooongFunction(int Param1, int Param2);");
   5173   verifyFormat(
   5174       "template <typename T>\n"
   5175       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
   5176   verifyFormat("template <typename T>\n"
   5177                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
   5178                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
   5179   verifyFormat(
   5180       "template <typename T>\n"
   5181       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
   5182       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
   5183   verifyFormat(
   5184       "template <typename T>\n"
   5185       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
   5186       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
   5187       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   5188   verifyFormat("template <typename T>\n"
   5189                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5190                "    int aaaaaaaaaaaaaaaaaaaaaa);");
   5191   verifyFormat(
   5192       "template <typename T1, typename T2 = char, typename T3 = char,\n"
   5193       "          typename T4 = char>\n"
   5194       "void f();");
   5195   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
   5196                "          template <typename> class cccccccccccccccccccccc,\n"
   5197                "          typename ddddddddddddd>\n"
   5198                "class C {};");
   5199   verifyFormat(
   5200       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
   5201       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   5202 
   5203   verifyFormat("void f() {\n"
   5204                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
   5205                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
   5206                "}");
   5207 
   5208   verifyFormat("template <typename T> class C {};");
   5209   verifyFormat("template <typename T> void f();");
   5210   verifyFormat("template <typename T> void f() {}");
   5211   verifyFormat(
   5212       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
   5213       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5214       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
   5215       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
   5216       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5217       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
   5218       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
   5219       getLLVMStyleWithColumns(72));
   5220   EXPECT_EQ("static_cast<A< //\n"
   5221             "    B> *>(\n"
   5222             "\n"
   5223             "    );",
   5224             format("static_cast<A<//\n"
   5225                    "    B>*>(\n"
   5226                    "\n"
   5227                    "    );"));
   5228   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5229                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
   5230 
   5231   FormatStyle AlwaysBreak = getLLVMStyle();
   5232   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
   5233   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
   5234   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
   5235   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
   5236   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5237                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
   5238                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
   5239   verifyFormat("template <template <typename> class Fooooooo,\n"
   5240                "          template <typename> class Baaaaaaar>\n"
   5241                "struct C {};",
   5242                AlwaysBreak);
   5243   verifyFormat("template <typename T> // T can be A, B or C.\n"
   5244                "struct C {};",
   5245                AlwaysBreak);
   5246 }
   5247 
   5248 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
   5249   verifyFormat(
   5250       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
   5251       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   5252   verifyFormat(
   5253       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
   5254       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5255       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
   5256 
   5257   // FIXME: Should we have the extra indent after the second break?
   5258   verifyFormat(
   5259       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
   5260       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
   5261       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   5262 
   5263   verifyFormat(
   5264       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
   5265       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
   5266 
   5267   // Breaking at nested name specifiers is generally not desirable.
   5268   verifyFormat(
   5269       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5270       "    aaaaaaaaaaaaaaaaaaaaaaa);");
   5271 
   5272   verifyFormat(
   5273       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
   5274       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   5275       "                   aaaaaaaaaaaaaaaaaaaaa);",
   5276       getLLVMStyleWithColumns(74));
   5277 
   5278   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
   5279                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5280                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
   5281 }
   5282 
   5283 TEST_F(FormatTest, UnderstandsTemplateParameters) {
   5284   verifyFormat("A<int> a;");
   5285   verifyFormat("A<A<A<int>>> a;");
   5286   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
   5287   verifyFormat("bool x = a < 1 || 2 > a;");
   5288   verifyFormat("bool x = 5 < f<int>();");
   5289   verifyFormat("bool x = f<int>() > 5;");
   5290   verifyFormat("bool x = 5 < a<int>::x;");
   5291   verifyFormat("bool x = a < 4 ? a > 2 : false;");
   5292   verifyFormat("bool x = f() ? a < 2 : a > 2;");
   5293 
   5294   verifyGoogleFormat("A<A<int>> a;");
   5295   verifyGoogleFormat("A<A<A<int>>> a;");
   5296   verifyGoogleFormat("A<A<A<A<int>>>> a;");
   5297   verifyGoogleFormat("A<A<int> > a;");
   5298   verifyGoogleFormat("A<A<A<int> > > a;");
   5299   verifyGoogleFormat("A<A<A<A<int> > > > a;");
   5300   verifyGoogleFormat("A<::A<int>> a;");
   5301   verifyGoogleFormat("A<::A> a;");
   5302   verifyGoogleFormat("A< ::A> a;");
   5303   verifyGoogleFormat("A< ::A<int> > a;");
   5304   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
   5305   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
   5306   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
   5307   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
   5308   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
   5309             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
   5310 
   5311   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
   5312 
   5313   verifyFormat("test >> a >> b;");
   5314   verifyFormat("test << a >> b;");
   5315 
   5316   verifyFormat("f<int>();");
   5317   verifyFormat("template <typename T> void f() {}");
   5318   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
   5319   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
   5320                "sizeof(char)>::type>;");
   5321   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
   5322 
   5323   // Not template parameters.
   5324   verifyFormat("return a < b && c > d;");
   5325   verifyFormat("void f() {\n"
   5326                "  while (a < b && c > d) {\n"
   5327                "  }\n"
   5328                "}");
   5329   verifyFormat("template <typename... Types>\n"
   5330                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
   5331 
   5332   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5333                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
   5334                getLLVMStyleWithColumns(60));
   5335   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
   5336   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
   5337   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
   5338 }
   5339 
   5340 TEST_F(FormatTest, UnderstandsBinaryOperators) {
   5341   verifyFormat("COMPARE(a, ==, b);");
   5342 }
   5343 
   5344 TEST_F(FormatTest, UnderstandsPointersToMembers) {
   5345   verifyFormat("int A::*x;");
   5346   verifyFormat("int (S::*func)(void *);");
   5347   verifyFormat("void f() { int (S::*func)(void *); }");
   5348   verifyFormat("typedef bool *(Class::*Member)() const;");
   5349   verifyFormat("void f() {\n"
   5350                "  (a->*f)();\n"
   5351                "  a->*x;\n"
   5352                "  (a.*f)();\n"
   5353                "  ((*a).*f)();\n"
   5354                "  a.*x;\n"
   5355                "}");
   5356   verifyFormat("void f() {\n"
   5357                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
   5358                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
   5359                "}");
   5360   verifyFormat(
   5361       "(aaaaaaaaaa->*bbbbbbb)(\n"
   5362       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
   5363   FormatStyle Style = getLLVMStyle();
   5364   Style.PointerAlignment = FormatStyle::PAS_Left;
   5365   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
   5366 }
   5367 
   5368 TEST_F(FormatTest, UnderstandsUnaryOperators) {
   5369   verifyFormat("int a = -2;");
   5370   verifyFormat("f(-1, -2, -3);");
   5371   verifyFormat("a[-1] = 5;");
   5372   verifyFormat("int a = 5 + -2;");
   5373   verifyFormat("if (i == -1) {\n}");
   5374   verifyFormat("if (i != -1) {\n}");
   5375   verifyFormat("if (i > -1) {\n}");
   5376   verifyFormat("if (i < -1) {\n}");
   5377   verifyFormat("++(a->f());");
   5378   verifyFormat("--(a->f());");
   5379   verifyFormat("(a->f())++;");
   5380   verifyFormat("a[42]++;");
   5381   verifyFormat("if (!(a->f())) {\n}");
   5382 
   5383   verifyFormat("a-- > b;");
   5384   verifyFormat("b ? -a : c;");
   5385   verifyFormat("n * sizeof char16;");
   5386   verifyFormat("n * alignof char16;", getGoogleStyle());
   5387   verifyFormat("sizeof(char);");
   5388   verifyFormat("alignof(char);", getGoogleStyle());
   5389 
   5390   verifyFormat("return -1;");
   5391   verifyFormat("switch (a) {\n"
   5392                "case -1:\n"
   5393                "  break;\n"
   5394                "}");
   5395   verifyFormat("#define X -1");
   5396   verifyFormat("#define X -kConstant");
   5397 
   5398   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
   5399   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
   5400 
   5401   verifyFormat("int a = /* confusing comment */ -1;");
   5402   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
   5403   verifyFormat("int a = i /* confusing comment */++;");
   5404 }
   5405 
   5406 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
   5407   verifyFormat("if (!aaaaaaaaaa( // break\n"
   5408                "        aaaaa)) {\n"
   5409                "}");
   5410   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
   5411                "    aaaaa));");
   5412   verifyFormat("*aaa = aaaaaaa( // break\n"
   5413                "    bbbbbb);");
   5414 }
   5415 
   5416 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
   5417   verifyFormat("bool operator<();");
   5418   verifyFormat("bool operator>();");
   5419   verifyFormat("bool operator=();");
   5420   verifyFormat("bool operator==();");
   5421   verifyFormat("bool operator!=();");
   5422   verifyFormat("int operator+();");
   5423   verifyFormat("int operator++();");
   5424   verifyFormat("bool operator();");
   5425   verifyFormat("bool operator()();");
   5426   verifyFormat("bool operator[]();");
   5427   verifyFormat("operator bool();");
   5428   verifyFormat("operator int();");
   5429   verifyFormat("operator void *();");
   5430   verifyFormat("operator SomeType<int>();");
   5431   verifyFormat("operator SomeType<int, int>();");
   5432   verifyFormat("operator SomeType<SomeType<int>>();");
   5433   verifyFormat("void *operator new(std::size_t size);");
   5434   verifyFormat("void *operator new[](std::size_t size);");
   5435   verifyFormat("void operator delete(void *ptr);");
   5436   verifyFormat("void operator delete[](void *ptr);");
   5437   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
   5438                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
   5439 
   5440   verifyFormat(
   5441       "ostream &operator<<(ostream &OutputStream,\n"
   5442       "                    SomeReallyLongType WithSomeReallyLongValue);");
   5443   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
   5444                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
   5445                "  return left.group < right.group;\n"
   5446                "}");
   5447   verifyFormat("SomeType &operator=(const SomeType &S);");
   5448   verifyFormat("f.template operator()<int>();");
   5449 
   5450   verifyGoogleFormat("operator void*();");
   5451   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
   5452   verifyGoogleFormat("operator ::A();");
   5453 
   5454   verifyFormat("using A::operator+;");
   5455   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
   5456                "int i;");
   5457 }
   5458 
   5459 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
   5460   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
   5461   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
   5462   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
   5463   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
   5464   verifyFormat("Deleted &operator=(const Deleted &) &;");
   5465   verifyFormat("Deleted &operator=(const Deleted &) &&;");
   5466   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
   5467   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
   5468   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
   5469   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
   5470   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
   5471 
   5472   FormatStyle AlignLeft = getLLVMStyle();
   5473   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
   5474   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
   5475   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
   5476                AlignLeft);
   5477   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
   5478   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
   5479 
   5480   FormatStyle Spaces = getLLVMStyle();
   5481   Spaces.SpacesInCStyleCastParentheses = true;
   5482   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
   5483   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
   5484   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
   5485   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
   5486 
   5487   Spaces.SpacesInCStyleCastParentheses = false;
   5488   Spaces.SpacesInParentheses = true;
   5489   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
   5490   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
   5491   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
   5492   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
   5493 }
   5494 
   5495 TEST_F(FormatTest, UnderstandsNewAndDelete) {
   5496   verifyFormat("void f() {\n"
   5497                "  A *a = new A;\n"
   5498                "  A *a = new (placement) A;\n"
   5499                "  delete a;\n"
   5500                "  delete (A *)a;\n"
   5501                "}");
   5502   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
   5503                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
   5504   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   5505                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
   5506                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
   5507   verifyFormat("delete[] h->p;");
   5508 }
   5509 
   5510 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
   5511   verifyFormat("int *f(int *a) {}");
   5512   verifyFormat("int main(int argc, char **argv) {}");
   5513   verifyFormat("Test::Test(int b) : a(b * b) {}");
   5514   verifyIndependentOfContext("f(a, *a);");
   5515   verifyFormat("void g() { f(*a); }");
   5516   verifyIndependentOfContext("int a = b * 10;");
   5517   verifyIndependentOfContext("int a = 10 * b;");
   5518   verifyIndependentOfContext("int a = b * c;");
   5519   verifyIndependentOfContext("int a += b * c;");
   5520   verifyIndependentOfContext("int a -= b * c;");
   5521   verifyIndependentOfContext("int a *= b * c;");
   5522   verifyIndependentOfContext("int a /= b * c;");
   5523   verifyIndependentOfContext("int a = *b;");
   5524   verifyIndependentOfContext("int a = *b * c;");
   5525   verifyIndependentOfContext("int a = b * *c;");
   5526   verifyIndependentOfContext("int a = b * (10);");
   5527   verifyIndependentOfContext("S << b * (10);");
   5528   verifyIndependentOfContext("return 10 * b;");
   5529   verifyIndependentOfContext("return *b * *c;");
   5530   verifyIndependentOfContext("return a & ~b;");
   5531   verifyIndependentOfContext("f(b ? *c : *d);");
   5532   verifyIndependentOfContext("int a = b ? *c : *d;");
   5533   verifyIndependentOfContext("*b = a;");
   5534   verifyIndependentOfContext("a * ~b;");
   5535   verifyIndependentOfContext("a * !b;");
   5536   verifyIndependentOfContext("a * +b;");
   5537   verifyIndependentOfContext("a * -b;");
   5538   verifyIndependentOfContext("a * ++b;");
   5539   verifyIndependentOfContext("a * --b;");
   5540   verifyIndependentOfContext("a[4] * b;");
   5541   verifyIndependentOfContext("a[a * a] = 1;");
   5542   verifyIndependentOfContext("f() * b;");
   5543   verifyIndependentOfContext("a * [self dostuff];");
   5544   verifyIndependentOfContext("int x = a * (a + b);");
   5545   verifyIndependentOfContext("(a *)(a + b);");
   5546   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
   5547   verifyIndependentOfContext("int *pa = (int *)&a;");
   5548   verifyIndependentOfContext("return sizeof(int **);");
   5549   verifyIndependentOfContext("return sizeof(int ******);");
   5550   verifyIndependentOfContext("return (int **&)a;");
   5551   verifyIndependentOfContext("f((*PointerToArray)[10]);");
   5552   verifyFormat("void f(Type (*parameter)[10]) {}");
   5553   verifyFormat("void f(Type (&parameter)[10]) {}");
   5554   verifyGoogleFormat("return sizeof(int**);");
   5555   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
   5556   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
   5557   verifyFormat("auto a = [](int **&, int ***) {};");
   5558   verifyFormat("auto PointerBinding = [](const char *S) {};");
   5559   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
   5560   verifyFormat("[](const decltype(*a) &value) {}");
   5561   verifyFormat("decltype(a * b) F();");
   5562   verifyFormat("#define MACRO() [](A *a) { return 1; }");
   5563   verifyIndependentOfContext("typedef void (*f)(int *a);");
   5564   verifyIndependentOfContext("int i{a * b};");
   5565   verifyIndependentOfContext("aaa && aaa->f();");
   5566   verifyIndependentOfContext("int x = ~*p;");
   5567   verifyFormat("Constructor() : a(a), area(width * height) {}");
   5568   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
   5569   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
   5570   verifyFormat("void f() { f(a, c * d); }");
   5571   verifyFormat("void f() { f(new a(), c * d); }");
   5572 
   5573   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
   5574 
   5575   verifyIndependentOfContext("A<int *> a;");
   5576   verifyIndependentOfContext("A<int **> a;");
   5577   verifyIndependentOfContext("A<int *, int *> a;");
   5578   verifyIndependentOfContext("A<int *[]> a;");
   5579   verifyIndependentOfContext(
   5580       "const char *const p = reinterpret_cast<const char *const>(q);");
   5581   verifyIndependentOfContext("A<int **, int **> a;");
   5582   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
   5583   verifyFormat("for (char **a = b; *a; ++a) {\n}");
   5584   verifyFormat("for (; a && b;) {\n}");
   5585   verifyFormat("bool foo = true && [] { return false; }();");
   5586 
   5587   verifyFormat(
   5588       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   5589       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   5590 
   5591   verifyGoogleFormat("**outparam = 1;");
   5592   verifyGoogleFormat("*outparam = a * b;");
   5593   verifyGoogleFormat("int main(int argc, char** argv) {}");
   5594   verifyGoogleFormat("A<int*> a;");
   5595   verifyGoogleFormat("A<int**> a;");
   5596   verifyGoogleFormat("A<int*, int*> a;");
   5597   verifyGoogleFormat("A<int**, int**> a;");
   5598   verifyGoogleFormat("f(b ? *c : *d);");
   5599   verifyGoogleFormat("int a = b ? *c : *d;");
   5600   verifyGoogleFormat("Type* t = **x;");
   5601   verifyGoogleFormat("Type* t = *++*x;");
   5602   verifyGoogleFormat("*++*x;");
   5603   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
   5604   verifyGoogleFormat("Type* t = x++ * y;");
   5605   verifyGoogleFormat(
   5606       "const char* const p = reinterpret_cast<const char* const>(q);");
   5607   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
   5608   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
   5609   verifyGoogleFormat("template <typename T>\n"
   5610                      "void f(int i = 0, SomeType** temps = NULL);");
   5611 
   5612   FormatStyle Left = getLLVMStyle();
   5613   Left.PointerAlignment = FormatStyle::PAS_Left;
   5614   verifyFormat("x = *a(x) = *a(y);", Left);
   5615   verifyFormat("for (;; * = b) {\n}", Left);
   5616 
   5617   verifyIndependentOfContext("a = *(x + y);");
   5618   verifyIndependentOfContext("a = &(x + y);");
   5619   verifyIndependentOfContext("*(x + y).call();");
   5620   verifyIndependentOfContext("&(x + y)->call();");
   5621   verifyFormat("void f() { &(*I).first; }");
   5622 
   5623   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
   5624   verifyFormat(
   5625       "int *MyValues = {\n"
   5626       "    *A, // Operator detection might be confused by the '{'\n"
   5627       "    *BB // Operator detection might be confused by previous comment\n"
   5628       "};");
   5629 
   5630   verifyIndependentOfContext("if (int *a = &b)");
   5631   verifyIndependentOfContext("if (int &a = *b)");
   5632   verifyIndependentOfContext("if (a & b[i])");
   5633   verifyIndependentOfContext("if (a::b::c::d & b[i])");
   5634   verifyIndependentOfContext("if (*b[i])");
   5635   verifyIndependentOfContext("if (int *a = (&b))");
   5636   verifyIndependentOfContext("while (int *a = &b)");
   5637   verifyIndependentOfContext("size = sizeof *a;");
   5638   verifyIndependentOfContext("if (a && (b = c))");
   5639   verifyFormat("void f() {\n"
   5640                "  for (const int &v : Values) {\n"
   5641                "  }\n"
   5642                "}");
   5643   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
   5644   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
   5645   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
   5646 
   5647   verifyFormat("#define A (!a * b)");
   5648   verifyFormat("#define MACRO     \\\n"
   5649                "  int *i = a * b; \\\n"
   5650                "  void f(a *b);",
   5651                getLLVMStyleWithColumns(19));
   5652 
   5653   verifyIndependentOfContext("A = new SomeType *[Length];");
   5654   verifyIndependentOfContext("A = new SomeType *[Length]();");
   5655   verifyIndependentOfContext("T **t = new T *;");
   5656   verifyIndependentOfContext("T **t = new T *();");
   5657   verifyGoogleFormat("A = new SomeType*[Length]();");
   5658   verifyGoogleFormat("A = new SomeType*[Length];");
   5659   verifyGoogleFormat("T** t = new T*;");
   5660   verifyGoogleFormat("T** t = new T*();");
   5661 
   5662   FormatStyle PointerLeft = getLLVMStyle();
   5663   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
   5664   verifyFormat("delete *x;", PointerLeft);
   5665   verifyFormat("STATIC_ASSERT((a & b) == 0);");
   5666   verifyFormat("STATIC_ASSERT(0 == (a & b));");
   5667   verifyFormat("template <bool a, bool b> "
   5668                "typename t::if<x && y>::type f() {}");
   5669   verifyFormat("template <int *y> f() {}");
   5670   verifyFormat("vector<int *> v;");
   5671   verifyFormat("vector<int *const> v;");
   5672   verifyFormat("vector<int *const **const *> v;");
   5673   verifyFormat("vector<int *volatile> v;");
   5674   verifyFormat("vector<a * b> v;");
   5675   verifyFormat("foo<b && false>();");
   5676   verifyFormat("foo<b & 1>();");
   5677   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
   5678   verifyFormat(
   5679       "template <class T, class = typename std::enable_if<\n"
   5680       "                       std::is_integral<T>::value &&\n"
   5681       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
   5682       "void F();",
   5683       getLLVMStyleWithColumns(76));
   5684   verifyFormat(
   5685       "template <class T,\n"
   5686       "          class = typename ::std::enable_if<\n"
   5687       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
   5688       "void F();",
   5689       getGoogleStyleWithColumns(68));
   5690 
   5691   verifyIndependentOfContext("MACRO(int *i);");
   5692   verifyIndependentOfContext("MACRO(auto *a);");
   5693   verifyIndependentOfContext("MACRO(const A *a);");
   5694   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
   5695   // FIXME: Is there a way to make this work?
   5696   // verifyIndependentOfContext("MACRO(A *a);");
   5697 
   5698   verifyFormat("DatumHandle const *operator->() const { return input_; }");
   5699   verifyFormat("return options != nullptr && operator==(*options);");
   5700 
   5701   EXPECT_EQ("#define OP(x)                                    \\\n"
   5702             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
   5703             "    return s << a.DebugString();                 \\\n"
   5704             "  }",
   5705             format("#define OP(x) \\\n"
   5706                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
   5707                    "    return s << a.DebugString(); \\\n"
   5708                    "  }",
   5709                    getLLVMStyleWithColumns(50)));
   5710 
   5711   // FIXME: We cannot handle this case yet; we might be able to figure out that
   5712   // foo<x> d > v; doesn't make sense.
   5713   verifyFormat("foo<a<b && c> d> v;");
   5714 
   5715   FormatStyle PointerMiddle = getLLVMStyle();
   5716   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
   5717   verifyFormat("delete *x;", PointerMiddle);
   5718   verifyFormat("int * x;", PointerMiddle);
   5719   verifyFormat("template <int * y> f() {}", PointerMiddle);
   5720   verifyFormat("int * f(int * a) {}", PointerMiddle);
   5721   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
   5722   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
   5723   verifyFormat("A<int *> a;", PointerMiddle);
   5724   verifyFormat("A<int **> a;", PointerMiddle);
   5725   verifyFormat("A<int *, int *> a;", PointerMiddle);
   5726   verifyFormat("A<int * []> a;", PointerMiddle);
   5727   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
   5728   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
   5729   verifyFormat("T ** t = new T *;", PointerMiddle);
   5730 
   5731   // Member function reference qualifiers aren't binary operators.
   5732   verifyFormat("string // break\n"
   5733                "operator()() & {}");
   5734   verifyFormat("string // break\n"
   5735                "operator()() && {}");
   5736   verifyGoogleFormat("template <typename T>\n"
   5737                      "auto x() & -> int {}");
   5738 }
   5739 
   5740 TEST_F(FormatTest, UnderstandsAttributes) {
   5741   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
   5742   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
   5743                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
   5744   FormatStyle AfterType = getLLVMStyle();
   5745   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
   5746   verifyFormat("__attribute__((nodebug)) void\n"
   5747                "foo() {}\n",
   5748                AfterType);
   5749 }
   5750 
   5751 TEST_F(FormatTest, UnderstandsEllipsis) {
   5752   verifyFormat("int printf(const char *fmt, ...);");
   5753   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
   5754   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
   5755 
   5756   FormatStyle PointersLeft = getLLVMStyle();
   5757   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
   5758   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
   5759 }
   5760 
   5761 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
   5762   EXPECT_EQ("int *a;\n"
   5763             "int *a;\n"
   5764             "int *a;",
   5765             format("int *a;\n"
   5766                    "int* a;\n"
   5767                    "int *a;",
   5768                    getGoogleStyle()));
   5769   EXPECT_EQ("int* a;\n"
   5770             "int* a;\n"
   5771             "int* a;",
   5772             format("int* a;\n"
   5773                    "int* a;\n"
   5774                    "int *a;",
   5775                    getGoogleStyle()));
   5776   EXPECT_EQ("int *a;\n"
   5777             "int *a;\n"
   5778             "int *a;",
   5779             format("int *a;\n"
   5780                    "int * a;\n"
   5781                    "int *  a;",
   5782                    getGoogleStyle()));
   5783   EXPECT_EQ("auto x = [] {\n"
   5784             "  int *a;\n"
   5785             "  int *a;\n"
   5786             "  int *a;\n"
   5787             "};",
   5788             format("auto x=[]{int *a;\n"
   5789                    "int * a;\n"
   5790                    "int *  a;};",
   5791                    getGoogleStyle()));
   5792 }
   5793 
   5794 TEST_F(FormatTest, UnderstandsRvalueReferences) {
   5795   verifyFormat("int f(int &&a) {}");
   5796   verifyFormat("int f(int a, char &&b) {}");
   5797   verifyFormat("void f() { int &&a = b; }");
   5798   verifyGoogleFormat("int f(int a, char&& b) {}");
   5799   verifyGoogleFormat("void f() { int&& a = b; }");
   5800 
   5801   verifyIndependentOfContext("A<int &&> a;");
   5802   verifyIndependentOfContext("A<int &&, int &&> a;");
   5803   verifyGoogleFormat("A<int&&> a;");
   5804   verifyGoogleFormat("A<int&&, int&&> a;");
   5805 
   5806   // Not rvalue references:
   5807   verifyFormat("template <bool B, bool C> class A {\n"
   5808                "  static_assert(B && C, \"Something is wrong\");\n"
   5809                "};");
   5810   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
   5811   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
   5812   verifyFormat("#define A(a, b) (a && b)");
   5813 }
   5814 
   5815 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
   5816   verifyFormat("void f() {\n"
   5817                "  x[aaaaaaaaa -\n"
   5818                "    b] = 23;\n"
   5819                "}",
   5820                getLLVMStyleWithColumns(15));
   5821 }
   5822 
   5823 TEST_F(FormatTest, FormatsCasts) {
   5824   verifyFormat("Type *A = static_cast<Type *>(P);");
   5825   verifyFormat("Type *A = (Type *)P;");
   5826   verifyFormat("Type *A = (vector<Type *, int *>)P;");
   5827   verifyFormat("int a = (int)(2.0f);");
   5828   verifyFormat("int a = (int)2.0f;");
   5829   verifyFormat("x[(int32)y];");
   5830   verifyFormat("x = (int32)y;");
   5831   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
   5832   verifyFormat("int a = (int)*b;");
   5833   verifyFormat("int a = (int)2.0f;");
   5834   verifyFormat("int a = (int)~0;");
   5835   verifyFormat("int a = (int)++a;");
   5836   verifyFormat("int a = (int)sizeof(int);");
   5837   verifyFormat("int a = (int)+2;");
   5838   verifyFormat("my_int a = (my_int)2.0f;");
   5839   verifyFormat("my_int a = (my_int)sizeof(int);");
   5840   verifyFormat("return (my_int)aaa;");
   5841   verifyFormat("#define x ((int)-1)");
   5842   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
   5843   verifyFormat("#define p(q) ((int *)&q)");
   5844   verifyFormat("fn(a)(b) + 1;");
   5845 
   5846   verifyFormat("void f() { my_int a = (my_int)*b; }");
   5847   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
   5848   verifyFormat("my_int a = (my_int)~0;");
   5849   verifyFormat("my_int a = (my_int)++a;");
   5850   verifyFormat("my_int a = (my_int)-2;");
   5851   verifyFormat("my_int a = (my_int)1;");
   5852   verifyFormat("my_int a = (my_int *)1;");
   5853   verifyFormat("my_int a = (const my_int)-1;");
   5854   verifyFormat("my_int a = (const my_int *)-1;");
   5855   verifyFormat("my_int a = (my_int)(my_int)-1;");
   5856   verifyFormat("my_int a = (ns::my_int)-2;");
   5857   verifyFormat("case (my_int)ONE:");
   5858 
   5859   // FIXME: single value wrapped with paren will be treated as cast.
   5860   verifyFormat("void f(int i = (kValue)*kMask) {}");
   5861 
   5862   verifyFormat("{ (void)F; }");
   5863 
   5864   // Don't break after a cast's
   5865   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
   5866                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
   5867                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
   5868 
   5869   // These are not casts.
   5870   verifyFormat("void f(int *) {}");
   5871   verifyFormat("f(foo)->b;");
   5872   verifyFormat("f(foo).b;");
   5873   verifyFormat("f(foo)(b);");
   5874   verifyFormat("f(foo)[b];");
   5875   verifyFormat("[](foo) { return 4; }(bar);");
   5876   verifyFormat("(*funptr)(foo)[4];");
   5877   verifyFormat("funptrs[4](foo)[4];");
   5878   verifyFormat("void f(int *);");
   5879   verifyFormat("void f(int *) = 0;");
   5880   verifyFormat("void f(SmallVector<int>) {}");
   5881   verifyFormat("void f(SmallVector<int>);");
   5882   verifyFormat("void f(SmallVector<int>) = 0;");
   5883   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
   5884   verifyFormat("int a = sizeof(int) * b;");
   5885   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
   5886   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
   5887   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
   5888   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
   5889 
   5890   // These are not casts, but at some point were confused with casts.
   5891   verifyFormat("virtual void foo(int *) override;");
   5892   verifyFormat("virtual void foo(char &) const;");
   5893   verifyFormat("virtual void foo(int *a, char *) const;");
   5894   verifyFormat("int a = sizeof(int *) + b;");
   5895   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
   5896   verifyFormat("bool b = f(g<int>) && c;");
   5897   verifyFormat("typedef void (*f)(int i) func;");
   5898 
   5899   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
   5900                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
   5901   // FIXME: The indentation here is not ideal.
   5902   verifyFormat(
   5903       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   5904       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
   5905       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
   5906 }
   5907 
   5908 TEST_F(FormatTest, FormatsFunctionTypes) {
   5909   verifyFormat("A<bool()> a;");
   5910   verifyFormat("A<SomeType()> a;");
   5911   verifyFormat("A<void (*)(int, std::string)> a;");
   5912   verifyFormat("A<void *(int)>;");
   5913   verifyFormat("void *(*a)(int *, SomeType *);");
   5914   verifyFormat("int (*func)(void *);");
   5915   verifyFormat("void f() { int (*func)(void *); }");
   5916   verifyFormat("template <class CallbackClass>\n"
   5917                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
   5918 
   5919   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
   5920   verifyGoogleFormat("void* (*a)(int);");
   5921   verifyGoogleFormat(
   5922       "template <class CallbackClass>\n"
   5923       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
   5924 
   5925   // Other constructs can look somewhat like function types:
   5926   verifyFormat("A<sizeof(*x)> a;");
   5927   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
   5928   verifyFormat("some_var = function(*some_pointer_var)[0];");
   5929   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
   5930 }
   5931 
   5932 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
   5933   verifyFormat("A (*foo_)[6];");
   5934   verifyFormat("vector<int> (*foo_)[6];");
   5935 }
   5936 
   5937 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
   5938   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
   5939                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
   5940   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
   5941                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
   5942   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
   5943                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
   5944 
   5945   // Different ways of ()-initializiation.
   5946   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
   5947                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
   5948   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
   5949                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
   5950   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
   5951                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
   5952   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
   5953                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
   5954 }
   5955 
   5956 TEST_F(FormatTest, BreaksLongDeclarations) {
   5957   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
   5958                "    AnotherNameForTheLongType;");
   5959   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
   5960                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   5961   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
   5962                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
   5963   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
   5964                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
   5965   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
   5966                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
   5967   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
   5968                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
   5969   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
   5970                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
   5971   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
   5972                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
   5973   FormatStyle Indented = getLLVMStyle();
   5974   Indented.IndentWrappedFunctionNames = true;
   5975   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
   5976                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
   5977                Indented);
   5978   verifyFormat(
   5979       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
   5980       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
   5981       Indented);
   5982   verifyFormat(
   5983       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
   5984       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
   5985       Indented);
   5986   verifyFormat(
   5987       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
   5988       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
   5989       Indented);
   5990 
   5991   // FIXME: Without the comment, this breaks after "(".
   5992   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
   5993                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
   5994                getGoogleStyle());
   5995 
   5996   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
   5997                "                  int LoooooooooooooooooooongParam2) {}");
   5998   verifyFormat(
   5999       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
   6000       "                                   SourceLocation L, IdentifierIn *II,\n"
   6001       "                                   Type *T) {}");
   6002   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
   6003                "ReallyReaaallyLongFunctionName(\n"
   6004                "    const std::string &SomeParameter,\n"
   6005                "    const SomeType<string, SomeOtherTemplateParameter>\n"
   6006                "        &ReallyReallyLongParameterName,\n"
   6007                "    const SomeType<string, SomeOtherTemplateParameter>\n"
   6008                "        &AnotherLongParameterName) {}");
   6009   verifyFormat("template <typename A>\n"
   6010                "SomeLoooooooooooooooooooooongType<\n"
   6011                "    typename some_namespace::SomeOtherType<A>::Type>\n"
   6012                "Function() {}");
   6013 
   6014   verifyGoogleFormat(
   6015       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
   6016       "    aaaaaaaaaaaaaaaaaaaaaaa;");
   6017   verifyGoogleFormat(
   6018       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
   6019       "                                   SourceLocation L) {}");
   6020   verifyGoogleFormat(
   6021       "some_namespace::LongReturnType\n"
   6022       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
   6023       "    int first_long_parameter, int second_parameter) {}");
   6024 
   6025   verifyGoogleFormat("template <typename T>\n"
   6026                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
   6027                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
   6028   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   6029                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
   6030 
   6031   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
   6032                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   6033                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   6034   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   6035                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
   6036                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
   6037   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   6038                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
   6039                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
   6040                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   6041 }
   6042 
   6043 TEST_F(FormatTest, FormatsArrays) {
   6044   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
   6045                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
   6046   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   6047                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
   6048   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   6049                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
   6050   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   6051                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
   6052                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
   6053   verifyFormat(
   6054       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
   6055       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
   6056       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
   6057 
   6058   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
   6059                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
   6060   verifyFormat(
   6061       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
   6062       "                                  .aaaaaaa[0]\n"
   6063       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
   6064 
   6065   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
   6066 }
   6067 
   6068 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
   6069   verifyFormat("(a)->b();");
   6070   verifyFormat("--a;");
   6071 }
   6072 
   6073 TEST_F(FormatTest, HandlesIncludeDirectives) {
   6074   verifyFormat("#include <string>\n"
   6075                "#include <a/b/c.h>\n"
   6076                "#include \"a/b/string\"\n"
   6077                "#include \"string.h\"\n"
   6078                "#include \"string.h\"\n"
   6079                "#include <a-a>\n"
   6080                "#include < path with space >\n"
   6081                "#include_next <test.h>"
   6082                "#include \"abc.h\" // this is included for ABC\n"
   6083                "#include \"some long include\" // with a comment\n"
   6084                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
   6085                getLLVMStyleWithColumns(35));
   6086   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
   6087   EXPECT_EQ("#include <a>", format("#include<a>"));
   6088 
   6089   verifyFormat("#import <string>");
   6090   verifyFormat("#import <a/b/c.h>");
   6091   verifyFormat("#import \"a/b/string\"");
   6092   verifyFormat("#import \"string.h\"");
   6093   verifyFormat("#import \"string.h\"");
   6094   verifyFormat("#if __has_include(<strstream>)\n"
   6095                "#include <strstream>\n"
   6096                "#endif");
   6097 
   6098   verifyFormat("#define MY_IMPORT <a/b>");
   6099 
   6100   // Protocol buffer definition or missing "#".
   6101   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
   6102                getLLVMStyleWithColumns(30));
   6103 
   6104   FormatStyle Style = getLLVMStyle();
   6105   Style.AlwaysBreakBeforeMultilineStrings = true;
   6106   Style.ColumnLimit = 0;
   6107   verifyFormat("#import \"abc.h\"", Style);
   6108 
   6109   // But 'import' might also be a regular C++ namespace.
   6110   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   6111                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   6112 }
   6113 
   6114 //===----------------------------------------------------------------------===//
   6115 // Error recovery tests.
   6116 //===----------------------------------------------------------------------===//
   6117 
   6118 TEST_F(FormatTest, IncompleteParameterLists) {
   6119   FormatStyle NoBinPacking = getLLVMStyle();
   6120   NoBinPacking.BinPackParameters = false;
   6121   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
   6122                "                        double *min_x,\n"
   6123                "                        double *max_x,\n"
   6124                "                        double *min_y,\n"
   6125                "                        double *max_y,\n"
   6126                "                        double *min_z,\n"
   6127                "                        double *max_z, ) {}",
   6128                NoBinPacking);
   6129 }
   6130 
   6131 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
   6132   verifyFormat("void f() { return; }\n42");
   6133   verifyFormat("void f() {\n"
   6134                "  if (0)\n"
   6135                "    return;\n"
   6136                "}\n"
   6137                "42");
   6138   verifyFormat("void f() { return }\n42");
   6139   verifyFormat("void f() {\n"
   6140                "  if (0)\n"
   6141                "    return\n"
   6142                "}\n"
   6143                "42");
   6144 }
   6145 
   6146 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
   6147   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
   6148   EXPECT_EQ("void f() {\n"
   6149             "  if (a)\n"
   6150             "    return\n"
   6151             "}",
   6152             format("void  f  (  )  {  if  ( a )  return  }"));
   6153   EXPECT_EQ("namespace N {\n"
   6154             "void f()\n"
   6155             "}",
   6156             format("namespace  N  {  void f()  }"));
   6157   EXPECT_EQ("namespace N {\n"
   6158             "void f() {}\n"
   6159             "void g()\n"
   6160             "}",
   6161             format("namespace N  { void f( ) { } void g( ) }"));
   6162 }
   6163 
   6164 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
   6165   verifyFormat("int aaaaaaaa =\n"
   6166                "    // Overlylongcomment\n"
   6167                "    b;",
   6168                getLLVMStyleWithColumns(20));
   6169   verifyFormat("function(\n"
   6170                "    ShortArgument,\n"
   6171                "    LoooooooooooongArgument);\n",
   6172                getLLVMStyleWithColumns(20));
   6173 }
   6174 
   6175 TEST_F(FormatTest, IncorrectAccessSpecifier) {
   6176   verifyFormat("public:");
   6177   verifyFormat("class A {\n"
   6178                "public\n"
   6179                "  void f() {}\n"
   6180                "};");
   6181   verifyFormat("public\n"
   6182                "int qwerty;");
   6183   verifyFormat("public\n"
   6184                "B {}");
   6185   verifyFormat("public\n"
   6186                "{}");
   6187   verifyFormat("public\n"
   6188                "B { int x; }");
   6189 }
   6190 
   6191 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   6192   verifyFormat("{");
   6193   verifyFormat("#})");
   6194   verifyNoCrash("(/**/[:!] ?[).");
   6195 }
   6196 
   6197 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
   6198   verifyFormat("do {\n}");
   6199   verifyFormat("do {\n}\n"
   6200                "f();");
   6201   verifyFormat("do {\n}\n"
   6202                "wheeee(fun);");
   6203   verifyFormat("do {\n"
   6204                "  f();\n"
   6205                "}");
   6206 }
   6207 
   6208 TEST_F(FormatTest, IncorrectCodeMissingParens) {
   6209   verifyFormat("if {\n  foo;\n  foo();\n}");
   6210   verifyFormat("switch {\n  foo;\n  foo();\n}");
   6211   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
   6212   verifyFormat("while {\n  foo;\n  foo();\n}");
   6213   verifyFormat("do {\n  foo;\n  foo();\n} while;");
   6214 }
   6215 
   6216 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
   6217   verifyIncompleteFormat("namespace {\n"
   6218                          "class Foo { Foo (\n"
   6219                          "};\n"
   6220                          "} // comment");
   6221 }
   6222 
   6223 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
   6224   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
   6225   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
   6226   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
   6227   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
   6228 
   6229   EXPECT_EQ("{\n"
   6230             "  {\n"
   6231             "    breakme(\n"
   6232             "        qwe);\n"
   6233             "  }\n",
   6234             format("{\n"
   6235                    "    {\n"
   6236                    " breakme(qwe);\n"
   6237                    "}\n",
   6238                    getLLVMStyleWithColumns(10)));
   6239 }
   6240 
   6241 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
   6242   verifyFormat("int x = {\n"
   6243                "    avariable,\n"
   6244                "    b(alongervariable)};",
   6245                getLLVMStyleWithColumns(25));
   6246 }
   6247 
   6248 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
   6249   verifyFormat("return (a)(b){1, 2, 3};");
   6250 }
   6251 
   6252 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
   6253   verifyFormat("vector<int> x{1, 2, 3, 4};");
   6254   verifyFormat("vector<int> x{\n"
   6255                "    1, 2, 3, 4,\n"
   6256                "};");
   6257   verifyFormat("vector<T> x{{}, {}, {}, {}};");
   6258   verifyFormat("f({1, 2});");
   6259   verifyFormat("auto v = Foo{-1};");
   6260   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
   6261   verifyFormat("Class::Class : member{1, 2, 3} {}");
   6262   verifyFormat("new vector<int>{1, 2, 3};");
   6263   verifyFormat("new int[3]{1, 2, 3};");
   6264   verifyFormat("new int{1};");
   6265   verifyFormat("return {arg1, arg2};");
   6266   verifyFormat("return {arg1, SomeType{parameter}};");
   6267   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
   6268   verifyFormat("new T{arg1, arg2};");
   6269   verifyFormat("f(MyMap[{composite, key}]);");
   6270   verifyFormat("class Class {\n"
   6271                "  T member = {arg1, arg2};\n"
   6272                "};");
   6273   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
   6274   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
   6275   verifyFormat("int a = std::is_integral<int>{} + 0;");
   6276 
   6277   verifyFormat("int foo(int i) { return fo1{}(i); }");
   6278   verifyFormat("int foo(int i) { return fo1{}(i); }");
   6279   verifyFormat("auto i = decltype(x){};");
   6280   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
   6281   verifyFormat("Node n{1, Node{1000}, //\n"
   6282                "       2};");
   6283   verifyFormat("Aaaa aaaaaaa{\n"
   6284                "    {\n"
   6285                "        aaaa,\n"
   6286                "    },\n"
   6287                "};");
   6288   verifyFormat("class C : public D {\n"
   6289                "  SomeClass SC{2};\n"
   6290                "};");
   6291   verifyFormat("class C : public A {\n"
   6292                "  class D : public B {\n"
   6293                "    void f() { int i{2}; }\n"
   6294                "  };\n"
   6295                "};");
   6296   verifyFormat("#define A {a, a},");
   6297 
   6298   // In combination with BinPackArguments = false.
   6299   FormatStyle NoBinPacking = getLLVMStyle();
   6300   NoBinPacking.BinPackArguments = false;
   6301   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
   6302                "                      bbbbb,\n"
   6303                "                      ccccc,\n"
   6304                "                      ddddd,\n"
   6305                "                      eeeee,\n"
   6306                "                      ffffff,\n"
   6307                "                      ggggg,\n"
   6308                "                      hhhhhh,\n"
   6309                "                      iiiiii,\n"
   6310                "                      jjjjjj,\n"
   6311                "                      kkkkkk};",
   6312                NoBinPacking);
   6313   verifyFormat("const Aaaaaa aaaaa = {\n"
   6314                "    aaaaa,\n"
   6315                "    bbbbb,\n"
   6316                "    ccccc,\n"
   6317                "    ddddd,\n"
   6318                "    eeeee,\n"
   6319                "    ffffff,\n"
   6320                "    ggggg,\n"
   6321                "    hhhhhh,\n"
   6322                "    iiiiii,\n"
   6323                "    jjjjjj,\n"
   6324                "    kkkkkk,\n"
   6325                "};",
   6326                NoBinPacking);
   6327   verifyFormat(
   6328       "const Aaaaaa aaaaa = {\n"
   6329       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
   6330       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
   6331       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
   6332       "};",
   6333       NoBinPacking);
   6334 
   6335   // FIXME: The alignment of these trailing comments might be bad. Then again,
   6336   // this might be utterly useless in real code.
   6337   verifyFormat("Constructor::Constructor()\n"
   6338                "    : some_value{         //\n"
   6339                "                 aaaaaaa, //\n"
   6340                "                 bbbbbbb} {}");
   6341 
   6342   // In braced lists, the first comment is always assumed to belong to the
   6343   // first element. Thus, it can be moved to the next or previous line as
   6344   // appropriate.
   6345   EXPECT_EQ("function({// First element:\n"
   6346             "          1,\n"
   6347             "          // Second element:\n"
   6348             "          2});",
   6349             format("function({\n"
   6350                    "    // First element:\n"
   6351                    "    1,\n"
   6352                    "    // Second element:\n"
   6353                    "    2});"));
   6354   EXPECT_EQ("std::vector<int> MyNumbers{\n"
   6355             "    // First element:\n"
   6356             "    1,\n"
   6357             "    // Second element:\n"
   6358             "    2};",
   6359             format("std::vector<int> MyNumbers{// First element:\n"
   6360                    "                           1,\n"
   6361                    "                           // Second element:\n"
   6362                    "                           2};",
   6363                    getLLVMStyleWithColumns(30)));
   6364   // A trailing comma should still lead to an enforced line break.
   6365   EXPECT_EQ("vector<int> SomeVector = {\n"
   6366             "    // aaa\n"
   6367             "    1, 2,\n"
   6368             "};",
   6369             format("vector<int> SomeVector = { // aaa\n"
   6370                    "    1, 2, };"));
   6371 
   6372   FormatStyle ExtraSpaces = getLLVMStyle();
   6373   ExtraSpaces.Cpp11BracedListStyle = false;
   6374   ExtraSpaces.ColumnLimit = 75;
   6375   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
   6376   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
   6377   verifyFormat("f({ 1, 2 });", ExtraSpaces);
   6378   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
   6379   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
   6380   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
   6381   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
   6382   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
   6383   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
   6384   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
   6385   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
   6386   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
   6387   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
   6388   verifyFormat("class Class {\n"
   6389                "  T member = { arg1, arg2 };\n"
   6390                "};",
   6391                ExtraSpaces);
   6392   verifyFormat(
   6393       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   6394       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
   6395       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
   6396       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
   6397       ExtraSpaces);
   6398   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
   6399   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
   6400                ExtraSpaces);
   6401   verifyFormat(
   6402       "someFunction(OtherParam,\n"
   6403       "             BracedList{ // comment 1 (Forcing interesting break)\n"
   6404       "                         param1, param2,\n"
   6405       "                         // comment 2\n"
   6406       "                         param3, param4 });",
   6407       ExtraSpaces);
   6408   verifyFormat(
   6409       "std::this_thread::sleep_for(\n"
   6410       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
   6411       ExtraSpaces);
   6412   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
   6413                "    aaaaaaa,\n"
   6414                "    aaaaaaaaaa,\n"
   6415                "    aaaaa,\n"
   6416                "    aaaaaaaaaaaaaaa,\n"
   6417                "    aaa,\n"
   6418                "    aaaaaaaaaa,\n"
   6419                "    a,\n"
   6420                "    aaaaaaaaaaaaaaaaaaaaa,\n"
   6421                "    aaaaaaaaaaaa,\n"
   6422                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
   6423                "    aaaaaaa,\n"
   6424                "    a};");
   6425   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
   6426 }
   6427 
   6428 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
   6429   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6430                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6431                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6432                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6433                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6434                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
   6435   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
   6436                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6437                "                 1, 22, 333, 4444, 55555, //\n"
   6438                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
   6439                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
   6440   verifyFormat(
   6441       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
   6442       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
   6443       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
   6444       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
   6445       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
   6446       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
   6447       "                 7777777};");
   6448   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
   6449                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
   6450                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
   6451   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
   6452                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
   6453                "    // Separating comment.\n"
   6454                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
   6455   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
   6456                "    // Leading comment\n"
   6457                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
   6458                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
   6459   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
   6460                "                 1, 1, 1, 1};",
   6461                getLLVMStyleWithColumns(39));
   6462   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
   6463                "                 1, 1, 1, 1};",
   6464                getLLVMStyleWithColumns(38));
   6465   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
   6466                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
   6467                getLLVMStyleWithColumns(43));
   6468   verifyFormat(
   6469       "static unsigned SomeValues[10][3] = {\n"
   6470       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
   6471       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
   6472   verifyFormat("static auto fields = new vector<string>{\n"
   6473                "    \"aaaaaaaaaaaaa\",\n"
   6474                "    \"aaaaaaaaaaaaa\",\n"
   6475                "    \"aaaaaaaaaaaa\",\n"
   6476                "    \"aaaaaaaaaaaaaa\",\n"
   6477                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
   6478                "    \"aaaaaaaaaaaa\",\n"
   6479                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
   6480                "};");
   6481   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
   6482   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
   6483                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
   6484                "                 3, cccccccccccccccccccccc};",
   6485                getLLVMStyleWithColumns(60));
   6486 
   6487   // Trailing commas.
   6488   verifyFormat("vector<int> x = {\n"
   6489                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
   6490                "};",
   6491                getLLVMStyleWithColumns(39));
   6492   verifyFormat("vector<int> x = {\n"
   6493                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
   6494                "};",
   6495                getLLVMStyleWithColumns(39));
   6496   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
   6497                "                 1, 1, 1, 1,\n"
   6498                "                 /**/ /**/};",
   6499                getLLVMStyleWithColumns(39));
   6500 
   6501   // Trailing comment in the first line.
   6502   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
   6503                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
   6504                "    111111111,  222222222,  3333333333,  444444444,  //\n"
   6505                "    11111111,   22222222,   333333333,   44444444};");
   6506   // Trailing comment in the last line.
   6507   verifyFormat("int aaaaa[] = {\n"
   6508                "    1, 2, 3, // comment\n"
   6509                "    4, 5, 6  // comment\n"
   6510                "};");
   6511 
   6512   // With nested lists, we should either format one item per line or all nested
   6513   // lists one on line.
   6514   // FIXME: For some nested lists, we can do better.
   6515   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
   6516                "        {aaaaaaaaaaaaaaaaaaa},\n"
   6517                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
   6518                "        {aaaaaaaaaaaaaaaaa}};",
   6519                getLLVMStyleWithColumns(60));
   6520   verifyFormat(
   6521       "SomeStruct my_struct_array = {\n"
   6522       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
   6523       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
   6524       "    {aaa, aaa},\n"
   6525       "    {aaa, aaa},\n"
   6526       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
   6527       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
   6528       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
   6529 
   6530   // No column layout should be used here.
   6531   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
   6532                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
   6533 
   6534   verifyNoCrash("a<,");
   6535 
   6536   // No braced initializer here.
   6537   verifyFormat("void f() {\n"
   6538                "  struct Dummy {};\n"
   6539                "  f(v);\n"
   6540                "}");
   6541 }
   6542 
   6543 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
   6544   FormatStyle DoNotMerge = getLLVMStyle();
   6545   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
   6546 
   6547   verifyFormat("void f() { return 42; }");
   6548   verifyFormat("void f() {\n"
   6549                "  return 42;\n"
   6550                "}",
   6551                DoNotMerge);
   6552   verifyFormat("void f() {\n"
   6553                "  // Comment\n"
   6554                "}");
   6555   verifyFormat("{\n"
   6556                "#error {\n"
   6557                "  int a;\n"
   6558                "}");
   6559   verifyFormat("{\n"
   6560                "  int a;\n"
   6561                "#error {\n"
   6562                "}");
   6563   verifyFormat("void f() {} // comment");
   6564   verifyFormat("void f() { int a; } // comment");
   6565   verifyFormat("void f() {\n"
   6566                "} // comment",
   6567                DoNotMerge);
   6568   verifyFormat("void f() {\n"
   6569                "  int a;\n"
   6570                "} // comment",
   6571                DoNotMerge);
   6572   verifyFormat("void f() {\n"
   6573                "} // comment",
   6574                getLLVMStyleWithColumns(15));
   6575 
   6576   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
   6577   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
   6578 
   6579   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
   6580   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
   6581   verifyFormat("class C {\n"
   6582                "  C()\n"
   6583                "      : iiiiiiii(nullptr),\n"
   6584                "        kkkkkkk(nullptr),\n"
   6585                "        mmmmmmm(nullptr),\n"
   6586                "        nnnnnnn(nullptr) {}\n"
   6587                "};",
   6588                getGoogleStyle());
   6589 
   6590   FormatStyle NoColumnLimit = getLLVMStyle();
   6591   NoColumnLimit.ColumnLimit = 0;
   6592   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
   6593   EXPECT_EQ("class C {\n"
   6594             "  A() : b(0) {}\n"
   6595             "};",
   6596             format("class C{A():b(0){}};", NoColumnLimit));
   6597   EXPECT_EQ("A()\n"
   6598             "    : b(0) {\n"
   6599             "}",
   6600             format("A()\n:b(0)\n{\n}", NoColumnLimit));
   6601 
   6602   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
   6603   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
   6604       FormatStyle::SFS_None;
   6605   EXPECT_EQ("A()\n"
   6606             "    : b(0) {\n"
   6607             "}",
   6608             format("A():b(0){}", DoNotMergeNoColumnLimit));
   6609   EXPECT_EQ("A()\n"
   6610             "    : b(0) {\n"
   6611             "}",
   6612             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
   6613 
   6614   verifyFormat("#define A          \\\n"
   6615                "  void f() {       \\\n"
   6616                "    int i;         \\\n"
   6617                "  }",
   6618                getLLVMStyleWithColumns(20));
   6619   verifyFormat("#define A           \\\n"
   6620                "  void f() { int i; }",
   6621                getLLVMStyleWithColumns(21));
   6622   verifyFormat("#define A            \\\n"
   6623                "  void f() {         \\\n"
   6624                "    int i;           \\\n"
   6625                "  }                  \\\n"
   6626                "  int j;",
   6627                getLLVMStyleWithColumns(22));
   6628   verifyFormat("#define A             \\\n"
   6629                "  void f() { int i; } \\\n"
   6630                "  int j;",
   6631                getLLVMStyleWithColumns(23));
   6632 }
   6633 
   6634 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
   6635   FormatStyle MergeInlineOnly = getLLVMStyle();
   6636   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
   6637   verifyFormat("class C {\n"
   6638                "  int f() { return 42; }\n"
   6639                "};",
   6640                MergeInlineOnly);
   6641   verifyFormat("int f() {\n"
   6642                "  return 42;\n"
   6643                "}",
   6644                MergeInlineOnly);
   6645 }
   6646 
   6647 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
   6648   // Elaborate type variable declarations.
   6649   verifyFormat("struct foo a = {bar};\nint n;");
   6650   verifyFormat("class foo a = {bar};\nint n;");
   6651   verifyFormat("union foo a = {bar};\nint n;");
   6652 
   6653   // Elaborate types inside function definitions.
   6654   verifyFormat("struct foo f() {}\nint n;");
   6655   verifyFormat("class foo f() {}\nint n;");
   6656   verifyFormat("union foo f() {}\nint n;");
   6657 
   6658   // Templates.
   6659   verifyFormat("template <class X> void f() {}\nint n;");
   6660   verifyFormat("template <struct X> void f() {}\nint n;");
   6661   verifyFormat("template <union X> void f() {}\nint n;");
   6662 
   6663   // Actual definitions...
   6664   verifyFormat("struct {\n} n;");
   6665   verifyFormat(
   6666       "template <template <class T, class Y>, class Z> class X {\n} n;");
   6667   verifyFormat("union Z {\n  int n;\n} x;");
   6668   verifyFormat("class MACRO Z {\n} n;");
   6669   verifyFormat("class MACRO(X) Z {\n} n;");
   6670   verifyFormat("class __attribute__(X) Z {\n} n;");
   6671   verifyFormat("class __declspec(X) Z {\n} n;");
   6672   verifyFormat("class A##B##C {\n} n;");
   6673   verifyFormat("class alignas(16) Z {\n} n;");
   6674   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
   6675   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
   6676 
   6677   // Redefinition from nested context:
   6678   verifyFormat("class A::B::C {\n} n;");
   6679 
   6680   // Template definitions.
   6681   verifyFormat(
   6682       "template <typename F>\n"
   6683       "Matcher(const Matcher<F> &Other,\n"
   6684       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
   6685       "                             !is_same<F, T>::value>::type * = 0)\n"
   6686       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
   6687 
   6688   // FIXME: This is still incorrectly handled at the formatter side.
   6689   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
   6690   verifyFormat("int i = SomeFunction(a<b, a> b);");
   6691 
   6692   // FIXME:
   6693   // This now gets parsed incorrectly as class definition.
   6694   // verifyFormat("class A<int> f() {\n}\nint n;");
   6695 
   6696   // Elaborate types where incorrectly parsing the structural element would
   6697   // break the indent.
   6698   verifyFormat("if (true)\n"
   6699                "  class X x;\n"
   6700                "else\n"
   6701                "  f();\n");
   6702 
   6703   // This is simply incomplete. Formatting is not important, but must not crash.
   6704   verifyFormat("class A:");
   6705 }
   6706 
   6707 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
   6708   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
   6709             format("#error Leave     all         white!!!!! space* alone!\n"));
   6710   EXPECT_EQ(
   6711       "#warning Leave     all         white!!!!! space* alone!\n",
   6712       format("#warning Leave     all         white!!!!! space* alone!\n"));
   6713   EXPECT_EQ("#error 1", format("  #  error   1"));
   6714   EXPECT_EQ("#warning 1", format("  #  warning 1"));
   6715 }
   6716 
   6717 TEST_F(FormatTest, FormatHashIfExpressions) {
   6718   verifyFormat("#if AAAA && BBBB");
   6719   verifyFormat("#if (AAAA && BBBB)");
   6720   verifyFormat("#elif (AAAA && BBBB)");
   6721   // FIXME: Come up with a better indentation for #elif.
   6722   verifyFormat(
   6723       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
   6724       "    defined(BBBBBBBB)\n"
   6725       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
   6726       "    defined(BBBBBBBB)\n"
   6727       "#endif",
   6728       getLLVMStyleWithColumns(65));
   6729 }
   6730 
   6731 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
   6732   FormatStyle AllowsMergedIf = getGoogleStyle();
   6733   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
   6734   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
   6735   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
   6736   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
   6737   EXPECT_EQ("if (true) return 42;",
   6738             format("if (true)\nreturn 42;", AllowsMergedIf));
   6739   FormatStyle ShortMergedIf = AllowsMergedIf;
   6740   ShortMergedIf.ColumnLimit = 25;
   6741   verifyFormat("#define A \\\n"
   6742                "  if (true) return 42;",
   6743                ShortMergedIf);
   6744   verifyFormat("#define A \\\n"
   6745                "  f();    \\\n"
   6746                "  if (true)\n"
   6747                "#define B",
   6748                ShortMergedIf);
   6749   verifyFormat("#define A \\\n"
   6750                "  f();    \\\n"
   6751                "  if (true)\n"
   6752                "g();",
   6753                ShortMergedIf);
   6754   verifyFormat("{\n"
   6755                "#ifdef A\n"
   6756                "  // Comment\n"
   6757                "  if (true) continue;\n"
   6758                "#endif\n"
   6759                "  // Comment\n"
   6760                "  if (true) continue;\n"
   6761                "}",
   6762                ShortMergedIf);
   6763   ShortMergedIf.ColumnLimit = 29;
   6764   verifyFormat("#define A                   \\\n"
   6765                "  if (aaaaaaaaaa) return 1; \\\n"
   6766                "  return 2;",
   6767                ShortMergedIf);
   6768   ShortMergedIf.ColumnLimit = 28;
   6769   verifyFormat("#define A         \\\n"
   6770                "  if (aaaaaaaaaa) \\\n"
   6771                "    return 1;     \\\n"
   6772                "  return 2;",
   6773                ShortMergedIf);
   6774 }
   6775 
   6776 TEST_F(FormatTest, BlockCommentsInControlLoops) {
   6777   verifyFormat("if (0) /* a comment in a strange place */ {\n"
   6778                "  f();\n"
   6779                "}");
   6780   verifyFormat("if (0) /* a comment in a strange place */ {\n"
   6781                "  f();\n"
   6782                "} /* another comment */ else /* comment #3 */ {\n"
   6783                "  g();\n"
   6784                "}");
   6785   verifyFormat("while (0) /* a comment in a strange place */ {\n"
   6786                "  f();\n"
   6787                "}");
   6788   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
   6789                "  f();\n"
   6790                "}");
   6791   verifyFormat("do /* a comment in a strange place */ {\n"
   6792                "  f();\n"
   6793                "} /* another comment */ while (0);");
   6794 }
   6795 
   6796 TEST_F(FormatTest, BlockComments) {
   6797   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
   6798             format("/* *//* */  /* */\n/* *//* */  /* */"));
   6799   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
   6800   EXPECT_EQ("#define A /*123*/ \\\n"
   6801             "  b\n"
   6802             "/* */\n"
   6803             "someCall(\n"
   6804             "    parameter);",
   6805             format("#define A /*123*/ b\n"
   6806                    "/* */\n"
   6807                    "someCall(parameter);",
   6808                    getLLVMStyleWithColumns(15)));
   6809 
   6810   EXPECT_EQ("#define A\n"
   6811             "/* */ someCall(\n"
   6812             "    parameter);",
   6813             format("#define A\n"
   6814                    "/* */someCall(parameter);",
   6815                    getLLVMStyleWithColumns(15)));
   6816   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
   6817   EXPECT_EQ("/*\n"
   6818             "*\n"
   6819             " * aaaaaa\n"
   6820             " * aaaaaa\n"
   6821             "*/",
   6822             format("/*\n"
   6823                    "*\n"
   6824                    " * aaaaaa aaaaaa\n"
   6825                    "*/",
   6826                    getLLVMStyleWithColumns(10)));
   6827   EXPECT_EQ("/*\n"
   6828             "**\n"
   6829             "* aaaaaa\n"
   6830             "*aaaaaa\n"
   6831             "*/",
   6832             format("/*\n"
   6833                    "**\n"
   6834                    "* aaaaaa aaaaaa\n"
   6835                    "*/",
   6836                    getLLVMStyleWithColumns(10)));
   6837 
   6838   FormatStyle NoBinPacking = getLLVMStyle();
   6839   NoBinPacking.BinPackParameters = false;
   6840   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
   6841             "             2, /* comment 2 */\n"
   6842             "             3, /* comment 3 */\n"
   6843             "             aaaa,\n"
   6844             "             bbbb);",
   6845             format("someFunction (1,   /* comment 1 */\n"
   6846                    "                2, /* comment 2 */  \n"
   6847                    "               3,   /* comment 3 */\n"
   6848                    "aaaa, bbbb );",
   6849                    NoBinPacking));
   6850   verifyFormat(
   6851       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   6852       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
   6853   EXPECT_EQ(
   6854       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
   6855       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
   6856       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
   6857       format(
   6858           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
   6859           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
   6860           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
   6861   EXPECT_EQ(
   6862       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
   6863       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
   6864       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
   6865       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
   6866              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
   6867              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
   6868 
   6869   verifyFormat("void f(int * /* unused */) {}");
   6870 
   6871   EXPECT_EQ("/*\n"
   6872             " **\n"
   6873             " */",
   6874             format("/*\n"
   6875                    " **\n"
   6876                    " */"));
   6877   EXPECT_EQ("/*\n"
   6878             " *q\n"
   6879             " */",
   6880             format("/*\n"
   6881                    " *q\n"
   6882                    " */"));
   6883   EXPECT_EQ("/*\n"
   6884             " * q\n"
   6885             " */",
   6886             format("/*\n"
   6887                    " * q\n"
   6888                    " */"));
   6889   EXPECT_EQ("/*\n"
   6890             " **/",
   6891             format("/*\n"
   6892                    " **/"));
   6893   EXPECT_EQ("/*\n"
   6894             " ***/",
   6895             format("/*\n"
   6896                    " ***/"));
   6897 }
   6898 
   6899 TEST_F(FormatTest, BlockCommentsInMacros) {
   6900   EXPECT_EQ("#define A          \\\n"
   6901             "  {                \\\n"
   6902             "    /* one line */ \\\n"
   6903             "    someCall();",
   6904             format("#define A {        \\\n"
   6905                    "  /* one line */   \\\n"
   6906                    "  someCall();",
   6907                    getLLVMStyleWithColumns(20)));
   6908   EXPECT_EQ("#define A          \\\n"
   6909             "  {                \\\n"
   6910             "    /* previous */ \\\n"
   6911             "    /* one line */ \\\n"
   6912             "    someCall();",
   6913             format("#define A {        \\\n"
   6914                    "  /* previous */   \\\n"
   6915                    "  /* one line */   \\\n"
   6916                    "  someCall();",
   6917                    getLLVMStyleWithColumns(20)));
   6918 }
   6919 
   6920 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
   6921   EXPECT_EQ("a = {\n"
   6922             "    1111 /*    */\n"
   6923             "};",
   6924             format("a = {1111 /*    */\n"
   6925                    "};",
   6926                    getLLVMStyleWithColumns(15)));
   6927   EXPECT_EQ("a = {\n"
   6928             "    1111 /*      */\n"
   6929             "};",
   6930             format("a = {1111 /*      */\n"
   6931                    "};",
   6932                    getLLVMStyleWithColumns(15)));
   6933 
   6934   // FIXME: The formatting is still wrong here.
   6935   EXPECT_EQ("a = {\n"
   6936             "    1111 /*      a\n"
   6937             "            */\n"
   6938             "};",
   6939             format("a = {1111 /*      a */\n"
   6940                    "};",
   6941                    getLLVMStyleWithColumns(15)));
   6942 }
   6943 
   6944 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
   6945   // FIXME: This is not what we want...
   6946   verifyFormat("{\n"
   6947                "// a"
   6948                "// b");
   6949 }
   6950 
   6951 TEST_F(FormatTest, FormatStarDependingOnContext) {
   6952   verifyFormat("void f(int *a);");
   6953   verifyFormat("void f() { f(fint * b); }");
   6954   verifyFormat("class A {\n  void f(int *a);\n};");
   6955   verifyFormat("class A {\n  int *a;\n};");
   6956   verifyFormat("namespace a {\n"
   6957                "namespace b {\n"
   6958                "class A {\n"
   6959                "  void f() {}\n"
   6960                "  int *a;\n"
   6961                "};\n"
   6962                "}\n"
   6963                "}");
   6964 }
   6965 
   6966 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
   6967   verifyFormat("while");
   6968   verifyFormat("operator");
   6969 }
   6970 
   6971 //===----------------------------------------------------------------------===//
   6972 // Objective-C tests.
   6973 //===----------------------------------------------------------------------===//
   6974 
   6975 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
   6976   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
   6977   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
   6978             format("-(NSUInteger)indexOfObject:(id)anObject;"));
   6979   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
   6980   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
   6981   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
   6982             format("-(NSInteger)Method3:(id)anObject;"));
   6983   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
   6984             format("-(NSInteger)Method4:(id)anObject;"));
   6985   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
   6986             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
   6987   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
   6988             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
   6989   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
   6990             "forAllCells:(BOOL)flag;",
   6991             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
   6992                    "forAllCells:(BOOL)flag;"));
   6993 
   6994   // Very long objectiveC method declaration.
   6995   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
   6996                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
   6997   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
   6998                "                    inRange:(NSRange)range\n"
   6999                "                   outRange:(NSRange)out_range\n"
   7000                "                  outRange1:(NSRange)out_range1\n"
   7001                "                  outRange2:(NSRange)out_range2\n"
   7002                "                  outRange3:(NSRange)out_range3\n"
   7003                "                  outRange4:(NSRange)out_range4\n"
   7004                "                  outRange5:(NSRange)out_range5\n"
   7005                "                  outRange6:(NSRange)out_range6\n"
   7006                "                  outRange7:(NSRange)out_range7\n"
   7007                "                  outRange8:(NSRange)out_range8\n"
   7008                "                  outRange9:(NSRange)out_range9;");
   7009 
   7010   // When the function name has to be wrapped.
   7011   FormatStyle Style = getLLVMStyle();
   7012   Style.IndentWrappedFunctionNames = false;
   7013   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
   7014                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
   7015                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
   7016                "}",
   7017                Style);
   7018   Style.IndentWrappedFunctionNames = true;
   7019   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
   7020                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
   7021                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
   7022                "}",
   7023                Style);
   7024 
   7025   verifyFormat("- (int)sum:(vector<int>)numbers;");
   7026   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
   7027   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
   7028   // protocol lists (but not for template classes):
   7029   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
   7030 
   7031   verifyFormat("- (int (*)())foo:(int (*)())f;");
   7032   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
   7033 
   7034   // If there's no return type (very rare in practice!), LLVM and Google style
   7035   // agree.
   7036   verifyFormat("- foo;");
   7037   verifyFormat("- foo:(int)f;");
   7038   verifyGoogleFormat("- foo:(int)foo;");
   7039 }
   7040 
   7041 TEST_F(FormatTest, FormatObjCInterface) {
   7042   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
   7043                "@public\n"
   7044                "  int field1;\n"
   7045                "@protected\n"
   7046                "  int field2;\n"
   7047                "@private\n"
   7048                "  int field3;\n"
   7049                "@package\n"
   7050                "  int field4;\n"
   7051                "}\n"
   7052                "+ (id)init;\n"
   7053                "@end");
   7054 
   7055   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
   7056                      " @public\n"
   7057                      "  int field1;\n"
   7058                      " @protected\n"
   7059                      "  int field2;\n"
   7060                      " @private\n"
   7061                      "  int field3;\n"
   7062                      " @package\n"
   7063                      "  int field4;\n"
   7064                      "}\n"
   7065                      "+ (id)init;\n"
   7066                      "@end");
   7067 
   7068   verifyFormat("@interface /* wait for it */ Foo\n"
   7069                "+ (id)init;\n"
   7070                "// Look, a comment!\n"
   7071                "- (int)answerWith:(int)i;\n"
   7072                "@end");
   7073 
   7074   verifyFormat("@interface Foo\n"
   7075                "@end\n"
   7076                "@interface Bar\n"
   7077                "@end");
   7078 
   7079   verifyFormat("@interface Foo : Bar\n"
   7080                "+ (id)init;\n"
   7081                "@end");
   7082 
   7083   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
   7084                "+ (id)init;\n"
   7085                "@end");
   7086 
   7087   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
   7088                      "+ (id)init;\n"
   7089                      "@end");
   7090 
   7091   verifyFormat("@interface Foo (HackStuff)\n"
   7092                "+ (id)init;\n"
   7093                "@end");
   7094 
   7095   verifyFormat("@interface Foo ()\n"
   7096                "+ (id)init;\n"
   7097                "@end");
   7098 
   7099   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
   7100                "+ (id)init;\n"
   7101                "@end");
   7102 
   7103   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
   7104                      "+ (id)init;\n"
   7105                      "@end");
   7106 
   7107   verifyFormat("@interface Foo {\n"
   7108                "  int _i;\n"
   7109                "}\n"
   7110                "+ (id)init;\n"
   7111                "@end");
   7112 
   7113   verifyFormat("@interface Foo : Bar {\n"
   7114                "  int _i;\n"
   7115                "}\n"
   7116                "+ (id)init;\n"
   7117                "@end");
   7118 
   7119   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
   7120                "  int _i;\n"
   7121                "}\n"
   7122                "+ (id)init;\n"
   7123                "@end");
   7124 
   7125   verifyFormat("@interface Foo (HackStuff) {\n"
   7126                "  int _i;\n"
   7127                "}\n"
   7128                "+ (id)init;\n"
   7129                "@end");
   7130 
   7131   verifyFormat("@interface Foo () {\n"
   7132                "  int _i;\n"
   7133                "}\n"
   7134                "+ (id)init;\n"
   7135                "@end");
   7136 
   7137   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
   7138                "  int _i;\n"
   7139                "}\n"
   7140                "+ (id)init;\n"
   7141                "@end");
   7142 
   7143   FormatStyle OnePerLine = getGoogleStyle();
   7144   OnePerLine.BinPackParameters = false;
   7145   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
   7146                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   7147                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   7148                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
   7149                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
   7150                "}",
   7151                OnePerLine);
   7152 }
   7153 
   7154 TEST_F(FormatTest, FormatObjCImplementation) {
   7155   verifyFormat("@implementation Foo : NSObject {\n"
   7156                "@public\n"
   7157                "  int field1;\n"
   7158                "@protected\n"
   7159                "  int field2;\n"
   7160                "@private\n"
   7161                "  int field3;\n"
   7162                "@package\n"
   7163                "  int field4;\n"
   7164                "}\n"
   7165                "+ (id)init {\n}\n"
   7166                "@end");
   7167 
   7168   verifyGoogleFormat("@implementation Foo : NSObject {\n"
   7169                      " @public\n"
   7170                      "  int field1;\n"
   7171                      " @protected\n"
   7172                      "  int field2;\n"
   7173                      " @private\n"
   7174                      "  int field3;\n"
   7175                      " @package\n"
   7176                      "  int field4;\n"
   7177                      "}\n"
   7178                      "+ (id)init {\n}\n"
   7179                      "@end");
   7180 
   7181   verifyFormat("@implementation Foo\n"
   7182                "+ (id)init {\n"
   7183                "  if (true)\n"
   7184                "    return nil;\n"
   7185                "}\n"
   7186                "// Look, a comment!\n"
   7187                "- (int)answerWith:(int)i {\n"
   7188                "  return i;\n"
   7189                "}\n"
   7190                "+ (int)answerWith:(int)i {\n"
   7191                "  return i;\n"
   7192                "}\n"
   7193                "@end");
   7194 
   7195   verifyFormat("@implementation Foo\n"
   7196                "@end\n"
   7197                "@implementation Bar\n"
   7198                "@end");
   7199 
   7200   EXPECT_EQ("@implementation Foo : Bar\n"
   7201             "+ (id)init {\n}\n"
   7202             "- (void)foo {\n}\n"
   7203             "@end",
   7204             format("@implementation Foo : Bar\n"
   7205                    "+(id)init{}\n"
   7206                    "-(void)foo{}\n"
   7207                    "@end"));
   7208 
   7209   verifyFormat("@implementation Foo {\n"
   7210                "  int _i;\n"
   7211                "}\n"
   7212                "+ (id)init {\n}\n"
   7213                "@end");
   7214 
   7215   verifyFormat("@implementation Foo : Bar {\n"
   7216                "  int _i;\n"
   7217                "}\n"
   7218                "+ (id)init {\n}\n"
   7219                "@end");
   7220 
   7221   verifyFormat("@implementation Foo (HackStuff)\n"
   7222                "+ (id)init {\n}\n"
   7223                "@end");
   7224   verifyFormat("@implementation ObjcClass\n"
   7225                "- (void)method;\n"
   7226                "{}\n"
   7227                "@end");
   7228 }
   7229 
   7230 TEST_F(FormatTest, FormatObjCProtocol) {
   7231   verifyFormat("@protocol Foo\n"
   7232                "@property(weak) id delegate;\n"
   7233                "- (NSUInteger)numberOfThings;\n"
   7234                "@end");
   7235 
   7236   verifyFormat("@protocol MyProtocol <NSObject>\n"
   7237                "- (NSUInteger)numberOfThings;\n"
   7238                "@end");
   7239 
   7240   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
   7241                      "- (NSUInteger)numberOfThings;\n"
   7242                      "@end");
   7243 
   7244   verifyFormat("@protocol Foo;\n"
   7245                "@protocol Bar;\n");
   7246 
   7247   verifyFormat("@protocol Foo\n"
   7248                "@end\n"
   7249                "@protocol Bar\n"
   7250                "@end");
   7251 
   7252   verifyFormat("@protocol myProtocol\n"
   7253                "- (void)mandatoryWithInt:(int)i;\n"
   7254                "@optional\n"
   7255                "- (void)optional;\n"
   7256                "@required\n"
   7257                "- (void)required;\n"
   7258                "@optional\n"
   7259                "@property(assign) int madProp;\n"
   7260                "@end\n");
   7261 
   7262   verifyFormat("@property(nonatomic, assign, readonly)\n"
   7263                "    int *looooooooooooooooooooooooooooongNumber;\n"
   7264                "@property(nonatomic, assign, readonly)\n"
   7265                "    NSString *looooooooooooooooooooooooooooongName;");
   7266 
   7267   verifyFormat("@implementation PR18406\n"
   7268                "}\n"
   7269                "@end");
   7270 }
   7271 
   7272 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
   7273   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
   7274                "                   rect:(NSRect)theRect\n"
   7275                "               interval:(float)theInterval {\n"
   7276                "}");
   7277   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
   7278                "          longKeyword:(NSRect)theRect\n"
   7279                "    evenLongerKeyword:(float)theInterval\n"
   7280                "                error:(NSError **)theError {\n"
   7281                "}");
   7282   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
   7283                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
   7284                "    NS_DESIGNATED_INITIALIZER;",
   7285                getLLVMStyleWithColumns(60));
   7286 
   7287   // Continuation indent width should win over aligning colons if the function
   7288   // name is long.
   7289   FormatStyle continuationStyle = getGoogleStyle();
   7290   continuationStyle.ColumnLimit = 40;
   7291   continuationStyle.IndentWrappedFunctionNames = true;
   7292   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
   7293                "    dontAlignNamef:(NSRect)theRect {\n"
   7294                "}",
   7295                continuationStyle);
   7296 
   7297   // Make sure we don't break aligning for short parameter names.
   7298   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
   7299                "       aShortf:(NSRect)theRect {\n"
   7300                "}",
   7301                continuationStyle);
   7302 }
   7303 
   7304 TEST_F(FormatTest, FormatObjCMethodExpr) {
   7305   verifyFormat("[foo bar:baz];");
   7306   verifyFormat("return [foo bar:baz];");
   7307   verifyFormat("return (a)[foo bar:baz];");
   7308   verifyFormat("f([foo bar:baz]);");
   7309   verifyFormat("f(2, [foo bar:baz]);");
   7310   verifyFormat("f(2, a ? b : c);");
   7311   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
   7312 
   7313   // Unary operators.
   7314   verifyFormat("int a = +[foo bar:baz];");
   7315   verifyFormat("int a = -[foo bar:baz];");
   7316   verifyFormat("int a = ![foo bar:baz];");
   7317   verifyFormat("int a = ~[foo bar:baz];");
   7318   verifyFormat("int a = ++[foo bar:baz];");
   7319   verifyFormat("int a = --[foo bar:baz];");
   7320   verifyFormat("int a = sizeof [foo bar:baz];");
   7321   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
   7322   verifyFormat("int a = &[foo bar:baz];");
   7323   verifyFormat("int a = *[foo bar:baz];");
   7324   // FIXME: Make casts work, without breaking f()[4].
   7325   // verifyFormat("int a = (int)[foo bar:baz];");
   7326   // verifyFormat("return (int)[foo bar:baz];");
   7327   // verifyFormat("(void)[foo bar:baz];");
   7328   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
   7329 
   7330   // Binary operators.
   7331   verifyFormat("[foo bar:baz], [foo bar:baz];");
   7332   verifyFormat("[foo bar:baz] = [foo bar:baz];");
   7333   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
   7334   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
   7335   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
   7336   verifyFormat("[foo bar:baz] += [foo bar:baz];");
   7337   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
   7338   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
   7339   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
   7340   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
   7341   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
   7342   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
   7343   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
   7344   verifyFormat("[foo bar:baz] || [foo bar:baz];");
   7345   verifyFormat("[foo bar:baz] && [foo bar:baz];");
   7346   verifyFormat("[foo bar:baz] | [foo bar:baz];");
   7347   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
   7348   verifyFormat("[foo bar:baz] & [foo bar:baz];");
   7349   verifyFormat("[foo bar:baz] == [foo bar:baz];");
   7350   verifyFormat("[foo bar:baz] != [foo bar:baz];");
   7351   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
   7352   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
   7353   verifyFormat("[foo bar:baz] > [foo bar:baz];");
   7354   verifyFormat("[foo bar:baz] < [foo bar:baz];");
   7355   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
   7356   verifyFormat("[foo bar:baz] << [foo bar:baz];");
   7357   verifyFormat("[foo bar:baz] - [foo bar:baz];");
   7358   verifyFormat("[foo bar:baz] + [foo bar:baz];");
   7359   verifyFormat("[foo bar:baz] * [foo bar:baz];");
   7360   verifyFormat("[foo bar:baz] / [foo bar:baz];");
   7361   verifyFormat("[foo bar:baz] % [foo bar:baz];");
   7362   // Whew!
   7363 
   7364   verifyFormat("return in[42];");
   7365   verifyFormat("for (auto v : in[1]) {\n}");
   7366   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
   7367   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
   7368   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
   7369   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
   7370   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
   7371   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
   7372                "}");
   7373   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
   7374   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
   7375   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
   7376 
   7377   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
   7378   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
   7379   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
   7380   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
   7381   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
   7382   verifyFormat("[button setAction:@selector(zoomOut:)];");
   7383   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
   7384 
   7385   verifyFormat("arr[[self indexForFoo:a]];");
   7386   verifyFormat("throw [self errorFor:a];");
   7387   verifyFormat("@throw [self errorFor:a];");
   7388 
   7389   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
   7390   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
   7391   verifyFormat("4 > 4 ? (id)a : (id)baz;");
   7392 
   7393   // This tests that the formatter doesn't break after "backing" but before ":",
   7394   // which would be at 80 columns.
   7395   verifyFormat(
   7396       "void f() {\n"
   7397       "  if ((self = [super initWithContentRect:contentRect\n"
   7398       "                               styleMask:styleMask ?: otherMask\n"
   7399       "                                 backing:NSBackingStoreBuffered\n"
   7400       "                                   defer:YES]))");
   7401 
   7402   verifyFormat(
   7403       "[foo checkThatBreakingAfterColonWorksOk:\n"
   7404       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
   7405 
   7406   verifyFormat("[myObj short:arg1 // Force line break\n"
   7407                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
   7408                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
   7409                "                error:arg4];");
   7410   verifyFormat(
   7411       "void f() {\n"
   7412       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
   7413       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
   7414       "                                     pos.width(), pos.height())\n"
   7415       "                styleMask:NSBorderlessWindowMask\n"
   7416       "                  backing:NSBackingStoreBuffered\n"
   7417       "                    defer:NO]);\n"
   7418       "}");
   7419   verifyFormat(
   7420       "void f() {\n"
   7421       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
   7422       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
   7423       "                                 pos.width(), pos.height())\n"
   7424       "                syeMask:NSBorderlessWindowMask\n"
   7425       "                  bking:NSBackingStoreBuffered\n"
   7426       "                    der:NO]);\n"
   7427       "}",
   7428       getLLVMStyleWithColumns(70));
   7429   verifyFormat(
   7430       "void f() {\n"
   7431       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
   7432       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
   7433       "                                     pos.width(), pos.height())\n"
   7434       "                styleMask:NSBorderlessWindowMask\n"
   7435       "                  backing:NSBackingStoreBuffered\n"
   7436       "                    defer:NO]);\n"
   7437       "}",
   7438       getChromiumStyle(FormatStyle::LK_Cpp));
   7439   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
   7440                "                             with:contentsNativeView];");
   7441 
   7442   verifyFormat(
   7443       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
   7444       "           owner:nillllll];");
   7445 
   7446   verifyFormat(
   7447       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
   7448       "        forType:kBookmarkButtonDragType];");
   7449 
   7450   verifyFormat("[defaultCenter addObserver:self\n"
   7451                "                  selector:@selector(willEnterFullscreen)\n"
   7452                "                      name:kWillEnterFullscreenNotification\n"
   7453                "                    object:nil];");
   7454   verifyFormat("[image_rep drawInRect:drawRect\n"
   7455                "             fromRect:NSZeroRect\n"
   7456                "            operation:NSCompositeCopy\n"
   7457                "             fraction:1.0\n"
   7458                "       respectFlipped:NO\n"
   7459                "                hints:nil];");
   7460   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   7461                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
   7462   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
   7463                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
   7464   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
   7465                "    aaaaaaaaaaaaaaaaaaaaaa];");
   7466   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
   7467                "        .aaaaaaaa.aaaaaaaa];", // FIXME: Indentation seems off.
   7468                getLLVMStyleWithColumns(60));
   7469 
   7470   verifyFormat(
   7471       "scoped_nsobject<NSTextField> message(\n"
   7472       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
   7473       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
   7474   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
   7475                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
   7476                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
   7477                "          aaaa:bbb];");
   7478   verifyFormat("[self param:function( //\n"
   7479                "                parameter)]");
   7480   verifyFormat(
   7481       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
   7482       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
   7483       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
   7484 
   7485   // FIXME: This violates the column limit.
   7486   verifyFormat(
   7487       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
   7488       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
   7489       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
   7490       getLLVMStyleWithColumns(60));
   7491 
   7492   // Variadic parameters.
   7493   verifyFormat(
   7494       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
   7495   verifyFormat(
   7496       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
   7497       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
   7498       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
   7499   verifyFormat("[self // break\n"
   7500                "      a:a\n"
   7501                "    aaa:aaa];");
   7502   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
   7503                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
   7504 }
   7505 
   7506 TEST_F(FormatTest, ObjCAt) {
   7507   verifyFormat("@autoreleasepool");
   7508   verifyFormat("@catch");
   7509   verifyFormat("@class");
   7510   verifyFormat("@compatibility_alias");
   7511   verifyFormat("@defs");
   7512   verifyFormat("@dynamic");
   7513   verifyFormat("@encode");
   7514   verifyFormat("@end");
   7515   verifyFormat("@finally");
   7516   verifyFormat("@implementation");
   7517   verifyFormat("@import");
   7518   verifyFormat("@interface");
   7519   verifyFormat("@optional");
   7520   verifyFormat("@package");
   7521   verifyFormat("@private");
   7522   verifyFormat("@property");
   7523   verifyFormat("@protected");
   7524   verifyFormat("@protocol");
   7525   verifyFormat("@public");
   7526   verifyFormat("@required");
   7527   verifyFormat("@selector");
   7528   verifyFormat("@synchronized");
   7529   verifyFormat("@synthesize");
   7530   verifyFormat("@throw");
   7531   verifyFormat("@try");
   7532 
   7533   EXPECT_EQ("@interface", format("@ interface"));
   7534 
   7535   // The precise formatting of this doesn't matter, nobody writes code like
   7536   // this.
   7537   verifyFormat("@ /*foo*/ interface");
   7538 }
   7539 
   7540 TEST_F(FormatTest, ObjCSnippets) {
   7541   verifyFormat("@autoreleasepool {\n"
   7542                "  foo();\n"
   7543                "}");
   7544   verifyFormat("@class Foo, Bar;");
   7545   verifyFormat("@compatibility_alias AliasName ExistingClass;");
   7546   verifyFormat("@dynamic textColor;");
   7547   verifyFormat("char *buf1 = @encode(int *);");
   7548   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
   7549   verifyFormat("char *buf1 = @encode(int **);");
   7550   verifyFormat("Protocol *proto = @protocol(p1);");
   7551   verifyFormat("SEL s = @selector(foo:);");
   7552   verifyFormat("@synchronized(self) {\n"
   7553                "  f();\n"
   7554                "}");
   7555 
   7556   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
   7557   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
   7558 
   7559   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
   7560   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
   7561   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
   7562   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
   7563                getMozillaStyle());
   7564   verifyFormat("@property BOOL editable;", getMozillaStyle());
   7565   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
   7566                getWebKitStyle());
   7567   verifyFormat("@property BOOL editable;", getWebKitStyle());
   7568 
   7569   verifyFormat("@import foo.bar;\n"
   7570                "@import baz;");
   7571 }
   7572 
   7573 TEST_F(FormatTest, ObjCForIn) {
   7574   verifyFormat("- (void)test {\n"
   7575                "  for (NSString *n in arrayOfStrings) {\n"
   7576                "    foo(n);\n"
   7577                "  }\n"
   7578                "}");
   7579   verifyFormat("- (void)test {\n"
   7580                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
   7581                "    foo(n);\n"
   7582                "  }\n"
   7583                "}");
   7584 }
   7585 
   7586 TEST_F(FormatTest, ObjCLiterals) {
   7587   verifyFormat("@\"String\"");
   7588   verifyFormat("@1");
   7589   verifyFormat("@+4.8");
   7590   verifyFormat("@-4");
   7591   verifyFormat("@1LL");
   7592   verifyFormat("@.5");
   7593   verifyFormat("@'c'");
   7594   verifyFormat("@true");
   7595 
   7596   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
   7597   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
   7598   verifyFormat("NSNumber *favoriteColor = @(Green);");
   7599   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
   7600 
   7601   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
   7602 }
   7603 
   7604 TEST_F(FormatTest, ObjCDictLiterals) {
   7605   verifyFormat("@{");
   7606   verifyFormat("@{}");
   7607   verifyFormat("@{@\"one\" : @1}");
   7608   verifyFormat("return @{@\"one\" : @1;");
   7609   verifyFormat("@{@\"one\" : @1}");
   7610 
   7611   verifyFormat("@{@\"one\" : @{@2 : @1}}");
   7612   verifyFormat("@{\n"
   7613                "  @\"one\" : @{@2 : @1},\n"
   7614                "}");
   7615 
   7616   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
   7617   verifyIncompleteFormat("[self setDict:@{}");
   7618   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
   7619   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
   7620   verifyFormat(
   7621       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
   7622   verifyFormat(
   7623       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
   7624 
   7625   verifyFormat("NSDictionary *d = @{\n"
   7626                "  @\"nam\" : NSUserNam(),\n"
   7627                "  @\"dte\" : [NSDate date],\n"
   7628                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
   7629                "};");
   7630   verifyFormat(
   7631       "@{\n"
   7632       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
   7633       "regularFont,\n"
   7634       "};");
   7635   verifyGoogleFormat(
   7636       "@{\n"
   7637       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
   7638       "regularFont,\n"
   7639       "};");
   7640   verifyFormat(
   7641       "@{\n"
   7642       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
   7643       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
   7644       "};");
   7645 
   7646   // We should try to be robust in case someone forgets the "@".
   7647   verifyFormat("NSDictionary *d = {\n"
   7648                "  @\"nam\" : NSUserNam(),\n"
   7649                "  @\"dte\" : [NSDate date],\n"
   7650                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
   7651                "};");
   7652   verifyFormat("NSMutableDictionary *dictionary =\n"
   7653                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
   7654                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
   7655                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
   7656                "      cccccccccccccccc : ccccccccccccccc\n"
   7657                "    }];");
   7658 
   7659   // Ensure that casts before the key are kept on the same line as the key.
   7660   verifyFormat(
   7661       "NSDictionary *d = @{\n"
   7662       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
   7663       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
   7664       "};");
   7665 }
   7666 
   7667 TEST_F(FormatTest, ObjCArrayLiterals) {
   7668   verifyIncompleteFormat("@[");
   7669   verifyFormat("@[]");
   7670   verifyFormat(
   7671       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
   7672   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
   7673   verifyFormat("NSArray *array = @[ [foo description] ];");
   7674 
   7675   verifyFormat(
   7676       "NSArray *some_variable = @[\n"
   7677       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
   7678       "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7679       "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7680       "  @\"aaaaaaaaaaaaaaaaa\"\n"
   7681       "];");
   7682   verifyFormat("NSArray *some_variable = @[\n"
   7683                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7684                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7685                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7686                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7687                "];");
   7688   verifyGoogleFormat("NSArray *some_variable = @[\n"
   7689                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7690                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7691                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7692                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
   7693                      "];");
   7694   verifyFormat("NSArray *array = @[\n"
   7695                "  @\"a\",\n"
   7696                "  @\"a\",\n" // Trailing comma -> one per line.
   7697                "];");
   7698 
   7699   // We should try to be robust in case someone forgets the "@".
   7700   verifyFormat("NSArray *some_variable = [\n"
   7701                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7702                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7703                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7704                "  @\"aaaaaaaaaaaaaaaaa\",\n"
   7705                "];");
   7706   verifyFormat(
   7707       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
   7708       "                                             index:(NSUInteger)index\n"
   7709       "                                nonDigitAttributes:\n"
   7710       "                                    (NSDictionary *)noDigitAttributes;");
   7711   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
   7712                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
   7713                "]];");
   7714 }
   7715 
   7716 TEST_F(FormatTest, BreaksStringLiterals) {
   7717   EXPECT_EQ("\"some text \"\n"
   7718             "\"other\";",
   7719             format("\"some text other\";", getLLVMStyleWithColumns(12)));
   7720   EXPECT_EQ("\"some text \"\n"
   7721             "\"other\";",
   7722             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
   7723   EXPECT_EQ(
   7724       "#define A  \\\n"
   7725       "  \"some \"  \\\n"
   7726       "  \"text \"  \\\n"
   7727       "  \"other\";",
   7728       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
   7729   EXPECT_EQ(
   7730       "#define A  \\\n"
   7731       "  \"so \"    \\\n"
   7732       "  \"text \"  \\\n"
   7733       "  \"other\";",
   7734       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
   7735 
   7736   EXPECT_EQ("\"some text\"",
   7737             format("\"some text\"", getLLVMStyleWithColumns(1)));
   7738   EXPECT_EQ("\"some text\"",
   7739             format("\"some text\"", getLLVMStyleWithColumns(11)));
   7740   EXPECT_EQ("\"some \"\n"
   7741             "\"text\"",
   7742             format("\"some text\"", getLLVMStyleWithColumns(10)));
   7743   EXPECT_EQ("\"some \"\n"
   7744             "\"text\"",
   7745             format("\"some text\"", getLLVMStyleWithColumns(7)));
   7746   EXPECT_EQ("\"some\"\n"
   7747             "\" tex\"\n"
   7748             "\"t\"",
   7749             format("\"some text\"", getLLVMStyleWithColumns(6)));
   7750   EXPECT_EQ("\"some\"\n"
   7751             "\" tex\"\n"
   7752             "\" and\"",
   7753             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
   7754   EXPECT_EQ("\"some\"\n"
   7755             "\"/tex\"\n"
   7756             "\"/and\"",
   7757             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
   7758 
   7759   EXPECT_EQ("variable =\n"
   7760             "    \"long string \"\n"
   7761             "    \"literal\";",
   7762             format("variable = \"long string literal\";",
   7763                    getLLVMStyleWithColumns(20)));
   7764 
   7765   EXPECT_EQ("variable = f(\n"
   7766             "    \"long string \"\n"
   7767             "    \"literal\",\n"
   7768             "    short,\n"
   7769             "    loooooooooooooooooooong);",
   7770             format("variable = f(\"long string literal\", short, "
   7771                    "loooooooooooooooooooong);",
   7772                    getLLVMStyleWithColumns(20)));
   7773 
   7774   EXPECT_EQ(
   7775       "f(g(\"long string \"\n"
   7776       "    \"literal\"),\n"
   7777       "  b);",
   7778       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
   7779   EXPECT_EQ("f(g(\"long string \"\n"
   7780             "    \"literal\",\n"
   7781             "    a),\n"
   7782             "  b);",
   7783             format("f(g(\"long string literal\", a), b);",
   7784                    getLLVMStyleWithColumns(20)));
   7785   EXPECT_EQ(
   7786       "f(\"one two\".split(\n"
   7787       "    variable));",
   7788       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
   7789   EXPECT_EQ("f(\"one two three four five six \"\n"
   7790             "  \"seven\".split(\n"
   7791             "      really_looooong_variable));",
   7792             format("f(\"one two three four five six seven\"."
   7793                    "split(really_looooong_variable));",
   7794                    getLLVMStyleWithColumns(33)));
   7795 
   7796   EXPECT_EQ("f(\"some \"\n"
   7797             "  \"text\",\n"
   7798             "  other);",
   7799             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
   7800 
   7801   // Only break as a last resort.
   7802   verifyFormat(
   7803       "aaaaaaaaaaaaaaaaaaaa(\n"
   7804       "    aaaaaaaaaaaaaaaaaaaa,\n"
   7805       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
   7806 
   7807   EXPECT_EQ("\"splitmea\"\n"
   7808             "\"trandomp\"\n"
   7809             "\"oint\"",
   7810             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
   7811 
   7812   EXPECT_EQ("\"split/\"\n"
   7813             "\"pathat/\"\n"
   7814             "\"slashes\"",
   7815             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
   7816 
   7817   EXPECT_EQ("\"split/\"\n"
   7818             "\"pathat/\"\n"
   7819             "\"slashes\"",
   7820             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
   7821   EXPECT_EQ("\"split at \"\n"
   7822             "\"spaces/at/\"\n"
   7823             "\"slashes.at.any$\"\n"
   7824             "\"non-alphanumeric%\"\n"
   7825             "\"1111111111characte\"\n"
   7826             "\"rs\"",
   7827             format("\"split at "
   7828                    "spaces/at/"
   7829                    "slashes.at."
   7830                    "any$non-"
   7831                    "alphanumeric%"
   7832                    "1111111111characte"
   7833                    "rs\"",
   7834                    getLLVMStyleWithColumns(20)));
   7835 
   7836   // Verify that splitting the strings understands
   7837   // Style::AlwaysBreakBeforeMultilineStrings.
   7838   EXPECT_EQ(
   7839       "aaaaaaaaaaaa(\n"
   7840       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
   7841       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
   7842       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
   7843              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
   7844              "aaaaaaaaaaaaaaaaaaaaaa\");",
   7845              getGoogleStyle()));
   7846   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
   7847             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
   7848             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
   7849                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
   7850                    "aaaaaaaaaaaaaaaaaaaaaa\";",
   7851                    getGoogleStyle()));
   7852   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
   7853             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
   7854             format("llvm::outs() << "
   7855                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
   7856                    "aaaaaaaaaaaaaaaaaaa\";"));
   7857   EXPECT_EQ("ffff(\n"
   7858             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
   7859             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
   7860             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
   7861                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
   7862                    getGoogleStyle()));
   7863 
   7864   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
   7865   AlignLeft.AlignEscapedNewlinesLeft = true;
   7866   EXPECT_EQ("#define A \\\n"
   7867             "  \"some \" \\\n"
   7868             "  \"text \" \\\n"
   7869             "  \"other\";",
   7870             format("#define A \"some text other\";", AlignLeft));
   7871 }
   7872 
   7873 TEST_F(FormatTest, FullyRemoveEmptyLines) {
   7874   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
   7875   NoEmptyLines.MaxEmptyLinesToKeep = 0;
   7876   EXPECT_EQ("int i = a(b());",
   7877             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
   7878 }
   7879 
   7880 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
   7881   EXPECT_EQ(
   7882       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
   7883       "(\n"
   7884       "    \"x\t\");",
   7885       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
   7886              "aaaaaaa("
   7887              "\"x\t\");"));
   7888 }
   7889 
   7890 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
   7891   EXPECT_EQ(
   7892       "u8\"utf8 string \"\n"
   7893       "u8\"literal\";",
   7894       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
   7895   EXPECT_EQ(
   7896       "u\"utf16 string \"\n"
   7897       "u\"literal\";",
   7898       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
   7899   EXPECT_EQ(
   7900       "U\"utf32 string \"\n"
   7901       "U\"literal\";",
   7902       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
   7903   EXPECT_EQ("L\"wide string \"\n"
   7904             "L\"literal\";",
   7905             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
   7906   EXPECT_EQ("@\"NSString \"\n"
   7907             "@\"literal\";",
   7908             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
   7909 
   7910   // This input makes clang-format try to split the incomplete unicode escape
   7911   // sequence, which used to lead to a crasher.
   7912   verifyNoCrash(
   7913       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
   7914       getLLVMStyleWithColumns(60));
   7915 }
   7916 
   7917 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
   7918   FormatStyle Style = getGoogleStyleWithColumns(15);
   7919   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
   7920   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
   7921   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
   7922   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
   7923   EXPECT_EQ("u8R\"x(raw literal)x\";",
   7924             format("u8R\"x(raw literal)x\";", Style));
   7925 }
   7926 
   7927 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
   7928   FormatStyle Style = getLLVMStyleWithColumns(20);
   7929   EXPECT_EQ(
   7930       "_T(\"aaaaaaaaaaaaaa\")\n"
   7931       "_T(\"aaaaaaaaaaaaaa\")\n"
   7932       "_T(\"aaaaaaaaaaaa\")",
   7933       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
   7934   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
   7935             "     _T(\"aaaaaa\"),\n"
   7936             "  z);",
   7937             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
   7938 
   7939   // FIXME: Handle embedded spaces in one iteration.
   7940   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
   7941   //            "_T(\"aaaaaaaaaaaaa\")\n"
   7942   //            "_T(\"aaaaaaaaaaaaa\")\n"
   7943   //            "_T(\"a\")",
   7944   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
   7945   //                   getLLVMStyleWithColumns(20)));
   7946   EXPECT_EQ(
   7947       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
   7948       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
   7949   EXPECT_EQ("f(\n"
   7950             "#if !TEST\n"
   7951             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
   7952             "#endif\n"
   7953             "    );",
   7954             format("f(\n"
   7955                    "#if !TEST\n"
   7956                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
   7957                    "#endif\n"
   7958                    ");"));
   7959   EXPECT_EQ("f(\n"
   7960             "\n"
   7961             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
   7962             format("f(\n"
   7963                    "\n"
   7964                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
   7965 }
   7966 
   7967 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
   7968   EXPECT_EQ(
   7969       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   7970       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   7971       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
   7972       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   7973              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
   7974              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
   7975 }
   7976 
   7977 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
   7978   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
   7979             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
   7980   EXPECT_EQ("fffffffffff(g(R\"x(\n"
   7981             "multiline raw string literal xxxxxxxxxxxxxx\n"
   7982             ")x\",\n"
   7983             "              a),\n"
   7984             "            b);",
   7985             format("fffffffffff(g(R\"x(\n"
   7986                    "multiline raw string literal xxxxxxxxxxxxxx\n"
   7987                    ")x\", a), b);",
   7988                    getGoogleStyleWithColumns(20)));
   7989   EXPECT_EQ("fffffffffff(\n"
   7990             "    g(R\"x(qqq\n"
   7991             "multiline raw string literal xxxxxxxxxxxxxx\n"
   7992             ")x\",\n"
   7993             "      a),\n"
   7994             "    b);",
   7995             format("fffffffffff(g(R\"x(qqq\n"
   7996                    "multiline raw string literal xxxxxxxxxxxxxx\n"
   7997                    ")x\", a), b);",
   7998                    getGoogleStyleWithColumns(20)));
   7999 
   8000   EXPECT_EQ("fffffffffff(R\"x(\n"
   8001             "multiline raw string literal xxxxxxxxxxxxxx\n"
   8002             ")x\");",
   8003             format("fffffffffff(R\"x(\n"
   8004                    "multiline raw string literal xxxxxxxxxxxxxx\n"
   8005                    ")x\");",
   8006                    getGoogleStyleWithColumns(20)));
   8007   EXPECT_EQ("fffffffffff(R\"x(\n"
   8008             "multiline raw string literal xxxxxxxxxxxxxx\n"
   8009             ")x\" + bbbbbb);",
   8010             format("fffffffffff(R\"x(\n"
   8011                    "multiline raw string literal xxxxxxxxxxxxxx\n"
   8012                    ")x\" +   bbbbbb);",
   8013                    getGoogleStyleWithColumns(20)));
   8014   EXPECT_EQ("fffffffffff(\n"
   8015             "    R\"x(\n"
   8016             "multiline raw string literal xxxxxxxxxxxxxx\n"
   8017             ")x\" +\n"
   8018             "    bbbbbb);",
   8019             format("fffffffffff(\n"
   8020                    " R\"x(\n"
   8021                    "multiline raw string literal xxxxxxxxxxxxxx\n"
   8022                    ")x\" + bbbbbb);",
   8023                    getGoogleStyleWithColumns(20)));
   8024 }
   8025 
   8026 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
   8027   verifyFormat("string a = \"unterminated;");
   8028   EXPECT_EQ("function(\"unterminated,\n"
   8029             "         OtherParameter);",
   8030             format("function(  \"unterminated,\n"
   8031                    "    OtherParameter);"));
   8032 }
   8033 
   8034 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
   8035   FormatStyle Style = getLLVMStyle();
   8036   Style.Standard = FormatStyle::LS_Cpp03;
   8037   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
   8038             format("#define x(_a) printf(\"foo\"_a);", Style));
   8039 }
   8040 
   8041 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
   8042 
   8043 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
   8044   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
   8045             "             \"ddeeefff\");",
   8046             format("someFunction(\"aaabbbcccdddeeefff\");",
   8047                    getLLVMStyleWithColumns(25)));
   8048   EXPECT_EQ("someFunction1234567890(\n"
   8049             "    \"aaabbbcccdddeeefff\");",
   8050             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
   8051                    getLLVMStyleWithColumns(26)));
   8052   EXPECT_EQ("someFunction1234567890(\n"
   8053             "    \"aaabbbcccdddeeeff\"\n"
   8054             "    \"f\");",
   8055             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
   8056                    getLLVMStyleWithColumns(25)));
   8057   EXPECT_EQ("someFunction1234567890(\n"
   8058             "    \"aaabbbcccdddeeeff\"\n"
   8059             "    \"f\");",
   8060             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
   8061                    getLLVMStyleWithColumns(24)));
   8062   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
   8063             "             \"ddde \"\n"
   8064             "             \"efff\");",
   8065             format("someFunction(\"aaabbbcc ddde efff\");",
   8066                    getLLVMStyleWithColumns(25)));
   8067   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
   8068             "             \"ddeeefff\");",
   8069             format("someFunction(\"aaabbbccc ddeeefff\");",
   8070                    getLLVMStyleWithColumns(25)));
   8071   EXPECT_EQ("someFunction1234567890(\n"
   8072             "    \"aaabb \"\n"
   8073             "    \"cccdddeeefff\");",
   8074             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
   8075                    getLLVMStyleWithColumns(25)));
   8076   EXPECT_EQ("#define A          \\\n"
   8077             "  string s =       \\\n"
   8078             "      \"123456789\"  \\\n"
   8079             "      \"0\";         \\\n"
   8080             "  int i;",
   8081             format("#define A string s = \"1234567890\"; int i;",
   8082                    getLLVMStyleWithColumns(20)));
   8083   // FIXME: Put additional penalties on breaking at non-whitespace locations.
   8084   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
   8085             "             \"dddeeeff\"\n"
   8086             "             \"f\");",
   8087             format("someFunction(\"aaabbbcc dddeeefff\");",
   8088                    getLLVMStyleWithColumns(25)));
   8089 }
   8090 
   8091 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
   8092   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
   8093   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
   8094   EXPECT_EQ("\"test\"\n"
   8095             "\"\\n\"",
   8096             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
   8097   EXPECT_EQ("\"tes\\\\\"\n"
   8098             "\"n\"",
   8099             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
   8100   EXPECT_EQ("\"\\\\\\\\\"\n"
   8101             "\"\\n\"",
   8102             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
   8103   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
   8104   EXPECT_EQ("\"\\uff01\"\n"
   8105             "\"test\"",
   8106             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
   8107   EXPECT_EQ("\"\\Uff01ff02\"",
   8108             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
   8109   EXPECT_EQ("\"\\x000000000001\"\n"
   8110             "\"next\"",
   8111             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
   8112   EXPECT_EQ("\"\\x000000000001next\"",
   8113             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
   8114   EXPECT_EQ("\"\\x000000000001\"",
   8115             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
   8116   EXPECT_EQ("\"test\"\n"
   8117             "\"\\000000\"\n"
   8118             "\"000001\"",
   8119             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
   8120   EXPECT_EQ("\"test\\000\"\n"
   8121             "\"00000000\"\n"
   8122             "\"1\"",
   8123             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
   8124 }
   8125 
   8126 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
   8127   verifyFormat("void f() {\n"
   8128                "  return g() {}\n"
   8129                "  void h() {}");
   8130   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
   8131                "g();\n"
   8132                "}");
   8133 }
   8134 
   8135 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
   8136   verifyFormat(
   8137       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
   8138 }
   8139 
   8140 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
   8141   verifyFormat("class X {\n"
   8142                "  void f() {\n"
   8143                "  }\n"
   8144                "};",
   8145                getLLVMStyleWithColumns(12));
   8146 }
   8147 
   8148 TEST_F(FormatTest, ConfigurableIndentWidth) {
   8149   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
   8150   EightIndent.IndentWidth = 8;
   8151   EightIndent.ContinuationIndentWidth = 8;
   8152   verifyFormat("void f() {\n"
   8153                "        someFunction();\n"
   8154                "        if (true) {\n"
   8155                "                f();\n"
   8156                "        }\n"
   8157                "}",
   8158                EightIndent);
   8159   verifyFormat("class X {\n"
   8160                "        void f() {\n"
   8161                "        }\n"
   8162                "};",
   8163                EightIndent);
   8164   verifyFormat("int x[] = {\n"
   8165                "        call(),\n"
   8166                "        call()};",
   8167                EightIndent);
   8168 }
   8169 
   8170 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
   8171   verifyFormat("double\n"
   8172                "f();",
   8173                getLLVMStyleWithColumns(8));
   8174 }
   8175 
   8176 TEST_F(FormatTest, ConfigurableUseOfTab) {
   8177   FormatStyle Tab = getLLVMStyleWithColumns(42);
   8178   Tab.IndentWidth = 8;
   8179   Tab.UseTab = FormatStyle::UT_Always;
   8180   Tab.AlignEscapedNewlinesLeft = true;
   8181 
   8182   EXPECT_EQ("if (aaaaaaaa && // q\n"
   8183             "    bb)\t\t// w\n"
   8184             "\t;",
   8185             format("if (aaaaaaaa &&// q\n"
   8186                    "bb)// w\n"
   8187                    ";",
   8188                    Tab));
   8189   EXPECT_EQ("if (aaa && bbb) // w\n"
   8190             "\t;",
   8191             format("if(aaa&&bbb)// w\n"
   8192                    ";",
   8193                    Tab));
   8194 
   8195   verifyFormat("class X {\n"
   8196                "\tvoid f() {\n"
   8197                "\t\tsomeFunction(parameter1,\n"
   8198                "\t\t\t     parameter2);\n"
   8199                "\t}\n"
   8200                "};",
   8201                Tab);
   8202   verifyFormat("#define A                        \\\n"
   8203                "\tvoid f() {               \\\n"
   8204                "\t\tsomeFunction(    \\\n"
   8205                "\t\t    parameter1,  \\\n"
   8206                "\t\t    parameter2); \\\n"
   8207                "\t}",
   8208                Tab);
   8209 
   8210   Tab.TabWidth = 4;
   8211   Tab.IndentWidth = 8;
   8212   verifyFormat("class TabWidth4Indent8 {\n"
   8213                "\t\tvoid f() {\n"
   8214                "\t\t\t\tsomeFunction(parameter1,\n"
   8215                "\t\t\t\t\t\t\t parameter2);\n"
   8216                "\t\t}\n"
   8217                "};",
   8218                Tab);
   8219 
   8220   Tab.TabWidth = 4;
   8221   Tab.IndentWidth = 4;
   8222   verifyFormat("class TabWidth4Indent4 {\n"
   8223                "\tvoid f() {\n"
   8224                "\t\tsomeFunction(parameter1,\n"
   8225                "\t\t\t\t\t parameter2);\n"
   8226                "\t}\n"
   8227                "};",
   8228                Tab);
   8229 
   8230   Tab.TabWidth = 8;
   8231   Tab.IndentWidth = 4;
   8232   verifyFormat("class TabWidth8Indent4 {\n"
   8233                "    void f() {\n"
   8234                "\tsomeFunction(parameter1,\n"
   8235                "\t\t     parameter2);\n"
   8236                "    }\n"
   8237                "};",
   8238                Tab);
   8239 
   8240   Tab.TabWidth = 8;
   8241   Tab.IndentWidth = 8;
   8242   EXPECT_EQ("/*\n"
   8243             "\t      a\t\tcomment\n"
   8244             "\t      in multiple lines\n"
   8245             "       */",
   8246             format("   /*\t \t \n"
   8247                    " \t \t a\t\tcomment\t \t\n"
   8248                    " \t \t in multiple lines\t\n"
   8249                    " \t  */",
   8250                    Tab));
   8251 
   8252   Tab.UseTab = FormatStyle::UT_ForIndentation;
   8253   verifyFormat("{\n"
   8254                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
   8255                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
   8256                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
   8257                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
   8258                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
   8259                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
   8260                "};",
   8261                Tab);
   8262   verifyFormat("enum A {\n"
   8263                "\ta1, // Force multiple lines\n"
   8264                "\ta2,\n"
   8265                "\ta3\n"
   8266                "};",
   8267                Tab);
   8268   EXPECT_EQ("if (aaaaaaaa && // q\n"
   8269             "    bb)         // w\n"
   8270             "\t;",
   8271             format("if (aaaaaaaa &&// q\n"
   8272                    "bb)// w\n"
   8273                    ";",
   8274                    Tab));
   8275   verifyFormat("class X {\n"
   8276                "\tvoid f() {\n"
   8277                "\t\tsomeFunction(parameter1,\n"
   8278                "\t\t             parameter2);\n"
   8279                "\t}\n"
   8280                "};",
   8281                Tab);
   8282   verifyFormat("{\n"
   8283                "\tQ(\n"
   8284                "\t    {\n"
   8285                "\t\t    int a;\n"
   8286                "\t\t    someFunction(aaaaaaaa,\n"
   8287                "\t\t                 bbbbbbb);\n"
   8288                "\t    },\n"
   8289                "\t    p);\n"
   8290                "}",
   8291                Tab);
   8292   EXPECT_EQ("{\n"
   8293             "\t/* aaaa\n"
   8294             "\t   bbbb */\n"
   8295             "}",
   8296             format("{\n"
   8297                    "/* aaaa\n"
   8298                    "   bbbb */\n"
   8299                    "}",
   8300                    Tab));
   8301   EXPECT_EQ("{\n"
   8302             "\t/*\n"
   8303             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   8304             "\t  bbbbbbbbbbbbb\n"
   8305             "\t*/\n"
   8306             "}",
   8307             format("{\n"
   8308                    "/*\n"
   8309                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
   8310                    "*/\n"
   8311                    "}",
   8312                    Tab));
   8313   EXPECT_EQ("{\n"
   8314             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   8315             "\t// bbbbbbbbbbbbb\n"
   8316             "}",
   8317             format("{\n"
   8318                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
   8319                    "}",
   8320                    Tab));
   8321   EXPECT_EQ("{\n"
   8322             "\t/*\n"
   8323             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
   8324             "\t  bbbbbbbbbbbbb\n"
   8325             "\t*/\n"
   8326             "}",
   8327             format("{\n"
   8328                    "\t/*\n"
   8329                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
   8330                    "\t*/\n"
   8331                    "}",
   8332                    Tab));
   8333   EXPECT_EQ("{\n"
   8334             "\t/*\n"
   8335             "\n"
   8336             "\t*/\n"
   8337             "}",
   8338             format("{\n"
   8339                    "\t/*\n"
   8340                    "\n"
   8341                    "\t*/\n"
   8342                    "}",
   8343                    Tab));
   8344   EXPECT_EQ("{\n"
   8345             "\t/*\n"
   8346             " asdf\n"
   8347             "\t*/\n"
   8348             "}",
   8349             format("{\n"
   8350                    "\t/*\n"
   8351                    " asdf\n"
   8352                    "\t*/\n"
   8353                    "}",
   8354                    Tab));
   8355 
   8356   Tab.UseTab = FormatStyle::UT_Never;
   8357   EXPECT_EQ("/*\n"
   8358             "              a\t\tcomment\n"
   8359             "              in multiple lines\n"
   8360             "       */",
   8361             format("   /*\t \t \n"
   8362                    " \t \t a\t\tcomment\t \t\n"
   8363                    " \t \t in multiple lines\t\n"
   8364                    " \t  */",
   8365                    Tab));
   8366   EXPECT_EQ("/* some\n"
   8367             "   comment */",
   8368             format(" \t \t /* some\n"
   8369                    " \t \t    comment */",
   8370                    Tab));
   8371   EXPECT_EQ("int a; /* some\n"
   8372             "   comment */",
   8373             format(" \t \t int a; /* some\n"
   8374                    " \t \t    comment */",
   8375                    Tab));
   8376 
   8377   EXPECT_EQ("int a; /* some\n"
   8378             "comment */",
   8379             format(" \t \t int\ta; /* some\n"
   8380                    " \t \t    comment */",
   8381                    Tab));
   8382   EXPECT_EQ("f(\"\t\t\"); /* some\n"
   8383             "    comment */",
   8384             format(" \t \t f(\"\t\t\"); /* some\n"
   8385                    " \t \t    comment */",
   8386                    Tab));
   8387   EXPECT_EQ("{\n"
   8388             "  /*\n"
   8389             "   * Comment\n"
   8390             "   */\n"
   8391             "  int i;\n"
   8392             "}",
   8393             format("{\n"
   8394                    "\t/*\n"
   8395                    "\t * Comment\n"
   8396                    "\t */\n"
   8397                    "\t int i;\n"
   8398                    "}"));
   8399 }
   8400 
   8401 TEST_F(FormatTest, CalculatesOriginalColumn) {
   8402   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
   8403             "q\"; /* some\n"
   8404             "       comment */",
   8405             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
   8406                    "q\"; /* some\n"
   8407                    "       comment */",
   8408                    getLLVMStyle()));
   8409   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
   8410             "/* some\n"
   8411             "   comment */",
   8412             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
   8413                    " /* some\n"
   8414                    "    comment */",
   8415                    getLLVMStyle()));
   8416   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
   8417             "qqq\n"
   8418             "/* some\n"
   8419             "   comment */",
   8420             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
   8421                    "qqq\n"
   8422                    " /* some\n"
   8423                    "    comment */",
   8424                    getLLVMStyle()));
   8425   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
   8426             "wwww; /* some\n"
   8427             "         comment */",
   8428             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
   8429                    "wwww; /* some\n"
   8430                    "         comment */",
   8431                    getLLVMStyle()));
   8432 }
   8433 
   8434 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   8435   FormatStyle NoSpace = getLLVMStyle();
   8436   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
   8437 
   8438   verifyFormat("while(true)\n"
   8439                "  continue;",
   8440                NoSpace);
   8441   verifyFormat("for(;;)\n"
   8442                "  continue;",
   8443                NoSpace);
   8444   verifyFormat("if(true)\n"
   8445                "  f();\n"
   8446                "else if(true)\n"
   8447                "  f();",
   8448                NoSpace);
   8449   verifyFormat("do {\n"
   8450                "  do_something();\n"
   8451                "} while(something());",
   8452                NoSpace);
   8453   verifyFormat("switch(x) {\n"
   8454                "default:\n"
   8455                "  break;\n"
   8456                "}",
   8457                NoSpace);
   8458   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
   8459   verifyFormat("size_t x = sizeof(x);", NoSpace);
   8460   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
   8461   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
   8462   verifyFormat("alignas(128) char a[128];", NoSpace);
   8463   verifyFormat("size_t x = alignof(MyType);", NoSpace);
   8464   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
   8465   verifyFormat("int f() throw(Deprecated);", NoSpace);
   8466   verifyFormat("typedef void (*cb)(int);", NoSpace);
   8467   verifyFormat("T A::operator()();", NoSpace);
   8468   verifyFormat("X A::operator++(T);", NoSpace);
   8469 
   8470   FormatStyle Space = getLLVMStyle();
   8471   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
   8472 
   8473   verifyFormat("int f ();", Space);
   8474   verifyFormat("void f (int a, T b) {\n"
   8475                "  while (true)\n"
   8476                "    continue;\n"
   8477                "}",
   8478                Space);
   8479   verifyFormat("if (true)\n"
   8480                "  f ();\n"
   8481                "else if (true)\n"
   8482                "  f ();",
   8483                Space);
   8484   verifyFormat("do {\n"
   8485                "  do_something ();\n"
   8486                "} while (something ());",
   8487                Space);
   8488   verifyFormat("switch (x) {\n"
   8489                "default:\n"
   8490                "  break;\n"
   8491                "}",
   8492                Space);
   8493   verifyFormat("A::A () : a (1) {}", Space);
   8494   verifyFormat("void f () __attribute__ ((asdf));", Space);
   8495   verifyFormat("*(&a + 1);\n"
   8496                "&((&a)[1]);\n"
   8497                "a[(b + c) * d];\n"
   8498                "(((a + 1) * 2) + 3) * 4;",
   8499                Space);
   8500   verifyFormat("#define A(x) x", Space);
   8501   verifyFormat("#define A (x) x", Space);
   8502   verifyFormat("#if defined(x)\n"
   8503                "#endif",
   8504                Space);
   8505   verifyFormat("auto i = std::make_unique<int> (5);", Space);
   8506   verifyFormat("size_t x = sizeof (x);", Space);
   8507   verifyFormat("auto f (int x) -> decltype (x);", Space);
   8508   verifyFormat("int f (T x) noexcept (x.create ());", Space);
   8509   verifyFormat("alignas (128) char a[128];", Space);
   8510   verifyFormat("size_t x = alignof (MyType);", Space);
   8511   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
   8512   verifyFormat("int f () throw (Deprecated);", Space);
   8513   verifyFormat("typedef void (*cb) (int);", Space);
   8514   verifyFormat("T A::operator() ();", Space);
   8515   verifyFormat("X A::operator++ (T);", Space);
   8516 }
   8517 
   8518 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
   8519   FormatStyle Spaces = getLLVMStyle();
   8520 
   8521   Spaces.SpacesInParentheses = true;
   8522   verifyFormat("call( x, y, z );", Spaces);
   8523   verifyFormat("call();", Spaces);
   8524   verifyFormat("std::function<void( int, int )> callback;", Spaces);
   8525   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
   8526                Spaces);
   8527   verifyFormat("while ( (bool)1 )\n"
   8528                "  continue;",
   8529                Spaces);
   8530   verifyFormat("for ( ;; )\n"
   8531                "  continue;",
   8532                Spaces);
   8533   verifyFormat("if ( true )\n"
   8534                "  f();\n"
   8535                "else if ( true )\n"
   8536                "  f();",
   8537                Spaces);
   8538   verifyFormat("do {\n"
   8539                "  do_something( (int)i );\n"
   8540                "} while ( something() );",
   8541                Spaces);
   8542   verifyFormat("switch ( x ) {\n"
   8543                "default:\n"
   8544                "  break;\n"
   8545                "}",
   8546                Spaces);
   8547 
   8548   Spaces.SpacesInParentheses = false;
   8549   Spaces.SpacesInCStyleCastParentheses = true;
   8550   verifyFormat("Type *A = ( Type * )P;", Spaces);
   8551   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
   8552   verifyFormat("x = ( int32 )y;", Spaces);
   8553   verifyFormat("int a = ( int )(2.0f);", Spaces);
   8554   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
   8555   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
   8556   verifyFormat("#define x (( int )-1)", Spaces);
   8557 
   8558   // Run the first set of tests again with:
   8559   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
   8560   Spaces.SpacesInCStyleCastParentheses = true;
   8561   verifyFormat("call(x, y, z);", Spaces);
   8562   verifyFormat("call( );", Spaces);
   8563   verifyFormat("std::function<void(int, int)> callback;", Spaces);
   8564   verifyFormat("while (( bool )1)\n"
   8565                "  continue;",
   8566                Spaces);
   8567   verifyFormat("for (;;)\n"
   8568                "  continue;",
   8569                Spaces);
   8570   verifyFormat("if (true)\n"
   8571                "  f( );\n"
   8572                "else if (true)\n"
   8573                "  f( );",
   8574                Spaces);
   8575   verifyFormat("do {\n"
   8576                "  do_something(( int )i);\n"
   8577                "} while (something( ));",
   8578                Spaces);
   8579   verifyFormat("switch (x) {\n"
   8580                "default:\n"
   8581                "  break;\n"
   8582                "}",
   8583                Spaces);
   8584 
   8585   // Run the first set of tests again with:
   8586   Spaces.SpaceAfterCStyleCast = true;
   8587   verifyFormat("call(x, y, z);", Spaces);
   8588   verifyFormat("call( );", Spaces);
   8589   verifyFormat("std::function<void(int, int)> callback;", Spaces);
   8590   verifyFormat("while (( bool ) 1)\n"
   8591                "  continue;",
   8592                Spaces);
   8593   verifyFormat("for (;;)\n"
   8594                "  continue;",
   8595                Spaces);
   8596   verifyFormat("if (true)\n"
   8597                "  f( );\n"
   8598                "else if (true)\n"
   8599                "  f( );",
   8600                Spaces);
   8601   verifyFormat("do {\n"
   8602                "  do_something(( int ) i);\n"
   8603                "} while (something( ));",
   8604                Spaces);
   8605   verifyFormat("switch (x) {\n"
   8606                "default:\n"
   8607                "  break;\n"
   8608                "}",
   8609                Spaces);
   8610 
   8611   // Run subset of tests again with:
   8612   Spaces.SpacesInCStyleCastParentheses = false;
   8613   Spaces.SpaceAfterCStyleCast = true;
   8614   verifyFormat("while ((bool) 1)\n"
   8615                "  continue;",
   8616                Spaces);
   8617   verifyFormat("do {\n"
   8618                "  do_something((int) i);\n"
   8619                "} while (something( ));",
   8620                Spaces);
   8621 }
   8622 
   8623 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
   8624   verifyFormat("int a[5];");
   8625   verifyFormat("a[3] += 42;");
   8626 
   8627   FormatStyle Spaces = getLLVMStyle();
   8628   Spaces.SpacesInSquareBrackets = true;
   8629   // Lambdas unchanged.
   8630   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
   8631   verifyFormat("return [i, args...] {};", Spaces);
   8632 
   8633   // Not lambdas.
   8634   verifyFormat("int a[ 5 ];", Spaces);
   8635   verifyFormat("a[ 3 ] += 42;", Spaces);
   8636   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
   8637   verifyFormat("double &operator[](int i) { return 0; }\n"
   8638                "int i;",
   8639                Spaces);
   8640   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
   8641   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
   8642   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
   8643 }
   8644 
   8645 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
   8646   verifyFormat("int a = 5;");
   8647   verifyFormat("a += 42;");
   8648   verifyFormat("a or_eq 8;");
   8649 
   8650   FormatStyle Spaces = getLLVMStyle();
   8651   Spaces.SpaceBeforeAssignmentOperators = false;
   8652   verifyFormat("int a= 5;", Spaces);
   8653   verifyFormat("a+= 42;", Spaces);
   8654   verifyFormat("a or_eq 8;", Spaces);
   8655 }
   8656 
   8657 TEST_F(FormatTest, AlignConsecutiveAssignments) {
   8658   FormatStyle Alignment = getLLVMStyle();
   8659   Alignment.AlignConsecutiveAssignments = false;
   8660   verifyFormat("int a = 5;\n"
   8661                "int oneTwoThree = 123;",
   8662                Alignment);
   8663   verifyFormat("int a = 5;\n"
   8664                "int oneTwoThree = 123;",
   8665                Alignment);
   8666 
   8667   Alignment.AlignConsecutiveAssignments = true;
   8668   verifyFormat("int a           = 5;\n"
   8669                "int oneTwoThree = 123;",
   8670                Alignment);
   8671   verifyFormat("int a           = method();\n"
   8672                "int oneTwoThree = 133;",
   8673                Alignment);
   8674   verifyFormat("a &= 5;\n"
   8675                "bcd *= 5;\n"
   8676                "ghtyf += 5;\n"
   8677                "dvfvdb -= 5;\n"
   8678                "a /= 5;\n"
   8679                "vdsvsv %= 5;\n"
   8680                "sfdbddfbdfbb ^= 5;\n"
   8681                "dvsdsv |= 5;\n"
   8682                "int dsvvdvsdvvv = 123;",
   8683                Alignment);
   8684   verifyFormat("int i = 1, j = 10;\n"
   8685                "something = 2000;",
   8686                Alignment);
   8687   verifyFormat("something = 2000;\n"
   8688                "int i = 1, j = 10;\n",
   8689                Alignment);
   8690   verifyFormat("something = 2000;\n"
   8691                "another   = 911;\n"
   8692                "int i = 1, j = 10;\n"
   8693                "oneMore = 1;\n"
   8694                "i       = 2;",
   8695                Alignment);
   8696   verifyFormat("int a   = 5;\n"
   8697                "int one = 1;\n"
   8698                "method();\n"
   8699                "int oneTwoThree = 123;\n"
   8700                "int oneTwo      = 12;",
   8701                Alignment);
   8702   verifyFormat("int oneTwoThree = 123;\n"
   8703                "int oneTwo      = 12;\n"
   8704                "method();\n",
   8705                Alignment);
   8706   verifyFormat("int oneTwoThree = 123; // comment\n"
   8707                "int oneTwo      = 12;  // comment",
   8708                Alignment);
   8709   EXPECT_EQ("int a = 5;\n"
   8710             "\n"
   8711             "int oneTwoThree = 123;",
   8712             format("int a       = 5;\n"
   8713                    "\n"
   8714                    "int oneTwoThree= 123;",
   8715                    Alignment));
   8716   EXPECT_EQ("int a   = 5;\n"
   8717             "int one = 1;\n"
   8718             "\n"
   8719             "int oneTwoThree = 123;",
   8720             format("int a = 5;\n"
   8721                    "int one = 1;\n"
   8722                    "\n"
   8723                    "int oneTwoThree = 123;",
   8724                    Alignment));
   8725   EXPECT_EQ("int a   = 5;\n"
   8726             "int one = 1;\n"
   8727             "\n"
   8728             "int oneTwoThree = 123;\n"
   8729             "int oneTwo      = 12;",
   8730             format("int a = 5;\n"
   8731                    "int one = 1;\n"
   8732                    "\n"
   8733                    "int oneTwoThree = 123;\n"
   8734                    "int oneTwo = 12;",
   8735                    Alignment));
   8736   Alignment.AlignEscapedNewlinesLeft = true;
   8737   verifyFormat("#define A               \\\n"
   8738                "  int aaaa       = 12;  \\\n"
   8739                "  int b          = 23;  \\\n"
   8740                "  int ccc        = 234; \\\n"
   8741                "  int dddddddddd = 2345;",
   8742                Alignment);
   8743   Alignment.AlignEscapedNewlinesLeft = false;
   8744   verifyFormat("#define A                                                      "
   8745                "                \\\n"
   8746                "  int aaaa       = 12;                                         "
   8747                "                \\\n"
   8748                "  int b          = 23;                                         "
   8749                "                \\\n"
   8750                "  int ccc        = 234;                                        "
   8751                "                \\\n"
   8752                "  int dddddddddd = 2345;",
   8753                Alignment);
   8754   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
   8755                "k = 4, int l = 5,\n"
   8756                "                  int m = 6) {\n"
   8757                "  int j      = 10;\n"
   8758                "  otherThing = 1;\n"
   8759                "}",
   8760                Alignment);
   8761   verifyFormat("void SomeFunction(int parameter = 0) {\n"
   8762                "  int i   = 1;\n"
   8763                "  int j   = 2;\n"
   8764                "  int big = 10000;\n"
   8765                "}",
   8766                Alignment);
   8767   verifyFormat("class C {\n"
   8768                "public:\n"
   8769                "  int i            = 1;\n"
   8770                "  virtual void f() = 0;\n"
   8771                "};",
   8772                Alignment);
   8773   verifyFormat("int i = 1;\n"
   8774                "if (SomeType t = getSomething()) {\n"
   8775                "}\n"
   8776                "int j   = 2;\n"
   8777                "int big = 10000;",
   8778                Alignment);
   8779   verifyFormat("int j = 7;\n"
   8780                "for (int k = 0; k < N; ++k) {\n"
   8781                "}\n"
   8782                "int j   = 2;\n"
   8783                "int big = 10000;\n"
   8784                "}",
   8785                Alignment);
   8786   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
   8787   verifyFormat("int i = 1;\n"
   8788                "LooooooooooongType loooooooooooooooooooooongVariable\n"
   8789                "    = someLooooooooooooooooongFunction();\n"
   8790                "int j = 2;",
   8791                Alignment);
   8792   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
   8793   verifyFormat("int i = 1;\n"
   8794                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
   8795                "    someLooooooooooooooooongFunction();\n"
   8796                "int j = 2;",
   8797                Alignment);
   8798 
   8799   verifyFormat("auto lambda = []() {\n"
   8800                "  auto i = 0;\n"
   8801                "  return 0;\n"
   8802                "};\n"
   8803                "int i  = 0;\n"
   8804                "auto v = type{\n"
   8805                "    i = 1,   //\n"
   8806                "    (i = 2), //\n"
   8807                "    i = 3    //\n"
   8808                "};",
   8809                Alignment);
   8810 
   8811   // FIXME: Should align all three assignments
   8812   verifyFormat(
   8813       "int i      = 1;\n"
   8814       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
   8815       "                          loooooooooooooooooooooongParameterB);\n"
   8816       "int j = 2;",
   8817       Alignment);
   8818 
   8819   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
   8820                "          typename B   = very_long_type_name_1,\n"
   8821                "          typename T_2 = very_long_type_name_2>\n"
   8822                "auto foo() {}\n",
   8823                Alignment);
   8824   verifyFormat("int a, b = 1;\n"
   8825                "int c  = 2;\n"
   8826                "int dd = 3;\n",
   8827                Alignment);
   8828   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
   8829                "float b[1][] = {{3.f}};\n",
   8830                Alignment);
   8831 }
   8832 
   8833 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
   8834   FormatStyle Alignment = getLLVMStyle();
   8835   Alignment.AlignConsecutiveDeclarations = false;
   8836   verifyFormat("float const a = 5;\n"
   8837                "int oneTwoThree = 123;",
   8838                Alignment);
   8839   verifyFormat("int a = 5;\n"
   8840                "float const oneTwoThree = 123;",
   8841                Alignment);
   8842 
   8843   Alignment.AlignConsecutiveDeclarations = true;
   8844   verifyFormat("float const a = 5;\n"
   8845                "int         oneTwoThree = 123;",
   8846                Alignment);
   8847   verifyFormat("int         a = method();\n"
   8848                "float const oneTwoThree = 133;",
   8849                Alignment);
   8850   verifyFormat("int i = 1, j = 10;\n"
   8851                "something = 2000;",
   8852                Alignment);
   8853   verifyFormat("something = 2000;\n"
   8854                "int i = 1, j = 10;\n",
   8855                Alignment);
   8856   verifyFormat("float      something = 2000;\n"
   8857                "double     another = 911;\n"
   8858                "int        i = 1, j = 10;\n"
   8859                "const int *oneMore = 1;\n"
   8860                "unsigned   i = 2;",
   8861                Alignment);
   8862   verifyFormat("float a = 5;\n"
   8863                "int   one = 1;\n"
   8864                "method();\n"
   8865                "const double       oneTwoThree = 123;\n"
   8866                "const unsigned int oneTwo = 12;",
   8867                Alignment);
   8868   verifyFormat("int      oneTwoThree{0}; // comment\n"
   8869                "unsigned oneTwo;         // comment",
   8870                Alignment);
   8871   EXPECT_EQ("float const a = 5;\n"
   8872             "\n"
   8873             "int oneTwoThree = 123;",
   8874             format("float const   a = 5;\n"
   8875                    "\n"
   8876                    "int           oneTwoThree= 123;",
   8877                    Alignment));
   8878   EXPECT_EQ("float a = 5;\n"
   8879             "int   one = 1;\n"
   8880             "\n"
   8881             "unsigned oneTwoThree = 123;",
   8882             format("float    a = 5;\n"
   8883                    "int      one = 1;\n"
   8884                    "\n"
   8885                    "unsigned oneTwoThree = 123;",
   8886                    Alignment));
   8887   EXPECT_EQ("float a = 5;\n"
   8888             "int   one = 1;\n"
   8889             "\n"
   8890             "unsigned oneTwoThree = 123;\n"
   8891             "int      oneTwo = 12;",
   8892             format("float    a = 5;\n"
   8893                    "int one = 1;\n"
   8894                    "\n"
   8895                    "unsigned oneTwoThree = 123;\n"
   8896                    "int oneTwo = 12;",
   8897                    Alignment));
   8898   Alignment.AlignConsecutiveAssignments = true;
   8899   verifyFormat("float      something = 2000;\n"
   8900                "double     another   = 911;\n"
   8901                "int        i = 1, j = 10;\n"
   8902                "const int *oneMore = 1;\n"
   8903                "unsigned   i       = 2;",
   8904                Alignment);
   8905   verifyFormat("int      oneTwoThree = {0}; // comment\n"
   8906                "unsigned oneTwo      = 0;   // comment",
   8907                Alignment);
   8908   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
   8909             "  int const i   = 1;\n"
   8910             "  int *     j   = 2;\n"
   8911             "  int       big = 10000;\n"
   8912             "\n"
   8913             "  unsigned oneTwoThree = 123;\n"
   8914             "  int      oneTwo      = 12;\n"
   8915             "  method();\n"
   8916             "  float k  = 2;\n"
   8917             "  int   ll = 10000;\n"
   8918             "}",
   8919             format("void SomeFunction(int parameter= 0) {\n"
   8920                    " int const  i= 1;\n"
   8921                    "  int *j=2;\n"
   8922                    " int big  =  10000;\n"
   8923                    "\n"
   8924                    "unsigned oneTwoThree  =123;\n"
   8925                    "int oneTwo = 12;\n"
   8926                    "  method();\n"
   8927                    "float k= 2;\n"
   8928                    "int ll=10000;\n"
   8929                    "}",
   8930                    Alignment));
   8931   Alignment.AlignConsecutiveAssignments = false;
   8932   Alignment.AlignEscapedNewlinesLeft = true;
   8933   verifyFormat("#define A              \\\n"
   8934                "  int       aaaa = 12; \\\n"
   8935                "  float     b = 23;    \\\n"
   8936                "  const int ccc = 234; \\\n"
   8937                "  unsigned  dddddddddd = 2345;",
   8938                Alignment);
   8939   Alignment.AlignEscapedNewlinesLeft = false;
   8940   Alignment.ColumnLimit = 30;
   8941   verifyFormat("#define A                    \\\n"
   8942                "  int       aaaa = 12;       \\\n"
   8943                "  float     b = 23;          \\\n"
   8944                "  const int ccc = 234;       \\\n"
   8945                "  int       dddddddddd = 2345;",
   8946                Alignment);
   8947   Alignment.ColumnLimit = 80;
   8948   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
   8949                "k = 4, int l = 5,\n"
   8950                "                  int m = 6) {\n"
   8951                "  const int j = 10;\n"
   8952                "  otherThing = 1;\n"
   8953                "}",
   8954                Alignment);
   8955   verifyFormat("void SomeFunction(int parameter = 0) {\n"
   8956                "  int const i = 1;\n"
   8957                "  int *     j = 2;\n"
   8958                "  int       big = 10000;\n"
   8959                "}",
   8960                Alignment);
   8961   verifyFormat("class C {\n"
   8962                "public:\n"
   8963                "  int          i = 1;\n"
   8964                "  virtual void f() = 0;\n"
   8965                "};",
   8966                Alignment);
   8967   verifyFormat("float i = 1;\n"
   8968                "if (SomeType t = getSomething()) {\n"
   8969                "}\n"
   8970                "const unsigned j = 2;\n"
   8971                "int            big = 10000;",
   8972                Alignment);
   8973   verifyFormat("float j = 7;\n"
   8974                "for (int k = 0; k < N; ++k) {\n"
   8975                "}\n"
   8976                "unsigned j = 2;\n"
   8977                "int      big = 10000;\n"
   8978                "}",
   8979                Alignment);
   8980   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
   8981   verifyFormat("float              i = 1;\n"
   8982                "LooooooooooongType loooooooooooooooooooooongVariable\n"
   8983                "    = someLooooooooooooooooongFunction();\n"
   8984                "int j = 2;",
   8985                Alignment);
   8986   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
   8987   verifyFormat("int                i = 1;\n"
   8988                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
   8989                "    someLooooooooooooooooongFunction();\n"
   8990                "int j = 2;",
   8991                Alignment);
   8992 
   8993   Alignment.AlignConsecutiveAssignments = true;
   8994   verifyFormat("auto lambda = []() {\n"
   8995                "  auto  ii = 0;\n"
   8996                "  float j  = 0;\n"
   8997                "  return 0;\n"
   8998                "};\n"
   8999                "int   i  = 0;\n"
   9000                "float i2 = 0;\n"
   9001                "auto  v  = type{\n"
   9002                "    i = 1,   //\n"
   9003                "    (i = 2), //\n"
   9004                "    i = 3    //\n"
   9005                "};",
   9006                Alignment);
   9007   Alignment.AlignConsecutiveAssignments = false;
   9008 
   9009   // FIXME: Should align all three declarations
   9010   verifyFormat(
   9011       "int      i = 1;\n"
   9012       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
   9013       "                          loooooooooooooooooooooongParameterB);\n"
   9014       "int j = 2;",
   9015       Alignment);
   9016 
   9017   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
   9018   // We expect declarations and assignments to align, as long as it doesn't
   9019   // exceed the column limit, starting a new alignemnt sequence whenever it
   9020   // happens.
   9021   Alignment.AlignConsecutiveAssignments = true;
   9022   Alignment.ColumnLimit = 30;
   9023   verifyFormat("float    ii              = 1;\n"
   9024                "unsigned j               = 2;\n"
   9025                "int someVerylongVariable = 1;\n"
   9026                "AnotherLongType  ll = 123456;\n"
   9027                "VeryVeryLongType k  = 2;\n"
   9028                "int              myvar = 1;",
   9029                Alignment);
   9030   Alignment.ColumnLimit = 80;
   9031   Alignment.AlignConsecutiveAssignments = false;
   9032 
   9033   verifyFormat(
   9034       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
   9035       "          typename LongType, typename B>\n"
   9036       "auto foo() {}\n",
   9037       Alignment);
   9038   verifyFormat("float a, b = 1;\n"
   9039                "int   c = 2;\n"
   9040                "int   dd = 3;\n",
   9041                Alignment);
   9042   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
   9043                "float b[1][] = {{3.f}};\n",
   9044                Alignment);
   9045   Alignment.AlignConsecutiveAssignments = true;
   9046   verifyFormat("float a, b = 1;\n"
   9047                "int   c  = 2;\n"
   9048                "int   dd = 3;\n",
   9049                Alignment);
   9050   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
   9051                "float b[1][] = {{3.f}};\n",
   9052                Alignment);
   9053   Alignment.AlignConsecutiveAssignments = false;
   9054 
   9055   Alignment.ColumnLimit = 30;
   9056   Alignment.BinPackParameters = false;
   9057   verifyFormat("void foo(float     a,\n"
   9058                "         float     b,\n"
   9059                "         int       c,\n"
   9060                "         uint32_t *d) {\n"
   9061                "  int *  e = 0;\n"
   9062                "  float  f = 0;\n"
   9063                "  double g = 0;\n"
   9064                "}\n"
   9065                "void bar(ino_t     a,\n"
   9066                "         int       b,\n"
   9067                "         uint32_t *c,\n"
   9068                "         bool      d) {}\n",
   9069                Alignment);
   9070   Alignment.BinPackParameters = true;
   9071   Alignment.ColumnLimit = 80;
   9072 }
   9073 
   9074 TEST_F(FormatTest, LinuxBraceBreaking) {
   9075   FormatStyle LinuxBraceStyle = getLLVMStyle();
   9076   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
   9077   verifyFormat("namespace a\n"
   9078                "{\n"
   9079                "class A\n"
   9080                "{\n"
   9081                "  void f()\n"
   9082                "  {\n"
   9083                "    if (true) {\n"
   9084                "      a();\n"
   9085                "      b();\n"
   9086                "    } else {\n"
   9087                "      a();\n"
   9088                "    }\n"
   9089                "  }\n"
   9090                "  void g() { return; }\n"
   9091                "};\n"
   9092                "struct B {\n"
   9093                "  int x;\n"
   9094                "};\n"
   9095                "}\n",
   9096                LinuxBraceStyle);
   9097   verifyFormat("enum X {\n"
   9098                "  Y = 0,\n"
   9099                "}\n",
   9100                LinuxBraceStyle);
   9101   verifyFormat("struct S {\n"
   9102                "  int Type;\n"
   9103                "  union {\n"
   9104                "    int x;\n"
   9105                "    double y;\n"
   9106                "  } Value;\n"
   9107                "  class C\n"
   9108                "  {\n"
   9109                "    MyFavoriteType Value;\n"
   9110                "  } Class;\n"
   9111                "}\n",
   9112                LinuxBraceStyle);
   9113 }
   9114 
   9115 TEST_F(FormatTest, MozillaBraceBreaking) {
   9116   FormatStyle MozillaBraceStyle = getLLVMStyle();
   9117   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
   9118   verifyFormat("namespace a {\n"
   9119                "class A\n"
   9120                "{\n"
   9121                "  void f()\n"
   9122                "  {\n"
   9123                "    if (true) {\n"
   9124                "      a();\n"
   9125                "      b();\n"
   9126                "    }\n"
   9127                "  }\n"
   9128                "  void g() { return; }\n"
   9129                "};\n"
   9130                "enum E\n"
   9131                "{\n"
   9132                "  A,\n"
   9133                "  // foo\n"
   9134                "  B,\n"
   9135                "  C\n"
   9136                "};\n"
   9137                "struct B\n"
   9138                "{\n"
   9139                "  int x;\n"
   9140                "};\n"
   9141                "}\n",
   9142                MozillaBraceStyle);
   9143   verifyFormat("struct S\n"
   9144                "{\n"
   9145                "  int Type;\n"
   9146                "  union\n"
   9147                "  {\n"
   9148                "    int x;\n"
   9149                "    double y;\n"
   9150                "  } Value;\n"
   9151                "  class C\n"
   9152                "  {\n"
   9153                "    MyFavoriteType Value;\n"
   9154                "  } Class;\n"
   9155                "}\n",
   9156                MozillaBraceStyle);
   9157 }
   9158 
   9159 TEST_F(FormatTest, StroustrupBraceBreaking) {
   9160   FormatStyle StroustrupBraceStyle = getLLVMStyle();
   9161   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
   9162   verifyFormat("namespace a {\n"
   9163                "class A {\n"
   9164                "  void f()\n"
   9165                "  {\n"
   9166                "    if (true) {\n"
   9167                "      a();\n"
   9168                "      b();\n"
   9169                "    }\n"
   9170                "  }\n"
   9171                "  void g() { return; }\n"
   9172                "};\n"
   9173                "struct B {\n"
   9174                "  int x;\n"
   9175                "};\n"
   9176                "}\n",
   9177                StroustrupBraceStyle);
   9178 
   9179   verifyFormat("void foo()\n"
   9180                "{\n"
   9181                "  if (a) {\n"
   9182                "    a();\n"
   9183                "  }\n"
   9184                "  else {\n"
   9185                "    b();\n"
   9186                "  }\n"
   9187                "}\n",
   9188                StroustrupBraceStyle);
   9189 
   9190   verifyFormat("#ifdef _DEBUG\n"
   9191                "int foo(int i = 0)\n"
   9192                "#else\n"
   9193                "int foo(int i = 5)\n"
   9194                "#endif\n"
   9195                "{\n"
   9196                "  return i;\n"
   9197                "}",
   9198                StroustrupBraceStyle);
   9199 
   9200   verifyFormat("void foo() {}\n"
   9201                "void bar()\n"
   9202                "#ifdef _DEBUG\n"
   9203                "{\n"
   9204                "  foo();\n"
   9205                "}\n"
   9206                "#else\n"
   9207                "{\n"
   9208                "}\n"
   9209                "#endif",
   9210                StroustrupBraceStyle);
   9211 
   9212   verifyFormat("void foobar() { int i = 5; }\n"
   9213                "#ifdef _DEBUG\n"
   9214                "void bar() {}\n"
   9215                "#else\n"
   9216                "void bar() { foobar(); }\n"
   9217                "#endif",
   9218                StroustrupBraceStyle);
   9219 }
   9220 
   9221 TEST_F(FormatTest, AllmanBraceBreaking) {
   9222   FormatStyle AllmanBraceStyle = getLLVMStyle();
   9223   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
   9224   verifyFormat("namespace a\n"
   9225                "{\n"
   9226                "class A\n"
   9227                "{\n"
   9228                "  void f()\n"
   9229                "  {\n"
   9230                "    if (true)\n"
   9231                "    {\n"
   9232                "      a();\n"
   9233                "      b();\n"
   9234                "    }\n"
   9235                "  }\n"
   9236                "  void g() { return; }\n"
   9237                "};\n"
   9238                "struct B\n"
   9239                "{\n"
   9240                "  int x;\n"
   9241                "};\n"
   9242                "}",
   9243                AllmanBraceStyle);
   9244 
   9245   verifyFormat("void f()\n"
   9246                "{\n"
   9247                "  if (true)\n"
   9248                "  {\n"
   9249                "    a();\n"
   9250                "  }\n"
   9251                "  else if (false)\n"
   9252                "  {\n"
   9253                "    b();\n"
   9254                "  }\n"
   9255                "  else\n"
   9256                "  {\n"
   9257                "    c();\n"
   9258                "  }\n"
   9259                "}\n",
   9260                AllmanBraceStyle);
   9261 
   9262   verifyFormat("void f()\n"
   9263                "{\n"
   9264                "  for (int i = 0; i < 10; ++i)\n"
   9265                "  {\n"
   9266                "    a();\n"
   9267                "  }\n"
   9268                "  while (false)\n"
   9269                "  {\n"
   9270                "    b();\n"
   9271                "  }\n"
   9272                "  do\n"
   9273                "  {\n"
   9274                "    c();\n"
   9275                "  } while (false)\n"
   9276                "}\n",
   9277                AllmanBraceStyle);
   9278 
   9279   verifyFormat("void f(int a)\n"
   9280                "{\n"
   9281                "  switch (a)\n"
   9282                "  {\n"
   9283                "  case 0:\n"
   9284                "    break;\n"
   9285                "  case 1:\n"
   9286                "  {\n"
   9287                "    break;\n"
   9288                "  }\n"
   9289                "  case 2:\n"
   9290                "  {\n"
   9291                "  }\n"
   9292                "  break;\n"
   9293                "  default:\n"
   9294                "    break;\n"
   9295                "  }\n"
   9296                "}\n",
   9297                AllmanBraceStyle);
   9298 
   9299   verifyFormat("enum X\n"
   9300                "{\n"
   9301                "  Y = 0,\n"
   9302                "}\n",
   9303                AllmanBraceStyle);
   9304   verifyFormat("enum X\n"
   9305                "{\n"
   9306                "  Y = 0\n"
   9307                "}\n",
   9308                AllmanBraceStyle);
   9309 
   9310   verifyFormat("@interface BSApplicationController ()\n"
   9311                "{\n"
   9312                "@private\n"
   9313                "  id _extraIvar;\n"
   9314                "}\n"
   9315                "@end\n",
   9316                AllmanBraceStyle);
   9317 
   9318   verifyFormat("#ifdef _DEBUG\n"
   9319                "int foo(int i = 0)\n"
   9320                "#else\n"
   9321                "int foo(int i = 5)\n"
   9322                "#endif\n"
   9323                "{\n"
   9324                "  return i;\n"
   9325                "}",
   9326                AllmanBraceStyle);
   9327 
   9328   verifyFormat("void foo() {}\n"
   9329                "void bar()\n"
   9330                "#ifdef _DEBUG\n"
   9331                "{\n"
   9332                "  foo();\n"
   9333                "}\n"
   9334                "#else\n"
   9335                "{\n"
   9336                "}\n"
   9337                "#endif",
   9338                AllmanBraceStyle);
   9339 
   9340   verifyFormat("void foobar() { int i = 5; }\n"
   9341                "#ifdef _DEBUG\n"
   9342                "void bar() {}\n"
   9343                "#else\n"
   9344                "void bar() { foobar(); }\n"
   9345                "#endif",
   9346                AllmanBraceStyle);
   9347 
   9348   // This shouldn't affect ObjC blocks..
   9349   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
   9350                "  // ...\n"
   9351                "  int i;\n"
   9352                "}];",
   9353                AllmanBraceStyle);
   9354   verifyFormat("void (^block)(void) = ^{\n"
   9355                "  // ...\n"
   9356                "  int i;\n"
   9357                "};",
   9358                AllmanBraceStyle);
   9359   // .. or dict literals.
   9360   verifyFormat("void f()\n"
   9361                "{\n"
   9362                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
   9363                "}",
   9364                AllmanBraceStyle);
   9365   verifyFormat("int f()\n"
   9366                "{ // comment\n"
   9367                "  return 42;\n"
   9368                "}",
   9369                AllmanBraceStyle);
   9370 
   9371   AllmanBraceStyle.ColumnLimit = 19;
   9372   verifyFormat("void f() { int i; }", AllmanBraceStyle);
   9373   AllmanBraceStyle.ColumnLimit = 18;
   9374   verifyFormat("void f()\n"
   9375                "{\n"
   9376                "  int i;\n"
   9377                "}",
   9378                AllmanBraceStyle);
   9379   AllmanBraceStyle.ColumnLimit = 80;
   9380 
   9381   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
   9382   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
   9383   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
   9384   verifyFormat("void f(bool b)\n"
   9385                "{\n"
   9386                "  if (b)\n"
   9387                "  {\n"
   9388                "    return;\n"
   9389                "  }\n"
   9390                "}\n",
   9391                BreakBeforeBraceShortIfs);
   9392   verifyFormat("void f(bool b)\n"
   9393                "{\n"
   9394                "  if (b) return;\n"
   9395                "}\n",
   9396                BreakBeforeBraceShortIfs);
   9397   verifyFormat("void f(bool b)\n"
   9398                "{\n"
   9399                "  while (b)\n"
   9400                "  {\n"
   9401                "    return;\n"
   9402                "  }\n"
   9403                "}\n",
   9404                BreakBeforeBraceShortIfs);
   9405 }
   9406 
   9407 TEST_F(FormatTest, GNUBraceBreaking) {
   9408   FormatStyle GNUBraceStyle = getLLVMStyle();
   9409   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
   9410   verifyFormat("namespace a\n"
   9411                "{\n"
   9412                "class A\n"
   9413                "{\n"
   9414                "  void f()\n"
   9415                "  {\n"
   9416                "    int a;\n"
   9417                "    {\n"
   9418                "      int b;\n"
   9419                "    }\n"
   9420                "    if (true)\n"
   9421                "      {\n"
   9422                "        a();\n"
   9423                "        b();\n"
   9424                "      }\n"
   9425                "  }\n"
   9426                "  void g() { return; }\n"
   9427                "}\n"
   9428                "}",
   9429                GNUBraceStyle);
   9430 
   9431   verifyFormat("void f()\n"
   9432                "{\n"
   9433                "  if (true)\n"
   9434                "    {\n"
   9435                "      a();\n"
   9436                "    }\n"
   9437                "  else if (false)\n"
   9438                "    {\n"
   9439                "      b();\n"
   9440                "    }\n"
   9441                "  else\n"
   9442                "    {\n"
   9443                "      c();\n"
   9444                "    }\n"
   9445                "}\n",
   9446                GNUBraceStyle);
   9447 
   9448   verifyFormat("void f()\n"
   9449                "{\n"
   9450                "  for (int i = 0; i < 10; ++i)\n"
   9451                "    {\n"
   9452                "      a();\n"
   9453                "    }\n"
   9454                "  while (false)\n"
   9455                "    {\n"
   9456                "      b();\n"
   9457                "    }\n"
   9458                "  do\n"
   9459                "    {\n"
   9460                "      c();\n"
   9461                "    }\n"
   9462                "  while (false);\n"
   9463                "}\n",
   9464                GNUBraceStyle);
   9465 
   9466   verifyFormat("void f(int a)\n"
   9467                "{\n"
   9468                "  switch (a)\n"
   9469                "    {\n"
   9470                "    case 0:\n"
   9471                "      break;\n"
   9472                "    case 1:\n"
   9473                "      {\n"
   9474                "        break;\n"
   9475                "      }\n"
   9476                "    case 2:\n"
   9477                "      {\n"
   9478                "      }\n"
   9479                "      break;\n"
   9480                "    default:\n"
   9481                "      break;\n"
   9482                "    }\n"
   9483                "}\n",
   9484                GNUBraceStyle);
   9485 
   9486   verifyFormat("enum X\n"
   9487                "{\n"
   9488                "  Y = 0,\n"
   9489                "}\n",
   9490                GNUBraceStyle);
   9491 
   9492   verifyFormat("@interface BSApplicationController ()\n"
   9493                "{\n"
   9494                "@private\n"
   9495                "  id _extraIvar;\n"
   9496                "}\n"
   9497                "@end\n",
   9498                GNUBraceStyle);
   9499 
   9500   verifyFormat("#ifdef _DEBUG\n"
   9501                "int foo(int i = 0)\n"
   9502                "#else\n"
   9503                "int foo(int i = 5)\n"
   9504                "#endif\n"
   9505                "{\n"
   9506                "  return i;\n"
   9507                "}",
   9508                GNUBraceStyle);
   9509 
   9510   verifyFormat("void foo() {}\n"
   9511                "void bar()\n"
   9512                "#ifdef _DEBUG\n"
   9513                "{\n"
   9514                "  foo();\n"
   9515                "}\n"
   9516                "#else\n"
   9517                "{\n"
   9518                "}\n"
   9519                "#endif",
   9520                GNUBraceStyle);
   9521 
   9522   verifyFormat("void foobar() { int i = 5; }\n"
   9523                "#ifdef _DEBUG\n"
   9524                "void bar() {}\n"
   9525                "#else\n"
   9526                "void bar() { foobar(); }\n"
   9527                "#endif",
   9528                GNUBraceStyle);
   9529 }
   9530 
   9531 TEST_F(FormatTest, WebKitBraceBreaking) {
   9532   FormatStyle WebKitBraceStyle = getLLVMStyle();
   9533   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
   9534   verifyFormat("namespace a {\n"
   9535                "class A {\n"
   9536                "  void f()\n"
   9537                "  {\n"
   9538                "    if (true) {\n"
   9539                "      a();\n"
   9540                "      b();\n"
   9541                "    }\n"
   9542                "  }\n"
   9543                "  void g() { return; }\n"
   9544                "};\n"
   9545                "enum E {\n"
   9546                "  A,\n"
   9547                "  // foo\n"
   9548                "  B,\n"
   9549                "  C\n"
   9550                "};\n"
   9551                "struct B {\n"
   9552                "  int x;\n"
   9553                "};\n"
   9554                "}\n",
   9555                WebKitBraceStyle);
   9556   verifyFormat("struct S {\n"
   9557                "  int Type;\n"
   9558                "  union {\n"
   9559                "    int x;\n"
   9560                "    double y;\n"
   9561                "  } Value;\n"
   9562                "  class C {\n"
   9563                "    MyFavoriteType Value;\n"
   9564                "  } Class;\n"
   9565                "};\n",
   9566                WebKitBraceStyle);
   9567 }
   9568 
   9569 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
   9570   verifyFormat("void f() {\n"
   9571                "  try {\n"
   9572                "  } catch (const Exception &e) {\n"
   9573                "  }\n"
   9574                "}\n",
   9575                getLLVMStyle());
   9576 }
   9577 
   9578 TEST_F(FormatTest, UnderstandsPragmas) {
   9579   verifyFormat("#pragma omp reduction(| : var)");
   9580   verifyFormat("#pragma omp reduction(+ : var)");
   9581 
   9582   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
   9583             "(including parentheses).",
   9584             format("#pragma    mark   Any non-hyphenated or hyphenated string "
   9585                    "(including parentheses)."));
   9586 }
   9587 
   9588 TEST_F(FormatTest, UnderstandPragmaOption) {
   9589   verifyFormat("#pragma option -C -A");
   9590 
   9591   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
   9592 }
   9593 
   9594 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
   9595   for (size_t i = 1; i < Styles.size(); ++i)                                   \
   9596   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
   9597                                   << " differs from Style #0"
   9598 
   9599 TEST_F(FormatTest, GetsPredefinedStyleByName) {
   9600   SmallVector<FormatStyle, 3> Styles;
   9601   Styles.resize(3);
   9602 
   9603   Styles[0] = getLLVMStyle();
   9604   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
   9605   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
   9606   EXPECT_ALL_STYLES_EQUAL(Styles);
   9607 
   9608   Styles[0] = getGoogleStyle();
   9609   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
   9610   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
   9611   EXPECT_ALL_STYLES_EQUAL(Styles);
   9612 
   9613   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
   9614   EXPECT_TRUE(
   9615       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
   9616   EXPECT_TRUE(
   9617       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
   9618   EXPECT_ALL_STYLES_EQUAL(Styles);
   9619 
   9620   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
   9621   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
   9622   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
   9623   EXPECT_ALL_STYLES_EQUAL(Styles);
   9624 
   9625   Styles[0] = getMozillaStyle();
   9626   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
   9627   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
   9628   EXPECT_ALL_STYLES_EQUAL(Styles);
   9629 
   9630   Styles[0] = getWebKitStyle();
   9631   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
   9632   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
   9633   EXPECT_ALL_STYLES_EQUAL(Styles);
   9634 
   9635   Styles[0] = getGNUStyle();
   9636   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
   9637   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
   9638   EXPECT_ALL_STYLES_EQUAL(Styles);
   9639 
   9640   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
   9641 }
   9642 
   9643 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
   9644   SmallVector<FormatStyle, 8> Styles;
   9645   Styles.resize(2);
   9646 
   9647   Styles[0] = getGoogleStyle();
   9648   Styles[1] = getLLVMStyle();
   9649   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
   9650   EXPECT_ALL_STYLES_EQUAL(Styles);
   9651 
   9652   Styles.resize(5);
   9653   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
   9654   Styles[1] = getLLVMStyle();
   9655   Styles[1].Language = FormatStyle::LK_JavaScript;
   9656   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
   9657 
   9658   Styles[2] = getLLVMStyle();
   9659   Styles[2].Language = FormatStyle::LK_JavaScript;
   9660   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
   9661                                   "BasedOnStyle: Google",
   9662                                   &Styles[2])
   9663                    .value());
   9664 
   9665   Styles[3] = getLLVMStyle();
   9666   Styles[3].Language = FormatStyle::LK_JavaScript;
   9667   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
   9668                                   "Language: JavaScript",
   9669                                   &Styles[3])
   9670                    .value());
   9671 
   9672   Styles[4] = getLLVMStyle();
   9673   Styles[4].Language = FormatStyle::LK_JavaScript;
   9674   EXPECT_EQ(0, parseConfiguration("---\n"
   9675                                   "BasedOnStyle: LLVM\n"
   9676                                   "IndentWidth: 123\n"
   9677                                   "---\n"
   9678                                   "BasedOnStyle: Google\n"
   9679                                   "Language: JavaScript",
   9680                                   &Styles[4])
   9681                    .value());
   9682   EXPECT_ALL_STYLES_EQUAL(Styles);
   9683 }
   9684 
   9685 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
   9686   Style.FIELD = false;                                                         \
   9687   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
   9688   EXPECT_TRUE(Style.FIELD);                                                    \
   9689   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
   9690   EXPECT_FALSE(Style.FIELD);
   9691 
   9692 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
   9693 
   9694 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
   9695   Style.STRUCT.FIELD = false;                                                  \
   9696   EXPECT_EQ(0,                                                                 \
   9697             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
   9698                 .value());                                                     \
   9699   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
   9700   EXPECT_EQ(0,                                                                 \
   9701             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
   9702                 .value());                                                     \
   9703   EXPECT_FALSE(Style.STRUCT.FIELD);
   9704 
   9705 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
   9706   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
   9707 
   9708 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
   9709   EXPECT_NE(VALUE, Style.FIELD);                                               \
   9710   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
   9711   EXPECT_EQ(VALUE, Style.FIELD)
   9712 
   9713 TEST_F(FormatTest, ParsesConfigurationBools) {
   9714   FormatStyle Style = {};
   9715   Style.Language = FormatStyle::LK_Cpp;
   9716   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
   9717   CHECK_PARSE_BOOL(AlignOperands);
   9718   CHECK_PARSE_BOOL(AlignTrailingComments);
   9719   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
   9720   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
   9721   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
   9722   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
   9723   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
   9724   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
   9725   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
   9726   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
   9727   CHECK_PARSE_BOOL(BinPackArguments);
   9728   CHECK_PARSE_BOOL(BinPackParameters);
   9729   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
   9730   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
   9731   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
   9732   CHECK_PARSE_BOOL(DerivePointerAlignment);
   9733   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
   9734   CHECK_PARSE_BOOL(DisableFormat);
   9735   CHECK_PARSE_BOOL(IndentCaseLabels);
   9736   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
   9737   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
   9738   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
   9739   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
   9740   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
   9741   CHECK_PARSE_BOOL(ReflowComments);
   9742   CHECK_PARSE_BOOL(SortIncludes);
   9743   CHECK_PARSE_BOOL(SpacesInParentheses);
   9744   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
   9745   CHECK_PARSE_BOOL(SpacesInAngles);
   9746   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
   9747   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
   9748   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
   9749   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
   9750   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
   9751 
   9752   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
   9753   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
   9754   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
   9755   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
   9756   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
   9757   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
   9758   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
   9759   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
   9760   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
   9761   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
   9762   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
   9763 }
   9764 
   9765 #undef CHECK_PARSE_BOOL
   9766 
   9767 TEST_F(FormatTest, ParsesConfiguration) {
   9768   FormatStyle Style = {};
   9769   Style.Language = FormatStyle::LK_Cpp;
   9770   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
   9771   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
   9772               ConstructorInitializerIndentWidth, 1234u);
   9773   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
   9774   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
   9775   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
   9776   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
   9777               PenaltyBreakBeforeFirstCallParameter, 1234u);
   9778   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
   9779   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
   9780               PenaltyReturnTypeOnItsOwnLine, 1234u);
   9781   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
   9782               SpacesBeforeTrailingComments, 1234u);
   9783   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
   9784   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
   9785 
   9786   Style.PointerAlignment = FormatStyle::PAS_Middle;
   9787   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
   9788               FormatStyle::PAS_Left);
   9789   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
   9790               FormatStyle::PAS_Right);
   9791   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
   9792               FormatStyle::PAS_Middle);
   9793   // For backward compatibility:
   9794   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
   9795               FormatStyle::PAS_Left);
   9796   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
   9797               FormatStyle::PAS_Right);
   9798   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
   9799               FormatStyle::PAS_Middle);
   9800 
   9801   Style.Standard = FormatStyle::LS_Auto;
   9802   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
   9803   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
   9804   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
   9805   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
   9806   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
   9807 
   9808   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
   9809   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
   9810               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
   9811   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
   9812               FormatStyle::BOS_None);
   9813   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
   9814               FormatStyle::BOS_All);
   9815   // For backward compatibility:
   9816   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
   9817               FormatStyle::BOS_None);
   9818   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
   9819               FormatStyle::BOS_All);
   9820 
   9821   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
   9822   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
   9823               FormatStyle::BAS_Align);
   9824   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
   9825               FormatStyle::BAS_DontAlign);
   9826   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
   9827               FormatStyle::BAS_AlwaysBreak);
   9828   // For backward compatibility:
   9829   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
   9830               FormatStyle::BAS_DontAlign);
   9831   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
   9832               FormatStyle::BAS_Align);
   9833 
   9834   Style.UseTab = FormatStyle::UT_ForIndentation;
   9835   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
   9836   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
   9837   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
   9838   // For backward compatibility:
   9839   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
   9840   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
   9841 
   9842   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
   9843   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
   9844               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
   9845   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
   9846               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
   9847   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
   9848               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
   9849   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
   9850               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
   9851   // For backward compatibility:
   9852   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
   9853               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
   9854   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
   9855               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
   9856 
   9857   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
   9858   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
   9859               FormatStyle::SBPO_Never);
   9860   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
   9861               FormatStyle::SBPO_Always);
   9862   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
   9863               FormatStyle::SBPO_ControlStatements);
   9864   // For backward compatibility:
   9865   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
   9866               FormatStyle::SBPO_Never);
   9867   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
   9868               FormatStyle::SBPO_ControlStatements);
   9869 
   9870   Style.ColumnLimit = 123;
   9871   FormatStyle BaseStyle = getLLVMStyle();
   9872   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
   9873   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
   9874 
   9875   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
   9876   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
   9877               FormatStyle::BS_Attach);
   9878   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
   9879               FormatStyle::BS_Linux);
   9880   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
   9881               FormatStyle::BS_Mozilla);
   9882   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
   9883               FormatStyle::BS_Stroustrup);
   9884   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
   9885               FormatStyle::BS_Allman);
   9886   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
   9887   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
   9888               FormatStyle::BS_WebKit);
   9889   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
   9890               FormatStyle::BS_Custom);
   9891 
   9892   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
   9893   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
   9894               FormatStyle::RTBS_None);
   9895   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
   9896               FormatStyle::RTBS_All);
   9897   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
   9898               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
   9899   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
   9900               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
   9901   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
   9902               AlwaysBreakAfterReturnType,
   9903               FormatStyle::RTBS_TopLevelDefinitions);
   9904 
   9905   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
   9906   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
   9907               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
   9908   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
   9909               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
   9910   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
   9911               AlwaysBreakAfterDefinitionReturnType,
   9912               FormatStyle::DRTBS_TopLevel);
   9913 
   9914   Style.NamespaceIndentation = FormatStyle::NI_All;
   9915   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
   9916               FormatStyle::NI_None);
   9917   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
   9918               FormatStyle::NI_Inner);
   9919   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
   9920               FormatStyle::NI_All);
   9921 
   9922   // FIXME: This is required because parsing a configuration simply overwrites
   9923   // the first N elements of the list instead of resetting it.
   9924   Style.ForEachMacros.clear();
   9925   std::vector<std::string> BoostForeach;
   9926   BoostForeach.push_back("BOOST_FOREACH");
   9927   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
   9928   std::vector<std::string> BoostAndQForeach;
   9929   BoostAndQForeach.push_back("BOOST_FOREACH");
   9930   BoostAndQForeach.push_back("Q_FOREACH");
   9931   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
   9932               BoostAndQForeach);
   9933 
   9934   Style.IncludeCategories.clear();
   9935   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
   9936                                                                   {".*", 1}};
   9937   CHECK_PARSE("IncludeCategories:\n"
   9938               "  - Regex: abc/.*\n"
   9939               "    Priority: 2\n"
   9940               "  - Regex: .*\n"
   9941               "    Priority: 1",
   9942               IncludeCategories, ExpectedCategories);
   9943 }
   9944 
   9945 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
   9946   FormatStyle Style = {};
   9947   Style.Language = FormatStyle::LK_Cpp;
   9948   CHECK_PARSE("Language: Cpp\n"
   9949               "IndentWidth: 12",
   9950               IndentWidth, 12u);
   9951   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
   9952                                "IndentWidth: 34",
   9953                                &Style),
   9954             ParseError::Unsuitable);
   9955   EXPECT_EQ(12u, Style.IndentWidth);
   9956   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
   9957   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
   9958 
   9959   Style.Language = FormatStyle::LK_JavaScript;
   9960   CHECK_PARSE("Language: JavaScript\n"
   9961               "IndentWidth: 12",
   9962               IndentWidth, 12u);
   9963   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
   9964   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
   9965                                "IndentWidth: 34",
   9966                                &Style),
   9967             ParseError::Unsuitable);
   9968   EXPECT_EQ(23u, Style.IndentWidth);
   9969   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
   9970   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
   9971 
   9972   CHECK_PARSE("BasedOnStyle: LLVM\n"
   9973               "IndentWidth: 67",
   9974               IndentWidth, 67u);
   9975 
   9976   CHECK_PARSE("---\n"
   9977               "Language: JavaScript\n"
   9978               "IndentWidth: 12\n"
   9979               "---\n"
   9980               "Language: Cpp\n"
   9981               "IndentWidth: 34\n"
   9982               "...\n",
   9983               IndentWidth, 12u);
   9984 
   9985   Style.Language = FormatStyle::LK_Cpp;
   9986   CHECK_PARSE("---\n"
   9987               "Language: JavaScript\n"
   9988               "IndentWidth: 12\n"
   9989               "---\n"
   9990               "Language: Cpp\n"
   9991               "IndentWidth: 34\n"
   9992               "...\n",
   9993               IndentWidth, 34u);
   9994   CHECK_PARSE("---\n"
   9995               "IndentWidth: 78\n"
   9996               "---\n"
   9997               "Language: JavaScript\n"
   9998               "IndentWidth: 56\n"
   9999               "...\n",
   10000               IndentWidth, 78u);
   10001 
   10002   Style.ColumnLimit = 123;
   10003   Style.IndentWidth = 234;
   10004   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
   10005   Style.TabWidth = 345;
   10006   EXPECT_FALSE(parseConfiguration("---\n"
   10007                                   "IndentWidth: 456\n"
   10008                                   "BreakBeforeBraces: Allman\n"
   10009                                   "---\n"
   10010                                   "Language: JavaScript\n"
   10011                                   "IndentWidth: 111\n"
   10012                                   "TabWidth: 111\n"
   10013                                   "---\n"
   10014                                   "Language: Cpp\n"
   10015                                   "BreakBeforeBraces: Stroustrup\n"
   10016                                   "TabWidth: 789\n"
   10017                                   "...\n",
   10018                                   &Style));
   10019   EXPECT_EQ(123u, Style.ColumnLimit);
   10020   EXPECT_EQ(456u, Style.IndentWidth);
   10021   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
   10022   EXPECT_EQ(789u, Style.TabWidth);
   10023 
   10024   EXPECT_EQ(parseConfiguration("---\n"
   10025                                "Language: JavaScript\n"
   10026                                "IndentWidth: 56\n"
   10027                                "---\n"
   10028                                "IndentWidth: 78\n"
   10029                                "...\n",
   10030                                &Style),
   10031             ParseError::Error);
   10032   EXPECT_EQ(parseConfiguration("---\n"
   10033                                "Language: JavaScript\n"
   10034                                "IndentWidth: 56\n"
   10035                                "---\n"
   10036                                "Language: JavaScript\n"
   10037                                "IndentWidth: 78\n"
   10038                                "...\n",
   10039                                &Style),
   10040             ParseError::Error);
   10041 
   10042   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
   10043 }
   10044 
   10045 #undef CHECK_PARSE
   10046 
   10047 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
   10048   FormatStyle Style = {};
   10049   Style.Language = FormatStyle::LK_JavaScript;
   10050   Style.BreakBeforeTernaryOperators = true;
   10051   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
   10052   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
   10053 
   10054   Style.BreakBeforeTernaryOperators = true;
   10055   EXPECT_EQ(0, parseConfiguration("---\n"
   10056                                   "BasedOnStyle: Google\n"
   10057                                   "---\n"
   10058                                   "Language: JavaScript\n"
   10059                                   "IndentWidth: 76\n"
   10060                                   "...\n",
   10061                                   &Style)
   10062                    .value());
   10063   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
   10064   EXPECT_EQ(76u, Style.IndentWidth);
   10065   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
   10066 }
   10067 
   10068 TEST_F(FormatTest, ConfigurationRoundTripTest) {
   10069   FormatStyle Style = getLLVMStyle();
   10070   std::string YAML = configurationAsText(Style);
   10071   FormatStyle ParsedStyle = {};
   10072   ParsedStyle.Language = FormatStyle::LK_Cpp;
   10073   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
   10074   EXPECT_EQ(Style, ParsedStyle);
   10075 }
   10076 
   10077 TEST_F(FormatTest, WorksFor8bitEncodings) {
   10078   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
   10079             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
   10080             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
   10081             "\"\xef\xee\xf0\xf3...\"",
   10082             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
   10083                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
   10084                    "\xef\xee\xf0\xf3...\"",
   10085                    getLLVMStyleWithColumns(12)));
   10086 }
   10087 
   10088 TEST_F(FormatTest, HandlesUTF8BOM) {
   10089   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
   10090   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
   10091             format("\xef\xbb\xbf#include <iostream>"));
   10092   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
   10093             format("\xef\xbb\xbf\n#include <iostream>"));
   10094 }
   10095 
   10096 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
   10097 #if !defined(_MSC_VER)
   10098 
   10099 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
   10100   verifyFormat("\"    ...\"",
   10101                getLLVMStyleWithColumns(35));
   10102   verifyFormat("\"         \"",
   10103                getLLVMStyleWithColumns(31));
   10104   verifyFormat("//     ...",
   10105                getLLVMStyleWithColumns(36));
   10106   verifyFormat("//          ", getLLVMStyleWithColumns(32));
   10107   verifyFormat("/*     ... */",
   10108                getLLVMStyleWithColumns(39));
   10109   verifyFormat("/*           */",
   10110                getLLVMStyleWithColumns(35));
   10111 }
   10112 
   10113 TEST_F(FormatTest, SplitsUTF8Strings) {
   10114   // Non-printable characters' width is currently considered to be the length in
   10115   // bytes in UTF8. The characters can be displayed in very different manner
   10116   // (zero-width, single width with a substitution glyph, expanded to their code
   10117   // (e.g. "<8d>"), so there's no single correct way to handle them.
   10118   EXPECT_EQ("\"aaaa\"\n"
   10119             "\"\xc2\x8d\";",
   10120             format("\"aaaa\xc2\x8d\";", getLLVMStyleWithColumns(10)));
   10121   EXPECT_EQ("\"aaaaaaa\"\n"
   10122             "\"\xc2\x8d\";",
   10123             format("\"aaaaaaa\xc2\x8d\";", getLLVMStyleWithColumns(10)));
   10124   EXPECT_EQ("\",  \"\n"
   10125             "\" \"\n"
   10126             "\" \"\n"
   10127             "\",\"",
   10128             format("\",    ,\"",
   10129                    getLLVMStyleWithColumns(13)));
   10130   EXPECT_EQ(
   10131       "\"   \"\n"
   10132       "\"  \"\n"
   10133       "\"   \"\n"
   10134       "\"\"",
   10135       format("\"        \"", getLLVMStyleWithColumns(11)));
   10136   EXPECT_EQ("\"\t \"\n"
   10137             "\"\t \"\n"
   10138             "\" \t \"\n"
   10139             "\"\t \"\n"
   10140             "\"\tqq\"",
   10141             format("\"\t \t  \t \t \tqq\"",
   10142                    getLLVMStyleWithColumns(11)));
   10143 
   10144   // UTF8 character in an escape sequence.
   10145   EXPECT_EQ("\"aaaaaa\"\n"
   10146             "\"\\\xC2\x8D\"",
   10147             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
   10148 }
   10149 
   10150 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
   10151   EXPECT_EQ("const char *sssss =\n"
   10152             "    \"\\\n"
   10153             "  \";",
   10154             format("const char *sssss = \"\\\n"
   10155                    "  \";",
   10156                    getLLVMStyleWithColumns(30)));
   10157 }
   10158 
   10159 TEST_F(FormatTest, SplitsUTF8LineComments) {
   10160   EXPECT_EQ("// aaaa\xc2\x8d",
   10161             format("// aaaa\xc2\x8d", getLLVMStyleWithColumns(10)));
   10162   EXPECT_EQ("//   \n"
   10163             "// ; \n"
   10164             "// \n"
   10165             "// .",
   10166             format("//    ;   .",
   10167                    getLLVMStyleWithColumns(13)));
   10168   EXPECT_EQ("// \n"
   10169             "// \n"
   10170             "//   \n"
   10171             "// ",
   10172             format("//      ", getLLVMStyleWithColumns(9)));
   10173 }
   10174 
   10175 TEST_F(FormatTest, SplitsUTF8BlockComments) {
   10176   EXPECT_EQ("/* ,\n"
   10177             " * \n"
   10178             " *  \n"
   10179             " * \n"
   10180             " * ,\n"
   10181             " * \n"
   10182             " * \n"
   10183             " * . */",
   10184             format("/* ,    \n"
   10185                    " * ,   . */",
   10186                    getLLVMStyleWithColumns(13)));
   10187   EXPECT_EQ(
   10188       "/* \n"
   10189       " * \n"
   10190       " *   \n"
   10191       " *   */",
   10192       format("/*        */", getLLVMStyleWithColumns(9)));
   10193   EXPECT_EQ("/*  \n"
   10194             " * \n"
   10195             " * - */",
   10196             format("/*    - */", getLLVMStyleWithColumns(12)));
   10197 }
   10198 
   10199 #endif // _MSC_VER
   10200 
   10201 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
   10202   FormatStyle Style = getLLVMStyle();
   10203 
   10204   Style.ConstructorInitializerIndentWidth = 4;
   10205   verifyFormat(
   10206       "SomeClass::Constructor()\n"
   10207       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   10208       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
   10209       Style);
   10210 
   10211   Style.ConstructorInitializerIndentWidth = 2;
   10212   verifyFormat(
   10213       "SomeClass::Constructor()\n"
   10214       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   10215       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
   10216       Style);
   10217 
   10218   Style.ConstructorInitializerIndentWidth = 0;
   10219   verifyFormat(
   10220       "SomeClass::Constructor()\n"
   10221       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
   10222       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
   10223       Style);
   10224 }
   10225 
   10226 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
   10227   FormatStyle Style = getLLVMStyle();
   10228   Style.BreakConstructorInitializersBeforeComma = true;
   10229   Style.ConstructorInitializerIndentWidth = 4;
   10230   verifyFormat("SomeClass::Constructor()\n"
   10231                "    : a(a)\n"
   10232                "    , b(b)\n"
   10233                "    , c(c) {}",
   10234                Style);
   10235   verifyFormat("SomeClass::Constructor()\n"
   10236                "    : a(a) {}",
   10237                Style);
   10238 
   10239   Style.ColumnLimit = 0;
   10240   verifyFormat("SomeClass::Constructor()\n"
   10241                "    : a(a) {}",
   10242                Style);
   10243   verifyFormat("SomeClass::Constructor()\n"
   10244                "    : a(a)\n"
   10245                "    , b(b)\n"
   10246                "    , c(c) {}",
   10247                Style);
   10248   verifyFormat("SomeClass::Constructor()\n"
   10249                "    : a(a) {\n"
   10250                "  foo();\n"
   10251                "  bar();\n"
   10252                "}",
   10253                Style);
   10254 
   10255   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
   10256   verifyFormat("SomeClass::Constructor()\n"
   10257                "    : a(a)\n"
   10258                "    , b(b)\n"
   10259                "    , c(c) {\n}",
   10260                Style);
   10261   verifyFormat("SomeClass::Constructor()\n"
   10262                "    : a(a) {\n}",
   10263                Style);
   10264 
   10265   Style.ColumnLimit = 80;
   10266   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
   10267   Style.ConstructorInitializerIndentWidth = 2;
   10268   verifyFormat("SomeClass::Constructor()\n"
   10269                "  : a(a)\n"
   10270                "  , b(b)\n"
   10271                "  , c(c) {}",
   10272                Style);
   10273 
   10274   Style.ConstructorInitializerIndentWidth = 0;
   10275   verifyFormat("SomeClass::Constructor()\n"
   10276                ": a(a)\n"
   10277                ", b(b)\n"
   10278                ", c(c) {}",
   10279                Style);
   10280 
   10281   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   10282   Style.ConstructorInitializerIndentWidth = 4;
   10283   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
   10284   verifyFormat(
   10285       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
   10286       Style);
   10287   verifyFormat(
   10288       "SomeClass::Constructor()\n"
   10289       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
   10290       Style);
   10291   Style.ConstructorInitializerIndentWidth = 4;
   10292   Style.ColumnLimit = 60;
   10293   verifyFormat("SomeClass::Constructor()\n"
   10294                "    : aaaaaaaa(aaaaaaaa)\n"
   10295                "    , aaaaaaaa(aaaaaaaa)\n"
   10296                "    , aaaaaaaa(aaaaaaaa) {}",
   10297                Style);
   10298 }
   10299 
   10300 TEST_F(FormatTest, Destructors) {
   10301   verifyFormat("void F(int &i) { i.~int(); }");
   10302   verifyFormat("void F(int &i) { i->~int(); }");
   10303 }
   10304 
   10305 TEST_F(FormatTest, FormatsWithWebKitStyle) {
   10306   FormatStyle Style = getWebKitStyle();
   10307 
   10308   // Don't indent in outer namespaces.
   10309   verifyFormat("namespace outer {\n"
   10310                "int i;\n"
   10311                "namespace inner {\n"
   10312                "    int i;\n"
   10313                "} // namespace inner\n"
   10314                "} // namespace outer\n"
   10315                "namespace other_outer {\n"
   10316                "int i;\n"
   10317                "}",
   10318                Style);
   10319 
   10320   // Don't indent case labels.
   10321   verifyFormat("switch (variable) {\n"
   10322                "case 1:\n"
   10323                "case 2:\n"
   10324                "    doSomething();\n"
   10325                "    break;\n"
   10326                "default:\n"
   10327                "    ++variable;\n"
   10328                "}",
   10329                Style);
   10330 
   10331   // Wrap before binary operators.
   10332   EXPECT_EQ("void f()\n"
   10333             "{\n"
   10334             "    if (aaaaaaaaaaaaaaaa\n"
   10335             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
   10336             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
   10337             "        return;\n"
   10338             "}",
   10339             format("void f() {\n"
   10340                    "if (aaaaaaaaaaaaaaaa\n"
   10341                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
   10342                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
   10343                    "return;\n"
   10344                    "}",
   10345                    Style));
   10346 
   10347   // Allow functions on a single line.
   10348   verifyFormat("void f() { return; }", Style);
   10349 
   10350   // Constructor initializers are formatted one per line with the "," on the
   10351   // new line.
   10352   verifyFormat("Constructor()\n"
   10353                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
   10354                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
   10355                "          aaaaaaaaaaaaaa)\n"
   10356                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
   10357                "{\n"
   10358                "}",
   10359                Style);
   10360   verifyFormat("SomeClass::Constructor()\n"
   10361                "    : a(a)\n"
   10362                "{\n"
   10363                "}",
   10364                Style);
   10365   EXPECT_EQ("SomeClass::Constructor()\n"
   10366             "    : a(a)\n"
   10367             "{\n"
   10368             "}",
   10369             format("SomeClass::Constructor():a(a){}", Style));
   10370   verifyFormat("SomeClass::Constructor()\n"
   10371                "    : a(a)\n"
   10372                "    , b(b)\n"
   10373                "    , c(c)\n"
   10374                "{\n"
   10375                "}",
   10376                Style);
   10377   verifyFormat("SomeClass::Constructor()\n"
   10378                "    : a(a)\n"
   10379                "{\n"
   10380                "    foo();\n"
   10381                "    bar();\n"
   10382                "}",
   10383                Style);
   10384 
   10385   // Access specifiers should be aligned left.
   10386   verifyFormat("class C {\n"
   10387                "public:\n"
   10388                "    int i;\n"
   10389                "};",
   10390                Style);
   10391 
   10392   // Do not align comments.
   10393   verifyFormat("int a; // Do not\n"
   10394                "double b; // align comments.",
   10395                Style);
   10396 
   10397   // Do not align operands.
   10398   EXPECT_EQ("ASSERT(aaaa\n"
   10399             "    || bbbb);",
   10400             format("ASSERT ( aaaa\n||bbbb);", Style));
   10401 
   10402   // Accept input's line breaks.
   10403   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
   10404             "    || bbbbbbbbbbbbbbb) {\n"
   10405             "    i++;\n"
   10406             "}",
   10407             format("if (aaaaaaaaaaaaaaa\n"
   10408                    "|| bbbbbbbbbbbbbbb) { i++; }",
   10409                    Style));
   10410   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
   10411             "    i++;\n"
   10412             "}",
   10413             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
   10414 
   10415   // Don't automatically break all macro definitions (llvm.org/PR17842).
   10416   verifyFormat("#define aNumber 10", Style);
   10417   // However, generally keep the line breaks that the user authored.
   10418   EXPECT_EQ("#define aNumber \\\n"
   10419             "    10",
   10420             format("#define aNumber \\\n"
   10421                    " 10",
   10422                    Style));
   10423 
   10424   // Keep empty and one-element array literals on a single line.
   10425   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
   10426             "                                  copyItems:YES];",
   10427             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
   10428                    "copyItems:YES];",
   10429                    Style));
   10430   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
   10431             "                                  copyItems:YES];",
   10432             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
   10433                    "             copyItems:YES];",
   10434                    Style));
   10435   // FIXME: This does not seem right, there should be more indentation before
   10436   // the array literal's entries. Nested blocks have the same problem.
   10437   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
   10438             "    @\"a\",\n"
   10439             "    @\"a\"\n"
   10440             "]\n"
   10441             "                                  copyItems:YES];",
   10442             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
   10443                    "     @\"a\",\n"
   10444                    "     @\"a\"\n"
   10445                    "     ]\n"
   10446                    "       copyItems:YES];",
   10447                    Style));
   10448   EXPECT_EQ(
   10449       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
   10450       "                                  copyItems:YES];",
   10451       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
   10452              "   copyItems:YES];",
   10453              Style));
   10454 
   10455   verifyFormat("[self.a b:c c:d];", Style);
   10456   EXPECT_EQ("[self.a b:c\n"
   10457             "        c:d];",
   10458             format("[self.a b:c\n"
   10459                    "c:d];",
   10460                    Style));
   10461 }
   10462 
   10463 TEST_F(FormatTest, FormatsLambdas) {
   10464   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
   10465   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
   10466   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
   10467   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
   10468   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
   10469   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
   10470   verifyFormat("void f() {\n"
   10471                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
   10472                "}\n");
   10473   verifyFormat("void f() {\n"
   10474                "  other(x.begin(), //\n"
   10475                "        x.end(),   //\n"
   10476                "        [&](int, int) { return 1; });\n"
   10477                "}\n");
   10478   verifyFormat("SomeFunction([]() { // A cool function...\n"
   10479                "  return 43;\n"
   10480                "});");
   10481   EXPECT_EQ("SomeFunction([]() {\n"
   10482             "#define A a\n"
   10483             "  return 43;\n"
   10484             "});",
   10485             format("SomeFunction([](){\n"
   10486                    "#define A a\n"
   10487                    "return 43;\n"
   10488                    "});"));
   10489   verifyFormat("void f() {\n"
   10490                "  SomeFunction([](decltype(x), A *a) {});\n"
   10491                "}");
   10492   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
   10493                "    [](const aaaaaaaaaa &a) { return a; });");
   10494   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
   10495                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
   10496                "});");
   10497   verifyFormat("Constructor()\n"
   10498                "    : Field([] { // comment\n"
   10499                "        int i;\n"
   10500                "      }) {}");
   10501   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
   10502                "  return some_parameter.size();\n"
   10503                "};");
   10504   verifyFormat("int i = aaaaaa ? 1 //\n"
   10505                "               : [] {\n"
   10506                "                   return 2; //\n"
   10507                "                 }();");
   10508   verifyFormat("llvm::errs() << \"number of twos is \"\n"
   10509                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
   10510                "                  return x == 2; // force break\n"
   10511                "                });");
   10512   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
   10513                "    int iiiiiiiiiiii) {\n"
   10514                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
   10515                "});",
   10516                getLLVMStyleWithColumns(60));
   10517   verifyFormat("SomeFunction({[&] {\n"
   10518                "                // comment\n"
   10519                "              },\n"
   10520                "              [&] {\n"
   10521                "                // comment\n"
   10522                "              }});");
   10523   verifyFormat("SomeFunction({[&] {\n"
   10524                "  // comment\n"
   10525                "}});");
   10526   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
   10527                "                             [&]() { return true; },\n"
   10528                "                         aaaaa aaaaaaaaa);");
   10529 
   10530   // Lambdas with return types.
   10531   verifyFormat("int c = []() -> int { return 2; }();\n");
   10532   verifyFormat("int c = []() -> int * { return 2; }();\n");
   10533   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
   10534   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
   10535   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
   10536   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
   10537   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
   10538   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
   10539   verifyFormat("[a, a]() -> a<1> {};");
   10540   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
   10541                "                   int j) -> int {\n"
   10542                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
   10543                "};");
   10544   verifyFormat(
   10545       "aaaaaaaaaaaaaaaaaaaaaa(\n"
   10546       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
   10547       "      return aaaaaaaaaaaaaaaaa;\n"
   10548       "    });",
   10549       getLLVMStyleWithColumns(70));
   10550 
   10551   // Multiple lambdas in the same parentheses change indentation rules.
   10552   verifyFormat("SomeFunction(\n"
   10553                "    []() {\n"
   10554                "      int i = 42;\n"
   10555                "      return i;\n"
   10556                "    },\n"
   10557                "    []() {\n"
   10558                "      int j = 43;\n"
   10559                "      return j;\n"
   10560                "    });");
   10561 
   10562   // More complex introducers.
   10563   verifyFormat("return [i, args...] {};");
   10564 
   10565   // Not lambdas.
   10566   verifyFormat("constexpr char hello[]{\"hello\"};");
   10567   verifyFormat("double &operator[](int i) { return 0; }\n"
   10568                "int i;");
   10569   verifyFormat("std::unique_ptr<int[]> foo() {}");
   10570   verifyFormat("int i = a[a][a]->f();");
   10571   verifyFormat("int i = (*b)[a]->f();");
   10572 
   10573   // Other corner cases.
   10574   verifyFormat("void f() {\n"
   10575                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
   10576                "      );\n"
   10577                "}");
   10578 
   10579   // Lambdas created through weird macros.
   10580   verifyFormat("void f() {\n"
   10581                "  MACRO((const AA &a) { return 1; });\n"
   10582                "}");
   10583 
   10584   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
   10585                "      doo_dah();\n"
   10586                "      doo_dah();\n"
   10587                "    })) {\n"
   10588                "}");
   10589   verifyFormat("auto lambda = []() {\n"
   10590                "  int a = 2\n"
   10591                "#if A\n"
   10592                "          + 2\n"
   10593                "#endif\n"
   10594                "      ;\n"
   10595                "};");
   10596 }
   10597 
   10598 TEST_F(FormatTest, FormatsBlocks) {
   10599   FormatStyle ShortBlocks = getLLVMStyle();
   10600   ShortBlocks.AllowShortBlocksOnASingleLine = true;
   10601   verifyFormat("int (^Block)(int, int);", ShortBlocks);
   10602   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
   10603   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
   10604   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
   10605   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
   10606   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
   10607 
   10608   verifyFormat("foo(^{ bar(); });", ShortBlocks);
   10609   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
   10610   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
   10611 
   10612   verifyFormat("[operation setCompletionBlock:^{\n"
   10613                "  [self onOperationDone];\n"
   10614                "}];");
   10615   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
   10616                "  [self onOperationDone];\n"
   10617                "}]};");
   10618   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
   10619                "  f();\n"
   10620                "}];");
   10621   verifyFormat("int a = [operation block:^int(int *i) {\n"
   10622                "  return 1;\n"
   10623                "}];");
   10624   verifyFormat("[myObject doSomethingWith:arg1\n"
   10625                "                      aaa:^int(int *a) {\n"
   10626                "                        return 1;\n"
   10627                "                      }\n"
   10628                "                      bbb:f(a * bbbbbbbb)];");
   10629 
   10630   verifyFormat("[operation setCompletionBlock:^{\n"
   10631                "  [self.delegate newDataAvailable];\n"
   10632                "}];",
   10633                getLLVMStyleWithColumns(60));
   10634   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
   10635                "  NSString *path = [self sessionFilePath];\n"
   10636                "  if (path) {\n"
   10637                "    // ...\n"
   10638                "  }\n"
   10639                "});");
   10640   verifyFormat("[[SessionService sharedService]\n"
   10641                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
   10642                "      if (window) {\n"
   10643                "        [self windowDidLoad:window];\n"
   10644                "      } else {\n"
   10645                "        [self errorLoadingWindow];\n"
   10646                "      }\n"
   10647                "    }];");
   10648   verifyFormat("void (^largeBlock)(void) = ^{\n"
   10649                "  // ...\n"
   10650                "};\n",
   10651                getLLVMStyleWithColumns(40));
   10652   verifyFormat("[[SessionService sharedService]\n"
   10653                "    loadWindowWithCompletionBlock: //\n"
   10654                "        ^(SessionWindow *window) {\n"
   10655                "          if (window) {\n"
   10656                "            [self windowDidLoad:window];\n"
   10657                "          } else {\n"
   10658                "            [self errorLoadingWindow];\n"
   10659                "          }\n"
   10660                "        }];",
   10661                getLLVMStyleWithColumns(60));
   10662   verifyFormat("[myObject doSomethingWith:arg1\n"
   10663                "    firstBlock:^(Foo *a) {\n"
   10664                "      // ...\n"
   10665                "      int i;\n"
   10666                "    }\n"
   10667                "    secondBlock:^(Bar *b) {\n"
   10668                "      // ...\n"
   10669                "      int i;\n"
   10670                "    }\n"
   10671                "    thirdBlock:^Foo(Bar *b) {\n"
   10672                "      // ...\n"
   10673                "      int i;\n"
   10674                "    }];");
   10675   verifyFormat("[myObject doSomethingWith:arg1\n"
   10676                "               firstBlock:-1\n"
   10677                "              secondBlock:^(Bar *b) {\n"
   10678                "                // ...\n"
   10679                "                int i;\n"
   10680                "              }];");
   10681 
   10682   verifyFormat("f(^{\n"
   10683                "  @autoreleasepool {\n"
   10684                "    if (a) {\n"
   10685                "      g();\n"
   10686                "    }\n"
   10687                "  }\n"
   10688                "});");
   10689   verifyFormat("Block b = ^int *(A *a, B *b) {}");
   10690 
   10691   FormatStyle FourIndent = getLLVMStyle();
   10692   FourIndent.ObjCBlockIndentWidth = 4;
   10693   verifyFormat("[operation setCompletionBlock:^{\n"
   10694                "    [self onOperationDone];\n"
   10695                "}];",
   10696                FourIndent);
   10697 }
   10698 
   10699 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
   10700   FormatStyle ZeroColumn = getLLVMStyle();
   10701   ZeroColumn.ColumnLimit = 0;
   10702 
   10703   verifyFormat("[[SessionService sharedService] "
   10704                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
   10705                "  if (window) {\n"
   10706                "    [self windowDidLoad:window];\n"
   10707                "  } else {\n"
   10708                "    [self errorLoadingWindow];\n"
   10709                "  }\n"
   10710                "}];",
   10711                ZeroColumn);
   10712   EXPECT_EQ("[[SessionService sharedService]\n"
   10713             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
   10714             "      if (window) {\n"
   10715             "        [self windowDidLoad:window];\n"
   10716             "      } else {\n"
   10717             "        [self errorLoadingWindow];\n"
   10718             "      }\n"
   10719             "    }];",
   10720             format("[[SessionService sharedService]\n"
   10721                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
   10722                    "                if (window) {\n"
   10723                    "    [self windowDidLoad:window];\n"
   10724                    "  } else {\n"
   10725                    "    [self errorLoadingWindow];\n"
   10726                    "  }\n"
   10727                    "}];",
   10728                    ZeroColumn));
   10729   verifyFormat("[myObject doSomethingWith:arg1\n"
   10730                "    firstBlock:^(Foo *a) {\n"
   10731                "      // ...\n"
   10732                "      int i;\n"
   10733                "    }\n"
   10734                "    secondBlock:^(Bar *b) {\n"
   10735                "      // ...\n"
   10736                "      int i;\n"
   10737                "    }\n"
   10738                "    thirdBlock:^Foo(Bar *b) {\n"
   10739                "      // ...\n"
   10740                "      int i;\n"
   10741                "    }];",
   10742                ZeroColumn);
   10743   verifyFormat("f(^{\n"
   10744                "  @autoreleasepool {\n"
   10745                "    if (a) {\n"
   10746                "      g();\n"
   10747                "    }\n"
   10748                "  }\n"
   10749                "});",
   10750                ZeroColumn);
   10751   verifyFormat("void (^largeBlock)(void) = ^{\n"
   10752                "  // ...\n"
   10753                "};",
   10754                ZeroColumn);
   10755 
   10756   ZeroColumn.AllowShortBlocksOnASingleLine = true;
   10757   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
   10758             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
   10759   ZeroColumn.AllowShortBlocksOnASingleLine = false;
   10760   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
   10761             "  int i;\n"
   10762             "};",
   10763             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
   10764 }
   10765 
   10766 TEST_F(FormatTest, SupportsCRLF) {
   10767   EXPECT_EQ("int a;\r\n"
   10768             "int b;\r\n"
   10769             "int c;\r\n",
   10770             format("int a;\r\n"
   10771                    "  int b;\r\n"
   10772                    "    int c;\r\n",
   10773                    getLLVMStyle()));
   10774   EXPECT_EQ("int a;\r\n"
   10775             "int b;\r\n"
   10776             "int c;\r\n",
   10777             format("int a;\r\n"
   10778                    "  int b;\n"
   10779                    "    int c;\r\n",
   10780                    getLLVMStyle()));
   10781   EXPECT_EQ("int a;\n"
   10782             "int b;\n"
   10783             "int c;\n",
   10784             format("int a;\r\n"
   10785                    "  int b;\n"
   10786                    "    int c;\n",
   10787                    getLLVMStyle()));
   10788   EXPECT_EQ("\"aaaaaaa \"\r\n"
   10789             "\"bbbbbbb\";\r\n",
   10790             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
   10791   EXPECT_EQ("#define A \\\r\n"
   10792             "  b;      \\\r\n"
   10793             "  c;      \\\r\n"
   10794             "  d;\r\n",
   10795             format("#define A \\\r\n"
   10796                    "  b; \\\r\n"
   10797                    "  c; d; \r\n",
   10798                    getGoogleStyle()));
   10799 
   10800   EXPECT_EQ("/*\r\n"
   10801             "multi line block comments\r\n"
   10802             "should not introduce\r\n"
   10803             "an extra carriage return\r\n"
   10804             "*/\r\n",
   10805             format("/*\r\n"
   10806                    "multi line block comments\r\n"
   10807                    "should not introduce\r\n"
   10808                    "an extra carriage return\r\n"
   10809                    "*/\r\n"));
   10810 }
   10811 
   10812 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
   10813   verifyFormat("MY_CLASS(C) {\n"
   10814                "  int i;\n"
   10815                "  int j;\n"
   10816                "};");
   10817 }
   10818 
   10819 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
   10820   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
   10821   TwoIndent.ContinuationIndentWidth = 2;
   10822 
   10823   EXPECT_EQ("int i =\n"
   10824             "  longFunction(\n"
   10825             "    arg);",
   10826             format("int i = longFunction(arg);", TwoIndent));
   10827 
   10828   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
   10829   SixIndent.ContinuationIndentWidth = 6;
   10830 
   10831   EXPECT_EQ("int i =\n"
   10832             "      longFunction(\n"
   10833             "            arg);",
   10834             format("int i = longFunction(arg);", SixIndent));
   10835 }
   10836 
   10837 TEST_F(FormatTest, SpacesInAngles) {
   10838   FormatStyle Spaces = getLLVMStyle();
   10839   Spaces.SpacesInAngles = true;
   10840 
   10841   verifyFormat("static_cast< int >(arg);", Spaces);
   10842   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
   10843   verifyFormat("f< int, float >();", Spaces);
   10844   verifyFormat("template <> g() {}", Spaces);
   10845   verifyFormat("template < std::vector< int > > f() {}", Spaces);
   10846   verifyFormat("std::function< void(int, int) > fct;", Spaces);
   10847   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
   10848                Spaces);
   10849 
   10850   Spaces.Standard = FormatStyle::LS_Cpp03;
   10851   Spaces.SpacesInAngles = true;
   10852   verifyFormat("A< A< int > >();", Spaces);
   10853 
   10854   Spaces.SpacesInAngles = false;
   10855   verifyFormat("A<A<int> >();", Spaces);
   10856 
   10857   Spaces.Standard = FormatStyle::LS_Cpp11;
   10858   Spaces.SpacesInAngles = true;
   10859   verifyFormat("A< A< int > >();", Spaces);
   10860 
   10861   Spaces.SpacesInAngles = false;
   10862   verifyFormat("A<A<int>>();", Spaces);
   10863 }
   10864 
   10865 TEST_F(FormatTest, TripleAngleBrackets) {
   10866   verifyFormat("f<<<1, 1>>>();");
   10867   verifyFormat("f<<<1, 1, 1, s>>>();");
   10868   verifyFormat("f<<<a, b, c, d>>>();");
   10869   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
   10870   verifyFormat("f<param><<<1, 1>>>();");
   10871   verifyFormat("f<1><<<1, 1>>>();");
   10872   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
   10873   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
   10874                "aaaaaaaaaaa<<<\n    1, 1>>>();");
   10875 }
   10876 
   10877 TEST_F(FormatTest, MergeLessLessAtEnd) {
   10878   verifyFormat("<<");
   10879   EXPECT_EQ("< < <", format("\\\n<<<"));
   10880   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
   10881                "aaallvm::outs() <<");
   10882   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
   10883                "aaaallvm::outs()\n    <<");
   10884 }
   10885 
   10886 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
   10887   std::string code = "#if A\n"
   10888                      "#if B\n"
   10889                      "a.\n"
   10890                      "#endif\n"
   10891                      "    a = 1;\n"
   10892                      "#else\n"
   10893                      "#endif\n"
   10894                      "#if C\n"
   10895                      "#else\n"
   10896                      "#endif\n";
   10897   EXPECT_EQ(code, format(code));
   10898 }
   10899 
   10900 TEST_F(FormatTest, HandleConflictMarkers) {
   10901   // Git/SVN conflict markers.
   10902   EXPECT_EQ("int a;\n"
   10903             "void f() {\n"
   10904             "  callme(some(parameter1,\n"
   10905             "<<<<<<< text by the vcs\n"
   10906             "              parameter2),\n"
   10907             "||||||| text by the vcs\n"
   10908             "              parameter2),\n"
   10909             "         parameter3,\n"
   10910             "======= text by the vcs\n"
   10911             "              parameter2, parameter3),\n"
   10912             ">>>>>>> text by the vcs\n"
   10913             "         otherparameter);\n",
   10914             format("int a;\n"
   10915                    "void f() {\n"
   10916                    "  callme(some(parameter1,\n"
   10917                    "<<<<<<< text by the vcs\n"
   10918                    "  parameter2),\n"
   10919                    "||||||| text by the vcs\n"
   10920                    "  parameter2),\n"
   10921                    "  parameter3,\n"
   10922                    "======= text by the vcs\n"
   10923                    "  parameter2,\n"
   10924                    "  parameter3),\n"
   10925                    ">>>>>>> text by the vcs\n"
   10926                    "  otherparameter);\n"));
   10927 
   10928   // Perforce markers.
   10929   EXPECT_EQ("void f() {\n"
   10930             "  function(\n"
   10931             ">>>> text by the vcs\n"
   10932             "      parameter,\n"
   10933             "==== text by the vcs\n"
   10934             "      parameter,\n"
   10935             "==== text by the vcs\n"
   10936             "      parameter,\n"
   10937             "<<<< text by the vcs\n"
   10938             "      parameter);\n",
   10939             format("void f() {\n"
   10940                    "  function(\n"
   10941                    ">>>> text by the vcs\n"
   10942                    "  parameter,\n"
   10943                    "==== text by the vcs\n"
   10944                    "  parameter,\n"
   10945                    "==== text by the vcs\n"
   10946                    "  parameter,\n"
   10947                    "<<<< text by the vcs\n"
   10948                    "  parameter);\n"));
   10949 
   10950   EXPECT_EQ("<<<<<<<\n"
   10951             "|||||||\n"
   10952             "=======\n"
   10953             ">>>>>>>",
   10954             format("<<<<<<<\n"
   10955                    "|||||||\n"
   10956                    "=======\n"
   10957                    ">>>>>>>"));
   10958 
   10959   EXPECT_EQ("<<<<<<<\n"
   10960             "|||||||\n"
   10961             "int i;\n"
   10962             "=======\n"
   10963             ">>>>>>>",
   10964             format("<<<<<<<\n"
   10965                    "|||||||\n"
   10966                    "int i;\n"
   10967                    "=======\n"
   10968                    ">>>>>>>"));
   10969 
   10970   // FIXME: Handle parsing of macros around conflict markers correctly:
   10971   EXPECT_EQ("#define Macro \\\n"
   10972             "<<<<<<<\n"
   10973             "Something \\\n"
   10974             "|||||||\n"
   10975             "Else \\\n"
   10976             "=======\n"
   10977             "Other \\\n"
   10978             ">>>>>>>\n"
   10979             "    End int i;\n",
   10980             format("#define Macro \\\n"
   10981                    "<<<<<<<\n"
   10982                    "  Something \\\n"
   10983                    "|||||||\n"
   10984                    "  Else \\\n"
   10985                    "=======\n"
   10986                    "  Other \\\n"
   10987                    ">>>>>>>\n"
   10988                    "  End\n"
   10989                    "int i;\n"));
   10990 }
   10991 
   10992 TEST_F(FormatTest, DisableRegions) {
   10993   EXPECT_EQ("int i;\n"
   10994             "// clang-format off\n"
   10995             "  int j;\n"
   10996             "// clang-format on\n"
   10997             "int k;",
   10998             format(" int  i;\n"
   10999                    "   // clang-format off\n"
   11000                    "  int j;\n"
   11001                    " // clang-format on\n"
   11002                    "   int   k;"));
   11003   EXPECT_EQ("int i;\n"
   11004             "/* clang-format off */\n"
   11005             "  int j;\n"
   11006             "/* clang-format on */\n"
   11007             "int k;",
   11008             format(" int  i;\n"
   11009                    "   /* clang-format off */\n"
   11010                    "  int j;\n"
   11011                    " /* clang-format on */\n"
   11012                    "   int   k;"));
   11013 }
   11014 
   11015 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
   11016   format("? ) =");
   11017   verifyNoCrash("#define a\\\n /**/}");
   11018 }
   11019 
   11020 } // end namespace
   11021 } // end namespace format
   11022 } // end namespace clang
   11023