1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_ExpectComplete) {
42     LLVM_DEBUG(llvm::errs() << "---\n");
43     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59 
60   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
66     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
67   }
68 
69   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
70     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
71   }
72 
73   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
74                     const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Expected.str(), format(Expected, Style))
76         << "Expected code is not stable";
77     EXPECT_EQ(Expected.str(), format(Code, Style));
78     if (Style.Language == FormatStyle::LK_Cpp) {
79       // Objective-C++ is a superset of C++, so everything checked for C++
80       // needs to be checked for Objective-C++ as well.
81       FormatStyle ObjCStyle = Style;
82       ObjCStyle.Language = FormatStyle::LK_ObjC;
83       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
84     }
85   }
86 
87   void verifyFormat(llvm::StringRef Code,
88                     const FormatStyle &Style = getLLVMStyle()) {
89     verifyFormat(Code, test::messUp(Code), Style);
90   }
91 
92   void verifyIncompleteFormat(llvm::StringRef Code,
93                               const FormatStyle &Style = getLLVMStyle()) {
94     EXPECT_EQ(Code.str(),
95               format(test::messUp(Code), Style, SC_ExpectIncomplete));
96   }
97 
98   void verifyGoogleFormat(llvm::StringRef Code) {
99     verifyFormat(Code, getGoogleStyle());
100   }
101 
102   void verifyIndependentOfContext(llvm::StringRef text) {
103     verifyFormat(text);
104     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
105   }
106 
107   /// \brief Verify that clang-format does not crash on the given input.
108   void verifyNoCrash(llvm::StringRef Code,
109                      const FormatStyle &Style = getLLVMStyle()) {
110     format(Code, Style, SC_DoNotCheck);
111   }
112 
113   int ReplacementCount;
114 };
115 
116 TEST_F(FormatTest, MessUp) {
117   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
118   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
119   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
120   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
121   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
122 }
123 
124 //===----------------------------------------------------------------------===//
125 // Basic function tests.
126 //===----------------------------------------------------------------------===//
127 
128 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
129   EXPECT_EQ(";", format(";"));
130 }
131 
132 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
133   EXPECT_EQ("int i;", format("  int i;"));
134   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
135   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
136   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
137 }
138 
139 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
140   EXPECT_EQ("int i;", format("int\ni;"));
141 }
142 
143 TEST_F(FormatTest, FormatsNestedBlockStatements) {
144   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
145 }
146 
147 TEST_F(FormatTest, FormatsNestedCall) {
148   verifyFormat("Method(f1, f2(f3));");
149   verifyFormat("Method(f1(f2, f3()));");
150   verifyFormat("Method(f1(f2, (f3())));");
151 }
152 
153 TEST_F(FormatTest, NestedNameSpecifiers) {
154   verifyFormat("vector<::Type> v;");
155   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
156   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
157   verifyFormat("bool a = 2 < ::SomeFunction();");
158   verifyFormat("ALWAYS_INLINE ::std::string getName();");
159   verifyFormat("some::string getName();");
160 }
161 
162 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
163   EXPECT_EQ("if (a) {\n"
164             "  f();\n"
165             "}",
166             format("if(a){f();}"));
167   EXPECT_EQ(4, ReplacementCount);
168   EXPECT_EQ("if (a) {\n"
169             "  f();\n"
170             "}",
171             format("if (a) {\n"
172                    "  f();\n"
173                    "}"));
174   EXPECT_EQ(0, ReplacementCount);
175   EXPECT_EQ("/*\r\n"
176             "\r\n"
177             "*/\r\n",
178             format("/*\r\n"
179                    "\r\n"
180                    "*/\r\n"));
181   EXPECT_EQ(0, ReplacementCount);
182 }
183 
184 TEST_F(FormatTest, RemovesEmptyLines) {
185   EXPECT_EQ("class C {\n"
186             "  int i;\n"
187             "};",
188             format("class C {\n"
189                    " int i;\n"
190                    "\n"
191                    "};"));
192 
193   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
194   EXPECT_EQ("namespace N {\n"
195             "\n"
196             "int i;\n"
197             "}",
198             format("namespace N {\n"
199                    "\n"
200                    "int    i;\n"
201                    "}",
202                    getGoogleStyle()));
203   EXPECT_EQ("/* something */ namespace N {\n"
204             "\n"
205             "int i;\n"
206             "}",
207             format("/* something */ namespace N {\n"
208                    "\n"
209                    "int    i;\n"
210                    "}",
211                    getGoogleStyle()));
212   EXPECT_EQ("inline namespace N {\n"
213             "\n"
214             "int i;\n"
215             "}",
216             format("inline namespace N {\n"
217                    "\n"
218                    "int    i;\n"
219                    "}",
220                    getGoogleStyle()));
221   EXPECT_EQ("/* something */ inline namespace N {\n"
222             "\n"
223             "int i;\n"
224             "}",
225             format("/* something */ inline namespace N {\n"
226                    "\n"
227                    "int    i;\n"
228                    "}",
229                    getGoogleStyle()));
230   EXPECT_EQ("export namespace N {\n"
231             "\n"
232             "int i;\n"
233             "}",
234             format("export namespace N {\n"
235                    "\n"
236                    "int    i;\n"
237                    "}",
238                    getGoogleStyle()));
239   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
240             "\n"
241             "int i;\n"
242             "}",
243             format("extern /**/ \"C\" /**/ {\n"
244                    "\n"
245                    "int    i;\n"
246                    "}",
247                    getGoogleStyle()));
248 
249   // ...but do keep inlining and removing empty lines for non-block extern "C"
250   // functions.
251   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
252   EXPECT_EQ("extern \"C\" int f() {\n"
253             "  int i = 42;\n"
254             "  return i;\n"
255             "}",
256             format("extern \"C\" int f() {\n"
257                    "\n"
258                    "  int i = 42;\n"
259                    "  return i;\n"
260                    "}",
261                    getGoogleStyle()));
262 
263   // Remove empty lines at the beginning and end of blocks.
264   EXPECT_EQ("void f() {\n"
265             "\n"
266             "  if (a) {\n"
267             "\n"
268             "    f();\n"
269             "  }\n"
270             "}",
271             format("void f() {\n"
272                    "\n"
273                    "  if (a) {\n"
274                    "\n"
275                    "    f();\n"
276                    "\n"
277                    "  }\n"
278                    "\n"
279                    "}",
280                    getLLVMStyle()));
281   EXPECT_EQ("void f() {\n"
282             "  if (a) {\n"
283             "    f();\n"
284             "  }\n"
285             "}",
286             format("void f() {\n"
287                    "\n"
288                    "  if (a) {\n"
289                    "\n"
290                    "    f();\n"
291                    "\n"
292                    "  }\n"
293                    "\n"
294                    "}",
295                    getGoogleStyle()));
296 
297   // Don't remove empty lines in more complex control statements.
298   EXPECT_EQ("void f() {\n"
299             "  if (a) {\n"
300             "    f();\n"
301             "\n"
302             "  } else if (b) {\n"
303             "    f();\n"
304             "  }\n"
305             "}",
306             format("void f() {\n"
307                    "  if (a) {\n"
308                    "    f();\n"
309                    "\n"
310                    "  } else if (b) {\n"
311                    "    f();\n"
312                    "\n"
313                    "  }\n"
314                    "\n"
315                    "}"));
316 
317   // Don't remove empty lines before namespace endings.
318   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
319   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
320   EXPECT_EQ("namespace {\n"
321             "int i;\n"
322             "\n"
323             "}",
324             format("namespace {\n"
325                    "int i;\n"
326                    "\n"
327                    "}", LLVMWithNoNamespaceFix));
328   EXPECT_EQ("namespace {\n"
329             "int i;\n"
330             "}",
331             format("namespace {\n"
332                    "int i;\n"
333                    "}", LLVMWithNoNamespaceFix));
334   EXPECT_EQ("namespace {\n"
335             "int i;\n"
336             "\n"
337             "};",
338             format("namespace {\n"
339                    "int i;\n"
340                    "\n"
341                    "};", LLVMWithNoNamespaceFix));
342   EXPECT_EQ("namespace {\n"
343             "int i;\n"
344             "};",
345             format("namespace {\n"
346                    "int i;\n"
347                    "};", LLVMWithNoNamespaceFix));
348   EXPECT_EQ("namespace {\n"
349             "int i;\n"
350             "\n"
351             "}",
352             format("namespace {\n"
353                    "int i;\n"
354                    "\n"
355                    "}"));
356   EXPECT_EQ("namespace {\n"
357             "int i;\n"
358             "\n"
359             "} // namespace",
360             format("namespace {\n"
361                    "int i;\n"
362                    "\n"
363                    "}  // namespace"));
364 
365   FormatStyle Style = getLLVMStyle();
366   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
367   Style.MaxEmptyLinesToKeep = 2;
368   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
369   Style.BraceWrapping.AfterClass = true;
370   Style.BraceWrapping.AfterFunction = true;
371   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
372 
373   EXPECT_EQ("class Foo\n"
374             "{\n"
375             "  Foo() {}\n"
376             "\n"
377             "  void funk() {}\n"
378             "};",
379             format("class Foo\n"
380                    "{\n"
381                    "  Foo()\n"
382                    "  {\n"
383                    "  }\n"
384                    "\n"
385                    "  void funk() {}\n"
386                    "};",
387                    Style));
388 }
389 
390 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
391   verifyFormat("x = (a) and (b);");
392   verifyFormat("x = (a) or (b);");
393   verifyFormat("x = (a) bitand (b);");
394   verifyFormat("x = (a) bitor (b);");
395   verifyFormat("x = (a) not_eq (b);");
396   verifyFormat("x = (a) and_eq (b);");
397   verifyFormat("x = (a) or_eq (b);");
398   verifyFormat("x = (a) xor (b);");
399 }
400 
401 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
402   verifyFormat("x = compl(a);");
403   verifyFormat("x = not(a);");
404   verifyFormat("x = bitand(a);");
405   // Unary operator must not be merged with the next identifier
406   verifyFormat("x = compl a;");
407   verifyFormat("x = not a;");
408   verifyFormat("x = bitand a;");
409 }
410 
411 //===----------------------------------------------------------------------===//
412 // Tests for control statements.
413 //===----------------------------------------------------------------------===//
414 
415 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
416   verifyFormat("if (true)\n  f();\ng();");
417   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
418   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
419   verifyFormat("if constexpr (true)\n"
420                "  f();\ng();");
421   verifyFormat("if constexpr (a)\n"
422                "  if constexpr (b)\n"
423                "    if constexpr (c)\n"
424                "      g();\n"
425                "h();");
426   verifyFormat("if constexpr (a)\n"
427                "  if constexpr (b) {\n"
428                "    f();\n"
429                "  }\n"
430                "g();");
431 
432   FormatStyle AllowsMergedIf = getLLVMStyle();
433   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
434   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
435   verifyFormat("if (a)\n"
436                "  // comment\n"
437                "  f();",
438                AllowsMergedIf);
439   verifyFormat("{\n"
440                "  if (a)\n"
441                "  label:\n"
442                "    f();\n"
443                "}",
444                AllowsMergedIf);
445   verifyFormat("#define A \\\n"
446                "  if (a)  \\\n"
447                "  label:  \\\n"
448                "    f()",
449                AllowsMergedIf);
450   verifyFormat("if (a)\n"
451                "  ;",
452                AllowsMergedIf);
453   verifyFormat("if (a)\n"
454                "  if (b) return;",
455                AllowsMergedIf);
456 
457   verifyFormat("if (a) // Can't merge this\n"
458                "  f();\n",
459                AllowsMergedIf);
460   verifyFormat("if (a) /* still don't merge */\n"
461                "  f();",
462                AllowsMergedIf);
463   verifyFormat("if (a) { // Never merge this\n"
464                "  f();\n"
465                "}",
466                AllowsMergedIf);
467   verifyFormat("if (a) { /* Never merge this */\n"
468                "  f();\n"
469                "}",
470                AllowsMergedIf);
471 
472   AllowsMergedIf.ColumnLimit = 14;
473   verifyFormat("if (a) return;", AllowsMergedIf);
474   verifyFormat("if (aaaaaaaaa)\n"
475                "  return;",
476                AllowsMergedIf);
477 
478   AllowsMergedIf.ColumnLimit = 13;
479   verifyFormat("if (a)\n  return;", AllowsMergedIf);
480 }
481 
482 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
483   FormatStyle AllowsMergedLoops = getLLVMStyle();
484   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
485   verifyFormat("while (true) continue;", AllowsMergedLoops);
486   verifyFormat("for (;;) continue;", AllowsMergedLoops);
487   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
488   verifyFormat("while (true)\n"
489                "  ;",
490                AllowsMergedLoops);
491   verifyFormat("for (;;)\n"
492                "  ;",
493                AllowsMergedLoops);
494   verifyFormat("for (;;)\n"
495                "  for (;;) continue;",
496                AllowsMergedLoops);
497   verifyFormat("for (;;) // Can't merge this\n"
498                "  continue;",
499                AllowsMergedLoops);
500   verifyFormat("for (;;) /* still don't merge */\n"
501                "  continue;",
502                AllowsMergedLoops);
503 }
504 
505 TEST_F(FormatTest, FormatShortBracedStatements) {
506   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
507   AllowSimpleBracedStatements.ColumnLimit = 40;
508   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
509 
510   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
511   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
512 
513   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
514   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
515   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
516 
517   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
518   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
519   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
520   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
521   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
522   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
523   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
524   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
525   verifyFormat("if (true) {\n"
526                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
527                "}",
528                AllowSimpleBracedStatements);
529   verifyFormat("if (true) { //\n"
530                "  f();\n"
531                "}",
532                AllowSimpleBracedStatements);
533   verifyFormat("if (true) {\n"
534                "  f();\n"
535                "  f();\n"
536                "}",
537                AllowSimpleBracedStatements);
538   verifyFormat("if (true) {\n"
539                "  f();\n"
540                "} else {\n"
541                "  f();\n"
542                "}",
543                AllowSimpleBracedStatements);
544 
545   verifyFormat("struct A2 {\n"
546                "  int X;\n"
547                "};",
548                AllowSimpleBracedStatements);
549   verifyFormat("typedef struct A2 {\n"
550                "  int X;\n"
551                "} A2_t;",
552                AllowSimpleBracedStatements);
553   verifyFormat("template <int> struct A2 {\n"
554                "  struct B {};\n"
555                "};",
556                AllowSimpleBracedStatements);
557 
558   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
559   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
560   verifyFormat("if (true) {\n"
561                "  f();\n"
562                "}",
563                AllowSimpleBracedStatements);
564   verifyFormat("if (true) {\n"
565                "  f();\n"
566                "} else {\n"
567                "  f();\n"
568                "}",
569                AllowSimpleBracedStatements);
570 
571   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
572   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
573   verifyFormat("while (true) {\n"
574                "  f();\n"
575                "}",
576                AllowSimpleBracedStatements);
577   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
578   verifyFormat("for (;;) {\n"
579                "  f();\n"
580                "}",
581                AllowSimpleBracedStatements);
582 
583   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
584   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
585   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
586 
587   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
588   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
589   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
590   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
591   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
592   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
593   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
594   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
595   verifyFormat("if (true)\n"
596                "{\n"
597                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
598                "}",
599                AllowSimpleBracedStatements);
600   verifyFormat("if (true)\n"
601                "{ //\n"
602                "  f();\n"
603                "}",
604                AllowSimpleBracedStatements);
605   verifyFormat("if (true)\n"
606                "{\n"
607                "  f();\n"
608                "  f();\n"
609                "}",
610                AllowSimpleBracedStatements);
611   verifyFormat("if (true)\n"
612                "{\n"
613                "  f();\n"
614                "} else\n"
615                "{\n"
616                "  f();\n"
617                "}",
618                AllowSimpleBracedStatements);
619 
620   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
621   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
622   verifyFormat("if (true)\n"
623                "{\n"
624                "  f();\n"
625                "}",
626                AllowSimpleBracedStatements);
627   verifyFormat("if (true)\n"
628                "{\n"
629                "  f();\n"
630                "} else\n"
631                "{\n"
632                "  f();\n"
633                "}",
634                AllowSimpleBracedStatements);
635 
636   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
637   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
638   verifyFormat("while (true)\n"
639                "{\n"
640                "  f();\n"
641                "}",
642                AllowSimpleBracedStatements);
643   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
644   verifyFormat("for (;;)\n"
645                "{\n"
646                "  f();\n"
647                "}",
648                AllowSimpleBracedStatements);
649 }
650 
651 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
652   FormatStyle Style = getLLVMStyleWithColumns(60);
653   Style.AllowShortBlocksOnASingleLine = true;
654   Style.AllowShortIfStatementsOnASingleLine = true;
655   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
656   EXPECT_EQ("#define A                                                  \\\n"
657             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
658             "  { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n"
659             "X;",
660             format("#define A \\\n"
661                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
662                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
663                    "   }\n"
664                    "X;",
665                    Style));
666 }
667 
668 TEST_F(FormatTest, ParseIfElse) {
669   verifyFormat("if (true)\n"
670                "  if (true)\n"
671                "    if (true)\n"
672                "      f();\n"
673                "    else\n"
674                "      g();\n"
675                "  else\n"
676                "    h();\n"
677                "else\n"
678                "  i();");
679   verifyFormat("if (true)\n"
680                "  if (true)\n"
681                "    if (true) {\n"
682                "      if (true)\n"
683                "        f();\n"
684                "    } else {\n"
685                "      g();\n"
686                "    }\n"
687                "  else\n"
688                "    h();\n"
689                "else {\n"
690                "  i();\n"
691                "}");
692   verifyFormat("if (true)\n"
693                "  if constexpr (true)\n"
694                "    if (true) {\n"
695                "      if constexpr (true)\n"
696                "        f();\n"
697                "    } else {\n"
698                "      g();\n"
699                "    }\n"
700                "  else\n"
701                "    h();\n"
702                "else {\n"
703                "  i();\n"
704                "}");
705   verifyFormat("void f() {\n"
706                "  if (a) {\n"
707                "  } else {\n"
708                "  }\n"
709                "}");
710 }
711 
712 TEST_F(FormatTest, ElseIf) {
713   verifyFormat("if (a) {\n} else if (b) {\n}");
714   verifyFormat("if (a)\n"
715                "  f();\n"
716                "else if (b)\n"
717                "  g();\n"
718                "else\n"
719                "  h();");
720   verifyFormat("if constexpr (a)\n"
721                "  f();\n"
722                "else if constexpr (b)\n"
723                "  g();\n"
724                "else\n"
725                "  h();");
726   verifyFormat("if (a) {\n"
727                "  f();\n"
728                "}\n"
729                "// or else ..\n"
730                "else {\n"
731                "  g()\n"
732                "}");
733 
734   verifyFormat("if (a) {\n"
735                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
736                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
737                "}");
738   verifyFormat("if (a) {\n"
739                "} else if (\n"
740                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
741                "}",
742                getLLVMStyleWithColumns(62));
743   verifyFormat("if (a) {\n"
744                "} else if constexpr (\n"
745                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
746                "}",
747                getLLVMStyleWithColumns(62));
748 }
749 
750 TEST_F(FormatTest, FormatsForLoop) {
751   verifyFormat(
752       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
753       "     ++VeryVeryLongLoopVariable)\n"
754       "  ;");
755   verifyFormat("for (;;)\n"
756                "  f();");
757   verifyFormat("for (;;) {\n}");
758   verifyFormat("for (;;) {\n"
759                "  f();\n"
760                "}");
761   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
762 
763   verifyFormat(
764       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
765       "                                          E = UnwrappedLines.end();\n"
766       "     I != E; ++I) {\n}");
767 
768   verifyFormat(
769       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
770       "     ++IIIII) {\n}");
771   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
772                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
773                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
774   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
775                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
776                "         E = FD->getDeclsInPrototypeScope().end();\n"
777                "     I != E; ++I) {\n}");
778   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
779                "         I = Container.begin(),\n"
780                "         E = Container.end();\n"
781                "     I != E; ++I) {\n}",
782                getLLVMStyleWithColumns(76));
783 
784   verifyFormat(
785       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
786       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
787       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
788       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
789       "     ++aaaaaaaaaaa) {\n}");
790   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
791                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
792                "     ++i) {\n}");
793   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
794                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
795                "}");
796   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
797                "         aaaaaaaaaa);\n"
798                "     iter; ++iter) {\n"
799                "}");
800   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
801                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
802                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
803                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
804 
805   // These should not be formatted as Objective-C for-in loops.
806   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
807   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
808   verifyFormat("Foo *x;\nfor (x in y) {\n}");
809   verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
810 
811   FormatStyle NoBinPacking = getLLVMStyle();
812   NoBinPacking.BinPackParameters = false;
813   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
814                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
815                "                                           aaaaaaaaaaaaaaaa,\n"
816                "                                           aaaaaaaaaaaaaaaa,\n"
817                "                                           aaaaaaaaaaaaaaaa);\n"
818                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
819                "}",
820                NoBinPacking);
821   verifyFormat(
822       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
823       "                                          E = UnwrappedLines.end();\n"
824       "     I != E;\n"
825       "     ++I) {\n}",
826       NoBinPacking);
827 
828   FormatStyle AlignLeft = getLLVMStyle();
829   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
830   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
831 }
832 
833 TEST_F(FormatTest, RangeBasedForLoops) {
834   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
835                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
836   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
837                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
838   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
839                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
840   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
841                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
842 }
843 
844 TEST_F(FormatTest, ForEachLoops) {
845   verifyFormat("void f() {\n"
846                "  foreach (Item *item, itemlist) {}\n"
847                "  Q_FOREACH (Item *item, itemlist) {}\n"
848                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
849                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
850                "}");
851 
852   // As function-like macros.
853   verifyFormat("#define foreach(x, y)\n"
854                "#define Q_FOREACH(x, y)\n"
855                "#define BOOST_FOREACH(x, y)\n"
856                "#define UNKNOWN_FOREACH(x, y)\n");
857 
858   // Not as function-like macros.
859   verifyFormat("#define foreach (x, y)\n"
860                "#define Q_FOREACH (x, y)\n"
861                "#define BOOST_FOREACH (x, y)\n"
862                "#define UNKNOWN_FOREACH (x, y)\n");
863 }
864 
865 TEST_F(FormatTest, FormatsWhileLoop) {
866   verifyFormat("while (true) {\n}");
867   verifyFormat("while (true)\n"
868                "  f();");
869   verifyFormat("while () {\n}");
870   verifyFormat("while () {\n"
871                "  f();\n"
872                "}");
873 }
874 
875 TEST_F(FormatTest, FormatsDoWhile) {
876   verifyFormat("do {\n"
877                "  do_something();\n"
878                "} while (something());");
879   verifyFormat("do\n"
880                "  do_something();\n"
881                "while (something());");
882 }
883 
884 TEST_F(FormatTest, FormatsSwitchStatement) {
885   verifyFormat("switch (x) {\n"
886                "case 1:\n"
887                "  f();\n"
888                "  break;\n"
889                "case kFoo:\n"
890                "case ns::kBar:\n"
891                "case kBaz:\n"
892                "  break;\n"
893                "default:\n"
894                "  g();\n"
895                "  break;\n"
896                "}");
897   verifyFormat("switch (x) {\n"
898                "case 1: {\n"
899                "  f();\n"
900                "  break;\n"
901                "}\n"
902                "case 2: {\n"
903                "  break;\n"
904                "}\n"
905                "}");
906   verifyFormat("switch (x) {\n"
907                "case 1: {\n"
908                "  f();\n"
909                "  {\n"
910                "    g();\n"
911                "    h();\n"
912                "  }\n"
913                "  break;\n"
914                "}\n"
915                "}");
916   verifyFormat("switch (x) {\n"
917                "case 1: {\n"
918                "  f();\n"
919                "  if (foo) {\n"
920                "    g();\n"
921                "    h();\n"
922                "  }\n"
923                "  break;\n"
924                "}\n"
925                "}");
926   verifyFormat("switch (x) {\n"
927                "case 1: {\n"
928                "  f();\n"
929                "  g();\n"
930                "} break;\n"
931                "}");
932   verifyFormat("switch (test)\n"
933                "  ;");
934   verifyFormat("switch (x) {\n"
935                "default: {\n"
936                "  // Do nothing.\n"
937                "}\n"
938                "}");
939   verifyFormat("switch (x) {\n"
940                "// comment\n"
941                "// if 1, do f()\n"
942                "case 1:\n"
943                "  f();\n"
944                "}");
945   verifyFormat("switch (x) {\n"
946                "case 1:\n"
947                "  // Do amazing stuff\n"
948                "  {\n"
949                "    f();\n"
950                "    g();\n"
951                "  }\n"
952                "  break;\n"
953                "}");
954   verifyFormat("#define A          \\\n"
955                "  switch (x) {     \\\n"
956                "  case a:          \\\n"
957                "    foo = b;       \\\n"
958                "  }",
959                getLLVMStyleWithColumns(20));
960   verifyFormat("#define OPERATION_CASE(name)           \\\n"
961                "  case OP_name:                        \\\n"
962                "    return operations::Operation##name\n",
963                getLLVMStyleWithColumns(40));
964   verifyFormat("switch (x) {\n"
965                "case 1:;\n"
966                "default:;\n"
967                "  int i;\n"
968                "}");
969 
970   verifyGoogleFormat("switch (x) {\n"
971                      "  case 1:\n"
972                      "    f();\n"
973                      "    break;\n"
974                      "  case kFoo:\n"
975                      "  case ns::kBar:\n"
976                      "  case kBaz:\n"
977                      "    break;\n"
978                      "  default:\n"
979                      "    g();\n"
980                      "    break;\n"
981                      "}");
982   verifyGoogleFormat("switch (x) {\n"
983                      "  case 1: {\n"
984                      "    f();\n"
985                      "    break;\n"
986                      "  }\n"
987                      "}");
988   verifyGoogleFormat("switch (test)\n"
989                      "  ;");
990 
991   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
992                      "  case OP_name:              \\\n"
993                      "    return operations::Operation##name\n");
994   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
995                      "  // Get the correction operation class.\n"
996                      "  switch (OpCode) {\n"
997                      "    CASE(Add);\n"
998                      "    CASE(Subtract);\n"
999                      "    default:\n"
1000                      "      return operations::Unknown;\n"
1001                      "  }\n"
1002                      "#undef OPERATION_CASE\n"
1003                      "}");
1004   verifyFormat("DEBUG({\n"
1005                "  switch (x) {\n"
1006                "  case A:\n"
1007                "    f();\n"
1008                "    break;\n"
1009                "    // fallthrough\n"
1010                "  case B:\n"
1011                "    g();\n"
1012                "    break;\n"
1013                "  }\n"
1014                "});");
1015   EXPECT_EQ("DEBUG({\n"
1016             "  switch (x) {\n"
1017             "  case A:\n"
1018             "    f();\n"
1019             "    break;\n"
1020             "  // On B:\n"
1021             "  case B:\n"
1022             "    g();\n"
1023             "    break;\n"
1024             "  }\n"
1025             "});",
1026             format("DEBUG({\n"
1027                    "  switch (x) {\n"
1028                    "  case A:\n"
1029                    "    f();\n"
1030                    "    break;\n"
1031                    "  // On B:\n"
1032                    "  case B:\n"
1033                    "    g();\n"
1034                    "    break;\n"
1035                    "  }\n"
1036                    "});",
1037                    getLLVMStyle()));
1038   EXPECT_EQ("switch (n) {\n"
1039             "case 0: {\n"
1040             "  return false;\n"
1041             "}\n"
1042             "default: {\n"
1043             "  return true;\n"
1044             "}\n"
1045             "}",
1046             format("switch (n)\n"
1047                    "{\n"
1048                    "case 0: {\n"
1049                    "  return false;\n"
1050                    "}\n"
1051                    "default: {\n"
1052                    "  return true;\n"
1053                    "}\n"
1054                    "}",
1055                    getLLVMStyle()));
1056   verifyFormat("switch (a) {\n"
1057                "case (b):\n"
1058                "  return;\n"
1059                "}");
1060 
1061   verifyFormat("switch (a) {\n"
1062                "case some_namespace::\n"
1063                "    some_constant:\n"
1064                "  return;\n"
1065                "}",
1066                getLLVMStyleWithColumns(34));
1067 
1068   FormatStyle Style = getLLVMStyle();
1069   Style.IndentCaseLabels = true;
1070   Style.AllowShortBlocksOnASingleLine = false;
1071   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1072   Style.BraceWrapping.AfterControlStatement = true;
1073   EXPECT_EQ("switch (n)\n"
1074             "{\n"
1075             "  case 0:\n"
1076             "  {\n"
1077             "    return false;\n"
1078             "  }\n"
1079             "  default:\n"
1080             "  {\n"
1081             "    return true;\n"
1082             "  }\n"
1083             "}",
1084             format("switch (n) {\n"
1085                    "  case 0: {\n"
1086                    "    return false;\n"
1087                    "  }\n"
1088                    "  default: {\n"
1089                    "    return true;\n"
1090                    "  }\n"
1091                    "}",
1092                    Style));
1093 }
1094 
1095 TEST_F(FormatTest, CaseRanges) {
1096   verifyFormat("switch (x) {\n"
1097                "case 'A' ... 'Z':\n"
1098                "case 1 ... 5:\n"
1099                "case a ... b:\n"
1100                "  break;\n"
1101                "}");
1102 }
1103 
1104 TEST_F(FormatTest, ShortCaseLabels) {
1105   FormatStyle Style = getLLVMStyle();
1106   Style.AllowShortCaseLabelsOnASingleLine = true;
1107   verifyFormat("switch (a) {\n"
1108                "case 1: x = 1; break;\n"
1109                "case 2: return;\n"
1110                "case 3:\n"
1111                "case 4:\n"
1112                "case 5: return;\n"
1113                "case 6: // comment\n"
1114                "  return;\n"
1115                "case 7:\n"
1116                "  // comment\n"
1117                "  return;\n"
1118                "case 8:\n"
1119                "  x = 8; // comment\n"
1120                "  break;\n"
1121                "default: y = 1; break;\n"
1122                "}",
1123                Style);
1124   verifyFormat("switch (a) {\n"
1125                "case 0: return; // comment\n"
1126                "case 1: break;  // comment\n"
1127                "case 2: return;\n"
1128                "// comment\n"
1129                "case 3: return;\n"
1130                "// comment 1\n"
1131                "// comment 2\n"
1132                "// comment 3\n"
1133                "case 4: break; /* comment */\n"
1134                "case 5:\n"
1135                "  // comment\n"
1136                "  break;\n"
1137                "case 6: /* comment */ x = 1; break;\n"
1138                "case 7: x = /* comment */ 1; break;\n"
1139                "case 8:\n"
1140                "  x = 1; /* comment */\n"
1141                "  break;\n"
1142                "case 9:\n"
1143                "  break; // comment line 1\n"
1144                "         // comment line 2\n"
1145                "}",
1146                Style);
1147   EXPECT_EQ("switch (a) {\n"
1148             "case 1:\n"
1149             "  x = 8;\n"
1150             "  // fall through\n"
1151             "case 2: x = 8;\n"
1152             "// comment\n"
1153             "case 3:\n"
1154             "  return; /* comment line 1\n"
1155             "           * comment line 2 */\n"
1156             "case 4: i = 8;\n"
1157             "// something else\n"
1158             "#if FOO\n"
1159             "case 5: break;\n"
1160             "#endif\n"
1161             "}",
1162             format("switch (a) {\n"
1163                    "case 1: x = 8;\n"
1164                    "  // fall through\n"
1165                    "case 2:\n"
1166                    "  x = 8;\n"
1167                    "// comment\n"
1168                    "case 3:\n"
1169                    "  return; /* comment line 1\n"
1170                    "           * comment line 2 */\n"
1171                    "case 4:\n"
1172                    "  i = 8;\n"
1173                    "// something else\n"
1174                    "#if FOO\n"
1175                    "case 5: break;\n"
1176                    "#endif\n"
1177                    "}",
1178                    Style));
1179   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1180             "  return; // long long long long long long long long long long long long comment\n"
1181             "          // line\n" "}",
1182             format("switch (a) {\n"
1183                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1184                    "}",
1185                    Style));
1186   EXPECT_EQ("switch (a) {\n"
1187             "case 0:\n"
1188             "  return; /* long long long long long long long long long long long long comment\n"
1189             "             line */\n"
1190             "}",
1191             format("switch (a) {\n"
1192                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1193                    "}",
1194                    Style));
1195   verifyFormat("switch (a) {\n"
1196                "#if FOO\n"
1197                "case 0: return 0;\n"
1198                "#endif\n"
1199                "}",
1200                Style);
1201   verifyFormat("switch (a) {\n"
1202                "case 1: {\n"
1203                "}\n"
1204                "case 2: {\n"
1205                "  return;\n"
1206                "}\n"
1207                "case 3: {\n"
1208                "  x = 1;\n"
1209                "  return;\n"
1210                "}\n"
1211                "case 4:\n"
1212                "  if (x)\n"
1213                "    return;\n"
1214                "}",
1215                Style);
1216   Style.ColumnLimit = 21;
1217   verifyFormat("switch (a) {\n"
1218                "case 1: x = 1; break;\n"
1219                "case 2: return;\n"
1220                "case 3:\n"
1221                "case 4:\n"
1222                "case 5: return;\n"
1223                "default:\n"
1224                "  y = 1;\n"
1225                "  break;\n"
1226                "}",
1227                Style);
1228   Style.ColumnLimit = 80;
1229   Style.AllowShortCaseLabelsOnASingleLine = false;
1230   Style.IndentCaseLabels = true;
1231   EXPECT_EQ("switch (n) {\n"
1232             "  default /*comments*/:\n"
1233             "    return true;\n"
1234             "  case 0:\n"
1235             "    return false;\n"
1236             "}",
1237             format("switch (n) {\n"
1238                    "default/*comments*/:\n"
1239                    "  return true;\n"
1240                    "case 0:\n"
1241                    "  return false;\n"
1242                    "}",
1243                    Style));
1244   Style.AllowShortCaseLabelsOnASingleLine = true;
1245   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1246   Style.BraceWrapping.AfterControlStatement = true;
1247   EXPECT_EQ("switch (n)\n"
1248             "{\n"
1249             "  case 0:\n"
1250             "  {\n"
1251             "    return false;\n"
1252             "  }\n"
1253             "  default:\n"
1254             "  {\n"
1255             "    return true;\n"
1256             "  }\n"
1257             "}",
1258             format("switch (n) {\n"
1259                    "  case 0: {\n"
1260                    "    return false;\n"
1261                    "  }\n"
1262                    "  default:\n"
1263                    "  {\n"
1264                    "    return true;\n"
1265                    "  }\n"
1266                    "}",
1267                    Style));
1268 }
1269 
1270 TEST_F(FormatTest, FormatsLabels) {
1271   verifyFormat("void f() {\n"
1272                "  some_code();\n"
1273                "test_label:\n"
1274                "  some_other_code();\n"
1275                "  {\n"
1276                "    some_more_code();\n"
1277                "  another_label:\n"
1278                "    some_more_code();\n"
1279                "  }\n"
1280                "}");
1281   verifyFormat("{\n"
1282                "  some_code();\n"
1283                "test_label:\n"
1284                "  some_other_code();\n"
1285                "}");
1286   verifyFormat("{\n"
1287                "  some_code();\n"
1288                "test_label:;\n"
1289                "  int i = 0;\n"
1290                "}");
1291 }
1292 
1293 //===----------------------------------------------------------------------===//
1294 // Tests for classes, namespaces, etc.
1295 //===----------------------------------------------------------------------===//
1296 
1297 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1298   verifyFormat("class A {};");
1299 }
1300 
1301 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1302   verifyFormat("class A {\n"
1303                "public:\n"
1304                "public: // comment\n"
1305                "protected:\n"
1306                "private:\n"
1307                "  void f() {}\n"
1308                "};");
1309   verifyFormat("export class A {\n"
1310                "public:\n"
1311                "public: // comment\n"
1312                "protected:\n"
1313                "private:\n"
1314                "  void f() {}\n"
1315                "};");
1316   verifyGoogleFormat("class A {\n"
1317                      " public:\n"
1318                      " protected:\n"
1319                      " private:\n"
1320                      "  void f() {}\n"
1321                      "};");
1322   verifyGoogleFormat("export class A {\n"
1323                      " public:\n"
1324                      " protected:\n"
1325                      " private:\n"
1326                      "  void f() {}\n"
1327                      "};");
1328   verifyFormat("class A {\n"
1329                "public slots:\n"
1330                "  void f1() {}\n"
1331                "public Q_SLOTS:\n"
1332                "  void f2() {}\n"
1333                "protected slots:\n"
1334                "  void f3() {}\n"
1335                "protected Q_SLOTS:\n"
1336                "  void f4() {}\n"
1337                "private slots:\n"
1338                "  void f5() {}\n"
1339                "private Q_SLOTS:\n"
1340                "  void f6() {}\n"
1341                "signals:\n"
1342                "  void g1();\n"
1343                "Q_SIGNALS:\n"
1344                "  void g2();\n"
1345                "};");
1346 
1347   // Don't interpret 'signals' the wrong way.
1348   verifyFormat("signals.set();");
1349   verifyFormat("for (Signals signals : f()) {\n}");
1350   verifyFormat("{\n"
1351                "  signals.set(); // This needs indentation.\n"
1352                "}");
1353   verifyFormat("void f() {\n"
1354                "label:\n"
1355                "  signals.baz();\n"
1356                "}");
1357 }
1358 
1359 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1360   EXPECT_EQ("class A {\n"
1361             "public:\n"
1362             "  void f();\n"
1363             "\n"
1364             "private:\n"
1365             "  void g() {}\n"
1366             "  // test\n"
1367             "protected:\n"
1368             "  int h;\n"
1369             "};",
1370             format("class A {\n"
1371                    "public:\n"
1372                    "void f();\n"
1373                    "private:\n"
1374                    "void g() {}\n"
1375                    "// test\n"
1376                    "protected:\n"
1377                    "int h;\n"
1378                    "};"));
1379   EXPECT_EQ("class A {\n"
1380             "protected:\n"
1381             "public:\n"
1382             "  void f();\n"
1383             "};",
1384             format("class A {\n"
1385                    "protected:\n"
1386                    "\n"
1387                    "public:\n"
1388                    "\n"
1389                    "  void f();\n"
1390                    "};"));
1391 
1392   // Even ensure proper spacing inside macros.
1393   EXPECT_EQ("#define B     \\\n"
1394             "  class A {   \\\n"
1395             "   protected: \\\n"
1396             "   public:    \\\n"
1397             "    void f(); \\\n"
1398             "  };",
1399             format("#define B     \\\n"
1400                    "  class A {   \\\n"
1401                    "   protected: \\\n"
1402                    "              \\\n"
1403                    "   public:    \\\n"
1404                    "              \\\n"
1405                    "    void f(); \\\n"
1406                    "  };",
1407                    getGoogleStyle()));
1408   // But don't remove empty lines after macros ending in access specifiers.
1409   EXPECT_EQ("#define A private:\n"
1410             "\n"
1411             "int i;",
1412             format("#define A         private:\n"
1413                    "\n"
1414                    "int              i;"));
1415 }
1416 
1417 TEST_F(FormatTest, FormatsClasses) {
1418   verifyFormat("class A : public B {};");
1419   verifyFormat("class A : public ::B {};");
1420 
1421   verifyFormat(
1422       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1423       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1424   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1425                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1426                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1427   verifyFormat(
1428       "class A : public B, public C, public D, public E, public F {};");
1429   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1430                "                     public C,\n"
1431                "                     public D,\n"
1432                "                     public E,\n"
1433                "                     public F,\n"
1434                "                     public G {};");
1435 
1436   verifyFormat("class\n"
1437                "    ReallyReallyLongClassName {\n"
1438                "  int i;\n"
1439                "};",
1440                getLLVMStyleWithColumns(32));
1441   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1442                "                           aaaaaaaaaaaaaaaa> {};");
1443   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1444                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1445                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1446   verifyFormat("template <class R, class C>\n"
1447                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1448                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1449   verifyFormat("class ::A::B {};");
1450 }
1451 
1452 TEST_F(FormatTest, BreakInheritanceStyle) {
1453   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1454   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1455           FormatStyle::BILS_BeforeComma;
1456   verifyFormat("class MyClass : public X {};",
1457                StyleWithInheritanceBreakBeforeComma);
1458   verifyFormat("class MyClass\n"
1459                "    : public X\n"
1460                "    , public Y {};",
1461                StyleWithInheritanceBreakBeforeComma);
1462   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1463                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1464                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1465                StyleWithInheritanceBreakBeforeComma);
1466   verifyFormat("struct aaaaaaaaaaaaa\n"
1467                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
1468                "          aaaaaaaaaaaaaaaa> {};",
1469                StyleWithInheritanceBreakBeforeComma);
1470 
1471   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1472   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1473           FormatStyle::BILS_AfterColon;
1474   verifyFormat("class MyClass : public X {};",
1475                StyleWithInheritanceBreakAfterColon);
1476   verifyFormat("class MyClass : public X, public Y {};",
1477                StyleWithInheritanceBreakAfterColon);
1478   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1479                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1480                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1481                StyleWithInheritanceBreakAfterColon);
1482   verifyFormat("struct aaaaaaaaaaaaa :\n"
1483                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
1484                "        aaaaaaaaaaaaaaaa> {};",
1485                StyleWithInheritanceBreakAfterColon);
1486 }
1487 
1488 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1489   verifyFormat("class A {\n} a, b;");
1490   verifyFormat("struct A {\n} a, b;");
1491   verifyFormat("union A {\n} a;");
1492 }
1493 
1494 TEST_F(FormatTest, FormatsEnum) {
1495   verifyFormat("enum {\n"
1496                "  Zero,\n"
1497                "  One = 1,\n"
1498                "  Two = One + 1,\n"
1499                "  Three = (One + Two),\n"
1500                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1501                "  Five = (One, Two, Three, Four, 5)\n"
1502                "};");
1503   verifyGoogleFormat("enum {\n"
1504                      "  Zero,\n"
1505                      "  One = 1,\n"
1506                      "  Two = One + 1,\n"
1507                      "  Three = (One + Two),\n"
1508                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1509                      "  Five = (One, Two, Three, Four, 5)\n"
1510                      "};");
1511   verifyFormat("enum Enum {};");
1512   verifyFormat("enum {};");
1513   verifyFormat("enum X E {} d;");
1514   verifyFormat("enum __attribute__((...)) E {} d;");
1515   verifyFormat("enum __declspec__((...)) E {} d;");
1516   verifyFormat("enum {\n"
1517                "  Bar = Foo<int, int>::value\n"
1518                "};",
1519                getLLVMStyleWithColumns(30));
1520 
1521   verifyFormat("enum ShortEnum { A, B, C };");
1522   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1523 
1524   EXPECT_EQ("enum KeepEmptyLines {\n"
1525             "  ONE,\n"
1526             "\n"
1527             "  TWO,\n"
1528             "\n"
1529             "  THREE\n"
1530             "}",
1531             format("enum KeepEmptyLines {\n"
1532                    "  ONE,\n"
1533                    "\n"
1534                    "  TWO,\n"
1535                    "\n"
1536                    "\n"
1537                    "  THREE\n"
1538                    "}"));
1539   verifyFormat("enum E { // comment\n"
1540                "  ONE,\n"
1541                "  TWO\n"
1542                "};\n"
1543                "int i;");
1544   // Not enums.
1545   verifyFormat("enum X f() {\n"
1546                "  a();\n"
1547                "  return 42;\n"
1548                "}");
1549   verifyFormat("enum X Type::f() {\n"
1550                "  a();\n"
1551                "  return 42;\n"
1552                "}");
1553   verifyFormat("enum ::X f() {\n"
1554                "  a();\n"
1555                "  return 42;\n"
1556                "}");
1557   verifyFormat("enum ns::X f() {\n"
1558                "  a();\n"
1559                "  return 42;\n"
1560                "}");
1561 }
1562 
1563 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1564   verifyFormat("enum Type {\n"
1565                "  One = 0; // These semicolons should be commas.\n"
1566                "  Two = 1;\n"
1567                "};");
1568   verifyFormat("namespace n {\n"
1569                "enum Type {\n"
1570                "  One,\n"
1571                "  Two, // missing };\n"
1572                "  int i;\n"
1573                "}\n"
1574                "void g() {}");
1575 }
1576 
1577 TEST_F(FormatTest, FormatsEnumStruct) {
1578   verifyFormat("enum struct {\n"
1579                "  Zero,\n"
1580                "  One = 1,\n"
1581                "  Two = One + 1,\n"
1582                "  Three = (One + Two),\n"
1583                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1584                "  Five = (One, Two, Three, Four, 5)\n"
1585                "};");
1586   verifyFormat("enum struct Enum {};");
1587   verifyFormat("enum struct {};");
1588   verifyFormat("enum struct X E {} d;");
1589   verifyFormat("enum struct __attribute__((...)) E {} d;");
1590   verifyFormat("enum struct __declspec__((...)) E {} d;");
1591   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1592 }
1593 
1594 TEST_F(FormatTest, FormatsEnumClass) {
1595   verifyFormat("enum class {\n"
1596                "  Zero,\n"
1597                "  One = 1,\n"
1598                "  Two = One + 1,\n"
1599                "  Three = (One + Two),\n"
1600                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1601                "  Five = (One, Two, Three, Four, 5)\n"
1602                "};");
1603   verifyFormat("enum class Enum {};");
1604   verifyFormat("enum class {};");
1605   verifyFormat("enum class X E {} d;");
1606   verifyFormat("enum class __attribute__((...)) E {} d;");
1607   verifyFormat("enum class __declspec__((...)) E {} d;");
1608   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1609 }
1610 
1611 TEST_F(FormatTest, FormatsEnumTypes) {
1612   verifyFormat("enum X : int {\n"
1613                "  A, // Force multiple lines.\n"
1614                "  B\n"
1615                "};");
1616   verifyFormat("enum X : int { A, B };");
1617   verifyFormat("enum X : std::uint32_t { A, B };");
1618 }
1619 
1620 TEST_F(FormatTest, FormatsTypedefEnum) {
1621   FormatStyle Style = getLLVMStyle();
1622   Style.ColumnLimit = 40;
1623   verifyFormat("typedef enum {} EmptyEnum;");
1624   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1625   verifyFormat("typedef enum {\n"
1626                "  ZERO = 0,\n"
1627                "  ONE = 1,\n"
1628                "  TWO = 2,\n"
1629                "  THREE = 3\n"
1630                "} LongEnum;",
1631                Style);
1632   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1633   Style.BraceWrapping.AfterEnum = true;
1634   verifyFormat("typedef enum {} EmptyEnum;");
1635   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1636   verifyFormat("typedef enum\n"
1637                "{\n"
1638                "  ZERO = 0,\n"
1639                "  ONE = 1,\n"
1640                "  TWO = 2,\n"
1641                "  THREE = 3\n"
1642                "} LongEnum;",
1643                Style);
1644 }
1645 
1646 TEST_F(FormatTest, FormatsNSEnums) {
1647   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1648   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1649                      "  // Information about someDecentlyLongValue.\n"
1650                      "  someDecentlyLongValue,\n"
1651                      "  // Information about anotherDecentlyLongValue.\n"
1652                      "  anotherDecentlyLongValue,\n"
1653                      "  // Information about aThirdDecentlyLongValue.\n"
1654                      "  aThirdDecentlyLongValue\n"
1655                      "};");
1656   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1657                      "  a = 1,\n"
1658                      "  b = 2,\n"
1659                      "  c = 3,\n"
1660                      "};");
1661   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1662                      "  a = 1,\n"
1663                      "  b = 2,\n"
1664                      "  c = 3,\n"
1665                      "};");
1666   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1667                      "  a = 1,\n"
1668                      "  b = 2,\n"
1669                      "  c = 3,\n"
1670                      "};");
1671 }
1672 
1673 TEST_F(FormatTest, FormatsBitfields) {
1674   verifyFormat("struct Bitfields {\n"
1675                "  unsigned sClass : 8;\n"
1676                "  unsigned ValueKind : 2;\n"
1677                "};");
1678   verifyFormat("struct A {\n"
1679                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1680                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1681                "};");
1682   verifyFormat("struct MyStruct {\n"
1683                "  uchar data;\n"
1684                "  uchar : 8;\n"
1685                "  uchar : 8;\n"
1686                "  uchar other;\n"
1687                "};");
1688 }
1689 
1690 TEST_F(FormatTest, FormatsNamespaces) {
1691   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1692   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1693 
1694   verifyFormat("namespace some_namespace {\n"
1695                "class A {};\n"
1696                "void f() { f(); }\n"
1697                "}",
1698                LLVMWithNoNamespaceFix);
1699   verifyFormat("/* something */ namespace some_namespace {\n"
1700                "class A {};\n"
1701                "void f() { f(); }\n"
1702                "}",
1703                LLVMWithNoNamespaceFix);
1704   verifyFormat("namespace {\n"
1705                "class A {};\n"
1706                "void f() { f(); }\n"
1707                "}",
1708                LLVMWithNoNamespaceFix);
1709   verifyFormat("/* something */ namespace {\n"
1710                "class A {};\n"
1711                "void f() { f(); }\n"
1712                "}",
1713                LLVMWithNoNamespaceFix);
1714   verifyFormat("inline namespace X {\n"
1715                "class A {};\n"
1716                "void f() { f(); }\n"
1717                "}",
1718                LLVMWithNoNamespaceFix);
1719   verifyFormat("/* something */ inline namespace X {\n"
1720                "class A {};\n"
1721                "void f() { f(); }\n"
1722                "}",
1723                LLVMWithNoNamespaceFix);
1724   verifyFormat("export namespace X {\n"
1725                "class A {};\n"
1726                "void f() { f(); }\n"
1727                "}",
1728                LLVMWithNoNamespaceFix);
1729   verifyFormat("using namespace some_namespace;\n"
1730                "class A {};\n"
1731                "void f() { f(); }",
1732                LLVMWithNoNamespaceFix);
1733 
1734   // This code is more common than we thought; if we
1735   // layout this correctly the semicolon will go into
1736   // its own line, which is undesirable.
1737   verifyFormat("namespace {};",
1738                LLVMWithNoNamespaceFix);
1739   verifyFormat("namespace {\n"
1740                "class A {};\n"
1741                "};",
1742                LLVMWithNoNamespaceFix);
1743 
1744   verifyFormat("namespace {\n"
1745                "int SomeVariable = 0; // comment\n"
1746                "} // namespace",
1747                LLVMWithNoNamespaceFix);
1748   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1749             "#define HEADER_GUARD\n"
1750             "namespace my_namespace {\n"
1751             "int i;\n"
1752             "} // my_namespace\n"
1753             "#endif // HEADER_GUARD",
1754             format("#ifndef HEADER_GUARD\n"
1755                    " #define HEADER_GUARD\n"
1756                    "   namespace my_namespace {\n"
1757                    "int i;\n"
1758                    "}    // my_namespace\n"
1759                    "#endif    // HEADER_GUARD",
1760                    LLVMWithNoNamespaceFix));
1761 
1762   EXPECT_EQ("namespace A::B {\n"
1763             "class C {};\n"
1764             "}",
1765             format("namespace A::B {\n"
1766                    "class C {};\n"
1767                    "}",
1768                    LLVMWithNoNamespaceFix));
1769 
1770   FormatStyle Style = getLLVMStyle();
1771   Style.NamespaceIndentation = FormatStyle::NI_All;
1772   EXPECT_EQ("namespace out {\n"
1773             "  int i;\n"
1774             "  namespace in {\n"
1775             "    int i;\n"
1776             "  } // namespace in\n"
1777             "} // namespace out",
1778             format("namespace out {\n"
1779                    "int i;\n"
1780                    "namespace in {\n"
1781                    "int i;\n"
1782                    "} // namespace in\n"
1783                    "} // namespace out",
1784                    Style));
1785 
1786   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1787   EXPECT_EQ("namespace out {\n"
1788             "int i;\n"
1789             "namespace in {\n"
1790             "  int i;\n"
1791             "} // namespace in\n"
1792             "} // namespace out",
1793             format("namespace out {\n"
1794                    "int i;\n"
1795                    "namespace in {\n"
1796                    "int i;\n"
1797                    "} // namespace in\n"
1798                    "} // namespace out",
1799                    Style));
1800 }
1801 
1802 TEST_F(FormatTest, FormatsCompactNamespaces) {
1803   FormatStyle Style = getLLVMStyle();
1804   Style.CompactNamespaces = true;
1805 
1806   verifyFormat("namespace A { namespace B {\n"
1807 			   "}} // namespace A::B",
1808 			   Style);
1809 
1810   EXPECT_EQ("namespace out { namespace in {\n"
1811             "}} // namespace out::in",
1812             format("namespace out {\n"
1813                    "namespace in {\n"
1814                    "} // namespace in\n"
1815                    "} // namespace out",
1816                    Style));
1817 
1818   // Only namespaces which have both consecutive opening and end get compacted
1819   EXPECT_EQ("namespace out {\n"
1820             "namespace in1 {\n"
1821             "} // namespace in1\n"
1822             "namespace in2 {\n"
1823             "} // namespace in2\n"
1824             "} // namespace out",
1825             format("namespace out {\n"
1826                    "namespace in1 {\n"
1827                    "} // namespace in1\n"
1828                    "namespace in2 {\n"
1829                    "} // namespace in2\n"
1830                    "} // namespace out",
1831                    Style));
1832 
1833   EXPECT_EQ("namespace out {\n"
1834             "int i;\n"
1835             "namespace in {\n"
1836             "int j;\n"
1837             "} // namespace in\n"
1838             "int k;\n"
1839             "} // namespace out",
1840             format("namespace out { int i;\n"
1841                    "namespace in { int j; } // namespace in\n"
1842                    "int k; } // namespace out",
1843                    Style));
1844 
1845   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1846             "}}} // namespace A::B::C\n",
1847             format("namespace A { namespace B {\n"
1848                    "namespace C {\n"
1849                    "}} // namespace B::C\n"
1850                    "} // namespace A\n",
1851                    Style));
1852 
1853   Style.ColumnLimit = 40;
1854   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1855             "namespace bbbbbbbbbb {\n"
1856             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1857             format("namespace aaaaaaaaaa {\n"
1858                    "namespace bbbbbbbbbb {\n"
1859                    "} // namespace bbbbbbbbbb\n"
1860                    "} // namespace aaaaaaaaaa",
1861                    Style));
1862 
1863   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1864             "namespace cccccc {\n"
1865             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1866             format("namespace aaaaaa {\n"
1867                    "namespace bbbbbb {\n"
1868                    "namespace cccccc {\n"
1869                    "} // namespace cccccc\n"
1870                    "} // namespace bbbbbb\n"
1871                    "} // namespace aaaaaa",
1872                    Style));
1873   Style.ColumnLimit = 80;
1874 
1875   // Extra semicolon after 'inner' closing brace prevents merging
1876   EXPECT_EQ("namespace out { namespace in {\n"
1877             "}; } // namespace out::in",
1878             format("namespace out {\n"
1879                    "namespace in {\n"
1880                    "}; // namespace in\n"
1881                    "} // namespace out",
1882                    Style));
1883 
1884   // Extra semicolon after 'outer' closing brace is conserved
1885   EXPECT_EQ("namespace out { namespace in {\n"
1886             "}}; // namespace out::in",
1887             format("namespace out {\n"
1888                    "namespace in {\n"
1889                    "} // namespace in\n"
1890                    "}; // namespace out",
1891                    Style));
1892 
1893   Style.NamespaceIndentation = FormatStyle::NI_All;
1894   EXPECT_EQ("namespace out { namespace in {\n"
1895             "  int i;\n"
1896             "}} // namespace out::in",
1897             format("namespace out {\n"
1898                    "namespace in {\n"
1899                    "int i;\n"
1900                    "} // namespace in\n"
1901                    "} // namespace out",
1902                    Style));
1903   EXPECT_EQ("namespace out { namespace mid {\n"
1904             "  namespace in {\n"
1905             "    int j;\n"
1906             "  } // namespace in\n"
1907             "  int k;\n"
1908             "}} // namespace out::mid",
1909             format("namespace out { namespace mid {\n"
1910                    "namespace in { int j; } // namespace in\n"
1911                    "int k; }} // namespace out::mid",
1912                    Style));
1913 
1914   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1915   EXPECT_EQ("namespace out { namespace in {\n"
1916             "  int i;\n"
1917             "}} // namespace out::in",
1918             format("namespace out {\n"
1919                    "namespace in {\n"
1920                    "int i;\n"
1921                    "} // namespace in\n"
1922                    "} // namespace out",
1923                    Style));
1924   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1925             "  int i;\n"
1926             "}}} // namespace out::mid::in",
1927             format("namespace out {\n"
1928                    "namespace mid {\n"
1929                    "namespace in {\n"
1930                    "int i;\n"
1931                    "} // namespace in\n"
1932                    "} // namespace mid\n"
1933                    "} // namespace out",
1934                    Style));
1935 }
1936 
1937 TEST_F(FormatTest, FormatsExternC) {
1938   verifyFormat("extern \"C\" {\nint a;");
1939   verifyFormat("extern \"C\" {}");
1940   verifyFormat("extern \"C\" {\n"
1941                "int foo();\n"
1942                "}");
1943   verifyFormat("extern \"C\" int foo() {}");
1944   verifyFormat("extern \"C\" int foo();");
1945   verifyFormat("extern \"C\" int foo() {\n"
1946                "  int i = 42;\n"
1947                "  return i;\n"
1948                "}");
1949 
1950   FormatStyle Style = getLLVMStyle();
1951   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1952   Style.BraceWrapping.AfterFunction = true;
1953   verifyFormat("extern \"C\" int foo() {}", Style);
1954   verifyFormat("extern \"C\" int foo();", Style);
1955   verifyFormat("extern \"C\" int foo()\n"
1956                "{\n"
1957                "  int i = 42;\n"
1958                "  return i;\n"
1959                "}",
1960                Style);
1961 
1962   Style.BraceWrapping.AfterExternBlock = true;
1963   Style.BraceWrapping.SplitEmptyRecord = false;
1964   verifyFormat("extern \"C\"\n"
1965                "{}",
1966                Style);
1967   verifyFormat("extern \"C\"\n"
1968                "{\n"
1969                "  int foo();\n"
1970                "}",
1971                Style);
1972 }
1973 
1974 TEST_F(FormatTest, FormatsInlineASM) {
1975   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1976   verifyFormat("asm(\"nop\" ::: \"memory\");");
1977   verifyFormat(
1978       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1979       "    \"cpuid\\n\\t\"\n"
1980       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1981       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1982       "    : \"a\"(value));");
1983   EXPECT_EQ(
1984       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1985       "  __asm {\n"
1986       "        mov     edx,[that] // vtable in edx\n"
1987       "        mov     eax,methodIndex\n"
1988       "        call    [edx][eax*4] // stdcall\n"
1989       "  }\n"
1990       "}",
1991       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1992              "    __asm {\n"
1993              "        mov     edx,[that] // vtable in edx\n"
1994              "        mov     eax,methodIndex\n"
1995              "        call    [edx][eax*4] // stdcall\n"
1996              "    }\n"
1997              "}"));
1998   EXPECT_EQ("_asm {\n"
1999             "  xor eax, eax;\n"
2000             "  cpuid;\n"
2001             "}",
2002             format("_asm {\n"
2003                    "  xor eax, eax;\n"
2004                    "  cpuid;\n"
2005                    "}"));
2006   verifyFormat("void function() {\n"
2007                "  // comment\n"
2008                "  asm(\"\");\n"
2009                "}");
2010   EXPECT_EQ("__asm {\n"
2011             "}\n"
2012             "int i;",
2013             format("__asm   {\n"
2014                    "}\n"
2015                    "int   i;"));
2016 }
2017 
2018 TEST_F(FormatTest, FormatTryCatch) {
2019   verifyFormat("try {\n"
2020                "  throw a * b;\n"
2021                "} catch (int a) {\n"
2022                "  // Do nothing.\n"
2023                "} catch (...) {\n"
2024                "  exit(42);\n"
2025                "}");
2026 
2027   // Function-level try statements.
2028   verifyFormat("int f() try { return 4; } catch (...) {\n"
2029                "  return 5;\n"
2030                "}");
2031   verifyFormat("class A {\n"
2032                "  int a;\n"
2033                "  A() try : a(0) {\n"
2034                "  } catch (...) {\n"
2035                "    throw;\n"
2036                "  }\n"
2037                "};\n");
2038 
2039   // Incomplete try-catch blocks.
2040   verifyIncompleteFormat("try {} catch (");
2041 }
2042 
2043 TEST_F(FormatTest, FormatSEHTryCatch) {
2044   verifyFormat("__try {\n"
2045                "  int a = b * c;\n"
2046                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2047                "  // Do nothing.\n"
2048                "}");
2049 
2050   verifyFormat("__try {\n"
2051                "  int a = b * c;\n"
2052                "} __finally {\n"
2053                "  // Do nothing.\n"
2054                "}");
2055 
2056   verifyFormat("DEBUG({\n"
2057                "  __try {\n"
2058                "  } __finally {\n"
2059                "  }\n"
2060                "});\n");
2061 }
2062 
2063 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2064   verifyFormat("try {\n"
2065                "  f();\n"
2066                "} catch {\n"
2067                "  g();\n"
2068                "}");
2069   verifyFormat("try {\n"
2070                "  f();\n"
2071                "} catch (A a) MACRO(x) {\n"
2072                "  g();\n"
2073                "} catch (B b) MACRO(x) {\n"
2074                "  g();\n"
2075                "}");
2076 }
2077 
2078 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2079   FormatStyle Style = getLLVMStyle();
2080   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2081                           FormatStyle::BS_WebKit}) {
2082     Style.BreakBeforeBraces = BraceStyle;
2083     verifyFormat("try {\n"
2084                  "  // something\n"
2085                  "} catch (...) {\n"
2086                  "  // something\n"
2087                  "}",
2088                  Style);
2089   }
2090   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2091   verifyFormat("try {\n"
2092                "  // something\n"
2093                "}\n"
2094                "catch (...) {\n"
2095                "  // something\n"
2096                "}",
2097                Style);
2098   verifyFormat("__try {\n"
2099                "  // something\n"
2100                "}\n"
2101                "__finally {\n"
2102                "  // something\n"
2103                "}",
2104                Style);
2105   verifyFormat("@try {\n"
2106                "  // something\n"
2107                "}\n"
2108                "@finally {\n"
2109                "  // something\n"
2110                "}",
2111                Style);
2112   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2113   verifyFormat("try\n"
2114                "{\n"
2115                "  // something\n"
2116                "}\n"
2117                "catch (...)\n"
2118                "{\n"
2119                "  // something\n"
2120                "}",
2121                Style);
2122   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2123   verifyFormat("try\n"
2124                "  {\n"
2125                "    // something\n"
2126                "  }\n"
2127                "catch (...)\n"
2128                "  {\n"
2129                "    // something\n"
2130                "  }",
2131                Style);
2132   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2133   Style.BraceWrapping.BeforeCatch = true;
2134   verifyFormat("try {\n"
2135                "  // something\n"
2136                "}\n"
2137                "catch (...) {\n"
2138                "  // something\n"
2139                "}",
2140                Style);
2141 }
2142 
2143 TEST_F(FormatTest, StaticInitializers) {
2144   verifyFormat("static SomeClass SC = {1, 'a'};");
2145 
2146   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2147                "    100000000, "
2148                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2149 
2150   // Here, everything other than the "}" would fit on a line.
2151   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2152                "    10000000000000000000000000};");
2153   EXPECT_EQ("S s = {a,\n"
2154             "\n"
2155             "       b};",
2156             format("S s = {\n"
2157                    "  a,\n"
2158                    "\n"
2159                    "  b\n"
2160                    "};"));
2161 
2162   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2163   // line. However, the formatting looks a bit off and this probably doesn't
2164   // happen often in practice.
2165   verifyFormat("static int Variable[1] = {\n"
2166                "    {1000000000000000000000000000000000000}};",
2167                getLLVMStyleWithColumns(40));
2168 }
2169 
2170 TEST_F(FormatTest, DesignatedInitializers) {
2171   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2172   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2173                "                    .bbbbbbbbbb = 2,\n"
2174                "                    .cccccccccc = 3,\n"
2175                "                    .dddddddddd = 4,\n"
2176                "                    .eeeeeeeeee = 5};");
2177   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2178                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2179                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2180                "    .ccccccccccccccccccccccccccc = 3,\n"
2181                "    .ddddddddddddddddddddddddddd = 4,\n"
2182                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2183 
2184   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2185 
2186   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2187   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2188                "                    [2] = bbbbbbbbbb,\n"
2189                "                    [3] = cccccccccc,\n"
2190                "                    [4] = dddddddddd,\n"
2191                "                    [5] = eeeeeeeeee};");
2192   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2193                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2194                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2195                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2196                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2197                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2198 }
2199 
2200 TEST_F(FormatTest, NestedStaticInitializers) {
2201   verifyFormat("static A x = {{{}}};\n");
2202   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2203                "               {init1, init2, init3, init4}}};",
2204                getLLVMStyleWithColumns(50));
2205 
2206   verifyFormat("somes Status::global_reps[3] = {\n"
2207                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2208                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2209                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2210                getLLVMStyleWithColumns(60));
2211   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2212                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2213                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2214                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2215   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2216                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2217                "rect.fTop}};");
2218 
2219   verifyFormat(
2220       "SomeArrayOfSomeType a = {\n"
2221       "    {{1, 2, 3},\n"
2222       "     {1, 2, 3},\n"
2223       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2224       "      333333333333333333333333333333},\n"
2225       "     {1, 2, 3},\n"
2226       "     {1, 2, 3}}};");
2227   verifyFormat(
2228       "SomeArrayOfSomeType a = {\n"
2229       "    {{1, 2, 3}},\n"
2230       "    {{1, 2, 3}},\n"
2231       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2232       "      333333333333333333333333333333}},\n"
2233       "    {{1, 2, 3}},\n"
2234       "    {{1, 2, 3}}};");
2235 
2236   verifyFormat("struct {\n"
2237                "  unsigned bit;\n"
2238                "  const char *const name;\n"
2239                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2240                "                 {kOsWin, \"Windows\"},\n"
2241                "                 {kOsLinux, \"Linux\"},\n"
2242                "                 {kOsCrOS, \"Chrome OS\"}};");
2243   verifyFormat("struct {\n"
2244                "  unsigned bit;\n"
2245                "  const char *const name;\n"
2246                "} kBitsToOs[] = {\n"
2247                "    {kOsMac, \"Mac\"},\n"
2248                "    {kOsWin, \"Windows\"},\n"
2249                "    {kOsLinux, \"Linux\"},\n"
2250                "    {kOsCrOS, \"Chrome OS\"},\n"
2251                "};");
2252 }
2253 
2254 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2255   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2256                "                      \\\n"
2257                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2258 }
2259 
2260 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2261   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2262                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2263 
2264   // Do break defaulted and deleted functions.
2265   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2266                "    default;",
2267                getLLVMStyleWithColumns(40));
2268   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2269                "    delete;",
2270                getLLVMStyleWithColumns(40));
2271 }
2272 
2273 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2274   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2275                getLLVMStyleWithColumns(40));
2276   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2277                getLLVMStyleWithColumns(40));
2278   EXPECT_EQ("#define Q                              \\\n"
2279             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2280             "  \"aaaaaaaa.cpp\"",
2281             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2282                    getLLVMStyleWithColumns(40)));
2283 }
2284 
2285 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2286   EXPECT_EQ("# 123 \"A string literal\"",
2287             format("   #     123    \"A string literal\""));
2288 }
2289 
2290 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2291   EXPECT_EQ("#;", format("#;"));
2292   verifyFormat("#\n;\n;\n;");
2293 }
2294 
2295 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2296   EXPECT_EQ("#line 42 \"test\"\n",
2297             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2298   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2299                                     getLLVMStyleWithColumns(12)));
2300 }
2301 
2302 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2303   EXPECT_EQ("#line 42 \"test\"",
2304             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2305   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2306 }
2307 
2308 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2309   verifyFormat("#define A \\x20");
2310   verifyFormat("#define A \\ x20");
2311   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2312   verifyFormat("#define A ''");
2313   verifyFormat("#define A ''qqq");
2314   verifyFormat("#define A `qqq");
2315   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2316   EXPECT_EQ("const char *c = STRINGIFY(\n"
2317             "\\na : b);",
2318             format("const char * c = STRINGIFY(\n"
2319                    "\\na : b);"));
2320 
2321   verifyFormat("a\r\\");
2322   verifyFormat("a\v\\");
2323   verifyFormat("a\f\\");
2324 }
2325 
2326 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2327   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2328   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2329   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2330   // FIXME: We never break before the macro name.
2331   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2332 
2333   verifyFormat("#define A A\n#define A A");
2334   verifyFormat("#define A(X) A\n#define A A");
2335 
2336   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2337   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2338 }
2339 
2340 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2341   EXPECT_EQ("// somecomment\n"
2342             "#include \"a.h\"\n"
2343             "#define A(  \\\n"
2344             "    A, B)\n"
2345             "#include \"b.h\"\n"
2346             "// somecomment\n",
2347             format("  // somecomment\n"
2348                    "  #include \"a.h\"\n"
2349                    "#define A(A,\\\n"
2350                    "    B)\n"
2351                    "    #include \"b.h\"\n"
2352                    " // somecomment\n",
2353                    getLLVMStyleWithColumns(13)));
2354 }
2355 
2356 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2357 
2358 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2359   EXPECT_EQ("#define A    \\\n"
2360             "  c;         \\\n"
2361             "  e;\n"
2362             "f;",
2363             format("#define A c; e;\n"
2364                    "f;",
2365                    getLLVMStyleWithColumns(14)));
2366 }
2367 
2368 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2369 
2370 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2371   EXPECT_EQ("int x,\n"
2372             "#define A\n"
2373             "    y;",
2374             format("int x,\n#define A\ny;"));
2375 }
2376 
2377 TEST_F(FormatTest, HashInMacroDefinition) {
2378   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2379   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2380   verifyFormat("#define A  \\\n"
2381                "  {        \\\n"
2382                "    f(#c); \\\n"
2383                "  }",
2384                getLLVMStyleWithColumns(11));
2385 
2386   verifyFormat("#define A(X)         \\\n"
2387                "  void function##X()",
2388                getLLVMStyleWithColumns(22));
2389 
2390   verifyFormat("#define A(a, b, c)   \\\n"
2391                "  void a##b##c()",
2392                getLLVMStyleWithColumns(22));
2393 
2394   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2395 }
2396 
2397 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2398   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2399   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2400 }
2401 
2402 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2403   EXPECT_EQ("#define A b;", format("#define A \\\n"
2404                                    "          \\\n"
2405                                    "  b;",
2406                                    getLLVMStyleWithColumns(25)));
2407   EXPECT_EQ("#define A \\\n"
2408             "          \\\n"
2409             "  a;      \\\n"
2410             "  b;",
2411             format("#define A \\\n"
2412                    "          \\\n"
2413                    "  a;      \\\n"
2414                    "  b;",
2415                    getLLVMStyleWithColumns(11)));
2416   EXPECT_EQ("#define A \\\n"
2417             "  a;      \\\n"
2418             "          \\\n"
2419             "  b;",
2420             format("#define A \\\n"
2421                    "  a;      \\\n"
2422                    "          \\\n"
2423                    "  b;",
2424                    getLLVMStyleWithColumns(11)));
2425 }
2426 
2427 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2428   verifyIncompleteFormat("#define A :");
2429   verifyFormat("#define SOMECASES  \\\n"
2430                "  case 1:          \\\n"
2431                "  case 2\n",
2432                getLLVMStyleWithColumns(20));
2433   verifyFormat("#define MACRO(a) \\\n"
2434                "  if (a)         \\\n"
2435                "    f();         \\\n"
2436                "  else           \\\n"
2437                "    g()",
2438                getLLVMStyleWithColumns(18));
2439   verifyFormat("#define A template <typename T>");
2440   verifyIncompleteFormat("#define STR(x) #x\n"
2441                          "f(STR(this_is_a_string_literal{));");
2442   verifyFormat("#pragma omp threadprivate( \\\n"
2443                "    y)), // expected-warning",
2444                getLLVMStyleWithColumns(28));
2445   verifyFormat("#d, = };");
2446   verifyFormat("#if \"a");
2447   verifyIncompleteFormat("({\n"
2448                          "#define b     \\\n"
2449                          "  }           \\\n"
2450                          "  a\n"
2451                          "a",
2452                          getLLVMStyleWithColumns(15));
2453   verifyFormat("#define A     \\\n"
2454                "  {           \\\n"
2455                "    {\n"
2456                "#define B     \\\n"
2457                "  }           \\\n"
2458                "  }",
2459                getLLVMStyleWithColumns(15));
2460   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2461   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2462   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2463   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2464 }
2465 
2466 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2467   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2468   EXPECT_EQ("class A : public QObject {\n"
2469             "  Q_OBJECT\n"
2470             "\n"
2471             "  A() {}\n"
2472             "};",
2473             format("class A  :  public QObject {\n"
2474                    "     Q_OBJECT\n"
2475                    "\n"
2476                    "  A() {\n}\n"
2477                    "}  ;"));
2478   EXPECT_EQ("MACRO\n"
2479             "/*static*/ int i;",
2480             format("MACRO\n"
2481                    " /*static*/ int   i;"));
2482   EXPECT_EQ("SOME_MACRO\n"
2483             "namespace {\n"
2484             "void f();\n"
2485             "} // namespace",
2486             format("SOME_MACRO\n"
2487                    "  namespace    {\n"
2488                    "void   f(  );\n"
2489                    "} // namespace"));
2490   // Only if the identifier contains at least 5 characters.
2491   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2492   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2493   // Only if everything is upper case.
2494   EXPECT_EQ("class A : public QObject {\n"
2495             "  Q_Object A() {}\n"
2496             "};",
2497             format("class A  :  public QObject {\n"
2498                    "     Q_Object\n"
2499                    "  A() {\n}\n"
2500                    "}  ;"));
2501 
2502   // Only if the next line can actually start an unwrapped line.
2503   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2504             format("SOME_WEIRD_LOG_MACRO\n"
2505                    "<< SomeThing;"));
2506 
2507   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2508                "(n, buffers))\n",
2509                getChromiumStyle(FormatStyle::LK_Cpp));
2510 }
2511 
2512 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2513   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2514             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2515             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2516             "class X {};\n"
2517             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2518             "int *createScopDetectionPass() { return 0; }",
2519             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2520                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2521                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2522                    "  class X {};\n"
2523                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2524                    "  int *createScopDetectionPass() { return 0; }"));
2525   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2526   // braces, so that inner block is indented one level more.
2527   EXPECT_EQ("int q() {\n"
2528             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2529             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2530             "  IPC_END_MESSAGE_MAP()\n"
2531             "}",
2532             format("int q() {\n"
2533                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2534                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2535                    "  IPC_END_MESSAGE_MAP()\n"
2536                    "}"));
2537 
2538   // Same inside macros.
2539   EXPECT_EQ("#define LIST(L) \\\n"
2540             "  L(A)          \\\n"
2541             "  L(B)          \\\n"
2542             "  L(C)",
2543             format("#define LIST(L) \\\n"
2544                    "  L(A) \\\n"
2545                    "  L(B) \\\n"
2546                    "  L(C)",
2547                    getGoogleStyle()));
2548 
2549   // These must not be recognized as macros.
2550   EXPECT_EQ("int q() {\n"
2551             "  f(x);\n"
2552             "  f(x) {}\n"
2553             "  f(x)->g();\n"
2554             "  f(x)->*g();\n"
2555             "  f(x).g();\n"
2556             "  f(x) = x;\n"
2557             "  f(x) += x;\n"
2558             "  f(x) -= x;\n"
2559             "  f(x) *= x;\n"
2560             "  f(x) /= x;\n"
2561             "  f(x) %= x;\n"
2562             "  f(x) &= x;\n"
2563             "  f(x) |= x;\n"
2564             "  f(x) ^= x;\n"
2565             "  f(x) >>= x;\n"
2566             "  f(x) <<= x;\n"
2567             "  f(x)[y].z();\n"
2568             "  LOG(INFO) << x;\n"
2569             "  ifstream(x) >> x;\n"
2570             "}\n",
2571             format("int q() {\n"
2572                    "  f(x)\n;\n"
2573                    "  f(x)\n {}\n"
2574                    "  f(x)\n->g();\n"
2575                    "  f(x)\n->*g();\n"
2576                    "  f(x)\n.g();\n"
2577                    "  f(x)\n = x;\n"
2578                    "  f(x)\n += x;\n"
2579                    "  f(x)\n -= x;\n"
2580                    "  f(x)\n *= x;\n"
2581                    "  f(x)\n /= x;\n"
2582                    "  f(x)\n %= x;\n"
2583                    "  f(x)\n &= x;\n"
2584                    "  f(x)\n |= x;\n"
2585                    "  f(x)\n ^= x;\n"
2586                    "  f(x)\n >>= x;\n"
2587                    "  f(x)\n <<= x;\n"
2588                    "  f(x)\n[y].z();\n"
2589                    "  LOG(INFO)\n << x;\n"
2590                    "  ifstream(x)\n >> x;\n"
2591                    "}\n"));
2592   EXPECT_EQ("int q() {\n"
2593             "  F(x)\n"
2594             "  if (1) {\n"
2595             "  }\n"
2596             "  F(x)\n"
2597             "  while (1) {\n"
2598             "  }\n"
2599             "  F(x)\n"
2600             "  G(x);\n"
2601             "  F(x)\n"
2602             "  try {\n"
2603             "    Q();\n"
2604             "  } catch (...) {\n"
2605             "  }\n"
2606             "}\n",
2607             format("int q() {\n"
2608                    "F(x)\n"
2609                    "if (1) {}\n"
2610                    "F(x)\n"
2611                    "while (1) {}\n"
2612                    "F(x)\n"
2613                    "G(x);\n"
2614                    "F(x)\n"
2615                    "try { Q(); } catch (...) {}\n"
2616                    "}\n"));
2617   EXPECT_EQ("class A {\n"
2618             "  A() : t(0) {}\n"
2619             "  A(int i) noexcept() : {}\n"
2620             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2621             "  try : t(0) {\n"
2622             "  } catch (...) {\n"
2623             "  }\n"
2624             "};",
2625             format("class A {\n"
2626                    "  A()\n : t(0) {}\n"
2627                    "  A(int i)\n noexcept() : {}\n"
2628                    "  A(X x)\n"
2629                    "  try : t(0) {} catch (...) {}\n"
2630                    "};"));
2631   FormatStyle Style = getLLVMStyle();
2632   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2633   Style.BraceWrapping.AfterControlStatement = true;
2634   Style.BraceWrapping.AfterFunction = true;
2635   EXPECT_EQ("void f()\n"
2636             "try\n"
2637             "{\n"
2638             "}",
2639             format("void f() try {\n"
2640                    "}", Style));
2641   EXPECT_EQ("class SomeClass {\n"
2642             "public:\n"
2643             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2644             "};",
2645             format("class SomeClass {\n"
2646                    "public:\n"
2647                    "  SomeClass()\n"
2648                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2649                    "};"));
2650   EXPECT_EQ("class SomeClass {\n"
2651             "public:\n"
2652             "  SomeClass()\n"
2653             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2654             "};",
2655             format("class SomeClass {\n"
2656                    "public:\n"
2657                    "  SomeClass()\n"
2658                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2659                    "};",
2660                    getLLVMStyleWithColumns(40)));
2661 
2662   verifyFormat("MACRO(>)");
2663 
2664   // Some macros contain an implicit semicolon.
2665   Style = getLLVMStyle();
2666   Style.StatementMacros.push_back("FOO");
2667   verifyFormat("FOO(a) int b = 0;");
2668   verifyFormat("FOO(a)\n"
2669                "int b = 0;",
2670                Style);
2671   verifyFormat("FOO(a);\n"
2672                "int b = 0;",
2673                Style);
2674   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
2675                "int b = 0;",
2676                Style);
2677   verifyFormat("FOO()\n"
2678                "int b = 0;",
2679                Style);
2680   verifyFormat("FOO\n"
2681                "int b = 0;",
2682                Style);
2683   verifyFormat("void f() {\n"
2684                "  FOO(a)\n"
2685                "  return a;\n"
2686                "}",
2687                Style);
2688   verifyFormat("FOO(a)\n"
2689                "FOO(b)",
2690                Style);
2691   verifyFormat("int a = 0;\n"
2692                "FOO(b)\n"
2693                "int c = 0;",
2694                Style);
2695   verifyFormat("int a = 0;\n"
2696                "int x = FOO(a)\n"
2697                "int b = 0;",
2698                Style);
2699   verifyFormat("void foo(int a) { FOO(a) }\n"
2700                "uint32_t bar() {}",
2701                Style);
2702 }
2703 
2704 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2705   verifyFormat("#define A \\\n"
2706                "  f({     \\\n"
2707                "    g();  \\\n"
2708                "  });",
2709                getLLVMStyleWithColumns(11));
2710 }
2711 
2712 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2713   FormatStyle Style = getLLVMStyle();
2714   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2715   Style.ColumnLimit = 40;
2716   verifyFormat("#ifdef _WIN32\n"
2717                "#define A 0\n"
2718                "#ifdef VAR2\n"
2719                "#define B 1\n"
2720                "#include <someheader.h>\n"
2721                "#define MACRO                          \\\n"
2722                "  some_very_long_func_aaaaaaaaaa();\n"
2723                "#endif\n"
2724                "#else\n"
2725                "#define A 1\n"
2726                "#endif",
2727                Style);
2728   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2729   verifyFormat("#ifdef _WIN32\n"
2730                "#  define A 0\n"
2731                "#  ifdef VAR2\n"
2732                "#    define B 1\n"
2733                "#    include <someheader.h>\n"
2734                "#    define MACRO                      \\\n"
2735                "      some_very_long_func_aaaaaaaaaa();\n"
2736                "#  endif\n"
2737                "#else\n"
2738                "#  define A 1\n"
2739                "#endif",
2740                Style);
2741   verifyFormat("#if A\n"
2742                "#  define MACRO                        \\\n"
2743                "    void a(int x) {                    \\\n"
2744                "      b();                             \\\n"
2745                "      c();                             \\\n"
2746                "      d();                             \\\n"
2747                "      e();                             \\\n"
2748                "      f();                             \\\n"
2749                "    }\n"
2750                "#endif",
2751                Style);
2752   // Comments before include guard.
2753   verifyFormat("// file comment\n"
2754                "// file comment\n"
2755                "#ifndef HEADER_H\n"
2756                "#define HEADER_H\n"
2757                "code();\n"
2758                "#endif",
2759                Style);
2760   // Test with include guards.
2761   verifyFormat("#ifndef HEADER_H\n"
2762                "#define HEADER_H\n"
2763                "code();\n"
2764                "#endif",
2765                Style);
2766   // Include guards must have a #define with the same variable immediately
2767   // after #ifndef.
2768   verifyFormat("#ifndef NOT_GUARD\n"
2769                "#  define FOO\n"
2770                "code();\n"
2771                "#endif",
2772                Style);
2773 
2774   // Include guards must cover the entire file.
2775   verifyFormat("code();\n"
2776                "code();\n"
2777                "#ifndef NOT_GUARD\n"
2778                "#  define NOT_GUARD\n"
2779                "code();\n"
2780                "#endif",
2781                Style);
2782   verifyFormat("#ifndef NOT_GUARD\n"
2783                "#  define NOT_GUARD\n"
2784                "code();\n"
2785                "#endif\n"
2786                "code();",
2787                Style);
2788   // Test with trailing blank lines.
2789   verifyFormat("#ifndef HEADER_H\n"
2790                "#define HEADER_H\n"
2791                "code();\n"
2792                "#endif\n",
2793                Style);
2794   // Include guards don't have #else.
2795   verifyFormat("#ifndef NOT_GUARD\n"
2796                "#  define NOT_GUARD\n"
2797                "code();\n"
2798                "#else\n"
2799                "#endif",
2800                Style);
2801   verifyFormat("#ifndef NOT_GUARD\n"
2802                "#  define NOT_GUARD\n"
2803                "code();\n"
2804                "#elif FOO\n"
2805                "#endif",
2806                Style);
2807   // Non-identifier #define after potential include guard.
2808   verifyFormat("#ifndef FOO\n"
2809                "#  define 1\n"
2810                "#endif\n",
2811                Style);
2812   // #if closes past last non-preprocessor line.
2813   verifyFormat("#ifndef FOO\n"
2814                "#define FOO\n"
2815                "#if 1\n"
2816                "int i;\n"
2817                "#  define A 0\n"
2818                "#endif\n"
2819                "#endif\n",
2820                Style);
2821   // FIXME: This doesn't handle the case where there's code between the
2822   // #ifndef and #define but all other conditions hold. This is because when
2823   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2824   // previous code line yet, so we can't detect it.
2825   EXPECT_EQ("#ifndef NOT_GUARD\n"
2826             "code();\n"
2827             "#define NOT_GUARD\n"
2828             "code();\n"
2829             "#endif",
2830             format("#ifndef NOT_GUARD\n"
2831                    "code();\n"
2832                    "#  define NOT_GUARD\n"
2833                    "code();\n"
2834                    "#endif",
2835                    Style));
2836   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2837   // be outside an include guard. Examples are #pragma once and
2838   // #pragma GCC diagnostic, or anything else that does not change the meaning
2839   // of the file if it's included multiple times.
2840   EXPECT_EQ("#ifdef WIN32\n"
2841             "#  pragma once\n"
2842             "#endif\n"
2843             "#ifndef HEADER_H\n"
2844             "#  define HEADER_H\n"
2845             "code();\n"
2846             "#endif",
2847             format("#ifdef WIN32\n"
2848                    "#  pragma once\n"
2849                    "#endif\n"
2850                    "#ifndef HEADER_H\n"
2851                    "#define HEADER_H\n"
2852                    "code();\n"
2853                    "#endif",
2854                    Style));
2855   // FIXME: This does not detect when there is a single non-preprocessor line
2856   // in front of an include-guard-like structure where other conditions hold
2857   // because ScopedLineState hides the line.
2858   EXPECT_EQ("code();\n"
2859             "#ifndef HEADER_H\n"
2860             "#define HEADER_H\n"
2861             "code();\n"
2862             "#endif",
2863             format("code();\n"
2864                    "#ifndef HEADER_H\n"
2865                    "#  define HEADER_H\n"
2866                    "code();\n"
2867                    "#endif",
2868                    Style));
2869   // Keep comments aligned with #, otherwise indent comments normally. These
2870   // tests cannot use verifyFormat because messUp manipulates leading
2871   // whitespace.
2872   {
2873     const char *Expected = ""
2874                            "void f() {\n"
2875                            "#if 1\n"
2876                            "// Preprocessor aligned.\n"
2877                            "#  define A 0\n"
2878                            "  // Code. Separated by blank line.\n"
2879                            "\n"
2880                            "#  define B 0\n"
2881                            "  // Code. Not aligned with #\n"
2882                            "#  define C 0\n"
2883                            "#endif";
2884     const char *ToFormat = ""
2885                            "void f() {\n"
2886                            "#if 1\n"
2887                            "// Preprocessor aligned.\n"
2888                            "#  define A 0\n"
2889                            "// Code. Separated by blank line.\n"
2890                            "\n"
2891                            "#  define B 0\n"
2892                            "   // Code. Not aligned with #\n"
2893                            "#  define C 0\n"
2894                            "#endif";
2895     EXPECT_EQ(Expected, format(ToFormat, Style));
2896     EXPECT_EQ(Expected, format(Expected, Style));
2897   }
2898   // Keep block quotes aligned.
2899   {
2900     const char *Expected = ""
2901                            "void f() {\n"
2902                            "#if 1\n"
2903                            "/* Preprocessor aligned. */\n"
2904                            "#  define A 0\n"
2905                            "  /* Code. Separated by blank line. */\n"
2906                            "\n"
2907                            "#  define B 0\n"
2908                            "  /* Code. Not aligned with # */\n"
2909                            "#  define C 0\n"
2910                            "#endif";
2911     const char *ToFormat = ""
2912                            "void f() {\n"
2913                            "#if 1\n"
2914                            "/* Preprocessor aligned. */\n"
2915                            "#  define A 0\n"
2916                            "/* Code. Separated by blank line. */\n"
2917                            "\n"
2918                            "#  define B 0\n"
2919                            "   /* Code. Not aligned with # */\n"
2920                            "#  define C 0\n"
2921                            "#endif";
2922     EXPECT_EQ(Expected, format(ToFormat, Style));
2923     EXPECT_EQ(Expected, format(Expected, Style));
2924   }
2925   // Keep comments aligned with un-indented directives.
2926   {
2927     const char *Expected = ""
2928                            "void f() {\n"
2929                            "// Preprocessor aligned.\n"
2930                            "#define A 0\n"
2931                            "  // Code. Separated by blank line.\n"
2932                            "\n"
2933                            "#define B 0\n"
2934                            "  // Code. Not aligned with #\n"
2935                            "#define C 0\n";
2936     const char *ToFormat = ""
2937                            "void f() {\n"
2938                            "// Preprocessor aligned.\n"
2939                            "#define A 0\n"
2940                            "// Code. Separated by blank line.\n"
2941                            "\n"
2942                            "#define B 0\n"
2943                            "   // Code. Not aligned with #\n"
2944                            "#define C 0\n";
2945     EXPECT_EQ(Expected, format(ToFormat, Style));
2946     EXPECT_EQ(Expected, format(Expected, Style));
2947   }
2948   // Test with tabs.
2949   Style.UseTab = FormatStyle::UT_Always;
2950   Style.IndentWidth = 8;
2951   Style.TabWidth = 8;
2952   verifyFormat("#ifdef _WIN32\n"
2953                "#\tdefine A 0\n"
2954                "#\tifdef VAR2\n"
2955                "#\t\tdefine B 1\n"
2956                "#\t\tinclude <someheader.h>\n"
2957                "#\t\tdefine MACRO          \\\n"
2958                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2959                "#\tendif\n"
2960                "#else\n"
2961                "#\tdefine A 1\n"
2962                "#endif",
2963                Style);
2964 
2965   // Regression test: Multiline-macro inside include guards.
2966   verifyFormat("#ifndef HEADER_H\n"
2967                "#define HEADER_H\n"
2968                "#define A()        \\\n"
2969                "  int i;           \\\n"
2970                "  int j;\n"
2971                "#endif // HEADER_H",
2972                getLLVMStyleWithColumns(20));
2973 }
2974 
2975 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2976   verifyFormat("{\n  { a #c; }\n}");
2977 }
2978 
2979 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2980   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2981             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2982   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2983             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2984 }
2985 
2986 TEST_F(FormatTest, EscapedNewlines) {
2987   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2988   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2989             format("#define A \\\nint i;\\\n  int j;", Narrow));
2990   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2991   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2992   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2993   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2994 
2995   FormatStyle AlignLeft = getLLVMStyle();
2996   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2997   EXPECT_EQ("#define MACRO(x) \\\n"
2998             "private:         \\\n"
2999             "  int x(int a);\n",
3000             format("#define MACRO(x) \\\n"
3001                    "private:         \\\n"
3002                    "  int x(int a);\n",
3003                    AlignLeft));
3004 
3005   // CRLF line endings
3006   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
3007             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
3008   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
3009   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3010   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
3011   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
3012   EXPECT_EQ("#define MACRO(x) \\\r\n"
3013             "private:         \\\r\n"
3014             "  int x(int a);\r\n",
3015             format("#define MACRO(x) \\\r\n"
3016                    "private:         \\\r\n"
3017                    "  int x(int a);\r\n",
3018                    AlignLeft));
3019 
3020   FormatStyle DontAlign = getLLVMStyle();
3021   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
3022   DontAlign.MaxEmptyLinesToKeep = 3;
3023   // FIXME: can't use verifyFormat here because the newline before
3024   // "public:" is not inserted the first time it's reformatted
3025   EXPECT_EQ("#define A \\\n"
3026             "  class Foo { \\\n"
3027             "    void bar(); \\\n"
3028             "\\\n"
3029             "\\\n"
3030             "\\\n"
3031             "  public: \\\n"
3032             "    void baz(); \\\n"
3033             "  };",
3034             format("#define A \\\n"
3035                    "  class Foo { \\\n"
3036                    "    void bar(); \\\n"
3037                    "\\\n"
3038                    "\\\n"
3039                    "\\\n"
3040                    "  public: \\\n"
3041                    "    void baz(); \\\n"
3042                    "  };",
3043                    DontAlign));
3044 }
3045 
3046 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3047   verifyFormat("#define A \\\n"
3048                "  int v(  \\\n"
3049                "      a); \\\n"
3050                "  int i;",
3051                getLLVMStyleWithColumns(11));
3052 }
3053 
3054 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3055   EXPECT_EQ(
3056       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3057       "                      \\\n"
3058       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3059       "\n"
3060       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3061       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3062       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3063              "\\\n"
3064              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3065              "  \n"
3066              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3067              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3068 }
3069 
3070 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3071   EXPECT_EQ("int\n"
3072             "#define A\n"
3073             "    a;",
3074             format("int\n#define A\na;"));
3075   verifyFormat("functionCallTo(\n"
3076                "    someOtherFunction(\n"
3077                "        withSomeParameters, whichInSequence,\n"
3078                "        areLongerThanALine(andAnotherCall,\n"
3079                "#define A B\n"
3080                "                           withMoreParamters,\n"
3081                "                           whichStronglyInfluenceTheLayout),\n"
3082                "        andMoreParameters),\n"
3083                "    trailing);",
3084                getLLVMStyleWithColumns(69));
3085   verifyFormat("Foo::Foo()\n"
3086                "#ifdef BAR\n"
3087                "    : baz(0)\n"
3088                "#endif\n"
3089                "{\n"
3090                "}");
3091   verifyFormat("void f() {\n"
3092                "  if (true)\n"
3093                "#ifdef A\n"
3094                "    f(42);\n"
3095                "  x();\n"
3096                "#else\n"
3097                "    g();\n"
3098                "  x();\n"
3099                "#endif\n"
3100                "}");
3101   verifyFormat("void f(param1, param2,\n"
3102                "       param3,\n"
3103                "#ifdef A\n"
3104                "       param4(param5,\n"
3105                "#ifdef A1\n"
3106                "              param6,\n"
3107                "#ifdef A2\n"
3108                "              param7),\n"
3109                "#else\n"
3110                "              param8),\n"
3111                "       param9,\n"
3112                "#endif\n"
3113                "       param10,\n"
3114                "#endif\n"
3115                "       param11)\n"
3116                "#else\n"
3117                "       param12)\n"
3118                "#endif\n"
3119                "{\n"
3120                "  x();\n"
3121                "}",
3122                getLLVMStyleWithColumns(28));
3123   verifyFormat("#if 1\n"
3124                "int i;");
3125   verifyFormat("#if 1\n"
3126                "#endif\n"
3127                "#if 1\n"
3128                "#else\n"
3129                "#endif\n");
3130   verifyFormat("DEBUG({\n"
3131                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3132                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3133                "});\n"
3134                "#if a\n"
3135                "#else\n"
3136                "#endif");
3137 
3138   verifyIncompleteFormat("void f(\n"
3139                          "#if A\n"
3140                          ");\n"
3141                          "#else\n"
3142                          "#endif");
3143 }
3144 
3145 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3146   verifyFormat("#endif\n"
3147                "#if B");
3148 }
3149 
3150 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3151   FormatStyle SingleLine = getLLVMStyle();
3152   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3153   verifyFormat("#if 0\n"
3154                "#elif 1\n"
3155                "#endif\n"
3156                "void foo() {\n"
3157                "  if (test) foo2();\n"
3158                "}",
3159                SingleLine);
3160 }
3161 
3162 TEST_F(FormatTest, LayoutBlockInsideParens) {
3163   verifyFormat("functionCall({ int i; });");
3164   verifyFormat("functionCall({\n"
3165                "  int i;\n"
3166                "  int j;\n"
3167                "});");
3168   verifyFormat("functionCall(\n"
3169                "    {\n"
3170                "      int i;\n"
3171                "      int j;\n"
3172                "    },\n"
3173                "    aaaa, bbbb, cccc);");
3174   verifyFormat("functionA(functionB({\n"
3175                "            int i;\n"
3176                "            int j;\n"
3177                "          }),\n"
3178                "          aaaa, bbbb, cccc);");
3179   verifyFormat("functionCall(\n"
3180                "    {\n"
3181                "      int i;\n"
3182                "      int j;\n"
3183                "    },\n"
3184                "    aaaa, bbbb, // comment\n"
3185                "    cccc);");
3186   verifyFormat("functionA(functionB({\n"
3187                "            int i;\n"
3188                "            int j;\n"
3189                "          }),\n"
3190                "          aaaa, bbbb, // comment\n"
3191                "          cccc);");
3192   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3193   verifyFormat("functionCall(aaaa, bbbb, {\n"
3194                "  int i;\n"
3195                "  int j;\n"
3196                "});");
3197   verifyFormat(
3198       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3199       "    {\n"
3200       "      int i; // break\n"
3201       "    },\n"
3202       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3203       "                                     ccccccccccccccccc));");
3204   verifyFormat("DEBUG({\n"
3205                "  if (a)\n"
3206                "    f();\n"
3207                "});");
3208 }
3209 
3210 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3211   EXPECT_EQ("SOME_MACRO { int i; }\n"
3212             "int i;",
3213             format("  SOME_MACRO  {int i;}  int i;"));
3214 }
3215 
3216 TEST_F(FormatTest, LayoutNestedBlocks) {
3217   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3218                "  struct s {\n"
3219                "    int i;\n"
3220                "  };\n"
3221                "  s kBitsToOs[] = {{10}};\n"
3222                "  for (int i = 0; i < 10; ++i)\n"
3223                "    return;\n"
3224                "}");
3225   verifyFormat("call(parameter, {\n"
3226                "  something();\n"
3227                "  // Comment using all columns.\n"
3228                "  somethingelse();\n"
3229                "});",
3230                getLLVMStyleWithColumns(40));
3231   verifyFormat("DEBUG( //\n"
3232                "    { f(); }, a);");
3233   verifyFormat("DEBUG( //\n"
3234                "    {\n"
3235                "      f(); //\n"
3236                "    },\n"
3237                "    a);");
3238 
3239   EXPECT_EQ("call(parameter, {\n"
3240             "  something();\n"
3241             "  // Comment too\n"
3242             "  // looooooooooong.\n"
3243             "  somethingElse();\n"
3244             "});",
3245             format("call(parameter, {\n"
3246                    "  something();\n"
3247                    "  // Comment too looooooooooong.\n"
3248                    "  somethingElse();\n"
3249                    "});",
3250                    getLLVMStyleWithColumns(29)));
3251   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3252   EXPECT_EQ("DEBUG({ // comment\n"
3253             "  int i;\n"
3254             "});",
3255             format("DEBUG({ // comment\n"
3256                    "int  i;\n"
3257                    "});"));
3258   EXPECT_EQ("DEBUG({\n"
3259             "  int i;\n"
3260             "\n"
3261             "  // comment\n"
3262             "  int j;\n"
3263             "});",
3264             format("DEBUG({\n"
3265                    "  int  i;\n"
3266                    "\n"
3267                    "  // comment\n"
3268                    "  int  j;\n"
3269                    "});"));
3270 
3271   verifyFormat("DEBUG({\n"
3272                "  if (a)\n"
3273                "    return;\n"
3274                "});");
3275   verifyGoogleFormat("DEBUG({\n"
3276                      "  if (a) return;\n"
3277                      "});");
3278   FormatStyle Style = getGoogleStyle();
3279   Style.ColumnLimit = 45;
3280   verifyFormat("Debug(\n"
3281                "    aaaaa,\n"
3282                "    {\n"
3283                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3284                "    },\n"
3285                "    a);",
3286                Style);
3287 
3288   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3289 
3290   verifyNoCrash("^{v^{a}}");
3291 }
3292 
3293 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3294   EXPECT_EQ("#define MACRO()                     \\\n"
3295             "  Debug(aaa, /* force line break */ \\\n"
3296             "        {                           \\\n"
3297             "          int i;                    \\\n"
3298             "          int j;                    \\\n"
3299             "        })",
3300             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3301                    "          {  int   i;  int  j;   })",
3302                    getGoogleStyle()));
3303 
3304   EXPECT_EQ("#define A                                       \\\n"
3305             "  [] {                                          \\\n"
3306             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3307             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3308             "  }",
3309             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3310                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3311                    getGoogleStyle()));
3312 }
3313 
3314 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3315   EXPECT_EQ("{}", format("{}"));
3316   verifyFormat("enum E {};");
3317   verifyFormat("enum E {}");
3318 }
3319 
3320 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3321   FormatStyle Style = getLLVMStyle();
3322   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3323   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3324   verifyFormat("FOO_BEGIN\n"
3325                "  FOO_ENTRY\n"
3326                "FOO_END", Style);
3327   verifyFormat("FOO_BEGIN\n"
3328                "  NESTED_FOO_BEGIN\n"
3329                "    NESTED_FOO_ENTRY\n"
3330                "  NESTED_FOO_END\n"
3331                "FOO_END", Style);
3332   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3333                "  int x;\n"
3334                "  x = 1;\n"
3335                "FOO_END(Baz)", Style);
3336 }
3337 
3338 //===----------------------------------------------------------------------===//
3339 // Line break tests.
3340 //===----------------------------------------------------------------------===//
3341 
3342 TEST_F(FormatTest, PreventConfusingIndents) {
3343   verifyFormat(
3344       "void f() {\n"
3345       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3346       "                         parameter, parameter, parameter)),\n"
3347       "                     SecondLongCall(parameter));\n"
3348       "}");
3349   verifyFormat(
3350       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3351       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3352       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3353       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3354   verifyFormat(
3355       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3356       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3357       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3358       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3359   verifyFormat(
3360       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3361       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3362       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3363       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3364   verifyFormat("int a = bbbb && ccc &&\n"
3365                "        fffff(\n"
3366                "#define A Just forcing a new line\n"
3367                "            ddd);");
3368 }
3369 
3370 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3371   verifyFormat(
3372       "bool aaaaaaa =\n"
3373       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3374       "    bbbbbbbb();");
3375   verifyFormat(
3376       "bool aaaaaaa =\n"
3377       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3378       "    bbbbbbbb();");
3379 
3380   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3381                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3382                "    ccccccccc == ddddddddddd;");
3383   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3384                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3385                "    ccccccccc == ddddddddddd;");
3386   verifyFormat(
3387       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3388       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3389       "    ccccccccc == ddddddddddd;");
3390 
3391   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3392                "                 aaaaaa) &&\n"
3393                "         bbbbbb && cccccc;");
3394   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3395                "                 aaaaaa) >>\n"
3396                "         bbbbbb;");
3397   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3398                "    SourceMgr.getSpellingColumnNumber(\n"
3399                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3400                "    1);");
3401 
3402   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3403                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3404                "    cccccc) {\n}");
3405   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3406                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3407                "              cccccc) {\n}");
3408   verifyFormat("b = a &&\n"
3409                "    // Comment\n"
3410                "    b.c && d;");
3411 
3412   // If the LHS of a comparison is not a binary expression itself, the
3413   // additional linebreak confuses many people.
3414   verifyFormat(
3415       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3416       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3417       "}");
3418   verifyFormat(
3419       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3420       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3421       "}");
3422   verifyFormat(
3423       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3424       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3425       "}");
3426   verifyFormat(
3427       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3428       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
3429       "}");
3430   // Even explicit parentheses stress the precedence enough to make the
3431   // additional break unnecessary.
3432   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3433                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3434                "}");
3435   // This cases is borderline, but with the indentation it is still readable.
3436   verifyFormat(
3437       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3438       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3439       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3440       "}",
3441       getLLVMStyleWithColumns(75));
3442 
3443   // If the LHS is a binary expression, we should still use the additional break
3444   // as otherwise the formatting hides the operator precedence.
3445   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3446                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3447                "    5) {\n"
3448                "}");
3449   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3450                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
3451                "    5) {\n"
3452                "}");
3453 
3454   FormatStyle OnePerLine = getLLVMStyle();
3455   OnePerLine.BinPackParameters = false;
3456   verifyFormat(
3457       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3458       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3459       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3460       OnePerLine);
3461 
3462   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3463                "                .aaa(aaaaaaaaaaaaa) *\n"
3464                "            aaaaaaa +\n"
3465                "        aaaaaaa;",
3466                getLLVMStyleWithColumns(40));
3467 }
3468 
3469 TEST_F(FormatTest, ExpressionIndentation) {
3470   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3471                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3472                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3473                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3474                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3475                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3476                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3477                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3478                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3479   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3480                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3481                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3482                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3483   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3484                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3485                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3486                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3487   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3488                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3489                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3490                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3491   verifyFormat("if () {\n"
3492                "} else if (aaaaa && bbbbb > // break\n"
3493                "                        ccccc) {\n"
3494                "}");
3495   verifyFormat("if () {\n"
3496                "} else if (aaaaa &&\n"
3497                "           bbbbb > // break\n"
3498                "               ccccc &&\n"
3499                "           ddddd) {\n"
3500                "}");
3501 
3502   // Presence of a trailing comment used to change indentation of b.
3503   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3504                "       b;\n"
3505                "return aaaaaaaaaaaaaaaaaaa +\n"
3506                "       b; //",
3507                getLLVMStyleWithColumns(30));
3508 }
3509 
3510 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3511   // Not sure what the best system is here. Like this, the LHS can be found
3512   // immediately above an operator (everything with the same or a higher
3513   // indent). The RHS is aligned right of the operator and so compasses
3514   // everything until something with the same indent as the operator is found.
3515   // FIXME: Is this a good system?
3516   FormatStyle Style = getLLVMStyle();
3517   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3518   verifyFormat(
3519       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3520       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3521       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3522       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3523       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3524       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3525       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3526       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3527       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3528       Style);
3529   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3530                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3531                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3532                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3533                Style);
3534   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3535                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3536                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3537                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3538                Style);
3539   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3540                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3541                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3542                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3543                Style);
3544   verifyFormat("if () {\n"
3545                "} else if (aaaaa\n"
3546                "           && bbbbb // break\n"
3547                "                  > ccccc) {\n"
3548                "}",
3549                Style);
3550   verifyFormat("return (a)\n"
3551                "       // comment\n"
3552                "       + b;",
3553                Style);
3554   verifyFormat(
3555       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3556       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3557       "             + cc;",
3558       Style);
3559 
3560   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3561                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3562                Style);
3563 
3564   // Forced by comments.
3565   verifyFormat(
3566       "unsigned ContentSize =\n"
3567       "    sizeof(int16_t)   // DWARF ARange version number\n"
3568       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3569       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3570       "    + sizeof(int8_t); // Segment Size (in bytes)");
3571 
3572   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3573                "       == boost::fusion::at_c<1>(iiii).second;",
3574                Style);
3575 
3576   Style.ColumnLimit = 60;
3577   verifyFormat("zzzzzzzzzz\n"
3578                "    = bbbbbbbbbbbbbbbbb\n"
3579                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3580                Style);
3581 
3582   Style.ColumnLimit = 80;
3583   Style.IndentWidth = 4;
3584   Style.TabWidth = 4;
3585   Style.UseTab = FormatStyle::UT_Always;
3586   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3587   Style.AlignOperands = false;
3588   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
3589             "\t&& (someOtherLongishConditionPart1\n"
3590             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
3591             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);",
3592                    Style));
3593 }
3594 
3595 TEST_F(FormatTest, EnforcedOperatorWraps) {
3596   // Here we'd like to wrap after the || operators, but a comment is forcing an
3597   // earlier wrap.
3598   verifyFormat("bool x = aaaaa //\n"
3599                "         || bbbbb\n"
3600                "         //\n"
3601                "         || cccc;");
3602 }
3603 
3604 TEST_F(FormatTest, NoOperandAlignment) {
3605   FormatStyle Style = getLLVMStyle();
3606   Style.AlignOperands = false;
3607   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3608                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3609                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3610                Style);
3611   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3612   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3613                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3614                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3615                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3616                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3617                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3618                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3619                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3620                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3621                Style);
3622 
3623   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3624                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3625                "    + cc;",
3626                Style);
3627   verifyFormat("int a = aa\n"
3628                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3629                "        * cccccccccccccccccccccccccccccccccccc;\n",
3630                Style);
3631 
3632   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3633   verifyFormat("return (a > b\n"
3634                "    // comment1\n"
3635                "    // comment2\n"
3636                "    || c);",
3637                Style);
3638 }
3639 
3640 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3641   FormatStyle Style = getLLVMStyle();
3642   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3643   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3644                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3645                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3646                Style);
3647 }
3648 
3649 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3650   FormatStyle Style = getLLVMStyle();
3651   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3652   Style.BinPackArguments = false;
3653   Style.ColumnLimit = 40;
3654   verifyFormat("void test() {\n"
3655                "  someFunction(\n"
3656                "      this + argument + is + quite\n"
3657                "      + long + so + it + gets + wrapped\n"
3658                "      + but + remains + bin - packed);\n"
3659                "}",
3660                Style);
3661   verifyFormat("void test() {\n"
3662                "  someFunction(arg1,\n"
3663                "               this + argument + is\n"
3664                "                   + quite + long + so\n"
3665                "                   + it + gets + wrapped\n"
3666                "                   + but + remains + bin\n"
3667                "                   - packed,\n"
3668                "               arg3);\n"
3669                "}",
3670                Style);
3671   verifyFormat("void test() {\n"
3672                "  someFunction(\n"
3673                "      arg1,\n"
3674                "      this + argument + has\n"
3675                "          + anotherFunc(nested,\n"
3676                "                        calls + whose\n"
3677                "                            + arguments\n"
3678                "                            + are + also\n"
3679                "                            + wrapped,\n"
3680                "                        in + addition)\n"
3681                "          + to + being + bin - packed,\n"
3682                "      arg3);\n"
3683                "}",
3684                Style);
3685 
3686   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3687   verifyFormat("void test() {\n"
3688                "  someFunction(\n"
3689                "      arg1,\n"
3690                "      this + argument + has +\n"
3691                "          anotherFunc(nested,\n"
3692                "                      calls + whose +\n"
3693                "                          arguments +\n"
3694                "                          are + also +\n"
3695                "                          wrapped,\n"
3696                "                      in + addition) +\n"
3697                "          to + being + bin - packed,\n"
3698                "      arg3);\n"
3699                "}",
3700                Style);
3701 }
3702 
3703 TEST_F(FormatTest, ConstructorInitializers) {
3704   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3705   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3706                getLLVMStyleWithColumns(45));
3707   verifyFormat("Constructor()\n"
3708                "    : Inttializer(FitsOnTheLine) {}",
3709                getLLVMStyleWithColumns(44));
3710   verifyFormat("Constructor()\n"
3711                "    : Inttializer(FitsOnTheLine) {}",
3712                getLLVMStyleWithColumns(43));
3713 
3714   verifyFormat("template <typename T>\n"
3715                "Constructor() : Initializer(FitsOnTheLine) {}",
3716                getLLVMStyleWithColumns(45));
3717 
3718   verifyFormat(
3719       "SomeClass::Constructor()\n"
3720       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3721 
3722   verifyFormat(
3723       "SomeClass::Constructor()\n"
3724       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3725       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3726   verifyFormat(
3727       "SomeClass::Constructor()\n"
3728       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3729       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3730   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3731                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3732                "    : aaaaaaaaaa(aaaaaa) {}");
3733 
3734   verifyFormat("Constructor()\n"
3735                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3736                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3737                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3738                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3739 
3740   verifyFormat("Constructor()\n"
3741                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3742                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3743 
3744   verifyFormat("Constructor(int Parameter = 0)\n"
3745                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3746                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3747   verifyFormat("Constructor()\n"
3748                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3749                "}",
3750                getLLVMStyleWithColumns(60));
3751   verifyFormat("Constructor()\n"
3752                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3753                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3754 
3755   // Here a line could be saved by splitting the second initializer onto two
3756   // lines, but that is not desirable.
3757   verifyFormat("Constructor()\n"
3758                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3759                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3760                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3761 
3762   FormatStyle OnePerLine = getLLVMStyle();
3763   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3764   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3765   verifyFormat("SomeClass::Constructor()\n"
3766                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3767                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3768                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3769                OnePerLine);
3770   verifyFormat("SomeClass::Constructor()\n"
3771                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3772                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3773                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3774                OnePerLine);
3775   verifyFormat("MyClass::MyClass(int var)\n"
3776                "    : some_var_(var),            // 4 space indent\n"
3777                "      some_other_var_(var + 1) { // lined up\n"
3778                "}",
3779                OnePerLine);
3780   verifyFormat("Constructor()\n"
3781                "    : aaaaa(aaaaaa),\n"
3782                "      aaaaa(aaaaaa),\n"
3783                "      aaaaa(aaaaaa),\n"
3784                "      aaaaa(aaaaaa),\n"
3785                "      aaaaa(aaaaaa) {}",
3786                OnePerLine);
3787   verifyFormat("Constructor()\n"
3788                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3789                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3790                OnePerLine);
3791   OnePerLine.BinPackParameters = false;
3792   verifyFormat(
3793       "Constructor()\n"
3794       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3795       "          aaaaaaaaaaa().aaa(),\n"
3796       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3797       OnePerLine);
3798   OnePerLine.ColumnLimit = 60;
3799   verifyFormat("Constructor()\n"
3800                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3801                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3802                OnePerLine);
3803 
3804   EXPECT_EQ("Constructor()\n"
3805             "    : // Comment forcing unwanted break.\n"
3806             "      aaaa(aaaa) {}",
3807             format("Constructor() :\n"
3808                    "    // Comment forcing unwanted break.\n"
3809                    "    aaaa(aaaa) {}"));
3810 }
3811 
3812 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3813   FormatStyle Style = getLLVMStyle();
3814   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3815 
3816   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3817   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3818                getStyleWithColumns(Style, 45));
3819   verifyFormat("Constructor() :\n"
3820                "    Initializer(FitsOnTheLine) {}",
3821                getStyleWithColumns(Style, 44));
3822   verifyFormat("Constructor() :\n"
3823                "    Initializer(FitsOnTheLine) {}",
3824                getStyleWithColumns(Style, 43));
3825 
3826   verifyFormat("template <typename T>\n"
3827                "Constructor() : Initializer(FitsOnTheLine) {}",
3828                getStyleWithColumns(Style, 50));
3829 
3830   verifyFormat(
3831       "SomeClass::Constructor() :\n"
3832       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3833 	  Style);
3834 
3835   verifyFormat(
3836       "SomeClass::Constructor() :\n"
3837       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3838       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3839 	  Style);
3840   verifyFormat(
3841       "SomeClass::Constructor() :\n"
3842       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3843       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3844 	  Style);
3845   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3846                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3847                "    aaaaaaaaaa(aaaaaa) {}",
3848 			   Style);
3849 
3850   verifyFormat("Constructor() :\n"
3851                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3852                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3853                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3854                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3855 			   Style);
3856 
3857   verifyFormat("Constructor() :\n"
3858                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3859                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3860 			   Style);
3861 
3862   verifyFormat("Constructor(int Parameter = 0) :\n"
3863                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3864                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3865 			   Style);
3866   verifyFormat("Constructor() :\n"
3867                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3868                "}",
3869                getStyleWithColumns(Style, 60));
3870   verifyFormat("Constructor() :\n"
3871                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3872                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3873 			   Style);
3874 
3875   // Here a line could be saved by splitting the second initializer onto two
3876   // lines, but that is not desirable.
3877   verifyFormat("Constructor() :\n"
3878                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3879                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3880                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3881 			   Style);
3882 
3883   FormatStyle OnePerLine = Style;
3884   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3885   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3886   verifyFormat("SomeClass::Constructor() :\n"
3887                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3888                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3889                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3890                OnePerLine);
3891   verifyFormat("SomeClass::Constructor() :\n"
3892                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3893                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3894                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3895                OnePerLine);
3896   verifyFormat("MyClass::MyClass(int var) :\n"
3897                "    some_var_(var),            // 4 space indent\n"
3898                "    some_other_var_(var + 1) { // lined up\n"
3899                "}",
3900                OnePerLine);
3901   verifyFormat("Constructor() :\n"
3902                "    aaaaa(aaaaaa),\n"
3903                "    aaaaa(aaaaaa),\n"
3904                "    aaaaa(aaaaaa),\n"
3905                "    aaaaa(aaaaaa),\n"
3906                "    aaaaa(aaaaaa) {}",
3907                OnePerLine);
3908   verifyFormat("Constructor() :\n"
3909                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3910                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3911                OnePerLine);
3912   OnePerLine.BinPackParameters = false;
3913   verifyFormat(
3914       "Constructor() :\n"
3915       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3916       "        aaaaaaaaaaa().aaa(),\n"
3917       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3918       OnePerLine);
3919   OnePerLine.ColumnLimit = 60;
3920   verifyFormat("Constructor() :\n"
3921                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3922                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3923                OnePerLine);
3924 
3925   EXPECT_EQ("Constructor() :\n"
3926             "    // Comment forcing unwanted break.\n"
3927             "    aaaa(aaaa) {}",
3928             format("Constructor() :\n"
3929                    "    // Comment forcing unwanted break.\n"
3930                    "    aaaa(aaaa) {}",
3931 				   Style));
3932 
3933   Style.ColumnLimit = 0;
3934   verifyFormat("SomeClass::Constructor() :\n"
3935                "    a(a) {}",
3936                Style);
3937   verifyFormat("SomeClass::Constructor() noexcept :\n"
3938                "    a(a) {}",
3939                Style);
3940   verifyFormat("SomeClass::Constructor() :\n"
3941 			   "    a(a), b(b), c(c) {}",
3942                Style);
3943   verifyFormat("SomeClass::Constructor() :\n"
3944                "    a(a) {\n"
3945                "  foo();\n"
3946                "  bar();\n"
3947                "}",
3948                Style);
3949 
3950   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3951   verifyFormat("SomeClass::Constructor() :\n"
3952 			   "    a(a), b(b), c(c) {\n"
3953 			   "}",
3954                Style);
3955   verifyFormat("SomeClass::Constructor() :\n"
3956                "    a(a) {\n"
3957 			   "}",
3958                Style);
3959 
3960   Style.ColumnLimit = 80;
3961   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3962   Style.ConstructorInitializerIndentWidth = 2;
3963   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3964                Style);
3965   verifyFormat("SomeClass::Constructor() :\n"
3966                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3967                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3968                Style);
3969 
3970   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well
3971   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
3972   verifyFormat("class SomeClass\n"
3973                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3974                "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3975                Style);
3976   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
3977   verifyFormat("class SomeClass\n"
3978                "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3979                "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3980                Style);
3981   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
3982   verifyFormat("class SomeClass :\n"
3983                "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3984                "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
3985                Style);
3986 }
3987 
3988 #ifndef EXPENSIVE_CHECKS
3989 // Expensive checks enables libstdc++ checking which includes validating the
3990 // state of ranges used in std::priority_queue - this blows out the
3991 // runtime/scalability of the function and makes this test unacceptably slow.
3992 TEST_F(FormatTest, MemoizationTests) {
3993   // This breaks if the memoization lookup does not take \c Indent and
3994   // \c LastSpace into account.
3995   verifyFormat(
3996       "extern CFRunLoopTimerRef\n"
3997       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3998       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3999       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
4000       "                     CFRunLoopTimerContext *context) {}");
4001 
4002   // Deep nesting somewhat works around our memoization.
4003   verifyFormat(
4004       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4005       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4006       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4007       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
4008       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
4009       getLLVMStyleWithColumns(65));
4010   verifyFormat(
4011       "aaaaa(\n"
4012       "    aaaaa,\n"
4013       "    aaaaa(\n"
4014       "        aaaaa,\n"
4015       "        aaaaa(\n"
4016       "            aaaaa,\n"
4017       "            aaaaa(\n"
4018       "                aaaaa,\n"
4019       "                aaaaa(\n"
4020       "                    aaaaa,\n"
4021       "                    aaaaa(\n"
4022       "                        aaaaa,\n"
4023       "                        aaaaa(\n"
4024       "                            aaaaa,\n"
4025       "                            aaaaa(\n"
4026       "                                aaaaa,\n"
4027       "                                aaaaa(\n"
4028       "                                    aaaaa,\n"
4029       "                                    aaaaa(\n"
4030       "                                        aaaaa,\n"
4031       "                                        aaaaa(\n"
4032       "                                            aaaaa,\n"
4033       "                                            aaaaa(\n"
4034       "                                                aaaaa,\n"
4035       "                                                aaaaa))))))))))));",
4036       getLLVMStyleWithColumns(65));
4037   verifyFormat(
4038       "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"
4039       "                                  a),\n"
4040       "                                a),\n"
4041       "                              a),\n"
4042       "                            a),\n"
4043       "                          a),\n"
4044       "                        a),\n"
4045       "                      a),\n"
4046       "                    a),\n"
4047       "                  a),\n"
4048       "                a),\n"
4049       "              a),\n"
4050       "            a),\n"
4051       "          a),\n"
4052       "        a),\n"
4053       "      a),\n"
4054       "    a),\n"
4055       "  a)",
4056       getLLVMStyleWithColumns(65));
4057 
4058   // This test takes VERY long when memoization is broken.
4059   FormatStyle OnePerLine = getLLVMStyle();
4060   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4061   OnePerLine.BinPackParameters = false;
4062   std::string input = "Constructor()\n"
4063                       "    : aaaa(a,\n";
4064   for (unsigned i = 0, e = 80; i != e; ++i) {
4065     input += "           a,\n";
4066   }
4067   input += "           a) {}";
4068   verifyFormat(input, OnePerLine);
4069 }
4070 #endif
4071 
4072 TEST_F(FormatTest, BreaksAsHighAsPossible) {
4073   verifyFormat(
4074       "void f() {\n"
4075       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
4076       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
4077       "    f();\n"
4078       "}");
4079   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
4080                "    Intervals[i - 1].getRange().getLast()) {\n}");
4081 }
4082 
4083 TEST_F(FormatTest, BreaksFunctionDeclarations) {
4084   // Principially, we break function declarations in a certain order:
4085   // 1) break amongst arguments.
4086   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
4087                "                              Cccccccccccccc cccccccccccccc);");
4088   verifyFormat("template <class TemplateIt>\n"
4089                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
4090                "                            TemplateIt *stop) {}");
4091 
4092   // 2) break after return type.
4093   verifyFormat(
4094       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4095       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
4096       getGoogleStyle());
4097 
4098   // 3) break after (.
4099   verifyFormat(
4100       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
4101       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
4102       getGoogleStyle());
4103 
4104   // 4) break before after nested name specifiers.
4105   verifyFormat(
4106       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4107       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
4108       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
4109       getGoogleStyle());
4110 
4111   // However, there are exceptions, if a sufficient amount of lines can be
4112   // saved.
4113   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
4114   // more adjusting.
4115   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4116                "                                  Cccccccccccccc cccccccccc,\n"
4117                "                                  Cccccccccccccc cccccccccc,\n"
4118                "                                  Cccccccccccccc cccccccccc,\n"
4119                "                                  Cccccccccccccc cccccccccc);");
4120   verifyFormat(
4121       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4122       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4123       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4124       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
4125       getGoogleStyle());
4126   verifyFormat(
4127       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
4128       "                                          Cccccccccccccc cccccccccc,\n"
4129       "                                          Cccccccccccccc cccccccccc,\n"
4130       "                                          Cccccccccccccc cccccccccc,\n"
4131       "                                          Cccccccccccccc cccccccccc,\n"
4132       "                                          Cccccccccccccc cccccccccc,\n"
4133       "                                          Cccccccccccccc cccccccccc);");
4134   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4135                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4136                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4137                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
4138                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
4139 
4140   // Break after multi-line parameters.
4141   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4142                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4143                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4144                "    bbbb bbbb);");
4145   verifyFormat("void SomeLoooooooooooongFunction(\n"
4146                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
4147                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4148                "    int bbbbbbbbbbbbb);");
4149 
4150   // Treat overloaded operators like other functions.
4151   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4152                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
4153   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4154                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
4155   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
4156                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
4157   verifyGoogleFormat(
4158       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
4159       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4160   verifyGoogleFormat(
4161       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
4162       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
4163   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4164                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4165   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
4166                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
4167   verifyGoogleFormat(
4168       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
4169       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4170       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
4171   verifyGoogleFormat(
4172       "template <typename T>\n"
4173       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4174       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
4175       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
4176 
4177   FormatStyle Style = getLLVMStyle();
4178   Style.PointerAlignment = FormatStyle::PAS_Left;
4179   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4180                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
4181                Style);
4182   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4183                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4184                Style);
4185 }
4186 
4187 TEST_F(FormatTest, TrailingReturnType) {
4188   verifyFormat("auto foo() -> int;\n");
4189   verifyFormat("struct S {\n"
4190                "  auto bar() const -> int;\n"
4191                "};");
4192   verifyFormat("template <size_t Order, typename T>\n"
4193                "auto load_img(const std::string &filename)\n"
4194                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
4195   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
4196                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
4197   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
4198   verifyFormat("template <typename T>\n"
4199                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
4200                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
4201 
4202   // Not trailing return types.
4203   verifyFormat("void f() { auto a = b->c(); }");
4204 }
4205 
4206 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
4207   // Avoid breaking before trailing 'const' or other trailing annotations, if
4208   // they are not function-like.
4209   FormatStyle Style = getGoogleStyle();
4210   Style.ColumnLimit = 47;
4211   verifyFormat("void someLongFunction(\n"
4212                "    int someLoooooooooooooongParameter) const {\n}",
4213                getLLVMStyleWithColumns(47));
4214   verifyFormat("LoooooongReturnType\n"
4215                "someLoooooooongFunction() const {}",
4216                getLLVMStyleWithColumns(47));
4217   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
4218                "    const {}",
4219                Style);
4220   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4221                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
4222   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4223                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
4224   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
4225                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
4226   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
4227                "                   aaaaaaaaaaa aaaaa) const override;");
4228   verifyGoogleFormat(
4229       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4230       "    const override;");
4231 
4232   // Even if the first parameter has to be wrapped.
4233   verifyFormat("void someLongFunction(\n"
4234                "    int someLongParameter) const {}",
4235                getLLVMStyleWithColumns(46));
4236   verifyFormat("void someLongFunction(\n"
4237                "    int someLongParameter) const {}",
4238                Style);
4239   verifyFormat("void someLongFunction(\n"
4240                "    int someLongParameter) override {}",
4241                Style);
4242   verifyFormat("void someLongFunction(\n"
4243                "    int someLongParameter) OVERRIDE {}",
4244                Style);
4245   verifyFormat("void someLongFunction(\n"
4246                "    int someLongParameter) final {}",
4247                Style);
4248   verifyFormat("void someLongFunction(\n"
4249                "    int someLongParameter) FINAL {}",
4250                Style);
4251   verifyFormat("void someLongFunction(\n"
4252                "    int parameter) const override {}",
4253                Style);
4254 
4255   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4256   verifyFormat("void someLongFunction(\n"
4257                "    int someLongParameter) const\n"
4258                "{\n"
4259                "}",
4260                Style);
4261 
4262   // Unless these are unknown annotations.
4263   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
4264                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4265                "    LONG_AND_UGLY_ANNOTATION;");
4266 
4267   // Breaking before function-like trailing annotations is fine to keep them
4268   // close to their arguments.
4269   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4270                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4271   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4272                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
4273   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
4274                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
4275   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
4276                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
4277   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
4278 
4279   verifyFormat(
4280       "void aaaaaaaaaaaaaaaaaa()\n"
4281       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
4282       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
4283   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4284                "    __attribute__((unused));");
4285   verifyGoogleFormat(
4286       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4287       "    GUARDED_BY(aaaaaaaaaaaa);");
4288   verifyGoogleFormat(
4289       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4290       "    GUARDED_BY(aaaaaaaaaaaa);");
4291   verifyGoogleFormat(
4292       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4293       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4294   verifyGoogleFormat(
4295       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4296       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4297 }
4298 
4299 TEST_F(FormatTest, FunctionAnnotations) {
4300   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4301                "int OldFunction(const string &parameter) {}");
4302   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4303                "string OldFunction(const string &parameter) {}");
4304   verifyFormat("template <typename T>\n"
4305                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4306                "string OldFunction(const string &parameter) {}");
4307 
4308   // Not function annotations.
4309   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4310                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4311   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4312                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4313   verifyFormat("MACRO(abc).function() // wrap\n"
4314                "    << abc;");
4315   verifyFormat("MACRO(abc)->function() // wrap\n"
4316                "    << abc;");
4317   verifyFormat("MACRO(abc)::function() // wrap\n"
4318                "    << abc;");
4319 }
4320 
4321 TEST_F(FormatTest, BreaksDesireably) {
4322   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4323                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4324                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4325   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4326                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4327                "}");
4328 
4329   verifyFormat(
4330       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4331       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4332 
4333   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4334                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4335                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4336 
4337   verifyFormat(
4338       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4339       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4340       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4341       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4342       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4343 
4344   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4345                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4346 
4347   verifyFormat(
4348       "void f() {\n"
4349       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4350       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4351       "}");
4352   verifyFormat(
4353       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4354       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4355   verifyFormat(
4356       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4357       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4358   verifyFormat(
4359       "aaaaaa(aaa,\n"
4360       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4361       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4362       "       aaaa);");
4363   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4364                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4365                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4366 
4367   // Indent consistently independent of call expression and unary operator.
4368   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4369                "    dddddddddddddddddddddddddddddd));");
4370   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4371                "    dddddddddddddddddddddddddddddd));");
4372   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4373                "    dddddddddddddddddddddddddddddd));");
4374 
4375   // This test case breaks on an incorrect memoization, i.e. an optimization not
4376   // taking into account the StopAt value.
4377   verifyFormat(
4378       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4379       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4380       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4381       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4382 
4383   verifyFormat("{\n  {\n    {\n"
4384                "      Annotation.SpaceRequiredBefore =\n"
4385                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4386                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4387                "    }\n  }\n}");
4388 
4389   // Break on an outer level if there was a break on an inner level.
4390   EXPECT_EQ("f(g(h(a, // comment\n"
4391             "      b, c),\n"
4392             "    d, e),\n"
4393             "  x, y);",
4394             format("f(g(h(a, // comment\n"
4395                    "    b, c), d, e), x, y);"));
4396 
4397   // Prefer breaking similar line breaks.
4398   verifyFormat(
4399       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4400       "                             NSTrackingMouseEnteredAndExited |\n"
4401       "                             NSTrackingActiveAlways;");
4402 }
4403 
4404 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4405   FormatStyle NoBinPacking = getGoogleStyle();
4406   NoBinPacking.BinPackParameters = false;
4407   NoBinPacking.BinPackArguments = true;
4408   verifyFormat("void f() {\n"
4409                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4410                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4411                "}",
4412                NoBinPacking);
4413   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4414                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4415                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4416                NoBinPacking);
4417 
4418   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4419   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4420                "                        vector<int> bbbbbbbbbbbbbbb);",
4421                NoBinPacking);
4422   // FIXME: This behavior difference is probably not wanted. However, currently
4423   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4424   // template arguments from BreakBeforeParameter being set because of the
4425   // one-per-line formatting.
4426   verifyFormat(
4427       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4428       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4429       NoBinPacking);
4430   verifyFormat(
4431       "void fffffffffff(\n"
4432       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4433       "        aaaaaaaaaa);");
4434 }
4435 
4436 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4437   FormatStyle NoBinPacking = getGoogleStyle();
4438   NoBinPacking.BinPackParameters = false;
4439   NoBinPacking.BinPackArguments = false;
4440   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4441                "  aaaaaaaaaaaaaaaaaaaa,\n"
4442                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4443                NoBinPacking);
4444   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4445                "        aaaaaaaaaaaaa,\n"
4446                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4447                NoBinPacking);
4448   verifyFormat(
4449       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4450       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4451       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4452       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4453       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4454       NoBinPacking);
4455   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4456                "    .aaaaaaaaaaaaaaaaaa();",
4457                NoBinPacking);
4458   verifyFormat("void f() {\n"
4459                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4460                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4461                "}",
4462                NoBinPacking);
4463 
4464   verifyFormat(
4465       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4466       "             aaaaaaaaaaaa,\n"
4467       "             aaaaaaaaaaaa);",
4468       NoBinPacking);
4469   verifyFormat(
4470       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4471       "                               ddddddddddddddddddddddddddddd),\n"
4472       "             test);",
4473       NoBinPacking);
4474 
4475   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4476                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4477                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4478                "    aaaaaaaaaaaaaaaaaa;",
4479                NoBinPacking);
4480   verifyFormat("a(\"a\"\n"
4481                "  \"a\",\n"
4482                "  a);");
4483 
4484   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4485   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4486                "                aaaaaaaaa,\n"
4487                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4488                NoBinPacking);
4489   verifyFormat(
4490       "void f() {\n"
4491       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4492       "      .aaaaaaa();\n"
4493       "}",
4494       NoBinPacking);
4495   verifyFormat(
4496       "template <class SomeType, class SomeOtherType>\n"
4497       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4498       NoBinPacking);
4499 }
4500 
4501 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4502   FormatStyle Style = getLLVMStyleWithColumns(15);
4503   Style.ExperimentalAutoDetectBinPacking = true;
4504   EXPECT_EQ("aaa(aaaa,\n"
4505             "    aaaa,\n"
4506             "    aaaa);\n"
4507             "aaa(aaaa,\n"
4508             "    aaaa,\n"
4509             "    aaaa);",
4510             format("aaa(aaaa,\n" // one-per-line
4511                    "  aaaa,\n"
4512                    "    aaaa  );\n"
4513                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4514                    Style));
4515   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4516             "    aaaa);\n"
4517             "aaa(aaaa, aaaa,\n"
4518             "    aaaa);",
4519             format("aaa(aaaa,  aaaa,\n" // bin-packed
4520                    "    aaaa  );\n"
4521                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4522                    Style));
4523 }
4524 
4525 TEST_F(FormatTest, FormatsBuilderPattern) {
4526   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4527                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4528                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4529                "    .StartsWith(\".init\", ORDER_INIT)\n"
4530                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4531                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4532                "    .Default(ORDER_TEXT);\n");
4533 
4534   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4535                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4536   verifyFormat(
4537       "aaaaaaa->aaaaaaa\n"
4538       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4539       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4540       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4541   verifyFormat(
4542       "aaaaaaa->aaaaaaa\n"
4543       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4544       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4545   verifyFormat(
4546       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4547       "    aaaaaaaaaaaaaa);");
4548   verifyFormat(
4549       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4550       "    aaaaaa->aaaaaaaaaaaa()\n"
4551       "        ->aaaaaaaaaaaaaaaa(\n"
4552       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4553       "        ->aaaaaaaaaaaaaaaaa();");
4554   verifyGoogleFormat(
4555       "void f() {\n"
4556       "  someo->Add((new util::filetools::Handler(dir))\n"
4557       "                 ->OnEvent1(NewPermanentCallback(\n"
4558       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4559       "                 ->OnEvent2(NewPermanentCallback(\n"
4560       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4561       "                 ->OnEvent3(NewPermanentCallback(\n"
4562       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4563       "                 ->OnEvent5(NewPermanentCallback(\n"
4564       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4565       "                 ->OnEvent6(NewPermanentCallback(\n"
4566       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4567       "}");
4568 
4569   verifyFormat(
4570       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4571   verifyFormat("aaaaaaaaaaaaaaa()\n"
4572                "    .aaaaaaaaaaaaaaa()\n"
4573                "    .aaaaaaaaaaaaaaa()\n"
4574                "    .aaaaaaaaaaaaaaa()\n"
4575                "    .aaaaaaaaaaaaaaa();");
4576   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4577                "    .aaaaaaaaaaaaaaa()\n"
4578                "    .aaaaaaaaaaaaaaa()\n"
4579                "    .aaaaaaaaaaaaaaa();");
4580   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4581                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4582                "    .aaaaaaaaaaaaaaa();");
4583   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4584                "    ->aaaaaaaaaaaaaae(0)\n"
4585                "    ->aaaaaaaaaaaaaaa();");
4586 
4587   // Don't linewrap after very short segments.
4588   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4589                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4590                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4591   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4592                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4593                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4594   verifyFormat("aaa()\n"
4595                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4596                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4597                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4598 
4599   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4600                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4601                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4602   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4603                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4604                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4605 
4606   // Prefer not to break after empty parentheses.
4607   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4608                "    First->LastNewlineOffset);");
4609 
4610   // Prefer not to create "hanging" indents.
4611   verifyFormat(
4612       "return !soooooooooooooome_map\n"
4613       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4614       "            .second;");
4615   verifyFormat(
4616       "return aaaaaaaaaaaaaaaa\n"
4617       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4618       "    .aaaa(aaaaaaaaaaaaaa);");
4619   // No hanging indent here.
4620   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4621                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4622   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4623                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4624   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4625                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4626                getLLVMStyleWithColumns(60));
4627   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4628                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4629                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4630                getLLVMStyleWithColumns(59));
4631   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4632                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4633                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4634 
4635   // Dont break if only closing statements before member call
4636   verifyFormat("test() {\n"
4637                "  ([]() -> {\n"
4638                "    int b = 32;\n"
4639                "    return 3;\n"
4640                "  }).foo();\n"
4641                "}");
4642   verifyFormat("test() {\n"
4643                "  (\n"
4644                "      []() -> {\n"
4645                "        int b = 32;\n"
4646                "        return 3;\n"
4647                "      },\n"
4648                "      foo, bar)\n"
4649                "      .foo();\n"
4650                "}");
4651   verifyFormat("test() {\n"
4652                "  ([]() -> {\n"
4653                "    int b = 32;\n"
4654                "    return 3;\n"
4655                "  })\n"
4656                "      .foo()\n"
4657                "      .bar();\n"
4658                "}");
4659   verifyFormat("test() {\n"
4660                "  ([]() -> {\n"
4661                "    int b = 32;\n"
4662                "    return 3;\n"
4663                "  })\n"
4664                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
4665                "           \"bbbb\");\n"
4666                "}",
4667                getLLVMStyleWithColumns(30));
4668 }
4669 
4670 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4671   verifyFormat(
4672       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4673       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4674   verifyFormat(
4675       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4676       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4677 
4678   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4679                "    ccccccccccccccccccccccccc) {\n}");
4680   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4681                "    ccccccccccccccccccccccccc) {\n}");
4682 
4683   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4684                "    ccccccccccccccccccccccccc) {\n}");
4685   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4686                "    ccccccccccccccccccccccccc) {\n}");
4687 
4688   verifyFormat(
4689       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4690       "    ccccccccccccccccccccccccc) {\n}");
4691   verifyFormat(
4692       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4693       "    ccccccccccccccccccccccccc) {\n}");
4694 
4695   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4696                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4697                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4698                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4699   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4700                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4701                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4702                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4703 
4704   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4705                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4706                "    aaaaaaaaaaaaaaa != aa) {\n}");
4707   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4708                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4709                "    aaaaaaaaaaaaaaa != aa) {\n}");
4710 }
4711 
4712 TEST_F(FormatTest, BreaksAfterAssignments) {
4713   verifyFormat(
4714       "unsigned Cost =\n"
4715       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4716       "                        SI->getPointerAddressSpaceee());\n");
4717   verifyFormat(
4718       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4719       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4720 
4721   verifyFormat(
4722       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4723       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4724   verifyFormat("unsigned OriginalStartColumn =\n"
4725                "    SourceMgr.getSpellingColumnNumber(\n"
4726                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4727                "    1;");
4728 }
4729 
4730 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4731   FormatStyle Style = getLLVMStyle();
4732   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4733                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4734                Style);
4735 
4736   Style.PenaltyBreakAssignment = 20;
4737   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4738                "                                 cccccccccccccccccccccccccc;",
4739                Style);
4740 }
4741 
4742 TEST_F(FormatTest, AlignsAfterAssignments) {
4743   verifyFormat(
4744       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4745       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4746   verifyFormat(
4747       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4748       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4749   verifyFormat(
4750       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4751       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4752   verifyFormat(
4753       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4754       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4755   verifyFormat(
4756       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4757       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4758       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4759 }
4760 
4761 TEST_F(FormatTest, AlignsAfterReturn) {
4762   verifyFormat(
4763       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4764       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4765   verifyFormat(
4766       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4767       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4768   verifyFormat(
4769       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4770       "       aaaaaaaaaaaaaaaaaaaaaa();");
4771   verifyFormat(
4772       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4773       "        aaaaaaaaaaaaaaaaaaaaaa());");
4774   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4775                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4776   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4777                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4778                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4779   verifyFormat("return\n"
4780                "    // true if code is one of a or b.\n"
4781                "    code == a || code == b;");
4782 }
4783 
4784 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4785   verifyFormat(
4786       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4787       "                                                aaaaaaaaa aaaaaaa) {}");
4788   verifyFormat(
4789       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4790       "                                               aaaaaaaaaaa aaaaaaaaa);");
4791   verifyFormat(
4792       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4793       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4794   FormatStyle Style = getLLVMStyle();
4795   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4796   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4797                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4798                Style);
4799   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4800                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4801                Style);
4802   verifyFormat("SomeLongVariableName->someFunction(\n"
4803                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4804                Style);
4805   verifyFormat(
4806       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4807       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4808       Style);
4809   verifyFormat(
4810       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4811       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4812       Style);
4813   verifyFormat(
4814       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4815       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4816       Style);
4817 
4818   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4819                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4820                "        b));",
4821                Style);
4822 
4823   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4824   Style.BinPackArguments = false;
4825   Style.BinPackParameters = false;
4826   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4827                "    aaaaaaaaaaa aaaaaaaa,\n"
4828                "    aaaaaaaaa aaaaaaa,\n"
4829                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4830                Style);
4831   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4832                "    aaaaaaaaaaa aaaaaaaaa,\n"
4833                "    aaaaaaaaaaa aaaaaaaaa,\n"
4834                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4835                Style);
4836   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4837                "    aaaaaaaaaaaaaaa,\n"
4838                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4839                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4840                Style);
4841   verifyFormat(
4842       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4843       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4844       Style);
4845   verifyFormat(
4846       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4847       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4848       Style);
4849   verifyFormat(
4850       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4851       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4852       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4853       "    aaaaaaaaaaaaaaaa);",
4854       Style);
4855   verifyFormat(
4856       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4857       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4858       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4859       "    aaaaaaaaaaaaaaaa);",
4860       Style);
4861 }
4862 
4863 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4864   FormatStyle Style = getLLVMStyleWithColumns(40);
4865   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4866                "          bbbbbbbbbbbbbbbbbbbbbb);",
4867                Style);
4868   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4869   Style.AlignOperands = false;
4870   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4871                "          bbbbbbbbbbbbbbbbbbbbbb);",
4872                Style);
4873   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4874   Style.AlignOperands = true;
4875   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4876                "          bbbbbbbbbbbbbbbbbbbbbb);",
4877                Style);
4878   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4879   Style.AlignOperands = false;
4880   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4881                "    bbbbbbbbbbbbbbbbbbbbbb);",
4882                Style);
4883 }
4884 
4885 TEST_F(FormatTest, BreaksConditionalExpressions) {
4886   verifyFormat(
4887       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4888       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4889       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4890   verifyFormat(
4891       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4892       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4893       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4894   verifyFormat(
4895       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4896       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4897   verifyFormat(
4898       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4899       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4900       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4901   verifyFormat(
4902       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4903       "                                                    : aaaaaaaaaaaaa);");
4904   verifyFormat(
4905       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4906       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4907       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4908       "                   aaaaaaaaaaaaa);");
4909   verifyFormat(
4910       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4911       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4912       "                   aaaaaaaaaaaaa);");
4913   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4914                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4915                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4916                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4917                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4918   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4919                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4920                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4921                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4922                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4923                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4924                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4925   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4926                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4927                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4928                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4929                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4930   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4931                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4932                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4933   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4934                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4935                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4936                "        : aaaaaaaaaaaaaaaa;");
4937   verifyFormat(
4938       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4939       "    ? aaaaaaaaaaaaaaa\n"
4940       "    : aaaaaaaaaaaaaaa;");
4941   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4942                "          aaaaaaaaa\n"
4943                "      ? b\n"
4944                "      : c);");
4945   verifyFormat("return aaaa == bbbb\n"
4946                "           // comment\n"
4947                "           ? aaaa\n"
4948                "           : bbbb;");
4949   verifyFormat("unsigned Indent =\n"
4950                "    format(TheLine.First,\n"
4951                "           IndentForLevel[TheLine.Level] >= 0\n"
4952                "               ? IndentForLevel[TheLine.Level]\n"
4953                "               : TheLine * 2,\n"
4954                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4955                getLLVMStyleWithColumns(60));
4956   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4957                "                  ? aaaaaaaaaaaaaaa\n"
4958                "                  : bbbbbbbbbbbbbbb //\n"
4959                "                        ? ccccccccccccccc\n"
4960                "                        : ddddddddddddddd;");
4961   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4962                "                  ? aaaaaaaaaaaaaaa\n"
4963                "                  : (bbbbbbbbbbbbbbb //\n"
4964                "                         ? ccccccccccccccc\n"
4965                "                         : ddddddddddddddd);");
4966   verifyFormat(
4967       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4968       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4969       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4970       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4971       "                                      : aaaaaaaaaa;");
4972   verifyFormat(
4973       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4974       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4975       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4976 
4977   FormatStyle NoBinPacking = getLLVMStyle();
4978   NoBinPacking.BinPackArguments = false;
4979   verifyFormat(
4980       "void f() {\n"
4981       "  g(aaa,\n"
4982       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4983       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4984       "        ? aaaaaaaaaaaaaaa\n"
4985       "        : aaaaaaaaaaaaaaa);\n"
4986       "}",
4987       NoBinPacking);
4988   verifyFormat(
4989       "void f() {\n"
4990       "  g(aaa,\n"
4991       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4992       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4993       "        ?: aaaaaaaaaaaaaaa);\n"
4994       "}",
4995       NoBinPacking);
4996 
4997   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4998                "             // comment.\n"
4999                "             ccccccccccccccccccccccccccccccccccccccc\n"
5000                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5001                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
5002 
5003   // Assignments in conditional expressions. Apparently not uncommon :-(.
5004   verifyFormat("return a != b\n"
5005                "           // comment\n"
5006                "           ? a = b\n"
5007                "           : a = b;");
5008   verifyFormat("return a != b\n"
5009                "           // comment\n"
5010                "           ? a = a != b\n"
5011                "                     // comment\n"
5012                "                     ? a = b\n"
5013                "                     : a\n"
5014                "           : a;\n");
5015   verifyFormat("return a != b\n"
5016                "           // comment\n"
5017                "           ? a\n"
5018                "           : a = a != b\n"
5019                "                     // comment\n"
5020                "                     ? a = b\n"
5021                "                     : a;");
5022 }
5023 
5024 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
5025   FormatStyle Style = getLLVMStyle();
5026   Style.BreakBeforeTernaryOperators = false;
5027   Style.ColumnLimit = 70;
5028   verifyFormat(
5029       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5030       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5031       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5032       Style);
5033   verifyFormat(
5034       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
5035       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5036       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5037       Style);
5038   verifyFormat(
5039       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5040       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5041       Style);
5042   verifyFormat(
5043       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
5044       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5045       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5046       Style);
5047   verifyFormat(
5048       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
5049       "                                                      aaaaaaaaaaaaa);",
5050       Style);
5051   verifyFormat(
5052       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5054       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5055       "                   aaaaaaaaaaaaa);",
5056       Style);
5057   verifyFormat(
5058       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5059       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5060       "                   aaaaaaaaaaaaa);",
5061       Style);
5062   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5063                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5064                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5065                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5066                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5067                Style);
5068   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5069                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5070                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5071                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5072                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5073                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5074                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5075                Style);
5076   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5077                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
5078                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5079                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5080                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5081                Style);
5082   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5083                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5084                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5085                Style);
5086   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
5087                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5088                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
5089                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5090                Style);
5091   verifyFormat(
5092       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5093       "    aaaaaaaaaaaaaaa :\n"
5094       "    aaaaaaaaaaaaaaa;",
5095       Style);
5096   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
5097                "          aaaaaaaaa ?\n"
5098                "      b :\n"
5099                "      c);",
5100                Style);
5101   verifyFormat("unsigned Indent =\n"
5102                "    format(TheLine.First,\n"
5103                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
5104                "               IndentForLevel[TheLine.Level] :\n"
5105                "               TheLine * 2,\n"
5106                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
5107                Style);
5108   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5109                "                  aaaaaaaaaaaaaaa :\n"
5110                "                  bbbbbbbbbbbbbbb ? //\n"
5111                "                      ccccccccccccccc :\n"
5112                "                      ddddddddddddddd;",
5113                Style);
5114   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
5115                "                  aaaaaaaaaaaaaaa :\n"
5116                "                  (bbbbbbbbbbbbbbb ? //\n"
5117                "                       ccccccccccccccc :\n"
5118                "                       ddddddddddddddd);",
5119                Style);
5120   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5121                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
5122                "            ccccccccccccccccccccccccccc;",
5123                Style);
5124   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
5125                "           aaaaa :\n"
5126                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
5127                Style);
5128 }
5129 
5130 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
5131   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
5132                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
5133   verifyFormat("bool a = true, b = false;");
5134 
5135   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5136                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
5137                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
5138                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
5139   verifyFormat(
5140       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
5141       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
5142       "     d = e && f;");
5143   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
5144                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
5145   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5146                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
5147   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
5148                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
5149 
5150   FormatStyle Style = getGoogleStyle();
5151   Style.PointerAlignment = FormatStyle::PAS_Left;
5152   Style.DerivePointerAlignment = false;
5153   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5154                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
5155                "    *b = bbbbbbbbbbbbbbbbbbb;",
5156                Style);
5157   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
5158                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
5159                Style);
5160   verifyFormat("vector<int*> a, b;", Style);
5161   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
5162 }
5163 
5164 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
5165   verifyFormat("arr[foo ? bar : baz];");
5166   verifyFormat("f()[foo ? bar : baz];");
5167   verifyFormat("(a + b)[foo ? bar : baz];");
5168   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
5169 }
5170 
5171 TEST_F(FormatTest, AlignsStringLiterals) {
5172   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
5173                "                                      \"short literal\");");
5174   verifyFormat(
5175       "looooooooooooooooooooooooongFunction(\n"
5176       "    \"short literal\"\n"
5177       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
5178   verifyFormat("someFunction(\"Always break between multi-line\"\n"
5179                "             \" string literals\",\n"
5180                "             and, other, parameters);");
5181   EXPECT_EQ("fun + \"1243\" /* comment */\n"
5182             "      \"5678\";",
5183             format("fun + \"1243\" /* comment */\n"
5184                    "    \"5678\";",
5185                    getLLVMStyleWithColumns(28)));
5186   EXPECT_EQ(
5187       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
5188       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
5189       "         \"aaaaaaaaaaaaaaaa\";",
5190       format("aaaaaa ="
5191              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
5192              "aaaaaaaaaaaaaaaaaaaaa\" "
5193              "\"aaaaaaaaaaaaaaaa\";"));
5194   verifyFormat("a = a + \"a\"\n"
5195                "        \"a\"\n"
5196                "        \"a\";");
5197   verifyFormat("f(\"a\", \"b\"\n"
5198                "       \"c\");");
5199 
5200   verifyFormat(
5201       "#define LL_FORMAT \"ll\"\n"
5202       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
5203       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
5204 
5205   verifyFormat("#define A(X)          \\\n"
5206                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
5207                "  \"ccccc\"",
5208                getLLVMStyleWithColumns(23));
5209   verifyFormat("#define A \"def\"\n"
5210                "f(\"abc\" A \"ghi\"\n"
5211                "  \"jkl\");");
5212 
5213   verifyFormat("f(L\"a\"\n"
5214                "  L\"b\");");
5215   verifyFormat("#define A(X)            \\\n"
5216                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
5217                "  L\"ccccc\"",
5218                getLLVMStyleWithColumns(25));
5219 
5220   verifyFormat("f(@\"a\"\n"
5221                "  @\"b\");");
5222   verifyFormat("NSString s = @\"a\"\n"
5223                "             @\"b\"\n"
5224                "             @\"c\";");
5225   verifyFormat("NSString s = @\"a\"\n"
5226                "              \"b\"\n"
5227                "              \"c\";");
5228 }
5229 
5230 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
5231   FormatStyle Style = getLLVMStyle();
5232   // No declarations or definitions should be moved to own line.
5233   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
5234   verifyFormat("class A {\n"
5235                "  int f() { return 1; }\n"
5236                "  int g();\n"
5237                "};\n"
5238                "int f() { return 1; }\n"
5239                "int g();\n",
5240                Style);
5241 
5242   // All declarations and definitions should have the return type moved to its
5243   // own
5244   // line.
5245   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
5246   verifyFormat("class E {\n"
5247                "  int\n"
5248                "  f() {\n"
5249                "    return 1;\n"
5250                "  }\n"
5251                "  int\n"
5252                "  g();\n"
5253                "};\n"
5254                "int\n"
5255                "f() {\n"
5256                "  return 1;\n"
5257                "}\n"
5258                "int\n"
5259                "g();\n",
5260                Style);
5261 
5262   // Top-level definitions, and no kinds of declarations should have the
5263   // return type moved to its own line.
5264   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
5265   verifyFormat("class B {\n"
5266                "  int f() { return 1; }\n"
5267                "  int g();\n"
5268                "};\n"
5269                "int\n"
5270                "f() {\n"
5271                "  return 1;\n"
5272                "}\n"
5273                "int g();\n",
5274                Style);
5275 
5276   // Top-level definitions and declarations should have the return type moved
5277   // to its own line.
5278   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
5279   verifyFormat("class C {\n"
5280                "  int f() { return 1; }\n"
5281                "  int g();\n"
5282                "};\n"
5283                "int\n"
5284                "f() {\n"
5285                "  return 1;\n"
5286                "}\n"
5287                "int\n"
5288                "g();\n",
5289                Style);
5290 
5291   // All definitions should have the return type moved to its own line, but no
5292   // kinds of declarations.
5293   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5294   verifyFormat("class D {\n"
5295                "  int\n"
5296                "  f() {\n"
5297                "    return 1;\n"
5298                "  }\n"
5299                "  int g();\n"
5300                "};\n"
5301                "int\n"
5302                "f() {\n"
5303                "  return 1;\n"
5304                "}\n"
5305                "int g();\n",
5306                Style);
5307   verifyFormat("const char *\n"
5308                "f(void) {\n" // Break here.
5309                "  return \"\";\n"
5310                "}\n"
5311                "const char *bar(void);\n", // No break here.
5312                Style);
5313   verifyFormat("template <class T>\n"
5314                "T *\n"
5315                "f(T &c) {\n" // Break here.
5316                "  return NULL;\n"
5317                "}\n"
5318                "template <class T> T *f(T &c);\n", // No break here.
5319                Style);
5320   verifyFormat("class C {\n"
5321                "  int\n"
5322                "  operator+() {\n"
5323                "    return 1;\n"
5324                "  }\n"
5325                "  int\n"
5326                "  operator()() {\n"
5327                "    return 1;\n"
5328                "  }\n"
5329                "};\n",
5330                Style);
5331   verifyFormat("void\n"
5332                "A::operator()() {}\n"
5333                "void\n"
5334                "A::operator>>() {}\n"
5335                "void\n"
5336                "A::operator+() {}\n",
5337                Style);
5338   verifyFormat("void *operator new(std::size_t s);", // No break here.
5339                Style);
5340   verifyFormat("void *\n"
5341                "operator new(std::size_t s) {}",
5342                Style);
5343   verifyFormat("void *\n"
5344                "operator delete[](void *ptr) {}",
5345                Style);
5346   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
5347   verifyFormat("const char *\n"
5348                "f(void)\n" // Break here.
5349                "{\n"
5350                "  return \"\";\n"
5351                "}\n"
5352                "const char *bar(void);\n", // No break here.
5353                Style);
5354   verifyFormat("template <class T>\n"
5355                "T *\n"     // Problem here: no line break
5356                "f(T &c)\n" // Break here.
5357                "{\n"
5358                "  return NULL;\n"
5359                "}\n"
5360                "template <class T> T *f(T &c);\n", // No break here.
5361                Style);
5362 }
5363 
5364 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5365   FormatStyle NoBreak = getLLVMStyle();
5366   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5367   FormatStyle Break = getLLVMStyle();
5368   Break.AlwaysBreakBeforeMultilineStrings = true;
5369   verifyFormat("aaaa = \"bbbb\"\n"
5370                "       \"cccc\";",
5371                NoBreak);
5372   verifyFormat("aaaa =\n"
5373                "    \"bbbb\"\n"
5374                "    \"cccc\";",
5375                Break);
5376   verifyFormat("aaaa(\"bbbb\"\n"
5377                "     \"cccc\");",
5378                NoBreak);
5379   verifyFormat("aaaa(\n"
5380                "    \"bbbb\"\n"
5381                "    \"cccc\");",
5382                Break);
5383   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5384                "          \"cccc\");",
5385                NoBreak);
5386   verifyFormat("aaaa(qqq,\n"
5387                "     \"bbbb\"\n"
5388                "     \"cccc\");",
5389                Break);
5390   verifyFormat("aaaa(qqq,\n"
5391                "     L\"bbbb\"\n"
5392                "     L\"cccc\");",
5393                Break);
5394   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5395                "                      \"bbbb\"));",
5396                Break);
5397   verifyFormat("string s = someFunction(\n"
5398                "    \"abc\"\n"
5399                "    \"abc\");",
5400                Break);
5401 
5402   // As we break before unary operators, breaking right after them is bad.
5403   verifyFormat("string foo = abc ? \"x\"\n"
5404                "                   \"blah blah blah blah blah blah\"\n"
5405                "                 : \"y\";",
5406                Break);
5407 
5408   // Don't break if there is no column gain.
5409   verifyFormat("f(\"aaaa\"\n"
5410                "  \"bbbb\");",
5411                Break);
5412 
5413   // Treat literals with escaped newlines like multi-line string literals.
5414   EXPECT_EQ("x = \"a\\\n"
5415             "b\\\n"
5416             "c\";",
5417             format("x = \"a\\\n"
5418                    "b\\\n"
5419                    "c\";",
5420                    NoBreak));
5421   EXPECT_EQ("xxxx =\n"
5422             "    \"a\\\n"
5423             "b\\\n"
5424             "c\";",
5425             format("xxxx = \"a\\\n"
5426                    "b\\\n"
5427                    "c\";",
5428                    Break));
5429 
5430   EXPECT_EQ("NSString *const kString =\n"
5431             "    @\"aaaa\"\n"
5432             "    @\"bbbb\";",
5433             format("NSString *const kString = @\"aaaa\"\n"
5434                    "@\"bbbb\";",
5435                    Break));
5436 
5437   Break.ColumnLimit = 0;
5438   verifyFormat("const char *hello = \"hello llvm\";", Break);
5439 }
5440 
5441 TEST_F(FormatTest, AlignsPipes) {
5442   verifyFormat(
5443       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5444       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5445       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5446   verifyFormat(
5447       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5448       "                     << aaaaaaaaaaaaaaaaaaaa;");
5449   verifyFormat(
5450       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5451       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5452   verifyFormat(
5453       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5454       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5455   verifyFormat(
5456       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5457       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5458       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5459   verifyFormat(
5460       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5461       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5462       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5463   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5464                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5465                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5466                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5467   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5468                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5469   verifyFormat(
5470       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5471       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5472   verifyFormat(
5473       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5474       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5475 
5476   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5477                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5478   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5479                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5480                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5481                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5482   verifyFormat("LOG_IF(aaa == //\n"
5483                "       bbb)\n"
5484                "    << a << b;");
5485 
5486   // But sometimes, breaking before the first "<<" is desirable.
5487   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5488                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5489   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5490                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5491                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5492   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5493                "    << BEF << IsTemplate << Description << E->getType();");
5494   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5495                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5496                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5497   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5498                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5499                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5500                "    << aaa;");
5501 
5502   verifyFormat(
5503       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5504       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5505 
5506   // Incomplete string literal.
5507   EXPECT_EQ("llvm::errs() << \"\n"
5508             "             << a;",
5509             format("llvm::errs() << \"\n<<a;"));
5510 
5511   verifyFormat("void f() {\n"
5512                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5513                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5514                "}");
5515 
5516   // Handle 'endl'.
5517   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5518                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5519   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5520 
5521   // Handle '\n'.
5522   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5523                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5524   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5525                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5526   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5527                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5528   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5529 }
5530 
5531 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5532   verifyFormat("return out << \"somepacket = {\\n\"\n"
5533                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5534                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5535                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5536                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5537                "           << \"}\";");
5538 
5539   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5540                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5541                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5542   verifyFormat(
5543       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5544       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5545       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5546       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5547       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5548   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5549                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5550   verifyFormat(
5551       "void f() {\n"
5552       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5553       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5554       "}");
5555 
5556   // Breaking before the first "<<" is generally not desirable.
5557   verifyFormat(
5558       "llvm::errs()\n"
5559       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5560       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5561       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5562       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5563       getLLVMStyleWithColumns(70));
5564   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5565                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5566                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5567                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5568                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5569                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5570                getLLVMStyleWithColumns(70));
5571 
5572   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5573                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5574                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5575   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5576                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5577                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5578   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5579                "           (aaaa + aaaa);",
5580                getLLVMStyleWithColumns(40));
5581   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5582                "                  (aaaaaaa + aaaaa));",
5583                getLLVMStyleWithColumns(40));
5584   verifyFormat(
5585       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5586       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5587       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5588 }
5589 
5590 TEST_F(FormatTest, UnderstandsEquals) {
5591   verifyFormat(
5592       "aaaaaaaaaaaaaaaaa =\n"
5593       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5594   verifyFormat(
5595       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5596       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5597   verifyFormat(
5598       "if (a) {\n"
5599       "  f();\n"
5600       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5601       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5602       "}");
5603 
5604   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5605                "        100000000 + 10000000) {\n}");
5606 }
5607 
5608 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5609   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5610                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5611 
5612   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5613                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5614 
5615   verifyFormat(
5616       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5617       "                                                          Parameter2);");
5618 
5619   verifyFormat(
5620       "ShortObject->shortFunction(\n"
5621       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5622       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5623 
5624   verifyFormat("loooooooooooooongFunction(\n"
5625                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5626 
5627   verifyFormat(
5628       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5629       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5630 
5631   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5632                "    .WillRepeatedly(Return(SomeValue));");
5633   verifyFormat("void f() {\n"
5634                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5635                "      .Times(2)\n"
5636                "      .WillRepeatedly(Return(SomeValue));\n"
5637                "}");
5638   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5639                "    ccccccccccccccccccccccc);");
5640   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5641                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5642                "          .aaaaa(aaaaa),\n"
5643                "      aaaaaaaaaaaaaaaaaaaaa);");
5644   verifyFormat("void f() {\n"
5645                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5646                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5647                "}");
5648   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5649                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5650                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5651                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5652                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5653   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5654                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5655                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5656                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5657                "}");
5658 
5659   // Here, it is not necessary to wrap at "." or "->".
5660   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5661                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5662   verifyFormat(
5663       "aaaaaaaaaaa->aaaaaaaaa(\n"
5664       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5665       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5666 
5667   verifyFormat(
5668       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5669       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5670   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5671                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5672   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5673                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5674 
5675   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5676                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5677                "    .a();");
5678 
5679   FormatStyle NoBinPacking = getLLVMStyle();
5680   NoBinPacking.BinPackParameters = false;
5681   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5682                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5683                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5684                "                         aaaaaaaaaaaaaaaaaaa,\n"
5685                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5686                NoBinPacking);
5687 
5688   // If there is a subsequent call, change to hanging indentation.
5689   verifyFormat(
5690       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5691       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5692       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5693   verifyFormat(
5694       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5695       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5696   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5697                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5698                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5699   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5700                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5701                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5702 }
5703 
5704 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5705   verifyFormat("template <typename T>\n"
5706                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5707   verifyFormat("template <typename T>\n"
5708                "// T should be one of {A, B}.\n"
5709                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5710   verifyFormat(
5711       "template <typename T>\n"
5712       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5713   verifyFormat("template <typename T>\n"
5714                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5715                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5716   verifyFormat(
5717       "template <typename T>\n"
5718       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5719       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5720   verifyFormat(
5721       "template <typename T>\n"
5722       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5723       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5724       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5725   verifyFormat("template <typename T>\n"
5726                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5727                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5728   verifyFormat(
5729       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5730       "          typename T4 = char>\n"
5731       "void f();");
5732   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5733                "          template <typename> class cccccccccccccccccccccc,\n"
5734                "          typename ddddddddddddd>\n"
5735                "class C {};");
5736   verifyFormat(
5737       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5738       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5739 
5740   verifyFormat("void f() {\n"
5741                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5742                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5743                "}");
5744 
5745   verifyFormat("template <typename T> class C {};");
5746   verifyFormat("template <typename T> void f();");
5747   verifyFormat("template <typename T> void f() {}");
5748   verifyFormat(
5749       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5750       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5751       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5752       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5753       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5754       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5755       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5756       getLLVMStyleWithColumns(72));
5757   EXPECT_EQ("static_cast<A< //\n"
5758             "    B> *>(\n"
5759             "\n"
5760             ");",
5761             format("static_cast<A<//\n"
5762                    "    B>*>(\n"
5763                    "\n"
5764                    "    );"));
5765   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5766                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5767 
5768   FormatStyle AlwaysBreak = getLLVMStyle();
5769   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
5770   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5771   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5772   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5773   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5774                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5775                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5776   verifyFormat("template <template <typename> class Fooooooo,\n"
5777                "          template <typename> class Baaaaaaar>\n"
5778                "struct C {};",
5779                AlwaysBreak);
5780   verifyFormat("template <typename T> // T can be A, B or C.\n"
5781                "struct C {};",
5782                AlwaysBreak);
5783   verifyFormat("template <enum E> class A {\n"
5784                "public:\n"
5785                "  E *f();\n"
5786                "};");
5787 
5788   FormatStyle NeverBreak = getLLVMStyle();
5789   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
5790   verifyFormat("template <typename T> class C {};", NeverBreak);
5791   verifyFormat("template <typename T> void f();", NeverBreak);
5792   verifyFormat("template <typename T> void f() {}", NeverBreak);
5793   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5794                NeverBreak);
5795   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5796                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5797                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
5798                NeverBreak);
5799   verifyFormat("template <template <typename> class Fooooooo,\n"
5800                "          template <typename> class Baaaaaaar>\n"
5801                "struct C {};",
5802                NeverBreak);
5803   verifyFormat("template <typename T> // T can be A, B or C.\n"
5804                "struct C {};",
5805                NeverBreak);
5806   verifyFormat("template <enum E> class A {\n"
5807                "public:\n"
5808                "  E *f();\n"
5809                "};", NeverBreak);
5810   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
5811   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}",
5812                NeverBreak);
5813 }
5814 
5815 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
5816   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
5817   Style.ColumnLimit = 60;
5818   EXPECT_EQ("// Baseline - no comments.\n"
5819             "template <\n"
5820             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5821             "void f() {}",
5822             format("// Baseline - no comments.\n"
5823                    "template <\n"
5824                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
5825                    "void f() {}",
5826                    Style));
5827 
5828   EXPECT_EQ("template <\n"
5829             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5830             "void f() {}",
5831             format("template <\n"
5832                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5833                    "void f() {}",
5834                    Style));
5835 
5836   EXPECT_EQ(
5837       "template <\n"
5838       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
5839       "void f() {}",
5840       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
5841              "void f() {}",
5842              Style));
5843 
5844   EXPECT_EQ(
5845       "template <\n"
5846       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
5847       "                                               // multiline\n"
5848       "void f() {}",
5849       format("template <\n"
5850              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
5851              "                                              // multiline\n"
5852              "void f() {}",
5853              Style));
5854 
5855   EXPECT_EQ(
5856       "template <typename aaaaaaaaaa<\n"
5857       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
5858       "void f() {}",
5859       format(
5860           "template <\n"
5861           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
5862           "void f() {}",
5863           Style));
5864 }
5865 
5866 TEST_F(FormatTest, WrapsTemplateParameters) {
5867   FormatStyle Style = getLLVMStyle();
5868   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5869   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5870   verifyFormat(
5871       "template <typename... a> struct q {};\n"
5872       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5873       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5874       "    y;",
5875       Style);
5876   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5877   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5878   verifyFormat(
5879       "template <typename... a> struct r {};\n"
5880       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5881       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5882       "    y;",
5883       Style);
5884   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5885   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5886   verifyFormat(
5887       "template <typename... a> struct s {};\n"
5888       "extern s<\n"
5889       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5890       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5891       "    y;",
5892       Style);
5893   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5894   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5895   verifyFormat(
5896       "template <typename... a> struct t {};\n"
5897       "extern t<\n"
5898       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5899       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5900       "    y;",
5901       Style);
5902 }
5903 
5904 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5905   verifyFormat(
5906       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5907       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5908   verifyFormat(
5909       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5910       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5911       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5912 
5913   // FIXME: Should we have the extra indent after the second break?
5914   verifyFormat(
5915       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5916       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5917       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5918 
5919   verifyFormat(
5920       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5921       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5922 
5923   // Breaking at nested name specifiers is generally not desirable.
5924   verifyFormat(
5925       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5926       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5927 
5928   verifyFormat(
5929       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5930       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5931       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5932       "                   aaaaaaaaaaaaaaaaaaaaa);",
5933       getLLVMStyleWithColumns(74));
5934 
5935   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5936                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5937                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5938 }
5939 
5940 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5941   verifyFormat("A<int> a;");
5942   verifyFormat("A<A<A<int>>> a;");
5943   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5944   verifyFormat("bool x = a < 1 || 2 > a;");
5945   verifyFormat("bool x = 5 < f<int>();");
5946   verifyFormat("bool x = f<int>() > 5;");
5947   verifyFormat("bool x = 5 < a<int>::x;");
5948   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5949   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5950 
5951   verifyGoogleFormat("A<A<int>> a;");
5952   verifyGoogleFormat("A<A<A<int>>> a;");
5953   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5954   verifyGoogleFormat("A<A<int> > a;");
5955   verifyGoogleFormat("A<A<A<int> > > a;");
5956   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5957   verifyGoogleFormat("A<::A<int>> a;");
5958   verifyGoogleFormat("A<::A> a;");
5959   verifyGoogleFormat("A< ::A> a;");
5960   verifyGoogleFormat("A< ::A<int> > a;");
5961   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5962   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5963   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5964   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5965   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5966             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5967 
5968   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5969 
5970   verifyFormat("test >> a >> b;");
5971   verifyFormat("test << a >> b;");
5972 
5973   verifyFormat("f<int>();");
5974   verifyFormat("template <typename T> void f() {}");
5975   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5976   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5977                "sizeof(char)>::type>;");
5978   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5979   verifyFormat("f(a.operator()<A>());");
5980   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5981                "      .template operator()<A>());",
5982                getLLVMStyleWithColumns(35));
5983 
5984   // Not template parameters.
5985   verifyFormat("return a < b && c > d;");
5986   verifyFormat("void f() {\n"
5987                "  while (a < b && c > d) {\n"
5988                "  }\n"
5989                "}");
5990   verifyFormat("template <typename... Types>\n"
5991                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5992 
5993   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5994                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5995                getLLVMStyleWithColumns(60));
5996   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5997   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5998   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5999 }
6000 
6001 TEST_F(FormatTest, BitshiftOperatorWidth) {
6002   EXPECT_EQ("int a = 1 << 2; /* foo\n"
6003             "                   bar */",
6004             format("int    a=1<<2;  /* foo\n"
6005                    "                   bar */"));
6006 
6007   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
6008             "                     bar */",
6009             format("int  b  =256>>1 ;  /* foo\n"
6010                    "                      bar */"));
6011 }
6012 
6013 TEST_F(FormatTest, UnderstandsBinaryOperators) {
6014   verifyFormat("COMPARE(a, ==, b);");
6015   verifyFormat("auto s = sizeof...(Ts) - 1;");
6016 }
6017 
6018 TEST_F(FormatTest, UnderstandsPointersToMembers) {
6019   verifyFormat("int A::*x;");
6020   verifyFormat("int (S::*func)(void *);");
6021   verifyFormat("void f() { int (S::*func)(void *); }");
6022   verifyFormat("typedef bool *(Class::*Member)() const;");
6023   verifyFormat("void f() {\n"
6024                "  (a->*f)();\n"
6025                "  a->*x;\n"
6026                "  (a.*f)();\n"
6027                "  ((*a).*f)();\n"
6028                "  a.*x;\n"
6029                "}");
6030   verifyFormat("void f() {\n"
6031                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6032                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
6033                "}");
6034   verifyFormat(
6035       "(aaaaaaaaaa->*bbbbbbb)(\n"
6036       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6037   FormatStyle Style = getLLVMStyle();
6038   Style.PointerAlignment = FormatStyle::PAS_Left;
6039   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
6040 }
6041 
6042 TEST_F(FormatTest, UnderstandsUnaryOperators) {
6043   verifyFormat("int a = -2;");
6044   verifyFormat("f(-1, -2, -3);");
6045   verifyFormat("a[-1] = 5;");
6046   verifyFormat("int a = 5 + -2;");
6047   verifyFormat("if (i == -1) {\n}");
6048   verifyFormat("if (i != -1) {\n}");
6049   verifyFormat("if (i > -1) {\n}");
6050   verifyFormat("if (i < -1) {\n}");
6051   verifyFormat("++(a->f());");
6052   verifyFormat("--(a->f());");
6053   verifyFormat("(a->f())++;");
6054   verifyFormat("a[42]++;");
6055   verifyFormat("if (!(a->f())) {\n}");
6056   verifyFormat("if (!+i) {\n}");
6057   verifyFormat("~&a;");
6058 
6059   verifyFormat("a-- > b;");
6060   verifyFormat("b ? -a : c;");
6061   verifyFormat("n * sizeof char16;");
6062   verifyFormat("n * alignof char16;", getGoogleStyle());
6063   verifyFormat("sizeof(char);");
6064   verifyFormat("alignof(char);", getGoogleStyle());
6065 
6066   verifyFormat("return -1;");
6067   verifyFormat("switch (a) {\n"
6068                "case -1:\n"
6069                "  break;\n"
6070                "}");
6071   verifyFormat("#define X -1");
6072   verifyFormat("#define X -kConstant");
6073 
6074   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
6075   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
6076 
6077   verifyFormat("int a = /* confusing comment */ -1;");
6078   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
6079   verifyFormat("int a = i /* confusing comment */++;");
6080 }
6081 
6082 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
6083   verifyFormat("if (!aaaaaaaaaa( // break\n"
6084                "        aaaaa)) {\n"
6085                "}");
6086   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
6087                "    aaaaa));");
6088   verifyFormat("*aaa = aaaaaaa( // break\n"
6089                "    bbbbbb);");
6090 }
6091 
6092 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
6093   verifyFormat("bool operator<();");
6094   verifyFormat("bool operator>();");
6095   verifyFormat("bool operator=();");
6096   verifyFormat("bool operator==();");
6097   verifyFormat("bool operator!=();");
6098   verifyFormat("int operator+();");
6099   verifyFormat("int operator++();");
6100   verifyFormat("int operator++(int) volatile noexcept;");
6101   verifyFormat("bool operator,();");
6102   verifyFormat("bool operator();");
6103   verifyFormat("bool operator()();");
6104   verifyFormat("bool operator[]();");
6105   verifyFormat("operator bool();");
6106   verifyFormat("operator int();");
6107   verifyFormat("operator void *();");
6108   verifyFormat("operator SomeType<int>();");
6109   verifyFormat("operator SomeType<int, int>();");
6110   verifyFormat("operator SomeType<SomeType<int>>();");
6111   verifyFormat("void *operator new(std::size_t size);");
6112   verifyFormat("void *operator new[](std::size_t size);");
6113   verifyFormat("void operator delete(void *ptr);");
6114   verifyFormat("void operator delete[](void *ptr);");
6115   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
6116                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
6117   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
6118                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
6119 
6120   verifyFormat(
6121       "ostream &operator<<(ostream &OutputStream,\n"
6122       "                    SomeReallyLongType WithSomeReallyLongValue);");
6123   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
6124                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
6125                "  return left.group < right.group;\n"
6126                "}");
6127   verifyFormat("SomeType &operator=(const SomeType &S);");
6128   verifyFormat("f.template operator()<int>();");
6129 
6130   verifyGoogleFormat("operator void*();");
6131   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
6132   verifyGoogleFormat("operator ::A();");
6133 
6134   verifyFormat("using A::operator+;");
6135   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
6136                "int i;");
6137 }
6138 
6139 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
6140   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
6141   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
6142   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
6143   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
6144   verifyFormat("Deleted &operator=(const Deleted &) &;");
6145   verifyFormat("Deleted &operator=(const Deleted &) &&;");
6146   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
6147   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
6148   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
6149   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
6150   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
6151   verifyFormat("void Fn(T const &) const &;");
6152   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
6153   verifyFormat("template <typename T>\n"
6154                "void F(T) && = delete;",
6155                getGoogleStyle());
6156 
6157   FormatStyle AlignLeft = getLLVMStyle();
6158   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
6159   verifyFormat("void A::b() && {}", AlignLeft);
6160   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
6161   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
6162                AlignLeft);
6163   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
6164   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
6165   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
6166   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
6167   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
6168   verifyFormat("auto Function(T) & -> void;", AlignLeft);
6169   verifyFormat("void Fn(T const&) const&;", AlignLeft);
6170   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
6171 
6172   FormatStyle Spaces = getLLVMStyle();
6173   Spaces.SpacesInCStyleCastParentheses = true;
6174   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
6175   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
6176   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
6177   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
6178 
6179   Spaces.SpacesInCStyleCastParentheses = false;
6180   Spaces.SpacesInParentheses = true;
6181   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
6182   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
6183   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
6184   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
6185 }
6186 
6187 TEST_F(FormatTest, UnderstandsNewAndDelete) {
6188   verifyFormat("void f() {\n"
6189                "  A *a = new A;\n"
6190                "  A *a = new (placement) A;\n"
6191                "  delete a;\n"
6192                "  delete (A *)a;\n"
6193                "}");
6194   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6195                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6196   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6197                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
6198                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
6199   verifyFormat("delete[] h->p;");
6200 }
6201 
6202 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
6203   verifyFormat("int *f(int *a) {}");
6204   verifyFormat("int main(int argc, char **argv) {}");
6205   verifyFormat("Test::Test(int b) : a(b * b) {}");
6206   verifyIndependentOfContext("f(a, *a);");
6207   verifyFormat("void g() { f(*a); }");
6208   verifyIndependentOfContext("int a = b * 10;");
6209   verifyIndependentOfContext("int a = 10 * b;");
6210   verifyIndependentOfContext("int a = b * c;");
6211   verifyIndependentOfContext("int a += b * c;");
6212   verifyIndependentOfContext("int a -= b * c;");
6213   verifyIndependentOfContext("int a *= b * c;");
6214   verifyIndependentOfContext("int a /= b * c;");
6215   verifyIndependentOfContext("int a = *b;");
6216   verifyIndependentOfContext("int a = *b * c;");
6217   verifyIndependentOfContext("int a = b * *c;");
6218   verifyIndependentOfContext("int a = b * (10);");
6219   verifyIndependentOfContext("S << b * (10);");
6220   verifyIndependentOfContext("return 10 * b;");
6221   verifyIndependentOfContext("return *b * *c;");
6222   verifyIndependentOfContext("return a & ~b;");
6223   verifyIndependentOfContext("f(b ? *c : *d);");
6224   verifyIndependentOfContext("int a = b ? *c : *d;");
6225   verifyIndependentOfContext("*b = a;");
6226   verifyIndependentOfContext("a * ~b;");
6227   verifyIndependentOfContext("a * !b;");
6228   verifyIndependentOfContext("a * +b;");
6229   verifyIndependentOfContext("a * -b;");
6230   verifyIndependentOfContext("a * ++b;");
6231   verifyIndependentOfContext("a * --b;");
6232   verifyIndependentOfContext("a[4] * b;");
6233   verifyIndependentOfContext("a[a * a] = 1;");
6234   verifyIndependentOfContext("f() * b;");
6235   verifyIndependentOfContext("a * [self dostuff];");
6236   verifyIndependentOfContext("int x = a * (a + b);");
6237   verifyIndependentOfContext("(a *)(a + b);");
6238   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
6239   verifyIndependentOfContext("int *pa = (int *)&a;");
6240   verifyIndependentOfContext("return sizeof(int **);");
6241   verifyIndependentOfContext("return sizeof(int ******);");
6242   verifyIndependentOfContext("return (int **&)a;");
6243   verifyIndependentOfContext("f((*PointerToArray)[10]);");
6244   verifyFormat("void f(Type (*parameter)[10]) {}");
6245   verifyFormat("void f(Type (&parameter)[10]) {}");
6246   verifyGoogleFormat("return sizeof(int**);");
6247   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
6248   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
6249   verifyFormat("auto a = [](int **&, int ***) {};");
6250   verifyFormat("auto PointerBinding = [](const char *S) {};");
6251   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
6252   verifyFormat("[](const decltype(*a) &value) {}");
6253   verifyFormat("decltype(a * b) F();");
6254   verifyFormat("#define MACRO() [](A *a) { return 1; }");
6255   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
6256   verifyIndependentOfContext("typedef void (*f)(int *a);");
6257   verifyIndependentOfContext("int i{a * b};");
6258   verifyIndependentOfContext("aaa && aaa->f();");
6259   verifyIndependentOfContext("int x = ~*p;");
6260   verifyFormat("Constructor() : a(a), area(width * height) {}");
6261   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
6262   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
6263   verifyFormat("void f() { f(a, c * d); }");
6264   verifyFormat("void f() { f(new a(), c * d); }");
6265   verifyFormat("void f(const MyOverride &override);");
6266   verifyFormat("void f(const MyFinal &final);");
6267   verifyIndependentOfContext("bool a = f() && override.f();");
6268   verifyIndependentOfContext("bool a = f() && final.f();");
6269 
6270   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
6271 
6272   verifyIndependentOfContext("A<int *> a;");
6273   verifyIndependentOfContext("A<int **> a;");
6274   verifyIndependentOfContext("A<int *, int *> a;");
6275   verifyIndependentOfContext("A<int *[]> a;");
6276   verifyIndependentOfContext(
6277       "const char *const p = reinterpret_cast<const char *const>(q);");
6278   verifyIndependentOfContext("A<int **, int **> a;");
6279   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
6280   verifyFormat("for (char **a = b; *a; ++a) {\n}");
6281   verifyFormat("for (; a && b;) {\n}");
6282   verifyFormat("bool foo = true && [] { return false; }();");
6283 
6284   verifyFormat(
6285       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6286       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6287 
6288   verifyGoogleFormat("int const* a = &b;");
6289   verifyGoogleFormat("**outparam = 1;");
6290   verifyGoogleFormat("*outparam = a * b;");
6291   verifyGoogleFormat("int main(int argc, char** argv) {}");
6292   verifyGoogleFormat("A<int*> a;");
6293   verifyGoogleFormat("A<int**> a;");
6294   verifyGoogleFormat("A<int*, int*> a;");
6295   verifyGoogleFormat("A<int**, int**> a;");
6296   verifyGoogleFormat("f(b ? *c : *d);");
6297   verifyGoogleFormat("int a = b ? *c : *d;");
6298   verifyGoogleFormat("Type* t = **x;");
6299   verifyGoogleFormat("Type* t = *++*x;");
6300   verifyGoogleFormat("*++*x;");
6301   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
6302   verifyGoogleFormat("Type* t = x++ * y;");
6303   verifyGoogleFormat(
6304       "const char* const p = reinterpret_cast<const char* const>(q);");
6305   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
6306   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
6307   verifyGoogleFormat("template <typename T>\n"
6308                      "void f(int i = 0, SomeType** temps = NULL);");
6309 
6310   FormatStyle Left = getLLVMStyle();
6311   Left.PointerAlignment = FormatStyle::PAS_Left;
6312   verifyFormat("x = *a(x) = *a(y);", Left);
6313   verifyFormat("for (;; *a = b) {\n}", Left);
6314   verifyFormat("return *this += 1;", Left);
6315   verifyFormat("throw *x;", Left);
6316   verifyFormat("delete *x;", Left);
6317   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
6318   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
6319   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
6320 
6321   verifyIndependentOfContext("a = *(x + y);");
6322   verifyIndependentOfContext("a = &(x + y);");
6323   verifyIndependentOfContext("*(x + y).call();");
6324   verifyIndependentOfContext("&(x + y)->call();");
6325   verifyFormat("void f() { &(*I).first; }");
6326 
6327   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
6328   verifyFormat(
6329       "int *MyValues = {\n"
6330       "    *A, // Operator detection might be confused by the '{'\n"
6331       "    *BB // Operator detection might be confused by previous comment\n"
6332       "};");
6333 
6334   verifyIndependentOfContext("if (int *a = &b)");
6335   verifyIndependentOfContext("if (int &a = *b)");
6336   verifyIndependentOfContext("if (a & b[i])");
6337   verifyIndependentOfContext("if (a::b::c::d & b[i])");
6338   verifyIndependentOfContext("if (*b[i])");
6339   verifyIndependentOfContext("if (int *a = (&b))");
6340   verifyIndependentOfContext("while (int *a = &b)");
6341   verifyIndependentOfContext("size = sizeof *a;");
6342   verifyIndependentOfContext("if (a && (b = c))");
6343   verifyFormat("void f() {\n"
6344                "  for (const int &v : Values) {\n"
6345                "  }\n"
6346                "}");
6347   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
6348   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
6349   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
6350 
6351   verifyFormat("#define A (!a * b)");
6352   verifyFormat("#define MACRO     \\\n"
6353                "  int *i = a * b; \\\n"
6354                "  void f(a *b);",
6355                getLLVMStyleWithColumns(19));
6356 
6357   verifyIndependentOfContext("A = new SomeType *[Length];");
6358   verifyIndependentOfContext("A = new SomeType *[Length]();");
6359   verifyIndependentOfContext("T **t = new T *;");
6360   verifyIndependentOfContext("T **t = new T *();");
6361   verifyGoogleFormat("A = new SomeType*[Length]();");
6362   verifyGoogleFormat("A = new SomeType*[Length];");
6363   verifyGoogleFormat("T** t = new T*;");
6364   verifyGoogleFormat("T** t = new T*();");
6365 
6366   verifyFormat("STATIC_ASSERT((a & b) == 0);");
6367   verifyFormat("STATIC_ASSERT(0 == (a & b));");
6368   verifyFormat("template <bool a, bool b> "
6369                "typename t::if<x && y>::type f() {}");
6370   verifyFormat("template <int *y> f() {}");
6371   verifyFormat("vector<int *> v;");
6372   verifyFormat("vector<int *const> v;");
6373   verifyFormat("vector<int *const **const *> v;");
6374   verifyFormat("vector<int *volatile> v;");
6375   verifyFormat("vector<a * b> v;");
6376   verifyFormat("foo<b && false>();");
6377   verifyFormat("foo<b & 1>();");
6378   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
6379   verifyFormat(
6380       "template <class T, class = typename std::enable_if<\n"
6381       "                       std::is_integral<T>::value &&\n"
6382       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
6383       "void F();",
6384       getLLVMStyleWithColumns(70));
6385   verifyFormat(
6386       "template <class T,\n"
6387       "          class = typename std::enable_if<\n"
6388       "              std::is_integral<T>::value &&\n"
6389       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
6390       "          class U>\n"
6391       "void F();",
6392       getLLVMStyleWithColumns(70));
6393   verifyFormat(
6394       "template <class T,\n"
6395       "          class = typename ::std::enable_if<\n"
6396       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
6397       "void F();",
6398       getGoogleStyleWithColumns(68));
6399 
6400   verifyIndependentOfContext("MACRO(int *i);");
6401   verifyIndependentOfContext("MACRO(auto *a);");
6402   verifyIndependentOfContext("MACRO(const A *a);");
6403   verifyIndependentOfContext("MACRO(A *const a);");
6404   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
6405   verifyFormat("void f() { f(float{1}, a * a); }");
6406   // FIXME: Is there a way to make this work?
6407   // verifyIndependentOfContext("MACRO(A *a);");
6408 
6409   verifyFormat("DatumHandle const *operator->() const { return input_; }");
6410   verifyFormat("return options != nullptr && operator==(*options);");
6411 
6412   EXPECT_EQ("#define OP(x)                                    \\\n"
6413             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
6414             "    return s << a.DebugString();                 \\\n"
6415             "  }",
6416             format("#define OP(x) \\\n"
6417                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
6418                    "    return s << a.DebugString(); \\\n"
6419                    "  }",
6420                    getLLVMStyleWithColumns(50)));
6421 
6422   // FIXME: We cannot handle this case yet; we might be able to figure out that
6423   // foo<x> d > v; doesn't make sense.
6424   verifyFormat("foo<a<b && c> d> v;");
6425 
6426   FormatStyle PointerMiddle = getLLVMStyle();
6427   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
6428   verifyFormat("delete *x;", PointerMiddle);
6429   verifyFormat("int * x;", PointerMiddle);
6430   verifyFormat("int *[] x;", PointerMiddle);
6431   verifyFormat("template <int * y> f() {}", PointerMiddle);
6432   verifyFormat("int * f(int * a) {}", PointerMiddle);
6433   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
6434   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
6435   verifyFormat("A<int *> a;", PointerMiddle);
6436   verifyFormat("A<int **> a;", PointerMiddle);
6437   verifyFormat("A<int *, int *> a;", PointerMiddle);
6438   verifyFormat("A<int *[]> a;", PointerMiddle);
6439   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
6440   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
6441   verifyFormat("T ** t = new T *;", PointerMiddle);
6442 
6443   // Member function reference qualifiers aren't binary operators.
6444   verifyFormat("string // break\n"
6445                "operator()() & {}");
6446   verifyFormat("string // break\n"
6447                "operator()() && {}");
6448   verifyGoogleFormat("template <typename T>\n"
6449                      "auto x() & -> int {}");
6450 }
6451 
6452 TEST_F(FormatTest, UnderstandsAttributes) {
6453   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
6454   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
6455                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6456   FormatStyle AfterType = getLLVMStyle();
6457   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6458   verifyFormat("__attribute__((nodebug)) void\n"
6459                "foo() {}\n",
6460                AfterType);
6461 }
6462 
6463 TEST_F(FormatTest, UnderstandsSquareAttributes) {
6464   verifyFormat("SomeType s [[unused]] (InitValue);");
6465   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
6466   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
6467   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
6468   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
6469   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6470                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
6471 
6472   // Make sure we do not mistake attributes for array subscripts.
6473   verifyFormat("int a() {}\n"
6474                "[[unused]] int b() {}\n");
6475 
6476   // On the other hand, we still need to correctly find array subscripts.
6477   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
6478 
6479   // Make sure we do not parse attributes as lambda introducers.
6480   FormatStyle MultiLineFunctions = getLLVMStyle();
6481   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6482   verifyFormat("[[unused]] int b() {\n"
6483                "  return 42;\n"
6484                "}\n",
6485                MultiLineFunctions);
6486 }
6487 
6488 TEST_F(FormatTest, UnderstandsEllipsis) {
6489   verifyFormat("int printf(const char *fmt, ...);");
6490   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
6491   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
6492 
6493   FormatStyle PointersLeft = getLLVMStyle();
6494   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
6495   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
6496 }
6497 
6498 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
6499   EXPECT_EQ("int *a;\n"
6500             "int *a;\n"
6501             "int *a;",
6502             format("int *a;\n"
6503                    "int* a;\n"
6504                    "int *a;",
6505                    getGoogleStyle()));
6506   EXPECT_EQ("int* a;\n"
6507             "int* a;\n"
6508             "int* a;",
6509             format("int* a;\n"
6510                    "int* a;\n"
6511                    "int *a;",
6512                    getGoogleStyle()));
6513   EXPECT_EQ("int *a;\n"
6514             "int *a;\n"
6515             "int *a;",
6516             format("int *a;\n"
6517                    "int * a;\n"
6518                    "int *  a;",
6519                    getGoogleStyle()));
6520   EXPECT_EQ("auto x = [] {\n"
6521             "  int *a;\n"
6522             "  int *a;\n"
6523             "  int *a;\n"
6524             "};",
6525             format("auto x=[]{int *a;\n"
6526                    "int * a;\n"
6527                    "int *  a;};",
6528                    getGoogleStyle()));
6529 }
6530 
6531 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6532   verifyFormat("int f(int &&a) {}");
6533   verifyFormat("int f(int a, char &&b) {}");
6534   verifyFormat("void f() { int &&a = b; }");
6535   verifyGoogleFormat("int f(int a, char&& b) {}");
6536   verifyGoogleFormat("void f() { int&& a = b; }");
6537 
6538   verifyIndependentOfContext("A<int &&> a;");
6539   verifyIndependentOfContext("A<int &&, int &&> a;");
6540   verifyGoogleFormat("A<int&&> a;");
6541   verifyGoogleFormat("A<int&&, int&&> a;");
6542 
6543   // Not rvalue references:
6544   verifyFormat("template <bool B, bool C> class A {\n"
6545                "  static_assert(B && C, \"Something is wrong\");\n"
6546                "};");
6547   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6548   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6549   verifyFormat("#define A(a, b) (a && b)");
6550 }
6551 
6552 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6553   verifyFormat("void f() {\n"
6554                "  x[aaaaaaaaa -\n"
6555                "    b] = 23;\n"
6556                "}",
6557                getLLVMStyleWithColumns(15));
6558 }
6559 
6560 TEST_F(FormatTest, FormatsCasts) {
6561   verifyFormat("Type *A = static_cast<Type *>(P);");
6562   verifyFormat("Type *A = (Type *)P;");
6563   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6564   verifyFormat("int a = (int)(2.0f);");
6565   verifyFormat("int a = (int)2.0f;");
6566   verifyFormat("x[(int32)y];");
6567   verifyFormat("x = (int32)y;");
6568   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6569   verifyFormat("int a = (int)*b;");
6570   verifyFormat("int a = (int)2.0f;");
6571   verifyFormat("int a = (int)~0;");
6572   verifyFormat("int a = (int)++a;");
6573   verifyFormat("int a = (int)sizeof(int);");
6574   verifyFormat("int a = (int)+2;");
6575   verifyFormat("my_int a = (my_int)2.0f;");
6576   verifyFormat("my_int a = (my_int)sizeof(int);");
6577   verifyFormat("return (my_int)aaa;");
6578   verifyFormat("#define x ((int)-1)");
6579   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6580   verifyFormat("#define p(q) ((int *)&q)");
6581   verifyFormat("fn(a)(b) + 1;");
6582 
6583   verifyFormat("void f() { my_int a = (my_int)*b; }");
6584   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6585   verifyFormat("my_int a = (my_int)~0;");
6586   verifyFormat("my_int a = (my_int)++a;");
6587   verifyFormat("my_int a = (my_int)-2;");
6588   verifyFormat("my_int a = (my_int)1;");
6589   verifyFormat("my_int a = (my_int *)1;");
6590   verifyFormat("my_int a = (const my_int)-1;");
6591   verifyFormat("my_int a = (const my_int *)-1;");
6592   verifyFormat("my_int a = (my_int)(my_int)-1;");
6593   verifyFormat("my_int a = (ns::my_int)-2;");
6594   verifyFormat("case (my_int)ONE:");
6595   verifyFormat("auto x = (X)this;");
6596 
6597   // FIXME: single value wrapped with paren will be treated as cast.
6598   verifyFormat("void f(int i = (kValue)*kMask) {}");
6599 
6600   verifyFormat("{ (void)F; }");
6601 
6602   // Don't break after a cast's
6603   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6604                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6605                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6606 
6607   // These are not casts.
6608   verifyFormat("void f(int *) {}");
6609   verifyFormat("f(foo)->b;");
6610   verifyFormat("f(foo).b;");
6611   verifyFormat("f(foo)(b);");
6612   verifyFormat("f(foo)[b];");
6613   verifyFormat("[](foo) { return 4; }(bar);");
6614   verifyFormat("(*funptr)(foo)[4];");
6615   verifyFormat("funptrs[4](foo)[4];");
6616   verifyFormat("void f(int *);");
6617   verifyFormat("void f(int *) = 0;");
6618   verifyFormat("void f(SmallVector<int>) {}");
6619   verifyFormat("void f(SmallVector<int>);");
6620   verifyFormat("void f(SmallVector<int>) = 0;");
6621   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6622   verifyFormat("int a = sizeof(int) * b;");
6623   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6624   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6625   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6626   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6627 
6628   // These are not casts, but at some point were confused with casts.
6629   verifyFormat("virtual void foo(int *) override;");
6630   verifyFormat("virtual void foo(char &) const;");
6631   verifyFormat("virtual void foo(int *a, char *) const;");
6632   verifyFormat("int a = sizeof(int *) + b;");
6633   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6634   verifyFormat("bool b = f(g<int>) && c;");
6635   verifyFormat("typedef void (*f)(int i) func;");
6636 
6637   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6638                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6639   // FIXME: The indentation here is not ideal.
6640   verifyFormat(
6641       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6642       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6643       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6644 }
6645 
6646 TEST_F(FormatTest, FormatsFunctionTypes) {
6647   verifyFormat("A<bool()> a;");
6648   verifyFormat("A<SomeType()> a;");
6649   verifyFormat("A<void (*)(int, std::string)> a;");
6650   verifyFormat("A<void *(int)>;");
6651   verifyFormat("void *(*a)(int *, SomeType *);");
6652   verifyFormat("int (*func)(void *);");
6653   verifyFormat("void f() { int (*func)(void *); }");
6654   verifyFormat("template <class CallbackClass>\n"
6655                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6656 
6657   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6658   verifyGoogleFormat("void* (*a)(int);");
6659   verifyGoogleFormat(
6660       "template <class CallbackClass>\n"
6661       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6662 
6663   // Other constructs can look somewhat like function types:
6664   verifyFormat("A<sizeof(*x)> a;");
6665   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6666   verifyFormat("some_var = function(*some_pointer_var)[0];");
6667   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6668   verifyFormat("int x = f(&h)();");
6669   verifyFormat("returnsFunction(&param1, &param2)(param);");
6670   verifyFormat("std::function<\n"
6671                "    LooooooooooongTemplatedType<\n"
6672                "        SomeType>*(\n"
6673                "        LooooooooooooooooongType type)>\n"
6674                "    function;",
6675                getGoogleStyleWithColumns(40));
6676 }
6677 
6678 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6679   verifyFormat("A (*foo_)[6];");
6680   verifyFormat("vector<int> (*foo_)[6];");
6681 }
6682 
6683 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6684   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6685                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6686   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6687                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6688   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6689                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6690 
6691   // Different ways of ()-initializiation.
6692   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6693                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6694   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6695                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6696   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6697                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6698   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6699                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6700 
6701   // Lambdas should not confuse the variable declaration heuristic.
6702   verifyFormat("LooooooooooooooooongType\n"
6703                "    variable(nullptr, [](A *a) {});",
6704                getLLVMStyleWithColumns(40));
6705 }
6706 
6707 TEST_F(FormatTest, BreaksLongDeclarations) {
6708   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6709                "    AnotherNameForTheLongType;");
6710   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6711                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6712   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6713                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6714   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6715                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6716   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6717                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6718   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6719                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6720   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6721                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6722   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6723                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6724   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6725                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6726   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6727                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6728   FormatStyle Indented = getLLVMStyle();
6729   Indented.IndentWrappedFunctionNames = true;
6730   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6731                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6732                Indented);
6733   verifyFormat(
6734       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6735       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6736       Indented);
6737   verifyFormat(
6738       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6739       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6740       Indented);
6741   verifyFormat(
6742       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6743       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6744       Indented);
6745 
6746   // FIXME: Without the comment, this breaks after "(".
6747   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6748                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6749                getGoogleStyle());
6750 
6751   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6752                "                  int LoooooooooooooooooooongParam2) {}");
6753   verifyFormat(
6754       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6755       "                                   SourceLocation L, IdentifierIn *II,\n"
6756       "                                   Type *T) {}");
6757   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6758                "ReallyReaaallyLongFunctionName(\n"
6759                "    const std::string &SomeParameter,\n"
6760                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6761                "        &ReallyReallyLongParameterName,\n"
6762                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6763                "        &AnotherLongParameterName) {}");
6764   verifyFormat("template <typename A>\n"
6765                "SomeLoooooooooooooooooooooongType<\n"
6766                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6767                "Function() {}");
6768 
6769   verifyGoogleFormat(
6770       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6771       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6772   verifyGoogleFormat(
6773       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6774       "                                   SourceLocation L) {}");
6775   verifyGoogleFormat(
6776       "some_namespace::LongReturnType\n"
6777       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6778       "    int first_long_parameter, int second_parameter) {}");
6779 
6780   verifyGoogleFormat("template <typename T>\n"
6781                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6782                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6783   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6784                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6785 
6786   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6787                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6788                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6789   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6790                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6791                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6792   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6793                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6794                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6795                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6796 
6797   verifyFormat("template <typename T> // Templates on own line.\n"
6798                "static int            // Some comment.\n"
6799                "MyFunction(int a);",
6800                getLLVMStyle());
6801 }
6802 
6803 TEST_F(FormatTest, FormatsArrays) {
6804   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6805                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6806   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6807                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6808   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6809                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6810   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6811                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6812   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6813                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6814   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6815                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6816                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6817   verifyFormat(
6818       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6819       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6820       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6821   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6822                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6823 
6824   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6825                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6826   verifyFormat(
6827       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6828       "                                  .aaaaaaa[0]\n"
6829       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6830   verifyFormat("a[::b::c];");
6831 
6832   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6833 
6834   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6835   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6836 }
6837 
6838 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6839   verifyFormat("(a)->b();");
6840   verifyFormat("--a;");
6841 }
6842 
6843 TEST_F(FormatTest, HandlesIncludeDirectives) {
6844   verifyFormat("#include <string>\n"
6845                "#include <a/b/c.h>\n"
6846                "#include \"a/b/string\"\n"
6847                "#include \"string.h\"\n"
6848                "#include \"string.h\"\n"
6849                "#include <a-a>\n"
6850                "#include < path with space >\n"
6851                "#include_next <test.h>"
6852                "#include \"abc.h\" // this is included for ABC\n"
6853                "#include \"some long include\" // with a comment\n"
6854                "#include \"some very long include path\"\n"
6855                "#include <some/very/long/include/path>\n",
6856                getLLVMStyleWithColumns(35));
6857   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6858   EXPECT_EQ("#include <a>", format("#include<a>"));
6859 
6860   verifyFormat("#import <string>");
6861   verifyFormat("#import <a/b/c.h>");
6862   verifyFormat("#import \"a/b/string\"");
6863   verifyFormat("#import \"string.h\"");
6864   verifyFormat("#import \"string.h\"");
6865   verifyFormat("#if __has_include(<strstream>)\n"
6866                "#include <strstream>\n"
6867                "#endif");
6868 
6869   verifyFormat("#define MY_IMPORT <a/b>");
6870 
6871   verifyFormat("#if __has_include(<a/b>)");
6872   verifyFormat("#if __has_include_next(<a/b>)");
6873   verifyFormat("#define F __has_include(<a/b>)");
6874   verifyFormat("#define F __has_include_next(<a/b>)");
6875 
6876   // Protocol buffer definition or missing "#".
6877   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6878                getLLVMStyleWithColumns(30));
6879 
6880   FormatStyle Style = getLLVMStyle();
6881   Style.AlwaysBreakBeforeMultilineStrings = true;
6882   Style.ColumnLimit = 0;
6883   verifyFormat("#import \"abc.h\"", Style);
6884 
6885   // But 'import' might also be a regular C++ namespace.
6886   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6887                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6888 }
6889 
6890 //===----------------------------------------------------------------------===//
6891 // Error recovery tests.
6892 //===----------------------------------------------------------------------===//
6893 
6894 TEST_F(FormatTest, IncompleteParameterLists) {
6895   FormatStyle NoBinPacking = getLLVMStyle();
6896   NoBinPacking.BinPackParameters = false;
6897   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6898                "                        double *min_x,\n"
6899                "                        double *max_x,\n"
6900                "                        double *min_y,\n"
6901                "                        double *max_y,\n"
6902                "                        double *min_z,\n"
6903                "                        double *max_z, ) {}",
6904                NoBinPacking);
6905 }
6906 
6907 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6908   verifyFormat("void f() { return; }\n42");
6909   verifyFormat("void f() {\n"
6910                "  if (0)\n"
6911                "    return;\n"
6912                "}\n"
6913                "42");
6914   verifyFormat("void f() { return }\n42");
6915   verifyFormat("void f() {\n"
6916                "  if (0)\n"
6917                "    return\n"
6918                "}\n"
6919                "42");
6920 }
6921 
6922 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6923   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6924   EXPECT_EQ("void f() {\n"
6925             "  if (a)\n"
6926             "    return\n"
6927             "}",
6928             format("void  f  (  )  {  if  ( a )  return  }"));
6929   EXPECT_EQ("namespace N {\n"
6930             "void f()\n"
6931             "}",
6932             format("namespace  N  {  void f()  }"));
6933   EXPECT_EQ("namespace N {\n"
6934             "void f() {}\n"
6935             "void g()\n"
6936             "} // namespace N",
6937             format("namespace N  { void f( ) { } void g( ) }"));
6938 }
6939 
6940 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6941   verifyFormat("int aaaaaaaa =\n"
6942                "    // Overlylongcomment\n"
6943                "    b;",
6944                getLLVMStyleWithColumns(20));
6945   verifyFormat("function(\n"
6946                "    ShortArgument,\n"
6947                "    LoooooooooooongArgument);\n",
6948                getLLVMStyleWithColumns(20));
6949 }
6950 
6951 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6952   verifyFormat("public:");
6953   verifyFormat("class A {\n"
6954                "public\n"
6955                "  void f() {}\n"
6956                "};");
6957   verifyFormat("public\n"
6958                "int qwerty;");
6959   verifyFormat("public\n"
6960                "B {}");
6961   verifyFormat("public\n"
6962                "{}");
6963   verifyFormat("public\n"
6964                "B { int x; }");
6965 }
6966 
6967 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6968   verifyFormat("{");
6969   verifyFormat("#})");
6970   verifyNoCrash("(/**/[:!] ?[).");
6971 }
6972 
6973 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
6974   // Found by oss-fuzz:
6975   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
6976   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
6977   Style.ColumnLimit = 60;
6978   verifyNoCrash(
6979       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
6980       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
6981       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
6982       Style);
6983 }
6984 
6985 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6986   verifyFormat("do {\n}");
6987   verifyFormat("do {\n}\n"
6988                "f();");
6989   verifyFormat("do {\n}\n"
6990                "wheeee(fun);");
6991   verifyFormat("do {\n"
6992                "  f();\n"
6993                "}");
6994 }
6995 
6996 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6997   verifyFormat("if {\n  foo;\n  foo();\n}");
6998   verifyFormat("switch {\n  foo;\n  foo();\n}");
6999   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
7000   verifyFormat("while {\n  foo;\n  foo();\n}");
7001   verifyFormat("do {\n  foo;\n  foo();\n} while;");
7002 }
7003 
7004 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
7005   verifyIncompleteFormat("namespace {\n"
7006                          "class Foo { Foo (\n"
7007                          "};\n"
7008                          "} // namespace");
7009 }
7010 
7011 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
7012   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
7013   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
7014   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
7015   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
7016 
7017   EXPECT_EQ("{\n"
7018             "  {\n"
7019             "    breakme(\n"
7020             "        qwe);\n"
7021             "  }\n",
7022             format("{\n"
7023                    "    {\n"
7024                    " breakme(qwe);\n"
7025                    "}\n",
7026                    getLLVMStyleWithColumns(10)));
7027 }
7028 
7029 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
7030   verifyFormat("int x = {\n"
7031                "    avariable,\n"
7032                "    b(alongervariable)};",
7033                getLLVMStyleWithColumns(25));
7034 }
7035 
7036 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
7037   verifyFormat("return (a)(b){1, 2, 3};");
7038 }
7039 
7040 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
7041   verifyFormat("vector<int> x{1, 2, 3, 4};");
7042   verifyFormat("vector<int> x{\n"
7043                "    1,\n"
7044                "    2,\n"
7045                "    3,\n"
7046                "    4,\n"
7047                "};");
7048   verifyFormat("vector<T> x{{}, {}, {}, {}};");
7049   verifyFormat("f({1, 2});");
7050   verifyFormat("auto v = Foo{-1};");
7051   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
7052   verifyFormat("Class::Class : member{1, 2, 3} {}");
7053   verifyFormat("new vector<int>{1, 2, 3};");
7054   verifyFormat("new int[3]{1, 2, 3};");
7055   verifyFormat("new int{1};");
7056   verifyFormat("return {arg1, arg2};");
7057   verifyFormat("return {arg1, SomeType{parameter}};");
7058   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
7059   verifyFormat("new T{arg1, arg2};");
7060   verifyFormat("f(MyMap[{composite, key}]);");
7061   verifyFormat("class Class {\n"
7062                "  T member = {arg1, arg2};\n"
7063                "};");
7064   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
7065   verifyFormat("const struct A a = {.a = 1, .b = 2};");
7066   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
7067   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
7068   verifyFormat("int a = std::is_integral<int>{} + 0;");
7069 
7070   verifyFormat("int foo(int i) { return fo1{}(i); }");
7071   verifyFormat("int foo(int i) { return fo1{}(i); }");
7072   verifyFormat("auto i = decltype(x){};");
7073   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
7074   verifyFormat("Node n{1, Node{1000}, //\n"
7075                "       2};");
7076   verifyFormat("Aaaa aaaaaaa{\n"
7077                "    {\n"
7078                "        aaaa,\n"
7079                "    },\n"
7080                "};");
7081   verifyFormat("class C : public D {\n"
7082                "  SomeClass SC{2};\n"
7083                "};");
7084   verifyFormat("class C : public A {\n"
7085                "  class D : public B {\n"
7086                "    void f() { int i{2}; }\n"
7087                "  };\n"
7088                "};");
7089   verifyFormat("#define A {a, a},");
7090 
7091   // Avoid breaking between equal sign and opening brace
7092   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
7093   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
7094   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
7095                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
7096                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
7097                "     {\"ccccccccccccccccccccc\", 2}};",
7098                AvoidBreakingFirstArgument);
7099 
7100   // Binpacking only if there is no trailing comma
7101   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
7102                "                      cccccccccc, dddddddddd};",
7103 			   getLLVMStyleWithColumns(50));
7104   verifyFormat("const Aaaaaa aaaaa = {\n"
7105                "    aaaaaaaaaaa,\n"
7106                "    bbbbbbbbbbb,\n"
7107                "    ccccccccccc,\n"
7108                "    ddddddddddd,\n"
7109                "};", getLLVMStyleWithColumns(50));
7110 
7111   // Cases where distinguising braced lists and blocks is hard.
7112   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
7113   verifyFormat("void f() {\n"
7114                "  return; // comment\n"
7115                "}\n"
7116                "SomeType t;");
7117   verifyFormat("void f() {\n"
7118                "  if (a) {\n"
7119                "    f();\n"
7120                "  }\n"
7121                "}\n"
7122                "SomeType t;");
7123 
7124   // In combination with BinPackArguments = false.
7125   FormatStyle NoBinPacking = getLLVMStyle();
7126   NoBinPacking.BinPackArguments = false;
7127   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
7128                "                      bbbbb,\n"
7129                "                      ccccc,\n"
7130                "                      ddddd,\n"
7131                "                      eeeee,\n"
7132                "                      ffffff,\n"
7133                "                      ggggg,\n"
7134                "                      hhhhhh,\n"
7135                "                      iiiiii,\n"
7136                "                      jjjjjj,\n"
7137                "                      kkkkkk};",
7138                NoBinPacking);
7139   verifyFormat("const Aaaaaa aaaaa = {\n"
7140                "    aaaaa,\n"
7141                "    bbbbb,\n"
7142                "    ccccc,\n"
7143                "    ddddd,\n"
7144                "    eeeee,\n"
7145                "    ffffff,\n"
7146                "    ggggg,\n"
7147                "    hhhhhh,\n"
7148                "    iiiiii,\n"
7149                "    jjjjjj,\n"
7150                "    kkkkkk,\n"
7151                "};",
7152                NoBinPacking);
7153   verifyFormat(
7154       "const Aaaaaa aaaaa = {\n"
7155       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
7156       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
7157       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
7158       "};",
7159       NoBinPacking);
7160 
7161   // FIXME: The alignment of these trailing comments might be bad. Then again,
7162   // this might be utterly useless in real code.
7163   verifyFormat("Constructor::Constructor()\n"
7164                "    : some_value{         //\n"
7165                "                 aaaaaaa, //\n"
7166                "                 bbbbbbb} {}");
7167 
7168   // In braced lists, the first comment is always assumed to belong to the
7169   // first element. Thus, it can be moved to the next or previous line as
7170   // appropriate.
7171   EXPECT_EQ("function({// First element:\n"
7172             "          1,\n"
7173             "          // Second element:\n"
7174             "          2});",
7175             format("function({\n"
7176                    "    // First element:\n"
7177                    "    1,\n"
7178                    "    // Second element:\n"
7179                    "    2});"));
7180   EXPECT_EQ("std::vector<int> MyNumbers{\n"
7181             "    // First element:\n"
7182             "    1,\n"
7183             "    // Second element:\n"
7184             "    2};",
7185             format("std::vector<int> MyNumbers{// First element:\n"
7186                    "                           1,\n"
7187                    "                           // Second element:\n"
7188                    "                           2};",
7189                    getLLVMStyleWithColumns(30)));
7190   // A trailing comma should still lead to an enforced line break and no
7191   // binpacking.
7192   EXPECT_EQ("vector<int> SomeVector = {\n"
7193             "    // aaa\n"
7194             "    1,\n"
7195             "    2,\n"
7196             "};",
7197             format("vector<int> SomeVector = { // aaa\n"
7198                    "    1, 2, };"));
7199 
7200   FormatStyle ExtraSpaces = getLLVMStyle();
7201   ExtraSpaces.Cpp11BracedListStyle = false;
7202   ExtraSpaces.ColumnLimit = 75;
7203   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
7204   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
7205   verifyFormat("f({ 1, 2 });", ExtraSpaces);
7206   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
7207   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
7208   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
7209   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
7210   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
7211   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
7212   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
7213   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
7214   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
7215   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
7216   verifyFormat("class Class {\n"
7217                "  T member = { arg1, arg2 };\n"
7218                "};",
7219                ExtraSpaces);
7220   verifyFormat(
7221       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7222       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
7223       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7224       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
7225       ExtraSpaces);
7226   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
7227   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
7228                ExtraSpaces);
7229   verifyFormat(
7230       "someFunction(OtherParam,\n"
7231       "             BracedList{ // comment 1 (Forcing interesting break)\n"
7232       "                         param1, param2,\n"
7233       "                         // comment 2\n"
7234       "                         param3, param4 });",
7235       ExtraSpaces);
7236   verifyFormat(
7237       "std::this_thread::sleep_for(\n"
7238       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
7239       ExtraSpaces);
7240   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
7241                "    aaaaaaa,\n"
7242                "    aaaaaaaaaa,\n"
7243                "    aaaaa,\n"
7244                "    aaaaaaaaaaaaaaa,\n"
7245                "    aaa,\n"
7246                "    aaaaaaaaaa,\n"
7247                "    a,\n"
7248                "    aaaaaaaaaaaaaaaaaaaaa,\n"
7249                "    aaaaaaaaaaaa,\n"
7250                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
7251                "    aaaaaaa,\n"
7252                "    a};");
7253   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
7254   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
7255   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
7256 
7257   // Avoid breaking between initializer/equal sign and opening brace
7258   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
7259   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
7260                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7261                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7262                "  { \"ccccccccccccccccccccc\", 2 }\n"
7263                "};",
7264                ExtraSpaces);
7265   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
7266                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
7267                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
7268                "  { \"ccccccccccccccccccccc\", 2 }\n"
7269                "};",
7270                ExtraSpaces);
7271 
7272   FormatStyle SpaceBeforeBrace = getLLVMStyle();
7273   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
7274   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
7275   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
7276 }
7277 
7278 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
7279   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7280                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7281                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7282                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7283                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7284                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7285   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
7286                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7287                "                 1, 22, 333, 4444, 55555, //\n"
7288                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7289                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
7290   verifyFormat(
7291       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7292       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
7293       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
7294       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7295       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7296       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
7297       "                 7777777};");
7298   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7299                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7300                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7301   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7302                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7303                "    // Separating comment.\n"
7304                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
7305   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
7306                "    // Leading comment\n"
7307                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
7308                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
7309   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7310                "                 1, 1, 1, 1};",
7311                getLLVMStyleWithColumns(39));
7312   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7313                "                 1, 1, 1, 1};",
7314                getLLVMStyleWithColumns(38));
7315   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
7316                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
7317                getLLVMStyleWithColumns(43));
7318   verifyFormat(
7319       "static unsigned SomeValues[10][3] = {\n"
7320       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
7321       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
7322   verifyFormat("static auto fields = new vector<string>{\n"
7323                "    \"aaaaaaaaaaaaa\",\n"
7324                "    \"aaaaaaaaaaaaa\",\n"
7325                "    \"aaaaaaaaaaaa\",\n"
7326                "    \"aaaaaaaaaaaaaa\",\n"
7327                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7328                "    \"aaaaaaaaaaaa\",\n"
7329                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
7330                "};");
7331   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
7332   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
7333                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
7334                "                 3, cccccccccccccccccccccc};",
7335                getLLVMStyleWithColumns(60));
7336 
7337   // Trailing commas.
7338   verifyFormat("vector<int> x = {\n"
7339                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
7340                "};",
7341                getLLVMStyleWithColumns(39));
7342   verifyFormat("vector<int> x = {\n"
7343                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
7344                "};",
7345                getLLVMStyleWithColumns(39));
7346   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
7347                "                 1, 1, 1, 1,\n"
7348                "                 /**/ /**/};",
7349                getLLVMStyleWithColumns(39));
7350 
7351   // Trailing comment in the first line.
7352   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
7353                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
7354                "    111111111,  222222222,  3333333333,  444444444,  //\n"
7355                "    11111111,   22222222,   333333333,   44444444};");
7356   // Trailing comment in the last line.
7357   verifyFormat("int aaaaa[] = {\n"
7358                "    1, 2, 3, // comment\n"
7359                "    4, 5, 6  // comment\n"
7360                "};");
7361 
7362   // With nested lists, we should either format one item per line or all nested
7363   // lists one on line.
7364   // FIXME: For some nested lists, we can do better.
7365   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
7366                "        {aaaaaaaaaaaaaaaaaaa},\n"
7367                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
7368                "        {aaaaaaaaaaaaaaaaa}};",
7369                getLLVMStyleWithColumns(60));
7370   verifyFormat(
7371       "SomeStruct my_struct_array = {\n"
7372       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
7373       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
7374       "    {aaa, aaa},\n"
7375       "    {aaa, aaa},\n"
7376       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
7377       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7378       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
7379 
7380   // No column layout should be used here.
7381   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
7382                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
7383 
7384   verifyNoCrash("a<,");
7385 
7386   // No braced initializer here.
7387   verifyFormat("void f() {\n"
7388                "  struct Dummy {};\n"
7389                "  f(v);\n"
7390                "}");
7391 
7392   // Long lists should be formatted in columns even if they are nested.
7393   verifyFormat(
7394       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7395       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7396       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7397       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7398       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
7399       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
7400 
7401   // Allow "single-column" layout even if that violates the column limit. There
7402   // isn't going to be a better way.
7403   verifyFormat("std::vector<int> a = {\n"
7404                "    aaaaaaaa,\n"
7405                "    aaaaaaaa,\n"
7406                "    aaaaaaaa,\n"
7407                "    aaaaaaaa,\n"
7408                "    aaaaaaaaaa,\n"
7409                "    aaaaaaaa,\n"
7410                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
7411                getLLVMStyleWithColumns(30));
7412   verifyFormat("vector<int> aaaa = {\n"
7413                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7414                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7415                "    aaaaaa.aaaaaaa,\n"
7416                "    aaaaaa.aaaaaaa,\n"
7417                "    aaaaaa.aaaaaaa,\n"
7418                "    aaaaaa.aaaaaaa,\n"
7419                "};");
7420 
7421   // Don't create hanging lists.
7422   verifyFormat("someFunction(Param, {List1, List2,\n"
7423                "                     List3});",
7424                getLLVMStyleWithColumns(35));
7425   verifyFormat("someFunction(Param, Param,\n"
7426                "             {List1, List2,\n"
7427                "              List3});",
7428                getLLVMStyleWithColumns(35));
7429   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
7430                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
7431 }
7432 
7433 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
7434   FormatStyle DoNotMerge = getLLVMStyle();
7435   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7436 
7437   verifyFormat("void f() { return 42; }");
7438   verifyFormat("void f() {\n"
7439                "  return 42;\n"
7440                "}",
7441                DoNotMerge);
7442   verifyFormat("void f() {\n"
7443                "  // Comment\n"
7444                "}");
7445   verifyFormat("{\n"
7446                "#error {\n"
7447                "  int a;\n"
7448                "}");
7449   verifyFormat("{\n"
7450                "  int a;\n"
7451                "#error {\n"
7452                "}");
7453   verifyFormat("void f() {} // comment");
7454   verifyFormat("void f() { int a; } // comment");
7455   verifyFormat("void f() {\n"
7456                "} // comment",
7457                DoNotMerge);
7458   verifyFormat("void f() {\n"
7459                "  int a;\n"
7460                "} // comment",
7461                DoNotMerge);
7462   verifyFormat("void f() {\n"
7463                "} // comment",
7464                getLLVMStyleWithColumns(15));
7465 
7466   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
7467   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
7468 
7469   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
7470   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
7471   verifyFormat("class C {\n"
7472                "  C()\n"
7473                "      : iiiiiiii(nullptr),\n"
7474                "        kkkkkkk(nullptr),\n"
7475                "        mmmmmmm(nullptr),\n"
7476                "        nnnnnnn(nullptr) {}\n"
7477                "};",
7478                getGoogleStyle());
7479 
7480   FormatStyle NoColumnLimit = getLLVMStyle();
7481   NoColumnLimit.ColumnLimit = 0;
7482   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
7483   EXPECT_EQ("class C {\n"
7484             "  A() : b(0) {}\n"
7485             "};",
7486             format("class C{A():b(0){}};", NoColumnLimit));
7487   EXPECT_EQ("A()\n"
7488             "    : b(0) {\n"
7489             "}",
7490             format("A()\n:b(0)\n{\n}", NoColumnLimit));
7491 
7492   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
7493   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
7494       FormatStyle::SFS_None;
7495   EXPECT_EQ("A()\n"
7496             "    : b(0) {\n"
7497             "}",
7498             format("A():b(0){}", DoNotMergeNoColumnLimit));
7499   EXPECT_EQ("A()\n"
7500             "    : b(0) {\n"
7501             "}",
7502             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
7503 
7504   verifyFormat("#define A          \\\n"
7505                "  void f() {       \\\n"
7506                "    int i;         \\\n"
7507                "  }",
7508                getLLVMStyleWithColumns(20));
7509   verifyFormat("#define A           \\\n"
7510                "  void f() { int i; }",
7511                getLLVMStyleWithColumns(21));
7512   verifyFormat("#define A            \\\n"
7513                "  void f() {         \\\n"
7514                "    int i;           \\\n"
7515                "  }                  \\\n"
7516                "  int j;",
7517                getLLVMStyleWithColumns(22));
7518   verifyFormat("#define A             \\\n"
7519                "  void f() { int i; } \\\n"
7520                "  int j;",
7521                getLLVMStyleWithColumns(23));
7522 }
7523 
7524 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
7525   FormatStyle MergeEmptyOnly = getLLVMStyle();
7526   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7527   verifyFormat("class C {\n"
7528                "  int f() {}\n"
7529                "};",
7530                MergeEmptyOnly);
7531   verifyFormat("class C {\n"
7532                "  int f() {\n"
7533                "    return 42;\n"
7534                "  }\n"
7535                "};",
7536                MergeEmptyOnly);
7537   verifyFormat("int f() {}", MergeEmptyOnly);
7538   verifyFormat("int f() {\n"
7539                "  return 42;\n"
7540                "}",
7541                MergeEmptyOnly);
7542 
7543   // Also verify behavior when BraceWrapping.AfterFunction = true
7544   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7545   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
7546   verifyFormat("int f() {}", MergeEmptyOnly);
7547   verifyFormat("class C {\n"
7548                "  int f() {}\n"
7549                "};",
7550                MergeEmptyOnly);
7551 }
7552 
7553 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
7554   FormatStyle MergeInlineOnly = getLLVMStyle();
7555   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7556   verifyFormat("class C {\n"
7557                "  int f() { return 42; }\n"
7558                "};",
7559                MergeInlineOnly);
7560   verifyFormat("int f() {\n"
7561                "  return 42;\n"
7562                "}",
7563                MergeInlineOnly);
7564 
7565   // SFS_Inline implies SFS_Empty
7566   verifyFormat("class C {\n"
7567                "  int f() {}\n"
7568                "};",
7569                MergeInlineOnly);
7570   verifyFormat("int f() {}", MergeInlineOnly);
7571 
7572   // Also verify behavior when BraceWrapping.AfterFunction = true
7573   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7574   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7575   verifyFormat("class C {\n"
7576                "  int f() { return 42; }\n"
7577                "};",
7578                MergeInlineOnly);
7579   verifyFormat("int f()\n"
7580                "{\n"
7581                "  return 42;\n"
7582                "}",
7583                MergeInlineOnly);
7584 
7585   // SFS_Inline implies SFS_Empty
7586   verifyFormat("int f() {}", MergeInlineOnly);
7587   verifyFormat("class C {\n"
7588                "  int f() {}\n"
7589                "};",
7590                MergeInlineOnly);
7591 }
7592 
7593 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7594   FormatStyle MergeInlineOnly = getLLVMStyle();
7595   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7596       FormatStyle::SFS_InlineOnly;
7597   verifyFormat("class C {\n"
7598                "  int f() { return 42; }\n"
7599                "};",
7600                MergeInlineOnly);
7601   verifyFormat("int f() {\n"
7602                "  return 42;\n"
7603                "}",
7604                MergeInlineOnly);
7605 
7606   // SFS_InlineOnly does not imply SFS_Empty
7607   verifyFormat("class C {\n"
7608                "  int f() {}\n"
7609                "};",
7610                MergeInlineOnly);
7611   verifyFormat("int f() {\n"
7612                "}",
7613                MergeInlineOnly);
7614 
7615   // Also verify behavior when BraceWrapping.AfterFunction = true
7616   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7617   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7618   verifyFormat("class C {\n"
7619                "  int f() { return 42; }\n"
7620                "};",
7621                MergeInlineOnly);
7622   verifyFormat("int f()\n"
7623                "{\n"
7624                "  return 42;\n"
7625                "}",
7626                MergeInlineOnly);
7627 
7628   // SFS_InlineOnly does not imply SFS_Empty
7629   verifyFormat("int f()\n"
7630                "{\n"
7631                "}",
7632                MergeInlineOnly);
7633   verifyFormat("class C {\n"
7634                "  int f() {}\n"
7635                "};",
7636                MergeInlineOnly);
7637 }
7638 
7639 TEST_F(FormatTest, SplitEmptyFunction) {
7640   FormatStyle Style = getLLVMStyle();
7641   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7642   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7643   Style.BraceWrapping.AfterFunction = true;
7644   Style.BraceWrapping.SplitEmptyFunction = false;
7645   Style.ColumnLimit = 40;
7646 
7647   verifyFormat("int f()\n"
7648                "{}",
7649                Style);
7650   verifyFormat("int f()\n"
7651                "{\n"
7652                "  return 42;\n"
7653                "}",
7654                Style);
7655   verifyFormat("int f()\n"
7656                "{\n"
7657                "  // some comment\n"
7658                "}",
7659                Style);
7660 
7661   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7662   verifyFormat("int f() {}", Style);
7663   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7664                "{}",
7665                Style);
7666   verifyFormat("int f()\n"
7667                "{\n"
7668                "  return 0;\n"
7669                "}",
7670                Style);
7671 
7672   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7673   verifyFormat("class Foo {\n"
7674                "  int f() {}\n"
7675                "};\n",
7676                Style);
7677   verifyFormat("class Foo {\n"
7678                "  int f() { return 0; }\n"
7679                "};\n",
7680                Style);
7681   verifyFormat("class Foo {\n"
7682                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7683                "  {}\n"
7684                "};\n",
7685                Style);
7686   verifyFormat("class Foo {\n"
7687                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7688                "  {\n"
7689                "    return 0;\n"
7690                "  }\n"
7691                "};\n",
7692                Style);
7693 
7694   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7695   verifyFormat("int f() {}", Style);
7696   verifyFormat("int f() { return 0; }", Style);
7697   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7698                "{}",
7699                Style);
7700   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7701                "{\n"
7702                "  return 0;\n"
7703                "}",
7704                Style);
7705 }
7706 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7707   FormatStyle Style = getLLVMStyle();
7708   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7709   verifyFormat("#ifdef A\n"
7710                "int f() {}\n"
7711                "#else\n"
7712                "int g() {}\n"
7713                "#endif",
7714                Style);
7715 }
7716 
7717 TEST_F(FormatTest, SplitEmptyClass) {
7718   FormatStyle Style = getLLVMStyle();
7719   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7720   Style.BraceWrapping.AfterClass = true;
7721   Style.BraceWrapping.SplitEmptyRecord = false;
7722 
7723   verifyFormat("class Foo\n"
7724                "{};",
7725                Style);
7726   verifyFormat("/* something */ class Foo\n"
7727                "{};",
7728                Style);
7729   verifyFormat("template <typename X> class Foo\n"
7730                "{};",
7731                Style);
7732   verifyFormat("class Foo\n"
7733                "{\n"
7734                "  Foo();\n"
7735                "};",
7736                Style);
7737   verifyFormat("typedef class Foo\n"
7738                "{\n"
7739                "} Foo_t;",
7740                Style);
7741 }
7742 
7743 TEST_F(FormatTest, SplitEmptyStruct) {
7744   FormatStyle Style = getLLVMStyle();
7745   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7746   Style.BraceWrapping.AfterStruct = true;
7747   Style.BraceWrapping.SplitEmptyRecord = false;
7748 
7749   verifyFormat("struct Foo\n"
7750                "{};",
7751                Style);
7752   verifyFormat("/* something */ struct Foo\n"
7753                "{};",
7754                Style);
7755   verifyFormat("template <typename X> struct Foo\n"
7756                "{};",
7757                Style);
7758   verifyFormat("struct Foo\n"
7759                "{\n"
7760                "  Foo();\n"
7761                "};",
7762                Style);
7763   verifyFormat("typedef struct Foo\n"
7764                "{\n"
7765                "} Foo_t;",
7766                Style);
7767   //typedef struct Bar {} Bar_t;
7768 }
7769 
7770 TEST_F(FormatTest, SplitEmptyUnion) {
7771   FormatStyle Style = getLLVMStyle();
7772   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7773   Style.BraceWrapping.AfterUnion = true;
7774   Style.BraceWrapping.SplitEmptyRecord = false;
7775 
7776   verifyFormat("union Foo\n"
7777                "{};",
7778                Style);
7779   verifyFormat("/* something */ union Foo\n"
7780                "{};",
7781                Style);
7782   verifyFormat("union Foo\n"
7783                "{\n"
7784                "  A,\n"
7785                "};",
7786                Style);
7787   verifyFormat("typedef union Foo\n"
7788                "{\n"
7789                "} Foo_t;",
7790                Style);
7791 }
7792 
7793 TEST_F(FormatTest, SplitEmptyNamespace) {
7794   FormatStyle Style = getLLVMStyle();
7795   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7796   Style.BraceWrapping.AfterNamespace = true;
7797   Style.BraceWrapping.SplitEmptyNamespace = false;
7798 
7799   verifyFormat("namespace Foo\n"
7800                "{};",
7801                Style);
7802   verifyFormat("/* something */ namespace Foo\n"
7803                "{};",
7804                Style);
7805   verifyFormat("inline namespace Foo\n"
7806                "{};",
7807                Style);
7808   verifyFormat("/* something */ inline namespace Foo\n"
7809                "{};",
7810                Style);
7811   verifyFormat("export namespace Foo\n"
7812                "{};",
7813                Style);
7814   verifyFormat("namespace Foo\n"
7815                "{\n"
7816                "void Bar();\n"
7817                "};",
7818                Style);
7819 }
7820 
7821 TEST_F(FormatTest, NeverMergeShortRecords) {
7822   FormatStyle Style = getLLVMStyle();
7823 
7824   verifyFormat("class Foo {\n"
7825                "  Foo();\n"
7826                "};",
7827                Style);
7828   verifyFormat("typedef class Foo {\n"
7829                "  Foo();\n"
7830                "} Foo_t;",
7831                Style);
7832   verifyFormat("struct Foo {\n"
7833                "  Foo();\n"
7834                "};",
7835                Style);
7836   verifyFormat("typedef struct Foo {\n"
7837                "  Foo();\n"
7838                "} Foo_t;",
7839                Style);
7840   verifyFormat("union Foo {\n"
7841                "  A,\n"
7842                "};",
7843                Style);
7844   verifyFormat("typedef union Foo {\n"
7845                "  A,\n"
7846                "} Foo_t;",
7847                Style);
7848   verifyFormat("namespace Foo {\n"
7849                "void Bar();\n"
7850                "};",
7851                Style);
7852 
7853   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7854   Style.BraceWrapping.AfterClass = true;
7855   Style.BraceWrapping.AfterStruct = true;
7856   Style.BraceWrapping.AfterUnion = true;
7857   Style.BraceWrapping.AfterNamespace = true;
7858   verifyFormat("class Foo\n"
7859                "{\n"
7860                "  Foo();\n"
7861                "};",
7862                Style);
7863   verifyFormat("typedef class Foo\n"
7864                "{\n"
7865                "  Foo();\n"
7866                "} Foo_t;",
7867                Style);
7868   verifyFormat("struct Foo\n"
7869                "{\n"
7870                "  Foo();\n"
7871                "};",
7872                Style);
7873   verifyFormat("typedef struct Foo\n"
7874                "{\n"
7875                "  Foo();\n"
7876                "} Foo_t;",
7877                Style);
7878   verifyFormat("union Foo\n"
7879                "{\n"
7880                "  A,\n"
7881                "};",
7882                Style);
7883   verifyFormat("typedef union Foo\n"
7884                "{\n"
7885                "  A,\n"
7886                "} Foo_t;",
7887                Style);
7888   verifyFormat("namespace Foo\n"
7889                "{\n"
7890                "void Bar();\n"
7891                "};",
7892                Style);
7893 }
7894 
7895 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7896   // Elaborate type variable declarations.
7897   verifyFormat("struct foo a = {bar};\nint n;");
7898   verifyFormat("class foo a = {bar};\nint n;");
7899   verifyFormat("union foo a = {bar};\nint n;");
7900 
7901   // Elaborate types inside function definitions.
7902   verifyFormat("struct foo f() {}\nint n;");
7903   verifyFormat("class foo f() {}\nint n;");
7904   verifyFormat("union foo f() {}\nint n;");
7905 
7906   // Templates.
7907   verifyFormat("template <class X> void f() {}\nint n;");
7908   verifyFormat("template <struct X> void f() {}\nint n;");
7909   verifyFormat("template <union X> void f() {}\nint n;");
7910 
7911   // Actual definitions...
7912   verifyFormat("struct {\n} n;");
7913   verifyFormat(
7914       "template <template <class T, class Y>, class Z> class X {\n} n;");
7915   verifyFormat("union Z {\n  int n;\n} x;");
7916   verifyFormat("class MACRO Z {\n} n;");
7917   verifyFormat("class MACRO(X) Z {\n} n;");
7918   verifyFormat("class __attribute__(X) Z {\n} n;");
7919   verifyFormat("class __declspec(X) Z {\n} n;");
7920   verifyFormat("class A##B##C {\n} n;");
7921   verifyFormat("class alignas(16) Z {\n} n;");
7922   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7923   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7924 
7925   // Redefinition from nested context:
7926   verifyFormat("class A::B::C {\n} n;");
7927 
7928   // Template definitions.
7929   verifyFormat(
7930       "template <typename F>\n"
7931       "Matcher(const Matcher<F> &Other,\n"
7932       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7933       "                             !is_same<F, T>::value>::type * = 0)\n"
7934       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7935 
7936   // FIXME: This is still incorrectly handled at the formatter side.
7937   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7938   verifyFormat("int i = SomeFunction(a<b, a> b);");
7939 
7940   // FIXME:
7941   // This now gets parsed incorrectly as class definition.
7942   // verifyFormat("class A<int> f() {\n}\nint n;");
7943 
7944   // Elaborate types where incorrectly parsing the structural element would
7945   // break the indent.
7946   verifyFormat("if (true)\n"
7947                "  class X x;\n"
7948                "else\n"
7949                "  f();\n");
7950 
7951   // This is simply incomplete. Formatting is not important, but must not crash.
7952   verifyFormat("class A:");
7953 }
7954 
7955 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7956   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7957             format("#error Leave     all         white!!!!! space* alone!\n"));
7958   EXPECT_EQ(
7959       "#warning Leave     all         white!!!!! space* alone!\n",
7960       format("#warning Leave     all         white!!!!! space* alone!\n"));
7961   EXPECT_EQ("#error 1", format("  #  error   1"));
7962   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7963 }
7964 
7965 TEST_F(FormatTest, FormatHashIfExpressions) {
7966   verifyFormat("#if AAAA && BBBB");
7967   verifyFormat("#if (AAAA && BBBB)");
7968   verifyFormat("#elif (AAAA && BBBB)");
7969   // FIXME: Come up with a better indentation for #elif.
7970   verifyFormat(
7971       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7972       "    defined(BBBBBBBB)\n"
7973       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7974       "    defined(BBBBBBBB)\n"
7975       "#endif",
7976       getLLVMStyleWithColumns(65));
7977 }
7978 
7979 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7980   FormatStyle AllowsMergedIf = getGoogleStyle();
7981   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7982   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7983   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7984   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7985   EXPECT_EQ("if (true) return 42;",
7986             format("if (true)\nreturn 42;", AllowsMergedIf));
7987   FormatStyle ShortMergedIf = AllowsMergedIf;
7988   ShortMergedIf.ColumnLimit = 25;
7989   verifyFormat("#define A \\\n"
7990                "  if (true) return 42;",
7991                ShortMergedIf);
7992   verifyFormat("#define A \\\n"
7993                "  f();    \\\n"
7994                "  if (true)\n"
7995                "#define B",
7996                ShortMergedIf);
7997   verifyFormat("#define A \\\n"
7998                "  f();    \\\n"
7999                "  if (true)\n"
8000                "g();",
8001                ShortMergedIf);
8002   verifyFormat("{\n"
8003                "#ifdef A\n"
8004                "  // Comment\n"
8005                "  if (true) continue;\n"
8006                "#endif\n"
8007                "  // Comment\n"
8008                "  if (true) continue;\n"
8009                "}",
8010                ShortMergedIf);
8011   ShortMergedIf.ColumnLimit = 33;
8012   verifyFormat("#define A \\\n"
8013                "  if constexpr (true) return 42;",
8014                ShortMergedIf);
8015   ShortMergedIf.ColumnLimit = 29;
8016   verifyFormat("#define A                   \\\n"
8017                "  if (aaaaaaaaaa) return 1; \\\n"
8018                "  return 2;",
8019                ShortMergedIf);
8020   ShortMergedIf.ColumnLimit = 28;
8021   verifyFormat("#define A         \\\n"
8022                "  if (aaaaaaaaaa) \\\n"
8023                "    return 1;     \\\n"
8024                "  return 2;",
8025                ShortMergedIf);
8026   verifyFormat("#define A                \\\n"
8027                "  if constexpr (aaaaaaa) \\\n"
8028                "    return 1;            \\\n"
8029                "  return 2;",
8030                ShortMergedIf);
8031 }
8032 
8033 TEST_F(FormatTest, FormatStarDependingOnContext) {
8034   verifyFormat("void f(int *a);");
8035   verifyFormat("void f() { f(fint * b); }");
8036   verifyFormat("class A {\n  void f(int *a);\n};");
8037   verifyFormat("class A {\n  int *a;\n};");
8038   verifyFormat("namespace a {\n"
8039                "namespace b {\n"
8040                "class A {\n"
8041                "  void f() {}\n"
8042                "  int *a;\n"
8043                "};\n"
8044                "} // namespace b\n"
8045                "} // namespace a");
8046 }
8047 
8048 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
8049   verifyFormat("while");
8050   verifyFormat("operator");
8051 }
8052 
8053 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
8054   // This code would be painfully slow to format if we didn't skip it.
8055   std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
8056                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8057                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8058                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8059                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
8060                    "A(1, 1)\n"
8061                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
8062                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8063                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8064                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8065                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8066                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8067                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8068                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8069                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
8070                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
8071   // Deeply nested part is untouched, rest is formatted.
8072   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
8073             format(std::string("int    i;\n") + Code + "int    j;\n",
8074                    getLLVMStyle(), SC_ExpectIncomplete));
8075 }
8076 
8077 //===----------------------------------------------------------------------===//
8078 // Objective-C tests.
8079 //===----------------------------------------------------------------------===//
8080 
8081 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
8082   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
8083   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
8084             format("-(NSUInteger)indexOfObject:(id)anObject;"));
8085   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
8086   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
8087   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
8088             format("-(NSInteger)Method3:(id)anObject;"));
8089   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
8090             format("-(NSInteger)Method4:(id)anObject;"));
8091   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
8092             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
8093   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
8094             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
8095   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8096             "forAllCells:(BOOL)flag;",
8097             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
8098                    "forAllCells:(BOOL)flag;"));
8099 
8100   // Very long objectiveC method declaration.
8101   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
8102                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
8103   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
8104                "                    inRange:(NSRange)range\n"
8105                "                   outRange:(NSRange)out_range\n"
8106                "                  outRange1:(NSRange)out_range1\n"
8107                "                  outRange2:(NSRange)out_range2\n"
8108                "                  outRange3:(NSRange)out_range3\n"
8109                "                  outRange4:(NSRange)out_range4\n"
8110                "                  outRange5:(NSRange)out_range5\n"
8111                "                  outRange6:(NSRange)out_range6\n"
8112                "                  outRange7:(NSRange)out_range7\n"
8113                "                  outRange8:(NSRange)out_range8\n"
8114                "                  outRange9:(NSRange)out_range9;");
8115 
8116   // When the function name has to be wrapped.
8117   FormatStyle Style = getLLVMStyle();
8118   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
8119   // and always indents instead.
8120   Style.IndentWrappedFunctionNames = false;
8121   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8122                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
8123                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
8124                "}",
8125                Style);
8126   Style.IndentWrappedFunctionNames = true;
8127   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
8128                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
8129                "               anotherName:(NSString)dddddddddddddd {\n"
8130                "}",
8131                Style);
8132 
8133   verifyFormat("- (int)sum:(vector<int>)numbers;");
8134   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
8135   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
8136   // protocol lists (but not for template classes):
8137   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
8138 
8139   verifyFormat("- (int (*)())foo:(int (*)())f;");
8140   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
8141 
8142   // If there's no return type (very rare in practice!), LLVM and Google style
8143   // agree.
8144   verifyFormat("- foo;");
8145   verifyFormat("- foo:(int)f;");
8146   verifyGoogleFormat("- foo:(int)foo;");
8147 }
8148 
8149 
8150 TEST_F(FormatTest, BreaksStringLiterals) {
8151   EXPECT_EQ("\"some text \"\n"
8152             "\"other\";",
8153             format("\"some text other\";", getLLVMStyleWithColumns(12)));
8154   EXPECT_EQ("\"some text \"\n"
8155             "\"other\";",
8156             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
8157   EXPECT_EQ(
8158       "#define A  \\\n"
8159       "  \"some \"  \\\n"
8160       "  \"text \"  \\\n"
8161       "  \"other\";",
8162       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
8163   EXPECT_EQ(
8164       "#define A  \\\n"
8165       "  \"so \"    \\\n"
8166       "  \"text \"  \\\n"
8167       "  \"other\";",
8168       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
8169 
8170   EXPECT_EQ("\"some text\"",
8171             format("\"some text\"", getLLVMStyleWithColumns(1)));
8172   EXPECT_EQ("\"some text\"",
8173             format("\"some text\"", getLLVMStyleWithColumns(11)));
8174   EXPECT_EQ("\"some \"\n"
8175             "\"text\"",
8176             format("\"some text\"", getLLVMStyleWithColumns(10)));
8177   EXPECT_EQ("\"some \"\n"
8178             "\"text\"",
8179             format("\"some text\"", getLLVMStyleWithColumns(7)));
8180   EXPECT_EQ("\"some\"\n"
8181             "\" tex\"\n"
8182             "\"t\"",
8183             format("\"some text\"", getLLVMStyleWithColumns(6)));
8184   EXPECT_EQ("\"some\"\n"
8185             "\" tex\"\n"
8186             "\" and\"",
8187             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
8188   EXPECT_EQ("\"some\"\n"
8189             "\"/tex\"\n"
8190             "\"/and\"",
8191             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
8192 
8193   EXPECT_EQ("variable =\n"
8194             "    \"long string \"\n"
8195             "    \"literal\";",
8196             format("variable = \"long string literal\";",
8197                    getLLVMStyleWithColumns(20)));
8198 
8199   EXPECT_EQ("variable = f(\n"
8200             "    \"long string \"\n"
8201             "    \"literal\",\n"
8202             "    short,\n"
8203             "    loooooooooooooooooooong);",
8204             format("variable = f(\"long string literal\", short, "
8205                    "loooooooooooooooooooong);",
8206                    getLLVMStyleWithColumns(20)));
8207 
8208   EXPECT_EQ(
8209       "f(g(\"long string \"\n"
8210       "    \"literal\"),\n"
8211       "  b);",
8212       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
8213   EXPECT_EQ("f(g(\"long string \"\n"
8214             "    \"literal\",\n"
8215             "    a),\n"
8216             "  b);",
8217             format("f(g(\"long string literal\", a), b);",
8218                    getLLVMStyleWithColumns(20)));
8219   EXPECT_EQ(
8220       "f(\"one two\".split(\n"
8221       "    variable));",
8222       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
8223   EXPECT_EQ("f(\"one two three four five six \"\n"
8224             "  \"seven\".split(\n"
8225             "      really_looooong_variable));",
8226             format("f(\"one two three four five six seven\"."
8227                    "split(really_looooong_variable));",
8228                    getLLVMStyleWithColumns(33)));
8229 
8230   EXPECT_EQ("f(\"some \"\n"
8231             "  \"text\",\n"
8232             "  other);",
8233             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8234 
8235   // Only break as a last resort.
8236   verifyFormat(
8237       "aaaaaaaaaaaaaaaaaaaa(\n"
8238       "    aaaaaaaaaaaaaaaaaaaa,\n"
8239       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8240 
8241   EXPECT_EQ("\"splitmea\"\n"
8242             "\"trandomp\"\n"
8243             "\"oint\"",
8244             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8245 
8246   EXPECT_EQ("\"split/\"\n"
8247             "\"pathat/\"\n"
8248             "\"slashes\"",
8249             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8250 
8251   EXPECT_EQ("\"split/\"\n"
8252             "\"pathat/\"\n"
8253             "\"slashes\"",
8254             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8255   EXPECT_EQ("\"split at \"\n"
8256             "\"spaces/at/\"\n"
8257             "\"slashes.at.any$\"\n"
8258             "\"non-alphanumeric%\"\n"
8259             "\"1111111111characte\"\n"
8260             "\"rs\"",
8261             format("\"split at "
8262                    "spaces/at/"
8263                    "slashes.at."
8264                    "any$non-"
8265                    "alphanumeric%"
8266                    "1111111111characte"
8267                    "rs\"",
8268                    getLLVMStyleWithColumns(20)));
8269 
8270   // Verify that splitting the strings understands
8271   // Style::AlwaysBreakBeforeMultilineStrings.
8272   EXPECT_EQ(
8273       "aaaaaaaaaaaa(\n"
8274       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8275       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8276       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8277              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8278              "aaaaaaaaaaaaaaaaaaaaaa\");",
8279              getGoogleStyle()));
8280   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8281             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8282             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8283                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8284                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8285                    getGoogleStyle()));
8286   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8287             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8288             format("llvm::outs() << "
8289                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8290                    "aaaaaaaaaaaaaaaaaaa\";"));
8291   EXPECT_EQ("ffff(\n"
8292             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8293             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8294             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8295                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8296                    getGoogleStyle()));
8297 
8298   FormatStyle Style = getLLVMStyleWithColumns(12);
8299   Style.BreakStringLiterals = false;
8300   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8301 
8302   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8303   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8304   EXPECT_EQ("#define A \\\n"
8305             "  \"some \" \\\n"
8306             "  \"text \" \\\n"
8307             "  \"other\";",
8308             format("#define A \"some text other\";", AlignLeft));
8309 }
8310 
8311 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
8312   EXPECT_EQ("C a = \"some more \"\n"
8313             "      \"text\";",
8314             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
8315 }
8316 
8317 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8318   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8319   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8320   EXPECT_EQ("int i = a(b());",
8321             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8322 }
8323 
8324 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8325   EXPECT_EQ(
8326       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8327       "(\n"
8328       "    \"x\t\");",
8329       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8330              "aaaaaaa("
8331              "\"x\t\");"));
8332 }
8333 
8334 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8335   EXPECT_EQ(
8336       "u8\"utf8 string \"\n"
8337       "u8\"literal\";",
8338       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8339   EXPECT_EQ(
8340       "u\"utf16 string \"\n"
8341       "u\"literal\";",
8342       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8343   EXPECT_EQ(
8344       "U\"utf32 string \"\n"
8345       "U\"literal\";",
8346       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8347   EXPECT_EQ("L\"wide string \"\n"
8348             "L\"literal\";",
8349             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8350   EXPECT_EQ("@\"NSString \"\n"
8351             "@\"literal\";",
8352             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8353   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
8354 
8355   // This input makes clang-format try to split the incomplete unicode escape
8356   // sequence, which used to lead to a crasher.
8357   verifyNoCrash(
8358       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8359       getLLVMStyleWithColumns(60));
8360 }
8361 
8362 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8363   FormatStyle Style = getGoogleStyleWithColumns(15);
8364   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8365   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8366   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8367   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8368   EXPECT_EQ("u8R\"x(raw literal)x\";",
8369             format("u8R\"x(raw literal)x\";", Style));
8370 }
8371 
8372 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8373   FormatStyle Style = getLLVMStyleWithColumns(20);
8374   EXPECT_EQ(
8375       "_T(\"aaaaaaaaaaaaaa\")\n"
8376       "_T(\"aaaaaaaaaaaaaa\")\n"
8377       "_T(\"aaaaaaaaaaaa\")",
8378       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8379   EXPECT_EQ("f(x,\n"
8380             "  _T(\"aaaaaaaaaaaa\")\n"
8381             "  _T(\"aaa\"),\n"
8382             "  z);",
8383             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8384 
8385   // FIXME: Handle embedded spaces in one iteration.
8386   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8387   //            "_T(\"aaaaaaaaaaaaa\")\n"
8388   //            "_T(\"aaaaaaaaaaaaa\")\n"
8389   //            "_T(\"a\")",
8390   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8391   //                   getLLVMStyleWithColumns(20)));
8392   EXPECT_EQ(
8393       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8394       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8395   EXPECT_EQ("f(\n"
8396             "#if !TEST\n"
8397             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8398             "#endif\n"
8399             ");",
8400             format("f(\n"
8401                    "#if !TEST\n"
8402                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8403                    "#endif\n"
8404                    ");"));
8405   EXPECT_EQ("f(\n"
8406             "\n"
8407             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8408             format("f(\n"
8409                    "\n"
8410                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8411 }
8412 
8413 TEST_F(FormatTest, BreaksStringLiteralOperands) {
8414   // In a function call with two operands, the second can be broken with no line
8415   // break before it.
8416   EXPECT_EQ("func(a, \"long long \"\n"
8417             "        \"long long\");",
8418             format("func(a, \"long long long long\");",
8419                    getLLVMStyleWithColumns(24)));
8420   // In a function call with three operands, the second must be broken with a
8421   // line break before it.
8422   EXPECT_EQ("func(a,\n"
8423             "     \"long long long \"\n"
8424             "     \"long\",\n"
8425             "     c);",
8426             format("func(a, \"long long long long\", c);",
8427                    getLLVMStyleWithColumns(24)));
8428   // In a function call with three operands, the third must be broken with a
8429   // line break before it.
8430   EXPECT_EQ("func(a, b,\n"
8431             "     \"long long long \"\n"
8432             "     \"long\");",
8433             format("func(a, b, \"long long long long\");",
8434                    getLLVMStyleWithColumns(24)));
8435   // In a function call with three operands, both the second and the third must
8436   // be broken with a line break before them.
8437   EXPECT_EQ("func(a,\n"
8438             "     \"long long long \"\n"
8439             "     \"long\",\n"
8440             "     \"long long long \"\n"
8441             "     \"long\");",
8442             format("func(a, \"long long long long\", \"long long long long\");",
8443                    getLLVMStyleWithColumns(24)));
8444   // In a chain of << with two operands, the second can be broken with no line
8445   // break before it.
8446   EXPECT_EQ("a << \"line line \"\n"
8447             "     \"line\";",
8448             format("a << \"line line line\";",
8449                    getLLVMStyleWithColumns(20)));
8450   // In a chain of << with three operands, the second can be broken with no line
8451   // break before it.
8452   EXPECT_EQ("abcde << \"line \"\n"
8453             "         \"line line\"\n"
8454             "      << c;",
8455             format("abcde << \"line line line\" << c;",
8456                    getLLVMStyleWithColumns(20)));
8457   // In a chain of << with three operands, the third must be broken with a line
8458   // break before it.
8459   EXPECT_EQ("a << b\n"
8460             "  << \"line line \"\n"
8461             "     \"line\";",
8462             format("a << b << \"line line line\";",
8463                    getLLVMStyleWithColumns(20)));
8464   // In a chain of << with three operands, the second can be broken with no line
8465   // break before it and the third must be broken with a line break before it.
8466   EXPECT_EQ("abcd << \"line line \"\n"
8467             "        \"line\"\n"
8468             "     << \"line line \"\n"
8469             "        \"line\";",
8470             format("abcd << \"line line line\" << \"line line line\";",
8471                    getLLVMStyleWithColumns(20)));
8472   // In a chain of binary operators with two operands, the second can be broken
8473   // with no line break before it.
8474   EXPECT_EQ("abcd + \"line line \"\n"
8475             "       \"line line\";",
8476             format("abcd + \"line line line line\";",
8477                    getLLVMStyleWithColumns(20)));
8478   // In a chain of binary operators with three operands, the second must be
8479   // broken with a line break before it.
8480   EXPECT_EQ("abcd +\n"
8481             "    \"line line \"\n"
8482             "    \"line line\" +\n"
8483             "    e;",
8484             format("abcd + \"line line line line\" + e;",
8485                    getLLVMStyleWithColumns(20)));
8486   // In a function call with two operands, with AlignAfterOpenBracket enabled,
8487   // the first must be broken with a line break before it.
8488   FormatStyle Style = getLLVMStyleWithColumns(25);
8489   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8490   EXPECT_EQ("someFunction(\n"
8491             "    \"long long long \"\n"
8492             "    \"long\",\n"
8493             "    a);",
8494             format("someFunction(\"long long long long\", a);", Style));
8495 }
8496 
8497 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8498   EXPECT_EQ(
8499       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8500       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8501       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8502       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8503              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8504              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8505 }
8506 
8507 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8508   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8509             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8510   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8511             "multiline raw string literal xxxxxxxxxxxxxx\n"
8512             ")x\",\n"
8513             "              a),\n"
8514             "            b);",
8515             format("fffffffffff(g(R\"x(\n"
8516                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8517                    ")x\", a), b);",
8518                    getGoogleStyleWithColumns(20)));
8519   EXPECT_EQ("fffffffffff(\n"
8520             "    g(R\"x(qqq\n"
8521             "multiline raw string literal xxxxxxxxxxxxxx\n"
8522             ")x\",\n"
8523             "      a),\n"
8524             "    b);",
8525             format("fffffffffff(g(R\"x(qqq\n"
8526                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8527                    ")x\", a), b);",
8528                    getGoogleStyleWithColumns(20)));
8529 
8530   EXPECT_EQ("fffffffffff(R\"x(\n"
8531             "multiline raw string literal xxxxxxxxxxxxxx\n"
8532             ")x\");",
8533             format("fffffffffff(R\"x(\n"
8534                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8535                    ")x\");",
8536                    getGoogleStyleWithColumns(20)));
8537   EXPECT_EQ("fffffffffff(R\"x(\n"
8538             "multiline raw string literal xxxxxxxxxxxxxx\n"
8539             ")x\" + bbbbbb);",
8540             format("fffffffffff(R\"x(\n"
8541                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8542                    ")x\" +   bbbbbb);",
8543                    getGoogleStyleWithColumns(20)));
8544   EXPECT_EQ("fffffffffff(\n"
8545             "    R\"x(\n"
8546             "multiline raw string literal xxxxxxxxxxxxxx\n"
8547             ")x\" +\n"
8548             "    bbbbbb);",
8549             format("fffffffffff(\n"
8550                    " R\"x(\n"
8551                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8552                    ")x\" + bbbbbb);",
8553                    getGoogleStyleWithColumns(20)));
8554   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
8555             format("fffffffffff(\n"
8556                    " R\"(single line raw string)\" + bbbbbb);"));
8557 }
8558 
8559 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8560   verifyFormat("string a = \"unterminated;");
8561   EXPECT_EQ("function(\"unterminated,\n"
8562             "         OtherParameter);",
8563             format("function(  \"unterminated,\n"
8564                    "    OtherParameter);"));
8565 }
8566 
8567 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8568   FormatStyle Style = getLLVMStyle();
8569   Style.Standard = FormatStyle::LS_Cpp03;
8570   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8571             format("#define x(_a) printf(\"foo\"_a);", Style));
8572 }
8573 
8574 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8575 
8576 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8577   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8578             "             \"ddeeefff\");",
8579             format("someFunction(\"aaabbbcccdddeeefff\");",
8580                    getLLVMStyleWithColumns(25)));
8581   EXPECT_EQ("someFunction1234567890(\n"
8582             "    \"aaabbbcccdddeeefff\");",
8583             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8584                    getLLVMStyleWithColumns(26)));
8585   EXPECT_EQ("someFunction1234567890(\n"
8586             "    \"aaabbbcccdddeeeff\"\n"
8587             "    \"f\");",
8588             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8589                    getLLVMStyleWithColumns(25)));
8590   EXPECT_EQ("someFunction1234567890(\n"
8591             "    \"aaabbbcccdddeeeff\"\n"
8592             "    \"f\");",
8593             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8594                    getLLVMStyleWithColumns(24)));
8595   EXPECT_EQ("someFunction(\n"
8596             "    \"aaabbbcc ddde \"\n"
8597             "    \"efff\");",
8598             format("someFunction(\"aaabbbcc ddde efff\");",
8599                    getLLVMStyleWithColumns(25)));
8600   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8601             "             \"ddeeefff\");",
8602             format("someFunction(\"aaabbbccc ddeeefff\");",
8603                    getLLVMStyleWithColumns(25)));
8604   EXPECT_EQ("someFunction1234567890(\n"
8605             "    \"aaabb \"\n"
8606             "    \"cccdddeeefff\");",
8607             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8608                    getLLVMStyleWithColumns(25)));
8609   EXPECT_EQ("#define A          \\\n"
8610             "  string s =       \\\n"
8611             "      \"123456789\"  \\\n"
8612             "      \"0\";         \\\n"
8613             "  int i;",
8614             format("#define A string s = \"1234567890\"; int i;",
8615                    getLLVMStyleWithColumns(20)));
8616   EXPECT_EQ("someFunction(\n"
8617             "    \"aaabbbcc \"\n"
8618             "    \"dddeeefff\");",
8619             format("someFunction(\"aaabbbcc dddeeefff\");",
8620                    getLLVMStyleWithColumns(25)));
8621 }
8622 
8623 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8624   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8625   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8626   EXPECT_EQ("\"test\"\n"
8627             "\"\\n\"",
8628             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8629   EXPECT_EQ("\"tes\\\\\"\n"
8630             "\"n\"",
8631             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8632   EXPECT_EQ("\"\\\\\\\\\"\n"
8633             "\"\\n\"",
8634             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8635   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8636   EXPECT_EQ("\"\\uff01\"\n"
8637             "\"test\"",
8638             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8639   EXPECT_EQ("\"\\Uff01ff02\"",
8640             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8641   EXPECT_EQ("\"\\x000000000001\"\n"
8642             "\"next\"",
8643             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8644   EXPECT_EQ("\"\\x000000000001next\"",
8645             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8646   EXPECT_EQ("\"\\x000000000001\"",
8647             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8648   EXPECT_EQ("\"test\"\n"
8649             "\"\\000000\"\n"
8650             "\"000001\"",
8651             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8652   EXPECT_EQ("\"test\\000\"\n"
8653             "\"00000000\"\n"
8654             "\"1\"",
8655             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8656 }
8657 
8658 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8659   verifyFormat("void f() {\n"
8660                "  return g() {}\n"
8661                "  void h() {}");
8662   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8663                "g();\n"
8664                "}");
8665 }
8666 
8667 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8668   verifyFormat(
8669       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8670 }
8671 
8672 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8673   verifyFormat("class X {\n"
8674                "  void f() {\n"
8675                "  }\n"
8676                "};",
8677                getLLVMStyleWithColumns(12));
8678 }
8679 
8680 TEST_F(FormatTest, ConfigurableIndentWidth) {
8681   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8682   EightIndent.IndentWidth = 8;
8683   EightIndent.ContinuationIndentWidth = 8;
8684   verifyFormat("void f() {\n"
8685                "        someFunction();\n"
8686                "        if (true) {\n"
8687                "                f();\n"
8688                "        }\n"
8689                "}",
8690                EightIndent);
8691   verifyFormat("class X {\n"
8692                "        void f() {\n"
8693                "        }\n"
8694                "};",
8695                EightIndent);
8696   verifyFormat("int x[] = {\n"
8697                "        call(),\n"
8698                "        call()};",
8699                EightIndent);
8700 }
8701 
8702 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8703   verifyFormat("double\n"
8704                "f();",
8705                getLLVMStyleWithColumns(8));
8706 }
8707 
8708 TEST_F(FormatTest, ConfigurableUseOfTab) {
8709   FormatStyle Tab = getLLVMStyleWithColumns(42);
8710   Tab.IndentWidth = 8;
8711   Tab.UseTab = FormatStyle::UT_Always;
8712   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8713 
8714   EXPECT_EQ("if (aaaaaaaa && // q\n"
8715             "    bb)\t\t// w\n"
8716             "\t;",
8717             format("if (aaaaaaaa &&// q\n"
8718                    "bb)// w\n"
8719                    ";",
8720                    Tab));
8721   EXPECT_EQ("if (aaa && bbb) // w\n"
8722             "\t;",
8723             format("if(aaa&&bbb)// w\n"
8724                    ";",
8725                    Tab));
8726 
8727   verifyFormat("class X {\n"
8728                "\tvoid f() {\n"
8729                "\t\tsomeFunction(parameter1,\n"
8730                "\t\t\t     parameter2);\n"
8731                "\t}\n"
8732                "};",
8733                Tab);
8734   verifyFormat("#define A                        \\\n"
8735                "\tvoid f() {               \\\n"
8736                "\t\tsomeFunction(    \\\n"
8737                "\t\t    parameter1,  \\\n"
8738                "\t\t    parameter2); \\\n"
8739                "\t}",
8740                Tab);
8741 
8742   Tab.TabWidth = 4;
8743   Tab.IndentWidth = 8;
8744   verifyFormat("class TabWidth4Indent8 {\n"
8745                "\t\tvoid f() {\n"
8746                "\t\t\t\tsomeFunction(parameter1,\n"
8747                "\t\t\t\t\t\t\t parameter2);\n"
8748                "\t\t}\n"
8749                "};",
8750                Tab);
8751 
8752   Tab.TabWidth = 4;
8753   Tab.IndentWidth = 4;
8754   verifyFormat("class TabWidth4Indent4 {\n"
8755                "\tvoid f() {\n"
8756                "\t\tsomeFunction(parameter1,\n"
8757                "\t\t\t\t\t parameter2);\n"
8758                "\t}\n"
8759                "};",
8760                Tab);
8761 
8762   Tab.TabWidth = 8;
8763   Tab.IndentWidth = 4;
8764   verifyFormat("class TabWidth8Indent4 {\n"
8765                "    void f() {\n"
8766                "\tsomeFunction(parameter1,\n"
8767                "\t\t     parameter2);\n"
8768                "    }\n"
8769                "};",
8770                Tab);
8771 
8772   Tab.TabWidth = 8;
8773   Tab.IndentWidth = 8;
8774   EXPECT_EQ("/*\n"
8775             "\t      a\t\tcomment\n"
8776             "\t      in multiple lines\n"
8777             "       */",
8778             format("   /*\t \t \n"
8779                    " \t \t a\t\tcomment\t \t\n"
8780                    " \t \t in multiple lines\t\n"
8781                    " \t  */",
8782                    Tab));
8783 
8784   Tab.UseTab = FormatStyle::UT_ForIndentation;
8785   verifyFormat("{\n"
8786                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8787                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8788                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8789                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8790                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8791                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8792                "};",
8793                Tab);
8794   verifyFormat("enum AA {\n"
8795                "\ta1, // Force multiple lines\n"
8796                "\ta2,\n"
8797                "\ta3\n"
8798                "};",
8799                Tab);
8800   EXPECT_EQ("if (aaaaaaaa && // q\n"
8801             "    bb)         // w\n"
8802             "\t;",
8803             format("if (aaaaaaaa &&// q\n"
8804                    "bb)// w\n"
8805                    ";",
8806                    Tab));
8807   verifyFormat("class X {\n"
8808                "\tvoid f() {\n"
8809                "\t\tsomeFunction(parameter1,\n"
8810                "\t\t             parameter2);\n"
8811                "\t}\n"
8812                "};",
8813                Tab);
8814   verifyFormat("{\n"
8815                "\tQ(\n"
8816                "\t    {\n"
8817                "\t\t    int a;\n"
8818                "\t\t    someFunction(aaaaaaaa,\n"
8819                "\t\t                 bbbbbbb);\n"
8820                "\t    },\n"
8821                "\t    p);\n"
8822                "}",
8823                Tab);
8824   EXPECT_EQ("{\n"
8825             "\t/* aaaa\n"
8826             "\t   bbbb */\n"
8827             "}",
8828             format("{\n"
8829                    "/* aaaa\n"
8830                    "   bbbb */\n"
8831                    "}",
8832                    Tab));
8833   EXPECT_EQ("{\n"
8834             "\t/*\n"
8835             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8836             "\t  bbbbbbbbbbbbb\n"
8837             "\t*/\n"
8838             "}",
8839             format("{\n"
8840                    "/*\n"
8841                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8842                    "*/\n"
8843                    "}",
8844                    Tab));
8845   EXPECT_EQ("{\n"
8846             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8847             "\t// bbbbbbbbbbbbb\n"
8848             "}",
8849             format("{\n"
8850                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8851                    "}",
8852                    Tab));
8853   EXPECT_EQ("{\n"
8854             "\t/*\n"
8855             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8856             "\t  bbbbbbbbbbbbb\n"
8857             "\t*/\n"
8858             "}",
8859             format("{\n"
8860                    "\t/*\n"
8861                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8862                    "\t*/\n"
8863                    "}",
8864                    Tab));
8865   EXPECT_EQ("{\n"
8866             "\t/*\n"
8867             "\n"
8868             "\t*/\n"
8869             "}",
8870             format("{\n"
8871                    "\t/*\n"
8872                    "\n"
8873                    "\t*/\n"
8874                    "}",
8875                    Tab));
8876   EXPECT_EQ("{\n"
8877             "\t/*\n"
8878             " asdf\n"
8879             "\t*/\n"
8880             "}",
8881             format("{\n"
8882                    "\t/*\n"
8883                    " asdf\n"
8884                    "\t*/\n"
8885                    "}",
8886                    Tab));
8887 
8888   Tab.UseTab = FormatStyle::UT_Never;
8889   EXPECT_EQ("/*\n"
8890             "              a\t\tcomment\n"
8891             "              in multiple lines\n"
8892             "       */",
8893             format("   /*\t \t \n"
8894                    " \t \t a\t\tcomment\t \t\n"
8895                    " \t \t in multiple lines\t\n"
8896                    " \t  */",
8897                    Tab));
8898   EXPECT_EQ("/* some\n"
8899             "   comment */",
8900             format(" \t \t /* some\n"
8901                    " \t \t    comment */",
8902                    Tab));
8903   EXPECT_EQ("int a; /* some\n"
8904             "   comment */",
8905             format(" \t \t int a; /* some\n"
8906                    " \t \t    comment */",
8907                    Tab));
8908 
8909   EXPECT_EQ("int a; /* some\n"
8910             "comment */",
8911             format(" \t \t int\ta; /* some\n"
8912                    " \t \t    comment */",
8913                    Tab));
8914   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8915             "    comment */",
8916             format(" \t \t f(\"\t\t\"); /* some\n"
8917                    " \t \t    comment */",
8918                    Tab));
8919   EXPECT_EQ("{\n"
8920             "  /*\n"
8921             "   * Comment\n"
8922             "   */\n"
8923             "  int i;\n"
8924             "}",
8925             format("{\n"
8926                    "\t/*\n"
8927                    "\t * Comment\n"
8928                    "\t */\n"
8929                    "\t int i;\n"
8930                    "}"));
8931 
8932   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8933   Tab.TabWidth = 8;
8934   Tab.IndentWidth = 8;
8935   EXPECT_EQ("if (aaaaaaaa && // q\n"
8936             "    bb)         // w\n"
8937             "\t;",
8938             format("if (aaaaaaaa &&// q\n"
8939                    "bb)// w\n"
8940                    ";",
8941                    Tab));
8942   EXPECT_EQ("if (aaa && bbb) // w\n"
8943             "\t;",
8944             format("if(aaa&&bbb)// w\n"
8945                    ";",
8946                    Tab));
8947   verifyFormat("class X {\n"
8948                "\tvoid f() {\n"
8949                "\t\tsomeFunction(parameter1,\n"
8950                "\t\t\t     parameter2);\n"
8951                "\t}\n"
8952                "};",
8953                Tab);
8954   verifyFormat("#define A                        \\\n"
8955                "\tvoid f() {               \\\n"
8956                "\t\tsomeFunction(    \\\n"
8957                "\t\t    parameter1,  \\\n"
8958                "\t\t    parameter2); \\\n"
8959                "\t}",
8960                Tab);
8961   Tab.TabWidth = 4;
8962   Tab.IndentWidth = 8;
8963   verifyFormat("class TabWidth4Indent8 {\n"
8964                "\t\tvoid f() {\n"
8965                "\t\t\t\tsomeFunction(parameter1,\n"
8966                "\t\t\t\t\t\t\t parameter2);\n"
8967                "\t\t}\n"
8968                "};",
8969                Tab);
8970   Tab.TabWidth = 4;
8971   Tab.IndentWidth = 4;
8972   verifyFormat("class TabWidth4Indent4 {\n"
8973                "\tvoid f() {\n"
8974                "\t\tsomeFunction(parameter1,\n"
8975                "\t\t\t\t\t parameter2);\n"
8976                "\t}\n"
8977                "};",
8978                Tab);
8979   Tab.TabWidth = 8;
8980   Tab.IndentWidth = 4;
8981   verifyFormat("class TabWidth8Indent4 {\n"
8982                "    void f() {\n"
8983                "\tsomeFunction(parameter1,\n"
8984                "\t\t     parameter2);\n"
8985                "    }\n"
8986                "};",
8987                Tab);
8988   Tab.TabWidth = 8;
8989   Tab.IndentWidth = 8;
8990   EXPECT_EQ("/*\n"
8991             "\t      a\t\tcomment\n"
8992             "\t      in multiple lines\n"
8993             "       */",
8994             format("   /*\t \t \n"
8995                    " \t \t a\t\tcomment\t \t\n"
8996                    " \t \t in multiple lines\t\n"
8997                    " \t  */",
8998                    Tab));
8999   verifyFormat("{\n"
9000                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9001                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9002                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9003                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9004                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9005                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
9006                "};",
9007                Tab);
9008   verifyFormat("enum AA {\n"
9009                "\ta1, // Force multiple lines\n"
9010                "\ta2,\n"
9011                "\ta3\n"
9012                "};",
9013                Tab);
9014   EXPECT_EQ("if (aaaaaaaa && // q\n"
9015             "    bb)         // w\n"
9016             "\t;",
9017             format("if (aaaaaaaa &&// q\n"
9018                    "bb)// w\n"
9019                    ";",
9020                    Tab));
9021   verifyFormat("class X {\n"
9022                "\tvoid f() {\n"
9023                "\t\tsomeFunction(parameter1,\n"
9024                "\t\t\t     parameter2);\n"
9025                "\t}\n"
9026                "};",
9027                Tab);
9028   verifyFormat("{\n"
9029                "\tQ(\n"
9030                "\t    {\n"
9031                "\t\t    int a;\n"
9032                "\t\t    someFunction(aaaaaaaa,\n"
9033                "\t\t\t\t bbbbbbb);\n"
9034                "\t    },\n"
9035                "\t    p);\n"
9036                "}",
9037                Tab);
9038   EXPECT_EQ("{\n"
9039             "\t/* aaaa\n"
9040             "\t   bbbb */\n"
9041             "}",
9042             format("{\n"
9043                    "/* aaaa\n"
9044                    "   bbbb */\n"
9045                    "}",
9046                    Tab));
9047   EXPECT_EQ("{\n"
9048             "\t/*\n"
9049             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9050             "\t  bbbbbbbbbbbbb\n"
9051             "\t*/\n"
9052             "}",
9053             format("{\n"
9054                    "/*\n"
9055                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9056                    "*/\n"
9057                    "}",
9058                    Tab));
9059   EXPECT_EQ("{\n"
9060             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9061             "\t// bbbbbbbbbbbbb\n"
9062             "}",
9063             format("{\n"
9064                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9065                    "}",
9066                    Tab));
9067   EXPECT_EQ("{\n"
9068             "\t/*\n"
9069             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9070             "\t  bbbbbbbbbbbbb\n"
9071             "\t*/\n"
9072             "}",
9073             format("{\n"
9074                    "\t/*\n"
9075                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
9076                    "\t*/\n"
9077                    "}",
9078                    Tab));
9079   EXPECT_EQ("{\n"
9080             "\t/*\n"
9081             "\n"
9082             "\t*/\n"
9083             "}",
9084             format("{\n"
9085                    "\t/*\n"
9086                    "\n"
9087                    "\t*/\n"
9088                    "}",
9089                    Tab));
9090   EXPECT_EQ("{\n"
9091             "\t/*\n"
9092             " asdf\n"
9093             "\t*/\n"
9094             "}",
9095             format("{\n"
9096                    "\t/*\n"
9097                    " asdf\n"
9098                    "\t*/\n"
9099                    "}",
9100                    Tab));
9101   EXPECT_EQ("/*\n"
9102             "\t      a\t\tcomment\n"
9103             "\t      in multiple lines\n"
9104             "       */",
9105             format("   /*\t \t \n"
9106                    " \t \t a\t\tcomment\t \t\n"
9107                    " \t \t in multiple lines\t\n"
9108                    " \t  */",
9109                    Tab));
9110   EXPECT_EQ("/* some\n"
9111             "   comment */",
9112             format(" \t \t /* some\n"
9113                    " \t \t    comment */",
9114                    Tab));
9115   EXPECT_EQ("int a; /* some\n"
9116             "   comment */",
9117             format(" \t \t int a; /* some\n"
9118                    " \t \t    comment */",
9119                    Tab));
9120   EXPECT_EQ("int a; /* some\n"
9121             "comment */",
9122             format(" \t \t int\ta; /* some\n"
9123                    " \t \t    comment */",
9124                    Tab));
9125   EXPECT_EQ("f(\"\t\t\"); /* some\n"
9126             "    comment */",
9127             format(" \t \t f(\"\t\t\"); /* some\n"
9128                    " \t \t    comment */",
9129                    Tab));
9130   EXPECT_EQ("{\n"
9131             "  /*\n"
9132             "   * Comment\n"
9133             "   */\n"
9134             "  int i;\n"
9135             "}",
9136             format("{\n"
9137                    "\t/*\n"
9138                    "\t * Comment\n"
9139                    "\t */\n"
9140                    "\t int i;\n"
9141                    "}"));
9142   Tab.AlignConsecutiveAssignments = true;
9143   Tab.AlignConsecutiveDeclarations = true;
9144   Tab.TabWidth = 4;
9145   Tab.IndentWidth = 4;
9146   verifyFormat("class Assign {\n"
9147                "\tvoid f() {\n"
9148                "\t\tint         x      = 123;\n"
9149                "\t\tint         random = 4;\n"
9150                "\t\tstd::string alphabet =\n"
9151                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
9152                "\t}\n"
9153                "};",
9154                Tab);
9155 }
9156 
9157 TEST_F(FormatTest, CalculatesOriginalColumn) {
9158   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9159             "q\"; /* some\n"
9160             "       comment */",
9161             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9162                    "q\"; /* some\n"
9163                    "       comment */",
9164                    getLLVMStyle()));
9165   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9166             "/* some\n"
9167             "   comment */",
9168             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
9169                    " /* some\n"
9170                    "    comment */",
9171                    getLLVMStyle()));
9172   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9173             "qqq\n"
9174             "/* some\n"
9175             "   comment */",
9176             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9177                    "qqq\n"
9178                    " /* some\n"
9179                    "    comment */",
9180                    getLLVMStyle()));
9181   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9182             "wwww; /* some\n"
9183             "         comment */",
9184             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
9185                    "wwww; /* some\n"
9186                    "         comment */",
9187                    getLLVMStyle()));
9188 }
9189 
9190 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
9191   FormatStyle NoSpace = getLLVMStyle();
9192   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
9193 
9194   verifyFormat("while(true)\n"
9195                "  continue;",
9196                NoSpace);
9197   verifyFormat("for(;;)\n"
9198                "  continue;",
9199                NoSpace);
9200   verifyFormat("if(true)\n"
9201                "  f();\n"
9202                "else if(true)\n"
9203                "  f();",
9204                NoSpace);
9205   verifyFormat("do {\n"
9206                "  do_something();\n"
9207                "} while(something());",
9208                NoSpace);
9209   verifyFormat("switch(x) {\n"
9210                "default:\n"
9211                "  break;\n"
9212                "}",
9213                NoSpace);
9214   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
9215   verifyFormat("size_t x = sizeof(x);", NoSpace);
9216   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
9217   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
9218   verifyFormat("alignas(128) char a[128];", NoSpace);
9219   verifyFormat("size_t x = alignof(MyType);", NoSpace);
9220   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
9221   verifyFormat("int f() throw(Deprecated);", NoSpace);
9222   verifyFormat("typedef void (*cb)(int);", NoSpace);
9223   verifyFormat("T A::operator()();", NoSpace);
9224   verifyFormat("X A::operator++(T);", NoSpace);
9225 
9226   FormatStyle Space = getLLVMStyle();
9227   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
9228 
9229   verifyFormat("int f ();", Space);
9230   verifyFormat("void f (int a, T b) {\n"
9231                "  while (true)\n"
9232                "    continue;\n"
9233                "}",
9234                Space);
9235   verifyFormat("if (true)\n"
9236                "  f ();\n"
9237                "else if (true)\n"
9238                "  f ();",
9239                Space);
9240   verifyFormat("do {\n"
9241                "  do_something ();\n"
9242                "} while (something ());",
9243                Space);
9244   verifyFormat("switch (x) {\n"
9245                "default:\n"
9246                "  break;\n"
9247                "}",
9248                Space);
9249   verifyFormat("A::A () : a (1) {}", Space);
9250   verifyFormat("void f () __attribute__ ((asdf));", Space);
9251   verifyFormat("*(&a + 1);\n"
9252                "&((&a)[1]);\n"
9253                "a[(b + c) * d];\n"
9254                "(((a + 1) * 2) + 3) * 4;",
9255                Space);
9256   verifyFormat("#define A(x) x", Space);
9257   verifyFormat("#define A (x) x", Space);
9258   verifyFormat("#if defined(x)\n"
9259                "#endif",
9260                Space);
9261   verifyFormat("auto i = std::make_unique<int> (5);", Space);
9262   verifyFormat("size_t x = sizeof (x);", Space);
9263   verifyFormat("auto f (int x) -> decltype (x);", Space);
9264   verifyFormat("int f (T x) noexcept (x.create ());", Space);
9265   verifyFormat("alignas (128) char a[128];", Space);
9266   verifyFormat("size_t x = alignof (MyType);", Space);
9267   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
9268   verifyFormat("int f () throw (Deprecated);", Space);
9269   verifyFormat("typedef void (*cb) (int);", Space);
9270   verifyFormat("T A::operator() ();", Space);
9271   verifyFormat("X A::operator++ (T);", Space);
9272 }
9273 
9274 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
9275   FormatStyle Spaces = getLLVMStyle();
9276 
9277   Spaces.SpacesInParentheses = true;
9278   verifyFormat("do_something( ::globalVar );", Spaces);
9279   verifyFormat("call( x, y, z );", Spaces);
9280   verifyFormat("call();", Spaces);
9281   verifyFormat("std::function<void( int, int )> callback;", Spaces);
9282   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
9283                Spaces);
9284   verifyFormat("while ( (bool)1 )\n"
9285                "  continue;",
9286                Spaces);
9287   verifyFormat("for ( ;; )\n"
9288                "  continue;",
9289                Spaces);
9290   verifyFormat("if ( true )\n"
9291                "  f();\n"
9292                "else if ( true )\n"
9293                "  f();",
9294                Spaces);
9295   verifyFormat("do {\n"
9296                "  do_something( (int)i );\n"
9297                "} while ( something() );",
9298                Spaces);
9299   verifyFormat("switch ( x ) {\n"
9300                "default:\n"
9301                "  break;\n"
9302                "}",
9303                Spaces);
9304 
9305   Spaces.SpacesInParentheses = false;
9306   Spaces.SpacesInCStyleCastParentheses = true;
9307   verifyFormat("Type *A = ( Type * )P;", Spaces);
9308   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
9309   verifyFormat("x = ( int32 )y;", Spaces);
9310   verifyFormat("int a = ( int )(2.0f);", Spaces);
9311   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
9312   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
9313   verifyFormat("#define x (( int )-1)", Spaces);
9314 
9315   // Run the first set of tests again with:
9316   Spaces.SpacesInParentheses = false;
9317   Spaces.SpaceInEmptyParentheses = true;
9318   Spaces.SpacesInCStyleCastParentheses = true;
9319   verifyFormat("call(x, y, z);", Spaces);
9320   verifyFormat("call( );", Spaces);
9321   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9322   verifyFormat("while (( bool )1)\n"
9323                "  continue;",
9324                Spaces);
9325   verifyFormat("for (;;)\n"
9326                "  continue;",
9327                Spaces);
9328   verifyFormat("if (true)\n"
9329                "  f( );\n"
9330                "else if (true)\n"
9331                "  f( );",
9332                Spaces);
9333   verifyFormat("do {\n"
9334                "  do_something(( int )i);\n"
9335                "} while (something( ));",
9336                Spaces);
9337   verifyFormat("switch (x) {\n"
9338                "default:\n"
9339                "  break;\n"
9340                "}",
9341                Spaces);
9342 
9343   // Run the first set of tests again with:
9344   Spaces.SpaceAfterCStyleCast = true;
9345   verifyFormat("call(x, y, z);", Spaces);
9346   verifyFormat("call( );", Spaces);
9347   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9348   verifyFormat("while (( bool ) 1)\n"
9349                "  continue;",
9350                Spaces);
9351   verifyFormat("for (;;)\n"
9352                "  continue;",
9353                Spaces);
9354   verifyFormat("if (true)\n"
9355                "  f( );\n"
9356                "else if (true)\n"
9357                "  f( );",
9358                Spaces);
9359   verifyFormat("do {\n"
9360                "  do_something(( int ) i);\n"
9361                "} while (something( ));",
9362                Spaces);
9363   verifyFormat("switch (x) {\n"
9364                "default:\n"
9365                "  break;\n"
9366                "}",
9367                Spaces);
9368 
9369   // Run subset of tests again with:
9370   Spaces.SpacesInCStyleCastParentheses = false;
9371   Spaces.SpaceAfterCStyleCast = true;
9372   verifyFormat("while ((bool) 1)\n"
9373                "  continue;",
9374                Spaces);
9375   verifyFormat("do {\n"
9376                "  do_something((int) i);\n"
9377                "} while (something( ));",
9378                Spaces);
9379 }
9380 
9381 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9382   verifyFormat("int a[5];");
9383   verifyFormat("a[3] += 42;");
9384 
9385   FormatStyle Spaces = getLLVMStyle();
9386   Spaces.SpacesInSquareBrackets = true;
9387   // Lambdas unchanged.
9388   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9389   verifyFormat("return [i, args...] {};", Spaces);
9390 
9391   // Not lambdas.
9392   verifyFormat("int a[ 5 ];", Spaces);
9393   verifyFormat("a[ 3 ] += 42;", Spaces);
9394   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9395   verifyFormat("double &operator[](int i) { return 0; }\n"
9396                "int i;",
9397                Spaces);
9398   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9399   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9400   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9401 }
9402 
9403 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9404   verifyFormat("int a = 5;");
9405   verifyFormat("a += 42;");
9406   verifyFormat("a or_eq 8;");
9407 
9408   FormatStyle Spaces = getLLVMStyle();
9409   Spaces.SpaceBeforeAssignmentOperators = false;
9410   verifyFormat("int a= 5;", Spaces);
9411   verifyFormat("a+= 42;", Spaces);
9412   verifyFormat("a or_eq 8;", Spaces);
9413 }
9414 
9415 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
9416   verifyFormat("class Foo : public Bar {};");
9417   verifyFormat("Foo::Foo() : foo(1) {}");
9418   verifyFormat("for (auto a : b) {\n}");
9419   verifyFormat("int x = a ? b : c;");
9420   verifyFormat("{\n"
9421                "label0:\n"
9422                "  int x = 0;\n"
9423                "}");
9424   verifyFormat("switch (x) {\n"
9425                "case 1:\n"
9426                "default:\n"
9427                "}");
9428 
9429   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
9430   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
9431   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
9432   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
9433   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
9434   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
9435   verifyFormat("{\n"
9436                "label1:\n"
9437                "  int x = 0;\n"
9438                "}",
9439                CtorInitializerStyle);
9440   verifyFormat("switch (x) {\n"
9441                "case 1:\n"
9442                "default:\n"
9443                "}",
9444                CtorInitializerStyle);
9445   CtorInitializerStyle.BreakConstructorInitializers =
9446       FormatStyle::BCIS_AfterColon;
9447   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
9448                "    aaaaaaaaaaaaaaaa(1),\n"
9449                "    bbbbbbbbbbbbbbbb(2) {}",
9450                CtorInitializerStyle);
9451   CtorInitializerStyle.BreakConstructorInitializers =
9452       FormatStyle::BCIS_BeforeComma;
9453   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9454                "    : aaaaaaaaaaaaaaaa(1)\n"
9455                "    , bbbbbbbbbbbbbbbb(2) {}",
9456                CtorInitializerStyle);
9457   CtorInitializerStyle.BreakConstructorInitializers =
9458       FormatStyle::BCIS_BeforeColon;
9459   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9460                "    : aaaaaaaaaaaaaaaa(1),\n"
9461                "      bbbbbbbbbbbbbbbb(2) {}",
9462                CtorInitializerStyle);
9463   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
9464   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
9465                ": aaaaaaaaaaaaaaaa(1),\n"
9466                "  bbbbbbbbbbbbbbbb(2) {}",
9467                CtorInitializerStyle);
9468 
9469   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
9470   InheritanceStyle.SpaceBeforeInheritanceColon = false;
9471   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
9472   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
9473   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
9474   verifyFormat("int x = a ? b : c;", InheritanceStyle);
9475   verifyFormat("{\n"
9476                "label2:\n"
9477                "  int x = 0;\n"
9478                "}",
9479                InheritanceStyle);
9480   verifyFormat("switch (x) {\n"
9481                "case 1:\n"
9482                "default:\n"
9483                "}",
9484                InheritanceStyle);
9485   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
9486   verifyFormat("class Foooooooooooooooooooooo:\n"
9487                "    public aaaaaaaaaaaaaaaaaa,\n"
9488                "    public bbbbbbbbbbbbbbbbbb {\n"
9489                "}",
9490                InheritanceStyle);
9491   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
9492   verifyFormat("class Foooooooooooooooooooooo\n"
9493                "    : public aaaaaaaaaaaaaaaaaa\n"
9494                "    , public bbbbbbbbbbbbbbbbbb {\n"
9495                "}",
9496                InheritanceStyle);
9497   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
9498   verifyFormat("class Foooooooooooooooooooooo\n"
9499                "    : public aaaaaaaaaaaaaaaaaa,\n"
9500                "      public bbbbbbbbbbbbbbbbbb {\n"
9501                "}",
9502                InheritanceStyle);
9503   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
9504   verifyFormat("class Foooooooooooooooooooooo\n"
9505                ": public aaaaaaaaaaaaaaaaaa,\n"
9506                "  public bbbbbbbbbbbbbbbbbb {}",
9507                InheritanceStyle);
9508 
9509   FormatStyle ForLoopStyle = getLLVMStyle();
9510   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
9511   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
9512   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
9513   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
9514   verifyFormat("int x = a ? b : c;", ForLoopStyle);
9515   verifyFormat("{\n"
9516                "label2:\n"
9517                "  int x = 0;\n"
9518                "}",
9519                ForLoopStyle);
9520   verifyFormat("switch (x) {\n"
9521                "case 1:\n"
9522                "default:\n"
9523                "}",
9524                ForLoopStyle);
9525 
9526   FormatStyle NoSpaceStyle = getLLVMStyle();
9527   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
9528   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
9529   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
9530   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
9531   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
9532   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
9533   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
9534   verifyFormat("{\n"
9535                "label3:\n"
9536                "  int x = 0;\n"
9537                "}",
9538                NoSpaceStyle);
9539   verifyFormat("switch (x) {\n"
9540                "case 1:\n"
9541                "default:\n"
9542                "}",
9543                NoSpaceStyle);
9544 }
9545 
9546 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9547   FormatStyle Alignment = getLLVMStyle();
9548   Alignment.AlignConsecutiveAssignments = false;
9549   verifyFormat("int a = 5;\n"
9550                "int oneTwoThree = 123;",
9551                Alignment);
9552   verifyFormat("int a = 5;\n"
9553                "int oneTwoThree = 123;",
9554                Alignment);
9555 
9556   Alignment.AlignConsecutiveAssignments = true;
9557   verifyFormat("int a           = 5;\n"
9558                "int oneTwoThree = 123;",
9559                Alignment);
9560   verifyFormat("int a           = method();\n"
9561                "int oneTwoThree = 133;",
9562                Alignment);
9563   verifyFormat("a &= 5;\n"
9564                "bcd *= 5;\n"
9565                "ghtyf += 5;\n"
9566                "dvfvdb -= 5;\n"
9567                "a /= 5;\n"
9568                "vdsvsv %= 5;\n"
9569                "sfdbddfbdfbb ^= 5;\n"
9570                "dvsdsv |= 5;\n"
9571                "int dsvvdvsdvvv = 123;",
9572                Alignment);
9573   verifyFormat("int i = 1, j = 10;\n"
9574                "something = 2000;",
9575                Alignment);
9576   verifyFormat("something = 2000;\n"
9577                "int i = 1, j = 10;\n",
9578                Alignment);
9579   verifyFormat("something = 2000;\n"
9580                "another   = 911;\n"
9581                "int i = 1, j = 10;\n"
9582                "oneMore = 1;\n"
9583                "i       = 2;",
9584                Alignment);
9585   verifyFormat("int a   = 5;\n"
9586                "int one = 1;\n"
9587                "method();\n"
9588                "int oneTwoThree = 123;\n"
9589                "int oneTwo      = 12;",
9590                Alignment);
9591   verifyFormat("int oneTwoThree = 123;\n"
9592                "int oneTwo      = 12;\n"
9593                "method();\n",
9594                Alignment);
9595   verifyFormat("int oneTwoThree = 123; // comment\n"
9596                "int oneTwo      = 12;  // comment",
9597                Alignment);
9598   EXPECT_EQ("int a = 5;\n"
9599             "\n"
9600             "int oneTwoThree = 123;",
9601             format("int a       = 5;\n"
9602                    "\n"
9603                    "int oneTwoThree= 123;",
9604                    Alignment));
9605   EXPECT_EQ("int a   = 5;\n"
9606             "int one = 1;\n"
9607             "\n"
9608             "int oneTwoThree = 123;",
9609             format("int a = 5;\n"
9610                    "int one = 1;\n"
9611                    "\n"
9612                    "int oneTwoThree = 123;",
9613                    Alignment));
9614   EXPECT_EQ("int a   = 5;\n"
9615             "int one = 1;\n"
9616             "\n"
9617             "int oneTwoThree = 123;\n"
9618             "int oneTwo      = 12;",
9619             format("int a = 5;\n"
9620                    "int one = 1;\n"
9621                    "\n"
9622                    "int oneTwoThree = 123;\n"
9623                    "int oneTwo = 12;",
9624                    Alignment));
9625   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9626   verifyFormat("#define A \\\n"
9627                "  int aaaa       = 12; \\\n"
9628                "  int b          = 23; \\\n"
9629                "  int ccc        = 234; \\\n"
9630                "  int dddddddddd = 2345;",
9631                Alignment);
9632   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9633   verifyFormat("#define A               \\\n"
9634                "  int aaaa       = 12;  \\\n"
9635                "  int b          = 23;  \\\n"
9636                "  int ccc        = 234; \\\n"
9637                "  int dddddddddd = 2345;",
9638                Alignment);
9639   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9640   verifyFormat("#define A                                                      "
9641                "                \\\n"
9642                "  int aaaa       = 12;                                         "
9643                "                \\\n"
9644                "  int b          = 23;                                         "
9645                "                \\\n"
9646                "  int ccc        = 234;                                        "
9647                "                \\\n"
9648                "  int dddddddddd = 2345;",
9649                Alignment);
9650   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9651                "k = 4, int l = 5,\n"
9652                "                  int m = 6) {\n"
9653                "  int j      = 10;\n"
9654                "  otherThing = 1;\n"
9655                "}",
9656                Alignment);
9657   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9658                "  int i   = 1;\n"
9659                "  int j   = 2;\n"
9660                "  int big = 10000;\n"
9661                "}",
9662                Alignment);
9663   verifyFormat("class C {\n"
9664                "public:\n"
9665                "  int i            = 1;\n"
9666                "  virtual void f() = 0;\n"
9667                "};",
9668                Alignment);
9669   verifyFormat("int i = 1;\n"
9670                "if (SomeType t = getSomething()) {\n"
9671                "}\n"
9672                "int j   = 2;\n"
9673                "int big = 10000;",
9674                Alignment);
9675   verifyFormat("int j = 7;\n"
9676                "for (int k = 0; k < N; ++k) {\n"
9677                "}\n"
9678                "int j   = 2;\n"
9679                "int big = 10000;\n"
9680                "}",
9681                Alignment);
9682   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9683   verifyFormat("int i = 1;\n"
9684                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9685                "    = someLooooooooooooooooongFunction();\n"
9686                "int j = 2;",
9687                Alignment);
9688   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9689   verifyFormat("int i = 1;\n"
9690                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9691                "    someLooooooooooooooooongFunction();\n"
9692                "int j = 2;",
9693                Alignment);
9694 
9695   verifyFormat("auto lambda = []() {\n"
9696                "  auto i = 0;\n"
9697                "  return 0;\n"
9698                "};\n"
9699                "int i  = 0;\n"
9700                "auto v = type{\n"
9701                "    i = 1,   //\n"
9702                "    (i = 2), //\n"
9703                "    i = 3    //\n"
9704                "};",
9705                Alignment);
9706 
9707   verifyFormat(
9708       "int i      = 1;\n"
9709       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9710       "                          loooooooooooooooooooooongParameterB);\n"
9711       "int j      = 2;",
9712       Alignment);
9713 
9714   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9715                "          typename B   = very_long_type_name_1,\n"
9716                "          typename T_2 = very_long_type_name_2>\n"
9717                "auto foo() {}\n",
9718                Alignment);
9719   verifyFormat("int a, b = 1;\n"
9720                "int c  = 2;\n"
9721                "int dd = 3;\n",
9722                Alignment);
9723   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9724                "float b[1][] = {{3.f}};\n",
9725                Alignment);
9726   verifyFormat("for (int i = 0; i < 1; i++)\n"
9727                "  int x = 1;\n",
9728                Alignment);
9729   verifyFormat("for (i = 0; i < 1; i++)\n"
9730                "  x = 1;\n"
9731                "y = 1;\n",
9732                Alignment);
9733 }
9734 
9735 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9736   FormatStyle Alignment = getLLVMStyle();
9737   Alignment.AlignConsecutiveDeclarations = false;
9738   verifyFormat("float const a = 5;\n"
9739                "int oneTwoThree = 123;",
9740                Alignment);
9741   verifyFormat("int a = 5;\n"
9742                "float const oneTwoThree = 123;",
9743                Alignment);
9744 
9745   Alignment.AlignConsecutiveDeclarations = true;
9746   verifyFormat("float const a = 5;\n"
9747                "int         oneTwoThree = 123;",
9748                Alignment);
9749   verifyFormat("int         a = method();\n"
9750                "float const oneTwoThree = 133;",
9751                Alignment);
9752   verifyFormat("int i = 1, j = 10;\n"
9753                "something = 2000;",
9754                Alignment);
9755   verifyFormat("something = 2000;\n"
9756                "int i = 1, j = 10;\n",
9757                Alignment);
9758   verifyFormat("float      something = 2000;\n"
9759                "double     another = 911;\n"
9760                "int        i = 1, j = 10;\n"
9761                "const int *oneMore = 1;\n"
9762                "unsigned   i = 2;",
9763                Alignment);
9764   verifyFormat("float a = 5;\n"
9765                "int   one = 1;\n"
9766                "method();\n"
9767                "const double       oneTwoThree = 123;\n"
9768                "const unsigned int oneTwo = 12;",
9769                Alignment);
9770   verifyFormat("int      oneTwoThree{0}; // comment\n"
9771                "unsigned oneTwo;         // comment",
9772                Alignment);
9773   EXPECT_EQ("float const a = 5;\n"
9774             "\n"
9775             "int oneTwoThree = 123;",
9776             format("float const   a = 5;\n"
9777                    "\n"
9778                    "int           oneTwoThree= 123;",
9779                    Alignment));
9780   EXPECT_EQ("float a = 5;\n"
9781             "int   one = 1;\n"
9782             "\n"
9783             "unsigned oneTwoThree = 123;",
9784             format("float    a = 5;\n"
9785                    "int      one = 1;\n"
9786                    "\n"
9787                    "unsigned oneTwoThree = 123;",
9788                    Alignment));
9789   EXPECT_EQ("float a = 5;\n"
9790             "int   one = 1;\n"
9791             "\n"
9792             "unsigned oneTwoThree = 123;\n"
9793             "int      oneTwo = 12;",
9794             format("float    a = 5;\n"
9795                    "int one = 1;\n"
9796                    "\n"
9797                    "unsigned oneTwoThree = 123;\n"
9798                    "int oneTwo = 12;",
9799                    Alignment));
9800   // Function prototype alignment
9801   verifyFormat("int    a();\n"
9802                "double b();",
9803                Alignment);
9804   verifyFormat("int    a(int x);\n"
9805                "double b();",
9806                Alignment);
9807   unsigned OldColumnLimit = Alignment.ColumnLimit;
9808   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9809   // otherwise the function parameters will be re-flowed onto a single line.
9810   Alignment.ColumnLimit = 0;
9811   EXPECT_EQ("int    a(int   x,\n"
9812             "         float y);\n"
9813             "double b(int    x,\n"
9814             "         double y);",
9815             format("int a(int x,\n"
9816                    " float y);\n"
9817                    "double b(int x,\n"
9818                    " double y);",
9819                    Alignment));
9820   // This ensures that function parameters of function declarations are
9821   // correctly indented when their owning functions are indented.
9822   // The failure case here is for 'double y' to not be indented enough.
9823   EXPECT_EQ("double a(int x);\n"
9824             "int    b(int    y,\n"
9825             "         double z);",
9826             format("double a(int x);\n"
9827                    "int b(int y,\n"
9828                    " double z);",
9829                    Alignment));
9830   // Set ColumnLimit low so that we induce wrapping immediately after
9831   // the function name and opening paren.
9832   Alignment.ColumnLimit = 13;
9833   verifyFormat("int function(\n"
9834                "    int  x,\n"
9835                "    bool y);",
9836                Alignment);
9837   Alignment.ColumnLimit = OldColumnLimit;
9838   // Ensure function pointers don't screw up recursive alignment
9839   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9840                "double b();",
9841                Alignment);
9842   Alignment.AlignConsecutiveAssignments = true;
9843   // Ensure recursive alignment is broken by function braces, so that the
9844   // "a = 1" does not align with subsequent assignments inside the function
9845   // body.
9846   verifyFormat("int func(int a = 1) {\n"
9847                "  int b  = 2;\n"
9848                "  int cc = 3;\n"
9849                "}",
9850                Alignment);
9851   verifyFormat("float      something = 2000;\n"
9852                "double     another   = 911;\n"
9853                "int        i = 1, j = 10;\n"
9854                "const int *oneMore = 1;\n"
9855                "unsigned   i       = 2;",
9856                Alignment);
9857   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9858                "unsigned oneTwo      = 0;   // comment",
9859                Alignment);
9860   // Make sure that scope is correctly tracked, in the absence of braces
9861   verifyFormat("for (int i = 0; i < n; i++)\n"
9862                "  j = i;\n"
9863                "double x = 1;\n",
9864                Alignment);
9865   verifyFormat("if (int i = 0)\n"
9866                "  j = i;\n"
9867                "double x = 1;\n",
9868                Alignment);
9869   // Ensure operator[] and operator() are comprehended
9870   verifyFormat("struct test {\n"
9871                "  long long int foo();\n"
9872                "  int           operator[](int a);\n"
9873                "  double        bar();\n"
9874                "};\n",
9875                Alignment);
9876   verifyFormat("struct test {\n"
9877                "  long long int foo();\n"
9878                "  int           operator()(int a);\n"
9879                "  double        bar();\n"
9880                "};\n",
9881                Alignment);
9882   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9883             "  int const i   = 1;\n"
9884             "  int *     j   = 2;\n"
9885             "  int       big = 10000;\n"
9886             "\n"
9887             "  unsigned oneTwoThree = 123;\n"
9888             "  int      oneTwo      = 12;\n"
9889             "  method();\n"
9890             "  float k  = 2;\n"
9891             "  int   ll = 10000;\n"
9892             "}",
9893             format("void SomeFunction(int parameter= 0) {\n"
9894                    " int const  i= 1;\n"
9895                    "  int *j=2;\n"
9896                    " int big  =  10000;\n"
9897                    "\n"
9898                    "unsigned oneTwoThree  =123;\n"
9899                    "int oneTwo = 12;\n"
9900                    "  method();\n"
9901                    "float k= 2;\n"
9902                    "int ll=10000;\n"
9903                    "}",
9904                    Alignment));
9905   Alignment.AlignConsecutiveAssignments = false;
9906   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9907   verifyFormat("#define A \\\n"
9908                "  int       aaaa = 12; \\\n"
9909                "  float     b = 23; \\\n"
9910                "  const int ccc = 234; \\\n"
9911                "  unsigned  dddddddddd = 2345;",
9912                Alignment);
9913   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9914   verifyFormat("#define A              \\\n"
9915                "  int       aaaa = 12; \\\n"
9916                "  float     b = 23;    \\\n"
9917                "  const int ccc = 234; \\\n"
9918                "  unsigned  dddddddddd = 2345;",
9919                Alignment);
9920   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9921   Alignment.ColumnLimit = 30;
9922   verifyFormat("#define A                    \\\n"
9923                "  int       aaaa = 12;       \\\n"
9924                "  float     b = 23;          \\\n"
9925                "  const int ccc = 234;       \\\n"
9926                "  int       dddddddddd = 2345;",
9927                Alignment);
9928   Alignment.ColumnLimit = 80;
9929   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9930                "k = 4, int l = 5,\n"
9931                "                  int m = 6) {\n"
9932                "  const int j = 10;\n"
9933                "  otherThing = 1;\n"
9934                "}",
9935                Alignment);
9936   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9937                "  int const i = 1;\n"
9938                "  int *     j = 2;\n"
9939                "  int       big = 10000;\n"
9940                "}",
9941                Alignment);
9942   verifyFormat("class C {\n"
9943                "public:\n"
9944                "  int          i = 1;\n"
9945                "  virtual void f() = 0;\n"
9946                "};",
9947                Alignment);
9948   verifyFormat("float i = 1;\n"
9949                "if (SomeType t = getSomething()) {\n"
9950                "}\n"
9951                "const unsigned j = 2;\n"
9952                "int            big = 10000;",
9953                Alignment);
9954   verifyFormat("float j = 7;\n"
9955                "for (int k = 0; k < N; ++k) {\n"
9956                "}\n"
9957                "unsigned j = 2;\n"
9958                "int      big = 10000;\n"
9959                "}",
9960                Alignment);
9961   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9962   verifyFormat("float              i = 1;\n"
9963                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9964                "    = someLooooooooooooooooongFunction();\n"
9965                "int j = 2;",
9966                Alignment);
9967   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9968   verifyFormat("int                i = 1;\n"
9969                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9970                "    someLooooooooooooooooongFunction();\n"
9971                "int j = 2;",
9972                Alignment);
9973 
9974   Alignment.AlignConsecutiveAssignments = true;
9975   verifyFormat("auto lambda = []() {\n"
9976                "  auto  ii = 0;\n"
9977                "  float j  = 0;\n"
9978                "  return 0;\n"
9979                "};\n"
9980                "int   i  = 0;\n"
9981                "float i2 = 0;\n"
9982                "auto  v  = type{\n"
9983                "    i = 1,   //\n"
9984                "    (i = 2), //\n"
9985                "    i = 3    //\n"
9986                "};",
9987                Alignment);
9988   Alignment.AlignConsecutiveAssignments = false;
9989 
9990   verifyFormat(
9991       "int      i = 1;\n"
9992       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9993       "                          loooooooooooooooooooooongParameterB);\n"
9994       "int      j = 2;",
9995       Alignment);
9996 
9997   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9998   // We expect declarations and assignments to align, as long as it doesn't
9999   // exceed the column limit, starting a new alignment sequence whenever it
10000   // happens.
10001   Alignment.AlignConsecutiveAssignments = true;
10002   Alignment.ColumnLimit = 30;
10003   verifyFormat("float    ii              = 1;\n"
10004                "unsigned j               = 2;\n"
10005                "int someVerylongVariable = 1;\n"
10006                "AnotherLongType  ll = 123456;\n"
10007                "VeryVeryLongType k  = 2;\n"
10008                "int              myvar = 1;",
10009                Alignment);
10010   Alignment.ColumnLimit = 80;
10011   Alignment.AlignConsecutiveAssignments = false;
10012 
10013   verifyFormat(
10014       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
10015       "          typename LongType, typename B>\n"
10016       "auto foo() {}\n",
10017       Alignment);
10018   verifyFormat("float a, b = 1;\n"
10019                "int   c = 2;\n"
10020                "int   dd = 3;\n",
10021                Alignment);
10022   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
10023                "float b[1][] = {{3.f}};\n",
10024                Alignment);
10025   Alignment.AlignConsecutiveAssignments = true;
10026   verifyFormat("float a, b = 1;\n"
10027                "int   c  = 2;\n"
10028                "int   dd = 3;\n",
10029                Alignment);
10030   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
10031                "float b[1][] = {{3.f}};\n",
10032                Alignment);
10033   Alignment.AlignConsecutiveAssignments = false;
10034 
10035   Alignment.ColumnLimit = 30;
10036   Alignment.BinPackParameters = false;
10037   verifyFormat("void foo(float     a,\n"
10038                "         float     b,\n"
10039                "         int       c,\n"
10040                "         uint32_t *d) {\n"
10041                "  int *  e = 0;\n"
10042                "  float  f = 0;\n"
10043                "  double g = 0;\n"
10044                "}\n"
10045                "void bar(ino_t     a,\n"
10046                "         int       b,\n"
10047                "         uint32_t *c,\n"
10048                "         bool      d) {}\n",
10049                Alignment);
10050   Alignment.BinPackParameters = true;
10051   Alignment.ColumnLimit = 80;
10052 
10053   // Bug 33507
10054   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
10055   verifyFormat(
10056       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
10057       "  static const Version verVs2017;\n"
10058       "  return true;\n"
10059       "});\n",
10060       Alignment);
10061   Alignment.PointerAlignment = FormatStyle::PAS_Right;
10062 
10063   // See llvm.org/PR35641
10064   Alignment.AlignConsecutiveDeclarations = true;
10065   verifyFormat("int func() { //\n"
10066                "  int      b;\n"
10067                "  unsigned c;\n"
10068                "}",
10069                Alignment);
10070 }
10071 
10072 TEST_F(FormatTest, LinuxBraceBreaking) {
10073   FormatStyle LinuxBraceStyle = getLLVMStyle();
10074   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
10075   verifyFormat("namespace a\n"
10076                "{\n"
10077                "class A\n"
10078                "{\n"
10079                "  void f()\n"
10080                "  {\n"
10081                "    if (true) {\n"
10082                "      a();\n"
10083                "      b();\n"
10084                "    } else {\n"
10085                "      a();\n"
10086                "    }\n"
10087                "  }\n"
10088                "  void g() { return; }\n"
10089                "};\n"
10090                "struct B {\n"
10091                "  int x;\n"
10092                "};\n"
10093                "} // namespace a\n",
10094                LinuxBraceStyle);
10095   verifyFormat("enum X {\n"
10096                "  Y = 0,\n"
10097                "}\n",
10098                LinuxBraceStyle);
10099   verifyFormat("struct S {\n"
10100                "  int Type;\n"
10101                "  union {\n"
10102                "    int x;\n"
10103                "    double y;\n"
10104                "  } Value;\n"
10105                "  class C\n"
10106                "  {\n"
10107                "    MyFavoriteType Value;\n"
10108                "  } Class;\n"
10109                "}\n",
10110                LinuxBraceStyle);
10111 }
10112 
10113 TEST_F(FormatTest, MozillaBraceBreaking) {
10114   FormatStyle MozillaBraceStyle = getLLVMStyle();
10115   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
10116   MozillaBraceStyle.FixNamespaceComments = false;
10117   verifyFormat("namespace a {\n"
10118                "class A\n"
10119                "{\n"
10120                "  void f()\n"
10121                "  {\n"
10122                "    if (true) {\n"
10123                "      a();\n"
10124                "      b();\n"
10125                "    }\n"
10126                "  }\n"
10127                "  void g() { return; }\n"
10128                "};\n"
10129                "enum E\n"
10130                "{\n"
10131                "  A,\n"
10132                "  // foo\n"
10133                "  B,\n"
10134                "  C\n"
10135                "};\n"
10136                "struct B\n"
10137                "{\n"
10138                "  int x;\n"
10139                "};\n"
10140                "}\n",
10141                MozillaBraceStyle);
10142   verifyFormat("struct S\n"
10143                "{\n"
10144                "  int Type;\n"
10145                "  union\n"
10146                "  {\n"
10147                "    int x;\n"
10148                "    double y;\n"
10149                "  } Value;\n"
10150                "  class C\n"
10151                "  {\n"
10152                "    MyFavoriteType Value;\n"
10153                "  } Class;\n"
10154                "}\n",
10155                MozillaBraceStyle);
10156 }
10157 
10158 TEST_F(FormatTest, StroustrupBraceBreaking) {
10159   FormatStyle StroustrupBraceStyle = getLLVMStyle();
10160   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10161   verifyFormat("namespace a {\n"
10162                "class A {\n"
10163                "  void f()\n"
10164                "  {\n"
10165                "    if (true) {\n"
10166                "      a();\n"
10167                "      b();\n"
10168                "    }\n"
10169                "  }\n"
10170                "  void g() { return; }\n"
10171                "};\n"
10172                "struct B {\n"
10173                "  int x;\n"
10174                "};\n"
10175                "} // namespace a\n",
10176                StroustrupBraceStyle);
10177 
10178   verifyFormat("void foo()\n"
10179                "{\n"
10180                "  if (a) {\n"
10181                "    a();\n"
10182                "  }\n"
10183                "  else {\n"
10184                "    b();\n"
10185                "  }\n"
10186                "}\n",
10187                StroustrupBraceStyle);
10188 
10189   verifyFormat("#ifdef _DEBUG\n"
10190                "int foo(int i = 0)\n"
10191                "#else\n"
10192                "int foo(int i = 5)\n"
10193                "#endif\n"
10194                "{\n"
10195                "  return i;\n"
10196                "}",
10197                StroustrupBraceStyle);
10198 
10199   verifyFormat("void foo() {}\n"
10200                "void bar()\n"
10201                "#ifdef _DEBUG\n"
10202                "{\n"
10203                "  foo();\n"
10204                "}\n"
10205                "#else\n"
10206                "{\n"
10207                "}\n"
10208                "#endif",
10209                StroustrupBraceStyle);
10210 
10211   verifyFormat("void foobar() { int i = 5; }\n"
10212                "#ifdef _DEBUG\n"
10213                "void bar() {}\n"
10214                "#else\n"
10215                "void bar() { foobar(); }\n"
10216                "#endif",
10217                StroustrupBraceStyle);
10218 }
10219 
10220 TEST_F(FormatTest, AllmanBraceBreaking) {
10221   FormatStyle AllmanBraceStyle = getLLVMStyle();
10222   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
10223 
10224   EXPECT_EQ("namespace a\n"
10225             "{\n"
10226             "void f();\n"
10227             "void g();\n"
10228             "} // namespace a\n",
10229             format("namespace a\n"
10230                    "{\n"
10231                    "void f();\n"
10232                    "void g();\n"
10233                    "}\n",
10234                    AllmanBraceStyle));
10235 
10236   verifyFormat("namespace a\n"
10237                "{\n"
10238                "class A\n"
10239                "{\n"
10240                "  void f()\n"
10241                "  {\n"
10242                "    if (true)\n"
10243                "    {\n"
10244                "      a();\n"
10245                "      b();\n"
10246                "    }\n"
10247                "  }\n"
10248                "  void g() { return; }\n"
10249                "};\n"
10250                "struct B\n"
10251                "{\n"
10252                "  int x;\n"
10253                "};\n"
10254                "} // namespace a",
10255                AllmanBraceStyle);
10256 
10257   verifyFormat("void f()\n"
10258                "{\n"
10259                "  if (true)\n"
10260                "  {\n"
10261                "    a();\n"
10262                "  }\n"
10263                "  else if (false)\n"
10264                "  {\n"
10265                "    b();\n"
10266                "  }\n"
10267                "  else\n"
10268                "  {\n"
10269                "    c();\n"
10270                "  }\n"
10271                "}\n",
10272                AllmanBraceStyle);
10273 
10274   verifyFormat("void f()\n"
10275                "{\n"
10276                "  for (int i = 0; i < 10; ++i)\n"
10277                "  {\n"
10278                "    a();\n"
10279                "  }\n"
10280                "  while (false)\n"
10281                "  {\n"
10282                "    b();\n"
10283                "  }\n"
10284                "  do\n"
10285                "  {\n"
10286                "    c();\n"
10287                "  } while (false)\n"
10288                "}\n",
10289                AllmanBraceStyle);
10290 
10291   verifyFormat("void f(int a)\n"
10292                "{\n"
10293                "  switch (a)\n"
10294                "  {\n"
10295                "  case 0:\n"
10296                "    break;\n"
10297                "  case 1:\n"
10298                "  {\n"
10299                "    break;\n"
10300                "  }\n"
10301                "  case 2:\n"
10302                "  {\n"
10303                "  }\n"
10304                "  break;\n"
10305                "  default:\n"
10306                "    break;\n"
10307                "  }\n"
10308                "}\n",
10309                AllmanBraceStyle);
10310 
10311   verifyFormat("enum X\n"
10312                "{\n"
10313                "  Y = 0,\n"
10314                "}\n",
10315                AllmanBraceStyle);
10316   verifyFormat("enum X\n"
10317                "{\n"
10318                "  Y = 0\n"
10319                "}\n",
10320                AllmanBraceStyle);
10321 
10322   verifyFormat("@interface BSApplicationController ()\n"
10323                "{\n"
10324                "@private\n"
10325                "  id _extraIvar;\n"
10326                "}\n"
10327                "@end\n",
10328                AllmanBraceStyle);
10329 
10330   verifyFormat("#ifdef _DEBUG\n"
10331                "int foo(int i = 0)\n"
10332                "#else\n"
10333                "int foo(int i = 5)\n"
10334                "#endif\n"
10335                "{\n"
10336                "  return i;\n"
10337                "}",
10338                AllmanBraceStyle);
10339 
10340   verifyFormat("void foo() {}\n"
10341                "void bar()\n"
10342                "#ifdef _DEBUG\n"
10343                "{\n"
10344                "  foo();\n"
10345                "}\n"
10346                "#else\n"
10347                "{\n"
10348                "}\n"
10349                "#endif",
10350                AllmanBraceStyle);
10351 
10352   verifyFormat("void foobar() { int i = 5; }\n"
10353                "#ifdef _DEBUG\n"
10354                "void bar() {}\n"
10355                "#else\n"
10356                "void bar() { foobar(); }\n"
10357                "#endif",
10358                AllmanBraceStyle);
10359 
10360   // This shouldn't affect ObjC blocks..
10361   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
10362                "  // ...\n"
10363                "  int i;\n"
10364                "}];",
10365                AllmanBraceStyle);
10366   verifyFormat("void (^block)(void) = ^{\n"
10367                "  // ...\n"
10368                "  int i;\n"
10369                "};",
10370                AllmanBraceStyle);
10371   // .. or dict literals.
10372   verifyFormat("void f()\n"
10373                "{\n"
10374                "  // ...\n"
10375                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
10376                "}",
10377                AllmanBraceStyle);
10378   verifyFormat("void f()\n"
10379                "{\n"
10380                "  // ...\n"
10381                "  [object someMethod:@{a : @\"b\"}];\n"
10382                "}",
10383                AllmanBraceStyle);
10384   verifyFormat("int f()\n"
10385                "{ // comment\n"
10386                "  return 42;\n"
10387                "}",
10388                AllmanBraceStyle);
10389 
10390   AllmanBraceStyle.ColumnLimit = 19;
10391   verifyFormat("void f() { int i; }", AllmanBraceStyle);
10392   AllmanBraceStyle.ColumnLimit = 18;
10393   verifyFormat("void f()\n"
10394                "{\n"
10395                "  int i;\n"
10396                "}",
10397                AllmanBraceStyle);
10398   AllmanBraceStyle.ColumnLimit = 80;
10399 
10400   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
10401   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
10402   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
10403   verifyFormat("void f(bool b)\n"
10404                "{\n"
10405                "  if (b)\n"
10406                "  {\n"
10407                "    return;\n"
10408                "  }\n"
10409                "}\n",
10410                BreakBeforeBraceShortIfs);
10411   verifyFormat("void f(bool b)\n"
10412                "{\n"
10413                "  if constexpr (b)\n"
10414                "  {\n"
10415                "    return;\n"
10416                "  }\n"
10417                "}\n",
10418                BreakBeforeBraceShortIfs);
10419   verifyFormat("void f(bool b)\n"
10420                "{\n"
10421                "  if (b) return;\n"
10422                "}\n",
10423                BreakBeforeBraceShortIfs);
10424   verifyFormat("void f(bool b)\n"
10425                "{\n"
10426                "  if constexpr (b) return;\n"
10427                "}\n",
10428                BreakBeforeBraceShortIfs);
10429   verifyFormat("void f(bool b)\n"
10430                "{\n"
10431                "  while (b)\n"
10432                "  {\n"
10433                "    return;\n"
10434                "  }\n"
10435                "}\n",
10436                BreakBeforeBraceShortIfs);
10437 }
10438 
10439 TEST_F(FormatTest, GNUBraceBreaking) {
10440   FormatStyle GNUBraceStyle = getLLVMStyle();
10441   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
10442   verifyFormat("namespace a\n"
10443                "{\n"
10444                "class A\n"
10445                "{\n"
10446                "  void f()\n"
10447                "  {\n"
10448                "    int a;\n"
10449                "    {\n"
10450                "      int b;\n"
10451                "    }\n"
10452                "    if (true)\n"
10453                "      {\n"
10454                "        a();\n"
10455                "        b();\n"
10456                "      }\n"
10457                "  }\n"
10458                "  void g() { return; }\n"
10459                "}\n"
10460                "} // namespace a",
10461                GNUBraceStyle);
10462 
10463   verifyFormat("void f()\n"
10464                "{\n"
10465                "  if (true)\n"
10466                "    {\n"
10467                "      a();\n"
10468                "    }\n"
10469                "  else if (false)\n"
10470                "    {\n"
10471                "      b();\n"
10472                "    }\n"
10473                "  else\n"
10474                "    {\n"
10475                "      c();\n"
10476                "    }\n"
10477                "}\n",
10478                GNUBraceStyle);
10479 
10480   verifyFormat("void f()\n"
10481                "{\n"
10482                "  for (int i = 0; i < 10; ++i)\n"
10483                "    {\n"
10484                "      a();\n"
10485                "    }\n"
10486                "  while (false)\n"
10487                "    {\n"
10488                "      b();\n"
10489                "    }\n"
10490                "  do\n"
10491                "    {\n"
10492                "      c();\n"
10493                "    }\n"
10494                "  while (false);\n"
10495                "}\n",
10496                GNUBraceStyle);
10497 
10498   verifyFormat("void f(int a)\n"
10499                "{\n"
10500                "  switch (a)\n"
10501                "    {\n"
10502                "    case 0:\n"
10503                "      break;\n"
10504                "    case 1:\n"
10505                "      {\n"
10506                "        break;\n"
10507                "      }\n"
10508                "    case 2:\n"
10509                "      {\n"
10510                "      }\n"
10511                "      break;\n"
10512                "    default:\n"
10513                "      break;\n"
10514                "    }\n"
10515                "}\n",
10516                GNUBraceStyle);
10517 
10518   verifyFormat("enum X\n"
10519                "{\n"
10520                "  Y = 0,\n"
10521                "}\n",
10522                GNUBraceStyle);
10523 
10524   verifyFormat("@interface BSApplicationController ()\n"
10525                "{\n"
10526                "@private\n"
10527                "  id _extraIvar;\n"
10528                "}\n"
10529                "@end\n",
10530                GNUBraceStyle);
10531 
10532   verifyFormat("#ifdef _DEBUG\n"
10533                "int foo(int i = 0)\n"
10534                "#else\n"
10535                "int foo(int i = 5)\n"
10536                "#endif\n"
10537                "{\n"
10538                "  return i;\n"
10539                "}",
10540                GNUBraceStyle);
10541 
10542   verifyFormat("void foo() {}\n"
10543                "void bar()\n"
10544                "#ifdef _DEBUG\n"
10545                "{\n"
10546                "  foo();\n"
10547                "}\n"
10548                "#else\n"
10549                "{\n"
10550                "}\n"
10551                "#endif",
10552                GNUBraceStyle);
10553 
10554   verifyFormat("void foobar() { int i = 5; }\n"
10555                "#ifdef _DEBUG\n"
10556                "void bar() {}\n"
10557                "#else\n"
10558                "void bar() { foobar(); }\n"
10559                "#endif",
10560                GNUBraceStyle);
10561 }
10562 
10563 TEST_F(FormatTest, WebKitBraceBreaking) {
10564   FormatStyle WebKitBraceStyle = getLLVMStyle();
10565   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
10566   WebKitBraceStyle.FixNamespaceComments = false;
10567   verifyFormat("namespace a {\n"
10568                "class A {\n"
10569                "  void f()\n"
10570                "  {\n"
10571                "    if (true) {\n"
10572                "      a();\n"
10573                "      b();\n"
10574                "    }\n"
10575                "  }\n"
10576                "  void g() { return; }\n"
10577                "};\n"
10578                "enum E {\n"
10579                "  A,\n"
10580                "  // foo\n"
10581                "  B,\n"
10582                "  C\n"
10583                "};\n"
10584                "struct B {\n"
10585                "  int x;\n"
10586                "};\n"
10587                "}\n",
10588                WebKitBraceStyle);
10589   verifyFormat("struct S {\n"
10590                "  int Type;\n"
10591                "  union {\n"
10592                "    int x;\n"
10593                "    double y;\n"
10594                "  } Value;\n"
10595                "  class C {\n"
10596                "    MyFavoriteType Value;\n"
10597                "  } Class;\n"
10598                "};\n",
10599                WebKitBraceStyle);
10600 }
10601 
10602 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10603   verifyFormat("void f() {\n"
10604                "  try {\n"
10605                "  } catch (const Exception &e) {\n"
10606                "  }\n"
10607                "}\n",
10608                getLLVMStyle());
10609 }
10610 
10611 TEST_F(FormatTest, UnderstandsPragmas) {
10612   verifyFormat("#pragma omp reduction(| : var)");
10613   verifyFormat("#pragma omp reduction(+ : var)");
10614 
10615   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10616             "(including parentheses).",
10617             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10618                    "(including parentheses)."));
10619 }
10620 
10621 TEST_F(FormatTest, UnderstandPragmaOption) {
10622   verifyFormat("#pragma option -C -A");
10623 
10624   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10625 }
10626 
10627 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
10628   FormatStyle Style = getLLVMStyle();
10629   Style.ColumnLimit = 20;
10630 
10631   verifyFormat("int a; // the\n"
10632                "       // comment", Style);
10633   EXPECT_EQ("int a; /* first line\n"
10634             "        * second\n"
10635             "        * line third\n"
10636             "        * line\n"
10637             "        */",
10638             format("int a; /* first line\n"
10639                    "        * second\n"
10640                    "        * line third\n"
10641                    "        * line\n"
10642                    "        */",
10643                    Style));
10644   EXPECT_EQ("int a; // first line\n"
10645             "       // second\n"
10646             "       // line third\n"
10647             "       // line",
10648             format("int a; // first line\n"
10649                    "       // second line\n"
10650                    "       // third line",
10651                    Style));
10652 
10653   Style.PenaltyExcessCharacter = 90;
10654   verifyFormat("int a; // the comment", Style);
10655   EXPECT_EQ("int a; // the comment\n"
10656             "       // aaa",
10657             format("int a; // the comment aaa", Style));
10658   EXPECT_EQ("int a; /* first line\n"
10659             "        * second line\n"
10660             "        * third line\n"
10661             "        */",
10662             format("int a; /* first line\n"
10663                    "        * second line\n"
10664                    "        * third line\n"
10665                    "        */",
10666                    Style));
10667   EXPECT_EQ("int a; // first line\n"
10668             "       // second line\n"
10669             "       // third line",
10670             format("int a; // first line\n"
10671                    "       // second line\n"
10672                    "       // third line",
10673                    Style));
10674   // FIXME: Investigate why this is not getting the same layout as the test
10675   // above.
10676   EXPECT_EQ("int a; /* first line\n"
10677             "        * second line\n"
10678             "        * third line\n"
10679             "        */",
10680             format("int a; /* first line second line third line"
10681                    "\n*/",
10682                    Style));
10683 
10684   EXPECT_EQ("// foo bar baz bazfoo\n"
10685             "// foo bar foo bar\n",
10686             format("// foo bar baz bazfoo\n"
10687                    "// foo bar foo           bar\n",
10688                    Style));
10689   EXPECT_EQ("// foo bar baz bazfoo\n"
10690             "// foo bar foo bar\n",
10691             format("// foo bar baz      bazfoo\n"
10692                    "// foo            bar foo bar\n",
10693                    Style));
10694 
10695   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
10696   // next one.
10697   EXPECT_EQ("// foo bar baz bazfoo\n"
10698             "// bar foo bar\n",
10699             format("// foo bar baz      bazfoo bar\n"
10700                    "// foo            bar\n",
10701                    Style));
10702 
10703   EXPECT_EQ("// foo bar baz bazfoo\n"
10704             "// foo bar baz bazfoo\n"
10705             "// bar foo bar\n",
10706             format("// foo bar baz      bazfoo\n"
10707                    "// foo bar baz      bazfoo bar\n"
10708                    "// foo bar\n",
10709                    Style));
10710 
10711   EXPECT_EQ("// foo bar baz bazfoo\n"
10712             "// foo bar baz bazfoo\n"
10713             "// bar foo bar\n",
10714             format("// foo bar baz      bazfoo\n"
10715                    "// foo bar baz      bazfoo bar\n"
10716                    "// foo           bar\n",
10717                    Style));
10718 
10719   // Make sure we do not keep protruding characters if strict mode reflow is
10720   // cheaper than keeping protruding characters.
10721   Style.ColumnLimit = 21;
10722   EXPECT_EQ("// foo foo foo foo\n"
10723             "// foo foo foo foo\n"
10724             "// foo foo foo foo\n",
10725             format("// foo foo foo foo foo foo foo foo foo foo foo foo\n",
10726                            Style));
10727 
10728   EXPECT_EQ("int a = /* long block\n"
10729             "           comment */\n"
10730             "    42;",
10731             format("int a = /* long block comment */ 42;", Style));
10732 }
10733 
10734 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10735   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10736   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10737                                   << " differs from Style #0"
10738 
10739 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10740   SmallVector<FormatStyle, 3> Styles;
10741   Styles.resize(3);
10742 
10743   Styles[0] = getLLVMStyle();
10744   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10745   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10746   EXPECT_ALL_STYLES_EQUAL(Styles);
10747 
10748   Styles[0] = getGoogleStyle();
10749   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10750   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10751   EXPECT_ALL_STYLES_EQUAL(Styles);
10752 
10753   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10754   EXPECT_TRUE(
10755       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10756   EXPECT_TRUE(
10757       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10758   EXPECT_ALL_STYLES_EQUAL(Styles);
10759 
10760   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10761   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10762   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10763   EXPECT_ALL_STYLES_EQUAL(Styles);
10764 
10765   Styles[0] = getMozillaStyle();
10766   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10767   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10768   EXPECT_ALL_STYLES_EQUAL(Styles);
10769 
10770   Styles[0] = getWebKitStyle();
10771   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10772   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10773   EXPECT_ALL_STYLES_EQUAL(Styles);
10774 
10775   Styles[0] = getGNUStyle();
10776   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10777   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10778   EXPECT_ALL_STYLES_EQUAL(Styles);
10779 
10780   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10781 }
10782 
10783 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10784   SmallVector<FormatStyle, 8> Styles;
10785   Styles.resize(2);
10786 
10787   Styles[0] = getGoogleStyle();
10788   Styles[1] = getLLVMStyle();
10789   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10790   EXPECT_ALL_STYLES_EQUAL(Styles);
10791 
10792   Styles.resize(5);
10793   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10794   Styles[1] = getLLVMStyle();
10795   Styles[1].Language = FormatStyle::LK_JavaScript;
10796   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10797 
10798   Styles[2] = getLLVMStyle();
10799   Styles[2].Language = FormatStyle::LK_JavaScript;
10800   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10801                                   "BasedOnStyle: Google",
10802                                   &Styles[2])
10803                    .value());
10804 
10805   Styles[3] = getLLVMStyle();
10806   Styles[3].Language = FormatStyle::LK_JavaScript;
10807   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10808                                   "Language: JavaScript",
10809                                   &Styles[3])
10810                    .value());
10811 
10812   Styles[4] = getLLVMStyle();
10813   Styles[4].Language = FormatStyle::LK_JavaScript;
10814   EXPECT_EQ(0, parseConfiguration("---\n"
10815                                   "BasedOnStyle: LLVM\n"
10816                                   "IndentWidth: 123\n"
10817                                   "---\n"
10818                                   "BasedOnStyle: Google\n"
10819                                   "Language: JavaScript",
10820                                   &Styles[4])
10821                    .value());
10822   EXPECT_ALL_STYLES_EQUAL(Styles);
10823 }
10824 
10825 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10826   Style.FIELD = false;                                                         \
10827   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10828   EXPECT_TRUE(Style.FIELD);                                                    \
10829   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10830   EXPECT_FALSE(Style.FIELD);
10831 
10832 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10833 
10834 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10835   Style.STRUCT.FIELD = false;                                                  \
10836   EXPECT_EQ(0,                                                                 \
10837             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10838                 .value());                                                     \
10839   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10840   EXPECT_EQ(0,                                                                 \
10841             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10842                 .value());                                                     \
10843   EXPECT_FALSE(Style.STRUCT.FIELD);
10844 
10845 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10846   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10847 
10848 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10849   EXPECT_NE(VALUE, Style.FIELD);                                               \
10850   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10851   EXPECT_EQ(VALUE, Style.FIELD)
10852 
10853 TEST_F(FormatTest, ParsesConfigurationBools) {
10854   FormatStyle Style = {};
10855   Style.Language = FormatStyle::LK_Cpp;
10856   CHECK_PARSE_BOOL(AlignOperands);
10857   CHECK_PARSE_BOOL(AlignTrailingComments);
10858   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10859   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10860   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10861   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10862   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10863   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10864   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10865   CHECK_PARSE_BOOL(BinPackArguments);
10866   CHECK_PARSE_BOOL(BinPackParameters);
10867   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10868   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10869   CHECK_PARSE_BOOL(BreakStringLiterals);
10870   CHECK_PARSE_BOOL(CompactNamespaces);
10871   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10872   CHECK_PARSE_BOOL(DerivePointerAlignment);
10873   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10874   CHECK_PARSE_BOOL(DisableFormat);
10875   CHECK_PARSE_BOOL(IndentCaseLabels);
10876   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10877   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10878   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10879   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10880   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10881   CHECK_PARSE_BOOL(ReflowComments);
10882   CHECK_PARSE_BOOL(SortIncludes);
10883   CHECK_PARSE_BOOL(SortUsingDeclarations);
10884   CHECK_PARSE_BOOL(SpacesInParentheses);
10885   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10886   CHECK_PARSE_BOOL(SpacesInAngles);
10887   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10888   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10889   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10890   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10891   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10892   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10893   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
10894   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
10895   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
10896   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
10897 
10898   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10899   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10900   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10901   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10902   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10903   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10904   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10905   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10906   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10907   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10908   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10909   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10910   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10911   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10912   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10913 }
10914 
10915 #undef CHECK_PARSE_BOOL
10916 
10917 TEST_F(FormatTest, ParsesConfiguration) {
10918   FormatStyle Style = {};
10919   Style.Language = FormatStyle::LK_Cpp;
10920   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10921   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10922               ConstructorInitializerIndentWidth, 1234u);
10923   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10924   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10925   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10926   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10927               PenaltyBreakAssignment, 1234u);
10928   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10929               PenaltyBreakBeforeFirstCallParameter, 1234u);
10930   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
10931               PenaltyBreakTemplateDeclaration, 1234u);
10932   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10933   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10934               PenaltyReturnTypeOnItsOwnLine, 1234u);
10935   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10936               SpacesBeforeTrailingComments, 1234u);
10937   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10938   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10939   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10940 
10941   Style.PointerAlignment = FormatStyle::PAS_Middle;
10942   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10943               FormatStyle::PAS_Left);
10944   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10945               FormatStyle::PAS_Right);
10946   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10947               FormatStyle::PAS_Middle);
10948   // For backward compatibility:
10949   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10950               FormatStyle::PAS_Left);
10951   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10952               FormatStyle::PAS_Right);
10953   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10954               FormatStyle::PAS_Middle);
10955 
10956   Style.Standard = FormatStyle::LS_Auto;
10957   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10958   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10959   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10960   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10961   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10962 
10963   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10964   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10965               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10966   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10967               FormatStyle::BOS_None);
10968   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10969               FormatStyle::BOS_All);
10970   // For backward compatibility:
10971   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10972               FormatStyle::BOS_None);
10973   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10974               FormatStyle::BOS_All);
10975 
10976   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10977   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10978               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10979   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10980               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10981   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10982               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10983   // For backward compatibility:
10984   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10985               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10986 
10987   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
10988   CHECK_PARSE("BreakInheritanceList: BeforeComma",
10989               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
10990   CHECK_PARSE("BreakInheritanceList: AfterColon",
10991               BreakInheritanceList, FormatStyle::BILS_AfterColon);
10992   CHECK_PARSE("BreakInheritanceList: BeforeColon",
10993               BreakInheritanceList, FormatStyle::BILS_BeforeColon);
10994   // For backward compatibility:
10995   CHECK_PARSE("BreakBeforeInheritanceComma: true",
10996               BreakInheritanceList, FormatStyle::BILS_BeforeComma);
10997 
10998   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10999   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
11000               FormatStyle::BAS_Align);
11001   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
11002               FormatStyle::BAS_DontAlign);
11003   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
11004               FormatStyle::BAS_AlwaysBreak);
11005   // For backward compatibility:
11006   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
11007               FormatStyle::BAS_DontAlign);
11008   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
11009               FormatStyle::BAS_Align);
11010 
11011   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11012   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
11013               FormatStyle::ENAS_DontAlign);
11014   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
11015               FormatStyle::ENAS_Left);
11016   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
11017               FormatStyle::ENAS_Right);
11018   // For backward compatibility:
11019   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
11020               FormatStyle::ENAS_Left);
11021   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
11022               FormatStyle::ENAS_Right);
11023 
11024   Style.UseTab = FormatStyle::UT_ForIndentation;
11025   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
11026   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
11027   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
11028   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
11029               FormatStyle::UT_ForContinuationAndIndentation);
11030   // For backward compatibility:
11031   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
11032   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
11033 
11034   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11035   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
11036               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11037   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
11038               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
11039   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
11040               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
11041   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
11042               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11043   // For backward compatibility:
11044   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
11045               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
11046   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
11047               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
11048 
11049   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
11050   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
11051               FormatStyle::SBPO_Never);
11052   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
11053               FormatStyle::SBPO_Always);
11054   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
11055               FormatStyle::SBPO_ControlStatements);
11056   // For backward compatibility:
11057   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
11058               FormatStyle::SBPO_Never);
11059   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
11060               FormatStyle::SBPO_ControlStatements);
11061 
11062   Style.ColumnLimit = 123;
11063   FormatStyle BaseStyle = getLLVMStyle();
11064   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
11065   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
11066 
11067   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
11068   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
11069               FormatStyle::BS_Attach);
11070   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
11071               FormatStyle::BS_Linux);
11072   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
11073               FormatStyle::BS_Mozilla);
11074   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
11075               FormatStyle::BS_Stroustrup);
11076   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
11077               FormatStyle::BS_Allman);
11078   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
11079   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
11080               FormatStyle::BS_WebKit);
11081   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
11082               FormatStyle::BS_Custom);
11083 
11084   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11085   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
11086               FormatStyle::RTBS_None);
11087   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
11088               FormatStyle::RTBS_All);
11089   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
11090               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
11091   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
11092               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
11093   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
11094               AlwaysBreakAfterReturnType,
11095               FormatStyle::RTBS_TopLevelDefinitions);
11096 
11097   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11098   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations,
11099               FormatStyle::BTDS_No);
11100   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations,
11101               FormatStyle::BTDS_MultiLine);
11102   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations,
11103               FormatStyle::BTDS_Yes);
11104   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations,
11105               FormatStyle::BTDS_MultiLine);
11106   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations,
11107               FormatStyle::BTDS_Yes);
11108 
11109   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
11110   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
11111               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
11112   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
11113               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
11114   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
11115               AlwaysBreakAfterDefinitionReturnType,
11116               FormatStyle::DRTBS_TopLevel);
11117 
11118   Style.NamespaceIndentation = FormatStyle::NI_All;
11119   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
11120               FormatStyle::NI_None);
11121   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
11122               FormatStyle::NI_Inner);
11123   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
11124               FormatStyle::NI_All);
11125 
11126   // FIXME: This is required because parsing a configuration simply overwrites
11127   // the first N elements of the list instead of resetting it.
11128   Style.ForEachMacros.clear();
11129   std::vector<std::string> BoostForeach;
11130   BoostForeach.push_back("BOOST_FOREACH");
11131   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
11132   std::vector<std::string> BoostAndQForeach;
11133   BoostAndQForeach.push_back("BOOST_FOREACH");
11134   BoostAndQForeach.push_back("Q_FOREACH");
11135   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
11136               BoostAndQForeach);
11137 
11138   Style.StatementMacros.clear();
11139   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
11140               std::vector<std::string>{"QUNUSED"});
11141   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
11142               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
11143 
11144   Style.IncludeStyle.IncludeCategories.clear();
11145   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
11146       {"abc/.*", 2}, {".*", 1}};
11147   CHECK_PARSE("IncludeCategories:\n"
11148               "  - Regex: abc/.*\n"
11149               "    Priority: 2\n"
11150               "  - Regex: .*\n"
11151               "    Priority: 1",
11152               IncludeStyle.IncludeCategories, ExpectedCategories);
11153   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
11154               "abc$");
11155 
11156   Style.RawStringFormats.clear();
11157   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
11158       {
11159           FormatStyle::LK_TextProto,
11160           {"pb", "proto"},
11161           {"PARSE_TEXT_PROTO"},
11162           /*CanonicalDelimiter=*/"",
11163           "llvm",
11164       },
11165       {
11166           FormatStyle::LK_Cpp,
11167           {"cc", "cpp"},
11168           {"C_CODEBLOCK", "CPPEVAL"},
11169           /*CanonicalDelimiter=*/"cc",
11170           /*BasedOnStyle=*/"",
11171       },
11172   };
11173 
11174   CHECK_PARSE("RawStringFormats:\n"
11175               "  - Language: TextProto\n"
11176               "    Delimiters:\n"
11177               "      - 'pb'\n"
11178               "      - 'proto'\n"
11179               "    EnclosingFunctions:\n"
11180               "      - 'PARSE_TEXT_PROTO'\n"
11181               "    BasedOnStyle: llvm\n"
11182               "  - Language: Cpp\n"
11183               "    Delimiters:\n"
11184               "      - 'cc'\n"
11185               "      - 'cpp'\n"
11186               "    EnclosingFunctions:\n"
11187               "      - 'C_CODEBLOCK'\n"
11188               "      - 'CPPEVAL'\n"
11189               "    CanonicalDelimiter: 'cc'",
11190               RawStringFormats, ExpectedRawStringFormats);
11191 }
11192 
11193 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
11194   FormatStyle Style = {};
11195   Style.Language = FormatStyle::LK_Cpp;
11196   CHECK_PARSE("Language: Cpp\n"
11197               "IndentWidth: 12",
11198               IndentWidth, 12u);
11199   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
11200                                "IndentWidth: 34",
11201                                &Style),
11202             ParseError::Unsuitable);
11203   EXPECT_EQ(12u, Style.IndentWidth);
11204   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11205   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11206 
11207   Style.Language = FormatStyle::LK_JavaScript;
11208   CHECK_PARSE("Language: JavaScript\n"
11209               "IndentWidth: 12",
11210               IndentWidth, 12u);
11211   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
11212   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
11213                                "IndentWidth: 34",
11214                                &Style),
11215             ParseError::Unsuitable);
11216   EXPECT_EQ(23u, Style.IndentWidth);
11217   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
11218   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11219 
11220   CHECK_PARSE("BasedOnStyle: LLVM\n"
11221               "IndentWidth: 67",
11222               IndentWidth, 67u);
11223 
11224   CHECK_PARSE("---\n"
11225               "Language: JavaScript\n"
11226               "IndentWidth: 12\n"
11227               "---\n"
11228               "Language: Cpp\n"
11229               "IndentWidth: 34\n"
11230               "...\n",
11231               IndentWidth, 12u);
11232 
11233   Style.Language = FormatStyle::LK_Cpp;
11234   CHECK_PARSE("---\n"
11235               "Language: JavaScript\n"
11236               "IndentWidth: 12\n"
11237               "---\n"
11238               "Language: Cpp\n"
11239               "IndentWidth: 34\n"
11240               "...\n",
11241               IndentWidth, 34u);
11242   CHECK_PARSE("---\n"
11243               "IndentWidth: 78\n"
11244               "---\n"
11245               "Language: JavaScript\n"
11246               "IndentWidth: 56\n"
11247               "...\n",
11248               IndentWidth, 78u);
11249 
11250   Style.ColumnLimit = 123;
11251   Style.IndentWidth = 234;
11252   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
11253   Style.TabWidth = 345;
11254   EXPECT_FALSE(parseConfiguration("---\n"
11255                                   "IndentWidth: 456\n"
11256                                   "BreakBeforeBraces: Allman\n"
11257                                   "---\n"
11258                                   "Language: JavaScript\n"
11259                                   "IndentWidth: 111\n"
11260                                   "TabWidth: 111\n"
11261                                   "---\n"
11262                                   "Language: Cpp\n"
11263                                   "BreakBeforeBraces: Stroustrup\n"
11264                                   "TabWidth: 789\n"
11265                                   "...\n",
11266                                   &Style));
11267   EXPECT_EQ(123u, Style.ColumnLimit);
11268   EXPECT_EQ(456u, Style.IndentWidth);
11269   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
11270   EXPECT_EQ(789u, Style.TabWidth);
11271 
11272   EXPECT_EQ(parseConfiguration("---\n"
11273                                "Language: JavaScript\n"
11274                                "IndentWidth: 56\n"
11275                                "---\n"
11276                                "IndentWidth: 78\n"
11277                                "...\n",
11278                                &Style),
11279             ParseError::Error);
11280   EXPECT_EQ(parseConfiguration("---\n"
11281                                "Language: JavaScript\n"
11282                                "IndentWidth: 56\n"
11283                                "---\n"
11284                                "Language: JavaScript\n"
11285                                "IndentWidth: 78\n"
11286                                "...\n",
11287                                &Style),
11288             ParseError::Error);
11289 
11290   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
11291 }
11292 
11293 #undef CHECK_PARSE
11294 
11295 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
11296   FormatStyle Style = {};
11297   Style.Language = FormatStyle::LK_JavaScript;
11298   Style.BreakBeforeTernaryOperators = true;
11299   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
11300   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11301 
11302   Style.BreakBeforeTernaryOperators = true;
11303   EXPECT_EQ(0, parseConfiguration("---\n"
11304                                   "BasedOnStyle: Google\n"
11305                                   "---\n"
11306                                   "Language: JavaScript\n"
11307                                   "IndentWidth: 76\n"
11308                                   "...\n",
11309                                   &Style)
11310                    .value());
11311   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
11312   EXPECT_EQ(76u, Style.IndentWidth);
11313   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
11314 }
11315 
11316 TEST_F(FormatTest, ConfigurationRoundTripTest) {
11317   FormatStyle Style = getLLVMStyle();
11318   std::string YAML = configurationAsText(Style);
11319   FormatStyle ParsedStyle = {};
11320   ParsedStyle.Language = FormatStyle::LK_Cpp;
11321   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
11322   EXPECT_EQ(Style, ParsedStyle);
11323 }
11324 
11325 TEST_F(FormatTest, WorksFor8bitEncodings) {
11326   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
11327             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
11328             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
11329             "\"\xef\xee\xf0\xf3...\"",
11330             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
11331                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
11332                    "\xef\xee\xf0\xf3...\"",
11333                    getLLVMStyleWithColumns(12)));
11334 }
11335 
11336 TEST_F(FormatTest, HandlesUTF8BOM) {
11337   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
11338   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
11339             format("\xef\xbb\xbf#include <iostream>"));
11340   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
11341             format("\xef\xbb\xbf\n#include <iostream>"));
11342 }
11343 
11344 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
11345 #if !defined(_MSC_VER)
11346 
11347 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
11348   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
11349                getLLVMStyleWithColumns(35));
11350   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
11351                getLLVMStyleWithColumns(31));
11352   verifyFormat("// Однажды в студёную зимнюю пору...",
11353                getLLVMStyleWithColumns(36));
11354   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
11355   verifyFormat("/* Однажды в студёную зимнюю пору... */",
11356                getLLVMStyleWithColumns(39));
11357   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
11358                getLLVMStyleWithColumns(35));
11359 }
11360 
11361 TEST_F(FormatTest, SplitsUTF8Strings) {
11362   // Non-printable characters' width is currently considered to be the length in
11363   // bytes in UTF8. The characters can be displayed in very different manner
11364   // (zero-width, single width with a substitution glyph, expanded to their code
11365   // (e.g. "<8d>"), so there's no single correct way to handle them.
11366   EXPECT_EQ("\"aaaaÄ\"\n"
11367             "\"\xc2\x8d\";",
11368             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11369   EXPECT_EQ("\"aaaaaaaÄ\"\n"
11370             "\"\xc2\x8d\";",
11371             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
11372   EXPECT_EQ("\"Однажды, в \"\n"
11373             "\"студёную \"\n"
11374             "\"зимнюю \"\n"
11375             "\"пору,\"",
11376             format("\"Однажды, в студёную зимнюю пору,\"",
11377                    getLLVMStyleWithColumns(13)));
11378   EXPECT_EQ(
11379       "\"一 二 三 \"\n"
11380       "\"四 五六 \"\n"
11381       "\"七 八 九 \"\n"
11382       "\"十\"",
11383       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
11384   EXPECT_EQ("\"一\t\"\n"
11385             "\"二 \t\"\n"
11386             "\"三 四 \"\n"
11387             "\"五\t\"\n"
11388             "\"六 \t\"\n"
11389             "\"七 \"\n"
11390             "\"八九十\tqq\"",
11391             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
11392                    getLLVMStyleWithColumns(11)));
11393 
11394   // UTF8 character in an escape sequence.
11395   EXPECT_EQ("\"aaaaaa\"\n"
11396             "\"\\\xC2\x8D\"",
11397             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
11398 }
11399 
11400 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
11401   EXPECT_EQ("const char *sssss =\n"
11402             "    \"一二三四五六七八\\\n"
11403             " 九 十\";",
11404             format("const char *sssss = \"一二三四五六七八\\\n"
11405                    " 九 十\";",
11406                    getLLVMStyleWithColumns(30)));
11407 }
11408 
11409 TEST_F(FormatTest, SplitsUTF8LineComments) {
11410   EXPECT_EQ("// aaaaÄ\xc2\x8d",
11411             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
11412   EXPECT_EQ("// Я из лесу\n"
11413             "// вышел; был\n"
11414             "// сильный\n"
11415             "// мороз.",
11416             format("// Я из лесу вышел; был сильный мороз.",
11417                    getLLVMStyleWithColumns(13)));
11418   EXPECT_EQ("// 一二三\n"
11419             "// 四五六七\n"
11420             "// 八  九\n"
11421             "// 十",
11422             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
11423 }
11424 
11425 TEST_F(FormatTest, SplitsUTF8BlockComments) {
11426   EXPECT_EQ("/* Гляжу,\n"
11427             " * поднимается\n"
11428             " * медленно в\n"
11429             " * гору\n"
11430             " * Лошадка,\n"
11431             " * везущая\n"
11432             " * хворосту\n"
11433             " * воз. */",
11434             format("/* Гляжу, поднимается медленно в гору\n"
11435                    " * Лошадка, везущая хворосту воз. */",
11436                    getLLVMStyleWithColumns(13)));
11437   EXPECT_EQ(
11438       "/* 一二三\n"
11439       " * 四五六七\n"
11440       " * 八  九\n"
11441       " * 十  */",
11442       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
11443   EXPECT_EQ("/* �������� ��������\n"
11444             " * ��������\n"
11445             " * ������-�� */",
11446             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
11447 }
11448 
11449 #endif // _MSC_VER
11450 
11451 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
11452   FormatStyle Style = getLLVMStyle();
11453 
11454   Style.ConstructorInitializerIndentWidth = 4;
11455   verifyFormat(
11456       "SomeClass::Constructor()\n"
11457       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11458       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11459       Style);
11460 
11461   Style.ConstructorInitializerIndentWidth = 2;
11462   verifyFormat(
11463       "SomeClass::Constructor()\n"
11464       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11465       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11466       Style);
11467 
11468   Style.ConstructorInitializerIndentWidth = 0;
11469   verifyFormat(
11470       "SomeClass::Constructor()\n"
11471       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
11472       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
11473       Style);
11474   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11475   verifyFormat(
11476       "SomeLongTemplateVariableName<\n"
11477       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
11478       Style);
11479   verifyFormat(
11480       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
11481       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11482       Style);
11483 }
11484 
11485 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
11486   FormatStyle Style = getLLVMStyle();
11487   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11488   Style.ConstructorInitializerIndentWidth = 4;
11489   verifyFormat("SomeClass::Constructor()\n"
11490                "    : a(a)\n"
11491                "    , b(b)\n"
11492                "    , c(c) {}",
11493                Style);
11494   verifyFormat("SomeClass::Constructor()\n"
11495                "    : a(a) {}",
11496                Style);
11497 
11498   Style.ColumnLimit = 0;
11499   verifyFormat("SomeClass::Constructor()\n"
11500                "    : a(a) {}",
11501                Style);
11502   verifyFormat("SomeClass::Constructor() noexcept\n"
11503                "    : a(a) {}",
11504                Style);
11505   verifyFormat("SomeClass::Constructor()\n"
11506                "    : a(a)\n"
11507                "    , b(b)\n"
11508                "    , c(c) {}",
11509                Style);
11510   verifyFormat("SomeClass::Constructor()\n"
11511                "    : a(a) {\n"
11512                "  foo();\n"
11513                "  bar();\n"
11514                "}",
11515                Style);
11516 
11517   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11518   verifyFormat("SomeClass::Constructor()\n"
11519                "    : a(a)\n"
11520                "    , b(b)\n"
11521                "    , c(c) {\n}",
11522                Style);
11523   verifyFormat("SomeClass::Constructor()\n"
11524                "    : a(a) {\n}",
11525                Style);
11526 
11527   Style.ColumnLimit = 80;
11528   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11529   Style.ConstructorInitializerIndentWidth = 2;
11530   verifyFormat("SomeClass::Constructor()\n"
11531                "  : a(a)\n"
11532                "  , b(b)\n"
11533                "  , c(c) {}",
11534                Style);
11535 
11536   Style.ConstructorInitializerIndentWidth = 0;
11537   verifyFormat("SomeClass::Constructor()\n"
11538                ": a(a)\n"
11539                ", b(b)\n"
11540                ", c(c) {}",
11541                Style);
11542 
11543   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
11544   Style.ConstructorInitializerIndentWidth = 4;
11545   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
11546   verifyFormat(
11547       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
11548       Style);
11549   verifyFormat(
11550       "SomeClass::Constructor()\n"
11551       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
11552       Style);
11553   Style.ConstructorInitializerIndentWidth = 4;
11554   Style.ColumnLimit = 60;
11555   verifyFormat("SomeClass::Constructor()\n"
11556                "    : aaaaaaaa(aaaaaaaa)\n"
11557                "    , aaaaaaaa(aaaaaaaa)\n"
11558                "    , aaaaaaaa(aaaaaaaa) {}",
11559                Style);
11560 }
11561 
11562 TEST_F(FormatTest, Destructors) {
11563   verifyFormat("void F(int &i) { i.~int(); }");
11564   verifyFormat("void F(int &i) { i->~int(); }");
11565 }
11566 
11567 TEST_F(FormatTest, FormatsWithWebKitStyle) {
11568   FormatStyle Style = getWebKitStyle();
11569 
11570   // Don't indent in outer namespaces.
11571   verifyFormat("namespace outer {\n"
11572                "int i;\n"
11573                "namespace inner {\n"
11574                "    int i;\n"
11575                "} // namespace inner\n"
11576                "} // namespace outer\n"
11577                "namespace other_outer {\n"
11578                "int i;\n"
11579                "}",
11580                Style);
11581 
11582   // Don't indent case labels.
11583   verifyFormat("switch (variable) {\n"
11584                "case 1:\n"
11585                "case 2:\n"
11586                "    doSomething();\n"
11587                "    break;\n"
11588                "default:\n"
11589                "    ++variable;\n"
11590                "}",
11591                Style);
11592 
11593   // Wrap before binary operators.
11594   EXPECT_EQ("void f()\n"
11595             "{\n"
11596             "    if (aaaaaaaaaaaaaaaa\n"
11597             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
11598             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11599             "        return;\n"
11600             "}",
11601             format("void f() {\n"
11602                    "if (aaaaaaaaaaaaaaaa\n"
11603                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
11604                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
11605                    "return;\n"
11606                    "}",
11607                    Style));
11608 
11609   // Allow functions on a single line.
11610   verifyFormat("void f() { return; }", Style);
11611 
11612   // Constructor initializers are formatted one per line with the "," on the
11613   // new line.
11614   verifyFormat("Constructor()\n"
11615                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11616                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
11617                "          aaaaaaaaaaaaaa)\n"
11618                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
11619                "{\n"
11620                "}",
11621                Style);
11622   verifyFormat("SomeClass::Constructor()\n"
11623                "    : a(a)\n"
11624                "{\n"
11625                "}",
11626                Style);
11627   EXPECT_EQ("SomeClass::Constructor()\n"
11628             "    : a(a)\n"
11629             "{\n"
11630             "}",
11631             format("SomeClass::Constructor():a(a){}", Style));
11632   verifyFormat("SomeClass::Constructor()\n"
11633                "    : a(a)\n"
11634                "    , b(b)\n"
11635                "    , c(c)\n"
11636                "{\n"
11637                "}",
11638                Style);
11639   verifyFormat("SomeClass::Constructor()\n"
11640                "    : a(a)\n"
11641                "{\n"
11642                "    foo();\n"
11643                "    bar();\n"
11644                "}",
11645                Style);
11646 
11647   // Access specifiers should be aligned left.
11648   verifyFormat("class C {\n"
11649                "public:\n"
11650                "    int i;\n"
11651                "};",
11652                Style);
11653 
11654   // Do not align comments.
11655   verifyFormat("int a; // Do not\n"
11656                "double b; // align comments.",
11657                Style);
11658 
11659   // Do not align operands.
11660   EXPECT_EQ("ASSERT(aaaa\n"
11661             "    || bbbb);",
11662             format("ASSERT ( aaaa\n||bbbb);", Style));
11663 
11664   // Accept input's line breaks.
11665   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
11666             "    || bbbbbbbbbbbbbbb) {\n"
11667             "    i++;\n"
11668             "}",
11669             format("if (aaaaaaaaaaaaaaa\n"
11670                    "|| bbbbbbbbbbbbbbb) { i++; }",
11671                    Style));
11672   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
11673             "    i++;\n"
11674             "}",
11675             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
11676 
11677   // Don't automatically break all macro definitions (llvm.org/PR17842).
11678   verifyFormat("#define aNumber 10", Style);
11679   // However, generally keep the line breaks that the user authored.
11680   EXPECT_EQ("#define aNumber \\\n"
11681             "    10",
11682             format("#define aNumber \\\n"
11683                    " 10",
11684                    Style));
11685 
11686   // Keep empty and one-element array literals on a single line.
11687   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
11688             "                                  copyItems:YES];",
11689             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
11690                    "copyItems:YES];",
11691                    Style));
11692   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
11693             "                                  copyItems:YES];",
11694             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
11695                    "             copyItems:YES];",
11696                    Style));
11697   // FIXME: This does not seem right, there should be more indentation before
11698   // the array literal's entries. Nested blocks have the same problem.
11699   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11700             "    @\"a\",\n"
11701             "    @\"a\"\n"
11702             "]\n"
11703             "                                  copyItems:YES];",
11704             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
11705                    "     @\"a\",\n"
11706                    "     @\"a\"\n"
11707                    "     ]\n"
11708                    "       copyItems:YES];",
11709                    Style));
11710   EXPECT_EQ(
11711       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11712       "                                  copyItems:YES];",
11713       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
11714              "   copyItems:YES];",
11715              Style));
11716 
11717   verifyFormat("[self.a b:c c:d];", Style);
11718   EXPECT_EQ("[self.a b:c\n"
11719             "        c:d];",
11720             format("[self.a b:c\n"
11721                    "c:d];",
11722                    Style));
11723 }
11724 
11725 TEST_F(FormatTest, FormatsLambdas) {
11726   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
11727   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
11728   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
11729   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
11730   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
11731   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
11732   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
11733   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
11734   verifyFormat("int x = f(*+[] {});");
11735   verifyFormat("void f() {\n"
11736                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
11737                "}\n");
11738   verifyFormat("void f() {\n"
11739                "  other(x.begin(), //\n"
11740                "        x.end(),   //\n"
11741                "        [&](int, int) { return 1; });\n"
11742                "}\n");
11743   verifyFormat("void f() {\n"
11744                "  other.other.other.other.other(\n"
11745                "      x.begin(), x.end(),\n"
11746                "      [something, rather](int, int, int, int, int, int, int) { return 1; });\n"
11747                "}\n");
11748   verifyFormat("void f() {\n"
11749                "  other.other.other.other.other(\n"
11750                "      x.begin(), x.end(),\n"
11751                "      [something, rather](int, int, int, int, int, int, int) {\n"
11752                "        //\n"
11753                "      });\n"
11754                "}\n");
11755   verifyFormat("SomeFunction([]() { // A cool function...\n"
11756                "  return 43;\n"
11757                "});");
11758   EXPECT_EQ("SomeFunction([]() {\n"
11759             "#define A a\n"
11760             "  return 43;\n"
11761             "});",
11762             format("SomeFunction([](){\n"
11763                    "#define A a\n"
11764                    "return 43;\n"
11765                    "});"));
11766   verifyFormat("void f() {\n"
11767                "  SomeFunction([](decltype(x), A *a) {});\n"
11768                "}");
11769   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11770                "    [](const aaaaaaaaaa &a) { return a; });");
11771   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
11772                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
11773                "});");
11774   verifyFormat("Constructor()\n"
11775                "    : Field([] { // comment\n"
11776                "        int i;\n"
11777                "      }) {}");
11778   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
11779                "  return some_parameter.size();\n"
11780                "};");
11781   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
11782                "    [](const string &s) { return s; };");
11783   verifyFormat("int i = aaaaaa ? 1 //\n"
11784                "               : [] {\n"
11785                "                   return 2; //\n"
11786                "                 }();");
11787   verifyFormat("llvm::errs() << \"number of twos is \"\n"
11788                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
11789                "                  return x == 2; // force break\n"
11790                "                });");
11791   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11792                "    [=](int iiiiiiiiiiii) {\n"
11793                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
11794                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
11795                "    });",
11796                getLLVMStyleWithColumns(60));
11797   verifyFormat("SomeFunction({[&] {\n"
11798                "                // comment\n"
11799                "              },\n"
11800                "              [&] {\n"
11801                "                // comment\n"
11802                "              }});");
11803   verifyFormat("SomeFunction({[&] {\n"
11804                "  // comment\n"
11805                "}});");
11806   verifyFormat("virtual aaaaaaaaaaaaaaaa(\n"
11807                "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
11808                "    aaaaa aaaaaaaaa);");
11809 
11810   // Lambdas with return types.
11811   verifyFormat("int c = []() -> int { return 2; }();\n");
11812   verifyFormat("int c = []() -> int * { return 2; }();\n");
11813   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
11814   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
11815   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
11816   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
11817   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11818   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11819   verifyFormat("[a, a]() -> a<1> {};");
11820   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11821                "                   int j) -> int {\n"
11822                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11823                "};");
11824   verifyFormat(
11825       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11826       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11827       "      return aaaaaaaaaaaaaaaaa;\n"
11828       "    });",
11829       getLLVMStyleWithColumns(70));
11830   verifyFormat("[]() //\n"
11831                "    -> int {\n"
11832                "  return 1; //\n"
11833                "};");
11834 
11835   // Multiple lambdas in the same parentheses change indentation rules. These
11836   // lambdas are forced to start on new lines.
11837   verifyFormat("SomeFunction(\n"
11838                "    []() {\n"
11839                "      //\n"
11840                "    },\n"
11841                "    []() {\n"
11842                "      //\n"
11843                "    });");
11844 
11845   // A lambda passed as arg0 is always pushed to the next line.
11846   verifyFormat("SomeFunction(\n"
11847                "    [this] {\n"
11848                "      //\n"
11849                "    },\n"
11850                "    1);\n");
11851 
11852   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0
11853   // case above.
11854   auto Style = getGoogleStyle();
11855   Style.BinPackArguments = false;
11856   verifyFormat("SomeFunction(\n"
11857                "    a,\n"
11858                "    [this] {\n"
11859                "      //\n"
11860                "    },\n"
11861                "    b);\n",
11862                Style);
11863   verifyFormat("SomeFunction(\n"
11864                "    a,\n"
11865                "    [this] {\n"
11866                "      //\n"
11867                "    },\n"
11868                "    b);\n");
11869 
11870   // A lambda with a very long line forces arg0 to be pushed out irrespective of
11871   // the BinPackArguments value (as long as the code is wide enough).
11872   verifyFormat("something->SomeFunction(\n"
11873                "    a,\n"
11874                "    [this] {\n"
11875                "      D0000000000000000000000000000000000000000000000000000000000001();\n"
11876                "    },\n"
11877                "    b);\n");
11878 
11879   // A multi-line lambda is pulled up as long as the introducer fits on the previous
11880   // line and there are no further args.
11881   verifyFormat("function(1, [this, that] {\n"
11882                "  //\n"
11883                "});\n");
11884   verifyFormat("function([this, that] {\n"
11885                "  //\n"
11886                "});\n");
11887   // FIXME: this format is not ideal and we should consider forcing the first arg
11888   // onto its own line.
11889   verifyFormat("function(a, b, c, //\n"
11890                "         d, [this, that] {\n"
11891                "           //\n"
11892                "         });\n");
11893 
11894   // Multiple lambdas are treated correctly even when there is a short arg0.
11895   verifyFormat("SomeFunction(\n"
11896                "    1,\n"
11897                "    [this] {\n"
11898                "      //\n"
11899                "    },\n"
11900                "    [this] {\n"
11901                "      //\n"
11902                "    },\n"
11903                "    1);\n");
11904 
11905   // More complex introducers.
11906   verifyFormat("return [i, args...] {};");
11907 
11908   // Not lambdas.
11909   verifyFormat("constexpr char hello[]{\"hello\"};");
11910   verifyFormat("double &operator[](int i) { return 0; }\n"
11911                "int i;");
11912   verifyFormat("std::unique_ptr<int[]> foo() {}");
11913   verifyFormat("int i = a[a][a]->f();");
11914   verifyFormat("int i = (*b)[a]->f();");
11915 
11916   // Other corner cases.
11917   verifyFormat("void f() {\n"
11918                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11919                "  );\n"
11920                "}");
11921 
11922   // Lambdas created through weird macros.
11923   verifyFormat("void f() {\n"
11924                "  MACRO((const AA &a) { return 1; });\n"
11925                "  MACRO((AA &a) { return 1; });\n"
11926                "}");
11927 
11928   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11929                "      doo_dah();\n"
11930                "      doo_dah();\n"
11931                "    })) {\n"
11932                "}");
11933   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
11934                "                doo_dah();\n"
11935                "                doo_dah();\n"
11936                "              })) {\n"
11937                "}");
11938   verifyFormat("auto lambda = []() {\n"
11939                "  int a = 2\n"
11940                "#if A\n"
11941                "          + 2\n"
11942                "#endif\n"
11943                "      ;\n"
11944                "};");
11945 
11946   // Lambdas with complex multiline introducers.
11947   verifyFormat(
11948       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11949       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
11950       "        -> ::std::unordered_set<\n"
11951       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
11952       "      //\n"
11953       "    });");
11954 }
11955 
11956 TEST_F(FormatTest, EmptyLinesInLambdas) {
11957   verifyFormat("auto lambda = []() {\n"
11958                "  x(); //\n"
11959                "};",
11960                "auto lambda = []() {\n"
11961                "\n"
11962                "  x(); //\n"
11963                "\n"
11964                "};");
11965 }
11966 
11967 TEST_F(FormatTest, FormatsBlocks) {
11968   FormatStyle ShortBlocks = getLLVMStyle();
11969   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11970   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11971   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11972   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11973   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11974   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11975   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11976 
11977   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11978   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11979   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11980 
11981   verifyFormat("[operation setCompletionBlock:^{\n"
11982                "  [self onOperationDone];\n"
11983                "}];");
11984   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11985                "  [self onOperationDone];\n"
11986                "}]};");
11987   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11988                "  f();\n"
11989                "}];");
11990   verifyFormat("int a = [operation block:^int(int *i) {\n"
11991                "  return 1;\n"
11992                "}];");
11993   verifyFormat("[myObject doSomethingWith:arg1\n"
11994                "                      aaa:^int(int *a) {\n"
11995                "                        return 1;\n"
11996                "                      }\n"
11997                "                      bbb:f(a * bbbbbbbb)];");
11998 
11999   verifyFormat("[operation setCompletionBlock:^{\n"
12000                "  [self.delegate newDataAvailable];\n"
12001                "}];",
12002                getLLVMStyleWithColumns(60));
12003   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
12004                "  NSString *path = [self sessionFilePath];\n"
12005                "  if (path) {\n"
12006                "    // ...\n"
12007                "  }\n"
12008                "});");
12009   verifyFormat("[[SessionService sharedService]\n"
12010                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12011                "      if (window) {\n"
12012                "        [self windowDidLoad:window];\n"
12013                "      } else {\n"
12014                "        [self errorLoadingWindow];\n"
12015                "      }\n"
12016                "    }];");
12017   verifyFormat("void (^largeBlock)(void) = ^{\n"
12018                "  // ...\n"
12019                "};\n",
12020                getLLVMStyleWithColumns(40));
12021   verifyFormat("[[SessionService sharedService]\n"
12022                "    loadWindowWithCompletionBlock: //\n"
12023                "        ^(SessionWindow *window) {\n"
12024                "          if (window) {\n"
12025                "            [self windowDidLoad:window];\n"
12026                "          } else {\n"
12027                "            [self errorLoadingWindow];\n"
12028                "          }\n"
12029                "        }];",
12030                getLLVMStyleWithColumns(60));
12031   verifyFormat("[myObject doSomethingWith:arg1\n"
12032                "    firstBlock:^(Foo *a) {\n"
12033                "      // ...\n"
12034                "      int i;\n"
12035                "    }\n"
12036                "    secondBlock:^(Bar *b) {\n"
12037                "      // ...\n"
12038                "      int i;\n"
12039                "    }\n"
12040                "    thirdBlock:^Foo(Bar *b) {\n"
12041                "      // ...\n"
12042                "      int i;\n"
12043                "    }];");
12044   verifyFormat("[myObject doSomethingWith:arg1\n"
12045                "               firstBlock:-1\n"
12046                "              secondBlock:^(Bar *b) {\n"
12047                "                // ...\n"
12048                "                int i;\n"
12049                "              }];");
12050 
12051   verifyFormat("f(^{\n"
12052                "  @autoreleasepool {\n"
12053                "    if (a) {\n"
12054                "      g();\n"
12055                "    }\n"
12056                "  }\n"
12057                "});");
12058   verifyFormat("Block b = ^int *(A *a, B *b) {}");
12059   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
12060                "};");
12061 
12062   FormatStyle FourIndent = getLLVMStyle();
12063   FourIndent.ObjCBlockIndentWidth = 4;
12064   verifyFormat("[operation setCompletionBlock:^{\n"
12065                "    [self onOperationDone];\n"
12066                "}];",
12067                FourIndent);
12068 }
12069 
12070 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
12071   FormatStyle ZeroColumn = getLLVMStyle();
12072   ZeroColumn.ColumnLimit = 0;
12073 
12074   verifyFormat("[[SessionService sharedService] "
12075                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12076                "  if (window) {\n"
12077                "    [self windowDidLoad:window];\n"
12078                "  } else {\n"
12079                "    [self errorLoadingWindow];\n"
12080                "  }\n"
12081                "}];",
12082                ZeroColumn);
12083   EXPECT_EQ("[[SessionService sharedService]\n"
12084             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12085             "      if (window) {\n"
12086             "        [self windowDidLoad:window];\n"
12087             "      } else {\n"
12088             "        [self errorLoadingWindow];\n"
12089             "      }\n"
12090             "    }];",
12091             format("[[SessionService sharedService]\n"
12092                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
12093                    "                if (window) {\n"
12094                    "    [self windowDidLoad:window];\n"
12095                    "  } else {\n"
12096                    "    [self errorLoadingWindow];\n"
12097                    "  }\n"
12098                    "}];",
12099                    ZeroColumn));
12100   verifyFormat("[myObject doSomethingWith:arg1\n"
12101                "    firstBlock:^(Foo *a) {\n"
12102                "      // ...\n"
12103                "      int i;\n"
12104                "    }\n"
12105                "    secondBlock:^(Bar *b) {\n"
12106                "      // ...\n"
12107                "      int i;\n"
12108                "    }\n"
12109                "    thirdBlock:^Foo(Bar *b) {\n"
12110                "      // ...\n"
12111                "      int i;\n"
12112                "    }];",
12113                ZeroColumn);
12114   verifyFormat("f(^{\n"
12115                "  @autoreleasepool {\n"
12116                "    if (a) {\n"
12117                "      g();\n"
12118                "    }\n"
12119                "  }\n"
12120                "});",
12121                ZeroColumn);
12122   verifyFormat("void (^largeBlock)(void) = ^{\n"
12123                "  // ...\n"
12124                "};",
12125                ZeroColumn);
12126 
12127   ZeroColumn.AllowShortBlocksOnASingleLine = true;
12128   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
12129             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12130   ZeroColumn.AllowShortBlocksOnASingleLine = false;
12131   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
12132             "  int i;\n"
12133             "};",
12134             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
12135 }
12136 
12137 TEST_F(FormatTest, SupportsCRLF) {
12138   EXPECT_EQ("int a;\r\n"
12139             "int b;\r\n"
12140             "int c;\r\n",
12141             format("int a;\r\n"
12142                    "  int b;\r\n"
12143                    "    int c;\r\n",
12144                    getLLVMStyle()));
12145   EXPECT_EQ("int a;\r\n"
12146             "int b;\r\n"
12147             "int c;\r\n",
12148             format("int a;\r\n"
12149                    "  int b;\n"
12150                    "    int c;\r\n",
12151                    getLLVMStyle()));
12152   EXPECT_EQ("int a;\n"
12153             "int b;\n"
12154             "int c;\n",
12155             format("int a;\r\n"
12156                    "  int b;\n"
12157                    "    int c;\n",
12158                    getLLVMStyle()));
12159   EXPECT_EQ("\"aaaaaaa \"\r\n"
12160             "\"bbbbbbb\";\r\n",
12161             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
12162   EXPECT_EQ("#define A \\\r\n"
12163             "  b;      \\\r\n"
12164             "  c;      \\\r\n"
12165             "  d;\r\n",
12166             format("#define A \\\r\n"
12167                    "  b; \\\r\n"
12168                    "  c; d; \r\n",
12169                    getGoogleStyle()));
12170 
12171   EXPECT_EQ("/*\r\n"
12172             "multi line block comments\r\n"
12173             "should not introduce\r\n"
12174             "an extra carriage return\r\n"
12175             "*/\r\n",
12176             format("/*\r\n"
12177                    "multi line block comments\r\n"
12178                    "should not introduce\r\n"
12179                    "an extra carriage return\r\n"
12180                    "*/\r\n"));
12181 }
12182 
12183 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
12184   verifyFormat("MY_CLASS(C) {\n"
12185                "  int i;\n"
12186                "  int j;\n"
12187                "};");
12188 }
12189 
12190 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
12191   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
12192   TwoIndent.ContinuationIndentWidth = 2;
12193 
12194   EXPECT_EQ("int i =\n"
12195             "  longFunction(\n"
12196             "    arg);",
12197             format("int i = longFunction(arg);", TwoIndent));
12198 
12199   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
12200   SixIndent.ContinuationIndentWidth = 6;
12201 
12202   EXPECT_EQ("int i =\n"
12203             "      longFunction(\n"
12204             "            arg);",
12205             format("int i = longFunction(arg);", SixIndent));
12206 }
12207 
12208 TEST_F(FormatTest, SpacesInAngles) {
12209   FormatStyle Spaces = getLLVMStyle();
12210   Spaces.SpacesInAngles = true;
12211 
12212   verifyFormat("static_cast< int >(arg);", Spaces);
12213   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
12214   verifyFormat("f< int, float >();", Spaces);
12215   verifyFormat("template <> g() {}", Spaces);
12216   verifyFormat("template < std::vector< int > > f() {}", Spaces);
12217   verifyFormat("std::function< void(int, int) > fct;", Spaces);
12218   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
12219                Spaces);
12220 
12221   Spaces.Standard = FormatStyle::LS_Cpp03;
12222   Spaces.SpacesInAngles = true;
12223   verifyFormat("A< A< int > >();", Spaces);
12224 
12225   Spaces.SpacesInAngles = false;
12226   verifyFormat("A<A<int> >();", Spaces);
12227 
12228   Spaces.Standard = FormatStyle::LS_Cpp11;
12229   Spaces.SpacesInAngles = true;
12230   verifyFormat("A< A< int > >();", Spaces);
12231 
12232   Spaces.SpacesInAngles = false;
12233   verifyFormat("A<A<int>>();", Spaces);
12234 }
12235 
12236 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
12237   FormatStyle Style = getLLVMStyle();
12238   Style.SpaceAfterTemplateKeyword = false;
12239   verifyFormat("template<int> void foo();", Style);
12240 }
12241 
12242 TEST_F(FormatTest, TripleAngleBrackets) {
12243   verifyFormat("f<<<1, 1>>>();");
12244   verifyFormat("f<<<1, 1, 1, s>>>();");
12245   verifyFormat("f<<<a, b, c, d>>>();");
12246   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
12247   verifyFormat("f<param><<<1, 1>>>();");
12248   verifyFormat("f<1><<<1, 1>>>();");
12249   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
12250   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12251                "aaaaaaaaaaa<<<\n    1, 1>>>();");
12252   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
12253                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
12254 }
12255 
12256 TEST_F(FormatTest, MergeLessLessAtEnd) {
12257   verifyFormat("<<");
12258   EXPECT_EQ("< < <", format("\\\n<<<"));
12259   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12260                "aaallvm::outs() <<");
12261   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12262                "aaaallvm::outs()\n    <<");
12263 }
12264 
12265 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
12266   std::string code = "#if A\n"
12267                      "#if B\n"
12268                      "a.\n"
12269                      "#endif\n"
12270                      "    a = 1;\n"
12271                      "#else\n"
12272                      "#endif\n"
12273                      "#if C\n"
12274                      "#else\n"
12275                      "#endif\n";
12276   EXPECT_EQ(code, format(code));
12277 }
12278 
12279 TEST_F(FormatTest, HandleConflictMarkers) {
12280   // Git/SVN conflict markers.
12281   EXPECT_EQ("int a;\n"
12282             "void f() {\n"
12283             "  callme(some(parameter1,\n"
12284             "<<<<<<< text by the vcs\n"
12285             "              parameter2),\n"
12286             "||||||| text by the vcs\n"
12287             "              parameter2),\n"
12288             "         parameter3,\n"
12289             "======= text by the vcs\n"
12290             "              parameter2, parameter3),\n"
12291             ">>>>>>> text by the vcs\n"
12292             "         otherparameter);\n",
12293             format("int a;\n"
12294                    "void f() {\n"
12295                    "  callme(some(parameter1,\n"
12296                    "<<<<<<< text by the vcs\n"
12297                    "  parameter2),\n"
12298                    "||||||| text by the vcs\n"
12299                    "  parameter2),\n"
12300                    "  parameter3,\n"
12301                    "======= text by the vcs\n"
12302                    "  parameter2,\n"
12303                    "  parameter3),\n"
12304                    ">>>>>>> text by the vcs\n"
12305                    "  otherparameter);\n"));
12306 
12307   // Perforce markers.
12308   EXPECT_EQ("void f() {\n"
12309             "  function(\n"
12310             ">>>> text by the vcs\n"
12311             "      parameter,\n"
12312             "==== text by the vcs\n"
12313             "      parameter,\n"
12314             "==== text by the vcs\n"
12315             "      parameter,\n"
12316             "<<<< text by the vcs\n"
12317             "      parameter);\n",
12318             format("void f() {\n"
12319                    "  function(\n"
12320                    ">>>> text by the vcs\n"
12321                    "  parameter,\n"
12322                    "==== text by the vcs\n"
12323                    "  parameter,\n"
12324                    "==== text by the vcs\n"
12325                    "  parameter,\n"
12326                    "<<<< text by the vcs\n"
12327                    "  parameter);\n"));
12328 
12329   EXPECT_EQ("<<<<<<<\n"
12330             "|||||||\n"
12331             "=======\n"
12332             ">>>>>>>",
12333             format("<<<<<<<\n"
12334                    "|||||||\n"
12335                    "=======\n"
12336                    ">>>>>>>"));
12337 
12338   EXPECT_EQ("<<<<<<<\n"
12339             "|||||||\n"
12340             "int i;\n"
12341             "=======\n"
12342             ">>>>>>>",
12343             format("<<<<<<<\n"
12344                    "|||||||\n"
12345                    "int i;\n"
12346                    "=======\n"
12347                    ">>>>>>>"));
12348 
12349   // FIXME: Handle parsing of macros around conflict markers correctly:
12350   EXPECT_EQ("#define Macro \\\n"
12351             "<<<<<<<\n"
12352             "Something \\\n"
12353             "|||||||\n"
12354             "Else \\\n"
12355             "=======\n"
12356             "Other \\\n"
12357             ">>>>>>>\n"
12358             "    End int i;\n",
12359             format("#define Macro \\\n"
12360                    "<<<<<<<\n"
12361                    "  Something \\\n"
12362                    "|||||||\n"
12363                    "  Else \\\n"
12364                    "=======\n"
12365                    "  Other \\\n"
12366                    ">>>>>>>\n"
12367                    "  End\n"
12368                    "int i;\n"));
12369 }
12370 
12371 TEST_F(FormatTest, DisableRegions) {
12372   EXPECT_EQ("int i;\n"
12373             "// clang-format off\n"
12374             "  int j;\n"
12375             "// clang-format on\n"
12376             "int k;",
12377             format(" int  i;\n"
12378                    "   // clang-format off\n"
12379                    "  int j;\n"
12380                    " // clang-format on\n"
12381                    "   int   k;"));
12382   EXPECT_EQ("int i;\n"
12383             "/* clang-format off */\n"
12384             "  int j;\n"
12385             "/* clang-format on */\n"
12386             "int k;",
12387             format(" int  i;\n"
12388                    "   /* clang-format off */\n"
12389                    "  int j;\n"
12390                    " /* clang-format on */\n"
12391                    "   int   k;"));
12392 
12393   // Don't reflow comments within disabled regions.
12394   EXPECT_EQ(
12395       "// clang-format off\n"
12396       "// long long long long long long line\n"
12397       "/* clang-format on */\n"
12398       "/* long long long\n"
12399       " * long long long\n"
12400       " * line */\n"
12401       "int i;\n"
12402       "/* clang-format off */\n"
12403       "/* long long long long long long line */\n",
12404       format("// clang-format off\n"
12405              "// long long long long long long line\n"
12406              "/* clang-format on */\n"
12407              "/* long long long long long long line */\n"
12408              "int i;\n"
12409              "/* clang-format off */\n"
12410              "/* long long long long long long line */\n",
12411              getLLVMStyleWithColumns(20)));
12412 }
12413 
12414 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
12415   format("? ) =");
12416   verifyNoCrash("#define a\\\n /**/}");
12417 }
12418 
12419 TEST_F(FormatTest, FormatsTableGenCode) {
12420   FormatStyle Style = getLLVMStyle();
12421   Style.Language = FormatStyle::LK_TableGen;
12422   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
12423 }
12424 
12425 TEST_F(FormatTest, ArrayOfTemplates) {
12426   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
12427             format("auto a = new unique_ptr<int > [ 10];"));
12428 
12429   FormatStyle Spaces = getLLVMStyle();
12430   Spaces.SpacesInSquareBrackets = true;
12431   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
12432             format("auto a = new unique_ptr<int > [10];", Spaces));
12433 }
12434 
12435 TEST_F(FormatTest, ArrayAsTemplateType) {
12436   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
12437             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
12438 
12439   FormatStyle Spaces = getLLVMStyle();
12440   Spaces.SpacesInSquareBrackets = true;
12441   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
12442             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
12443 }
12444 
12445 TEST_F(FormatTest, NoSpaceAfterSuper) {
12446     verifyFormat("__super::FooBar();");
12447 }
12448 
12449 TEST(FormatStyle, GetStyleWithEmptyFileName) {
12450   llvm::vfs::InMemoryFileSystem FS;
12451   auto Style1 = getStyle("file", "", "Google", "", &FS);
12452   ASSERT_TRUE((bool)Style1);
12453   ASSERT_EQ(*Style1, getGoogleStyle());
12454 }
12455 
12456 TEST(FormatStyle, GetStyleOfFile) {
12457   llvm::vfs::InMemoryFileSystem FS;
12458   // Test 1: format file in the same directory.
12459   ASSERT_TRUE(
12460       FS.addFile("/a/.clang-format", 0,
12461                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
12462   ASSERT_TRUE(
12463       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12464   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
12465   ASSERT_TRUE((bool)Style1);
12466   ASSERT_EQ(*Style1, getLLVMStyle());
12467 
12468   // Test 2.1: fallback to default.
12469   ASSERT_TRUE(
12470       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12471   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
12472   ASSERT_TRUE((bool)Style2);
12473   ASSERT_EQ(*Style2, getMozillaStyle());
12474 
12475   // Test 2.2: no format on 'none' fallback style.
12476   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12477   ASSERT_TRUE((bool)Style2);
12478   ASSERT_EQ(*Style2, getNoStyle());
12479 
12480   // Test 2.3: format if config is found with no based style while fallback is
12481   // 'none'.
12482   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
12483                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
12484   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
12485   ASSERT_TRUE((bool)Style2);
12486   ASSERT_EQ(*Style2, getLLVMStyle());
12487 
12488   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
12489   Style2 = getStyle("{}", "a.h", "none", "", &FS);
12490   ASSERT_TRUE((bool)Style2);
12491   ASSERT_EQ(*Style2, getLLVMStyle());
12492 
12493   // Test 3: format file in parent directory.
12494   ASSERT_TRUE(
12495       FS.addFile("/c/.clang-format", 0,
12496                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
12497   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
12498                          llvm::MemoryBuffer::getMemBuffer("int i;")));
12499   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
12500   ASSERT_TRUE((bool)Style3);
12501   ASSERT_EQ(*Style3, getGoogleStyle());
12502 
12503   // Test 4: error on invalid fallback style
12504   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
12505   ASSERT_FALSE((bool)Style4);
12506   llvm::consumeError(Style4.takeError());
12507 
12508   // Test 5: error on invalid yaml on command line
12509   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
12510   ASSERT_FALSE((bool)Style5);
12511   llvm::consumeError(Style5.takeError());
12512 
12513   // Test 6: error on invalid style
12514   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
12515   ASSERT_FALSE((bool)Style6);
12516   llvm::consumeError(Style6.takeError());
12517 
12518   // Test 7: found config file, error on parsing it
12519   ASSERT_TRUE(
12520       FS.addFile("/d/.clang-format", 0,
12521                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
12522                                                   "InvalidKey: InvalidValue")));
12523   ASSERT_TRUE(
12524       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
12525   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
12526   ASSERT_FALSE((bool)Style7);
12527   llvm::consumeError(Style7.takeError());
12528 }
12529 
12530 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
12531   // Column limit is 20.
12532   std::string Code = "Type *a =\n"
12533                      "    new Type();\n"
12534                      "g(iiiii, 0, jjjjj,\n"
12535                      "  0, kkkkk, 0, mm);\n"
12536                      "int  bad     = format   ;";
12537   std::string Expected = "auto a = new Type();\n"
12538                          "g(iiiii, nullptr,\n"
12539                          "  jjjjj, nullptr,\n"
12540                          "  kkkkk, nullptr,\n"
12541                          "  mm);\n"
12542                          "int  bad     = format   ;";
12543   FileID ID = Context.createInMemoryFile("format.cpp", Code);
12544   tooling::Replacements Replaces = toReplacements(
12545       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
12546                             "auto "),
12547        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
12548                             "nullptr"),
12549        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
12550                             "nullptr"),
12551        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
12552                             "nullptr")});
12553 
12554   format::FormatStyle Style = format::getLLVMStyle();
12555   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
12556   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12557   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12558       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12559   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12560   EXPECT_TRUE(static_cast<bool>(Result));
12561   EXPECT_EQ(Expected, *Result);
12562 }
12563 
12564 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
12565   std::string Code = "#include \"a.h\"\n"
12566                      "#include \"c.h\"\n"
12567                      "\n"
12568                      "int main() {\n"
12569                      "  return 0;\n"
12570                      "}";
12571   std::string Expected = "#include \"a.h\"\n"
12572                          "#include \"b.h\"\n"
12573                          "#include \"c.h\"\n"
12574                          "\n"
12575                          "int main() {\n"
12576                          "  return 0;\n"
12577                          "}";
12578   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
12579   tooling::Replacements Replaces = toReplacements(
12580       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
12581                             "#include \"b.h\"\n")});
12582 
12583   format::FormatStyle Style = format::getLLVMStyle();
12584   Style.SortIncludes = true;
12585   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
12586   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
12587       << llvm::toString(FormattedReplaces.takeError()) << "\n";
12588   auto Result = applyAllReplacements(Code, *FormattedReplaces);
12589   EXPECT_TRUE(static_cast<bool>(Result));
12590   EXPECT_EQ(Expected, *Result);
12591 }
12592 
12593 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
12594   EXPECT_EQ("using std::cin;\n"
12595             "using std::cout;",
12596             format("using std::cout;\n"
12597                    "using std::cin;", getGoogleStyle()));
12598 }
12599 
12600 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
12601   format::FormatStyle Style = format::getLLVMStyle();
12602   Style.Standard = FormatStyle::LS_Cpp03;
12603   // cpp03 recognize this string as identifier u8 and literal character 'a'
12604   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
12605 }
12606 
12607 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
12608   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
12609   // all modes, including C++11, C++14 and C++17
12610   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
12611 }
12612 
12613 TEST_F(FormatTest, DoNotFormatLikelyXml) {
12614   EXPECT_EQ("<!-- ;> -->",
12615             format("<!-- ;> -->", getGoogleStyle()));
12616   EXPECT_EQ(" <!-- >; -->",
12617             format(" <!-- >; -->", getGoogleStyle()));
12618 }
12619 
12620 TEST_F(FormatTest, StructuredBindings) {
12621   // Structured bindings is a C++17 feature.
12622   // all modes, including C++11, C++14 and C++17
12623   verifyFormat("auto [a, b] = f();");
12624   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
12625   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
12626   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
12627   EXPECT_EQ("auto const volatile [a, b] = f();",
12628             format("auto  const   volatile[a, b] = f();"));
12629   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
12630   EXPECT_EQ("auto &[a, b, c] = f();",
12631             format("auto   &[  a  ,  b,c   ] = f();"));
12632   EXPECT_EQ("auto &&[a, b, c] = f();",
12633             format("auto   &&[  a  ,  b,c   ] = f();"));
12634   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
12635   EXPECT_EQ("auto const volatile &&[a, b] = f();",
12636             format("auto  const  volatile  &&[a, b] = f();"));
12637   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
12638   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
12639   EXPECT_EQ("const auto volatile &&[a, b] = f();",
12640             format("const  auto   volatile  &&[a, b] = f();"));
12641   EXPECT_EQ("volatile const auto &&[a, b] = f();",
12642             format("volatile  const  auto   &&[a, b] = f();"));
12643   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
12644 
12645   // Make sure we don't mistake structured bindings for lambdas.
12646   FormatStyle PointerMiddle = getLLVMStyle();
12647   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12648   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
12649   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
12650   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
12651   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
12652   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
12653   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
12654   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
12655   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
12656   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
12657   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
12658   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
12659   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
12660 
12661   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
12662             format("for (const auto   &&   [a, b] : some_range) {\n}"));
12663   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
12664             format("for (const auto   &   [a, b] : some_range) {\n}"));
12665   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
12666             format("for (const auto[a, b] : some_range) {\n}"));
12667   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
12668   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
12669   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
12670   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
12671   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
12672   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
12673   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
12674   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
12675 
12676   format::FormatStyle Spaces = format::getLLVMStyle();
12677   Spaces.SpacesInSquareBrackets = true;
12678   verifyFormat("auto [ a, b ] = f();", Spaces);
12679   verifyFormat("auto &&[ a, b ] = f();", Spaces);
12680   verifyFormat("auto &[ a, b ] = f();", Spaces);
12681   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
12682   verifyFormat("auto const &[ a, b ] = f();", Spaces);
12683 }
12684 
12685 TEST_F(FormatTest, FileAndCode) {
12686   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
12687   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
12688   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
12689   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
12690   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
12691   EXPECT_EQ(
12692       FormatStyle::LK_ObjC,
12693       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
12694   EXPECT_EQ(FormatStyle::LK_ObjC,
12695             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
12696   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
12697   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
12698   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
12699   EXPECT_EQ(FormatStyle::LK_ObjC,
12700             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
12701   EXPECT_EQ(
12702       FormatStyle::LK_ObjC,
12703       guessLanguage("foo.h",
12704                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
12705   EXPECT_EQ(
12706       FormatStyle::LK_Cpp,
12707       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
12708 }
12709 
12710 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
12711   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
12712   EXPECT_EQ(FormatStyle::LK_ObjC,
12713             guessLanguage("foo.h", "array[[calculator getIndex]];"));
12714   EXPECT_EQ(FormatStyle::LK_Cpp,
12715             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
12716   EXPECT_EQ(
12717       FormatStyle::LK_Cpp,
12718       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
12719   EXPECT_EQ(FormatStyle::LK_ObjC,
12720             guessLanguage("foo.h", "[[noreturn foo] bar];"));
12721   EXPECT_EQ(FormatStyle::LK_Cpp,
12722             guessLanguage("foo.h", "[[clang::fallthrough]];"));
12723   EXPECT_EQ(FormatStyle::LK_ObjC,
12724             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
12725   EXPECT_EQ(FormatStyle::LK_Cpp,
12726             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
12727   EXPECT_EQ(FormatStyle::LK_Cpp,
12728             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
12729   EXPECT_EQ(FormatStyle::LK_ObjC,
12730             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
12731   EXPECT_EQ(FormatStyle::LK_Cpp,
12732             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
12733   EXPECT_EQ(
12734       FormatStyle::LK_Cpp,
12735       guessLanguage("foo.h",
12736                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
12737   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
12738 }
12739 
12740 TEST_F(FormatTest, GuessLanguageWithCaret) {
12741   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
12742   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
12743   EXPECT_EQ(FormatStyle::LK_ObjC,
12744             guessLanguage("foo.h", "int(^)(char, float);"));
12745   EXPECT_EQ(FormatStyle::LK_ObjC,
12746             guessLanguage("foo.h", "int(^foo)(char, float);"));
12747   EXPECT_EQ(FormatStyle::LK_ObjC,
12748             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
12749   EXPECT_EQ(FormatStyle::LK_ObjC,
12750             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
12751   EXPECT_EQ(
12752       FormatStyle::LK_ObjC,
12753       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
12754 }
12755 
12756 TEST_F(FormatTest, GuessLanguageWithChildLines) {
12757   EXPECT_EQ(FormatStyle::LK_Cpp,
12758             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
12759   EXPECT_EQ(FormatStyle::LK_ObjC,
12760             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12761   EXPECT_EQ(
12762       FormatStyle::LK_Cpp,
12763       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12764   EXPECT_EQ(
12765       FormatStyle::LK_ObjC,
12766       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
12767 }
12768 
12769 } // end namespace
12770 } // end namespace format
12771 } // end namespace clang
12772