1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Format/Format.h"
10 
11 #include "../Tooling/ReplacementTest.h"
12 #include "FormatTestUtils.h"
13 
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "gtest/gtest.h"
17 
18 #define DEBUG_TYPE "format-test"
19 
20 using clang::tooling::ReplacementTest;
21 using clang::tooling::toReplacements;
22 using testing::ScopedTrace;
23 
24 namespace clang {
25 namespace format {
26 namespace {
27 
28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
29 
30 class FormatTest : public ::testing::Test {
31 protected:
32   enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
33 
34   std::string format(llvm::StringRef Code,
35                      const FormatStyle &Style = getLLVMStyle(),
36                      StatusCheck CheckComplete = SC_ExpectComplete) {
37     LLVM_DEBUG(llvm::errs() << "---\n");
38     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
39     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
40     FormattingAttemptStatus Status;
41     tooling::Replacements Replaces =
42         reformat(Style, Code, Ranges, "<stdin>", &Status);
43     if (CheckComplete != SC_DoNotCheck) {
44       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
45       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
46           << Code << "\n\n";
47     }
48     ReplacementCount = Replaces.size();
49     auto Result = applyAllReplacements(Code, Replaces);
50     EXPECT_TRUE(static_cast<bool>(Result));
51     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
52     return *Result;
53   }
54 
55   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56     Style.ColumnLimit = ColumnLimit;
57     return Style;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62   }
63 
64   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66   }
67 
68   void _verifyFormat(const char *File, int Line, llvm::StringRef Expected,
69                      llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
72     EXPECT_EQ(Expected.str(), format(Expected, Style))
73         << "Expected code is not stable";
74     EXPECT_EQ(Expected.str(), format(Code, Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     _verifyFormat(File, Line, Code, test::messUp(Code), Style);
87   }
88 
89   void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code,
90                                const FormatStyle &Style = getLLVMStyle()) {
91     ScopedTrace t(File, Line, ::testing::Message() << Code.str());
92     EXPECT_EQ(Code.str(),
93               format(test::messUp(Code), Style, SC_ExpectIncomplete));
94   }
95 
96   void _verifyIndependentOfContext(const char *File, int Line,
97                                    llvm::StringRef Text,
98                                    const FormatStyle &Style = getLLVMStyle()) {
99     _verifyFormat(File, Line, Text, Style);
100     _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(),
101                   Style);
102   }
103 
104   /// \brief Verify that clang-format does not crash on the given input.
105   void verifyNoCrash(llvm::StringRef Code,
106                      const FormatStyle &Style = getLLVMStyle()) {
107     format(Code, Style, SC_DoNotCheck);
108   }
109 
110   int ReplacementCount;
111 };
112 
113 #define verifyIndependentOfContext(...)                                        \
114   _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__)
115 #define verifyIncompleteFormat(...)                                            \
116   _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle())
119 
120 TEST_F(FormatTest, MessUp) {
121   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
122   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
123   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
124   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
125   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
126 }
127 
128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
129   EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
130 }
131 
132 TEST_F(FormatTest, LLVMStyleOverride) {
133   EXPECT_EQ(FormatStyle::LK_Proto,
134             getLLVMStyle(FormatStyle::LK_Proto).Language);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // Basic function tests.
139 //===----------------------------------------------------------------------===//
140 
141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
142   EXPECT_EQ(";", format(";"));
143 }
144 
145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
146   EXPECT_EQ("int i;", format("  int i;"));
147   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
148   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
149   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
150 }
151 
152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
153   EXPECT_EQ("int i;", format("int\ni;"));
154 }
155 
156 TEST_F(FormatTest, FormatsNestedBlockStatements) {
157   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
158 }
159 
160 TEST_F(FormatTest, FormatsNestedCall) {
161   verifyFormat("Method(f1, f2(f3));");
162   verifyFormat("Method(f1(f2, f3()));");
163   verifyFormat("Method(f1(f2, (f3())));");
164 }
165 
166 TEST_F(FormatTest, NestedNameSpecifiers) {
167   verifyFormat("vector<::Type> v;");
168   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
169   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
170   verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
171   verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
172   verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
173   verifyFormat("bool a = 2 < ::SomeFunction();");
174   verifyFormat("ALWAYS_INLINE ::std::string getName();");
175   verifyFormat("some::string getName();");
176 }
177 
178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
179   EXPECT_EQ("if (a) {\n"
180             "  f();\n"
181             "}",
182             format("if(a){f();}"));
183   EXPECT_EQ(4, ReplacementCount);
184   EXPECT_EQ("if (a) {\n"
185             "  f();\n"
186             "}",
187             format("if (a) {\n"
188                    "  f();\n"
189                    "}"));
190   EXPECT_EQ(0, ReplacementCount);
191   EXPECT_EQ("/*\r\n"
192             "\r\n"
193             "*/\r\n",
194             format("/*\r\n"
195                    "\r\n"
196                    "*/\r\n"));
197   EXPECT_EQ(0, ReplacementCount);
198 }
199 
200 TEST_F(FormatTest, RemovesEmptyLines) {
201   EXPECT_EQ("class C {\n"
202             "  int i;\n"
203             "};",
204             format("class C {\n"
205                    " int i;\n"
206                    "\n"
207                    "};"));
208 
209   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
210   EXPECT_EQ("namespace N {\n"
211             "\n"
212             "int i;\n"
213             "}",
214             format("namespace N {\n"
215                    "\n"
216                    "int    i;\n"
217                    "}",
218                    getGoogleStyle()));
219   EXPECT_EQ("/* something */ namespace N {\n"
220             "\n"
221             "int i;\n"
222             "}",
223             format("/* something */ namespace N {\n"
224                    "\n"
225                    "int    i;\n"
226                    "}",
227                    getGoogleStyle()));
228   EXPECT_EQ("inline namespace N {\n"
229             "\n"
230             "int i;\n"
231             "}",
232             format("inline namespace N {\n"
233                    "\n"
234                    "int    i;\n"
235                    "}",
236                    getGoogleStyle()));
237   EXPECT_EQ("/* something */ inline namespace N {\n"
238             "\n"
239             "int i;\n"
240             "}",
241             format("/* something */ inline namespace N {\n"
242                    "\n"
243                    "int    i;\n"
244                    "}",
245                    getGoogleStyle()));
246   EXPECT_EQ("export namespace N {\n"
247             "\n"
248             "int i;\n"
249             "}",
250             format("export namespace N {\n"
251                    "\n"
252                    "int    i;\n"
253                    "}",
254                    getGoogleStyle()));
255   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
256             "\n"
257             "int i;\n"
258             "}",
259             format("extern /**/ \"C\" /**/ {\n"
260                    "\n"
261                    "int    i;\n"
262                    "}",
263                    getGoogleStyle()));
264 
265   // ...but do keep inlining and removing empty lines for non-block extern "C"
266   // functions.
267   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
268   EXPECT_EQ("extern \"C\" int f() {\n"
269             "  int i = 42;\n"
270             "  return i;\n"
271             "}",
272             format("extern \"C\" int f() {\n"
273                    "\n"
274                    "  int i = 42;\n"
275                    "  return i;\n"
276                    "}",
277                    getGoogleStyle()));
278 
279   // Remove empty lines at the beginning and end of blocks.
280   EXPECT_EQ("void f() {\n"
281             "\n"
282             "  if (a) {\n"
283             "\n"
284             "    f();\n"
285             "  }\n"
286             "}",
287             format("void f() {\n"
288                    "\n"
289                    "  if (a) {\n"
290                    "\n"
291                    "    f();\n"
292                    "\n"
293                    "  }\n"
294                    "\n"
295                    "}",
296                    getLLVMStyle()));
297   EXPECT_EQ("void f() {\n"
298             "  if (a) {\n"
299             "    f();\n"
300             "  }\n"
301             "}",
302             format("void f() {\n"
303                    "\n"
304                    "  if (a) {\n"
305                    "\n"
306                    "    f();\n"
307                    "\n"
308                    "  }\n"
309                    "\n"
310                    "}",
311                    getGoogleStyle()));
312 
313   // Don't remove empty lines in more complex control statements.
314   EXPECT_EQ("void f() {\n"
315             "  if (a) {\n"
316             "    f();\n"
317             "\n"
318             "  } else if (b) {\n"
319             "    f();\n"
320             "  }\n"
321             "}",
322             format("void f() {\n"
323                    "  if (a) {\n"
324                    "    f();\n"
325                    "\n"
326                    "  } else if (b) {\n"
327                    "    f();\n"
328                    "\n"
329                    "  }\n"
330                    "\n"
331                    "}"));
332 
333   // Don't remove empty lines before namespace endings.
334   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
335   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
336   EXPECT_EQ("namespace {\n"
337             "int i;\n"
338             "\n"
339             "}",
340             format("namespace {\n"
341                    "int i;\n"
342                    "\n"
343                    "}",
344                    LLVMWithNoNamespaceFix));
345   EXPECT_EQ("namespace {\n"
346             "int i;\n"
347             "}",
348             format("namespace {\n"
349                    "int i;\n"
350                    "}",
351                    LLVMWithNoNamespaceFix));
352   EXPECT_EQ("namespace {\n"
353             "int i;\n"
354             "\n"
355             "};",
356             format("namespace {\n"
357                    "int i;\n"
358                    "\n"
359                    "};",
360                    LLVMWithNoNamespaceFix));
361   EXPECT_EQ("namespace {\n"
362             "int i;\n"
363             "};",
364             format("namespace {\n"
365                    "int i;\n"
366                    "};",
367                    LLVMWithNoNamespaceFix));
368   EXPECT_EQ("namespace {\n"
369             "int i;\n"
370             "\n"
371             "}",
372             format("namespace {\n"
373                    "int i;\n"
374                    "\n"
375                    "}"));
376   EXPECT_EQ("namespace {\n"
377             "int i;\n"
378             "\n"
379             "} // namespace",
380             format("namespace {\n"
381                    "int i;\n"
382                    "\n"
383                    "}  // namespace"));
384 
385   FormatStyle Style = getLLVMStyle();
386   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
387   Style.MaxEmptyLinesToKeep = 2;
388   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
389   Style.BraceWrapping.AfterClass = true;
390   Style.BraceWrapping.AfterFunction = true;
391   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
392 
393   EXPECT_EQ("class Foo\n"
394             "{\n"
395             "  Foo() {}\n"
396             "\n"
397             "  void funk() {}\n"
398             "};",
399             format("class Foo\n"
400                    "{\n"
401                    "  Foo()\n"
402                    "  {\n"
403                    "  }\n"
404                    "\n"
405                    "  void funk() {}\n"
406                    "};",
407                    Style));
408 }
409 
410 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
411   verifyFormat("x = (a) and (b);");
412   verifyFormat("x = (a) or (b);");
413   verifyFormat("x = (a) bitand (b);");
414   verifyFormat("x = (a) bitor (b);");
415   verifyFormat("x = (a) not_eq (b);");
416   verifyFormat("x = (a) and_eq (b);");
417   verifyFormat("x = (a) or_eq (b);");
418   verifyFormat("x = (a) xor (b);");
419 }
420 
421 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
422   verifyFormat("x = compl(a);");
423   verifyFormat("x = not(a);");
424   verifyFormat("x = bitand(a);");
425   // Unary operator must not be merged with the next identifier
426   verifyFormat("x = compl a;");
427   verifyFormat("x = not a;");
428   verifyFormat("x = bitand a;");
429 }
430 
431 //===----------------------------------------------------------------------===//
432 // Tests for control statements.
433 //===----------------------------------------------------------------------===//
434 
435 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
436   verifyFormat("if (true)\n  f();\ng();");
437   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
438   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
439   verifyFormat("if constexpr (true)\n"
440                "  f();\ng();");
441   verifyFormat("if CONSTEXPR (true)\n"
442                "  f();\ng();");
443   verifyFormat("if constexpr (a)\n"
444                "  if constexpr (b)\n"
445                "    if constexpr (c)\n"
446                "      g();\n"
447                "h();");
448   verifyFormat("if CONSTEXPR (a)\n"
449                "  if CONSTEXPR (b)\n"
450                "    if CONSTEXPR (c)\n"
451                "      g();\n"
452                "h();");
453   verifyFormat("if constexpr (a)\n"
454                "  if constexpr (b) {\n"
455                "    f();\n"
456                "  }\n"
457                "g();");
458   verifyFormat("if CONSTEXPR (a)\n"
459                "  if CONSTEXPR (b) {\n"
460                "    f();\n"
461                "  }\n"
462                "g();");
463 
464   verifyFormat("if (a)\n"
465                "  g();");
466   verifyFormat("if (a) {\n"
467                "  g()\n"
468                "};");
469   verifyFormat("if (a)\n"
470                "  g();\n"
471                "else\n"
472                "  g();");
473   verifyFormat("if (a) {\n"
474                "  g();\n"
475                "} else\n"
476                "  g();");
477   verifyFormat("if (a)\n"
478                "  g();\n"
479                "else {\n"
480                "  g();\n"
481                "}");
482   verifyFormat("if (a) {\n"
483                "  g();\n"
484                "} else {\n"
485                "  g();\n"
486                "}");
487   verifyFormat("if (a)\n"
488                "  g();\n"
489                "else if (b)\n"
490                "  g();\n"
491                "else\n"
492                "  g();");
493   verifyFormat("if (a) {\n"
494                "  g();\n"
495                "} else if (b)\n"
496                "  g();\n"
497                "else\n"
498                "  g();");
499   verifyFormat("if (a)\n"
500                "  g();\n"
501                "else if (b) {\n"
502                "  g();\n"
503                "} else\n"
504                "  g();");
505   verifyFormat("if (a)\n"
506                "  g();\n"
507                "else if (b)\n"
508                "  g();\n"
509                "else {\n"
510                "  g();\n"
511                "}");
512   verifyFormat("if (a)\n"
513                "  g();\n"
514                "else if (b) {\n"
515                "  g();\n"
516                "} else {\n"
517                "  g();\n"
518                "}");
519   verifyFormat("if (a) {\n"
520                "  g();\n"
521                "} else if (b) {\n"
522                "  g();\n"
523                "} else {\n"
524                "  g();\n"
525                "}");
526 
527   FormatStyle AllowsMergedIf = getLLVMStyle();
528   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
529   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
530       FormatStyle::SIS_WithoutElse;
531   verifyFormat("if (a)\n"
532                "  // comment\n"
533                "  f();",
534                AllowsMergedIf);
535   verifyFormat("{\n"
536                "  if (a)\n"
537                "  label:\n"
538                "    f();\n"
539                "}",
540                AllowsMergedIf);
541   verifyFormat("#define A \\\n"
542                "  if (a)  \\\n"
543                "  label:  \\\n"
544                "    f()",
545                AllowsMergedIf);
546   verifyFormat("if (a)\n"
547                "  ;",
548                AllowsMergedIf);
549   verifyFormat("if (a)\n"
550                "  if (b) return;",
551                AllowsMergedIf);
552 
553   verifyFormat("if (a) // Can't merge this\n"
554                "  f();\n",
555                AllowsMergedIf);
556   verifyFormat("if (a) /* still don't merge */\n"
557                "  f();",
558                AllowsMergedIf);
559   verifyFormat("if (a) { // Never merge this\n"
560                "  f();\n"
561                "}",
562                AllowsMergedIf);
563   verifyFormat("if (a) { /* Never merge this */\n"
564                "  f();\n"
565                "}",
566                AllowsMergedIf);
567 
568   AllowsMergedIf.ColumnLimit = 14;
569   verifyFormat("if (a) return;", AllowsMergedIf);
570   verifyFormat("if (aaaaaaaaa)\n"
571                "  return;",
572                AllowsMergedIf);
573 
574   AllowsMergedIf.ColumnLimit = 13;
575   verifyFormat("if (a)\n  return;", AllowsMergedIf);
576 
577   FormatStyle AllowsMergedIfElse = getLLVMStyle();
578   AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
579       FormatStyle::SIS_AllIfsAndElse;
580   verifyFormat("if (a)\n"
581                "  // comment\n"
582                "  f();\n"
583                "else\n"
584                "  // comment\n"
585                "  f();",
586                AllowsMergedIfElse);
587   verifyFormat("{\n"
588                "  if (a)\n"
589                "  label:\n"
590                "    f();\n"
591                "  else\n"
592                "  label:\n"
593                "    f();\n"
594                "}",
595                AllowsMergedIfElse);
596   verifyFormat("if (a)\n"
597                "  ;\n"
598                "else\n"
599                "  ;",
600                AllowsMergedIfElse);
601   verifyFormat("if (a) {\n"
602                "} else {\n"
603                "}",
604                AllowsMergedIfElse);
605   verifyFormat("if (a) return;\n"
606                "else if (b) return;\n"
607                "else return;",
608                AllowsMergedIfElse);
609   verifyFormat("if (a) {\n"
610                "} else return;",
611                AllowsMergedIfElse);
612   verifyFormat("if (a) {\n"
613                "} else if (b) return;\n"
614                "else return;",
615                AllowsMergedIfElse);
616   verifyFormat("if (a) return;\n"
617                "else if (b) {\n"
618                "} else return;",
619                AllowsMergedIfElse);
620   verifyFormat("if (a)\n"
621                "  if (b) return;\n"
622                "  else return;",
623                AllowsMergedIfElse);
624   verifyFormat("if constexpr (a)\n"
625                "  if constexpr (b) return;\n"
626                "  else if constexpr (c) return;\n"
627                "  else return;",
628                AllowsMergedIfElse);
629 }
630 
631 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
632   FormatStyle AllowsMergedIf = getLLVMStyle();
633   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
634   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
635       FormatStyle::SIS_WithoutElse;
636   verifyFormat("if (a)\n"
637                "  f();\n"
638                "else {\n"
639                "  g();\n"
640                "}",
641                AllowsMergedIf);
642   verifyFormat("if (a)\n"
643                "  f();\n"
644                "else\n"
645                "  g();\n",
646                AllowsMergedIf);
647 
648   verifyFormat("if (a) g();", AllowsMergedIf);
649   verifyFormat("if (a) {\n"
650                "  g()\n"
651                "};",
652                AllowsMergedIf);
653   verifyFormat("if (a)\n"
654                "  g();\n"
655                "else\n"
656                "  g();",
657                AllowsMergedIf);
658   verifyFormat("if (a) {\n"
659                "  g();\n"
660                "} else\n"
661                "  g();",
662                AllowsMergedIf);
663   verifyFormat("if (a)\n"
664                "  g();\n"
665                "else {\n"
666                "  g();\n"
667                "}",
668                AllowsMergedIf);
669   verifyFormat("if (a) {\n"
670                "  g();\n"
671                "} else {\n"
672                "  g();\n"
673                "}",
674                AllowsMergedIf);
675   verifyFormat("if (a)\n"
676                "  g();\n"
677                "else if (b)\n"
678                "  g();\n"
679                "else\n"
680                "  g();",
681                AllowsMergedIf);
682   verifyFormat("if (a) {\n"
683                "  g();\n"
684                "} else if (b)\n"
685                "  g();\n"
686                "else\n"
687                "  g();",
688                AllowsMergedIf);
689   verifyFormat("if (a)\n"
690                "  g();\n"
691                "else if (b) {\n"
692                "  g();\n"
693                "} else\n"
694                "  g();",
695                AllowsMergedIf);
696   verifyFormat("if (a)\n"
697                "  g();\n"
698                "else if (b)\n"
699                "  g();\n"
700                "else {\n"
701                "  g();\n"
702                "}",
703                AllowsMergedIf);
704   verifyFormat("if (a)\n"
705                "  g();\n"
706                "else if (b) {\n"
707                "  g();\n"
708                "} else {\n"
709                "  g();\n"
710                "}",
711                AllowsMergedIf);
712   verifyFormat("if (a) {\n"
713                "  g();\n"
714                "} else if (b) {\n"
715                "  g();\n"
716                "} else {\n"
717                "  g();\n"
718                "}",
719                AllowsMergedIf);
720 
721   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
722       FormatStyle::SIS_OnlyFirstIf;
723 
724   verifyFormat("if (a) f();\n"
725                "else {\n"
726                "  g();\n"
727                "}",
728                AllowsMergedIf);
729   verifyFormat("if (a) f();\n"
730                "else {\n"
731                "  if (a) f();\n"
732                "  else {\n"
733                "    g();\n"
734                "  }\n"
735                "  g();\n"
736                "}",
737                AllowsMergedIf);
738 
739   verifyFormat("if (a) g();", AllowsMergedIf);
740   verifyFormat("if (a) {\n"
741                "  g()\n"
742                "};",
743                AllowsMergedIf);
744   verifyFormat("if (a) g();\n"
745                "else\n"
746                "  g();",
747                AllowsMergedIf);
748   verifyFormat("if (a) {\n"
749                "  g();\n"
750                "} else\n"
751                "  g();",
752                AllowsMergedIf);
753   verifyFormat("if (a) g();\n"
754                "else {\n"
755                "  g();\n"
756                "}",
757                AllowsMergedIf);
758   verifyFormat("if (a) {\n"
759                "  g();\n"
760                "} else {\n"
761                "  g();\n"
762                "}",
763                AllowsMergedIf);
764   verifyFormat("if (a) g();\n"
765                "else if (b)\n"
766                "  g();\n"
767                "else\n"
768                "  g();",
769                AllowsMergedIf);
770   verifyFormat("if (a) {\n"
771                "  g();\n"
772                "} else if (b)\n"
773                "  g();\n"
774                "else\n"
775                "  g();",
776                AllowsMergedIf);
777   verifyFormat("if (a) g();\n"
778                "else if (b) {\n"
779                "  g();\n"
780                "} else\n"
781                "  g();",
782                AllowsMergedIf);
783   verifyFormat("if (a) g();\n"
784                "else if (b)\n"
785                "  g();\n"
786                "else {\n"
787                "  g();\n"
788                "}",
789                AllowsMergedIf);
790   verifyFormat("if (a) g();\n"
791                "else if (b) {\n"
792                "  g();\n"
793                "} else {\n"
794                "  g();\n"
795                "}",
796                AllowsMergedIf);
797   verifyFormat("if (a) {\n"
798                "  g();\n"
799                "} else if (b) {\n"
800                "  g();\n"
801                "} else {\n"
802                "  g();\n"
803                "}",
804                AllowsMergedIf);
805 
806   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
807       FormatStyle::SIS_AllIfsAndElse;
808 
809   verifyFormat("if (a) f();\n"
810                "else {\n"
811                "  g();\n"
812                "}",
813                AllowsMergedIf);
814   verifyFormat("if (a) f();\n"
815                "else {\n"
816                "  if (a) f();\n"
817                "  else {\n"
818                "    g();\n"
819                "  }\n"
820                "  g();\n"
821                "}",
822                AllowsMergedIf);
823 
824   verifyFormat("if (a) g();", AllowsMergedIf);
825   verifyFormat("if (a) {\n"
826                "  g()\n"
827                "};",
828                AllowsMergedIf);
829   verifyFormat("if (a) g();\n"
830                "else g();",
831                AllowsMergedIf);
832   verifyFormat("if (a) {\n"
833                "  g();\n"
834                "} else g();",
835                AllowsMergedIf);
836   verifyFormat("if (a) g();\n"
837                "else {\n"
838                "  g();\n"
839                "}",
840                AllowsMergedIf);
841   verifyFormat("if (a) {\n"
842                "  g();\n"
843                "} else {\n"
844                "  g();\n"
845                "}",
846                AllowsMergedIf);
847   verifyFormat("if (a) g();\n"
848                "else if (b) g();\n"
849                "else g();",
850                AllowsMergedIf);
851   verifyFormat("if (a) {\n"
852                "  g();\n"
853                "} else if (b) g();\n"
854                "else g();",
855                AllowsMergedIf);
856   verifyFormat("if (a) g();\n"
857                "else if (b) {\n"
858                "  g();\n"
859                "} else g();",
860                AllowsMergedIf);
861   verifyFormat("if (a) g();\n"
862                "else if (b) g();\n"
863                "else {\n"
864                "  g();\n"
865                "}",
866                AllowsMergedIf);
867   verifyFormat("if (a) g();\n"
868                "else if (b) {\n"
869                "  g();\n"
870                "} else {\n"
871                "  g();\n"
872                "}",
873                AllowsMergedIf);
874   verifyFormat("if (a) {\n"
875                "  g();\n"
876                "} else if (b) {\n"
877                "  g();\n"
878                "} else {\n"
879                "  g();\n"
880                "}",
881                AllowsMergedIf);
882 }
883 
884 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
885   FormatStyle AllowsMergedLoops = getLLVMStyle();
886   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
887   verifyFormat("while (true) continue;", AllowsMergedLoops);
888   verifyFormat("for (;;) continue;", AllowsMergedLoops);
889   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
890   verifyFormat("while (true)\n"
891                "  ;",
892                AllowsMergedLoops);
893   verifyFormat("for (;;)\n"
894                "  ;",
895                AllowsMergedLoops);
896   verifyFormat("for (;;)\n"
897                "  for (;;) continue;",
898                AllowsMergedLoops);
899   verifyFormat("for (;;) // Can't merge this\n"
900                "  continue;",
901                AllowsMergedLoops);
902   verifyFormat("for (;;) /* still don't merge */\n"
903                "  continue;",
904                AllowsMergedLoops);
905   verifyFormat("do a++;\n"
906                "while (true);",
907                AllowsMergedLoops);
908   verifyFormat("do /* Don't merge */\n"
909                "  a++;\n"
910                "while (true);",
911                AllowsMergedLoops);
912   verifyFormat("do // Don't merge\n"
913                "  a++;\n"
914                "while (true);",
915                AllowsMergedLoops);
916   verifyFormat("do\n"
917                "  // Don't merge\n"
918                "  a++;\n"
919                "while (true);",
920                AllowsMergedLoops);
921   // Without braces labels are interpreted differently.
922   verifyFormat("{\n"
923                "  do\n"
924                "  label:\n"
925                "    a++;\n"
926                "  while (true);\n"
927                "}",
928                AllowsMergedLoops);
929 }
930 
931 TEST_F(FormatTest, FormatShortBracedStatements) {
932   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
933   AllowSimpleBracedStatements.ColumnLimit = 40;
934   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
935       FormatStyle::SBS_Always;
936 
937   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
938       FormatStyle::SIS_WithoutElse;
939   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
940 
941   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
942   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
943   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
944 
945   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
946   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
947   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
948   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
949   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
950   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
951   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
952   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
953   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
954   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
955   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
956                AllowSimpleBracedStatements);
957   verifyFormat("if (true) {\n"
958                "  ffffffffffffffffffffffff();\n"
959                "}",
960                AllowSimpleBracedStatements);
961   verifyFormat("if (true) {\n"
962                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
963                "}",
964                AllowSimpleBracedStatements);
965   verifyFormat("if (true) { //\n"
966                "  f();\n"
967                "}",
968                AllowSimpleBracedStatements);
969   verifyFormat("if (true) {\n"
970                "  f();\n"
971                "  f();\n"
972                "}",
973                AllowSimpleBracedStatements);
974   verifyFormat("if (true) {\n"
975                "  f();\n"
976                "} else {\n"
977                "  f();\n"
978                "}",
979                AllowSimpleBracedStatements);
980 
981   verifyFormat("struct A2 {\n"
982                "  int X;\n"
983                "};",
984                AllowSimpleBracedStatements);
985   verifyFormat("typedef struct A2 {\n"
986                "  int X;\n"
987                "} A2_t;",
988                AllowSimpleBracedStatements);
989   verifyFormat("template <int> struct A2 {\n"
990                "  struct B {};\n"
991                "};",
992                AllowSimpleBracedStatements);
993 
994   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
995       FormatStyle::SIS_Never;
996   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
997   verifyFormat("if (true) {\n"
998                "  f();\n"
999                "}",
1000                AllowSimpleBracedStatements);
1001   verifyFormat("if (true) {\n"
1002                "  f();\n"
1003                "} else {\n"
1004                "  f();\n"
1005                "}",
1006                AllowSimpleBracedStatements);
1007 
1008   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1009   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1010   verifyFormat("while (true) {\n"
1011                "  f();\n"
1012                "}",
1013                AllowSimpleBracedStatements);
1014   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1015   verifyFormat("for (;;) {\n"
1016                "  f();\n"
1017                "}",
1018                AllowSimpleBracedStatements);
1019 
1020   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1021       FormatStyle::SIS_WithoutElse;
1022   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1023   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1024       FormatStyle::BWACS_Always;
1025 
1026   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1027   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1028   verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1029   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1030   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1031   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1032   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1033   verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1034   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1035   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1036   verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1037                AllowSimpleBracedStatements);
1038   verifyFormat("if (true)\n"
1039                "{\n"
1040                "  ffffffffffffffffffffffff();\n"
1041                "}",
1042                AllowSimpleBracedStatements);
1043   verifyFormat("if (true)\n"
1044                "{\n"
1045                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1046                "}",
1047                AllowSimpleBracedStatements);
1048   verifyFormat("if (true)\n"
1049                "{ //\n"
1050                "  f();\n"
1051                "}",
1052                AllowSimpleBracedStatements);
1053   verifyFormat("if (true)\n"
1054                "{\n"
1055                "  f();\n"
1056                "  f();\n"
1057                "}",
1058                AllowSimpleBracedStatements);
1059   verifyFormat("if (true)\n"
1060                "{\n"
1061                "  f();\n"
1062                "} else\n"
1063                "{\n"
1064                "  f();\n"
1065                "}",
1066                AllowSimpleBracedStatements);
1067 
1068   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1069       FormatStyle::SIS_Never;
1070   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1071   verifyFormat("if (true)\n"
1072                "{\n"
1073                "  f();\n"
1074                "}",
1075                AllowSimpleBracedStatements);
1076   verifyFormat("if (true)\n"
1077                "{\n"
1078                "  f();\n"
1079                "} else\n"
1080                "{\n"
1081                "  f();\n"
1082                "}",
1083                AllowSimpleBracedStatements);
1084 
1085   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1086   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1087   verifyFormat("while (true)\n"
1088                "{\n"
1089                "  f();\n"
1090                "}",
1091                AllowSimpleBracedStatements);
1092   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1093   verifyFormat("for (;;)\n"
1094                "{\n"
1095                "  f();\n"
1096                "}",
1097                AllowSimpleBracedStatements);
1098 }
1099 
1100 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1101   FormatStyle Style = getLLVMStyleWithColumns(60);
1102   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1103   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1104   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1105   EXPECT_EQ("#define A                                                  \\\n"
1106             "  if (HANDLEwernufrnuLwrmviferuvnierv)                     \\\n"
1107             "  {                                                        \\\n"
1108             "    RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier;               \\\n"
1109             "  }\n"
1110             "X;",
1111             format("#define A \\\n"
1112                    "   if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1113                    "      RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1114                    "   }\n"
1115                    "X;",
1116                    Style));
1117 }
1118 
1119 TEST_F(FormatTest, ParseIfElse) {
1120   verifyFormat("if (true)\n"
1121                "  if (true)\n"
1122                "    if (true)\n"
1123                "      f();\n"
1124                "    else\n"
1125                "      g();\n"
1126                "  else\n"
1127                "    h();\n"
1128                "else\n"
1129                "  i();");
1130   verifyFormat("if (true)\n"
1131                "  if (true)\n"
1132                "    if (true) {\n"
1133                "      if (true)\n"
1134                "        f();\n"
1135                "    } else {\n"
1136                "      g();\n"
1137                "    }\n"
1138                "  else\n"
1139                "    h();\n"
1140                "else {\n"
1141                "  i();\n"
1142                "}");
1143   verifyFormat("if (true)\n"
1144                "  if constexpr (true)\n"
1145                "    if (true) {\n"
1146                "      if constexpr (true)\n"
1147                "        f();\n"
1148                "    } else {\n"
1149                "      g();\n"
1150                "    }\n"
1151                "  else\n"
1152                "    h();\n"
1153                "else {\n"
1154                "  i();\n"
1155                "}");
1156   verifyFormat("if (true)\n"
1157                "  if CONSTEXPR (true)\n"
1158                "    if (true) {\n"
1159                "      if CONSTEXPR (true)\n"
1160                "        f();\n"
1161                "    } else {\n"
1162                "      g();\n"
1163                "    }\n"
1164                "  else\n"
1165                "    h();\n"
1166                "else {\n"
1167                "  i();\n"
1168                "}");
1169   verifyFormat("void f() {\n"
1170                "  if (a) {\n"
1171                "  } else {\n"
1172                "  }\n"
1173                "}");
1174 }
1175 
1176 TEST_F(FormatTest, ElseIf) {
1177   verifyFormat("if (a) {\n} else if (b) {\n}");
1178   verifyFormat("if (a)\n"
1179                "  f();\n"
1180                "else if (b)\n"
1181                "  g();\n"
1182                "else\n"
1183                "  h();");
1184   verifyFormat("if constexpr (a)\n"
1185                "  f();\n"
1186                "else if constexpr (b)\n"
1187                "  g();\n"
1188                "else\n"
1189                "  h();");
1190   verifyFormat("if CONSTEXPR (a)\n"
1191                "  f();\n"
1192                "else if CONSTEXPR (b)\n"
1193                "  g();\n"
1194                "else\n"
1195                "  h();");
1196   verifyFormat("if (a) {\n"
1197                "  f();\n"
1198                "}\n"
1199                "// or else ..\n"
1200                "else {\n"
1201                "  g()\n"
1202                "}");
1203 
1204   verifyFormat("if (a) {\n"
1205                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1206                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1207                "}");
1208   verifyFormat("if (a) {\n"
1209                "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1210                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1211                "}");
1212   verifyFormat("if (a) {\n"
1213                "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1214                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1215                "}");
1216   verifyFormat("if (a) {\n"
1217                "} else if (\n"
1218                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1219                "}",
1220                getLLVMStyleWithColumns(62));
1221   verifyFormat("if (a) {\n"
1222                "} else if constexpr (\n"
1223                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1224                "}",
1225                getLLVMStyleWithColumns(62));
1226   verifyFormat("if (a) {\n"
1227                "} else if CONSTEXPR (\n"
1228                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1229                "}",
1230                getLLVMStyleWithColumns(62));
1231 }
1232 
1233 TEST_F(FormatTest, FormatsForLoop) {
1234   verifyFormat(
1235       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
1236       "     ++VeryVeryLongLoopVariable)\n"
1237       "  ;");
1238   verifyFormat("for (;;)\n"
1239                "  f();");
1240   verifyFormat("for (;;) {\n}");
1241   verifyFormat("for (;;) {\n"
1242                "  f();\n"
1243                "}");
1244   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
1245 
1246   verifyFormat(
1247       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
1248       "                                          E = UnwrappedLines.end();\n"
1249       "     I != E; ++I) {\n}");
1250 
1251   verifyFormat(
1252       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
1253       "     ++IIIII) {\n}");
1254   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
1255                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
1256                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
1257   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
1258                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
1259                "         E = FD->getDeclsInPrototypeScope().end();\n"
1260                "     I != E; ++I) {\n}");
1261   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
1262                "         I = Container.begin(),\n"
1263                "         E = Container.end();\n"
1264                "     I != E; ++I) {\n}",
1265                getLLVMStyleWithColumns(76));
1266 
1267   verifyFormat(
1268       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
1269       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
1270       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1271       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
1272       "     ++aaaaaaaaaaa) {\n}");
1273   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1274                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
1275                "     ++i) {\n}");
1276   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
1277                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
1278                "}");
1279   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
1280                "         aaaaaaaaaa);\n"
1281                "     iter; ++iter) {\n"
1282                "}");
1283   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1284                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
1285                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
1286                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
1287 
1288   // These should not be formatted as Objective-C for-in loops.
1289   verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
1290   verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
1291   verifyFormat("Foo *x;\nfor (x in y) {\n}");
1292   verifyFormat(
1293       "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
1294 
1295   FormatStyle NoBinPacking = getLLVMStyle();
1296   NoBinPacking.BinPackParameters = false;
1297   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
1298                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
1299                "                                           aaaaaaaaaaaaaaaa,\n"
1300                "                                           aaaaaaaaaaaaaaaa,\n"
1301                "                                           aaaaaaaaaaaaaaaa);\n"
1302                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
1303                "}",
1304                NoBinPacking);
1305   verifyFormat(
1306       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
1307       "                                          E = UnwrappedLines.end();\n"
1308       "     I != E;\n"
1309       "     ++I) {\n}",
1310       NoBinPacking);
1311 
1312   FormatStyle AlignLeft = getLLVMStyle();
1313   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
1314   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
1315 }
1316 
1317 TEST_F(FormatTest, RangeBasedForLoops) {
1318   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
1319                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1320   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
1321                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
1322   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
1323                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
1324   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
1325                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
1326 }
1327 
1328 TEST_F(FormatTest, ForEachLoops) {
1329   verifyFormat("void f() {\n"
1330                "  foreach (Item *item, itemlist) {}\n"
1331                "  Q_FOREACH (Item *item, itemlist) {}\n"
1332                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
1333                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
1334                "}");
1335 
1336   FormatStyle Style = getLLVMStyle();
1337   Style.SpaceBeforeParens =
1338       FormatStyle::SBPO_ControlStatementsExceptForEachMacros;
1339   verifyFormat("void f() {\n"
1340                "  foreach(Item *item, itemlist) {}\n"
1341                "  Q_FOREACH(Item *item, itemlist) {}\n"
1342                "  BOOST_FOREACH(Item *item, itemlist) {}\n"
1343                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
1344                "}",
1345                Style);
1346 
1347   // As function-like macros.
1348   verifyFormat("#define foreach(x, y)\n"
1349                "#define Q_FOREACH(x, y)\n"
1350                "#define BOOST_FOREACH(x, y)\n"
1351                "#define UNKNOWN_FOREACH(x, y)\n");
1352 
1353   // Not as function-like macros.
1354   verifyFormat("#define foreach (x, y)\n"
1355                "#define Q_FOREACH (x, y)\n"
1356                "#define BOOST_FOREACH (x, y)\n"
1357                "#define UNKNOWN_FOREACH (x, y)\n");
1358 
1359   // handle microsoft non standard extension
1360   verifyFormat("for each (char c in x->MyStringProperty)");
1361 }
1362 
1363 TEST_F(FormatTest, FormatsWhileLoop) {
1364   verifyFormat("while (true) {\n}");
1365   verifyFormat("while (true)\n"
1366                "  f();");
1367   verifyFormat("while () {\n}");
1368   verifyFormat("while () {\n"
1369                "  f();\n"
1370                "}");
1371 }
1372 
1373 TEST_F(FormatTest, FormatsDoWhile) {
1374   verifyFormat("do {\n"
1375                "  do_something();\n"
1376                "} while (something());");
1377   verifyFormat("do\n"
1378                "  do_something();\n"
1379                "while (something());");
1380 }
1381 
1382 TEST_F(FormatTest, FormatsSwitchStatement) {
1383   verifyFormat("switch (x) {\n"
1384                "case 1:\n"
1385                "  f();\n"
1386                "  break;\n"
1387                "case kFoo:\n"
1388                "case ns::kBar:\n"
1389                "case kBaz:\n"
1390                "  break;\n"
1391                "default:\n"
1392                "  g();\n"
1393                "  break;\n"
1394                "}");
1395   verifyFormat("switch (x) {\n"
1396                "case 1: {\n"
1397                "  f();\n"
1398                "  break;\n"
1399                "}\n"
1400                "case 2: {\n"
1401                "  break;\n"
1402                "}\n"
1403                "}");
1404   verifyFormat("switch (x) {\n"
1405                "case 1: {\n"
1406                "  f();\n"
1407                "  {\n"
1408                "    g();\n"
1409                "    h();\n"
1410                "  }\n"
1411                "  break;\n"
1412                "}\n"
1413                "}");
1414   verifyFormat("switch (x) {\n"
1415                "case 1: {\n"
1416                "  f();\n"
1417                "  if (foo) {\n"
1418                "    g();\n"
1419                "    h();\n"
1420                "  }\n"
1421                "  break;\n"
1422                "}\n"
1423                "}");
1424   verifyFormat("switch (x) {\n"
1425                "case 1: {\n"
1426                "  f();\n"
1427                "  g();\n"
1428                "} break;\n"
1429                "}");
1430   verifyFormat("switch (test)\n"
1431                "  ;");
1432   verifyFormat("switch (x) {\n"
1433                "default: {\n"
1434                "  // Do nothing.\n"
1435                "}\n"
1436                "}");
1437   verifyFormat("switch (x) {\n"
1438                "// comment\n"
1439                "// if 1, do f()\n"
1440                "case 1:\n"
1441                "  f();\n"
1442                "}");
1443   verifyFormat("switch (x) {\n"
1444                "case 1:\n"
1445                "  // Do amazing stuff\n"
1446                "  {\n"
1447                "    f();\n"
1448                "    g();\n"
1449                "  }\n"
1450                "  break;\n"
1451                "}");
1452   verifyFormat("#define A          \\\n"
1453                "  switch (x) {     \\\n"
1454                "  case a:          \\\n"
1455                "    foo = b;       \\\n"
1456                "  }",
1457                getLLVMStyleWithColumns(20));
1458   verifyFormat("#define OPERATION_CASE(name)           \\\n"
1459                "  case OP_name:                        \\\n"
1460                "    return operations::Operation##name\n",
1461                getLLVMStyleWithColumns(40));
1462   verifyFormat("switch (x) {\n"
1463                "case 1:;\n"
1464                "default:;\n"
1465                "  int i;\n"
1466                "}");
1467 
1468   verifyGoogleFormat("switch (x) {\n"
1469                      "  case 1:\n"
1470                      "    f();\n"
1471                      "    break;\n"
1472                      "  case kFoo:\n"
1473                      "  case ns::kBar:\n"
1474                      "  case kBaz:\n"
1475                      "    break;\n"
1476                      "  default:\n"
1477                      "    g();\n"
1478                      "    break;\n"
1479                      "}");
1480   verifyGoogleFormat("switch (x) {\n"
1481                      "  case 1: {\n"
1482                      "    f();\n"
1483                      "    break;\n"
1484                      "  }\n"
1485                      "}");
1486   verifyGoogleFormat("switch (test)\n"
1487                      "  ;");
1488 
1489   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
1490                      "  case OP_name:              \\\n"
1491                      "    return operations::Operation##name\n");
1492   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
1493                      "  // Get the correction operation class.\n"
1494                      "  switch (OpCode) {\n"
1495                      "    CASE(Add);\n"
1496                      "    CASE(Subtract);\n"
1497                      "    default:\n"
1498                      "      return operations::Unknown;\n"
1499                      "  }\n"
1500                      "#undef OPERATION_CASE\n"
1501                      "}");
1502   verifyFormat("DEBUG({\n"
1503                "  switch (x) {\n"
1504                "  case A:\n"
1505                "    f();\n"
1506                "    break;\n"
1507                "    // fallthrough\n"
1508                "  case B:\n"
1509                "    g();\n"
1510                "    break;\n"
1511                "  }\n"
1512                "});");
1513   EXPECT_EQ("DEBUG({\n"
1514             "  switch (x) {\n"
1515             "  case A:\n"
1516             "    f();\n"
1517             "    break;\n"
1518             "  // On B:\n"
1519             "  case B:\n"
1520             "    g();\n"
1521             "    break;\n"
1522             "  }\n"
1523             "});",
1524             format("DEBUG({\n"
1525                    "  switch (x) {\n"
1526                    "  case A:\n"
1527                    "    f();\n"
1528                    "    break;\n"
1529                    "  // On B:\n"
1530                    "  case B:\n"
1531                    "    g();\n"
1532                    "    break;\n"
1533                    "  }\n"
1534                    "});",
1535                    getLLVMStyle()));
1536   EXPECT_EQ("switch (n) {\n"
1537             "case 0: {\n"
1538             "  return false;\n"
1539             "}\n"
1540             "default: {\n"
1541             "  return true;\n"
1542             "}\n"
1543             "}",
1544             format("switch (n)\n"
1545                    "{\n"
1546                    "case 0: {\n"
1547                    "  return false;\n"
1548                    "}\n"
1549                    "default: {\n"
1550                    "  return true;\n"
1551                    "}\n"
1552                    "}",
1553                    getLLVMStyle()));
1554   verifyFormat("switch (a) {\n"
1555                "case (b):\n"
1556                "  return;\n"
1557                "}");
1558 
1559   verifyFormat("switch (a) {\n"
1560                "case some_namespace::\n"
1561                "    some_constant:\n"
1562                "  return;\n"
1563                "}",
1564                getLLVMStyleWithColumns(34));
1565 
1566   FormatStyle Style = getLLVMStyle();
1567   Style.IndentCaseLabels = true;
1568   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
1569   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1570   Style.BraceWrapping.AfterCaseLabel = true;
1571   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1572   EXPECT_EQ("switch (n)\n"
1573             "{\n"
1574             "  case 0:\n"
1575             "  {\n"
1576             "    return false;\n"
1577             "  }\n"
1578             "  default:\n"
1579             "  {\n"
1580             "    return true;\n"
1581             "  }\n"
1582             "}",
1583             format("switch (n) {\n"
1584                    "  case 0: {\n"
1585                    "    return false;\n"
1586                    "  }\n"
1587                    "  default: {\n"
1588                    "    return true;\n"
1589                    "  }\n"
1590                    "}",
1591                    Style));
1592   Style.BraceWrapping.AfterCaseLabel = false;
1593   EXPECT_EQ("switch (n)\n"
1594             "{\n"
1595             "  case 0: {\n"
1596             "    return false;\n"
1597             "  }\n"
1598             "  default: {\n"
1599             "    return true;\n"
1600             "  }\n"
1601             "}",
1602             format("switch (n) {\n"
1603                    "  case 0:\n"
1604                    "  {\n"
1605                    "    return false;\n"
1606                    "  }\n"
1607                    "  default:\n"
1608                    "  {\n"
1609                    "    return true;\n"
1610                    "  }\n"
1611                    "}",
1612                    Style));
1613   Style.IndentCaseLabels = false;
1614   Style.IndentCaseBlocks = true;
1615   EXPECT_EQ("switch (n)\n"
1616             "{\n"
1617             "case 0:\n"
1618             "  {\n"
1619             "    return false;\n"
1620             "  }\n"
1621             "case 1:\n"
1622             "  break;\n"
1623             "default:\n"
1624             "  {\n"
1625             "    return true;\n"
1626             "  }\n"
1627             "}",
1628             format("switch (n) {\n"
1629                    "case 0: {\n"
1630                    "  return false;\n"
1631                    "}\n"
1632                    "case 1:\n"
1633                    "  break;\n"
1634                    "default: {\n"
1635                    "  return true;\n"
1636                    "}\n"
1637                    "}",
1638                    Style));
1639   Style.IndentCaseLabels = true;
1640   Style.IndentCaseBlocks = true;
1641   EXPECT_EQ("switch (n)\n"
1642             "{\n"
1643             "  case 0:\n"
1644             "    {\n"
1645             "      return false;\n"
1646             "    }\n"
1647             "  case 1:\n"
1648             "    break;\n"
1649             "  default:\n"
1650             "    {\n"
1651             "      return true;\n"
1652             "    }\n"
1653             "}",
1654             format("switch (n) {\n"
1655                    "case 0: {\n"
1656                    "  return false;\n"
1657                    "}\n"
1658                    "case 1:\n"
1659                    "  break;\n"
1660                    "default: {\n"
1661                    "  return true;\n"
1662                    "}\n"
1663                    "}",
1664                    Style));
1665 }
1666 
1667 TEST_F(FormatTest, CaseRanges) {
1668   verifyFormat("switch (x) {\n"
1669                "case 'A' ... 'Z':\n"
1670                "case 1 ... 5:\n"
1671                "case a ... b:\n"
1672                "  break;\n"
1673                "}");
1674 }
1675 
1676 TEST_F(FormatTest, ShortEnums) {
1677   FormatStyle Style = getLLVMStyle();
1678   Style.AllowShortEnumsOnASingleLine = true;
1679   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
1680   Style.AllowShortEnumsOnASingleLine = false;
1681   verifyFormat("enum\n"
1682                "{\n"
1683                "  A,\n"
1684                "  B,\n"
1685                "  C\n"
1686                "} ShortEnum1, ShortEnum2;",
1687                Style);
1688 }
1689 
1690 TEST_F(FormatTest, ShortCaseLabels) {
1691   FormatStyle Style = getLLVMStyle();
1692   Style.AllowShortCaseLabelsOnASingleLine = true;
1693   verifyFormat("switch (a) {\n"
1694                "case 1: x = 1; break;\n"
1695                "case 2: return;\n"
1696                "case 3:\n"
1697                "case 4:\n"
1698                "case 5: return;\n"
1699                "case 6: // comment\n"
1700                "  return;\n"
1701                "case 7:\n"
1702                "  // comment\n"
1703                "  return;\n"
1704                "case 8:\n"
1705                "  x = 8; // comment\n"
1706                "  break;\n"
1707                "default: y = 1; break;\n"
1708                "}",
1709                Style);
1710   verifyFormat("switch (a) {\n"
1711                "case 0: return; // comment\n"
1712                "case 1: break;  // comment\n"
1713                "case 2: return;\n"
1714                "// comment\n"
1715                "case 3: return;\n"
1716                "// comment 1\n"
1717                "// comment 2\n"
1718                "// comment 3\n"
1719                "case 4: break; /* comment */\n"
1720                "case 5:\n"
1721                "  // comment\n"
1722                "  break;\n"
1723                "case 6: /* comment */ x = 1; break;\n"
1724                "case 7: x = /* comment */ 1; break;\n"
1725                "case 8:\n"
1726                "  x = 1; /* comment */\n"
1727                "  break;\n"
1728                "case 9:\n"
1729                "  break; // comment line 1\n"
1730                "         // comment line 2\n"
1731                "}",
1732                Style);
1733   EXPECT_EQ("switch (a) {\n"
1734             "case 1:\n"
1735             "  x = 8;\n"
1736             "  // fall through\n"
1737             "case 2: x = 8;\n"
1738             "// comment\n"
1739             "case 3:\n"
1740             "  return; /* comment line 1\n"
1741             "           * comment line 2 */\n"
1742             "case 4: i = 8;\n"
1743             "// something else\n"
1744             "#if FOO\n"
1745             "case 5: break;\n"
1746             "#endif\n"
1747             "}",
1748             format("switch (a) {\n"
1749                    "case 1: x = 8;\n"
1750                    "  // fall through\n"
1751                    "case 2:\n"
1752                    "  x = 8;\n"
1753                    "// comment\n"
1754                    "case 3:\n"
1755                    "  return; /* comment line 1\n"
1756                    "           * comment line 2 */\n"
1757                    "case 4:\n"
1758                    "  i = 8;\n"
1759                    "// something else\n"
1760                    "#if FOO\n"
1761                    "case 5: break;\n"
1762                    "#endif\n"
1763                    "}",
1764                    Style));
1765   EXPECT_EQ("switch (a) {\n"
1766             "case 0:\n"
1767             "  return; // long long long long long long long long long long "
1768             "long long comment\n"
1769             "          // line\n"
1770             "}",
1771             format("switch (a) {\n"
1772                    "case 0: return; // long long long long long long long long "
1773                    "long long long long comment line\n"
1774                    "}",
1775                    Style));
1776   EXPECT_EQ("switch (a) {\n"
1777             "case 0:\n"
1778             "  return; /* long long long long long long long long long long "
1779             "long long comment\n"
1780             "             line */\n"
1781             "}",
1782             format("switch (a) {\n"
1783                    "case 0: return; /* long long long long long long long long "
1784                    "long long long long comment line */\n"
1785                    "}",
1786                    Style));
1787   verifyFormat("switch (a) {\n"
1788                "#if FOO\n"
1789                "case 0: return 0;\n"
1790                "#endif\n"
1791                "}",
1792                Style);
1793   verifyFormat("switch (a) {\n"
1794                "case 1: {\n"
1795                "}\n"
1796                "case 2: {\n"
1797                "  return;\n"
1798                "}\n"
1799                "case 3: {\n"
1800                "  x = 1;\n"
1801                "  return;\n"
1802                "}\n"
1803                "case 4:\n"
1804                "  if (x)\n"
1805                "    return;\n"
1806                "}",
1807                Style);
1808   Style.ColumnLimit = 21;
1809   verifyFormat("switch (a) {\n"
1810                "case 1: x = 1; break;\n"
1811                "case 2: return;\n"
1812                "case 3:\n"
1813                "case 4:\n"
1814                "case 5: return;\n"
1815                "default:\n"
1816                "  y = 1;\n"
1817                "  break;\n"
1818                "}",
1819                Style);
1820   Style.ColumnLimit = 80;
1821   Style.AllowShortCaseLabelsOnASingleLine = false;
1822   Style.IndentCaseLabels = true;
1823   EXPECT_EQ("switch (n) {\n"
1824             "  default /*comments*/:\n"
1825             "    return true;\n"
1826             "  case 0:\n"
1827             "    return false;\n"
1828             "}",
1829             format("switch (n) {\n"
1830                    "default/*comments*/:\n"
1831                    "  return true;\n"
1832                    "case 0:\n"
1833                    "  return false;\n"
1834                    "}",
1835                    Style));
1836   Style.AllowShortCaseLabelsOnASingleLine = true;
1837   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1838   Style.BraceWrapping.AfterCaseLabel = true;
1839   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1840   EXPECT_EQ("switch (n)\n"
1841             "{\n"
1842             "  case 0:\n"
1843             "  {\n"
1844             "    return false;\n"
1845             "  }\n"
1846             "  default:\n"
1847             "  {\n"
1848             "    return true;\n"
1849             "  }\n"
1850             "}",
1851             format("switch (n) {\n"
1852                    "  case 0: {\n"
1853                    "    return false;\n"
1854                    "  }\n"
1855                    "  default:\n"
1856                    "  {\n"
1857                    "    return true;\n"
1858                    "  }\n"
1859                    "}",
1860                    Style));
1861 }
1862 
1863 TEST_F(FormatTest, FormatsLabels) {
1864   verifyFormat("void f() {\n"
1865                "  some_code();\n"
1866                "test_label:\n"
1867                "  some_other_code();\n"
1868                "  {\n"
1869                "    some_more_code();\n"
1870                "  another_label:\n"
1871                "    some_more_code();\n"
1872                "  }\n"
1873                "}");
1874   verifyFormat("{\n"
1875                "  some_code();\n"
1876                "test_label:\n"
1877                "  some_other_code();\n"
1878                "}");
1879   verifyFormat("{\n"
1880                "  some_code();\n"
1881                "test_label:;\n"
1882                "  int i = 0;\n"
1883                "}");
1884   FormatStyle Style = getLLVMStyle();
1885   Style.IndentGotoLabels = false;
1886   verifyFormat("void f() {\n"
1887                "  some_code();\n"
1888                "test_label:\n"
1889                "  some_other_code();\n"
1890                "  {\n"
1891                "    some_more_code();\n"
1892                "another_label:\n"
1893                "    some_more_code();\n"
1894                "  }\n"
1895                "}",
1896                Style);
1897   verifyFormat("{\n"
1898                "  some_code();\n"
1899                "test_label:\n"
1900                "  some_other_code();\n"
1901                "}",
1902                Style);
1903   verifyFormat("{\n"
1904                "  some_code();\n"
1905                "test_label:;\n"
1906                "  int i = 0;\n"
1907                "}");
1908 }
1909 
1910 TEST_F(FormatTest, MultiLineControlStatements) {
1911   FormatStyle Style = getLLVMStyle();
1912   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
1913   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
1914   Style.ColumnLimit = 20;
1915   // Short lines should keep opening brace on same line.
1916   EXPECT_EQ("if (foo) {\n"
1917             "  bar();\n"
1918             "}",
1919             format("if(foo){bar();}", Style));
1920   EXPECT_EQ("if (foo) {\n"
1921             "  bar();\n"
1922             "} else {\n"
1923             "  baz();\n"
1924             "}",
1925             format("if(foo){bar();}else{baz();}", Style));
1926   EXPECT_EQ("if (foo && bar) {\n"
1927             "  baz();\n"
1928             "}",
1929             format("if(foo&&bar){baz();}", Style));
1930   EXPECT_EQ("if (foo) {\n"
1931             "  bar();\n"
1932             "} else if (baz) {\n"
1933             "  quux();\n"
1934             "}",
1935             format("if(foo){bar();}else if(baz){quux();}", Style));
1936   EXPECT_EQ(
1937       "if (foo) {\n"
1938       "  bar();\n"
1939       "} else if (baz) {\n"
1940       "  quux();\n"
1941       "} else {\n"
1942       "  foobar();\n"
1943       "}",
1944       format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
1945   EXPECT_EQ("for (;;) {\n"
1946             "  foo();\n"
1947             "}",
1948             format("for(;;){foo();}"));
1949   EXPECT_EQ("while (1) {\n"
1950             "  foo();\n"
1951             "}",
1952             format("while(1){foo();}", Style));
1953   EXPECT_EQ("switch (foo) {\n"
1954             "case bar:\n"
1955             "  return;\n"
1956             "}",
1957             format("switch(foo){case bar:return;}", Style));
1958   EXPECT_EQ("try {\n"
1959             "  foo();\n"
1960             "} catch (...) {\n"
1961             "  bar();\n"
1962             "}",
1963             format("try{foo();}catch(...){bar();}", Style));
1964   EXPECT_EQ("do {\n"
1965             "  foo();\n"
1966             "} while (bar &&\n"
1967             "         baz);",
1968             format("do{foo();}while(bar&&baz);", Style));
1969   // Long lines should put opening brace on new line.
1970   EXPECT_EQ("if (foo && bar &&\n"
1971             "    baz)\n"
1972             "{\n"
1973             "  quux();\n"
1974             "}",
1975             format("if(foo&&bar&&baz){quux();}", Style));
1976   EXPECT_EQ("if (foo && bar &&\n"
1977             "    baz)\n"
1978             "{\n"
1979             "  quux();\n"
1980             "}",
1981             format("if (foo && bar &&\n"
1982                    "    baz) {\n"
1983                    "  quux();\n"
1984                    "}",
1985                    Style));
1986   EXPECT_EQ("if (foo) {\n"
1987             "  bar();\n"
1988             "} else if (baz ||\n"
1989             "           quux)\n"
1990             "{\n"
1991             "  foobar();\n"
1992             "}",
1993             format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
1994   EXPECT_EQ(
1995       "if (foo) {\n"
1996       "  bar();\n"
1997       "} else if (baz ||\n"
1998       "           quux)\n"
1999       "{\n"
2000       "  foobar();\n"
2001       "} else {\n"
2002       "  barbaz();\n"
2003       "}",
2004       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2005              Style));
2006   EXPECT_EQ("for (int i = 0;\n"
2007             "     i < 10; ++i)\n"
2008             "{\n"
2009             "  foo();\n"
2010             "}",
2011             format("for(int i=0;i<10;++i){foo();}", Style));
2012   EXPECT_EQ("foreach (int i,\n"
2013             "         list)\n"
2014             "{\n"
2015             "  foo();\n"
2016             "}",
2017             format("foreach(int i, list){foo();}", Style));
2018   Style.ColumnLimit =
2019       40; // to concentrate at brace wrapping, not line wrap due to column limit
2020   EXPECT_EQ("foreach (int i, list) {\n"
2021             "  foo();\n"
2022             "}",
2023             format("foreach(int i, list){foo();}", Style));
2024   Style.ColumnLimit =
2025       20; // to concentrate at brace wrapping, not line wrap due to column limit
2026   EXPECT_EQ("while (foo || bar ||\n"
2027             "       baz)\n"
2028             "{\n"
2029             "  quux();\n"
2030             "}",
2031             format("while(foo||bar||baz){quux();}", Style));
2032   EXPECT_EQ("switch (\n"
2033             "    foo = barbaz)\n"
2034             "{\n"
2035             "case quux:\n"
2036             "  return;\n"
2037             "}",
2038             format("switch(foo=barbaz){case quux:return;}", Style));
2039   EXPECT_EQ("try {\n"
2040             "  foo();\n"
2041             "} catch (\n"
2042             "    Exception &bar)\n"
2043             "{\n"
2044             "  baz();\n"
2045             "}",
2046             format("try{foo();}catch(Exception&bar){baz();}", Style));
2047   Style.ColumnLimit =
2048       40; // to concentrate at brace wrapping, not line wrap due to column limit
2049   EXPECT_EQ("try {\n"
2050             "  foo();\n"
2051             "} catch (Exception &bar) {\n"
2052             "  baz();\n"
2053             "}",
2054             format("try{foo();}catch(Exception&bar){baz();}", Style));
2055   Style.ColumnLimit =
2056       20; // to concentrate at brace wrapping, not line wrap due to column limit
2057 
2058   Style.BraceWrapping.BeforeElse = true;
2059   EXPECT_EQ(
2060       "if (foo) {\n"
2061       "  bar();\n"
2062       "}\n"
2063       "else if (baz ||\n"
2064       "         quux)\n"
2065       "{\n"
2066       "  foobar();\n"
2067       "}\n"
2068       "else {\n"
2069       "  barbaz();\n"
2070       "}",
2071       format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
2072              Style));
2073 
2074   Style.BraceWrapping.BeforeCatch = true;
2075   EXPECT_EQ("try {\n"
2076             "  foo();\n"
2077             "}\n"
2078             "catch (...) {\n"
2079             "  baz();\n"
2080             "}",
2081             format("try{foo();}catch(...){baz();}", Style));
2082 }
2083 
2084 TEST_F(FormatTest, BeforeWhile) {
2085   FormatStyle Style = getLLVMStyle();
2086   Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
2087 
2088   verifyFormat("do {\n"
2089                "  foo();\n"
2090                "} while (1);",
2091                Style);
2092   Style.BraceWrapping.BeforeWhile = true;
2093   verifyFormat("do {\n"
2094                "  foo();\n"
2095                "}\n"
2096                "while (1);",
2097                Style);
2098 }
2099 
2100 //===----------------------------------------------------------------------===//
2101 // Tests for classes, namespaces, etc.
2102 //===----------------------------------------------------------------------===//
2103 
2104 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
2105   verifyFormat("class A {};");
2106 }
2107 
2108 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
2109   verifyFormat("class A {\n"
2110                "public:\n"
2111                "public: // comment\n"
2112                "protected:\n"
2113                "private:\n"
2114                "  void f() {}\n"
2115                "};");
2116   verifyFormat("export class A {\n"
2117                "public:\n"
2118                "public: // comment\n"
2119                "protected:\n"
2120                "private:\n"
2121                "  void f() {}\n"
2122                "};");
2123   verifyGoogleFormat("class A {\n"
2124                      " public:\n"
2125                      " protected:\n"
2126                      " private:\n"
2127                      "  void f() {}\n"
2128                      "};");
2129   verifyGoogleFormat("export class A {\n"
2130                      " public:\n"
2131                      " protected:\n"
2132                      " private:\n"
2133                      "  void f() {}\n"
2134                      "};");
2135   verifyFormat("class A {\n"
2136                "public slots:\n"
2137                "  void f1() {}\n"
2138                "public Q_SLOTS:\n"
2139                "  void f2() {}\n"
2140                "protected slots:\n"
2141                "  void f3() {}\n"
2142                "protected Q_SLOTS:\n"
2143                "  void f4() {}\n"
2144                "private slots:\n"
2145                "  void f5() {}\n"
2146                "private Q_SLOTS:\n"
2147                "  void f6() {}\n"
2148                "signals:\n"
2149                "  void g1();\n"
2150                "Q_SIGNALS:\n"
2151                "  void g2();\n"
2152                "};");
2153 
2154   // Don't interpret 'signals' the wrong way.
2155   verifyFormat("signals.set();");
2156   verifyFormat("for (Signals signals : f()) {\n}");
2157   verifyFormat("{\n"
2158                "  signals.set(); // This needs indentation.\n"
2159                "}");
2160   verifyFormat("void f() {\n"
2161                "label:\n"
2162                "  signals.baz();\n"
2163                "}");
2164 }
2165 
2166 TEST_F(FormatTest, SeparatesLogicalBlocks) {
2167   EXPECT_EQ("class A {\n"
2168             "public:\n"
2169             "  void f();\n"
2170             "\n"
2171             "private:\n"
2172             "  void g() {}\n"
2173             "  // test\n"
2174             "protected:\n"
2175             "  int h;\n"
2176             "};",
2177             format("class A {\n"
2178                    "public:\n"
2179                    "void f();\n"
2180                    "private:\n"
2181                    "void g() {}\n"
2182                    "// test\n"
2183                    "protected:\n"
2184                    "int h;\n"
2185                    "};"));
2186   EXPECT_EQ("class A {\n"
2187             "protected:\n"
2188             "public:\n"
2189             "  void f();\n"
2190             "};",
2191             format("class A {\n"
2192                    "protected:\n"
2193                    "\n"
2194                    "public:\n"
2195                    "\n"
2196                    "  void f();\n"
2197                    "};"));
2198 
2199   // Even ensure proper spacing inside macros.
2200   EXPECT_EQ("#define B     \\\n"
2201             "  class A {   \\\n"
2202             "   protected: \\\n"
2203             "   public:    \\\n"
2204             "    void f(); \\\n"
2205             "  };",
2206             format("#define B     \\\n"
2207                    "  class A {   \\\n"
2208                    "   protected: \\\n"
2209                    "              \\\n"
2210                    "   public:    \\\n"
2211                    "              \\\n"
2212                    "    void f(); \\\n"
2213                    "  };",
2214                    getGoogleStyle()));
2215   // But don't remove empty lines after macros ending in access specifiers.
2216   EXPECT_EQ("#define A private:\n"
2217             "\n"
2218             "int i;",
2219             format("#define A         private:\n"
2220                    "\n"
2221                    "int              i;"));
2222 }
2223 
2224 TEST_F(FormatTest, FormatsClasses) {
2225   verifyFormat("class A : public B {};");
2226   verifyFormat("class A : public ::B {};");
2227 
2228   verifyFormat(
2229       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2230       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2231   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2232                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2233                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2234   verifyFormat(
2235       "class A : public B, public C, public D, public E, public F {};");
2236   verifyFormat("class AAAAAAAAAAAA : public B,\n"
2237                "                     public C,\n"
2238                "                     public D,\n"
2239                "                     public E,\n"
2240                "                     public F,\n"
2241                "                     public G {};");
2242 
2243   verifyFormat("class\n"
2244                "    ReallyReallyLongClassName {\n"
2245                "  int i;\n"
2246                "};",
2247                getLLVMStyleWithColumns(32));
2248   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2249                "                           aaaaaaaaaaaaaaaa> {};");
2250   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2251                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2252                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2253   verifyFormat("template <class R, class C>\n"
2254                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2255                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2256   verifyFormat("class ::A::B {};");
2257 }
2258 
2259 TEST_F(FormatTest, BreakInheritanceStyle) {
2260   FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
2261   StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
2262       FormatStyle::BILS_BeforeComma;
2263   verifyFormat("class MyClass : public X {};",
2264                StyleWithInheritanceBreakBeforeComma);
2265   verifyFormat("class MyClass\n"
2266                "    : public X\n"
2267                "    , public Y {};",
2268                StyleWithInheritanceBreakBeforeComma);
2269   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
2270                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
2271                "    , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
2272                StyleWithInheritanceBreakBeforeComma);
2273   verifyFormat("struct aaaaaaaaaaaaa\n"
2274                "    : public aaaaaaaaaaaaaaaaaaa< // break\n"
2275                "          aaaaaaaaaaaaaaaa> {};",
2276                StyleWithInheritanceBreakBeforeComma);
2277 
2278   FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
2279   StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
2280       FormatStyle::BILS_AfterColon;
2281   verifyFormat("class MyClass : public X {};",
2282                StyleWithInheritanceBreakAfterColon);
2283   verifyFormat("class MyClass : public X, public Y {};",
2284                StyleWithInheritanceBreakAfterColon);
2285   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
2286                "    public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2287                "    public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
2288                StyleWithInheritanceBreakAfterColon);
2289   verifyFormat("struct aaaaaaaaaaaaa :\n"
2290                "    public aaaaaaaaaaaaaaaaaaa< // break\n"
2291                "        aaaaaaaaaaaaaaaa> {};",
2292                StyleWithInheritanceBreakAfterColon);
2293 
2294   FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
2295   StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
2296       FormatStyle::BILS_AfterComma;
2297   verifyFormat("class MyClass : public X {};",
2298                StyleWithInheritanceBreakAfterComma);
2299   verifyFormat("class MyClass : public X,\n"
2300                "                public Y {};",
2301                StyleWithInheritanceBreakAfterComma);
2302   verifyFormat(
2303       "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2304       "                               public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
2305       "{};",
2306       StyleWithInheritanceBreakAfterComma);
2307   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2308                "                           aaaaaaaaaaaaaaaa> {};",
2309                StyleWithInheritanceBreakAfterComma);
2310   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2311                "    : public OnceBreak,\n"
2312                "      public AlwaysBreak,\n"
2313                "      EvenBasesFitInOneLine {};",
2314                StyleWithInheritanceBreakAfterComma);
2315 }
2316 
2317 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2318   verifyFormat("class A {\n} a, b;");
2319   verifyFormat("struct A {\n} a, b;");
2320   verifyFormat("union A {\n} a;");
2321 }
2322 
2323 TEST_F(FormatTest, FormatsEnum) {
2324   verifyFormat("enum {\n"
2325                "  Zero,\n"
2326                "  One = 1,\n"
2327                "  Two = One + 1,\n"
2328                "  Three = (One + Two),\n"
2329                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2330                "  Five = (One, Two, Three, Four, 5)\n"
2331                "};");
2332   verifyGoogleFormat("enum {\n"
2333                      "  Zero,\n"
2334                      "  One = 1,\n"
2335                      "  Two = One + 1,\n"
2336                      "  Three = (One + Two),\n"
2337                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2338                      "  Five = (One, Two, Three, Four, 5)\n"
2339                      "};");
2340   verifyFormat("enum Enum {};");
2341   verifyFormat("enum {};");
2342   verifyFormat("enum X E {} d;");
2343   verifyFormat("enum __attribute__((...)) E {} d;");
2344   verifyFormat("enum __declspec__((...)) E {} d;");
2345   verifyFormat("enum {\n"
2346                "  Bar = Foo<int, int>::value\n"
2347                "};",
2348                getLLVMStyleWithColumns(30));
2349 
2350   verifyFormat("enum ShortEnum { A, B, C };");
2351   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2352 
2353   EXPECT_EQ("enum KeepEmptyLines {\n"
2354             "  ONE,\n"
2355             "\n"
2356             "  TWO,\n"
2357             "\n"
2358             "  THREE\n"
2359             "}",
2360             format("enum KeepEmptyLines {\n"
2361                    "  ONE,\n"
2362                    "\n"
2363                    "  TWO,\n"
2364                    "\n"
2365                    "\n"
2366                    "  THREE\n"
2367                    "}"));
2368   verifyFormat("enum E { // comment\n"
2369                "  ONE,\n"
2370                "  TWO\n"
2371                "};\n"
2372                "int i;");
2373 
2374   FormatStyle EightIndent = getLLVMStyle();
2375   EightIndent.IndentWidth = 8;
2376   verifyFormat("enum {\n"
2377                "        VOID,\n"
2378                "        CHAR,\n"
2379                "        SHORT,\n"
2380                "        INT,\n"
2381                "        LONG,\n"
2382                "        SIGNED,\n"
2383                "        UNSIGNED,\n"
2384                "        BOOL,\n"
2385                "        FLOAT,\n"
2386                "        DOUBLE,\n"
2387                "        COMPLEX\n"
2388                "};",
2389                EightIndent);
2390 
2391   // Not enums.
2392   verifyFormat("enum X f() {\n"
2393                "  a();\n"
2394                "  return 42;\n"
2395                "}");
2396   verifyFormat("enum X Type::f() {\n"
2397                "  a();\n"
2398                "  return 42;\n"
2399                "}");
2400   verifyFormat("enum ::X f() {\n"
2401                "  a();\n"
2402                "  return 42;\n"
2403                "}");
2404   verifyFormat("enum ns::X f() {\n"
2405                "  a();\n"
2406                "  return 42;\n"
2407                "}");
2408 }
2409 
2410 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2411   verifyFormat("enum Type {\n"
2412                "  One = 0; // These semicolons should be commas.\n"
2413                "  Two = 1;\n"
2414                "};");
2415   verifyFormat("namespace n {\n"
2416                "enum Type {\n"
2417                "  One,\n"
2418                "  Two, // missing };\n"
2419                "  int i;\n"
2420                "}\n"
2421                "void g() {}");
2422 }
2423 
2424 TEST_F(FormatTest, FormatsEnumStruct) {
2425   verifyFormat("enum struct {\n"
2426                "  Zero,\n"
2427                "  One = 1,\n"
2428                "  Two = One + 1,\n"
2429                "  Three = (One + Two),\n"
2430                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2431                "  Five = (One, Two, Three, Four, 5)\n"
2432                "};");
2433   verifyFormat("enum struct Enum {};");
2434   verifyFormat("enum struct {};");
2435   verifyFormat("enum struct X E {} d;");
2436   verifyFormat("enum struct __attribute__((...)) E {} d;");
2437   verifyFormat("enum struct __declspec__((...)) E {} d;");
2438   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2439 }
2440 
2441 TEST_F(FormatTest, FormatsEnumClass) {
2442   verifyFormat("enum class {\n"
2443                "  Zero,\n"
2444                "  One = 1,\n"
2445                "  Two = One + 1,\n"
2446                "  Three = (One + Two),\n"
2447                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2448                "  Five = (One, Two, Three, Four, 5)\n"
2449                "};");
2450   verifyFormat("enum class Enum {};");
2451   verifyFormat("enum class {};");
2452   verifyFormat("enum class X E {} d;");
2453   verifyFormat("enum class __attribute__((...)) E {} d;");
2454   verifyFormat("enum class __declspec__((...)) E {} d;");
2455   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2456 }
2457 
2458 TEST_F(FormatTest, FormatsEnumTypes) {
2459   verifyFormat("enum X : int {\n"
2460                "  A, // Force multiple lines.\n"
2461                "  B\n"
2462                "};");
2463   verifyFormat("enum X : int { A, B };");
2464   verifyFormat("enum X : std::uint32_t { A, B };");
2465 }
2466 
2467 TEST_F(FormatTest, FormatsTypedefEnum) {
2468   FormatStyle Style = getLLVMStyle();
2469   Style.ColumnLimit = 40;
2470   verifyFormat("typedef enum {} EmptyEnum;");
2471   verifyFormat("typedef enum { A, B, C } ShortEnum;");
2472   verifyFormat("typedef enum {\n"
2473                "  ZERO = 0,\n"
2474                "  ONE = 1,\n"
2475                "  TWO = 2,\n"
2476                "  THREE = 3\n"
2477                "} LongEnum;",
2478                Style);
2479   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2480   Style.BraceWrapping.AfterEnum = true;
2481   verifyFormat("typedef enum {} EmptyEnum;");
2482   verifyFormat("typedef enum { A, B, C } ShortEnum;");
2483   verifyFormat("typedef enum\n"
2484                "{\n"
2485                "  ZERO = 0,\n"
2486                "  ONE = 1,\n"
2487                "  TWO = 2,\n"
2488                "  THREE = 3\n"
2489                "} LongEnum;",
2490                Style);
2491 }
2492 
2493 TEST_F(FormatTest, FormatsNSEnums) {
2494   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2495   verifyGoogleFormat(
2496       "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
2497   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2498                      "  // Information about someDecentlyLongValue.\n"
2499                      "  someDecentlyLongValue,\n"
2500                      "  // Information about anotherDecentlyLongValue.\n"
2501                      "  anotherDecentlyLongValue,\n"
2502                      "  // Information about aThirdDecentlyLongValue.\n"
2503                      "  aThirdDecentlyLongValue\n"
2504                      "};");
2505   verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
2506                      "  // Information about someDecentlyLongValue.\n"
2507                      "  someDecentlyLongValue,\n"
2508                      "  // Information about anotherDecentlyLongValue.\n"
2509                      "  anotherDecentlyLongValue,\n"
2510                      "  // Information about aThirdDecentlyLongValue.\n"
2511                      "  aThirdDecentlyLongValue\n"
2512                      "};");
2513   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2514                      "  a = 1,\n"
2515                      "  b = 2,\n"
2516                      "  c = 3,\n"
2517                      "};");
2518   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2519                      "  a = 1,\n"
2520                      "  b = 2,\n"
2521                      "  c = 3,\n"
2522                      "};");
2523   verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
2524                      "  a = 1,\n"
2525                      "  b = 2,\n"
2526                      "  c = 3,\n"
2527                      "};");
2528   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2529                      "  a = 1,\n"
2530                      "  b = 2,\n"
2531                      "  c = 3,\n"
2532                      "};");
2533 }
2534 
2535 TEST_F(FormatTest, FormatsBitfields) {
2536   verifyFormat("struct Bitfields {\n"
2537                "  unsigned sClass : 8;\n"
2538                "  unsigned ValueKind : 2;\n"
2539                "};");
2540   verifyFormat("struct A {\n"
2541                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2542                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2543                "};");
2544   verifyFormat("struct MyStruct {\n"
2545                "  uchar data;\n"
2546                "  uchar : 8;\n"
2547                "  uchar : 8;\n"
2548                "  uchar other;\n"
2549                "};");
2550   FormatStyle Style = getLLVMStyle();
2551   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
2552   verifyFormat("struct Bitfields {\n"
2553                "  unsigned sClass:8;\n"
2554                "  unsigned ValueKind:2;\n"
2555                "  uchar other;\n"
2556                "};",
2557                Style);
2558   verifyFormat("struct A {\n"
2559                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
2560                "      bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
2561                "};",
2562                Style);
2563   Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
2564   verifyFormat("struct Bitfields {\n"
2565                "  unsigned sClass :8;\n"
2566                "  unsigned ValueKind :2;\n"
2567                "  uchar other;\n"
2568                "};",
2569                Style);
2570   Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
2571   verifyFormat("struct Bitfields {\n"
2572                "  unsigned sClass: 8;\n"
2573                "  unsigned ValueKind: 2;\n"
2574                "  uchar other;\n"
2575                "};",
2576                Style);
2577 }
2578 
2579 TEST_F(FormatTest, FormatsNamespaces) {
2580   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
2581   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
2582 
2583   verifyFormat("namespace some_namespace {\n"
2584                "class A {};\n"
2585                "void f() { f(); }\n"
2586                "}",
2587                LLVMWithNoNamespaceFix);
2588   verifyFormat("namespace N::inline D {\n"
2589                "class A {};\n"
2590                "void f() { f(); }\n"
2591                "}",
2592                LLVMWithNoNamespaceFix);
2593   verifyFormat("namespace N::inline D::E {\n"
2594                "class A {};\n"
2595                "void f() { f(); }\n"
2596                "}",
2597                LLVMWithNoNamespaceFix);
2598   verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
2599                "class A {};\n"
2600                "void f() { f(); }\n"
2601                "}",
2602                LLVMWithNoNamespaceFix);
2603   verifyFormat("/* something */ namespace some_namespace {\n"
2604                "class A {};\n"
2605                "void f() { f(); }\n"
2606                "}",
2607                LLVMWithNoNamespaceFix);
2608   verifyFormat("namespace {\n"
2609                "class A {};\n"
2610                "void f() { f(); }\n"
2611                "}",
2612                LLVMWithNoNamespaceFix);
2613   verifyFormat("/* something */ namespace {\n"
2614                "class A {};\n"
2615                "void f() { f(); }\n"
2616                "}",
2617                LLVMWithNoNamespaceFix);
2618   verifyFormat("inline namespace X {\n"
2619                "class A {};\n"
2620                "void f() { f(); }\n"
2621                "}",
2622                LLVMWithNoNamespaceFix);
2623   verifyFormat("/* something */ inline namespace X {\n"
2624                "class A {};\n"
2625                "void f() { f(); }\n"
2626                "}",
2627                LLVMWithNoNamespaceFix);
2628   verifyFormat("export namespace X {\n"
2629                "class A {};\n"
2630                "void f() { f(); }\n"
2631                "}",
2632                LLVMWithNoNamespaceFix);
2633   verifyFormat("using namespace some_namespace;\n"
2634                "class A {};\n"
2635                "void f() { f(); }",
2636                LLVMWithNoNamespaceFix);
2637 
2638   // This code is more common than we thought; if we
2639   // layout this correctly the semicolon will go into
2640   // its own line, which is undesirable.
2641   verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
2642   verifyFormat("namespace {\n"
2643                "class A {};\n"
2644                "};",
2645                LLVMWithNoNamespaceFix);
2646 
2647   verifyFormat("namespace {\n"
2648                "int SomeVariable = 0; // comment\n"
2649                "} // namespace",
2650                LLVMWithNoNamespaceFix);
2651   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2652             "#define HEADER_GUARD\n"
2653             "namespace my_namespace {\n"
2654             "int i;\n"
2655             "} // my_namespace\n"
2656             "#endif // HEADER_GUARD",
2657             format("#ifndef HEADER_GUARD\n"
2658                    " #define HEADER_GUARD\n"
2659                    "   namespace my_namespace {\n"
2660                    "int i;\n"
2661                    "}    // my_namespace\n"
2662                    "#endif    // HEADER_GUARD",
2663                    LLVMWithNoNamespaceFix));
2664 
2665   EXPECT_EQ("namespace A::B {\n"
2666             "class C {};\n"
2667             "}",
2668             format("namespace A::B {\n"
2669                    "class C {};\n"
2670                    "}",
2671                    LLVMWithNoNamespaceFix));
2672 
2673   FormatStyle Style = getLLVMStyle();
2674   Style.NamespaceIndentation = FormatStyle::NI_All;
2675   EXPECT_EQ("namespace out {\n"
2676             "  int i;\n"
2677             "  namespace in {\n"
2678             "    int i;\n"
2679             "  } // namespace in\n"
2680             "} // namespace out",
2681             format("namespace out {\n"
2682                    "int i;\n"
2683                    "namespace in {\n"
2684                    "int i;\n"
2685                    "} // namespace in\n"
2686                    "} // namespace out",
2687                    Style));
2688 
2689   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2690   EXPECT_EQ("namespace out {\n"
2691             "int i;\n"
2692             "namespace in {\n"
2693             "  int i;\n"
2694             "} // namespace in\n"
2695             "} // namespace out",
2696             format("namespace out {\n"
2697                    "int i;\n"
2698                    "namespace in {\n"
2699                    "int i;\n"
2700                    "} // namespace in\n"
2701                    "} // namespace out",
2702                    Style));
2703 }
2704 
2705 TEST_F(FormatTest, NamespaceMacros) {
2706   FormatStyle Style = getLLVMStyle();
2707   Style.NamespaceMacros.push_back("TESTSUITE");
2708 
2709   verifyFormat("TESTSUITE(A) {\n"
2710                "int foo();\n"
2711                "} // TESTSUITE(A)",
2712                Style);
2713 
2714   verifyFormat("TESTSUITE(A, B) {\n"
2715                "int foo();\n"
2716                "} // TESTSUITE(A)",
2717                Style);
2718 
2719   // Properly indent according to NamespaceIndentation style
2720   Style.NamespaceIndentation = FormatStyle::NI_All;
2721   verifyFormat("TESTSUITE(A) {\n"
2722                "  int foo();\n"
2723                "} // TESTSUITE(A)",
2724                Style);
2725   verifyFormat("TESTSUITE(A) {\n"
2726                "  namespace B {\n"
2727                "    int foo();\n"
2728                "  } // namespace B\n"
2729                "} // TESTSUITE(A)",
2730                Style);
2731   verifyFormat("namespace A {\n"
2732                "  TESTSUITE(B) {\n"
2733                "    int foo();\n"
2734                "  } // TESTSUITE(B)\n"
2735                "} // namespace A",
2736                Style);
2737 
2738   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2739   verifyFormat("TESTSUITE(A) {\n"
2740                "TESTSUITE(B) {\n"
2741                "  int foo();\n"
2742                "} // TESTSUITE(B)\n"
2743                "} // TESTSUITE(A)",
2744                Style);
2745   verifyFormat("TESTSUITE(A) {\n"
2746                "namespace B {\n"
2747                "  int foo();\n"
2748                "} // namespace B\n"
2749                "} // TESTSUITE(A)",
2750                Style);
2751   verifyFormat("namespace A {\n"
2752                "TESTSUITE(B) {\n"
2753                "  int foo();\n"
2754                "} // TESTSUITE(B)\n"
2755                "} // namespace A",
2756                Style);
2757 
2758   // Properly merge namespace-macros blocks in CompactNamespaces mode
2759   Style.NamespaceIndentation = FormatStyle::NI_None;
2760   Style.CompactNamespaces = true;
2761   verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
2762                "}} // TESTSUITE(A::B)",
2763                Style);
2764 
2765   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
2766             "}} // TESTSUITE(out::in)",
2767             format("TESTSUITE(out) {\n"
2768                    "TESTSUITE(in) {\n"
2769                    "} // TESTSUITE(in)\n"
2770                    "} // TESTSUITE(out)",
2771                    Style));
2772 
2773   EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
2774             "}} // TESTSUITE(out::in)",
2775             format("TESTSUITE(out) {\n"
2776                    "TESTSUITE(in) {\n"
2777                    "} // TESTSUITE(in)\n"
2778                    "} // TESTSUITE(out)",
2779                    Style));
2780 
2781   // Do not merge different namespaces/macros
2782   EXPECT_EQ("namespace out {\n"
2783             "TESTSUITE(in) {\n"
2784             "} // TESTSUITE(in)\n"
2785             "} // namespace out",
2786             format("namespace out {\n"
2787                    "TESTSUITE(in) {\n"
2788                    "} // TESTSUITE(in)\n"
2789                    "} // namespace out",
2790                    Style));
2791   EXPECT_EQ("TESTSUITE(out) {\n"
2792             "namespace in {\n"
2793             "} // namespace in\n"
2794             "} // TESTSUITE(out)",
2795             format("TESTSUITE(out) {\n"
2796                    "namespace in {\n"
2797                    "} // namespace in\n"
2798                    "} // TESTSUITE(out)",
2799                    Style));
2800   Style.NamespaceMacros.push_back("FOOBAR");
2801   EXPECT_EQ("TESTSUITE(out) {\n"
2802             "FOOBAR(in) {\n"
2803             "} // FOOBAR(in)\n"
2804             "} // TESTSUITE(out)",
2805             format("TESTSUITE(out) {\n"
2806                    "FOOBAR(in) {\n"
2807                    "} // FOOBAR(in)\n"
2808                    "} // TESTSUITE(out)",
2809                    Style));
2810 }
2811 
2812 TEST_F(FormatTest, FormatsCompactNamespaces) {
2813   FormatStyle Style = getLLVMStyle();
2814   Style.CompactNamespaces = true;
2815   Style.NamespaceMacros.push_back("TESTSUITE");
2816 
2817   verifyFormat("namespace A { namespace B {\n"
2818                "}} // namespace A::B",
2819                Style);
2820 
2821   EXPECT_EQ("namespace out { namespace in {\n"
2822             "}} // namespace out::in",
2823             format("namespace out {\n"
2824                    "namespace in {\n"
2825                    "} // namespace in\n"
2826                    "} // namespace out",
2827                    Style));
2828 
2829   // Only namespaces which have both consecutive opening and end get compacted
2830   EXPECT_EQ("namespace out {\n"
2831             "namespace in1 {\n"
2832             "} // namespace in1\n"
2833             "namespace in2 {\n"
2834             "} // namespace in2\n"
2835             "} // namespace out",
2836             format("namespace out {\n"
2837                    "namespace in1 {\n"
2838                    "} // namespace in1\n"
2839                    "namespace in2 {\n"
2840                    "} // namespace in2\n"
2841                    "} // namespace out",
2842                    Style));
2843 
2844   EXPECT_EQ("namespace out {\n"
2845             "int i;\n"
2846             "namespace in {\n"
2847             "int j;\n"
2848             "} // namespace in\n"
2849             "int k;\n"
2850             "} // namespace out",
2851             format("namespace out { int i;\n"
2852                    "namespace in { int j; } // namespace in\n"
2853                    "int k; } // namespace out",
2854                    Style));
2855 
2856   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
2857             "}}} // namespace A::B::C\n",
2858             format("namespace A { namespace B {\n"
2859                    "namespace C {\n"
2860                    "}} // namespace B::C\n"
2861                    "} // namespace A\n",
2862                    Style));
2863 
2864   Style.ColumnLimit = 40;
2865   EXPECT_EQ("namespace aaaaaaaaaa {\n"
2866             "namespace bbbbbbbbbb {\n"
2867             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
2868             format("namespace aaaaaaaaaa {\n"
2869                    "namespace bbbbbbbbbb {\n"
2870                    "} // namespace bbbbbbbbbb\n"
2871                    "} // namespace aaaaaaaaaa",
2872                    Style));
2873 
2874   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
2875             "namespace cccccc {\n"
2876             "}}} // namespace aaaaaa::bbbbbb::cccccc",
2877             format("namespace aaaaaa {\n"
2878                    "namespace bbbbbb {\n"
2879                    "namespace cccccc {\n"
2880                    "} // namespace cccccc\n"
2881                    "} // namespace bbbbbb\n"
2882                    "} // namespace aaaaaa",
2883                    Style));
2884   Style.ColumnLimit = 80;
2885 
2886   // Extra semicolon after 'inner' closing brace prevents merging
2887   EXPECT_EQ("namespace out { namespace in {\n"
2888             "}; } // namespace out::in",
2889             format("namespace out {\n"
2890                    "namespace in {\n"
2891                    "}; // namespace in\n"
2892                    "} // namespace out",
2893                    Style));
2894 
2895   // Extra semicolon after 'outer' closing brace is conserved
2896   EXPECT_EQ("namespace out { namespace in {\n"
2897             "}}; // namespace out::in",
2898             format("namespace out {\n"
2899                    "namespace in {\n"
2900                    "} // namespace in\n"
2901                    "}; // namespace out",
2902                    Style));
2903 
2904   Style.NamespaceIndentation = FormatStyle::NI_All;
2905   EXPECT_EQ("namespace out { namespace in {\n"
2906             "  int i;\n"
2907             "}} // namespace out::in",
2908             format("namespace out {\n"
2909                    "namespace in {\n"
2910                    "int i;\n"
2911                    "} // namespace in\n"
2912                    "} // namespace out",
2913                    Style));
2914   EXPECT_EQ("namespace out { namespace mid {\n"
2915             "  namespace in {\n"
2916             "    int j;\n"
2917             "  } // namespace in\n"
2918             "  int k;\n"
2919             "}} // namespace out::mid",
2920             format("namespace out { namespace mid {\n"
2921                    "namespace in { int j; } // namespace in\n"
2922                    "int k; }} // namespace out::mid",
2923                    Style));
2924 
2925   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2926   EXPECT_EQ("namespace out { namespace in {\n"
2927             "  int i;\n"
2928             "}} // namespace out::in",
2929             format("namespace out {\n"
2930                    "namespace in {\n"
2931                    "int i;\n"
2932                    "} // namespace in\n"
2933                    "} // namespace out",
2934                    Style));
2935   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
2936             "  int i;\n"
2937             "}}} // namespace out::mid::in",
2938             format("namespace out {\n"
2939                    "namespace mid {\n"
2940                    "namespace in {\n"
2941                    "int i;\n"
2942                    "} // namespace in\n"
2943                    "} // namespace mid\n"
2944                    "} // namespace out",
2945                    Style));
2946 }
2947 
2948 TEST_F(FormatTest, FormatsExternC) {
2949   verifyFormat("extern \"C\" {\nint a;");
2950   verifyFormat("extern \"C\" {}");
2951   verifyFormat("extern \"C\" {\n"
2952                "int foo();\n"
2953                "}");
2954   verifyFormat("extern \"C\" int foo() {}");
2955   verifyFormat("extern \"C\" int foo();");
2956   verifyFormat("extern \"C\" int foo() {\n"
2957                "  int i = 42;\n"
2958                "  return i;\n"
2959                "}");
2960 
2961   FormatStyle Style = getLLVMStyle();
2962   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2963   Style.BraceWrapping.AfterFunction = true;
2964   verifyFormat("extern \"C\" int foo() {}", Style);
2965   verifyFormat("extern \"C\" int foo();", Style);
2966   verifyFormat("extern \"C\" int foo()\n"
2967                "{\n"
2968                "  int i = 42;\n"
2969                "  return i;\n"
2970                "}",
2971                Style);
2972 
2973   Style.BraceWrapping.AfterExternBlock = true;
2974   Style.BraceWrapping.SplitEmptyRecord = false;
2975   verifyFormat("extern \"C\"\n"
2976                "{}",
2977                Style);
2978   verifyFormat("extern \"C\"\n"
2979                "{\n"
2980                "  int foo();\n"
2981                "}",
2982                Style);
2983 }
2984 
2985 TEST_F(FormatTest, IndentExternBlockStyle) {
2986   FormatStyle Style = getLLVMStyle();
2987   Style.IndentWidth = 2;
2988 
2989   Style.IndentExternBlock = FormatStyle::IEBS_Indent;
2990   verifyFormat("extern \"C\" { /*9*/\n}", Style);
2991   verifyFormat("extern \"C\" {\n"
2992                "  int foo10();\n"
2993                "}",
2994                Style);
2995 
2996   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
2997   verifyFormat("extern \"C\" { /*11*/\n}", Style);
2998   verifyFormat("extern \"C\" {\n"
2999                "int foo12();\n"
3000                "}",
3001                Style);
3002 
3003   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3004   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3005   Style.BraceWrapping.AfterExternBlock = true;
3006   verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
3007   verifyFormat("extern \"C\"\n{\n"
3008                "  int foo14();\n"
3009                "}",
3010                Style);
3011 
3012   Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
3013   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3014   Style.BraceWrapping.AfterExternBlock = false;
3015   verifyFormat("extern \"C\" { /*15*/\n}", Style);
3016   verifyFormat("extern \"C\" {\n"
3017                "int foo16();\n"
3018                "}",
3019                Style);
3020 }
3021 
3022 TEST_F(FormatTest, FormatsInlineASM) {
3023   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
3024   verifyFormat("asm(\"nop\" ::: \"memory\");");
3025   verifyFormat(
3026       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
3027       "    \"cpuid\\n\\t\"\n"
3028       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
3029       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
3030       "    : \"a\"(value));");
3031   EXPECT_EQ(
3032       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
3033       "  __asm {\n"
3034       "        mov     edx,[that] // vtable in edx\n"
3035       "        mov     eax,methodIndex\n"
3036       "        call    [edx][eax*4] // stdcall\n"
3037       "  }\n"
3038       "}",
3039       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
3040              "    __asm {\n"
3041              "        mov     edx,[that] // vtable in edx\n"
3042              "        mov     eax,methodIndex\n"
3043              "        call    [edx][eax*4] // stdcall\n"
3044              "    }\n"
3045              "}"));
3046   EXPECT_EQ("_asm {\n"
3047             "  xor eax, eax;\n"
3048             "  cpuid;\n"
3049             "}",
3050             format("_asm {\n"
3051                    "  xor eax, eax;\n"
3052                    "  cpuid;\n"
3053                    "}"));
3054   verifyFormat("void function() {\n"
3055                "  // comment\n"
3056                "  asm(\"\");\n"
3057                "}");
3058   EXPECT_EQ("__asm {\n"
3059             "}\n"
3060             "int i;",
3061             format("__asm   {\n"
3062                    "}\n"
3063                    "int   i;"));
3064 }
3065 
3066 TEST_F(FormatTest, FormatTryCatch) {
3067   verifyFormat("try {\n"
3068                "  throw a * b;\n"
3069                "} catch (int a) {\n"
3070                "  // Do nothing.\n"
3071                "} catch (...) {\n"
3072                "  exit(42);\n"
3073                "}");
3074 
3075   // Function-level try statements.
3076   verifyFormat("int f() try { return 4; } catch (...) {\n"
3077                "  return 5;\n"
3078                "}");
3079   verifyFormat("class A {\n"
3080                "  int a;\n"
3081                "  A() try : a(0) {\n"
3082                "  } catch (...) {\n"
3083                "    throw;\n"
3084                "  }\n"
3085                "};\n");
3086   verifyFormat("class A {\n"
3087                "  int a;\n"
3088                "  A() try : a(0), b{1} {\n"
3089                "  } catch (...) {\n"
3090                "    throw;\n"
3091                "  }\n"
3092                "};\n");
3093   verifyFormat("class A {\n"
3094                "  int a;\n"
3095                "  A() try : a(0), b{1}, c{2} {\n"
3096                "  } catch (...) {\n"
3097                "    throw;\n"
3098                "  }\n"
3099                "};\n");
3100   verifyFormat("class A {\n"
3101                "  int a;\n"
3102                "  A() try : a(0), b{1}, c{2} {\n"
3103                "    { // New scope.\n"
3104                "    }\n"
3105                "  } catch (...) {\n"
3106                "    throw;\n"
3107                "  }\n"
3108                "};\n");
3109 
3110   // Incomplete try-catch blocks.
3111   verifyIncompleteFormat("try {} catch (");
3112 }
3113 
3114 TEST_F(FormatTest, FormatTryAsAVariable) {
3115   verifyFormat("int try;");
3116   verifyFormat("int try, size;");
3117   verifyFormat("try = foo();");
3118   verifyFormat("if (try < size) {\n  return true;\n}");
3119 
3120   verifyFormat("int catch;");
3121   verifyFormat("int catch, size;");
3122   verifyFormat("catch = foo();");
3123   verifyFormat("if (catch < size) {\n  return true;\n}");
3124 
3125   FormatStyle Style = getLLVMStyle();
3126   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3127   Style.BraceWrapping.AfterFunction = true;
3128   Style.BraceWrapping.BeforeCatch = true;
3129   verifyFormat("try {\n"
3130                "  int bar = 1;\n"
3131                "}\n"
3132                "catch (...) {\n"
3133                "  int bar = 1;\n"
3134                "}",
3135                Style);
3136   verifyFormat("#if NO_EX\n"
3137                "try\n"
3138                "#endif\n"
3139                "{\n"
3140                "}\n"
3141                "#if NO_EX\n"
3142                "catch (...) {\n"
3143                "}",
3144                Style);
3145   verifyFormat("try /* abc */ {\n"
3146                "  int bar = 1;\n"
3147                "}\n"
3148                "catch (...) {\n"
3149                "  int bar = 1;\n"
3150                "}",
3151                Style);
3152   verifyFormat("try\n"
3153                "// abc\n"
3154                "{\n"
3155                "  int bar = 1;\n"
3156                "}\n"
3157                "catch (...) {\n"
3158                "  int bar = 1;\n"
3159                "}",
3160                Style);
3161 }
3162 
3163 TEST_F(FormatTest, FormatSEHTryCatch) {
3164   verifyFormat("__try {\n"
3165                "  int a = b * c;\n"
3166                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
3167                "  // Do nothing.\n"
3168                "}");
3169 
3170   verifyFormat("__try {\n"
3171                "  int a = b * c;\n"
3172                "} __finally {\n"
3173                "  // Do nothing.\n"
3174                "}");
3175 
3176   verifyFormat("DEBUG({\n"
3177                "  __try {\n"
3178                "  } __finally {\n"
3179                "  }\n"
3180                "});\n");
3181 }
3182 
3183 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
3184   verifyFormat("try {\n"
3185                "  f();\n"
3186                "} catch {\n"
3187                "  g();\n"
3188                "}");
3189   verifyFormat("try {\n"
3190                "  f();\n"
3191                "} catch (A a) MACRO(x) {\n"
3192                "  g();\n"
3193                "} catch (B b) MACRO(x) {\n"
3194                "  g();\n"
3195                "}");
3196 }
3197 
3198 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
3199   FormatStyle Style = getLLVMStyle();
3200   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
3201                           FormatStyle::BS_WebKit}) {
3202     Style.BreakBeforeBraces = BraceStyle;
3203     verifyFormat("try {\n"
3204                  "  // something\n"
3205                  "} catch (...) {\n"
3206                  "  // something\n"
3207                  "}",
3208                  Style);
3209   }
3210   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
3211   verifyFormat("try {\n"
3212                "  // something\n"
3213                "}\n"
3214                "catch (...) {\n"
3215                "  // something\n"
3216                "}",
3217                Style);
3218   verifyFormat("__try {\n"
3219                "  // something\n"
3220                "}\n"
3221                "__finally {\n"
3222                "  // something\n"
3223                "}",
3224                Style);
3225   verifyFormat("@try {\n"
3226                "  // something\n"
3227                "}\n"
3228                "@finally {\n"
3229                "  // something\n"
3230                "}",
3231                Style);
3232   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3233   verifyFormat("try\n"
3234                "{\n"
3235                "  // something\n"
3236                "}\n"
3237                "catch (...)\n"
3238                "{\n"
3239                "  // something\n"
3240                "}",
3241                Style);
3242   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
3243   verifyFormat("try\n"
3244                "  {\n"
3245                "  // something white\n"
3246                "  }\n"
3247                "catch (...)\n"
3248                "  {\n"
3249                "  // something white\n"
3250                "  }",
3251                Style);
3252   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
3253   verifyFormat("try\n"
3254                "  {\n"
3255                "    // something\n"
3256                "  }\n"
3257                "catch (...)\n"
3258                "  {\n"
3259                "    // something\n"
3260                "  }",
3261                Style);
3262   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3263   Style.BraceWrapping.BeforeCatch = true;
3264   verifyFormat("try {\n"
3265                "  // something\n"
3266                "}\n"
3267                "catch (...) {\n"
3268                "  // something\n"
3269                "}",
3270                Style);
3271 }
3272 
3273 TEST_F(FormatTest, StaticInitializers) {
3274   verifyFormat("static SomeClass SC = {1, 'a'};");
3275 
3276   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
3277                "    100000000, "
3278                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
3279 
3280   // Here, everything other than the "}" would fit on a line.
3281   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
3282                "    10000000000000000000000000};");
3283   EXPECT_EQ("S s = {a,\n"
3284             "\n"
3285             "       b};",
3286             format("S s = {\n"
3287                    "  a,\n"
3288                    "\n"
3289                    "  b\n"
3290                    "};"));
3291 
3292   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
3293   // line. However, the formatting looks a bit off and this probably doesn't
3294   // happen often in practice.
3295   verifyFormat("static int Variable[1] = {\n"
3296                "    {1000000000000000000000000000000000000}};",
3297                getLLVMStyleWithColumns(40));
3298 }
3299 
3300 TEST_F(FormatTest, DesignatedInitializers) {
3301   verifyFormat("const struct A a = {.a = 1, .b = 2};");
3302   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
3303                "                    .bbbbbbbbbb = 2,\n"
3304                "                    .cccccccccc = 3,\n"
3305                "                    .dddddddddd = 4,\n"
3306                "                    .eeeeeeeeee = 5};");
3307   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
3308                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
3309                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
3310                "    .ccccccccccccccccccccccccccc = 3,\n"
3311                "    .ddddddddddddddddddddddddddd = 4,\n"
3312                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
3313 
3314   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
3315 
3316   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
3317   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
3318                "                    [2] = bbbbbbbbbb,\n"
3319                "                    [3] = cccccccccc,\n"
3320                "                    [4] = dddddddddd,\n"
3321                "                    [5] = eeeeeeeeee};");
3322   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
3323                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3324                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3325                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
3326                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
3327                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
3328 }
3329 
3330 TEST_F(FormatTest, NestedStaticInitializers) {
3331   verifyFormat("static A x = {{{}}};\n");
3332   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
3333                "               {init1, init2, init3, init4}}};",
3334                getLLVMStyleWithColumns(50));
3335 
3336   verifyFormat("somes Status::global_reps[3] = {\n"
3337                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
3338                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
3339                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
3340                getLLVMStyleWithColumns(60));
3341   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
3342                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
3343                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
3344                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
3345   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
3346                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
3347                "rect.fTop}};");
3348 
3349   verifyFormat(
3350       "SomeArrayOfSomeType a = {\n"
3351       "    {{1, 2, 3},\n"
3352       "     {1, 2, 3},\n"
3353       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
3354       "      333333333333333333333333333333},\n"
3355       "     {1, 2, 3},\n"
3356       "     {1, 2, 3}}};");
3357   verifyFormat(
3358       "SomeArrayOfSomeType a = {\n"
3359       "    {{1, 2, 3}},\n"
3360       "    {{1, 2, 3}},\n"
3361       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
3362       "      333333333333333333333333333333}},\n"
3363       "    {{1, 2, 3}},\n"
3364       "    {{1, 2, 3}}};");
3365 
3366   verifyFormat("struct {\n"
3367                "  unsigned bit;\n"
3368                "  const char *const name;\n"
3369                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
3370                "                 {kOsWin, \"Windows\"},\n"
3371                "                 {kOsLinux, \"Linux\"},\n"
3372                "                 {kOsCrOS, \"Chrome OS\"}};");
3373   verifyFormat("struct {\n"
3374                "  unsigned bit;\n"
3375                "  const char *const name;\n"
3376                "} kBitsToOs[] = {\n"
3377                "    {kOsMac, \"Mac\"},\n"
3378                "    {kOsWin, \"Windows\"},\n"
3379                "    {kOsLinux, \"Linux\"},\n"
3380                "    {kOsCrOS, \"Chrome OS\"},\n"
3381                "};");
3382 }
3383 
3384 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
3385   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3386                "                      \\\n"
3387                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
3388 }
3389 
3390 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
3391   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
3392                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
3393 
3394   // Do break defaulted and deleted functions.
3395   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
3396                "    default;",
3397                getLLVMStyleWithColumns(40));
3398   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
3399                "    delete;",
3400                getLLVMStyleWithColumns(40));
3401 }
3402 
3403 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
3404   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
3405                getLLVMStyleWithColumns(40));
3406   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
3407                getLLVMStyleWithColumns(40));
3408   EXPECT_EQ("#define Q                              \\\n"
3409             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
3410             "  \"aaaaaaaa.cpp\"",
3411             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
3412                    getLLVMStyleWithColumns(40)));
3413 }
3414 
3415 TEST_F(FormatTest, UnderstandsLinePPDirective) {
3416   EXPECT_EQ("# 123 \"A string literal\"",
3417             format("   #     123    \"A string literal\""));
3418 }
3419 
3420 TEST_F(FormatTest, LayoutUnknownPPDirective) {
3421   EXPECT_EQ("#;", format("#;"));
3422   verifyFormat("#\n;\n;\n;");
3423 }
3424 
3425 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
3426   EXPECT_EQ("#line 42 \"test\"\n",
3427             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
3428   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
3429                                     getLLVMStyleWithColumns(12)));
3430 }
3431 
3432 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
3433   EXPECT_EQ("#line 42 \"test\"",
3434             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
3435   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
3436 }
3437 
3438 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
3439   verifyFormat("#define A \\x20");
3440   verifyFormat("#define A \\ x20");
3441   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
3442   verifyFormat("#define A ''");
3443   verifyFormat("#define A ''qqq");
3444   verifyFormat("#define A `qqq");
3445   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
3446   EXPECT_EQ("const char *c = STRINGIFY(\n"
3447             "\\na : b);",
3448             format("const char * c = STRINGIFY(\n"
3449                    "\\na : b);"));
3450 
3451   verifyFormat("a\r\\");
3452   verifyFormat("a\v\\");
3453   verifyFormat("a\f\\");
3454 }
3455 
3456 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
3457   FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
3458   style.IndentWidth = 4;
3459   style.PPIndentWidth = 1;
3460 
3461   style.IndentPPDirectives = FormatStyle::PPDIS_None;
3462   verifyFormat("#ifdef __linux__\n"
3463                "void foo() {\n"
3464                "    int x = 0;\n"
3465                "}\n"
3466                "#define FOO\n"
3467                "#endif\n"
3468                "void bar() {\n"
3469                "    int y = 0;\n"
3470                "}\n",
3471                style);
3472 
3473   style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
3474   verifyFormat("#ifdef __linux__\n"
3475                "void foo() {\n"
3476                "    int x = 0;\n"
3477                "}\n"
3478                "# define FOO foo\n"
3479                "#endif\n"
3480                "void bar() {\n"
3481                "    int y = 0;\n"
3482                "}\n",
3483                style);
3484 
3485   style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
3486   verifyFormat("#ifdef __linux__\n"
3487                "void foo() {\n"
3488                "    int x = 0;\n"
3489                "}\n"
3490                " #define FOO foo\n"
3491                "#endif\n"
3492                "void bar() {\n"
3493                "    int y = 0;\n"
3494                "}\n",
3495                style);
3496 }
3497 
3498 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
3499   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
3500   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
3501   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
3502   // FIXME: We never break before the macro name.
3503   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
3504 
3505   verifyFormat("#define A A\n#define A A");
3506   verifyFormat("#define A(X) A\n#define A A");
3507 
3508   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
3509   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
3510 }
3511 
3512 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
3513   EXPECT_EQ("// somecomment\n"
3514             "#include \"a.h\"\n"
3515             "#define A(  \\\n"
3516             "    A, B)\n"
3517             "#include \"b.h\"\n"
3518             "// somecomment\n",
3519             format("  // somecomment\n"
3520                    "  #include \"a.h\"\n"
3521                    "#define A(A,\\\n"
3522                    "    B)\n"
3523                    "    #include \"b.h\"\n"
3524                    " // somecomment\n",
3525                    getLLVMStyleWithColumns(13)));
3526 }
3527 
3528 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
3529 
3530 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
3531   EXPECT_EQ("#define A    \\\n"
3532             "  c;         \\\n"
3533             "  e;\n"
3534             "f;",
3535             format("#define A c; e;\n"
3536                    "f;",
3537                    getLLVMStyleWithColumns(14)));
3538 }
3539 
3540 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
3541 
3542 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
3543   EXPECT_EQ("int x,\n"
3544             "#define A\n"
3545             "    y;",
3546             format("int x,\n#define A\ny;"));
3547 }
3548 
3549 TEST_F(FormatTest, HashInMacroDefinition) {
3550   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
3551   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
3552   verifyFormat("#define A  \\\n"
3553                "  {        \\\n"
3554                "    f(#c); \\\n"
3555                "  }",
3556                getLLVMStyleWithColumns(11));
3557 
3558   verifyFormat("#define A(X)         \\\n"
3559                "  void function##X()",
3560                getLLVMStyleWithColumns(22));
3561 
3562   verifyFormat("#define A(a, b, c)   \\\n"
3563                "  void a##b##c()",
3564                getLLVMStyleWithColumns(22));
3565 
3566   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
3567 }
3568 
3569 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
3570   EXPECT_EQ("#define A (x)", format("#define A (x)"));
3571   EXPECT_EQ("#define A(x)", format("#define A(x)"));
3572 
3573   FormatStyle Style = getLLVMStyle();
3574   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
3575   verifyFormat("#define true ((foo)1)", Style);
3576   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
3577   verifyFormat("#define false((foo)0)", Style);
3578 }
3579 
3580 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
3581   EXPECT_EQ("#define A b;", format("#define A \\\n"
3582                                    "          \\\n"
3583                                    "  b;",
3584                                    getLLVMStyleWithColumns(25)));
3585   EXPECT_EQ("#define A \\\n"
3586             "          \\\n"
3587             "  a;      \\\n"
3588             "  b;",
3589             format("#define A \\\n"
3590                    "          \\\n"
3591                    "  a;      \\\n"
3592                    "  b;",
3593                    getLLVMStyleWithColumns(11)));
3594   EXPECT_EQ("#define A \\\n"
3595             "  a;      \\\n"
3596             "          \\\n"
3597             "  b;",
3598             format("#define A \\\n"
3599                    "  a;      \\\n"
3600                    "          \\\n"
3601                    "  b;",
3602                    getLLVMStyleWithColumns(11)));
3603 }
3604 
3605 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
3606   verifyIncompleteFormat("#define A :");
3607   verifyFormat("#define SOMECASES  \\\n"
3608                "  case 1:          \\\n"
3609                "  case 2\n",
3610                getLLVMStyleWithColumns(20));
3611   verifyFormat("#define MACRO(a) \\\n"
3612                "  if (a)         \\\n"
3613                "    f();         \\\n"
3614                "  else           \\\n"
3615                "    g()",
3616                getLLVMStyleWithColumns(18));
3617   verifyFormat("#define A template <typename T>");
3618   verifyIncompleteFormat("#define STR(x) #x\n"
3619                          "f(STR(this_is_a_string_literal{));");
3620   verifyFormat("#pragma omp threadprivate( \\\n"
3621                "    y)), // expected-warning",
3622                getLLVMStyleWithColumns(28));
3623   verifyFormat("#d, = };");
3624   verifyFormat("#if \"a");
3625   verifyIncompleteFormat("({\n"
3626                          "#define b     \\\n"
3627                          "  }           \\\n"
3628                          "  a\n"
3629                          "a",
3630                          getLLVMStyleWithColumns(15));
3631   verifyFormat("#define A     \\\n"
3632                "  {           \\\n"
3633                "    {\n"
3634                "#define B     \\\n"
3635                "  }           \\\n"
3636                "  }",
3637                getLLVMStyleWithColumns(15));
3638   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
3639   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
3640   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
3641   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
3642 }
3643 
3644 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
3645   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
3646   EXPECT_EQ("class A : public QObject {\n"
3647             "  Q_OBJECT\n"
3648             "\n"
3649             "  A() {}\n"
3650             "};",
3651             format("class A  :  public QObject {\n"
3652                    "     Q_OBJECT\n"
3653                    "\n"
3654                    "  A() {\n}\n"
3655                    "}  ;"));
3656   EXPECT_EQ("MACRO\n"
3657             "/*static*/ int i;",
3658             format("MACRO\n"
3659                    " /*static*/ int   i;"));
3660   EXPECT_EQ("SOME_MACRO\n"
3661             "namespace {\n"
3662             "void f();\n"
3663             "} // namespace",
3664             format("SOME_MACRO\n"
3665                    "  namespace    {\n"
3666                    "void   f(  );\n"
3667                    "} // namespace"));
3668   // Only if the identifier contains at least 5 characters.
3669   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
3670   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
3671   // Only if everything is upper case.
3672   EXPECT_EQ("class A : public QObject {\n"
3673             "  Q_Object A() {}\n"
3674             "};",
3675             format("class A  :  public QObject {\n"
3676                    "     Q_Object\n"
3677                    "  A() {\n}\n"
3678                    "}  ;"));
3679 
3680   // Only if the next line can actually start an unwrapped line.
3681   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
3682             format("SOME_WEIRD_LOG_MACRO\n"
3683                    "<< SomeThing;"));
3684 
3685   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
3686                "(n, buffers))\n",
3687                getChromiumStyle(FormatStyle::LK_Cpp));
3688 
3689   // See PR41483
3690   EXPECT_EQ("/**/ FOO(a)\n"
3691             "FOO(b)",
3692             format("/**/ FOO(a)\n"
3693                    "FOO(b)"));
3694 }
3695 
3696 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
3697   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
3698             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
3699             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
3700             "class X {};\n"
3701             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
3702             "int *createScopDetectionPass() { return 0; }",
3703             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
3704                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
3705                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
3706                    "  class X {};\n"
3707                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
3708                    "  int *createScopDetectionPass() { return 0; }"));
3709   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
3710   // braces, so that inner block is indented one level more.
3711   EXPECT_EQ("int q() {\n"
3712             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
3713             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
3714             "  IPC_END_MESSAGE_MAP()\n"
3715             "}",
3716             format("int q() {\n"
3717                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
3718                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
3719                    "  IPC_END_MESSAGE_MAP()\n"
3720                    "}"));
3721 
3722   // Same inside macros.
3723   EXPECT_EQ("#define LIST(L) \\\n"
3724             "  L(A)          \\\n"
3725             "  L(B)          \\\n"
3726             "  L(C)",
3727             format("#define LIST(L) \\\n"
3728                    "  L(A) \\\n"
3729                    "  L(B) \\\n"
3730                    "  L(C)",
3731                    getGoogleStyle()));
3732 
3733   // These must not be recognized as macros.
3734   EXPECT_EQ("int q() {\n"
3735             "  f(x);\n"
3736             "  f(x) {}\n"
3737             "  f(x)->g();\n"
3738             "  f(x)->*g();\n"
3739             "  f(x).g();\n"
3740             "  f(x) = x;\n"
3741             "  f(x) += x;\n"
3742             "  f(x) -= x;\n"
3743             "  f(x) *= x;\n"
3744             "  f(x) /= x;\n"
3745             "  f(x) %= x;\n"
3746             "  f(x) &= x;\n"
3747             "  f(x) |= x;\n"
3748             "  f(x) ^= x;\n"
3749             "  f(x) >>= x;\n"
3750             "  f(x) <<= x;\n"
3751             "  f(x)[y].z();\n"
3752             "  LOG(INFO) << x;\n"
3753             "  ifstream(x) >> x;\n"
3754             "}\n",
3755             format("int q() {\n"
3756                    "  f(x)\n;\n"
3757                    "  f(x)\n {}\n"
3758                    "  f(x)\n->g();\n"
3759                    "  f(x)\n->*g();\n"
3760                    "  f(x)\n.g();\n"
3761                    "  f(x)\n = x;\n"
3762                    "  f(x)\n += x;\n"
3763                    "  f(x)\n -= x;\n"
3764                    "  f(x)\n *= x;\n"
3765                    "  f(x)\n /= x;\n"
3766                    "  f(x)\n %= x;\n"
3767                    "  f(x)\n &= x;\n"
3768                    "  f(x)\n |= x;\n"
3769                    "  f(x)\n ^= x;\n"
3770                    "  f(x)\n >>= x;\n"
3771                    "  f(x)\n <<= x;\n"
3772                    "  f(x)\n[y].z();\n"
3773                    "  LOG(INFO)\n << x;\n"
3774                    "  ifstream(x)\n >> x;\n"
3775                    "}\n"));
3776   EXPECT_EQ("int q() {\n"
3777             "  F(x)\n"
3778             "  if (1) {\n"
3779             "  }\n"
3780             "  F(x)\n"
3781             "  while (1) {\n"
3782             "  }\n"
3783             "  F(x)\n"
3784             "  G(x);\n"
3785             "  F(x)\n"
3786             "  try {\n"
3787             "    Q();\n"
3788             "  } catch (...) {\n"
3789             "  }\n"
3790             "}\n",
3791             format("int q() {\n"
3792                    "F(x)\n"
3793                    "if (1) {}\n"
3794                    "F(x)\n"
3795                    "while (1) {}\n"
3796                    "F(x)\n"
3797                    "G(x);\n"
3798                    "F(x)\n"
3799                    "try { Q(); } catch (...) {}\n"
3800                    "}\n"));
3801   EXPECT_EQ("class A {\n"
3802             "  A() : t(0) {}\n"
3803             "  A(int i) noexcept() : {}\n"
3804             "  A(X x)\n" // FIXME: function-level try blocks are broken.
3805             "  try : t(0) {\n"
3806             "  } catch (...) {\n"
3807             "  }\n"
3808             "};",
3809             format("class A {\n"
3810                    "  A()\n : t(0) {}\n"
3811                    "  A(int i)\n noexcept() : {}\n"
3812                    "  A(X x)\n"
3813                    "  try : t(0) {} catch (...) {}\n"
3814                    "};"));
3815   FormatStyle Style = getLLVMStyle();
3816   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3817   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3818   Style.BraceWrapping.AfterFunction = true;
3819   EXPECT_EQ("void f()\n"
3820             "try\n"
3821             "{\n"
3822             "}",
3823             format("void f() try {\n"
3824                    "}",
3825                    Style));
3826   EXPECT_EQ("class SomeClass {\n"
3827             "public:\n"
3828             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3829             "};",
3830             format("class SomeClass {\n"
3831                    "public:\n"
3832                    "  SomeClass()\n"
3833                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3834                    "};"));
3835   EXPECT_EQ("class SomeClass {\n"
3836             "public:\n"
3837             "  SomeClass()\n"
3838             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3839             "};",
3840             format("class SomeClass {\n"
3841                    "public:\n"
3842                    "  SomeClass()\n"
3843                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3844                    "};",
3845                    getLLVMStyleWithColumns(40)));
3846 
3847   verifyFormat("MACRO(>)");
3848 
3849   // Some macros contain an implicit semicolon.
3850   Style = getLLVMStyle();
3851   Style.StatementMacros.push_back("FOO");
3852   verifyFormat("FOO(a) int b = 0;");
3853   verifyFormat("FOO(a)\n"
3854                "int b = 0;",
3855                Style);
3856   verifyFormat("FOO(a);\n"
3857                "int b = 0;",
3858                Style);
3859   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
3860                "int b = 0;",
3861                Style);
3862   verifyFormat("FOO()\n"
3863                "int b = 0;",
3864                Style);
3865   verifyFormat("FOO\n"
3866                "int b = 0;",
3867                Style);
3868   verifyFormat("void f() {\n"
3869                "  FOO(a)\n"
3870                "  return a;\n"
3871                "}",
3872                Style);
3873   verifyFormat("FOO(a)\n"
3874                "FOO(b)",
3875                Style);
3876   verifyFormat("int a = 0;\n"
3877                "FOO(b)\n"
3878                "int c = 0;",
3879                Style);
3880   verifyFormat("int a = 0;\n"
3881                "int x = FOO(a)\n"
3882                "int b = 0;",
3883                Style);
3884   verifyFormat("void foo(int a) { FOO(a) }\n"
3885                "uint32_t bar() {}",
3886                Style);
3887 }
3888 
3889 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
3890   verifyFormat("#define A \\\n"
3891                "  f({     \\\n"
3892                "    g();  \\\n"
3893                "  });",
3894                getLLVMStyleWithColumns(11));
3895 }
3896 
3897 TEST_F(FormatTest, IndentPreprocessorDirectives) {
3898   FormatStyle Style = getLLVMStyle();
3899   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
3900   Style.ColumnLimit = 40;
3901   verifyFormat("#ifdef _WIN32\n"
3902                "#define A 0\n"
3903                "#ifdef VAR2\n"
3904                "#define B 1\n"
3905                "#include <someheader.h>\n"
3906                "#define MACRO                          \\\n"
3907                "  some_very_long_func_aaaaaaaaaa();\n"
3908                "#endif\n"
3909                "#else\n"
3910                "#define A 1\n"
3911                "#endif",
3912                Style);
3913   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
3914   verifyFormat("#ifdef _WIN32\n"
3915                "#  define A 0\n"
3916                "#  ifdef VAR2\n"
3917                "#    define B 1\n"
3918                "#    include <someheader.h>\n"
3919                "#    define MACRO                      \\\n"
3920                "      some_very_long_func_aaaaaaaaaa();\n"
3921                "#  endif\n"
3922                "#else\n"
3923                "#  define A 1\n"
3924                "#endif",
3925                Style);
3926   verifyFormat("#if A\n"
3927                "#  define MACRO                        \\\n"
3928                "    void a(int x) {                    \\\n"
3929                "      b();                             \\\n"
3930                "      c();                             \\\n"
3931                "      d();                             \\\n"
3932                "      e();                             \\\n"
3933                "      f();                             \\\n"
3934                "    }\n"
3935                "#endif",
3936                Style);
3937   // Comments before include guard.
3938   verifyFormat("// file comment\n"
3939                "// file comment\n"
3940                "#ifndef HEADER_H\n"
3941                "#define HEADER_H\n"
3942                "code();\n"
3943                "#endif",
3944                Style);
3945   // Test with include guards.
3946   verifyFormat("#ifndef HEADER_H\n"
3947                "#define HEADER_H\n"
3948                "code();\n"
3949                "#endif",
3950                Style);
3951   // Include guards must have a #define with the same variable immediately
3952   // after #ifndef.
3953   verifyFormat("#ifndef NOT_GUARD\n"
3954                "#  define FOO\n"
3955                "code();\n"
3956                "#endif",
3957                Style);
3958 
3959   // Include guards must cover the entire file.
3960   verifyFormat("code();\n"
3961                "code();\n"
3962                "#ifndef NOT_GUARD\n"
3963                "#  define NOT_GUARD\n"
3964                "code();\n"
3965                "#endif",
3966                Style);
3967   verifyFormat("#ifndef NOT_GUARD\n"
3968                "#  define NOT_GUARD\n"
3969                "code();\n"
3970                "#endif\n"
3971                "code();",
3972                Style);
3973   // Test with trailing blank lines.
3974   verifyFormat("#ifndef HEADER_H\n"
3975                "#define HEADER_H\n"
3976                "code();\n"
3977                "#endif\n",
3978                Style);
3979   // Include guards don't have #else.
3980   verifyFormat("#ifndef NOT_GUARD\n"
3981                "#  define NOT_GUARD\n"
3982                "code();\n"
3983                "#else\n"
3984                "#endif",
3985                Style);
3986   verifyFormat("#ifndef NOT_GUARD\n"
3987                "#  define NOT_GUARD\n"
3988                "code();\n"
3989                "#elif FOO\n"
3990                "#endif",
3991                Style);
3992   // Non-identifier #define after potential include guard.
3993   verifyFormat("#ifndef FOO\n"
3994                "#  define 1\n"
3995                "#endif\n",
3996                Style);
3997   // #if closes past last non-preprocessor line.
3998   verifyFormat("#ifndef FOO\n"
3999                "#define FOO\n"
4000                "#if 1\n"
4001                "int i;\n"
4002                "#  define A 0\n"
4003                "#endif\n"
4004                "#endif\n",
4005                Style);
4006   // Don't crash if there is an #elif directive without a condition.
4007   verifyFormat("#if 1\n"
4008                "int x;\n"
4009                "#elif\n"
4010                "int y;\n"
4011                "#else\n"
4012                "int z;\n"
4013                "#endif",
4014                Style);
4015   // FIXME: This doesn't handle the case where there's code between the
4016   // #ifndef and #define but all other conditions hold. This is because when
4017   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
4018   // previous code line yet, so we can't detect it.
4019   EXPECT_EQ("#ifndef NOT_GUARD\n"
4020             "code();\n"
4021             "#define NOT_GUARD\n"
4022             "code();\n"
4023             "#endif",
4024             format("#ifndef NOT_GUARD\n"
4025                    "code();\n"
4026                    "#  define NOT_GUARD\n"
4027                    "code();\n"
4028                    "#endif",
4029                    Style));
4030   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
4031   // be outside an include guard. Examples are #pragma once and
4032   // #pragma GCC diagnostic, or anything else that does not change the meaning
4033   // of the file if it's included multiple times.
4034   EXPECT_EQ("#ifdef WIN32\n"
4035             "#  pragma once\n"
4036             "#endif\n"
4037             "#ifndef HEADER_H\n"
4038             "#  define HEADER_H\n"
4039             "code();\n"
4040             "#endif",
4041             format("#ifdef WIN32\n"
4042                    "#  pragma once\n"
4043                    "#endif\n"
4044                    "#ifndef HEADER_H\n"
4045                    "#define HEADER_H\n"
4046                    "code();\n"
4047                    "#endif",
4048                    Style));
4049   // FIXME: This does not detect when there is a single non-preprocessor line
4050   // in front of an include-guard-like structure where other conditions hold
4051   // because ScopedLineState hides the line.
4052   EXPECT_EQ("code();\n"
4053             "#ifndef HEADER_H\n"
4054             "#define HEADER_H\n"
4055             "code();\n"
4056             "#endif",
4057             format("code();\n"
4058                    "#ifndef HEADER_H\n"
4059                    "#  define HEADER_H\n"
4060                    "code();\n"
4061                    "#endif",
4062                    Style));
4063   // Keep comments aligned with #, otherwise indent comments normally. These
4064   // tests cannot use verifyFormat because messUp manipulates leading
4065   // whitespace.
4066   {
4067     const char *Expected = ""
4068                            "void f() {\n"
4069                            "#if 1\n"
4070                            "// Preprocessor aligned.\n"
4071                            "#  define A 0\n"
4072                            "  // Code. Separated by blank line.\n"
4073                            "\n"
4074                            "#  define B 0\n"
4075                            "  // Code. Not aligned with #\n"
4076                            "#  define C 0\n"
4077                            "#endif";
4078     const char *ToFormat = ""
4079                            "void f() {\n"
4080                            "#if 1\n"
4081                            "// Preprocessor aligned.\n"
4082                            "#  define A 0\n"
4083                            "// Code. Separated by blank line.\n"
4084                            "\n"
4085                            "#  define B 0\n"
4086                            "   // Code. Not aligned with #\n"
4087                            "#  define C 0\n"
4088                            "#endif";
4089     EXPECT_EQ(Expected, format(ToFormat, Style));
4090     EXPECT_EQ(Expected, format(Expected, Style));
4091   }
4092   // Keep block quotes aligned.
4093   {
4094     const char *Expected = ""
4095                            "void f() {\n"
4096                            "#if 1\n"
4097                            "/* Preprocessor aligned. */\n"
4098                            "#  define A 0\n"
4099                            "  /* Code. Separated by blank line. */\n"
4100                            "\n"
4101                            "#  define B 0\n"
4102                            "  /* Code. Not aligned with # */\n"
4103                            "#  define C 0\n"
4104                            "#endif";
4105     const char *ToFormat = ""
4106                            "void f() {\n"
4107                            "#if 1\n"
4108                            "/* Preprocessor aligned. */\n"
4109                            "#  define A 0\n"
4110                            "/* Code. Separated by blank line. */\n"
4111                            "\n"
4112                            "#  define B 0\n"
4113                            "   /* Code. Not aligned with # */\n"
4114                            "#  define C 0\n"
4115                            "#endif";
4116     EXPECT_EQ(Expected, format(ToFormat, Style));
4117     EXPECT_EQ(Expected, format(Expected, Style));
4118   }
4119   // Keep comments aligned with un-indented directives.
4120   {
4121     const char *Expected = ""
4122                            "void f() {\n"
4123                            "// Preprocessor aligned.\n"
4124                            "#define A 0\n"
4125                            "  // Code. Separated by blank line.\n"
4126                            "\n"
4127                            "#define B 0\n"
4128                            "  // Code. Not aligned with #\n"
4129                            "#define C 0\n";
4130     const char *ToFormat = ""
4131                            "void f() {\n"
4132                            "// Preprocessor aligned.\n"
4133                            "#define A 0\n"
4134                            "// Code. Separated by blank line.\n"
4135                            "\n"
4136                            "#define B 0\n"
4137                            "   // Code. Not aligned with #\n"
4138                            "#define C 0\n";
4139     EXPECT_EQ(Expected, format(ToFormat, Style));
4140     EXPECT_EQ(Expected, format(Expected, Style));
4141   }
4142   // Test AfterHash with tabs.
4143   {
4144     FormatStyle Tabbed = Style;
4145     Tabbed.UseTab = FormatStyle::UT_Always;
4146     Tabbed.IndentWidth = 8;
4147     Tabbed.TabWidth = 8;
4148     verifyFormat("#ifdef _WIN32\n"
4149                  "#\tdefine A 0\n"
4150                  "#\tifdef VAR2\n"
4151                  "#\t\tdefine B 1\n"
4152                  "#\t\tinclude <someheader.h>\n"
4153                  "#\t\tdefine MACRO          \\\n"
4154                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4155                  "#\tendif\n"
4156                  "#else\n"
4157                  "#\tdefine A 1\n"
4158                  "#endif",
4159                  Tabbed);
4160   }
4161 
4162   // Regression test: Multiline-macro inside include guards.
4163   verifyFormat("#ifndef HEADER_H\n"
4164                "#define HEADER_H\n"
4165                "#define A()        \\\n"
4166                "  int i;           \\\n"
4167                "  int j;\n"
4168                "#endif // HEADER_H",
4169                getLLVMStyleWithColumns(20));
4170 
4171   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4172   // Basic before hash indent tests
4173   verifyFormat("#ifdef _WIN32\n"
4174                "  #define A 0\n"
4175                "  #ifdef VAR2\n"
4176                "    #define B 1\n"
4177                "    #include <someheader.h>\n"
4178                "    #define MACRO                      \\\n"
4179                "      some_very_long_func_aaaaaaaaaa();\n"
4180                "  #endif\n"
4181                "#else\n"
4182                "  #define A 1\n"
4183                "#endif",
4184                Style);
4185   verifyFormat("#if A\n"
4186                "  #define MACRO                        \\\n"
4187                "    void a(int x) {                    \\\n"
4188                "      b();                             \\\n"
4189                "      c();                             \\\n"
4190                "      d();                             \\\n"
4191                "      e();                             \\\n"
4192                "      f();                             \\\n"
4193                "    }\n"
4194                "#endif",
4195                Style);
4196   // Keep comments aligned with indented directives. These
4197   // tests cannot use verifyFormat because messUp manipulates leading
4198   // whitespace.
4199   {
4200     const char *Expected = "void f() {\n"
4201                            "// Aligned to preprocessor.\n"
4202                            "#if 1\n"
4203                            "  // Aligned to code.\n"
4204                            "  int a;\n"
4205                            "  #if 1\n"
4206                            "    // Aligned to preprocessor.\n"
4207                            "    #define A 0\n"
4208                            "  // Aligned to code.\n"
4209                            "  int b;\n"
4210                            "  #endif\n"
4211                            "#endif\n"
4212                            "}";
4213     const char *ToFormat = "void f() {\n"
4214                            "// Aligned to preprocessor.\n"
4215                            "#if 1\n"
4216                            "// Aligned to code.\n"
4217                            "int a;\n"
4218                            "#if 1\n"
4219                            "// Aligned to preprocessor.\n"
4220                            "#define A 0\n"
4221                            "// Aligned to code.\n"
4222                            "int b;\n"
4223                            "#endif\n"
4224                            "#endif\n"
4225                            "}";
4226     EXPECT_EQ(Expected, format(ToFormat, Style));
4227     EXPECT_EQ(Expected, format(Expected, Style));
4228   }
4229   {
4230     const char *Expected = "void f() {\n"
4231                            "/* Aligned to preprocessor. */\n"
4232                            "#if 1\n"
4233                            "  /* Aligned to code. */\n"
4234                            "  int a;\n"
4235                            "  #if 1\n"
4236                            "    /* Aligned to preprocessor. */\n"
4237                            "    #define A 0\n"
4238                            "  /* Aligned to code. */\n"
4239                            "  int b;\n"
4240                            "  #endif\n"
4241                            "#endif\n"
4242                            "}";
4243     const char *ToFormat = "void f() {\n"
4244                            "/* Aligned to preprocessor. */\n"
4245                            "#if 1\n"
4246                            "/* Aligned to code. */\n"
4247                            "int a;\n"
4248                            "#if 1\n"
4249                            "/* Aligned to preprocessor. */\n"
4250                            "#define A 0\n"
4251                            "/* Aligned to code. */\n"
4252                            "int b;\n"
4253                            "#endif\n"
4254                            "#endif\n"
4255                            "}";
4256     EXPECT_EQ(Expected, format(ToFormat, Style));
4257     EXPECT_EQ(Expected, format(Expected, Style));
4258   }
4259 
4260   // Test single comment before preprocessor
4261   verifyFormat("// Comment\n"
4262                "\n"
4263                "#if 1\n"
4264                "#endif",
4265                Style);
4266 }
4267 
4268 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
4269   verifyFormat("{\n  { a #c; }\n}");
4270 }
4271 
4272 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
4273   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
4274             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
4275   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
4276             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
4277 }
4278 
4279 TEST_F(FormatTest, EscapedNewlines) {
4280   FormatStyle Narrow = getLLVMStyleWithColumns(11);
4281   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
4282             format("#define A \\\nint i;\\\n  int j;", Narrow));
4283   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
4284   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
4285   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
4286   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
4287 
4288   FormatStyle AlignLeft = getLLVMStyle();
4289   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
4290   EXPECT_EQ("#define MACRO(x) \\\n"
4291             "private:         \\\n"
4292             "  int x(int a);\n",
4293             format("#define MACRO(x) \\\n"
4294                    "private:         \\\n"
4295                    "  int x(int a);\n",
4296                    AlignLeft));
4297 
4298   // CRLF line endings
4299   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
4300             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
4301   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
4302   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
4303   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
4304   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
4305   EXPECT_EQ("#define MACRO(x) \\\r\n"
4306             "private:         \\\r\n"
4307             "  int x(int a);\r\n",
4308             format("#define MACRO(x) \\\r\n"
4309                    "private:         \\\r\n"
4310                    "  int x(int a);\r\n",
4311                    AlignLeft));
4312 
4313   FormatStyle DontAlign = getLLVMStyle();
4314   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
4315   DontAlign.MaxEmptyLinesToKeep = 3;
4316   // FIXME: can't use verifyFormat here because the newline before
4317   // "public:" is not inserted the first time it's reformatted
4318   EXPECT_EQ("#define A \\\n"
4319             "  class Foo { \\\n"
4320             "    void bar(); \\\n"
4321             "\\\n"
4322             "\\\n"
4323             "\\\n"
4324             "  public: \\\n"
4325             "    void baz(); \\\n"
4326             "  };",
4327             format("#define A \\\n"
4328                    "  class Foo { \\\n"
4329                    "    void bar(); \\\n"
4330                    "\\\n"
4331                    "\\\n"
4332                    "\\\n"
4333                    "  public: \\\n"
4334                    "    void baz(); \\\n"
4335                    "  };",
4336                    DontAlign));
4337 }
4338 
4339 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
4340   verifyFormat("#define A \\\n"
4341                "  int v(  \\\n"
4342                "      a); \\\n"
4343                "  int i;",
4344                getLLVMStyleWithColumns(11));
4345 }
4346 
4347 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
4348   EXPECT_EQ(
4349       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4350       "                      \\\n"
4351       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
4352       "\n"
4353       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
4354       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
4355       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
4356              "\\\n"
4357              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
4358              "  \n"
4359              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
4360              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
4361 }
4362 
4363 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
4364   EXPECT_EQ("int\n"
4365             "#define A\n"
4366             "    a;",
4367             format("int\n#define A\na;"));
4368   verifyFormat("functionCallTo(\n"
4369                "    someOtherFunction(\n"
4370                "        withSomeParameters, whichInSequence,\n"
4371                "        areLongerThanALine(andAnotherCall,\n"
4372                "#define A B\n"
4373                "                           withMoreParamters,\n"
4374                "                           whichStronglyInfluenceTheLayout),\n"
4375                "        andMoreParameters),\n"
4376                "    trailing);",
4377                getLLVMStyleWithColumns(69));
4378   verifyFormat("Foo::Foo()\n"
4379                "#ifdef BAR\n"
4380                "    : baz(0)\n"
4381                "#endif\n"
4382                "{\n"
4383                "}");
4384   verifyFormat("void f() {\n"
4385                "  if (true)\n"
4386                "#ifdef A\n"
4387                "    f(42);\n"
4388                "  x();\n"
4389                "#else\n"
4390                "    g();\n"
4391                "  x();\n"
4392                "#endif\n"
4393                "}");
4394   verifyFormat("void f(param1, param2,\n"
4395                "       param3,\n"
4396                "#ifdef A\n"
4397                "       param4(param5,\n"
4398                "#ifdef A1\n"
4399                "              param6,\n"
4400                "#ifdef A2\n"
4401                "              param7),\n"
4402                "#else\n"
4403                "              param8),\n"
4404                "       param9,\n"
4405                "#endif\n"
4406                "       param10,\n"
4407                "#endif\n"
4408                "       param11)\n"
4409                "#else\n"
4410                "       param12)\n"
4411                "#endif\n"
4412                "{\n"
4413                "  x();\n"
4414                "}",
4415                getLLVMStyleWithColumns(28));
4416   verifyFormat("#if 1\n"
4417                "int i;");
4418   verifyFormat("#if 1\n"
4419                "#endif\n"
4420                "#if 1\n"
4421                "#else\n"
4422                "#endif\n");
4423   verifyFormat("DEBUG({\n"
4424                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4425                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
4426                "});\n"
4427                "#if a\n"
4428                "#else\n"
4429                "#endif");
4430 
4431   verifyIncompleteFormat("void f(\n"
4432                          "#if A\n"
4433                          ");\n"
4434                          "#else\n"
4435                          "#endif");
4436 }
4437 
4438 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
4439   verifyFormat("#endif\n"
4440                "#if B");
4441 }
4442 
4443 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
4444   FormatStyle SingleLine = getLLVMStyle();
4445   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
4446   verifyFormat("#if 0\n"
4447                "#elif 1\n"
4448                "#endif\n"
4449                "void foo() {\n"
4450                "  if (test) foo2();\n"
4451                "}",
4452                SingleLine);
4453 }
4454 
4455 TEST_F(FormatTest, LayoutBlockInsideParens) {
4456   verifyFormat("functionCall({ int i; });");
4457   verifyFormat("functionCall({\n"
4458                "  int i;\n"
4459                "  int j;\n"
4460                "});");
4461   verifyFormat("functionCall(\n"
4462                "    {\n"
4463                "      int i;\n"
4464                "      int j;\n"
4465                "    },\n"
4466                "    aaaa, bbbb, cccc);");
4467   verifyFormat("functionA(functionB({\n"
4468                "            int i;\n"
4469                "            int j;\n"
4470                "          }),\n"
4471                "          aaaa, bbbb, cccc);");
4472   verifyFormat("functionCall(\n"
4473                "    {\n"
4474                "      int i;\n"
4475                "      int j;\n"
4476                "    },\n"
4477                "    aaaa, bbbb, // comment\n"
4478                "    cccc);");
4479   verifyFormat("functionA(functionB({\n"
4480                "            int i;\n"
4481                "            int j;\n"
4482                "          }),\n"
4483                "          aaaa, bbbb, // comment\n"
4484                "          cccc);");
4485   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
4486   verifyFormat("functionCall(aaaa, bbbb, {\n"
4487                "  int i;\n"
4488                "  int j;\n"
4489                "});");
4490   verifyFormat(
4491       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
4492       "    {\n"
4493       "      int i; // break\n"
4494       "    },\n"
4495       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4496       "                                     ccccccccccccccccc));");
4497   verifyFormat("DEBUG({\n"
4498                "  if (a)\n"
4499                "    f();\n"
4500                "});");
4501 }
4502 
4503 TEST_F(FormatTest, LayoutBlockInsideStatement) {
4504   EXPECT_EQ("SOME_MACRO { int i; }\n"
4505             "int i;",
4506             format("  SOME_MACRO  {int i;}  int i;"));
4507 }
4508 
4509 TEST_F(FormatTest, LayoutNestedBlocks) {
4510   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
4511                "  struct s {\n"
4512                "    int i;\n"
4513                "  };\n"
4514                "  s kBitsToOs[] = {{10}};\n"
4515                "  for (int i = 0; i < 10; ++i)\n"
4516                "    return;\n"
4517                "}");
4518   verifyFormat("call(parameter, {\n"
4519                "  something();\n"
4520                "  // Comment using all columns.\n"
4521                "  somethingelse();\n"
4522                "});",
4523                getLLVMStyleWithColumns(40));
4524   verifyFormat("DEBUG( //\n"
4525                "    { f(); }, a);");
4526   verifyFormat("DEBUG( //\n"
4527                "    {\n"
4528                "      f(); //\n"
4529                "    },\n"
4530                "    a);");
4531 
4532   EXPECT_EQ("call(parameter, {\n"
4533             "  something();\n"
4534             "  // Comment too\n"
4535             "  // looooooooooong.\n"
4536             "  somethingElse();\n"
4537             "});",
4538             format("call(parameter, {\n"
4539                    "  something();\n"
4540                    "  // Comment too looooooooooong.\n"
4541                    "  somethingElse();\n"
4542                    "});",
4543                    getLLVMStyleWithColumns(29)));
4544   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
4545   EXPECT_EQ("DEBUG({ // comment\n"
4546             "  int i;\n"
4547             "});",
4548             format("DEBUG({ // comment\n"
4549                    "int  i;\n"
4550                    "});"));
4551   EXPECT_EQ("DEBUG({\n"
4552             "  int i;\n"
4553             "\n"
4554             "  // comment\n"
4555             "  int j;\n"
4556             "});",
4557             format("DEBUG({\n"
4558                    "  int  i;\n"
4559                    "\n"
4560                    "  // comment\n"
4561                    "  int  j;\n"
4562                    "});"));
4563 
4564   verifyFormat("DEBUG({\n"
4565                "  if (a)\n"
4566                "    return;\n"
4567                "});");
4568   verifyGoogleFormat("DEBUG({\n"
4569                      "  if (a) return;\n"
4570                      "});");
4571   FormatStyle Style = getGoogleStyle();
4572   Style.ColumnLimit = 45;
4573   verifyFormat("Debug(\n"
4574                "    aaaaa,\n"
4575                "    {\n"
4576                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
4577                "    },\n"
4578                "    a);",
4579                Style);
4580 
4581   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
4582 
4583   verifyNoCrash("^{v^{a}}");
4584 }
4585 
4586 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
4587   EXPECT_EQ("#define MACRO()                     \\\n"
4588             "  Debug(aaa, /* force line break */ \\\n"
4589             "        {                           \\\n"
4590             "          int i;                    \\\n"
4591             "          int j;                    \\\n"
4592             "        })",
4593             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
4594                    "          {  int   i;  int  j;   })",
4595                    getGoogleStyle()));
4596 
4597   EXPECT_EQ("#define A                                       \\\n"
4598             "  [] {                                          \\\n"
4599             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
4600             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
4601             "  }",
4602             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
4603                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
4604                    getGoogleStyle()));
4605 }
4606 
4607 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
4608   EXPECT_EQ("{}", format("{}"));
4609   verifyFormat("enum E {};");
4610   verifyFormat("enum E {}");
4611   FormatStyle Style = getLLVMStyle();
4612   Style.SpaceInEmptyBlock = true;
4613   EXPECT_EQ("void f() { }", format("void f() {}", Style));
4614   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
4615   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
4616 }
4617 
4618 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
4619   FormatStyle Style = getLLVMStyle();
4620   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
4621   Style.MacroBlockEnd = "^[A-Z_]+_END$";
4622   verifyFormat("FOO_BEGIN\n"
4623                "  FOO_ENTRY\n"
4624                "FOO_END",
4625                Style);
4626   verifyFormat("FOO_BEGIN\n"
4627                "  NESTED_FOO_BEGIN\n"
4628                "    NESTED_FOO_ENTRY\n"
4629                "  NESTED_FOO_END\n"
4630                "FOO_END",
4631                Style);
4632   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
4633                "  int x;\n"
4634                "  x = 1;\n"
4635                "FOO_END(Baz)",
4636                Style);
4637 }
4638 
4639 //===----------------------------------------------------------------------===//
4640 // Line break tests.
4641 //===----------------------------------------------------------------------===//
4642 
4643 TEST_F(FormatTest, PreventConfusingIndents) {
4644   verifyFormat(
4645       "void f() {\n"
4646       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
4647       "                         parameter, parameter, parameter)),\n"
4648       "                     SecondLongCall(parameter));\n"
4649       "}");
4650   verifyFormat(
4651       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4652       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4653       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4654       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
4655   verifyFormat(
4656       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4657       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
4658       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
4659       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
4660   verifyFormat(
4661       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4662       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
4663       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
4664       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
4665   verifyFormat("int a = bbbb && ccc &&\n"
4666                "        fffff(\n"
4667                "#define A Just forcing a new line\n"
4668                "            ddd);");
4669 }
4670 
4671 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
4672   verifyFormat(
4673       "bool aaaaaaa =\n"
4674       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
4675       "    bbbbbbbb();");
4676   verifyFormat(
4677       "bool aaaaaaa =\n"
4678       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
4679       "    bbbbbbbb();");
4680 
4681   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
4682                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
4683                "    ccccccccc == ddddddddddd;");
4684   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
4685                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
4686                "    ccccccccc == ddddddddddd;");
4687   verifyFormat(
4688       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4689       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
4690       "    ccccccccc == ddddddddddd;");
4691 
4692   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
4693                "                 aaaaaa) &&\n"
4694                "         bbbbbb && cccccc;");
4695   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
4696                "                 aaaaaa) >>\n"
4697                "         bbbbbb;");
4698   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
4699                "    SourceMgr.getSpellingColumnNumber(\n"
4700                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
4701                "    1);");
4702 
4703   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4704                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
4705                "    cccccc) {\n}");
4706   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4707                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
4708                "              cccccc) {\n}");
4709   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4710                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
4711                "              cccccc) {\n}");
4712   verifyFormat("b = a &&\n"
4713                "    // Comment\n"
4714                "    b.c && d;");
4715 
4716   // If the LHS of a comparison is not a binary expression itself, the
4717   // additional linebreak confuses many people.
4718   verifyFormat(
4719       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4720       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
4721       "}");
4722   verifyFormat(
4723       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4724       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4725       "}");
4726   verifyFormat(
4727       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
4728       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4729       "}");
4730   verifyFormat(
4731       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4732       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
4733       "}");
4734   // Even explicit parentheses stress the precedence enough to make the
4735   // additional break unnecessary.
4736   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4737                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4738                "}");
4739   // This cases is borderline, but with the indentation it is still readable.
4740   verifyFormat(
4741       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4742       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4743       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4744       "}",
4745       getLLVMStyleWithColumns(75));
4746 
4747   // If the LHS is a binary expression, we should still use the additional break
4748   // as otherwise the formatting hides the operator precedence.
4749   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4750                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4751                "    5) {\n"
4752                "}");
4753   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4754                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
4755                "    5) {\n"
4756                "}");
4757 
4758   FormatStyle OnePerLine = getLLVMStyle();
4759   OnePerLine.BinPackParameters = false;
4760   verifyFormat(
4761       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4762       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4763       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
4764       OnePerLine);
4765 
4766   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
4767                "                .aaa(aaaaaaaaaaaaa) *\n"
4768                "            aaaaaaa +\n"
4769                "        aaaaaaa;",
4770                getLLVMStyleWithColumns(40));
4771 }
4772 
4773 TEST_F(FormatTest, ExpressionIndentation) {
4774   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4775                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4776                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4777                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4778                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4779                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
4780                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4781                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
4782                "                 ccccccccccccccccccccccccccccccccccccccccc;");
4783   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4784                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4785                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4786                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4787   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4788                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4789                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4790                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4791   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4792                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4793                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4794                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4795   verifyFormat("if () {\n"
4796                "} else if (aaaaa && bbbbb > // break\n"
4797                "                        ccccc) {\n"
4798                "}");
4799   verifyFormat("if () {\n"
4800                "} else if constexpr (aaaaa && bbbbb > // break\n"
4801                "                                  ccccc) {\n"
4802                "}");
4803   verifyFormat("if () {\n"
4804                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
4805                "                                  ccccc) {\n"
4806                "}");
4807   verifyFormat("if () {\n"
4808                "} else if (aaaaa &&\n"
4809                "           bbbbb > // break\n"
4810                "               ccccc &&\n"
4811                "           ddddd) {\n"
4812                "}");
4813 
4814   // Presence of a trailing comment used to change indentation of b.
4815   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
4816                "       b;\n"
4817                "return aaaaaaaaaaaaaaaaaaa +\n"
4818                "       b; //",
4819                getLLVMStyleWithColumns(30));
4820 }
4821 
4822 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
4823   // Not sure what the best system is here. Like this, the LHS can be found
4824   // immediately above an operator (everything with the same or a higher
4825   // indent). The RHS is aligned right of the operator and so compasses
4826   // everything until something with the same indent as the operator is found.
4827   // FIXME: Is this a good system?
4828   FormatStyle Style = getLLVMStyle();
4829   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4830   verifyFormat(
4831       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4832       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4833       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4834       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4835       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4836       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4837       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4838       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4839       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
4840       Style);
4841   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4842                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4843                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4844                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4845                Style);
4846   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4847                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4848                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4849                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4850                Style);
4851   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4852                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4853                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4854                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4855                Style);
4856   verifyFormat("if () {\n"
4857                "} else if (aaaaa\n"
4858                "           && bbbbb // break\n"
4859                "                  > ccccc) {\n"
4860                "}",
4861                Style);
4862   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4863                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4864                Style);
4865   verifyFormat("return (a)\n"
4866                "       // comment\n"
4867                "       + b;",
4868                Style);
4869   verifyFormat(
4870       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4871       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4872       "             + cc;",
4873       Style);
4874 
4875   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4876                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4877                Style);
4878 
4879   // Forced by comments.
4880   verifyFormat(
4881       "unsigned ContentSize =\n"
4882       "    sizeof(int16_t)   // DWARF ARange version number\n"
4883       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4884       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
4885       "    + sizeof(int8_t); // Segment Size (in bytes)");
4886 
4887   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
4888                "       == boost::fusion::at_c<1>(iiii).second;",
4889                Style);
4890 
4891   Style.ColumnLimit = 60;
4892   verifyFormat("zzzzzzzzzz\n"
4893                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4894                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
4895                Style);
4896 
4897   Style.ColumnLimit = 80;
4898   Style.IndentWidth = 4;
4899   Style.TabWidth = 4;
4900   Style.UseTab = FormatStyle::UT_Always;
4901   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4902   Style.AlignOperands = FormatStyle::OAS_DontAlign;
4903   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
4904             "\t&& (someOtherLongishConditionPart1\n"
4905             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
4906             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
4907                    "(someOtherLongishConditionPart1 || "
4908                    "someOtherEvenLongerNestedConditionPart2);",
4909                    Style));
4910 }
4911 
4912 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
4913   FormatStyle Style = getLLVMStyle();
4914   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4915   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
4916 
4917   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4918                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4919                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4920                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4921                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4922                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4923                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4924                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4925                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
4926                Style);
4927   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4928                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4929                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4930                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4931                Style);
4932   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4933                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4934                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4935                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4936                Style);
4937   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4938                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4939                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4940                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4941                Style);
4942   verifyFormat("if () {\n"
4943                "} else if (aaaaa\n"
4944                "           && bbbbb // break\n"
4945                "                  > ccccc) {\n"
4946                "}",
4947                Style);
4948   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4949                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4950                Style);
4951   verifyFormat("return (a)\n"
4952                "     // comment\n"
4953                "     + b;",
4954                Style);
4955   verifyFormat(
4956       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4957       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4958       "           + cc;",
4959       Style);
4960   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
4961                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
4962                "                        : 3333333333333333;",
4963                Style);
4964   verifyFormat(
4965       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
4966       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
4967       "                                             : eeeeeeeeeeeeeeeeee)\n"
4968       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
4969       "                        : 3333333333333333;",
4970       Style);
4971   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4972                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4973                Style);
4974 
4975   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
4976                "    == boost::fusion::at_c<1>(iiii).second;",
4977                Style);
4978 
4979   Style.ColumnLimit = 60;
4980   verifyFormat("zzzzzzzzzzzzz\n"
4981                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4982                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
4983                Style);
4984 
4985   // Forced by comments.
4986   Style.ColumnLimit = 80;
4987   verifyFormat(
4988       "unsigned ContentSize\n"
4989       "    = sizeof(int16_t) // DWARF ARange version number\n"
4990       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4991       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
4992       "    + sizeof(int8_t); // Segment Size (in bytes)",
4993       Style);
4994 
4995   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4996   verifyFormat(
4997       "unsigned ContentSize =\n"
4998       "    sizeof(int16_t)   // DWARF ARange version number\n"
4999       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5000       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5001       "    + sizeof(int8_t); // Segment Size (in bytes)",
5002       Style);
5003 
5004   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5005   verifyFormat(
5006       "unsigned ContentSize =\n"
5007       "    sizeof(int16_t)   // DWARF ARange version number\n"
5008       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
5009       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
5010       "    + sizeof(int8_t); // Segment Size (in bytes)",
5011       Style);
5012 }
5013 
5014 TEST_F(FormatTest, EnforcedOperatorWraps) {
5015   // Here we'd like to wrap after the || operators, but a comment is forcing an
5016   // earlier wrap.
5017   verifyFormat("bool x = aaaaa //\n"
5018                "         || bbbbb\n"
5019                "         //\n"
5020                "         || cccc;");
5021 }
5022 
5023 TEST_F(FormatTest, NoOperandAlignment) {
5024   FormatStyle Style = getLLVMStyle();
5025   Style.AlignOperands = FormatStyle::OAS_DontAlign;
5026   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
5027                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5028                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5029                Style);
5030   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5031   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5032                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5033                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5034                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5035                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5036                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5037                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5038                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5039                "        > ccccccccccccccccccccccccccccccccccccccccc;",
5040                Style);
5041 
5042   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5043                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5044                "    + cc;",
5045                Style);
5046   verifyFormat("int a = aa\n"
5047                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5048                "        * cccccccccccccccccccccccccccccccccccc;\n",
5049                Style);
5050 
5051   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5052   verifyFormat("return (a > b\n"
5053                "    // comment1\n"
5054                "    // comment2\n"
5055                "    || c);",
5056                Style);
5057 }
5058 
5059 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5060   FormatStyle Style = getLLVMStyle();
5061   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5062   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5063                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5064                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5065                Style);
5066 }
5067 
5068 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5069   FormatStyle Style = getLLVMStyle();
5070   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5071   Style.BinPackArguments = false;
5072   Style.ColumnLimit = 40;
5073   verifyFormat("void test() {\n"
5074                "  someFunction(\n"
5075                "      this + argument + is + quite\n"
5076                "      + long + so + it + gets + wrapped\n"
5077                "      + but + remains + bin - packed);\n"
5078                "}",
5079                Style);
5080   verifyFormat("void test() {\n"
5081                "  someFunction(arg1,\n"
5082                "               this + argument + is\n"
5083                "                   + quite + long + so\n"
5084                "                   + it + gets + wrapped\n"
5085                "                   + but + remains + bin\n"
5086                "                   - packed,\n"
5087                "               arg3);\n"
5088                "}",
5089                Style);
5090   verifyFormat("void test() {\n"
5091                "  someFunction(\n"
5092                "      arg1,\n"
5093                "      this + argument + has\n"
5094                "          + anotherFunc(nested,\n"
5095                "                        calls + whose\n"
5096                "                            + arguments\n"
5097                "                            + are + also\n"
5098                "                            + wrapped,\n"
5099                "                        in + addition)\n"
5100                "          + to + being + bin - packed,\n"
5101                "      arg3);\n"
5102                "}",
5103                Style);
5104 
5105   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5106   verifyFormat("void test() {\n"
5107                "  someFunction(\n"
5108                "      arg1,\n"
5109                "      this + argument + has +\n"
5110                "          anotherFunc(nested,\n"
5111                "                      calls + whose +\n"
5112                "                          arguments +\n"
5113                "                          are + also +\n"
5114                "                          wrapped,\n"
5115                "                      in + addition) +\n"
5116                "          to + being + bin - packed,\n"
5117                "      arg3);\n"
5118                "}",
5119                Style);
5120 }
5121 
5122 TEST_F(FormatTest, ConstructorInitializers) {
5123   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5124   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5125                getLLVMStyleWithColumns(45));
5126   verifyFormat("Constructor()\n"
5127                "    : Inttializer(FitsOnTheLine) {}",
5128                getLLVMStyleWithColumns(44));
5129   verifyFormat("Constructor()\n"
5130                "    : Inttializer(FitsOnTheLine) {}",
5131                getLLVMStyleWithColumns(43));
5132 
5133   verifyFormat("template <typename T>\n"
5134                "Constructor() : Initializer(FitsOnTheLine) {}",
5135                getLLVMStyleWithColumns(45));
5136 
5137   verifyFormat(
5138       "SomeClass::Constructor()\n"
5139       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5140 
5141   verifyFormat(
5142       "SomeClass::Constructor()\n"
5143       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5144       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5145   verifyFormat(
5146       "SomeClass::Constructor()\n"
5147       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5148       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5149   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5150                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5151                "    : aaaaaaaaaa(aaaaaa) {}");
5152 
5153   verifyFormat("Constructor()\n"
5154                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5155                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5156                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5157                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5158 
5159   verifyFormat("Constructor()\n"
5160                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5161                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5162 
5163   verifyFormat("Constructor(int Parameter = 0)\n"
5164                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5165                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5166   verifyFormat("Constructor()\n"
5167                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5168                "}",
5169                getLLVMStyleWithColumns(60));
5170   verifyFormat("Constructor()\n"
5171                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5172                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5173 
5174   // Here a line could be saved by splitting the second initializer onto two
5175   // lines, but that is not desirable.
5176   verifyFormat("Constructor()\n"
5177                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5178                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5179                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5180 
5181   FormatStyle OnePerLine = getLLVMStyle();
5182   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5183   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
5184   verifyFormat("SomeClass::Constructor()\n"
5185                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5186                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5187                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5188                OnePerLine);
5189   verifyFormat("SomeClass::Constructor()\n"
5190                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5191                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5192                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5193                OnePerLine);
5194   verifyFormat("MyClass::MyClass(int var)\n"
5195                "    : some_var_(var),            // 4 space indent\n"
5196                "      some_other_var_(var + 1) { // lined up\n"
5197                "}",
5198                OnePerLine);
5199   verifyFormat("Constructor()\n"
5200                "    : aaaaa(aaaaaa),\n"
5201                "      aaaaa(aaaaaa),\n"
5202                "      aaaaa(aaaaaa),\n"
5203                "      aaaaa(aaaaaa),\n"
5204                "      aaaaa(aaaaaa) {}",
5205                OnePerLine);
5206   verifyFormat("Constructor()\n"
5207                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5208                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
5209                OnePerLine);
5210   OnePerLine.BinPackParameters = false;
5211   verifyFormat(
5212       "Constructor()\n"
5213       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5214       "          aaaaaaaaaaa().aaa(),\n"
5215       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5216       OnePerLine);
5217   OnePerLine.ColumnLimit = 60;
5218   verifyFormat("Constructor()\n"
5219                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5220                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5221                OnePerLine);
5222 
5223   EXPECT_EQ("Constructor()\n"
5224             "    : // Comment forcing unwanted break.\n"
5225             "      aaaa(aaaa) {}",
5226             format("Constructor() :\n"
5227                    "    // Comment forcing unwanted break.\n"
5228                    "    aaaa(aaaa) {}"));
5229 }
5230 
5231 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
5232   FormatStyle Style = getLLVMStyle();
5233   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5234   Style.ColumnLimit = 60;
5235   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5236   Style.AllowAllConstructorInitializersOnNextLine = true;
5237   Style.BinPackParameters = false;
5238 
5239   for (int i = 0; i < 4; ++i) {
5240     // Test all combinations of parameters that should not have an effect.
5241     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
5242     Style.AllowAllArgumentsOnNextLine = i & 2;
5243 
5244     Style.AllowAllConstructorInitializersOnNextLine = true;
5245     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5246     verifyFormat("Constructor()\n"
5247                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5248                  Style);
5249     verifyFormat("Constructor() : a(a), b(b) {}", Style);
5250 
5251     Style.AllowAllConstructorInitializersOnNextLine = false;
5252     verifyFormat("Constructor()\n"
5253                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5254                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5255                  Style);
5256     verifyFormat("Constructor() : a(a), b(b) {}", Style);
5257 
5258     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
5259     Style.AllowAllConstructorInitializersOnNextLine = true;
5260     verifyFormat("Constructor()\n"
5261                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5262                  Style);
5263 
5264     Style.AllowAllConstructorInitializersOnNextLine = false;
5265     verifyFormat("Constructor()\n"
5266                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5267                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5268                  Style);
5269 
5270     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5271     Style.AllowAllConstructorInitializersOnNextLine = true;
5272     verifyFormat("Constructor() :\n"
5273                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5274                  Style);
5275 
5276     Style.AllowAllConstructorInitializersOnNextLine = false;
5277     verifyFormat("Constructor() :\n"
5278                  "    aaaaaaaaaaaaaaaaaa(a),\n"
5279                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5280                  Style);
5281   }
5282 
5283   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
5284   // AllowAllConstructorInitializersOnNextLine in all
5285   // BreakConstructorInitializers modes
5286   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5287   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5288   Style.AllowAllConstructorInitializersOnNextLine = false;
5289   verifyFormat("SomeClassWithALongName::Constructor(\n"
5290                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
5291                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5292                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5293                Style);
5294 
5295   Style.AllowAllConstructorInitializersOnNextLine = true;
5296   verifyFormat("SomeClassWithALongName::Constructor(\n"
5297                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5298                "    int bbbbbbbbbbbbb,\n"
5299                "    int cccccccccccccccc)\n"
5300                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5301                Style);
5302 
5303   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5304   Style.AllowAllConstructorInitializersOnNextLine = false;
5305   verifyFormat("SomeClassWithALongName::Constructor(\n"
5306                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5307                "    int bbbbbbbbbbbbb)\n"
5308                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5309                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5310                Style);
5311 
5312   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
5313 
5314   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5315   verifyFormat("SomeClassWithALongName::Constructor(\n"
5316                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
5317                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5318                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5319                Style);
5320 
5321   Style.AllowAllConstructorInitializersOnNextLine = true;
5322   verifyFormat("SomeClassWithALongName::Constructor(\n"
5323                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5324                "    int bbbbbbbbbbbbb,\n"
5325                "    int cccccccccccccccc)\n"
5326                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5327                Style);
5328 
5329   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5330   Style.AllowAllConstructorInitializersOnNextLine = false;
5331   verifyFormat("SomeClassWithALongName::Constructor(\n"
5332                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5333                "    int bbbbbbbbbbbbb)\n"
5334                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5335                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5336                Style);
5337 
5338   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5339   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5340   verifyFormat("SomeClassWithALongName::Constructor(\n"
5341                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
5342                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5343                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5344                Style);
5345 
5346   Style.AllowAllConstructorInitializersOnNextLine = true;
5347   verifyFormat("SomeClassWithALongName::Constructor(\n"
5348                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5349                "    int bbbbbbbbbbbbb,\n"
5350                "    int cccccccccccccccc) :\n"
5351                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5352                Style);
5353 
5354   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5355   Style.AllowAllConstructorInitializersOnNextLine = false;
5356   verifyFormat("SomeClassWithALongName::Constructor(\n"
5357                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5358                "    int bbbbbbbbbbbbb) :\n"
5359                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5360                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5361                Style);
5362 }
5363 
5364 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
5365   FormatStyle Style = getLLVMStyle();
5366   Style.ColumnLimit = 60;
5367   Style.BinPackArguments = false;
5368   for (int i = 0; i < 4; ++i) {
5369     // Test all combinations of parameters that should not have an effect.
5370     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
5371     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
5372 
5373     Style.AllowAllArgumentsOnNextLine = true;
5374     verifyFormat("void foo() {\n"
5375                  "  FunctionCallWithReallyLongName(\n"
5376                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
5377                  "}",
5378                  Style);
5379     Style.AllowAllArgumentsOnNextLine = false;
5380     verifyFormat("void foo() {\n"
5381                  "  FunctionCallWithReallyLongName(\n"
5382                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5383                  "      bbbbbbbbbbbb);\n"
5384                  "}",
5385                  Style);
5386 
5387     Style.AllowAllArgumentsOnNextLine = true;
5388     verifyFormat("void foo() {\n"
5389                  "  auto VariableWithReallyLongName = {\n"
5390                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
5391                  "}",
5392                  Style);
5393     Style.AllowAllArgumentsOnNextLine = false;
5394     verifyFormat("void foo() {\n"
5395                  "  auto VariableWithReallyLongName = {\n"
5396                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5397                  "      bbbbbbbbbbbb};\n"
5398                  "}",
5399                  Style);
5400   }
5401 
5402   // This parameter should not affect declarations.
5403   Style.BinPackParameters = false;
5404   Style.AllowAllArgumentsOnNextLine = false;
5405   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5406   verifyFormat("void FunctionCallWithReallyLongName(\n"
5407                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
5408                Style);
5409   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5410   verifyFormat("void FunctionCallWithReallyLongName(\n"
5411                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
5412                "    int bbbbbbbbbbbb);",
5413                Style);
5414 }
5415 
5416 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
5417   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
5418   // and BAS_Align.
5419   auto Style = getLLVMStyle();
5420   Style.ColumnLimit = 35;
5421   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
5422                     "void functionDecl(int A, int B, int C);";
5423   Style.AllowAllArgumentsOnNextLine = false;
5424   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5425   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
5426                       "    paramC);\n"
5427                       "void functionDecl(int A, int B,\n"
5428                       "    int C);"),
5429             format(Input, Style));
5430   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5431   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
5432                       "             paramC);\n"
5433                       "void functionDecl(int A, int B,\n"
5434                       "                  int C);"),
5435             format(Input, Style));
5436   // However, BAS_AlwaysBreak should take precedence over
5437   // AllowAllArgumentsOnNextLine.
5438   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5439   EXPECT_EQ(StringRef("functionCall(\n"
5440                       "    paramA, paramB, paramC);\n"
5441                       "void functionDecl(\n"
5442                       "    int A, int B, int C);"),
5443             format(Input, Style));
5444 
5445   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
5446   // first argument.
5447   Style.AllowAllArgumentsOnNextLine = true;
5448   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5449   EXPECT_EQ(StringRef("functionCall(\n"
5450                       "    paramA, paramB, paramC);\n"
5451                       "void functionDecl(\n"
5452                       "    int A, int B, int C);"),
5453             format(Input, Style));
5454   // It wouldn't fit on one line with aligned parameters so this setting
5455   // doesn't change anything for BAS_Align.
5456   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5457   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
5458                       "             paramC);\n"
5459                       "void functionDecl(int A, int B,\n"
5460                       "                  int C);"),
5461             format(Input, Style));
5462   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5463   EXPECT_EQ(StringRef("functionCall(\n"
5464                       "    paramA, paramB, paramC);\n"
5465                       "void functionDecl(\n"
5466                       "    int A, int B, int C);"),
5467             format(Input, Style));
5468 }
5469 
5470 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
5471   FormatStyle Style = getLLVMStyle();
5472   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5473 
5474   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5475   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
5476                getStyleWithColumns(Style, 45));
5477   verifyFormat("Constructor() :\n"
5478                "    Initializer(FitsOnTheLine) {}",
5479                getStyleWithColumns(Style, 44));
5480   verifyFormat("Constructor() :\n"
5481                "    Initializer(FitsOnTheLine) {}",
5482                getStyleWithColumns(Style, 43));
5483 
5484   verifyFormat("template <typename T>\n"
5485                "Constructor() : Initializer(FitsOnTheLine) {}",
5486                getStyleWithColumns(Style, 50));
5487   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5488   verifyFormat(
5489       "SomeClass::Constructor() :\n"
5490       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
5491       Style);
5492 
5493   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
5494   verifyFormat(
5495       "SomeClass::Constructor() :\n"
5496       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
5497       Style);
5498 
5499   verifyFormat(
5500       "SomeClass::Constructor() :\n"
5501       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5502       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5503       Style);
5504   verifyFormat(
5505       "SomeClass::Constructor() :\n"
5506       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5507       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
5508       Style);
5509   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5510                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5511                "    aaaaaaaaaa(aaaaaa) {}",
5512                Style);
5513 
5514   verifyFormat("Constructor() :\n"
5515                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5516                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5517                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5518                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
5519                Style);
5520 
5521   verifyFormat("Constructor() :\n"
5522                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5523                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5524                Style);
5525 
5526   verifyFormat("Constructor(int Parameter = 0) :\n"
5527                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5528                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
5529                Style);
5530   verifyFormat("Constructor() :\n"
5531                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5532                "}",
5533                getStyleWithColumns(Style, 60));
5534   verifyFormat("Constructor() :\n"
5535                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5536                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
5537                Style);
5538 
5539   // Here a line could be saved by splitting the second initializer onto two
5540   // lines, but that is not desirable.
5541   verifyFormat("Constructor() :\n"
5542                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5543                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
5544                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5545                Style);
5546 
5547   FormatStyle OnePerLine = Style;
5548   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5549   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
5550   verifyFormat("SomeClass::Constructor() :\n"
5551                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5552                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5553                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5554                OnePerLine);
5555   verifyFormat("SomeClass::Constructor() :\n"
5556                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5557                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5558                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5559                OnePerLine);
5560   verifyFormat("MyClass::MyClass(int var) :\n"
5561                "    some_var_(var),            // 4 space indent\n"
5562                "    some_other_var_(var + 1) { // lined up\n"
5563                "}",
5564                OnePerLine);
5565   verifyFormat("Constructor() :\n"
5566                "    aaaaa(aaaaaa),\n"
5567                "    aaaaa(aaaaaa),\n"
5568                "    aaaaa(aaaaaa),\n"
5569                "    aaaaa(aaaaaa),\n"
5570                "    aaaaa(aaaaaa) {}",
5571                OnePerLine);
5572   verifyFormat("Constructor() :\n"
5573                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5574                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
5575                OnePerLine);
5576   OnePerLine.BinPackParameters = false;
5577   verifyFormat("Constructor() :\n"
5578                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5579                "        aaaaaaaaaaa().aaa(),\n"
5580                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5581                OnePerLine);
5582   OnePerLine.ColumnLimit = 60;
5583   verifyFormat("Constructor() :\n"
5584                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5585                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5586                OnePerLine);
5587 
5588   EXPECT_EQ("Constructor() :\n"
5589             "    // Comment forcing unwanted break.\n"
5590             "    aaaa(aaaa) {}",
5591             format("Constructor() :\n"
5592                    "    // Comment forcing unwanted break.\n"
5593                    "    aaaa(aaaa) {}",
5594                    Style));
5595 
5596   Style.ColumnLimit = 0;
5597   verifyFormat("SomeClass::Constructor() :\n"
5598                "    a(a) {}",
5599                Style);
5600   verifyFormat("SomeClass::Constructor() noexcept :\n"
5601                "    a(a) {}",
5602                Style);
5603   verifyFormat("SomeClass::Constructor() :\n"
5604                "    a(a), b(b), c(c) {}",
5605                Style);
5606   verifyFormat("SomeClass::Constructor() :\n"
5607                "    a(a) {\n"
5608                "  foo();\n"
5609                "  bar();\n"
5610                "}",
5611                Style);
5612 
5613   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5614   verifyFormat("SomeClass::Constructor() :\n"
5615                "    a(a), b(b), c(c) {\n"
5616                "}",
5617                Style);
5618   verifyFormat("SomeClass::Constructor() :\n"
5619                "    a(a) {\n"
5620                "}",
5621                Style);
5622 
5623   Style.ColumnLimit = 80;
5624   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
5625   Style.ConstructorInitializerIndentWidth = 2;
5626   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
5627   verifyFormat("SomeClass::Constructor() :\n"
5628                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5629                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
5630                Style);
5631 
5632   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
5633   // well
5634   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
5635   verifyFormat(
5636       "class SomeClass\n"
5637       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5638       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5639       Style);
5640   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
5641   verifyFormat(
5642       "class SomeClass\n"
5643       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5644       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5645       Style);
5646   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
5647   verifyFormat(
5648       "class SomeClass :\n"
5649       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5650       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5651       Style);
5652   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
5653   verifyFormat(
5654       "class SomeClass\n"
5655       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5656       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5657       Style);
5658 }
5659 
5660 #ifndef EXPENSIVE_CHECKS
5661 // Expensive checks enables libstdc++ checking which includes validating the
5662 // state of ranges used in std::priority_queue - this blows out the
5663 // runtime/scalability of the function and makes this test unacceptably slow.
5664 TEST_F(FormatTest, MemoizationTests) {
5665   // This breaks if the memoization lookup does not take \c Indent and
5666   // \c LastSpace into account.
5667   verifyFormat(
5668       "extern CFRunLoopTimerRef\n"
5669       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
5670       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
5671       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
5672       "                     CFRunLoopTimerContext *context) {}");
5673 
5674   // Deep nesting somewhat works around our memoization.
5675   verifyFormat(
5676       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5677       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5678       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5679       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5680       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
5681       getLLVMStyleWithColumns(65));
5682   verifyFormat(
5683       "aaaaa(\n"
5684       "    aaaaa,\n"
5685       "    aaaaa(\n"
5686       "        aaaaa,\n"
5687       "        aaaaa(\n"
5688       "            aaaaa,\n"
5689       "            aaaaa(\n"
5690       "                aaaaa,\n"
5691       "                aaaaa(\n"
5692       "                    aaaaa,\n"
5693       "                    aaaaa(\n"
5694       "                        aaaaa,\n"
5695       "                        aaaaa(\n"
5696       "                            aaaaa,\n"
5697       "                            aaaaa(\n"
5698       "                                aaaaa,\n"
5699       "                                aaaaa(\n"
5700       "                                    aaaaa,\n"
5701       "                                    aaaaa(\n"
5702       "                                        aaaaa,\n"
5703       "                                        aaaaa(\n"
5704       "                                            aaaaa,\n"
5705       "                                            aaaaa(\n"
5706       "                                                aaaaa,\n"
5707       "                                                aaaaa))))))))))));",
5708       getLLVMStyleWithColumns(65));
5709   verifyFormat(
5710       "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"
5711       "                                  a),\n"
5712       "                                a),\n"
5713       "                              a),\n"
5714       "                            a),\n"
5715       "                          a),\n"
5716       "                        a),\n"
5717       "                      a),\n"
5718       "                    a),\n"
5719       "                  a),\n"
5720       "                a),\n"
5721       "              a),\n"
5722       "            a),\n"
5723       "          a),\n"
5724       "        a),\n"
5725       "      a),\n"
5726       "    a),\n"
5727       "  a)",
5728       getLLVMStyleWithColumns(65));
5729 
5730   // This test takes VERY long when memoization is broken.
5731   FormatStyle OnePerLine = getLLVMStyle();
5732   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5733   OnePerLine.BinPackParameters = false;
5734   std::string input = "Constructor()\n"
5735                       "    : aaaa(a,\n";
5736   for (unsigned i = 0, e = 80; i != e; ++i) {
5737     input += "           a,\n";
5738   }
5739   input += "           a) {}";
5740   verifyFormat(input, OnePerLine);
5741 }
5742 #endif
5743 
5744 TEST_F(FormatTest, BreaksAsHighAsPossible) {
5745   verifyFormat(
5746       "void f() {\n"
5747       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
5748       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
5749       "    f();\n"
5750       "}");
5751   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
5752                "    Intervals[i - 1].getRange().getLast()) {\n}");
5753 }
5754 
5755 TEST_F(FormatTest, BreaksFunctionDeclarations) {
5756   // Principially, we break function declarations in a certain order:
5757   // 1) break amongst arguments.
5758   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
5759                "                              Cccccccccccccc cccccccccccccc);");
5760   verifyFormat("template <class TemplateIt>\n"
5761                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
5762                "                            TemplateIt *stop) {}");
5763 
5764   // 2) break after return type.
5765   verifyFormat(
5766       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5767       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
5768       getGoogleStyle());
5769 
5770   // 3) break after (.
5771   verifyFormat(
5772       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
5773       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
5774       getGoogleStyle());
5775 
5776   // 4) break before after nested name specifiers.
5777   verifyFormat(
5778       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5779       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
5780       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
5781       getGoogleStyle());
5782 
5783   // However, there are exceptions, if a sufficient amount of lines can be
5784   // saved.
5785   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
5786   // more adjusting.
5787   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
5788                "                                  Cccccccccccccc cccccccccc,\n"
5789                "                                  Cccccccccccccc cccccccccc,\n"
5790                "                                  Cccccccccccccc cccccccccc,\n"
5791                "                                  Cccccccccccccc cccccccccc);");
5792   verifyFormat(
5793       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5794       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5795       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5796       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
5797       getGoogleStyle());
5798   verifyFormat(
5799       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
5800       "                                          Cccccccccccccc cccccccccc,\n"
5801       "                                          Cccccccccccccc cccccccccc,\n"
5802       "                                          Cccccccccccccc cccccccccc,\n"
5803       "                                          Cccccccccccccc cccccccccc,\n"
5804       "                                          Cccccccccccccc cccccccccc,\n"
5805       "                                          Cccccccccccccc cccccccccc);");
5806   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
5807                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5808                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5809                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5810                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
5811 
5812   // Break after multi-line parameters.
5813   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5814                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5815                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5816                "    bbbb bbbb);");
5817   verifyFormat("void SomeLoooooooooooongFunction(\n"
5818                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5819                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5820                "    int bbbbbbbbbbbbb);");
5821 
5822   // Treat overloaded operators like other functions.
5823   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5824                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
5825   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5826                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
5827   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5828                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
5829   verifyGoogleFormat(
5830       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
5831       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
5832   verifyGoogleFormat(
5833       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
5834       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
5835   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5836                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
5837   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
5838                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
5839   verifyGoogleFormat(
5840       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
5841       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5842       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
5843   verifyGoogleFormat("template <typename T>\n"
5844                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5845                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
5846                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
5847 
5848   FormatStyle Style = getLLVMStyle();
5849   Style.PointerAlignment = FormatStyle::PAS_Left;
5850   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5851                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
5852                Style);
5853   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
5854                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5855                Style);
5856 }
5857 
5858 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
5859   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
5860   // Prefer keeping `::` followed by `operator` together.
5861   EXPECT_EQ("const aaaa::bbbbbbb &\n"
5862             "ccccccccc::operator++() {\n"
5863             "  stuff();\n"
5864             "}",
5865             format("const aaaa::bbbbbbb\n"
5866                    "&ccccccccc::operator++() { stuff(); }",
5867                    getLLVMStyleWithColumns(40)));
5868 }
5869 
5870 TEST_F(FormatTest, TrailingReturnType) {
5871   verifyFormat("auto foo() -> int;\n");
5872   // correct trailing return type spacing
5873   verifyFormat("auto operator->() -> int;\n");
5874   verifyFormat("auto operator++(int) -> int;\n");
5875 
5876   verifyFormat("struct S {\n"
5877                "  auto bar() const -> int;\n"
5878                "};");
5879   verifyFormat("template <size_t Order, typename T>\n"
5880                "auto load_img(const std::string &filename)\n"
5881                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
5882   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
5883                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
5884   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
5885   verifyFormat("template <typename T>\n"
5886                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
5887                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
5888 
5889   // Not trailing return types.
5890   verifyFormat("void f() { auto a = b->c(); }");
5891 }
5892 
5893 TEST_F(FormatTest, DeductionGuides) {
5894   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
5895   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
5896   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
5897   verifyFormat(
5898       "template <class... T>\n"
5899       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
5900   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
5901   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
5902   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
5903   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
5904   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
5905   verifyFormat("template <class T> x() -> x<1>;");
5906   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
5907 
5908   // Ensure not deduction guides.
5909   verifyFormat("c()->f<int>();");
5910   verifyFormat("x()->foo<1>;");
5911   verifyFormat("x = p->foo<3>();");
5912   verifyFormat("x()->x<1>();");
5913   verifyFormat("x()->x<1>;");
5914 }
5915 
5916 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
5917   // Avoid breaking before trailing 'const' or other trailing annotations, if
5918   // they are not function-like.
5919   FormatStyle Style = getGoogleStyle();
5920   Style.ColumnLimit = 47;
5921   verifyFormat("void someLongFunction(\n"
5922                "    int someLoooooooooooooongParameter) const {\n}",
5923                getLLVMStyleWithColumns(47));
5924   verifyFormat("LoooooongReturnType\n"
5925                "someLoooooooongFunction() const {}",
5926                getLLVMStyleWithColumns(47));
5927   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
5928                "    const {}",
5929                Style);
5930   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5931                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
5932   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5933                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
5934   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5935                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
5936   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
5937                "                   aaaaaaaaaaa aaaaa) const override;");
5938   verifyGoogleFormat(
5939       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5940       "    const override;");
5941 
5942   // Even if the first parameter has to be wrapped.
5943   verifyFormat("void someLongFunction(\n"
5944                "    int someLongParameter) const {}",
5945                getLLVMStyleWithColumns(46));
5946   verifyFormat("void someLongFunction(\n"
5947                "    int someLongParameter) const {}",
5948                Style);
5949   verifyFormat("void someLongFunction(\n"
5950                "    int someLongParameter) override {}",
5951                Style);
5952   verifyFormat("void someLongFunction(\n"
5953                "    int someLongParameter) OVERRIDE {}",
5954                Style);
5955   verifyFormat("void someLongFunction(\n"
5956                "    int someLongParameter) final {}",
5957                Style);
5958   verifyFormat("void someLongFunction(\n"
5959                "    int someLongParameter) FINAL {}",
5960                Style);
5961   verifyFormat("void someLongFunction(\n"
5962                "    int parameter) const override {}",
5963                Style);
5964 
5965   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
5966   verifyFormat("void someLongFunction(\n"
5967                "    int someLongParameter) const\n"
5968                "{\n"
5969                "}",
5970                Style);
5971 
5972   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
5973   verifyFormat("void someLongFunction(\n"
5974                "    int someLongParameter) const\n"
5975                "  {\n"
5976                "  }",
5977                Style);
5978 
5979   // Unless these are unknown annotations.
5980   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
5981                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5982                "    LONG_AND_UGLY_ANNOTATION;");
5983 
5984   // Breaking before function-like trailing annotations is fine to keep them
5985   // close to their arguments.
5986   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5987                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
5988   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
5989                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
5990   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
5991                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
5992   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
5993                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
5994   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
5995 
5996   verifyFormat(
5997       "void aaaaaaaaaaaaaaaaaa()\n"
5998       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
5999       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
6000   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6001                "    __attribute__((unused));");
6002   verifyGoogleFormat(
6003       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6004       "    GUARDED_BY(aaaaaaaaaaaa);");
6005   verifyGoogleFormat(
6006       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6007       "    GUARDED_BY(aaaaaaaaaaaa);");
6008   verifyGoogleFormat(
6009       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6010       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6011   verifyGoogleFormat(
6012       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
6013       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
6014 }
6015 
6016 TEST_F(FormatTest, FunctionAnnotations) {
6017   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6018                "int OldFunction(const string &parameter) {}");
6019   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6020                "string OldFunction(const string &parameter) {}");
6021   verifyFormat("template <typename T>\n"
6022                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
6023                "string OldFunction(const string &parameter) {}");
6024 
6025   // Not function annotations.
6026   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6027                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
6028   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
6029                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
6030   verifyFormat("MACRO(abc).function() // wrap\n"
6031                "    << abc;");
6032   verifyFormat("MACRO(abc)->function() // wrap\n"
6033                "    << abc;");
6034   verifyFormat("MACRO(abc)::function() // wrap\n"
6035                "    << abc;");
6036 }
6037 
6038 TEST_F(FormatTest, BreaksDesireably) {
6039   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6040                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
6041                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6042   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6043                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6044                "}");
6045 
6046   verifyFormat(
6047       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6048       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6049 
6050   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6051                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6052                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6053 
6054   verifyFormat(
6055       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6056       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6057       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6058       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6059       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6060 
6061   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6062                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6063 
6064   verifyFormat(
6065       "void f() {\n"
6066       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6067       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6068       "}");
6069   verifyFormat(
6070       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6071       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6072   verifyFormat(
6073       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6074       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6075   verifyFormat(
6076       "aaaaaa(aaa,\n"
6077       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6078       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6079       "       aaaa);");
6080   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6081                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6082                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6083 
6084   // Indent consistently independent of call expression and unary operator.
6085   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6086                "    dddddddddddddddddddddddddddddd));");
6087   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6088                "    dddddddddddddddddddddddddddddd));");
6089   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6090                "    dddddddddddddddddddddddddddddd));");
6091 
6092   // This test case breaks on an incorrect memoization, i.e. an optimization not
6093   // taking into account the StopAt value.
6094   verifyFormat(
6095       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6096       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6097       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6098       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6099 
6100   verifyFormat("{\n  {\n    {\n"
6101                "      Annotation.SpaceRequiredBefore =\n"
6102                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6103                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6104                "    }\n  }\n}");
6105 
6106   // Break on an outer level if there was a break on an inner level.
6107   EXPECT_EQ("f(g(h(a, // comment\n"
6108             "      b, c),\n"
6109             "    d, e),\n"
6110             "  x, y);",
6111             format("f(g(h(a, // comment\n"
6112                    "    b, c), d, e), x, y);"));
6113 
6114   // Prefer breaking similar line breaks.
6115   verifyFormat(
6116       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6117       "                             NSTrackingMouseEnteredAndExited |\n"
6118       "                             NSTrackingActiveAlways;");
6119 }
6120 
6121 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6122   FormatStyle NoBinPacking = getGoogleStyle();
6123   NoBinPacking.BinPackParameters = false;
6124   NoBinPacking.BinPackArguments = true;
6125   verifyFormat("void f() {\n"
6126                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6127                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6128                "}",
6129                NoBinPacking);
6130   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6131                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6132                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6133                NoBinPacking);
6134 
6135   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6136   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6137                "                        vector<int> bbbbbbbbbbbbbbb);",
6138                NoBinPacking);
6139   // FIXME: This behavior difference is probably not wanted. However, currently
6140   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6141   // template arguments from BreakBeforeParameter being set because of the
6142   // one-per-line formatting.
6143   verifyFormat(
6144       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6145       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6146       NoBinPacking);
6147   verifyFormat(
6148       "void fffffffffff(\n"
6149       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6150       "        aaaaaaaaaa);");
6151 }
6152 
6153 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6154   FormatStyle NoBinPacking = getGoogleStyle();
6155   NoBinPacking.BinPackParameters = false;
6156   NoBinPacking.BinPackArguments = false;
6157   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6158                "  aaaaaaaaaaaaaaaaaaaa,\n"
6159                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6160                NoBinPacking);
6161   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6162                "        aaaaaaaaaaaaa,\n"
6163                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6164                NoBinPacking);
6165   verifyFormat(
6166       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6167       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6168       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6169       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6170       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
6171       NoBinPacking);
6172   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6173                "    .aaaaaaaaaaaaaaaaaa();",
6174                NoBinPacking);
6175   verifyFormat("void f() {\n"
6176                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6177                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
6178                "}",
6179                NoBinPacking);
6180 
6181   verifyFormat(
6182       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6183       "             aaaaaaaaaaaa,\n"
6184       "             aaaaaaaaaaaa);",
6185       NoBinPacking);
6186   verifyFormat(
6187       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
6188       "                               ddddddddddddddddddddddddddddd),\n"
6189       "             test);",
6190       NoBinPacking);
6191 
6192   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6193                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
6194                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
6195                "    aaaaaaaaaaaaaaaaaa;",
6196                NoBinPacking);
6197   verifyFormat("a(\"a\"\n"
6198                "  \"a\",\n"
6199                "  a);");
6200 
6201   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6202   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
6203                "                aaaaaaaaa,\n"
6204                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6205                NoBinPacking);
6206   verifyFormat(
6207       "void f() {\n"
6208       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6209       "      .aaaaaaa();\n"
6210       "}",
6211       NoBinPacking);
6212   verifyFormat(
6213       "template <class SomeType, class SomeOtherType>\n"
6214       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
6215       NoBinPacking);
6216 }
6217 
6218 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
6219   FormatStyle Style = getLLVMStyleWithColumns(15);
6220   Style.ExperimentalAutoDetectBinPacking = true;
6221   EXPECT_EQ("aaa(aaaa,\n"
6222             "    aaaa,\n"
6223             "    aaaa);\n"
6224             "aaa(aaaa,\n"
6225             "    aaaa,\n"
6226             "    aaaa);",
6227             format("aaa(aaaa,\n" // one-per-line
6228                    "  aaaa,\n"
6229                    "    aaaa  );\n"
6230                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
6231                    Style));
6232   EXPECT_EQ("aaa(aaaa, aaaa,\n"
6233             "    aaaa);\n"
6234             "aaa(aaaa, aaaa,\n"
6235             "    aaaa);",
6236             format("aaa(aaaa,  aaaa,\n" // bin-packed
6237                    "    aaaa  );\n"
6238                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
6239                    Style));
6240 }
6241 
6242 TEST_F(FormatTest, FormatsBuilderPattern) {
6243   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
6244                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
6245                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
6246                "    .StartsWith(\".init\", ORDER_INIT)\n"
6247                "    .StartsWith(\".fini\", ORDER_FINI)\n"
6248                "    .StartsWith(\".hash\", ORDER_HASH)\n"
6249                "    .Default(ORDER_TEXT);\n");
6250 
6251   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
6252                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
6253   verifyFormat("aaaaaaa->aaaaaaa\n"
6254                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6255                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6256                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
6257   verifyFormat(
6258       "aaaaaaa->aaaaaaa\n"
6259       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6260       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
6261   verifyFormat(
6262       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
6263       "    aaaaaaaaaaaaaa);");
6264   verifyFormat(
6265       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
6266       "    aaaaaa->aaaaaaaaaaaa()\n"
6267       "        ->aaaaaaaaaaaaaaaa(\n"
6268       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6269       "        ->aaaaaaaaaaaaaaaaa();");
6270   verifyGoogleFormat(
6271       "void f() {\n"
6272       "  someo->Add((new util::filetools::Handler(dir))\n"
6273       "                 ->OnEvent1(NewPermanentCallback(\n"
6274       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
6275       "                 ->OnEvent2(NewPermanentCallback(\n"
6276       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
6277       "                 ->OnEvent3(NewPermanentCallback(\n"
6278       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
6279       "                 ->OnEvent5(NewPermanentCallback(\n"
6280       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
6281       "                 ->OnEvent6(NewPermanentCallback(\n"
6282       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
6283       "}");
6284 
6285   verifyFormat(
6286       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
6287   verifyFormat("aaaaaaaaaaaaaaa()\n"
6288                "    .aaaaaaaaaaaaaaa()\n"
6289                "    .aaaaaaaaaaaaaaa()\n"
6290                "    .aaaaaaaaaaaaaaa()\n"
6291                "    .aaaaaaaaaaaaaaa();");
6292   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6293                "    .aaaaaaaaaaaaaaa()\n"
6294                "    .aaaaaaaaaaaaaaa()\n"
6295                "    .aaaaaaaaaaaaaaa();");
6296   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6297                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6298                "    .aaaaaaaaaaaaaaa();");
6299   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
6300                "    ->aaaaaaaaaaaaaae(0)\n"
6301                "    ->aaaaaaaaaaaaaaa();");
6302 
6303   // Don't linewrap after very short segments.
6304   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6305                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6306                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6307   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6308                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6309                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6310   verifyFormat("aaa()\n"
6311                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6312                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6313                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6314 
6315   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
6316                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6317                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
6318   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
6319                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6320                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
6321 
6322   // Prefer not to break after empty parentheses.
6323   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
6324                "    First->LastNewlineOffset);");
6325 
6326   // Prefer not to create "hanging" indents.
6327   verifyFormat(
6328       "return !soooooooooooooome_map\n"
6329       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6330       "            .second;");
6331   verifyFormat(
6332       "return aaaaaaaaaaaaaaaa\n"
6333       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
6334       "    .aaaa(aaaaaaaaaaaaaa);");
6335   // No hanging indent here.
6336   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
6337                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6338   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
6339                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6340   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
6341                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6342                getLLVMStyleWithColumns(60));
6343   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
6344                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
6345                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6346                getLLVMStyleWithColumns(59));
6347   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6348                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6349                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6350 
6351   // Dont break if only closing statements before member call
6352   verifyFormat("test() {\n"
6353                "  ([]() -> {\n"
6354                "    int b = 32;\n"
6355                "    return 3;\n"
6356                "  }).foo();\n"
6357                "}");
6358   verifyFormat("test() {\n"
6359                "  (\n"
6360                "      []() -> {\n"
6361                "        int b = 32;\n"
6362                "        return 3;\n"
6363                "      },\n"
6364                "      foo, bar)\n"
6365                "      .foo();\n"
6366                "}");
6367   verifyFormat("test() {\n"
6368                "  ([]() -> {\n"
6369                "    int b = 32;\n"
6370                "    return 3;\n"
6371                "  })\n"
6372                "      .foo()\n"
6373                "      .bar();\n"
6374                "}");
6375   verifyFormat("test() {\n"
6376                "  ([]() -> {\n"
6377                "    int b = 32;\n"
6378                "    return 3;\n"
6379                "  })\n"
6380                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
6381                "           \"bbbb\");\n"
6382                "}",
6383                getLLVMStyleWithColumns(30));
6384 }
6385 
6386 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
6387   verifyFormat(
6388       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6389       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
6390   verifyFormat(
6391       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
6392       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
6393 
6394   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
6395                "    ccccccccccccccccccccccccc) {\n}");
6396   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
6397                "    ccccccccccccccccccccccccc) {\n}");
6398 
6399   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
6400                "    ccccccccccccccccccccccccc) {\n}");
6401   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
6402                "    ccccccccccccccccccccccccc) {\n}");
6403 
6404   verifyFormat(
6405       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
6406       "    ccccccccccccccccccccccccc) {\n}");
6407   verifyFormat(
6408       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
6409       "    ccccccccccccccccccccccccc) {\n}");
6410 
6411   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
6412                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
6413                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
6414                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
6415   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
6416                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
6417                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
6418                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
6419 
6420   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
6421                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
6422                "    aaaaaaaaaaaaaaa != aa) {\n}");
6423   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
6424                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
6425                "    aaaaaaaaaaaaaaa != aa) {\n}");
6426 }
6427 
6428 TEST_F(FormatTest, BreaksAfterAssignments) {
6429   verifyFormat(
6430       "unsigned Cost =\n"
6431       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
6432       "                        SI->getPointerAddressSpaceee());\n");
6433   verifyFormat(
6434       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
6435       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
6436 
6437   verifyFormat(
6438       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
6439       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
6440   verifyFormat("unsigned OriginalStartColumn =\n"
6441                "    SourceMgr.getSpellingColumnNumber(\n"
6442                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
6443                "    1;");
6444 }
6445 
6446 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
6447   FormatStyle Style = getLLVMStyle();
6448   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6449                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
6450                Style);
6451 
6452   Style.PenaltyBreakAssignment = 20;
6453   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
6454                "                                 cccccccccccccccccccccccccc;",
6455                Style);
6456 }
6457 
6458 TEST_F(FormatTest, AlignsAfterAssignments) {
6459   verifyFormat(
6460       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6461       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
6462   verifyFormat(
6463       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6464       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
6465   verifyFormat(
6466       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6467       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
6468   verifyFormat(
6469       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6470       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
6471   verifyFormat(
6472       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
6473       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
6474       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
6475 }
6476 
6477 TEST_F(FormatTest, AlignsAfterReturn) {
6478   verifyFormat(
6479       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6480       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
6481   verifyFormat(
6482       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6483       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
6484   verifyFormat(
6485       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
6486       "       aaaaaaaaaaaaaaaaaaaaaa();");
6487   verifyFormat(
6488       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
6489       "        aaaaaaaaaaaaaaaaaaaaaa());");
6490   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6491                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6492   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6493                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
6494                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6495   verifyFormat("return\n"
6496                "    // true if code is one of a or b.\n"
6497                "    code == a || code == b;");
6498 }
6499 
6500 TEST_F(FormatTest, AlignsAfterOpenBracket) {
6501   verifyFormat(
6502       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
6503       "                                                aaaaaaaaa aaaaaaa) {}");
6504   verifyFormat(
6505       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
6506       "                                               aaaaaaaaaaa aaaaaaaaa);");
6507   verifyFormat(
6508       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
6509       "                                             aaaaaaaaaaaaaaaaaaaaa));");
6510   FormatStyle Style = getLLVMStyle();
6511   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6512   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6513                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
6514                Style);
6515   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
6516                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
6517                Style);
6518   verifyFormat("SomeLongVariableName->someFunction(\n"
6519                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
6520                Style);
6521   verifyFormat(
6522       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
6523       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6524       Style);
6525   verifyFormat(
6526       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
6527       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6528       Style);
6529   verifyFormat(
6530       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
6531       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
6532       Style);
6533 
6534   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
6535                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
6536                "        b));",
6537                Style);
6538 
6539   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6540   Style.BinPackArguments = false;
6541   Style.BinPackParameters = false;
6542   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6543                "    aaaaaaaaaaa aaaaaaaa,\n"
6544                "    aaaaaaaaa aaaaaaa,\n"
6545                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6546                Style);
6547   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
6548                "    aaaaaaaaaaa aaaaaaaaa,\n"
6549                "    aaaaaaaaaaa aaaaaaaaa,\n"
6550                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6551                Style);
6552   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
6553                "    aaaaaaaaaaaaaaa,\n"
6554                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6555                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
6556                Style);
6557   verifyFormat(
6558       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
6559       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
6560       Style);
6561   verifyFormat(
6562       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
6563       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
6564       Style);
6565   verifyFormat(
6566       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6567       "    aaaaaaaaaaaaaaaaaaaaa(\n"
6568       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
6569       "    aaaaaaaaaaaaaaaa);",
6570       Style);
6571   verifyFormat(
6572       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6573       "    aaaaaaaaaaaaaaaaaaaaa(\n"
6574       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
6575       "    aaaaaaaaaaaaaaaa);",
6576       Style);
6577 }
6578 
6579 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
6580   FormatStyle Style = getLLVMStyleWithColumns(40);
6581   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6582                "          bbbbbbbbbbbbbbbbbbbbbb);",
6583                Style);
6584   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6585   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6586   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6587                "          bbbbbbbbbbbbbbbbbbbbbb);",
6588                Style);
6589   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6590   Style.AlignOperands = FormatStyle::OAS_Align;
6591   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6592                "          bbbbbbbbbbbbbbbbbbbbbb);",
6593                Style);
6594   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6595   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6596   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6597                "    bbbbbbbbbbbbbbbbbbbbbb);",
6598                Style);
6599 }
6600 
6601 TEST_F(FormatTest, BreaksConditionalExpressions) {
6602   verifyFormat(
6603       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6604       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6605       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6606   verifyFormat(
6607       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
6608       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6609       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6610   verifyFormat(
6611       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6612       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6613   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
6614                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6615                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6616   verifyFormat(
6617       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
6618       "                                                    : aaaaaaaaaaaaa);");
6619   verifyFormat(
6620       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6621       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6622       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6623       "                   aaaaaaaaaaaaa);");
6624   verifyFormat(
6625       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6626       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6627       "                   aaaaaaaaaaaaa);");
6628   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6629                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6630                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6631                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6632                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6633   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6634                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6635                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6636                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6637                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6638                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6639                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6640   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6641                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6642                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6643                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6644                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6645   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6646                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6647                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6648   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
6649                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6650                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6651                "        : aaaaaaaaaaaaaaaa;");
6652   verifyFormat(
6653       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6654       "    ? aaaaaaaaaaaaaaa\n"
6655       "    : aaaaaaaaaaaaaaa;");
6656   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
6657                "          aaaaaaaaa\n"
6658                "      ? b\n"
6659                "      : c);");
6660   verifyFormat("return aaaa == bbbb\n"
6661                "           // comment\n"
6662                "           ? aaaa\n"
6663                "           : bbbb;");
6664   verifyFormat("unsigned Indent =\n"
6665                "    format(TheLine.First,\n"
6666                "           IndentForLevel[TheLine.Level] >= 0\n"
6667                "               ? IndentForLevel[TheLine.Level]\n"
6668                "               : TheLine * 2,\n"
6669                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
6670                getLLVMStyleWithColumns(60));
6671   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
6672                "                  ? aaaaaaaaaaaaaaa\n"
6673                "                  : bbbbbbbbbbbbbbb //\n"
6674                "                        ? ccccccccccccccc\n"
6675                "                        : ddddddddddddddd;");
6676   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
6677                "                  ? aaaaaaaaaaaaaaa\n"
6678                "                  : (bbbbbbbbbbbbbbb //\n"
6679                "                         ? ccccccccccccccc\n"
6680                "                         : ddddddddddddddd);");
6681   verifyFormat(
6682       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6683       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6684       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
6685       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
6686       "                                      : aaaaaaaaaa;");
6687   verifyFormat(
6688       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6689       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
6690       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6691 
6692   FormatStyle NoBinPacking = getLLVMStyle();
6693   NoBinPacking.BinPackArguments = false;
6694   verifyFormat(
6695       "void f() {\n"
6696       "  g(aaa,\n"
6697       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
6698       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6699       "        ? aaaaaaaaaaaaaaa\n"
6700       "        : aaaaaaaaaaaaaaa);\n"
6701       "}",
6702       NoBinPacking);
6703   verifyFormat(
6704       "void f() {\n"
6705       "  g(aaa,\n"
6706       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
6707       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6708       "        ?: aaaaaaaaaaaaaaa);\n"
6709       "}",
6710       NoBinPacking);
6711 
6712   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
6713                "             // comment.\n"
6714                "             ccccccccccccccccccccccccccccccccccccccc\n"
6715                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6716                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
6717 
6718   // Assignments in conditional expressions. Apparently not uncommon :-(.
6719   verifyFormat("return a != b\n"
6720                "           // comment\n"
6721                "           ? a = b\n"
6722                "           : a = b;");
6723   verifyFormat("return a != b\n"
6724                "           // comment\n"
6725                "           ? a = a != b\n"
6726                "                     // comment\n"
6727                "                     ? a = b\n"
6728                "                     : a\n"
6729                "           : a;\n");
6730   verifyFormat("return a != b\n"
6731                "           // comment\n"
6732                "           ? a\n"
6733                "           : a = a != b\n"
6734                "                     // comment\n"
6735                "                     ? a = b\n"
6736                "                     : a;");
6737 
6738   // Chained conditionals
6739   FormatStyle Style = getLLVMStyle();
6740   Style.ColumnLimit = 70;
6741   Style.AlignOperands = FormatStyle::OAS_Align;
6742   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6743                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6744                "                        : 3333333333333333;",
6745                Style);
6746   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6747                "       : bbbbbbbbbb     ? 2222222222222222\n"
6748                "                        : 3333333333333333;",
6749                Style);
6750   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
6751                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6752                "                          : 3333333333333333;",
6753                Style);
6754   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6755                "       : bbbbbbbbbbbbbb ? 222222\n"
6756                "                        : 333333;",
6757                Style);
6758   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6759                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6760                "       : cccccccccccccc ? 3333333333333333\n"
6761                "                        : 4444444444444444;",
6762                Style);
6763   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
6764                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6765                "                        : 3333333333333333;",
6766                Style);
6767   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6768                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6769                "                        : (aaa ? bbb : ccc);",
6770                Style);
6771   verifyFormat(
6772       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6773       "                                             : cccccccccccccccccc)\n"
6774       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6775       "                        : 3333333333333333;",
6776       Style);
6777   verifyFormat(
6778       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6779       "                                             : cccccccccccccccccc)\n"
6780       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6781       "                        : 3333333333333333;",
6782       Style);
6783   verifyFormat(
6784       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6785       "                                             : dddddddddddddddddd)\n"
6786       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6787       "                        : 3333333333333333;",
6788       Style);
6789   verifyFormat(
6790       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6791       "                                             : dddddddddddddddddd)\n"
6792       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6793       "                        : 3333333333333333;",
6794       Style);
6795   verifyFormat(
6796       "return aaaaaaaaa        ? 1111111111111111\n"
6797       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6798       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6799       "                                             : dddddddddddddddddd)\n",
6800       Style);
6801   verifyFormat(
6802       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6803       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6804       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6805       "                                             : cccccccccccccccccc);",
6806       Style);
6807   verifyFormat(
6808       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6809       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6810       "                                             : eeeeeeeeeeeeeeeeee)\n"
6811       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6812       "                        : 3333333333333333;",
6813       Style);
6814   verifyFormat(
6815       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6816       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6817       "                                             : eeeeeeeeeeeeeeeeee)\n"
6818       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6819       "                        : 3333333333333333;",
6820       Style);
6821   verifyFormat(
6822       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6823       "                           : cccccccccccc    ? dddddddddddddddddd\n"
6824       "                                             : eeeeeeeeeeeeeeeeee)\n"
6825       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6826       "                        : 3333333333333333;",
6827       Style);
6828   verifyFormat(
6829       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6830       "                                             : cccccccccccccccccc\n"
6831       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6832       "                        : 3333333333333333;",
6833       Style);
6834   verifyFormat(
6835       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6836       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
6837       "                                             : eeeeeeeeeeeeeeeeee\n"
6838       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6839       "                        : 3333333333333333;",
6840       Style);
6841   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
6842                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
6843                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
6844                "                                   : eeeeeeeeeeeeeeeeee)\n"
6845                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
6846                "                             : 3333333333333333;",
6847                Style);
6848   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
6849                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6850                "             : cccccccccccccccc ? dddddddddddddddddd\n"
6851                "                                : eeeeeeeeeeeeeeeeee\n"
6852                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
6853                "                                 : 3333333333333333;",
6854                Style);
6855 
6856   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6857   Style.BreakBeforeTernaryOperators = false;
6858   // FIXME: Aligning the question marks is weird given DontAlign.
6859   // Consider disabling this alignment in this case. Also check whether this
6860   // will render the adjustment from https://reviews.llvm.org/D82199
6861   // unnecessary.
6862   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
6863                "    bbbb                ? cccccccccccccccccc :\n"
6864                "                          ddddd;\n",
6865                Style);
6866 
6867   EXPECT_EQ(
6868       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
6869       "    /*\n"
6870       "     */\n"
6871       "    function() {\n"
6872       "      try {\n"
6873       "        return JJJJJJJJJJJJJJ(\n"
6874       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
6875       "      }\n"
6876       "    } :\n"
6877       "    function() {};",
6878       format(
6879           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
6880           "     /*\n"
6881           "      */\n"
6882           "     function() {\n"
6883           "      try {\n"
6884           "        return JJJJJJJJJJJJJJ(\n"
6885           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
6886           "      }\n"
6887           "    } :\n"
6888           "    function() {};",
6889           getGoogleStyle(FormatStyle::LK_JavaScript)));
6890 }
6891 
6892 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
6893   FormatStyle Style = getLLVMStyle();
6894   Style.BreakBeforeTernaryOperators = false;
6895   Style.ColumnLimit = 70;
6896   verifyFormat(
6897       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6898       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6899       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6900       Style);
6901   verifyFormat(
6902       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
6903       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6904       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6905       Style);
6906   verifyFormat(
6907       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6908       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6909       Style);
6910   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
6911                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6912                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6913                Style);
6914   verifyFormat(
6915       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
6916       "                                                      aaaaaaaaaaaaa);",
6917       Style);
6918   verifyFormat(
6919       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6920       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6921       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6922       "                   aaaaaaaaaaaaa);",
6923       Style);
6924   verifyFormat(
6925       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6926       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6927       "                   aaaaaaaaaaaaa);",
6928       Style);
6929   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6930                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6931                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6932                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6933                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6934                Style);
6935   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6936                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6937                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6938                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6939                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6940                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6941                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6942                Style);
6943   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6944                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
6945                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6946                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6947                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6948                Style);
6949   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6950                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6951                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6952                Style);
6953   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
6954                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6955                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6956                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6957                Style);
6958   verifyFormat(
6959       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6960       "    aaaaaaaaaaaaaaa :\n"
6961       "    aaaaaaaaaaaaaaa;",
6962       Style);
6963   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
6964                "          aaaaaaaaa ?\n"
6965                "      b :\n"
6966                "      c);",
6967                Style);
6968   verifyFormat("unsigned Indent =\n"
6969                "    format(TheLine.First,\n"
6970                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
6971                "               IndentForLevel[TheLine.Level] :\n"
6972                "               TheLine * 2,\n"
6973                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
6974                Style);
6975   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
6976                "                  aaaaaaaaaaaaaaa :\n"
6977                "                  bbbbbbbbbbbbbbb ? //\n"
6978                "                      ccccccccccccccc :\n"
6979                "                      ddddddddddddddd;",
6980                Style);
6981   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
6982                "                  aaaaaaaaaaaaaaa :\n"
6983                "                  (bbbbbbbbbbbbbbb ? //\n"
6984                "                       ccccccccccccccc :\n"
6985                "                       ddddddddddddddd);",
6986                Style);
6987   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6988                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
6989                "            ccccccccccccccccccccccccccc;",
6990                Style);
6991   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6992                "           aaaaa :\n"
6993                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
6994                Style);
6995 
6996   // Chained conditionals
6997   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6998                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6999                "                          3333333333333333;",
7000                Style);
7001   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7002                "       bbbbbbbbbb       ? 2222222222222222 :\n"
7003                "                          3333333333333333;",
7004                Style);
7005   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
7006                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7007                "                          3333333333333333;",
7008                Style);
7009   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7010                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
7011                "                          333333;",
7012                Style);
7013   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7014                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7015                "       cccccccccccccccc ? 3333333333333333 :\n"
7016                "                          4444444444444444;",
7017                Style);
7018   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
7019                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7020                "                          3333333333333333;",
7021                Style);
7022   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7023                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7024                "                          (aaa ? bbb : ccc);",
7025                Style);
7026   verifyFormat(
7027       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7028       "                                               cccccccccccccccccc) :\n"
7029       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7030       "                          3333333333333333;",
7031       Style);
7032   verifyFormat(
7033       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7034       "                                               cccccccccccccccccc) :\n"
7035       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7036       "                          3333333333333333;",
7037       Style);
7038   verifyFormat(
7039       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7040       "                                               dddddddddddddddddd) :\n"
7041       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7042       "                          3333333333333333;",
7043       Style);
7044   verifyFormat(
7045       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7046       "                                               dddddddddddddddddd) :\n"
7047       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7048       "                          3333333333333333;",
7049       Style);
7050   verifyFormat(
7051       "return aaaaaaaaa        ? 1111111111111111 :\n"
7052       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7053       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7054       "                                               dddddddddddddddddd)\n",
7055       Style);
7056   verifyFormat(
7057       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7058       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7059       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7060       "                                               cccccccccccccccccc);",
7061       Style);
7062   verifyFormat(
7063       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7064       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7065       "                                               eeeeeeeeeeeeeeeeee) :\n"
7066       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7067       "                          3333333333333333;",
7068       Style);
7069   verifyFormat(
7070       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7071       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7072       "                                               eeeeeeeeeeeeeeeeee) :\n"
7073       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7074       "                          3333333333333333;",
7075       Style);
7076   verifyFormat(
7077       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7078       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7079       "                                               eeeeeeeeeeeeeeeeee) :\n"
7080       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7081       "                          3333333333333333;",
7082       Style);
7083   verifyFormat(
7084       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7085       "                                               cccccccccccccccccc :\n"
7086       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7087       "                          3333333333333333;",
7088       Style);
7089   verifyFormat(
7090       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7091       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7092       "                                               eeeeeeeeeeeeeeeeee :\n"
7093       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7094       "                          3333333333333333;",
7095       Style);
7096   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7097                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7098                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7099                "                                 eeeeeeeeeeeeeeeeee) :\n"
7100                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7101                "                               3333333333333333;",
7102                Style);
7103   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7104                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7105                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7106                "                                  eeeeeeeeeeeeeeeeee :\n"
7107                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7108                "                               3333333333333333;",
7109                Style);
7110 }
7111 
7112 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7113   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7114                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7115   verifyFormat("bool a = true, b = false;");
7116 
7117   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7118                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7119                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7120                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7121   verifyFormat(
7122       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7123       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7124       "     d = e && f;");
7125   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7126                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7127   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7128                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7129   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7130                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7131 
7132   FormatStyle Style = getGoogleStyle();
7133   Style.PointerAlignment = FormatStyle::PAS_Left;
7134   Style.DerivePointerAlignment = false;
7135   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7136                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7137                "    *b = bbbbbbbbbbbbbbbbbbb;",
7138                Style);
7139   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7140                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7141                Style);
7142   verifyFormat("vector<int*> a, b;", Style);
7143   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7144 }
7145 
7146 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7147   verifyFormat("arr[foo ? bar : baz];");
7148   verifyFormat("f()[foo ? bar : baz];");
7149   verifyFormat("(a + b)[foo ? bar : baz];");
7150   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7151 }
7152 
7153 TEST_F(FormatTest, AlignsStringLiterals) {
7154   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7155                "                                      \"short literal\");");
7156   verifyFormat(
7157       "looooooooooooooooooooooooongFunction(\n"
7158       "    \"short literal\"\n"
7159       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7160   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7161                "             \" string literals\",\n"
7162                "             and, other, parameters);");
7163   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7164             "      \"5678\";",
7165             format("fun + \"1243\" /* comment */\n"
7166                    "    \"5678\";",
7167                    getLLVMStyleWithColumns(28)));
7168   EXPECT_EQ(
7169       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7170       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
7171       "         \"aaaaaaaaaaaaaaaa\";",
7172       format("aaaaaa ="
7173              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7174              "aaaaaaaaaaaaaaaaaaaaa\" "
7175              "\"aaaaaaaaaaaaaaaa\";"));
7176   verifyFormat("a = a + \"a\"\n"
7177                "        \"a\"\n"
7178                "        \"a\";");
7179   verifyFormat("f(\"a\", \"b\"\n"
7180                "       \"c\");");
7181 
7182   verifyFormat(
7183       "#define LL_FORMAT \"ll\"\n"
7184       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
7185       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
7186 
7187   verifyFormat("#define A(X)          \\\n"
7188                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
7189                "  \"ccccc\"",
7190                getLLVMStyleWithColumns(23));
7191   verifyFormat("#define A \"def\"\n"
7192                "f(\"abc\" A \"ghi\"\n"
7193                "  \"jkl\");");
7194 
7195   verifyFormat("f(L\"a\"\n"
7196                "  L\"b\");");
7197   verifyFormat("#define A(X)            \\\n"
7198                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
7199                "  L\"ccccc\"",
7200                getLLVMStyleWithColumns(25));
7201 
7202   verifyFormat("f(@\"a\"\n"
7203                "  @\"b\");");
7204   verifyFormat("NSString s = @\"a\"\n"
7205                "             @\"b\"\n"
7206                "             @\"c\";");
7207   verifyFormat("NSString s = @\"a\"\n"
7208                "              \"b\"\n"
7209                "              \"c\";");
7210 }
7211 
7212 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
7213   FormatStyle Style = getLLVMStyle();
7214   // No declarations or definitions should be moved to own line.
7215   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
7216   verifyFormat("class A {\n"
7217                "  int f() { return 1; }\n"
7218                "  int g();\n"
7219                "};\n"
7220                "int f() { return 1; }\n"
7221                "int g();\n",
7222                Style);
7223 
7224   // All declarations and definitions should have the return type moved to its
7225   // own line.
7226   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
7227   Style.TypenameMacros = {"LIST"};
7228   verifyFormat("SomeType\n"
7229                "funcdecl(LIST(uint64_t));",
7230                Style);
7231   verifyFormat("class E {\n"
7232                "  int\n"
7233                "  f() {\n"
7234                "    return 1;\n"
7235                "  }\n"
7236                "  int\n"
7237                "  g();\n"
7238                "};\n"
7239                "int\n"
7240                "f() {\n"
7241                "  return 1;\n"
7242                "}\n"
7243                "int\n"
7244                "g();\n",
7245                Style);
7246 
7247   // Top-level definitions, and no kinds of declarations should have the
7248   // return type moved to its own line.
7249   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
7250   verifyFormat("class B {\n"
7251                "  int f() { return 1; }\n"
7252                "  int g();\n"
7253                "};\n"
7254                "int\n"
7255                "f() {\n"
7256                "  return 1;\n"
7257                "}\n"
7258                "int g();\n",
7259                Style);
7260 
7261   // Top-level definitions and declarations should have the return type moved
7262   // to its own line.
7263   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
7264   verifyFormat("class C {\n"
7265                "  int f() { return 1; }\n"
7266                "  int g();\n"
7267                "};\n"
7268                "int\n"
7269                "f() {\n"
7270                "  return 1;\n"
7271                "}\n"
7272                "int\n"
7273                "g();\n",
7274                Style);
7275 
7276   // All definitions should have the return type moved to its own line, but no
7277   // kinds of declarations.
7278   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
7279   verifyFormat("class D {\n"
7280                "  int\n"
7281                "  f() {\n"
7282                "    return 1;\n"
7283                "  }\n"
7284                "  int g();\n"
7285                "};\n"
7286                "int\n"
7287                "f() {\n"
7288                "  return 1;\n"
7289                "}\n"
7290                "int g();\n",
7291                Style);
7292   verifyFormat("const char *\n"
7293                "f(void) {\n" // Break here.
7294                "  return \"\";\n"
7295                "}\n"
7296                "const char *bar(void);\n", // No break here.
7297                Style);
7298   verifyFormat("template <class T>\n"
7299                "T *\n"
7300                "f(T &c) {\n" // Break here.
7301                "  return NULL;\n"
7302                "}\n"
7303                "template <class T> T *f(T &c);\n", // No break here.
7304                Style);
7305   verifyFormat("class C {\n"
7306                "  int\n"
7307                "  operator+() {\n"
7308                "    return 1;\n"
7309                "  }\n"
7310                "  int\n"
7311                "  operator()() {\n"
7312                "    return 1;\n"
7313                "  }\n"
7314                "};\n",
7315                Style);
7316   verifyFormat("void\n"
7317                "A::operator()() {}\n"
7318                "void\n"
7319                "A::operator>>() {}\n"
7320                "void\n"
7321                "A::operator+() {}\n"
7322                "void\n"
7323                "A::operator*() {}\n"
7324                "void\n"
7325                "A::operator->() {}\n"
7326                "void\n"
7327                "A::operator void *() {}\n"
7328                "void\n"
7329                "A::operator void &() {}\n"
7330                "void\n"
7331                "A::operator void &&() {}\n"
7332                "void\n"
7333                "A::operator char *() {}\n"
7334                "void\n"
7335                "A::operator[]() {}\n"
7336                "void\n"
7337                "A::operator!() {}\n"
7338                "void\n"
7339                "A::operator**() {}\n"
7340                "void\n"
7341                "A::operator<Foo> *() {}\n"
7342                "void\n"
7343                "A::operator<Foo> **() {}\n"
7344                "void\n"
7345                "A::operator<Foo> &() {}\n"
7346                "void\n"
7347                "A::operator void **() {}\n",
7348                Style);
7349   verifyFormat("constexpr auto\n"
7350                "operator()() const -> reference {}\n"
7351                "constexpr auto\n"
7352                "operator>>() const -> reference {}\n"
7353                "constexpr auto\n"
7354                "operator+() const -> reference {}\n"
7355                "constexpr auto\n"
7356                "operator*() const -> reference {}\n"
7357                "constexpr auto\n"
7358                "operator->() const -> reference {}\n"
7359                "constexpr auto\n"
7360                "operator++() const -> reference {}\n"
7361                "constexpr auto\n"
7362                "operator void *() const -> reference {}\n"
7363                "constexpr auto\n"
7364                "operator void **() const -> reference {}\n"
7365                "constexpr auto\n"
7366                "operator void *() const -> reference {}\n"
7367                "constexpr auto\n"
7368                "operator void &() const -> reference {}\n"
7369                "constexpr auto\n"
7370                "operator void &&() const -> reference {}\n"
7371                "constexpr auto\n"
7372                "operator char *() const -> reference {}\n"
7373                "constexpr auto\n"
7374                "operator!() const -> reference {}\n"
7375                "constexpr auto\n"
7376                "operator[]() const -> reference {}\n",
7377                Style);
7378   verifyFormat("void *operator new(std::size_t s);", // No break here.
7379                Style);
7380   verifyFormat("void *\n"
7381                "operator new(std::size_t s) {}",
7382                Style);
7383   verifyFormat("void *\n"
7384                "operator delete[](void *ptr) {}",
7385                Style);
7386   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
7387   verifyFormat("const char *\n"
7388                "f(void)\n" // Break here.
7389                "{\n"
7390                "  return \"\";\n"
7391                "}\n"
7392                "const char *bar(void);\n", // No break here.
7393                Style);
7394   verifyFormat("template <class T>\n"
7395                "T *\n"     // Problem here: no line break
7396                "f(T &c)\n" // Break here.
7397                "{\n"
7398                "  return NULL;\n"
7399                "}\n"
7400                "template <class T> T *f(T &c);\n", // No break here.
7401                Style);
7402   verifyFormat("int\n"
7403                "foo(A<bool> a)\n"
7404                "{\n"
7405                "  return a;\n"
7406                "}\n",
7407                Style);
7408   verifyFormat("int\n"
7409                "foo(A<8> a)\n"
7410                "{\n"
7411                "  return a;\n"
7412                "}\n",
7413                Style);
7414   verifyFormat("int\n"
7415                "foo(A<B<bool>, 8> a)\n"
7416                "{\n"
7417                "  return a;\n"
7418                "}\n",
7419                Style);
7420   verifyFormat("int\n"
7421                "foo(A<B<8>, bool> a)\n"
7422                "{\n"
7423                "  return a;\n"
7424                "}\n",
7425                Style);
7426   verifyFormat("int\n"
7427                "foo(A<B<bool>, bool> a)\n"
7428                "{\n"
7429                "  return a;\n"
7430                "}\n",
7431                Style);
7432   verifyFormat("int\n"
7433                "foo(A<B<8>, 8> a)\n"
7434                "{\n"
7435                "  return a;\n"
7436                "}\n",
7437                Style);
7438 
7439   Style = getGNUStyle();
7440 
7441   // Test for comments at the end of function declarations.
7442   verifyFormat("void\n"
7443                "foo (int a, /*abc*/ int b) // def\n"
7444                "{\n"
7445                "}\n",
7446                Style);
7447 
7448   verifyFormat("void\n"
7449                "foo (int a, /* abc */ int b) /* def */\n"
7450                "{\n"
7451                "}\n",
7452                Style);
7453 
7454   // Definitions that should not break after return type
7455   verifyFormat("void foo (int a, int b); // def\n", Style);
7456   verifyFormat("void foo (int a, int b); /* def */\n", Style);
7457   verifyFormat("void foo (int a, int b);\n", Style);
7458 }
7459 
7460 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
7461   FormatStyle NoBreak = getLLVMStyle();
7462   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
7463   FormatStyle Break = getLLVMStyle();
7464   Break.AlwaysBreakBeforeMultilineStrings = true;
7465   verifyFormat("aaaa = \"bbbb\"\n"
7466                "       \"cccc\";",
7467                NoBreak);
7468   verifyFormat("aaaa =\n"
7469                "    \"bbbb\"\n"
7470                "    \"cccc\";",
7471                Break);
7472   verifyFormat("aaaa(\"bbbb\"\n"
7473                "     \"cccc\");",
7474                NoBreak);
7475   verifyFormat("aaaa(\n"
7476                "    \"bbbb\"\n"
7477                "    \"cccc\");",
7478                Break);
7479   verifyFormat("aaaa(qqq, \"bbbb\"\n"
7480                "          \"cccc\");",
7481                NoBreak);
7482   verifyFormat("aaaa(qqq,\n"
7483                "     \"bbbb\"\n"
7484                "     \"cccc\");",
7485                Break);
7486   verifyFormat("aaaa(qqq,\n"
7487                "     L\"bbbb\"\n"
7488                "     L\"cccc\");",
7489                Break);
7490   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
7491                "                      \"bbbb\"));",
7492                Break);
7493   verifyFormat("string s = someFunction(\n"
7494                "    \"abc\"\n"
7495                "    \"abc\");",
7496                Break);
7497 
7498   // As we break before unary operators, breaking right after them is bad.
7499   verifyFormat("string foo = abc ? \"x\"\n"
7500                "                   \"blah blah blah blah blah blah\"\n"
7501                "                 : \"y\";",
7502                Break);
7503 
7504   // Don't break if there is no column gain.
7505   verifyFormat("f(\"aaaa\"\n"
7506                "  \"bbbb\");",
7507                Break);
7508 
7509   // Treat literals with escaped newlines like multi-line string literals.
7510   EXPECT_EQ("x = \"a\\\n"
7511             "b\\\n"
7512             "c\";",
7513             format("x = \"a\\\n"
7514                    "b\\\n"
7515                    "c\";",
7516                    NoBreak));
7517   EXPECT_EQ("xxxx =\n"
7518             "    \"a\\\n"
7519             "b\\\n"
7520             "c\";",
7521             format("xxxx = \"a\\\n"
7522                    "b\\\n"
7523                    "c\";",
7524                    Break));
7525 
7526   EXPECT_EQ("NSString *const kString =\n"
7527             "    @\"aaaa\"\n"
7528             "    @\"bbbb\";",
7529             format("NSString *const kString = @\"aaaa\"\n"
7530                    "@\"bbbb\";",
7531                    Break));
7532 
7533   Break.ColumnLimit = 0;
7534   verifyFormat("const char *hello = \"hello llvm\";", Break);
7535 }
7536 
7537 TEST_F(FormatTest, AlignsPipes) {
7538   verifyFormat(
7539       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7540       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7541       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7542   verifyFormat(
7543       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
7544       "                     << aaaaaaaaaaaaaaaaaaaa;");
7545   verifyFormat(
7546       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7547       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7548   verifyFormat(
7549       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7550       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7551   verifyFormat(
7552       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
7553       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
7554       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
7555   verifyFormat(
7556       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7557       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7558       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7559   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7560                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7561                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7562                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
7563   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
7564                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
7565   verifyFormat(
7566       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7567       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7568   verifyFormat(
7569       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
7570       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
7571 
7572   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
7573                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
7574   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7575                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7576                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
7577                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
7578   verifyFormat("LOG_IF(aaa == //\n"
7579                "       bbb)\n"
7580                "    << a << b;");
7581 
7582   // But sometimes, breaking before the first "<<" is desirable.
7583   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
7584                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
7585   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
7586                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7587                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7588   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
7589                "    << BEF << IsTemplate << Description << E->getType();");
7590   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
7591                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7592                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7593   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
7594                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7595                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7596                "    << aaa;");
7597 
7598   verifyFormat(
7599       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7600       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7601 
7602   // Incomplete string literal.
7603   EXPECT_EQ("llvm::errs() << \"\n"
7604             "             << a;",
7605             format("llvm::errs() << \"\n<<a;"));
7606 
7607   verifyFormat("void f() {\n"
7608                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
7609                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
7610                "}");
7611 
7612   // Handle 'endl'.
7613   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
7614                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
7615   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
7616 
7617   // Handle '\n'.
7618   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
7619                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
7620   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
7621                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
7622   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
7623                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
7624   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
7625 }
7626 
7627 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
7628   verifyFormat("return out << \"somepacket = {\\n\"\n"
7629                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
7630                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
7631                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
7632                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
7633                "           << \"}\";");
7634 
7635   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
7636                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
7637                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
7638   verifyFormat(
7639       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
7640       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
7641       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
7642       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
7643       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
7644   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
7645                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
7646   verifyFormat(
7647       "void f() {\n"
7648       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
7649       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7650       "}");
7651 
7652   // Breaking before the first "<<" is generally not desirable.
7653   verifyFormat(
7654       "llvm::errs()\n"
7655       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7656       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7657       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7658       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7659       getLLVMStyleWithColumns(70));
7660   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7661                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7662                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7663                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7664                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7665                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7666                getLLVMStyleWithColumns(70));
7667 
7668   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
7669                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
7670                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
7671   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
7672                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
7673                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
7674   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
7675                "           (aaaa + aaaa);",
7676                getLLVMStyleWithColumns(40));
7677   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
7678                "                  (aaaaaaa + aaaaa));",
7679                getLLVMStyleWithColumns(40));
7680   verifyFormat(
7681       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
7682       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
7683       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
7684 }
7685 
7686 TEST_F(FormatTest, UnderstandsEquals) {
7687   verifyFormat(
7688       "aaaaaaaaaaaaaaaaa =\n"
7689       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7690   verifyFormat(
7691       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7692       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
7693   verifyFormat(
7694       "if (a) {\n"
7695       "  f();\n"
7696       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7697       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7698       "}");
7699 
7700   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7701                "        100000000 + 10000000) {\n}");
7702 }
7703 
7704 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
7705   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
7706                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
7707 
7708   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
7709                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
7710 
7711   verifyFormat(
7712       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
7713       "                                                          Parameter2);");
7714 
7715   verifyFormat(
7716       "ShortObject->shortFunction(\n"
7717       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
7718       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
7719 
7720   verifyFormat("loooooooooooooongFunction(\n"
7721                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
7722 
7723   verifyFormat(
7724       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
7725       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
7726 
7727   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
7728                "    .WillRepeatedly(Return(SomeValue));");
7729   verifyFormat("void f() {\n"
7730                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
7731                "      .Times(2)\n"
7732                "      .WillRepeatedly(Return(SomeValue));\n"
7733                "}");
7734   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
7735                "    ccccccccccccccccccccccc);");
7736   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7737                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7738                "          .aaaaa(aaaaa),\n"
7739                "      aaaaaaaaaaaaaaaaaaaaa);");
7740   verifyFormat("void f() {\n"
7741                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7742                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
7743                "}");
7744   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7745                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7746                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7747                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7748                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7749   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7750                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7751                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7752                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
7753                "}");
7754 
7755   // Here, it is not necessary to wrap at "." or "->".
7756   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
7757                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
7758   verifyFormat(
7759       "aaaaaaaaaaa->aaaaaaaaa(\n"
7760       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7761       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
7762 
7763   verifyFormat(
7764       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7765       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
7766   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
7767                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
7768   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
7769                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
7770 
7771   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7772                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7773                "    .a();");
7774 
7775   FormatStyle NoBinPacking = getLLVMStyle();
7776   NoBinPacking.BinPackParameters = false;
7777   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7778                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7779                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
7780                "                         aaaaaaaaaaaaaaaaaaa,\n"
7781                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7782                NoBinPacking);
7783 
7784   // If there is a subsequent call, change to hanging indentation.
7785   verifyFormat(
7786       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7787       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
7788       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7789   verifyFormat(
7790       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7791       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
7792   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7793                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7794                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7795   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7796                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7797                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
7798 }
7799 
7800 TEST_F(FormatTest, WrapsTemplateDeclarations) {
7801   verifyFormat("template <typename T>\n"
7802                "virtual void loooooooooooongFunction(int Param1, int Param2);");
7803   verifyFormat("template <typename T>\n"
7804                "// T should be one of {A, B}.\n"
7805                "virtual void loooooooooooongFunction(int Param1, int Param2);");
7806   verifyFormat(
7807       "template <typename T>\n"
7808       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
7809   verifyFormat("template <typename T>\n"
7810                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
7811                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
7812   verifyFormat(
7813       "template <typename T>\n"
7814       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
7815       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
7816   verifyFormat(
7817       "template <typename T>\n"
7818       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
7819       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
7820       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7821   verifyFormat("template <typename T>\n"
7822                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7823                "    int aaaaaaaaaaaaaaaaaaaaaa);");
7824   verifyFormat(
7825       "template <typename T1, typename T2 = char, typename T3 = char,\n"
7826       "          typename T4 = char>\n"
7827       "void f();");
7828   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
7829                "          template <typename> class cccccccccccccccccccccc,\n"
7830                "          typename ddddddddddddd>\n"
7831                "class C {};");
7832   verifyFormat(
7833       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
7834       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7835 
7836   verifyFormat("void f() {\n"
7837                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
7838                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
7839                "}");
7840 
7841   verifyFormat("template <typename T> class C {};");
7842   verifyFormat("template <typename T> void f();");
7843   verifyFormat("template <typename T> void f() {}");
7844   verifyFormat(
7845       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
7846       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7847       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
7848       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
7849       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7850       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
7851       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
7852       getLLVMStyleWithColumns(72));
7853   EXPECT_EQ("static_cast<A< //\n"
7854             "    B> *>(\n"
7855             "\n"
7856             ");",
7857             format("static_cast<A<//\n"
7858                    "    B>*>(\n"
7859                    "\n"
7860                    "    );"));
7861   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7862                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
7863 
7864   FormatStyle AlwaysBreak = getLLVMStyle();
7865   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
7866   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
7867   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
7868   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
7869   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7870                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
7871                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
7872   verifyFormat("template <template <typename> class Fooooooo,\n"
7873                "          template <typename> class Baaaaaaar>\n"
7874                "struct C {};",
7875                AlwaysBreak);
7876   verifyFormat("template <typename T> // T can be A, B or C.\n"
7877                "struct C {};",
7878                AlwaysBreak);
7879   verifyFormat("template <enum E> class A {\n"
7880                "public:\n"
7881                "  E *f();\n"
7882                "};");
7883 
7884   FormatStyle NeverBreak = getLLVMStyle();
7885   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
7886   verifyFormat("template <typename T> class C {};", NeverBreak);
7887   verifyFormat("template <typename T> void f();", NeverBreak);
7888   verifyFormat("template <typename T> void f() {}", NeverBreak);
7889   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
7890                "bbbbbbbbbbbbbbbbbbbb) {}",
7891                NeverBreak);
7892   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7893                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
7894                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
7895                NeverBreak);
7896   verifyFormat("template <template <typename> class Fooooooo,\n"
7897                "          template <typename> class Baaaaaaar>\n"
7898                "struct C {};",
7899                NeverBreak);
7900   verifyFormat("template <typename T> // T can be A, B or C.\n"
7901                "struct C {};",
7902                NeverBreak);
7903   verifyFormat("template <enum E> class A {\n"
7904                "public:\n"
7905                "  E *f();\n"
7906                "};",
7907                NeverBreak);
7908   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
7909   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
7910                "bbbbbbbbbbbbbbbbbbbb) {}",
7911                NeverBreak);
7912 }
7913 
7914 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
7915   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
7916   Style.ColumnLimit = 60;
7917   EXPECT_EQ("// Baseline - no comments.\n"
7918             "template <\n"
7919             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
7920             "void f() {}",
7921             format("// Baseline - no comments.\n"
7922                    "template <\n"
7923                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
7924                    "void f() {}",
7925                    Style));
7926 
7927   EXPECT_EQ("template <\n"
7928             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
7929             "void f() {}",
7930             format("template <\n"
7931                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7932                    "void f() {}",
7933                    Style));
7934 
7935   EXPECT_EQ(
7936       "template <\n"
7937       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
7938       "void f() {}",
7939       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
7940              "void f() {}",
7941              Style));
7942 
7943   EXPECT_EQ(
7944       "template <\n"
7945       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
7946       "                                               // multiline\n"
7947       "void f() {}",
7948       format("template <\n"
7949              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7950              "                                              // multiline\n"
7951              "void f() {}",
7952              Style));
7953 
7954   EXPECT_EQ(
7955       "template <typename aaaaaaaaaa<\n"
7956       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
7957       "void f() {}",
7958       format(
7959           "template <\n"
7960           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
7961           "void f() {}",
7962           Style));
7963 }
7964 
7965 TEST_F(FormatTest, WrapsTemplateParameters) {
7966   FormatStyle Style = getLLVMStyle();
7967   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7968   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7969   verifyFormat(
7970       "template <typename... a> struct q {};\n"
7971       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
7972       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
7973       "    y;",
7974       Style);
7975   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7976   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7977   verifyFormat(
7978       "template <typename... a> struct r {};\n"
7979       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
7980       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
7981       "    y;",
7982       Style);
7983   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7984   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7985   verifyFormat("template <typename... a> struct s {};\n"
7986                "extern s<\n"
7987                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7988                "aaaaaaaaaaaaaaaaaaaaaa,\n"
7989                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7990                "aaaaaaaaaaaaaaaaaaaaaa>\n"
7991                "    y;",
7992                Style);
7993   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7994   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7995   verifyFormat("template <typename... a> struct t {};\n"
7996                "extern t<\n"
7997                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7998                "aaaaaaaaaaaaaaaaaaaaaa,\n"
7999                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
8000                "aaaaaaaaaaaaaaaaaaaaaa>\n"
8001                "    y;",
8002                Style);
8003 }
8004 
8005 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
8006   verifyFormat(
8007       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8008       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8009   verifyFormat(
8010       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8011       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8012       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
8013 
8014   // FIXME: Should we have the extra indent after the second break?
8015   verifyFormat(
8016       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8017       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8018       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8019 
8020   verifyFormat(
8021       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
8022       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
8023 
8024   // Breaking at nested name specifiers is generally not desirable.
8025   verifyFormat(
8026       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8027       "    aaaaaaaaaaaaaaaaaaaaaaa);");
8028 
8029   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
8030                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8031                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8032                "                   aaaaaaaaaaaaaaaaaaaaa);",
8033                getLLVMStyleWithColumns(74));
8034 
8035   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
8036                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8037                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8038 }
8039 
8040 TEST_F(FormatTest, UnderstandsTemplateParameters) {
8041   verifyFormat("A<int> a;");
8042   verifyFormat("A<A<A<int>>> a;");
8043   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8044   verifyFormat("bool x = a < 1 || 2 > a;");
8045   verifyFormat("bool x = 5 < f<int>();");
8046   verifyFormat("bool x = f<int>() > 5;");
8047   verifyFormat("bool x = 5 < a<int>::x;");
8048   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8049   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8050 
8051   verifyGoogleFormat("A<A<int>> a;");
8052   verifyGoogleFormat("A<A<A<int>>> a;");
8053   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8054   verifyGoogleFormat("A<A<int> > a;");
8055   verifyGoogleFormat("A<A<A<int> > > a;");
8056   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8057   verifyGoogleFormat("A<::A<int>> a;");
8058   verifyGoogleFormat("A<::A> a;");
8059   verifyGoogleFormat("A< ::A> a;");
8060   verifyGoogleFormat("A< ::A<int> > a;");
8061   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8062   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8063   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8064   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8065   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8066             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8067 
8068   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8069 
8070   // template closer followed by a token that starts with > or =
8071   verifyFormat("bool b = a<1> > 1;");
8072   verifyFormat("bool b = a<1> >= 1;");
8073   verifyFormat("int i = a<1> >> 1;");
8074   FormatStyle Style = getLLVMStyle();
8075   Style.SpaceBeforeAssignmentOperators = false;
8076   verifyFormat("bool b= a<1> == 1;", Style);
8077   verifyFormat("a<int> = 1;", Style);
8078   verifyFormat("a<int> >>= 1;", Style);
8079 
8080   verifyFormat("test < a | b >> c;");
8081   verifyFormat("test<test<a | b>> c;");
8082   verifyFormat("test >> a >> b;");
8083   verifyFormat("test << a >> b;");
8084 
8085   verifyFormat("f<int>();");
8086   verifyFormat("template <typename T> void f() {}");
8087   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8088   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8089                "sizeof(char)>::type>;");
8090   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8091   verifyFormat("f(a.operator()<A>());");
8092   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8093                "      .template operator()<A>());",
8094                getLLVMStyleWithColumns(35));
8095 
8096   // Not template parameters.
8097   verifyFormat("return a < b && c > d;");
8098   verifyFormat("void f() {\n"
8099                "  while (a < b && c > d) {\n"
8100                "  }\n"
8101                "}");
8102   verifyFormat("template <typename... Types>\n"
8103                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
8104 
8105   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8106                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
8107                getLLVMStyleWithColumns(60));
8108   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
8109   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
8110   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
8111   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
8112 }
8113 
8114 TEST_F(FormatTest, UnderstandsShiftOperators) {
8115   verifyFormat("if (i < x >> 1)");
8116   verifyFormat("while (i < x >> 1)");
8117   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
8118   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
8119   verifyFormat(
8120       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
8121   verifyFormat("Foo.call<Bar<Function>>()");
8122   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
8123   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
8124                "++i, v = v >> 1)");
8125   verifyFormat("if (w<u<v<x>>, 1>::t)");
8126 }
8127 
8128 TEST_F(FormatTest, BitshiftOperatorWidth) {
8129   EXPECT_EQ("int a = 1 << 2; /* foo\n"
8130             "                   bar */",
8131             format("int    a=1<<2;  /* foo\n"
8132                    "                   bar */"));
8133 
8134   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
8135             "                     bar */",
8136             format("int  b  =256>>1 ;  /* foo\n"
8137                    "                      bar */"));
8138 }
8139 
8140 TEST_F(FormatTest, UnderstandsBinaryOperators) {
8141   verifyFormat("COMPARE(a, ==, b);");
8142   verifyFormat("auto s = sizeof...(Ts) - 1;");
8143 }
8144 
8145 TEST_F(FormatTest, UnderstandsPointersToMembers) {
8146   verifyFormat("int A::*x;");
8147   verifyFormat("int (S::*func)(void *);");
8148   verifyFormat("void f() { int (S::*func)(void *); }");
8149   verifyFormat("typedef bool *(Class::*Member)() const;");
8150   verifyFormat("void f() {\n"
8151                "  (a->*f)();\n"
8152                "  a->*x;\n"
8153                "  (a.*f)();\n"
8154                "  ((*a).*f)();\n"
8155                "  a.*x;\n"
8156                "}");
8157   verifyFormat("void f() {\n"
8158                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
8159                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
8160                "}");
8161   verifyFormat(
8162       "(aaaaaaaaaa->*bbbbbbb)(\n"
8163       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8164   FormatStyle Style = getLLVMStyle();
8165   Style.PointerAlignment = FormatStyle::PAS_Left;
8166   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
8167 }
8168 
8169 TEST_F(FormatTest, UnderstandsUnaryOperators) {
8170   verifyFormat("int a = -2;");
8171   verifyFormat("f(-1, -2, -3);");
8172   verifyFormat("a[-1] = 5;");
8173   verifyFormat("int a = 5 + -2;");
8174   verifyFormat("if (i == -1) {\n}");
8175   verifyFormat("if (i != -1) {\n}");
8176   verifyFormat("if (i > -1) {\n}");
8177   verifyFormat("if (i < -1) {\n}");
8178   verifyFormat("++(a->f());");
8179   verifyFormat("--(a->f());");
8180   verifyFormat("(a->f())++;");
8181   verifyFormat("a[42]++;");
8182   verifyFormat("if (!(a->f())) {\n}");
8183   verifyFormat("if (!+i) {\n}");
8184   verifyFormat("~&a;");
8185 
8186   verifyFormat("a-- > b;");
8187   verifyFormat("b ? -a : c;");
8188   verifyFormat("n * sizeof char16;");
8189   verifyFormat("n * alignof char16;", getGoogleStyle());
8190   verifyFormat("sizeof(char);");
8191   verifyFormat("alignof(char);", getGoogleStyle());
8192 
8193   verifyFormat("return -1;");
8194   verifyFormat("throw -1;");
8195   verifyFormat("switch (a) {\n"
8196                "case -1:\n"
8197                "  break;\n"
8198                "}");
8199   verifyFormat("#define X -1");
8200   verifyFormat("#define X -kConstant");
8201 
8202   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
8203   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
8204 
8205   verifyFormat("int a = /* confusing comment */ -1;");
8206   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
8207   verifyFormat("int a = i /* confusing comment */++;");
8208 
8209   verifyFormat("co_yield -1;");
8210   verifyFormat("co_return -1;");
8211 
8212   // Check that * is not treated as a binary operator when we set
8213   // PointerAlignment as PAS_Left after a keyword and not a declaration.
8214   FormatStyle PASLeftStyle = getLLVMStyle();
8215   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
8216   verifyFormat("co_return *a;", PASLeftStyle);
8217   verifyFormat("co_await *a;", PASLeftStyle);
8218   verifyFormat("co_yield *a", PASLeftStyle);
8219   verifyFormat("return *a;", PASLeftStyle);
8220 }
8221 
8222 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
8223   verifyFormat("if (!aaaaaaaaaa( // break\n"
8224                "        aaaaa)) {\n"
8225                "}");
8226   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
8227                "    aaaaa));");
8228   verifyFormat("*aaa = aaaaaaa( // break\n"
8229                "    bbbbbb);");
8230 }
8231 
8232 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
8233   verifyFormat("bool operator<();");
8234   verifyFormat("bool operator>();");
8235   verifyFormat("bool operator=();");
8236   verifyFormat("bool operator==();");
8237   verifyFormat("bool operator!=();");
8238   verifyFormat("int operator+();");
8239   verifyFormat("int operator++();");
8240   verifyFormat("int operator++(int) volatile noexcept;");
8241   verifyFormat("bool operator,();");
8242   verifyFormat("bool operator();");
8243   verifyFormat("bool operator()();");
8244   verifyFormat("bool operator[]();");
8245   verifyFormat("operator bool();");
8246   verifyFormat("operator int();");
8247   verifyFormat("operator void *();");
8248   verifyFormat("operator SomeType<int>();");
8249   verifyFormat("operator SomeType<int, int>();");
8250   verifyFormat("operator SomeType<SomeType<int>>();");
8251   verifyFormat("void *operator new(std::size_t size);");
8252   verifyFormat("void *operator new[](std::size_t size);");
8253   verifyFormat("void operator delete(void *ptr);");
8254   verifyFormat("void operator delete[](void *ptr);");
8255   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
8256                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
8257   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
8258                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
8259 
8260   verifyFormat(
8261       "ostream &operator<<(ostream &OutputStream,\n"
8262       "                    SomeReallyLongType WithSomeReallyLongValue);");
8263   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
8264                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
8265                "  return left.group < right.group;\n"
8266                "}");
8267   verifyFormat("SomeType &operator=(const SomeType &S);");
8268   verifyFormat("f.template operator()<int>();");
8269 
8270   verifyGoogleFormat("operator void*();");
8271   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
8272   verifyGoogleFormat("operator ::A();");
8273 
8274   verifyFormat("using A::operator+;");
8275   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
8276                "int i;");
8277 }
8278 
8279 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
8280   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
8281   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
8282   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
8283   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
8284   verifyFormat("Deleted &operator=(const Deleted &) &;");
8285   verifyFormat("Deleted &operator=(const Deleted &) &&;");
8286   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
8287   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
8288   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
8289   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
8290   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
8291   verifyFormat("void Fn(T const &) const &;");
8292   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
8293   verifyFormat("template <typename T>\n"
8294                "void F(T) && = delete;",
8295                getGoogleStyle());
8296 
8297   FormatStyle AlignLeft = getLLVMStyle();
8298   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
8299   verifyFormat("void A::b() && {}", AlignLeft);
8300   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
8301   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
8302                AlignLeft);
8303   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
8304   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
8305   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
8306   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
8307   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
8308   verifyFormat("auto Function(T) & -> void;", AlignLeft);
8309   verifyFormat("void Fn(T const&) const&;", AlignLeft);
8310   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
8311 
8312   FormatStyle Spaces = getLLVMStyle();
8313   Spaces.SpacesInCStyleCastParentheses = true;
8314   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
8315   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
8316   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
8317   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
8318 
8319   Spaces.SpacesInCStyleCastParentheses = false;
8320   Spaces.SpacesInParentheses = true;
8321   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
8322   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
8323                Spaces);
8324   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
8325   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
8326 
8327   FormatStyle BreakTemplate = getLLVMStyle();
8328   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8329 
8330   verifyFormat("struct f {\n"
8331                "  template <class T>\n"
8332                "  int &foo(const std::string &str) &noexcept {}\n"
8333                "};",
8334                BreakTemplate);
8335 
8336   verifyFormat("struct f {\n"
8337                "  template <class T>\n"
8338                "  int &foo(const std::string &str) &&noexcept {}\n"
8339                "};",
8340                BreakTemplate);
8341 
8342   verifyFormat("struct f {\n"
8343                "  template <class T>\n"
8344                "  int &foo(const std::string &str) const &noexcept {}\n"
8345                "};",
8346                BreakTemplate);
8347 
8348   verifyFormat("struct f {\n"
8349                "  template <class T>\n"
8350                "  int &foo(const std::string &str) const &noexcept {}\n"
8351                "};",
8352                BreakTemplate);
8353 
8354   verifyFormat("struct f {\n"
8355                "  template <class T>\n"
8356                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
8357                "};",
8358                BreakTemplate);
8359 
8360   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
8361   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
8362       FormatStyle::BTDS_Yes;
8363   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
8364 
8365   verifyFormat("struct f {\n"
8366                "  template <class T>\n"
8367                "  int& foo(const std::string& str) & noexcept {}\n"
8368                "};",
8369                AlignLeftBreakTemplate);
8370 
8371   verifyFormat("struct f {\n"
8372                "  template <class T>\n"
8373                "  int& foo(const std::string& str) && noexcept {}\n"
8374                "};",
8375                AlignLeftBreakTemplate);
8376 
8377   verifyFormat("struct f {\n"
8378                "  template <class T>\n"
8379                "  int& foo(const std::string& str) const& noexcept {}\n"
8380                "};",
8381                AlignLeftBreakTemplate);
8382 
8383   verifyFormat("struct f {\n"
8384                "  template <class T>\n"
8385                "  int& foo(const std::string& str) const&& noexcept {}\n"
8386                "};",
8387                AlignLeftBreakTemplate);
8388 
8389   verifyFormat("struct f {\n"
8390                "  template <class T>\n"
8391                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
8392                "};",
8393                AlignLeftBreakTemplate);
8394 
8395   // The `&` in `Type&` should not be confused with a trailing `&` of
8396   // DEPRECATED(reason) member function.
8397   verifyFormat("struct f {\n"
8398                "  template <class T>\n"
8399                "  DEPRECATED(reason)\n"
8400                "  Type &foo(arguments) {}\n"
8401                "};",
8402                BreakTemplate);
8403 
8404   verifyFormat("struct f {\n"
8405                "  template <class T>\n"
8406                "  DEPRECATED(reason)\n"
8407                "  Type& foo(arguments) {}\n"
8408                "};",
8409                AlignLeftBreakTemplate);
8410 
8411   verifyFormat("void (*foopt)(int) = &func;");
8412 }
8413 
8414 TEST_F(FormatTest, UnderstandsNewAndDelete) {
8415   verifyFormat("void f() {\n"
8416                "  A *a = new A;\n"
8417                "  A *a = new (placement) A;\n"
8418                "  delete a;\n"
8419                "  delete (A *)a;\n"
8420                "}");
8421   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
8422                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
8423   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8424                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
8425                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
8426   verifyFormat("delete[] h->p;");
8427 }
8428 
8429 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
8430   verifyFormat("int *f(int *a) {}");
8431   verifyFormat("int main(int argc, char **argv) {}");
8432   verifyFormat("Test::Test(int b) : a(b * b) {}");
8433   verifyIndependentOfContext("f(a, *a);");
8434   verifyFormat("void g() { f(*a); }");
8435   verifyIndependentOfContext("int a = b * 10;");
8436   verifyIndependentOfContext("int a = 10 * b;");
8437   verifyIndependentOfContext("int a = b * c;");
8438   verifyIndependentOfContext("int a += b * c;");
8439   verifyIndependentOfContext("int a -= b * c;");
8440   verifyIndependentOfContext("int a *= b * c;");
8441   verifyIndependentOfContext("int a /= b * c;");
8442   verifyIndependentOfContext("int a = *b;");
8443   verifyIndependentOfContext("int a = *b * c;");
8444   verifyIndependentOfContext("int a = b * *c;");
8445   verifyIndependentOfContext("int a = b * (10);");
8446   verifyIndependentOfContext("S << b * (10);");
8447   verifyIndependentOfContext("return 10 * b;");
8448   verifyIndependentOfContext("return *b * *c;");
8449   verifyIndependentOfContext("return a & ~b;");
8450   verifyIndependentOfContext("f(b ? *c : *d);");
8451   verifyIndependentOfContext("int a = b ? *c : *d;");
8452   verifyIndependentOfContext("*b = a;");
8453   verifyIndependentOfContext("a * ~b;");
8454   verifyIndependentOfContext("a * !b;");
8455   verifyIndependentOfContext("a * +b;");
8456   verifyIndependentOfContext("a * -b;");
8457   verifyIndependentOfContext("a * ++b;");
8458   verifyIndependentOfContext("a * --b;");
8459   verifyIndependentOfContext("a[4] * b;");
8460   verifyIndependentOfContext("a[a * a] = 1;");
8461   verifyIndependentOfContext("f() * b;");
8462   verifyIndependentOfContext("a * [self dostuff];");
8463   verifyIndependentOfContext("int x = a * (a + b);");
8464   verifyIndependentOfContext("(a *)(a + b);");
8465   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
8466   verifyIndependentOfContext("int *pa = (int *)&a;");
8467   verifyIndependentOfContext("return sizeof(int **);");
8468   verifyIndependentOfContext("return sizeof(int ******);");
8469   verifyIndependentOfContext("return (int **&)a;");
8470   verifyIndependentOfContext("f((*PointerToArray)[10]);");
8471   verifyFormat("void f(Type (*parameter)[10]) {}");
8472   verifyFormat("void f(Type (&parameter)[10]) {}");
8473   verifyGoogleFormat("return sizeof(int**);");
8474   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
8475   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
8476   verifyFormat("auto a = [](int **&, int ***) {};");
8477   verifyFormat("auto PointerBinding = [](const char *S) {};");
8478   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
8479   verifyFormat("[](const decltype(*a) &value) {}");
8480   verifyFormat("[](const typeof(*a) &value) {}");
8481   verifyFormat("[](const _Atomic(a *) &value) {}");
8482   verifyFormat("[](const __underlying_type(a) &value) {}");
8483   verifyFormat("decltype(a * b) F();");
8484   verifyFormat("typeof(a * b) F();");
8485   verifyFormat("#define MACRO() [](A *a) { return 1; }");
8486   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
8487   verifyIndependentOfContext("typedef void (*f)(int *a);");
8488   verifyIndependentOfContext("int i{a * b};");
8489   verifyIndependentOfContext("aaa && aaa->f();");
8490   verifyIndependentOfContext("int x = ~*p;");
8491   verifyFormat("Constructor() : a(a), area(width * height) {}");
8492   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
8493   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
8494   verifyFormat("void f() { f(a, c * d); }");
8495   verifyFormat("void f() { f(new a(), c * d); }");
8496   verifyFormat("void f(const MyOverride &override);");
8497   verifyFormat("void f(const MyFinal &final);");
8498   verifyIndependentOfContext("bool a = f() && override.f();");
8499   verifyIndependentOfContext("bool a = f() && final.f();");
8500 
8501   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
8502 
8503   verifyIndependentOfContext("A<int *> a;");
8504   verifyIndependentOfContext("A<int **> a;");
8505   verifyIndependentOfContext("A<int *, int *> a;");
8506   verifyIndependentOfContext("A<int *[]> a;");
8507   verifyIndependentOfContext(
8508       "const char *const p = reinterpret_cast<const char *const>(q);");
8509   verifyIndependentOfContext("A<int **, int **> a;");
8510   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
8511   verifyFormat("for (char **a = b; *a; ++a) {\n}");
8512   verifyFormat("for (; a && b;) {\n}");
8513   verifyFormat("bool foo = true && [] { return false; }();");
8514 
8515   verifyFormat(
8516       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8517       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8518 
8519   verifyGoogleFormat("int const* a = &b;");
8520   verifyGoogleFormat("**outparam = 1;");
8521   verifyGoogleFormat("*outparam = a * b;");
8522   verifyGoogleFormat("int main(int argc, char** argv) {}");
8523   verifyGoogleFormat("A<int*> a;");
8524   verifyGoogleFormat("A<int**> a;");
8525   verifyGoogleFormat("A<int*, int*> a;");
8526   verifyGoogleFormat("A<int**, int**> a;");
8527   verifyGoogleFormat("f(b ? *c : *d);");
8528   verifyGoogleFormat("int a = b ? *c : *d;");
8529   verifyGoogleFormat("Type* t = **x;");
8530   verifyGoogleFormat("Type* t = *++*x;");
8531   verifyGoogleFormat("*++*x;");
8532   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
8533   verifyGoogleFormat("Type* t = x++ * y;");
8534   verifyGoogleFormat(
8535       "const char* const p = reinterpret_cast<const char* const>(q);");
8536   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
8537   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
8538   verifyGoogleFormat("template <typename T>\n"
8539                      "void f(int i = 0, SomeType** temps = NULL);");
8540 
8541   FormatStyle Left = getLLVMStyle();
8542   Left.PointerAlignment = FormatStyle::PAS_Left;
8543   verifyFormat("x = *a(x) = *a(y);", Left);
8544   verifyFormat("for (;; *a = b) {\n}", Left);
8545   verifyFormat("return *this += 1;", Left);
8546   verifyFormat("throw *x;", Left);
8547   verifyFormat("delete *x;", Left);
8548   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
8549   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
8550   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
8551   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
8552   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
8553   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
8554   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
8555   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
8556   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
8557 
8558   verifyIndependentOfContext("a = *(x + y);");
8559   verifyIndependentOfContext("a = &(x + y);");
8560   verifyIndependentOfContext("*(x + y).call();");
8561   verifyIndependentOfContext("&(x + y)->call();");
8562   verifyFormat("void f() { &(*I).first; }");
8563 
8564   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
8565   verifyFormat(
8566       "int *MyValues = {\n"
8567       "    *A, // Operator detection might be confused by the '{'\n"
8568       "    *BB // Operator detection might be confused by previous comment\n"
8569       "};");
8570 
8571   verifyIndependentOfContext("if (int *a = &b)");
8572   verifyIndependentOfContext("if (int &a = *b)");
8573   verifyIndependentOfContext("if (a & b[i])");
8574   verifyIndependentOfContext("if constexpr (a & b[i])");
8575   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
8576   verifyIndependentOfContext("if (a * (b * c))");
8577   verifyIndependentOfContext("if constexpr (a * (b * c))");
8578   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
8579   verifyIndependentOfContext("if (a::b::c::d & b[i])");
8580   verifyIndependentOfContext("if (*b[i])");
8581   verifyIndependentOfContext("if (int *a = (&b))");
8582   verifyIndependentOfContext("while (int *a = &b)");
8583   verifyIndependentOfContext("while (a * (b * c))");
8584   verifyIndependentOfContext("size = sizeof *a;");
8585   verifyIndependentOfContext("if (a && (b = c))");
8586   verifyFormat("void f() {\n"
8587                "  for (const int &v : Values) {\n"
8588                "  }\n"
8589                "}");
8590   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
8591   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
8592   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
8593 
8594   verifyFormat("#define A (!a * b)");
8595   verifyFormat("#define MACRO     \\\n"
8596                "  int *i = a * b; \\\n"
8597                "  void f(a *b);",
8598                getLLVMStyleWithColumns(19));
8599 
8600   verifyIndependentOfContext("A = new SomeType *[Length];");
8601   verifyIndependentOfContext("A = new SomeType *[Length]();");
8602   verifyIndependentOfContext("T **t = new T *;");
8603   verifyIndependentOfContext("T **t = new T *();");
8604   verifyGoogleFormat("A = new SomeType*[Length]();");
8605   verifyGoogleFormat("A = new SomeType*[Length];");
8606   verifyGoogleFormat("T** t = new T*;");
8607   verifyGoogleFormat("T** t = new T*();");
8608 
8609   verifyFormat("STATIC_ASSERT((a & b) == 0);");
8610   verifyFormat("STATIC_ASSERT(0 == (a & b));");
8611   verifyFormat("template <bool a, bool b> "
8612                "typename t::if<x && y>::type f() {}");
8613   verifyFormat("template <int *y> f() {}");
8614   verifyFormat("vector<int *> v;");
8615   verifyFormat("vector<int *const> v;");
8616   verifyFormat("vector<int *const **const *> v;");
8617   verifyFormat("vector<int *volatile> v;");
8618   verifyFormat("vector<a *_Nonnull> v;");
8619   verifyFormat("vector<a *_Nullable> v;");
8620   verifyFormat("vector<a *_Null_unspecified> v;");
8621   verifyFormat("vector<a *__ptr32> v;");
8622   verifyFormat("vector<a *__ptr64> v;");
8623   verifyFormat("vector<a *__capability> v;");
8624   FormatStyle TypeMacros = getLLVMStyle();
8625   TypeMacros.TypenameMacros = {"LIST"};
8626   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
8627   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
8628   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
8629   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
8630   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
8631 
8632   FormatStyle CustomQualifier = getLLVMStyle();
8633   // Add indentifers that should not be parsed as a qualifier by default.
8634   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
8635   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
8636   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
8637   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
8638   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
8639   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
8640   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
8641   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
8642   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
8643   verifyFormat("vector<a * _NotAQualifier> v;");
8644   verifyFormat("vector<a * __not_a_qualifier> v;");
8645   verifyFormat("vector<a * b> v;");
8646   verifyFormat("foo<b && false>();");
8647   verifyFormat("foo<b & 1>();");
8648   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
8649   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
8650   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
8651   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
8652   verifyFormat(
8653       "template <class T, class = typename std::enable_if<\n"
8654       "                       std::is_integral<T>::value &&\n"
8655       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
8656       "void F();",
8657       getLLVMStyleWithColumns(70));
8658   verifyFormat("template <class T,\n"
8659                "          class = typename std::enable_if<\n"
8660                "              std::is_integral<T>::value &&\n"
8661                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
8662                "          class U>\n"
8663                "void F();",
8664                getLLVMStyleWithColumns(70));
8665   verifyFormat(
8666       "template <class T,\n"
8667       "          class = typename ::std::enable_if<\n"
8668       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
8669       "void F();",
8670       getGoogleStyleWithColumns(68));
8671 
8672   verifyIndependentOfContext("MACRO(int *i);");
8673   verifyIndependentOfContext("MACRO(auto *a);");
8674   verifyIndependentOfContext("MACRO(const A *a);");
8675   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
8676   verifyIndependentOfContext("MACRO(decltype(A) *a);");
8677   verifyIndependentOfContext("MACRO(typeof(A) *a);");
8678   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
8679   verifyIndependentOfContext("MACRO(A *const a);");
8680   verifyIndependentOfContext("MACRO(A *restrict a);");
8681   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
8682   verifyIndependentOfContext("MACRO(A *__restrict a);");
8683   verifyIndependentOfContext("MACRO(A *volatile a);");
8684   verifyIndependentOfContext("MACRO(A *__volatile a);");
8685   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
8686   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
8687   verifyIndependentOfContext("MACRO(A *_Nullable a);");
8688   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
8689   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
8690   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
8691   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
8692   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
8693   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
8694   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
8695   verifyIndependentOfContext("MACRO(A *__capability);");
8696   verifyIndependentOfContext("MACRO(A &__capability);");
8697   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
8698   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
8699   // If we add __my_qualifier to AttributeMacros it should always be parsed as
8700   // a type declaration:
8701   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
8702   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
8703   // Also check that TypenameMacros prevents parsing it as multiplication:
8704   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
8705   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
8706 
8707   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
8708   verifyFormat("void f() { f(float{1}, a * a); }");
8709   verifyFormat("void f() { f(float(1), a * a); }");
8710 
8711   verifyFormat("f((void (*)(int))g);");
8712   verifyFormat("f((void (&)(int))g);");
8713   verifyFormat("f((void (^)(int))g);");
8714 
8715   // FIXME: Is there a way to make this work?
8716   // verifyIndependentOfContext("MACRO(A *a);");
8717   verifyFormat("MACRO(A &B);");
8718   verifyFormat("MACRO(A *B);");
8719   verifyFormat("void f() { MACRO(A * B); }");
8720   verifyFormat("void f() { MACRO(A & B); }");
8721 
8722   // This lambda was mis-formatted after D88956 (treating it as a binop):
8723   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
8724   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
8725   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
8726   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
8727 
8728   verifyFormat("DatumHandle const *operator->() const { return input_; }");
8729   verifyFormat("return options != nullptr && operator==(*options);");
8730 
8731   EXPECT_EQ("#define OP(x)                                    \\\n"
8732             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
8733             "    return s << a.DebugString();                 \\\n"
8734             "  }",
8735             format("#define OP(x) \\\n"
8736                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
8737                    "    return s << a.DebugString(); \\\n"
8738                    "  }",
8739                    getLLVMStyleWithColumns(50)));
8740 
8741   // FIXME: We cannot handle this case yet; we might be able to figure out that
8742   // foo<x> d > v; doesn't make sense.
8743   verifyFormat("foo<a<b && c> d> v;");
8744 
8745   FormatStyle PointerMiddle = getLLVMStyle();
8746   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
8747   verifyFormat("delete *x;", PointerMiddle);
8748   verifyFormat("int * x;", PointerMiddle);
8749   verifyFormat("int *[] x;", PointerMiddle);
8750   verifyFormat("template <int * y> f() {}", PointerMiddle);
8751   verifyFormat("int * f(int * a) {}", PointerMiddle);
8752   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
8753   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
8754   verifyFormat("A<int *> a;", PointerMiddle);
8755   verifyFormat("A<int **> a;", PointerMiddle);
8756   verifyFormat("A<int *, int *> a;", PointerMiddle);
8757   verifyFormat("A<int *[]> a;", PointerMiddle);
8758   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
8759   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
8760   verifyFormat("T ** t = new T *;", PointerMiddle);
8761 
8762   // Member function reference qualifiers aren't binary operators.
8763   verifyFormat("string // break\n"
8764                "operator()() & {}");
8765   verifyFormat("string // break\n"
8766                "operator()() && {}");
8767   verifyGoogleFormat("template <typename T>\n"
8768                      "auto x() & -> int {}");
8769 }
8770 
8771 TEST_F(FormatTest, UnderstandsAttributes) {
8772   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
8773   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
8774                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
8775   FormatStyle AfterType = getLLVMStyle();
8776   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8777   verifyFormat("__attribute__((nodebug)) void\n"
8778                "foo() {}\n",
8779                AfterType);
8780   verifyFormat("__unused void\n"
8781                "foo() {}",
8782                AfterType);
8783 
8784   FormatStyle CustomAttrs = getLLVMStyle();
8785   CustomAttrs.AttributeMacros.push_back("__unused");
8786   CustomAttrs.AttributeMacros.push_back("__attr1");
8787   CustomAttrs.AttributeMacros.push_back("__attr2");
8788   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
8789   verifyFormat("vector<SomeType *__attribute((foo))> v;");
8790   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
8791   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
8792   // Check that it is parsed as a multiplication without AttributeMacros and
8793   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
8794   verifyFormat("vector<SomeType * __attr1> v;");
8795   verifyFormat("vector<SomeType __attr1 *> v;");
8796   verifyFormat("vector<SomeType __attr1 *const> v;");
8797   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
8798   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
8799   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
8800   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
8801   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
8802   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
8803   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
8804   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
8805 
8806   // Check that these are not parsed as function declarations:
8807   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8808   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
8809   verifyFormat("SomeType s(InitValue);", CustomAttrs);
8810   verifyFormat("SomeType s{InitValue};", CustomAttrs);
8811   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
8812   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
8813   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
8814   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
8815   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
8816   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
8817 }
8818 
8819 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
8820   // Check that qualifiers on pointers don't break parsing of casts.
8821   verifyFormat("x = (foo *const)*v;");
8822   verifyFormat("x = (foo *volatile)*v;");
8823   verifyFormat("x = (foo *restrict)*v;");
8824   verifyFormat("x = (foo *__attribute__((foo)))*v;");
8825   verifyFormat("x = (foo *_Nonnull)*v;");
8826   verifyFormat("x = (foo *_Nullable)*v;");
8827   verifyFormat("x = (foo *_Null_unspecified)*v;");
8828   verifyFormat("x = (foo *_Nonnull)*v;");
8829   verifyFormat("x = (foo *[[clang::attr]])*v;");
8830   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
8831   verifyFormat("x = (foo *__ptr32)*v;");
8832   verifyFormat("x = (foo *__ptr64)*v;");
8833   verifyFormat("x = (foo *__capability)*v;");
8834 
8835   // Check that we handle multiple trailing qualifiers and skip them all to
8836   // determine that the expression is a cast to a pointer type.
8837   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
8838   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
8839   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
8840   StringRef AllQualifiers =
8841       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
8842       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
8843   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
8844   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
8845 
8846   // Also check that address-of is not parsed as a binary bitwise-and:
8847   verifyFormat("x = (foo *const)&v;");
8848   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
8849   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
8850 
8851   // Check custom qualifiers:
8852   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
8853   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
8854   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
8855   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
8856   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
8857                CustomQualifier);
8858   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
8859                CustomQualifier);
8860 
8861   // Check that unknown identifiers result in binary operator parsing:
8862   verifyFormat("x = (foo * __unknown_qualifier) * v;");
8863   verifyFormat("x = (foo * __unknown_qualifier) & v;");
8864 }
8865 
8866 TEST_F(FormatTest, UnderstandsSquareAttributes) {
8867   verifyFormat("SomeType s [[unused]] (InitValue);");
8868   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
8869   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
8870   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
8871   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
8872   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8873                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
8874   verifyFormat("[[nodiscard]] bool f() { return false; }");
8875   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
8876   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
8877   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
8878 
8879   // Make sure we do not mistake attributes for array subscripts.
8880   verifyFormat("int a() {}\n"
8881                "[[unused]] int b() {}\n");
8882   verifyFormat("NSArray *arr;\n"
8883                "arr[[Foo() bar]];");
8884 
8885   // On the other hand, we still need to correctly find array subscripts.
8886   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
8887 
8888   // Make sure that we do not mistake Objective-C method inside array literals
8889   // as attributes, even if those method names are also keywords.
8890   verifyFormat("@[ [foo bar] ];");
8891   verifyFormat("@[ [NSArray class] ];");
8892   verifyFormat("@[ [foo enum] ];");
8893 
8894   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
8895 
8896   // Make sure we do not parse attributes as lambda introducers.
8897   FormatStyle MultiLineFunctions = getLLVMStyle();
8898   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8899   verifyFormat("[[unused]] int b() {\n"
8900                "  return 42;\n"
8901                "}\n",
8902                MultiLineFunctions);
8903 }
8904 
8905 TEST_F(FormatTest, AttributeClass) {
8906   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
8907   verifyFormat("class S {\n"
8908                "  S(S&&) = default;\n"
8909                "};",
8910                Style);
8911   verifyFormat("class [[nodiscard]] S {\n"
8912                "  S(S&&) = default;\n"
8913                "};",
8914                Style);
8915   verifyFormat("class __attribute((maybeunused)) S {\n"
8916                "  S(S&&) = default;\n"
8917                "};",
8918                Style);
8919   verifyFormat("struct S {\n"
8920                "  S(S&&) = default;\n"
8921                "};",
8922                Style);
8923   verifyFormat("struct [[nodiscard]] S {\n"
8924                "  S(S&&) = default;\n"
8925                "};",
8926                Style);
8927 }
8928 
8929 TEST_F(FormatTest, AttributesAfterMacro) {
8930   FormatStyle Style = getLLVMStyle();
8931   verifyFormat("MACRO;\n"
8932                "__attribute__((maybe_unused)) int foo() {\n"
8933                "  //...\n"
8934                "}");
8935 
8936   verifyFormat("MACRO;\n"
8937                "[[nodiscard]] int foo() {\n"
8938                "  //...\n"
8939                "}");
8940 
8941   EXPECT_EQ("MACRO\n\n"
8942             "__attribute__((maybe_unused)) int foo() {\n"
8943             "  //...\n"
8944             "}",
8945             format("MACRO\n\n"
8946                    "__attribute__((maybe_unused)) int foo() {\n"
8947                    "  //...\n"
8948                    "}"));
8949 
8950   EXPECT_EQ("MACRO\n\n"
8951             "[[nodiscard]] int foo() {\n"
8952             "  //...\n"
8953             "}",
8954             format("MACRO\n\n"
8955                    "[[nodiscard]] int foo() {\n"
8956                    "  //...\n"
8957                    "}"));
8958 }
8959 
8960 TEST_F(FormatTest, AttributePenaltyBreaking) {
8961   FormatStyle Style = getLLVMStyle();
8962   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
8963                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
8964                Style);
8965   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
8966                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
8967                Style);
8968   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
8969                "shared_ptr<ALongTypeName> &C d) {\n}",
8970                Style);
8971 }
8972 
8973 TEST_F(FormatTest, UnderstandsEllipsis) {
8974   FormatStyle Style = getLLVMStyle();
8975   verifyFormat("int printf(const char *fmt, ...);");
8976   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
8977   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
8978 
8979   verifyFormat("template <int *...PP> a;", Style);
8980 
8981   Style.PointerAlignment = FormatStyle::PAS_Left;
8982   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
8983 
8984   verifyFormat("template <int*... PP> a;", Style);
8985 
8986   Style.PointerAlignment = FormatStyle::PAS_Middle;
8987   verifyFormat("template <int *... PP> a;", Style);
8988 }
8989 
8990 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
8991   EXPECT_EQ("int *a;\n"
8992             "int *a;\n"
8993             "int *a;",
8994             format("int *a;\n"
8995                    "int* a;\n"
8996                    "int *a;",
8997                    getGoogleStyle()));
8998   EXPECT_EQ("int* a;\n"
8999             "int* a;\n"
9000             "int* a;",
9001             format("int* a;\n"
9002                    "int* a;\n"
9003                    "int *a;",
9004                    getGoogleStyle()));
9005   EXPECT_EQ("int *a;\n"
9006             "int *a;\n"
9007             "int *a;",
9008             format("int *a;\n"
9009                    "int * a;\n"
9010                    "int *  a;",
9011                    getGoogleStyle()));
9012   EXPECT_EQ("auto x = [] {\n"
9013             "  int *a;\n"
9014             "  int *a;\n"
9015             "  int *a;\n"
9016             "};",
9017             format("auto x=[]{int *a;\n"
9018                    "int * a;\n"
9019                    "int *  a;};",
9020                    getGoogleStyle()));
9021 }
9022 
9023 TEST_F(FormatTest, UnderstandsRvalueReferences) {
9024   verifyFormat("int f(int &&a) {}");
9025   verifyFormat("int f(int a, char &&b) {}");
9026   verifyFormat("void f() { int &&a = b; }");
9027   verifyGoogleFormat("int f(int a, char&& b) {}");
9028   verifyGoogleFormat("void f() { int&& a = b; }");
9029 
9030   verifyIndependentOfContext("A<int &&> a;");
9031   verifyIndependentOfContext("A<int &&, int &&> a;");
9032   verifyGoogleFormat("A<int&&> a;");
9033   verifyGoogleFormat("A<int&&, int&&> a;");
9034 
9035   // Not rvalue references:
9036   verifyFormat("template <bool B, bool C> class A {\n"
9037                "  static_assert(B && C, \"Something is wrong\");\n"
9038                "};");
9039   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
9040   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
9041   verifyFormat("#define A(a, b) (a && b)");
9042 }
9043 
9044 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
9045   verifyFormat("void f() {\n"
9046                "  x[aaaaaaaaa -\n"
9047                "    b] = 23;\n"
9048                "}",
9049                getLLVMStyleWithColumns(15));
9050 }
9051 
9052 TEST_F(FormatTest, FormatsCasts) {
9053   verifyFormat("Type *A = static_cast<Type *>(P);");
9054   verifyFormat("Type *A = (Type *)P;");
9055   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9056   verifyFormat("int a = (int)(2.0f);");
9057   verifyFormat("int a = (int)2.0f;");
9058   verifyFormat("x[(int32)y];");
9059   verifyFormat("x = (int32)y;");
9060   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9061   verifyFormat("int a = (int)*b;");
9062   verifyFormat("int a = (int)2.0f;");
9063   verifyFormat("int a = (int)~0;");
9064   verifyFormat("int a = (int)++a;");
9065   verifyFormat("int a = (int)sizeof(int);");
9066   verifyFormat("int a = (int)+2;");
9067   verifyFormat("my_int a = (my_int)2.0f;");
9068   verifyFormat("my_int a = (my_int)sizeof(int);");
9069   verifyFormat("return (my_int)aaa;");
9070   verifyFormat("#define x ((int)-1)");
9071   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
9072   verifyFormat("#define p(q) ((int *)&q)");
9073   verifyFormat("fn(a)(b) + 1;");
9074 
9075   verifyFormat("void f() { my_int a = (my_int)*b; }");
9076   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
9077   verifyFormat("my_int a = (my_int)~0;");
9078   verifyFormat("my_int a = (my_int)++a;");
9079   verifyFormat("my_int a = (my_int)-2;");
9080   verifyFormat("my_int a = (my_int)1;");
9081   verifyFormat("my_int a = (my_int *)1;");
9082   verifyFormat("my_int a = (const my_int)-1;");
9083   verifyFormat("my_int a = (const my_int *)-1;");
9084   verifyFormat("my_int a = (my_int)(my_int)-1;");
9085   verifyFormat("my_int a = (ns::my_int)-2;");
9086   verifyFormat("case (my_int)ONE:");
9087   verifyFormat("auto x = (X)this;");
9088   // Casts in Obj-C style calls used to not be recognized as such.
9089   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
9090 
9091   // FIXME: single value wrapped with paren will be treated as cast.
9092   verifyFormat("void f(int i = (kValue)*kMask) {}");
9093 
9094   verifyFormat("{ (void)F; }");
9095 
9096   // Don't break after a cast's
9097   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9098                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
9099                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
9100 
9101   // These are not casts.
9102   verifyFormat("void f(int *) {}");
9103   verifyFormat("f(foo)->b;");
9104   verifyFormat("f(foo).b;");
9105   verifyFormat("f(foo)(b);");
9106   verifyFormat("f(foo)[b];");
9107   verifyFormat("[](foo) { return 4; }(bar);");
9108   verifyFormat("(*funptr)(foo)[4];");
9109   verifyFormat("funptrs[4](foo)[4];");
9110   verifyFormat("void f(int *);");
9111   verifyFormat("void f(int *) = 0;");
9112   verifyFormat("void f(SmallVector<int>) {}");
9113   verifyFormat("void f(SmallVector<int>);");
9114   verifyFormat("void f(SmallVector<int>) = 0;");
9115   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
9116   verifyFormat("int a = sizeof(int) * b;");
9117   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
9118   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
9119   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
9120   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
9121 
9122   // These are not casts, but at some point were confused with casts.
9123   verifyFormat("virtual void foo(int *) override;");
9124   verifyFormat("virtual void foo(char &) const;");
9125   verifyFormat("virtual void foo(int *a, char *) const;");
9126   verifyFormat("int a = sizeof(int *) + b;");
9127   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
9128   verifyFormat("bool b = f(g<int>) && c;");
9129   verifyFormat("typedef void (*f)(int i) func;");
9130   verifyFormat("void operator++(int) noexcept;");
9131   verifyFormat("void operator++(int &) noexcept;");
9132   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
9133                "&) noexcept;");
9134   verifyFormat(
9135       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
9136   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
9137   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
9138   verifyFormat("void operator delete(nothrow_t &) noexcept;");
9139   verifyFormat("void operator delete(foo &) noexcept;");
9140   verifyFormat("void operator delete(foo) noexcept;");
9141   verifyFormat("void operator delete(int) noexcept;");
9142   verifyFormat("void operator delete(int &) noexcept;");
9143   verifyFormat("void operator delete(int &) volatile noexcept;");
9144   verifyFormat("void operator delete(int &) const");
9145   verifyFormat("void operator delete(int &) = default");
9146   verifyFormat("void operator delete(int &) = delete");
9147   verifyFormat("void operator delete(int &) [[noreturn]]");
9148   verifyFormat("void operator delete(int &) throw();");
9149   verifyFormat("void operator delete(int &) throw(int);");
9150   verifyFormat("auto operator delete(int &) -> int;");
9151   verifyFormat("auto operator delete(int &) override");
9152   verifyFormat("auto operator delete(int &) final");
9153 
9154   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
9155                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9156   // FIXME: The indentation here is not ideal.
9157   verifyFormat(
9158       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9159       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
9160       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
9161 }
9162 
9163 TEST_F(FormatTest, FormatsFunctionTypes) {
9164   verifyFormat("A<bool()> a;");
9165   verifyFormat("A<SomeType()> a;");
9166   verifyFormat("A<void (*)(int, std::string)> a;");
9167   verifyFormat("A<void *(int)>;");
9168   verifyFormat("void *(*a)(int *, SomeType *);");
9169   verifyFormat("int (*func)(void *);");
9170   verifyFormat("void f() { int (*func)(void *); }");
9171   verifyFormat("template <class CallbackClass>\n"
9172                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
9173 
9174   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
9175   verifyGoogleFormat("void* (*a)(int);");
9176   verifyGoogleFormat(
9177       "template <class CallbackClass>\n"
9178       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
9179 
9180   // Other constructs can look somewhat like function types:
9181   verifyFormat("A<sizeof(*x)> a;");
9182   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
9183   verifyFormat("some_var = function(*some_pointer_var)[0];");
9184   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
9185   verifyFormat("int x = f(&h)();");
9186   verifyFormat("returnsFunction(&param1, &param2)(param);");
9187   verifyFormat("std::function<\n"
9188                "    LooooooooooongTemplatedType<\n"
9189                "        SomeType>*(\n"
9190                "        LooooooooooooooooongType type)>\n"
9191                "    function;",
9192                getGoogleStyleWithColumns(40));
9193 }
9194 
9195 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
9196   verifyFormat("A (*foo_)[6];");
9197   verifyFormat("vector<int> (*foo_)[6];");
9198 }
9199 
9200 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
9201   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9202                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9203   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
9204                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9205   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9206                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
9207 
9208   // Different ways of ()-initializiation.
9209   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9210                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
9211   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9212                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
9213   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9214                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
9215   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9216                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
9217 
9218   // Lambdas should not confuse the variable declaration heuristic.
9219   verifyFormat("LooooooooooooooooongType\n"
9220                "    variable(nullptr, [](A *a) {});",
9221                getLLVMStyleWithColumns(40));
9222 }
9223 
9224 TEST_F(FormatTest, BreaksLongDeclarations) {
9225   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
9226                "    AnotherNameForTheLongType;");
9227   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
9228                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9229   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9230                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
9231   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
9232                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
9233   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9234                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9235   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
9236                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9237   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
9238                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9239   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
9240                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9241   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
9242                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9243   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
9244                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9245   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
9246                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9247   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9248                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
9249   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9250                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
9251   FormatStyle Indented = getLLVMStyle();
9252   Indented.IndentWrappedFunctionNames = true;
9253   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9254                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
9255                Indented);
9256   verifyFormat(
9257       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9258       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9259       Indented);
9260   verifyFormat(
9261       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
9262       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9263       Indented);
9264   verifyFormat(
9265       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
9266       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9267       Indented);
9268 
9269   // FIXME: Without the comment, this breaks after "(".
9270   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
9271                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
9272                getGoogleStyle());
9273 
9274   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
9275                "                  int LoooooooooooooooooooongParam2) {}");
9276   verifyFormat(
9277       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
9278       "                                   SourceLocation L, IdentifierIn *II,\n"
9279       "                                   Type *T) {}");
9280   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
9281                "ReallyReaaallyLongFunctionName(\n"
9282                "    const std::string &SomeParameter,\n"
9283                "    const SomeType<string, SomeOtherTemplateParameter>\n"
9284                "        &ReallyReallyLongParameterName,\n"
9285                "    const SomeType<string, SomeOtherTemplateParameter>\n"
9286                "        &AnotherLongParameterName) {}");
9287   verifyFormat("template <typename A>\n"
9288                "SomeLoooooooooooooooooooooongType<\n"
9289                "    typename some_namespace::SomeOtherType<A>::Type>\n"
9290                "Function() {}");
9291 
9292   verifyGoogleFormat(
9293       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
9294       "    aaaaaaaaaaaaaaaaaaaaaaa;");
9295   verifyGoogleFormat(
9296       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
9297       "                                   SourceLocation L) {}");
9298   verifyGoogleFormat(
9299       "some_namespace::LongReturnType\n"
9300       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
9301       "    int first_long_parameter, int second_parameter) {}");
9302 
9303   verifyGoogleFormat("template <typename T>\n"
9304                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
9305                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
9306   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9307                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
9308 
9309   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9310                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9311                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9312   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9313                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
9314                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
9315   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9316                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9317                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
9318                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9319 
9320   verifyFormat("template <typename T> // Templates on own line.\n"
9321                "static int            // Some comment.\n"
9322                "MyFunction(int a);",
9323                getLLVMStyle());
9324 }
9325 
9326 TEST_F(FormatTest, FormatsAccessModifiers) {
9327   FormatStyle Style = getLLVMStyle();
9328   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
9329             FormatStyle::ELBAMS_LogicalBlock);
9330   verifyFormat("struct foo {\n"
9331                "private:\n"
9332                "  void f() {}\n"
9333                "\n"
9334                "private:\n"
9335                "  int i;\n"
9336                "\n"
9337                "protected:\n"
9338                "  int j;\n"
9339                "};\n",
9340                Style);
9341   verifyFormat("struct foo {\n"
9342                "private:\n"
9343                "  void f() {}\n"
9344                "\n"
9345                "private:\n"
9346                "  int i;\n"
9347                "\n"
9348                "protected:\n"
9349                "  int j;\n"
9350                "};\n",
9351                "struct foo {\n"
9352                "private:\n"
9353                "  void f() {}\n"
9354                "private:\n"
9355                "  int i;\n"
9356                "protected:\n"
9357                "  int j;\n"
9358                "};\n",
9359                Style);
9360   verifyFormat("struct foo { /* comment */\n"
9361                "private:\n"
9362                "  int i;\n"
9363                "  // comment\n"
9364                "private:\n"
9365                "  int j;\n"
9366                "};\n",
9367                Style);
9368   verifyFormat("struct foo {\n"
9369                "#ifdef FOO\n"
9370                "#endif\n"
9371                "private:\n"
9372                "  int i;\n"
9373                "#ifdef FOO\n"
9374                "private:\n"
9375                "#endif\n"
9376                "  int j;\n"
9377                "};\n",
9378                Style);
9379   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
9380   verifyFormat("struct foo {\n"
9381                "private:\n"
9382                "  void f() {}\n"
9383                "private:\n"
9384                "  int i;\n"
9385                "protected:\n"
9386                "  int j;\n"
9387                "};\n",
9388                Style);
9389   verifyFormat("struct foo {\n"
9390                "private:\n"
9391                "  void f() {}\n"
9392                "private:\n"
9393                "  int i;\n"
9394                "protected:\n"
9395                "  int j;\n"
9396                "};\n",
9397                "struct foo {\n"
9398                "\n"
9399                "private:\n"
9400                "  void f() {}\n"
9401                "\n"
9402                "private:\n"
9403                "  int i;\n"
9404                "\n"
9405                "protected:\n"
9406                "  int j;\n"
9407                "};\n",
9408                Style);
9409   verifyFormat("struct foo { /* comment */\n"
9410                "private:\n"
9411                "  int i;\n"
9412                "  // comment\n"
9413                "private:\n"
9414                "  int j;\n"
9415                "};\n",
9416                "struct foo { /* comment */\n"
9417                "\n"
9418                "private:\n"
9419                "  int i;\n"
9420                "  // comment\n"
9421                "\n"
9422                "private:\n"
9423                "  int j;\n"
9424                "};\n",
9425                Style);
9426   verifyFormat("struct foo {\n"
9427                "#ifdef FOO\n"
9428                "#endif\n"
9429                "private:\n"
9430                "  int i;\n"
9431                "#ifdef FOO\n"
9432                "private:\n"
9433                "#endif\n"
9434                "  int j;\n"
9435                "};\n",
9436                "struct foo {\n"
9437                "#ifdef FOO\n"
9438                "#endif\n"
9439                "\n"
9440                "private:\n"
9441                "  int i;\n"
9442                "#ifdef FOO\n"
9443                "\n"
9444                "private:\n"
9445                "#endif\n"
9446                "  int j;\n"
9447                "};\n",
9448                Style);
9449   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
9450   verifyFormat("struct foo {\n"
9451                "private:\n"
9452                "  void f() {}\n"
9453                "\n"
9454                "private:\n"
9455                "  int i;\n"
9456                "\n"
9457                "protected:\n"
9458                "  int j;\n"
9459                "};\n",
9460                Style);
9461   verifyFormat("struct foo {\n"
9462                "private:\n"
9463                "  void f() {}\n"
9464                "\n"
9465                "private:\n"
9466                "  int i;\n"
9467                "\n"
9468                "protected:\n"
9469                "  int j;\n"
9470                "};\n",
9471                "struct foo {\n"
9472                "private:\n"
9473                "  void f() {}\n"
9474                "private:\n"
9475                "  int i;\n"
9476                "protected:\n"
9477                "  int j;\n"
9478                "};\n",
9479                Style);
9480   verifyFormat("struct foo { /* comment */\n"
9481                "private:\n"
9482                "  int i;\n"
9483                "  // comment\n"
9484                "\n"
9485                "private:\n"
9486                "  int j;\n"
9487                "};\n",
9488                "struct foo { /* comment */\n"
9489                "private:\n"
9490                "  int i;\n"
9491                "  // comment\n"
9492                "\n"
9493                "private:\n"
9494                "  int j;\n"
9495                "};\n",
9496                Style);
9497   verifyFormat("struct foo {\n"
9498                "#ifdef FOO\n"
9499                "#endif\n"
9500                "\n"
9501                "private:\n"
9502                "  int i;\n"
9503                "#ifdef FOO\n"
9504                "\n"
9505                "private:\n"
9506                "#endif\n"
9507                "  int j;\n"
9508                "};\n",
9509                "struct foo {\n"
9510                "#ifdef FOO\n"
9511                "#endif\n"
9512                "private:\n"
9513                "  int i;\n"
9514                "#ifdef FOO\n"
9515                "private:\n"
9516                "#endif\n"
9517                "  int j;\n"
9518                "};\n",
9519                Style);
9520   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
9521   EXPECT_EQ("struct foo {\n"
9522             "\n"
9523             "private:\n"
9524             "  void f() {}\n"
9525             "\n"
9526             "private:\n"
9527             "  int i;\n"
9528             "\n"
9529             "protected:\n"
9530             "  int j;\n"
9531             "};\n",
9532             format("struct foo {\n"
9533                    "\n"
9534                    "private:\n"
9535                    "  void f() {}\n"
9536                    "\n"
9537                    "private:\n"
9538                    "  int i;\n"
9539                    "\n"
9540                    "protected:\n"
9541                    "  int j;\n"
9542                    "};\n",
9543                    Style));
9544   verifyFormat("struct foo {\n"
9545                "private:\n"
9546                "  void f() {}\n"
9547                "private:\n"
9548                "  int i;\n"
9549                "protected:\n"
9550                "  int j;\n"
9551                "};\n",
9552                Style);
9553   EXPECT_EQ("struct foo { /* comment */\n"
9554             "\n"
9555             "private:\n"
9556             "  int i;\n"
9557             "  // comment\n"
9558             "\n"
9559             "private:\n"
9560             "  int j;\n"
9561             "};\n",
9562             format("struct foo { /* comment */\n"
9563                    "\n"
9564                    "private:\n"
9565                    "  int i;\n"
9566                    "  // comment\n"
9567                    "\n"
9568                    "private:\n"
9569                    "  int j;\n"
9570                    "};\n",
9571                    Style));
9572   verifyFormat("struct foo { /* comment */\n"
9573                "private:\n"
9574                "  int i;\n"
9575                "  // comment\n"
9576                "private:\n"
9577                "  int j;\n"
9578                "};\n",
9579                Style);
9580   EXPECT_EQ("struct foo {\n"
9581             "#ifdef FOO\n"
9582             "#endif\n"
9583             "\n"
9584             "private:\n"
9585             "  int i;\n"
9586             "#ifdef FOO\n"
9587             "\n"
9588             "private:\n"
9589             "#endif\n"
9590             "  int j;\n"
9591             "};\n",
9592             format("struct foo {\n"
9593                    "#ifdef FOO\n"
9594                    "#endif\n"
9595                    "\n"
9596                    "private:\n"
9597                    "  int i;\n"
9598                    "#ifdef FOO\n"
9599                    "\n"
9600                    "private:\n"
9601                    "#endif\n"
9602                    "  int j;\n"
9603                    "};\n",
9604                    Style));
9605   verifyFormat("struct foo {\n"
9606                "#ifdef FOO\n"
9607                "#endif\n"
9608                "private:\n"
9609                "  int i;\n"
9610                "#ifdef FOO\n"
9611                "private:\n"
9612                "#endif\n"
9613                "  int j;\n"
9614                "};\n",
9615                Style);
9616 
9617   FormatStyle NoEmptyLines = getLLVMStyle();
9618   NoEmptyLines.MaxEmptyLinesToKeep = 0;
9619   verifyFormat("struct foo {\n"
9620                "private:\n"
9621                "  void f() {}\n"
9622                "\n"
9623                "private:\n"
9624                "  int i;\n"
9625                "\n"
9626                "public:\n"
9627                "protected:\n"
9628                "  int j;\n"
9629                "};\n",
9630                NoEmptyLines);
9631 
9632   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
9633   verifyFormat("struct foo {\n"
9634                "private:\n"
9635                "  void f() {}\n"
9636                "private:\n"
9637                "  int i;\n"
9638                "public:\n"
9639                "protected:\n"
9640                "  int j;\n"
9641                "};\n",
9642                NoEmptyLines);
9643 
9644   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
9645   verifyFormat("struct foo {\n"
9646                "private:\n"
9647                "  void f() {}\n"
9648                "\n"
9649                "private:\n"
9650                "  int i;\n"
9651                "\n"
9652                "public:\n"
9653                "\n"
9654                "protected:\n"
9655                "  int j;\n"
9656                "};\n",
9657                NoEmptyLines);
9658 }
9659 
9660 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
9661 
9662   FormatStyle Style = getLLVMStyle();
9663   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
9664   verifyFormat("struct foo {\n"
9665                "private:\n"
9666                "  void f() {}\n"
9667                "\n"
9668                "private:\n"
9669                "  int i;\n"
9670                "\n"
9671                "protected:\n"
9672                "  int j;\n"
9673                "};\n",
9674                Style);
9675 
9676   // Check if lines are removed.
9677   verifyFormat("struct foo {\n"
9678                "private:\n"
9679                "  void f() {}\n"
9680                "\n"
9681                "private:\n"
9682                "  int i;\n"
9683                "\n"
9684                "protected:\n"
9685                "  int j;\n"
9686                "};\n",
9687                "struct foo {\n"
9688                "private:\n"
9689                "\n"
9690                "  void f() {}\n"
9691                "\n"
9692                "private:\n"
9693                "\n"
9694                "  int i;\n"
9695                "\n"
9696                "protected:\n"
9697                "\n"
9698                "  int j;\n"
9699                "};\n",
9700                Style);
9701 
9702   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9703   verifyFormat("struct foo {\n"
9704                "private:\n"
9705                "\n"
9706                "  void f() {}\n"
9707                "\n"
9708                "private:\n"
9709                "\n"
9710                "  int i;\n"
9711                "\n"
9712                "protected:\n"
9713                "\n"
9714                "  int j;\n"
9715                "};\n",
9716                Style);
9717 
9718   // Check if lines are added.
9719   verifyFormat("struct foo {\n"
9720                "private:\n"
9721                "\n"
9722                "  void f() {}\n"
9723                "\n"
9724                "private:\n"
9725                "\n"
9726                "  int i;\n"
9727                "\n"
9728                "protected:\n"
9729                "\n"
9730                "  int j;\n"
9731                "};\n",
9732                "struct foo {\n"
9733                "private:\n"
9734                "  void f() {}\n"
9735                "\n"
9736                "private:\n"
9737                "  int i;\n"
9738                "\n"
9739                "protected:\n"
9740                "  int j;\n"
9741                "};\n",
9742                Style);
9743 
9744   // Leave tests rely on the code layout, test::messUp can not be used.
9745   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
9746   Style.MaxEmptyLinesToKeep = 0u;
9747   verifyFormat("struct foo {\n"
9748                "private:\n"
9749                "  void f() {}\n"
9750                "\n"
9751                "private:\n"
9752                "  int i;\n"
9753                "\n"
9754                "protected:\n"
9755                "  int j;\n"
9756                "};\n",
9757                Style);
9758 
9759   // Check if MaxEmptyLinesToKeep is respected.
9760   EXPECT_EQ("struct foo {\n"
9761             "private:\n"
9762             "  void f() {}\n"
9763             "\n"
9764             "private:\n"
9765             "  int i;\n"
9766             "\n"
9767             "protected:\n"
9768             "  int j;\n"
9769             "};\n",
9770             format("struct foo {\n"
9771                    "private:\n"
9772                    "\n\n\n"
9773                    "  void f() {}\n"
9774                    "\n"
9775                    "private:\n"
9776                    "\n\n\n"
9777                    "  int i;\n"
9778                    "\n"
9779                    "protected:\n"
9780                    "\n\n\n"
9781                    "  int j;\n"
9782                    "};\n",
9783                    Style));
9784 
9785   Style.MaxEmptyLinesToKeep = 1u;
9786   EXPECT_EQ("struct foo {\n"
9787             "private:\n"
9788             "\n"
9789             "  void f() {}\n"
9790             "\n"
9791             "private:\n"
9792             "\n"
9793             "  int i;\n"
9794             "\n"
9795             "protected:\n"
9796             "\n"
9797             "  int j;\n"
9798             "};\n",
9799             format("struct foo {\n"
9800                    "private:\n"
9801                    "\n"
9802                    "  void f() {}\n"
9803                    "\n"
9804                    "private:\n"
9805                    "\n"
9806                    "  int i;\n"
9807                    "\n"
9808                    "protected:\n"
9809                    "\n"
9810                    "  int j;\n"
9811                    "};\n",
9812                    Style));
9813   // Check if no lines are kept.
9814   EXPECT_EQ("struct foo {\n"
9815             "private:\n"
9816             "  void f() {}\n"
9817             "\n"
9818             "private:\n"
9819             "  int i;\n"
9820             "\n"
9821             "protected:\n"
9822             "  int j;\n"
9823             "};\n",
9824             format("struct foo {\n"
9825                    "private:\n"
9826                    "  void f() {}\n"
9827                    "\n"
9828                    "private:\n"
9829                    "  int i;\n"
9830                    "\n"
9831                    "protected:\n"
9832                    "  int j;\n"
9833                    "};\n",
9834                    Style));
9835   // Check if MaxEmptyLinesToKeep is respected.
9836   EXPECT_EQ("struct foo {\n"
9837             "private:\n"
9838             "\n"
9839             "  void f() {}\n"
9840             "\n"
9841             "private:\n"
9842             "\n"
9843             "  int i;\n"
9844             "\n"
9845             "protected:\n"
9846             "\n"
9847             "  int j;\n"
9848             "};\n",
9849             format("struct foo {\n"
9850                    "private:\n"
9851                    "\n\n\n"
9852                    "  void f() {}\n"
9853                    "\n"
9854                    "private:\n"
9855                    "\n\n\n"
9856                    "  int i;\n"
9857                    "\n"
9858                    "protected:\n"
9859                    "\n\n\n"
9860                    "  int j;\n"
9861                    "};\n",
9862                    Style));
9863 
9864   Style.MaxEmptyLinesToKeep = 10u;
9865   EXPECT_EQ("struct foo {\n"
9866             "private:\n"
9867             "\n\n\n"
9868             "  void f() {}\n"
9869             "\n"
9870             "private:\n"
9871             "\n\n\n"
9872             "  int i;\n"
9873             "\n"
9874             "protected:\n"
9875             "\n\n\n"
9876             "  int j;\n"
9877             "};\n",
9878             format("struct foo {\n"
9879                    "private:\n"
9880                    "\n\n\n"
9881                    "  void f() {}\n"
9882                    "\n"
9883                    "private:\n"
9884                    "\n\n\n"
9885                    "  int i;\n"
9886                    "\n"
9887                    "protected:\n"
9888                    "\n\n\n"
9889                    "  int j;\n"
9890                    "};\n",
9891                    Style));
9892 
9893   // Test with comments.
9894   Style = getLLVMStyle();
9895   verifyFormat("struct foo {\n"
9896                "private:\n"
9897                "  // comment\n"
9898                "  void f() {}\n"
9899                "\n"
9900                "private: /* comment */\n"
9901                "  int i;\n"
9902                "};\n",
9903                Style);
9904   verifyFormat("struct foo {\n"
9905                "private:\n"
9906                "  // comment\n"
9907                "  void f() {}\n"
9908                "\n"
9909                "private: /* comment */\n"
9910                "  int i;\n"
9911                "};\n",
9912                "struct foo {\n"
9913                "private:\n"
9914                "\n"
9915                "  // comment\n"
9916                "  void f() {}\n"
9917                "\n"
9918                "private: /* comment */\n"
9919                "\n"
9920                "  int i;\n"
9921                "};\n",
9922                Style);
9923 
9924   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9925   verifyFormat("struct foo {\n"
9926                "private:\n"
9927                "\n"
9928                "  // comment\n"
9929                "  void f() {}\n"
9930                "\n"
9931                "private: /* comment */\n"
9932                "\n"
9933                "  int i;\n"
9934                "};\n",
9935                "struct foo {\n"
9936                "private:\n"
9937                "  // comment\n"
9938                "  void f() {}\n"
9939                "\n"
9940                "private: /* comment */\n"
9941                "  int i;\n"
9942                "};\n",
9943                Style);
9944   verifyFormat("struct foo {\n"
9945                "private:\n"
9946                "\n"
9947                "  // comment\n"
9948                "  void f() {}\n"
9949                "\n"
9950                "private: /* comment */\n"
9951                "\n"
9952                "  int i;\n"
9953                "};\n",
9954                Style);
9955 
9956   // Test with preprocessor defines.
9957   Style = getLLVMStyle();
9958   verifyFormat("struct foo {\n"
9959                "private:\n"
9960                "#ifdef FOO\n"
9961                "#endif\n"
9962                "  void f() {}\n"
9963                "};\n",
9964                Style);
9965   verifyFormat("struct foo {\n"
9966                "private:\n"
9967                "#ifdef FOO\n"
9968                "#endif\n"
9969                "  void f() {}\n"
9970                "};\n",
9971                "struct foo {\n"
9972                "private:\n"
9973                "\n"
9974                "#ifdef FOO\n"
9975                "#endif\n"
9976                "  void f() {}\n"
9977                "};\n",
9978                Style);
9979 
9980   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9981   verifyFormat("struct foo {\n"
9982                "private:\n"
9983                "\n"
9984                "#ifdef FOO\n"
9985                "#endif\n"
9986                "  void f() {}\n"
9987                "};\n",
9988                "struct foo {\n"
9989                "private:\n"
9990                "#ifdef FOO\n"
9991                "#endif\n"
9992                "  void f() {}\n"
9993                "};\n",
9994                Style);
9995   verifyFormat("struct foo {\n"
9996                "private:\n"
9997                "\n"
9998                "#ifdef FOO\n"
9999                "#endif\n"
10000                "  void f() {}\n"
10001                "};\n",
10002                Style);
10003 }
10004 
10005 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
10006   // Combined tests of EmptyLineAfterAccessModifier and
10007   // EmptyLineBeforeAccessModifier.
10008   FormatStyle Style = getLLVMStyle();
10009   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10010   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10011   verifyFormat("struct foo {\n"
10012                "private:\n"
10013                "\n"
10014                "protected:\n"
10015                "};\n",
10016                Style);
10017 
10018   Style.MaxEmptyLinesToKeep = 10u;
10019   // Both remove all new lines.
10020   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10021   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10022   verifyFormat("struct foo {\n"
10023                "private:\n"
10024                "protected:\n"
10025                "};\n",
10026                "struct foo {\n"
10027                "private:\n"
10028                "\n\n\n"
10029                "protected:\n"
10030                "};\n",
10031                Style);
10032 
10033   // Leave tests rely on the code layout, test::messUp can not be used.
10034   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10035   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10036   Style.MaxEmptyLinesToKeep = 10u;
10037   EXPECT_EQ("struct foo {\n"
10038             "private:\n"
10039             "\n\n\n"
10040             "protected:\n"
10041             "};\n",
10042             format("struct foo {\n"
10043                    "private:\n"
10044                    "\n\n\n"
10045                    "protected:\n"
10046                    "};\n",
10047                    Style));
10048   Style.MaxEmptyLinesToKeep = 3u;
10049   EXPECT_EQ("struct foo {\n"
10050             "private:\n"
10051             "\n\n\n"
10052             "protected:\n"
10053             "};\n",
10054             format("struct foo {\n"
10055                    "private:\n"
10056                    "\n\n\n"
10057                    "protected:\n"
10058                    "};\n",
10059                    Style));
10060   Style.MaxEmptyLinesToKeep = 1u;
10061   EXPECT_EQ("struct foo {\n"
10062             "private:\n"
10063             "\n\n\n"
10064             "protected:\n"
10065             "};\n",
10066             format("struct foo {\n"
10067                    "private:\n"
10068                    "\n\n\n"
10069                    "protected:\n"
10070                    "};\n",
10071                    Style)); // Based on new lines in original document and not
10072                             // on the setting.
10073 
10074   Style.MaxEmptyLinesToKeep = 10u;
10075   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10076   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10077   // Newlines are kept if they are greater than zero,
10078   // test::messUp removes all new lines which changes the logic
10079   EXPECT_EQ("struct foo {\n"
10080             "private:\n"
10081             "\n\n\n"
10082             "protected:\n"
10083             "};\n",
10084             format("struct foo {\n"
10085                    "private:\n"
10086                    "\n\n\n"
10087                    "protected:\n"
10088                    "};\n",
10089                    Style));
10090 
10091   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10092   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10093   // test::messUp removes all new lines which changes the logic
10094   EXPECT_EQ("struct foo {\n"
10095             "private:\n"
10096             "\n\n\n"
10097             "protected:\n"
10098             "};\n",
10099             format("struct foo {\n"
10100                    "private:\n"
10101                    "\n\n\n"
10102                    "protected:\n"
10103                    "};\n",
10104                    Style));
10105 
10106   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10107   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10108   EXPECT_EQ("struct foo {\n"
10109             "private:\n"
10110             "\n\n\n"
10111             "protected:\n"
10112             "};\n",
10113             format("struct foo {\n"
10114                    "private:\n"
10115                    "\n\n\n"
10116                    "protected:\n"
10117                    "};\n",
10118                    Style)); // test::messUp removes all new lines which changes
10119                             // the logic.
10120 
10121   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10122   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10123   verifyFormat("struct foo {\n"
10124                "private:\n"
10125                "protected:\n"
10126                "};\n",
10127                "struct foo {\n"
10128                "private:\n"
10129                "\n\n\n"
10130                "protected:\n"
10131                "};\n",
10132                Style);
10133 
10134   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10135   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10136   EXPECT_EQ("struct foo {\n"
10137             "private:\n"
10138             "\n\n\n"
10139             "protected:\n"
10140             "};\n",
10141             format("struct foo {\n"
10142                    "private:\n"
10143                    "\n\n\n"
10144                    "protected:\n"
10145                    "};\n",
10146                    Style)); // test::messUp removes all new lines which changes
10147                             // the logic.
10148 
10149   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10150   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10151   verifyFormat("struct foo {\n"
10152                "private:\n"
10153                "protected:\n"
10154                "};\n",
10155                "struct foo {\n"
10156                "private:\n"
10157                "\n\n\n"
10158                "protected:\n"
10159                "};\n",
10160                Style);
10161 
10162   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10163   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10164   verifyFormat("struct foo {\n"
10165                "private:\n"
10166                "protected:\n"
10167                "};\n",
10168                "struct foo {\n"
10169                "private:\n"
10170                "\n\n\n"
10171                "protected:\n"
10172                "};\n",
10173                Style);
10174 
10175   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10176   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10177   verifyFormat("struct foo {\n"
10178                "private:\n"
10179                "protected:\n"
10180                "};\n",
10181                "struct foo {\n"
10182                "private:\n"
10183                "\n\n\n"
10184                "protected:\n"
10185                "};\n",
10186                Style);
10187 
10188   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10189   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10190   verifyFormat("struct foo {\n"
10191                "private:\n"
10192                "protected:\n"
10193                "};\n",
10194                "struct foo {\n"
10195                "private:\n"
10196                "\n\n\n"
10197                "protected:\n"
10198                "};\n",
10199                Style);
10200 }
10201 
10202 TEST_F(FormatTest, FormatsArrays) {
10203   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10204                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
10205   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
10206                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
10207   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
10208                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
10209   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10210                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
10211   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10212                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
10213   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10214                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10215                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
10216   verifyFormat(
10217       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
10218       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10219       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
10220   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
10221                "    .aaaaaaaaaaaaaaaaaaaaaa();");
10222 
10223   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
10224                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
10225   verifyFormat(
10226       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
10227       "                                  .aaaaaaa[0]\n"
10228       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
10229   verifyFormat("a[::b::c];");
10230 
10231   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
10232 
10233   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
10234   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
10235 }
10236 
10237 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
10238   verifyFormat("(a)->b();");
10239   verifyFormat("--a;");
10240 }
10241 
10242 TEST_F(FormatTest, HandlesIncludeDirectives) {
10243   verifyFormat("#include <string>\n"
10244                "#include <a/b/c.h>\n"
10245                "#include \"a/b/string\"\n"
10246                "#include \"string.h\"\n"
10247                "#include \"string.h\"\n"
10248                "#include <a-a>\n"
10249                "#include < path with space >\n"
10250                "#include_next <test.h>"
10251                "#include \"abc.h\" // this is included for ABC\n"
10252                "#include \"some long include\" // with a comment\n"
10253                "#include \"some very long include path\"\n"
10254                "#include <some/very/long/include/path>\n",
10255                getLLVMStyleWithColumns(35));
10256   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
10257   EXPECT_EQ("#include <a>", format("#include<a>"));
10258 
10259   verifyFormat("#import <string>");
10260   verifyFormat("#import <a/b/c.h>");
10261   verifyFormat("#import \"a/b/string\"");
10262   verifyFormat("#import \"string.h\"");
10263   verifyFormat("#import \"string.h\"");
10264   verifyFormat("#if __has_include(<strstream>)\n"
10265                "#include <strstream>\n"
10266                "#endif");
10267 
10268   verifyFormat("#define MY_IMPORT <a/b>");
10269 
10270   verifyFormat("#if __has_include(<a/b>)");
10271   verifyFormat("#if __has_include_next(<a/b>)");
10272   verifyFormat("#define F __has_include(<a/b>)");
10273   verifyFormat("#define F __has_include_next(<a/b>)");
10274 
10275   // Protocol buffer definition or missing "#".
10276   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
10277                getLLVMStyleWithColumns(30));
10278 
10279   FormatStyle Style = getLLVMStyle();
10280   Style.AlwaysBreakBeforeMultilineStrings = true;
10281   Style.ColumnLimit = 0;
10282   verifyFormat("#import \"abc.h\"", Style);
10283 
10284   // But 'import' might also be a regular C++ namespace.
10285   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10286                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10287 }
10288 
10289 //===----------------------------------------------------------------------===//
10290 // Error recovery tests.
10291 //===----------------------------------------------------------------------===//
10292 
10293 TEST_F(FormatTest, IncompleteParameterLists) {
10294   FormatStyle NoBinPacking = getLLVMStyle();
10295   NoBinPacking.BinPackParameters = false;
10296   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
10297                "                        double *min_x,\n"
10298                "                        double *max_x,\n"
10299                "                        double *min_y,\n"
10300                "                        double *max_y,\n"
10301                "                        double *min_z,\n"
10302                "                        double *max_z, ) {}",
10303                NoBinPacking);
10304 }
10305 
10306 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
10307   verifyFormat("void f() { return; }\n42");
10308   verifyFormat("void f() {\n"
10309                "  if (0)\n"
10310                "    return;\n"
10311                "}\n"
10312                "42");
10313   verifyFormat("void f() { return }\n42");
10314   verifyFormat("void f() {\n"
10315                "  if (0)\n"
10316                "    return\n"
10317                "}\n"
10318                "42");
10319 }
10320 
10321 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
10322   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
10323   EXPECT_EQ("void f() {\n"
10324             "  if (a)\n"
10325             "    return\n"
10326             "}",
10327             format("void  f  (  )  {  if  ( a )  return  }"));
10328   EXPECT_EQ("namespace N {\n"
10329             "void f()\n"
10330             "}",
10331             format("namespace  N  {  void f()  }"));
10332   EXPECT_EQ("namespace N {\n"
10333             "void f() {}\n"
10334             "void g()\n"
10335             "} // namespace N",
10336             format("namespace N  { void f( ) { } void g( ) }"));
10337 }
10338 
10339 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
10340   verifyFormat("int aaaaaaaa =\n"
10341                "    // Overlylongcomment\n"
10342                "    b;",
10343                getLLVMStyleWithColumns(20));
10344   verifyFormat("function(\n"
10345                "    ShortArgument,\n"
10346                "    LoooooooooooongArgument);\n",
10347                getLLVMStyleWithColumns(20));
10348 }
10349 
10350 TEST_F(FormatTest, IncorrectAccessSpecifier) {
10351   verifyFormat("public:");
10352   verifyFormat("class A {\n"
10353                "public\n"
10354                "  void f() {}\n"
10355                "};");
10356   verifyFormat("public\n"
10357                "int qwerty;");
10358   verifyFormat("public\n"
10359                "B {}");
10360   verifyFormat("public\n"
10361                "{}");
10362   verifyFormat("public\n"
10363                "B { int x; }");
10364 }
10365 
10366 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
10367   verifyFormat("{");
10368   verifyFormat("#})");
10369   verifyNoCrash("(/**/[:!] ?[).");
10370 }
10371 
10372 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
10373   // Found by oss-fuzz:
10374   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
10375   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
10376   Style.ColumnLimit = 60;
10377   verifyNoCrash(
10378       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
10379       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
10380       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
10381       Style);
10382 }
10383 
10384 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
10385   verifyFormat("do {\n}");
10386   verifyFormat("do {\n}\n"
10387                "f();");
10388   verifyFormat("do {\n}\n"
10389                "wheeee(fun);");
10390   verifyFormat("do {\n"
10391                "  f();\n"
10392                "}");
10393 }
10394 
10395 TEST_F(FormatTest, IncorrectCodeMissingParens) {
10396   verifyFormat("if {\n  foo;\n  foo();\n}");
10397   verifyFormat("switch {\n  foo;\n  foo();\n}");
10398   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
10399   verifyFormat("while {\n  foo;\n  foo();\n}");
10400   verifyFormat("do {\n  foo;\n  foo();\n} while;");
10401 }
10402 
10403 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
10404   verifyIncompleteFormat("namespace {\n"
10405                          "class Foo { Foo (\n"
10406                          "};\n"
10407                          "} // namespace");
10408 }
10409 
10410 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
10411   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
10412   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
10413   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
10414   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
10415 
10416   EXPECT_EQ("{\n"
10417             "  {\n"
10418             "    breakme(\n"
10419             "        qwe);\n"
10420             "  }\n",
10421             format("{\n"
10422                    "    {\n"
10423                    " breakme(qwe);\n"
10424                    "}\n",
10425                    getLLVMStyleWithColumns(10)));
10426 }
10427 
10428 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
10429   verifyFormat("int x = {\n"
10430                "    avariable,\n"
10431                "    b(alongervariable)};",
10432                getLLVMStyleWithColumns(25));
10433 }
10434 
10435 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
10436   verifyFormat("return (a)(b){1, 2, 3};");
10437 }
10438 
10439 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
10440   verifyFormat("vector<int> x{1, 2, 3, 4};");
10441   verifyFormat("vector<int> x{\n"
10442                "    1,\n"
10443                "    2,\n"
10444                "    3,\n"
10445                "    4,\n"
10446                "};");
10447   verifyFormat("vector<T> x{{}, {}, {}, {}};");
10448   verifyFormat("f({1, 2});");
10449   verifyFormat("auto v = Foo{-1};");
10450   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
10451   verifyFormat("Class::Class : member{1, 2, 3} {}");
10452   verifyFormat("new vector<int>{1, 2, 3};");
10453   verifyFormat("new int[3]{1, 2, 3};");
10454   verifyFormat("new int{1};");
10455   verifyFormat("return {arg1, arg2};");
10456   verifyFormat("return {arg1, SomeType{parameter}};");
10457   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
10458   verifyFormat("new T{arg1, arg2};");
10459   verifyFormat("f(MyMap[{composite, key}]);");
10460   verifyFormat("class Class {\n"
10461                "  T member = {arg1, arg2};\n"
10462                "};");
10463   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
10464   verifyFormat("const struct A a = {.a = 1, .b = 2};");
10465   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
10466   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
10467   verifyFormat("int a = std::is_integral<int>{} + 0;");
10468 
10469   verifyFormat("int foo(int i) { return fo1{}(i); }");
10470   verifyFormat("int foo(int i) { return fo1{}(i); }");
10471   verifyFormat("auto i = decltype(x){};");
10472   verifyFormat("auto i = typeof(x){};");
10473   verifyFormat("auto i = _Atomic(x){};");
10474   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
10475   verifyFormat("Node n{1, Node{1000}, //\n"
10476                "       2};");
10477   verifyFormat("Aaaa aaaaaaa{\n"
10478                "    {\n"
10479                "        aaaa,\n"
10480                "    },\n"
10481                "};");
10482   verifyFormat("class C : public D {\n"
10483                "  SomeClass SC{2};\n"
10484                "};");
10485   verifyFormat("class C : public A {\n"
10486                "  class D : public B {\n"
10487                "    void f() { int i{2}; }\n"
10488                "  };\n"
10489                "};");
10490   verifyFormat("#define A {a, a},");
10491 
10492   // Avoid breaking between equal sign and opening brace
10493   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
10494   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
10495   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
10496                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
10497                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
10498                "     {\"ccccccccccccccccccccc\", 2}};",
10499                AvoidBreakingFirstArgument);
10500 
10501   // Binpacking only if there is no trailing comma
10502   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
10503                "                      cccccccccc, dddddddddd};",
10504                getLLVMStyleWithColumns(50));
10505   verifyFormat("const Aaaaaa aaaaa = {\n"
10506                "    aaaaaaaaaaa,\n"
10507                "    bbbbbbbbbbb,\n"
10508                "    ccccccccccc,\n"
10509                "    ddddddddddd,\n"
10510                "};",
10511                getLLVMStyleWithColumns(50));
10512 
10513   // Cases where distinguising braced lists and blocks is hard.
10514   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
10515   verifyFormat("void f() {\n"
10516                "  return; // comment\n"
10517                "}\n"
10518                "SomeType t;");
10519   verifyFormat("void f() {\n"
10520                "  if (a) {\n"
10521                "    f();\n"
10522                "  }\n"
10523                "}\n"
10524                "SomeType t;");
10525 
10526   // In combination with BinPackArguments = false.
10527   FormatStyle NoBinPacking = getLLVMStyle();
10528   NoBinPacking.BinPackArguments = false;
10529   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
10530                "                      bbbbb,\n"
10531                "                      ccccc,\n"
10532                "                      ddddd,\n"
10533                "                      eeeee,\n"
10534                "                      ffffff,\n"
10535                "                      ggggg,\n"
10536                "                      hhhhhh,\n"
10537                "                      iiiiii,\n"
10538                "                      jjjjjj,\n"
10539                "                      kkkkkk};",
10540                NoBinPacking);
10541   verifyFormat("const Aaaaaa aaaaa = {\n"
10542                "    aaaaa,\n"
10543                "    bbbbb,\n"
10544                "    ccccc,\n"
10545                "    ddddd,\n"
10546                "    eeeee,\n"
10547                "    ffffff,\n"
10548                "    ggggg,\n"
10549                "    hhhhhh,\n"
10550                "    iiiiii,\n"
10551                "    jjjjjj,\n"
10552                "    kkkkkk,\n"
10553                "};",
10554                NoBinPacking);
10555   verifyFormat(
10556       "const Aaaaaa aaaaa = {\n"
10557       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
10558       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
10559       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
10560       "};",
10561       NoBinPacking);
10562 
10563   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10564   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
10565             "    CDDDP83848_BMCR_REGISTER,\n"
10566             "    CDDDP83848_BMSR_REGISTER,\n"
10567             "    CDDDP83848_RBR_REGISTER};",
10568             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
10569                    "                                CDDDP83848_BMSR_REGISTER,\n"
10570                    "                                CDDDP83848_RBR_REGISTER};",
10571                    NoBinPacking));
10572 
10573   // FIXME: The alignment of these trailing comments might be bad. Then again,
10574   // this might be utterly useless in real code.
10575   verifyFormat("Constructor::Constructor()\n"
10576                "    : some_value{         //\n"
10577                "                 aaaaaaa, //\n"
10578                "                 bbbbbbb} {}");
10579 
10580   // In braced lists, the first comment is always assumed to belong to the
10581   // first element. Thus, it can be moved to the next or previous line as
10582   // appropriate.
10583   EXPECT_EQ("function({// First element:\n"
10584             "          1,\n"
10585             "          // Second element:\n"
10586             "          2});",
10587             format("function({\n"
10588                    "    // First element:\n"
10589                    "    1,\n"
10590                    "    // Second element:\n"
10591                    "    2});"));
10592   EXPECT_EQ("std::vector<int> MyNumbers{\n"
10593             "    // First element:\n"
10594             "    1,\n"
10595             "    // Second element:\n"
10596             "    2};",
10597             format("std::vector<int> MyNumbers{// First element:\n"
10598                    "                           1,\n"
10599                    "                           // Second element:\n"
10600                    "                           2};",
10601                    getLLVMStyleWithColumns(30)));
10602   // A trailing comma should still lead to an enforced line break and no
10603   // binpacking.
10604   EXPECT_EQ("vector<int> SomeVector = {\n"
10605             "    // aaa\n"
10606             "    1,\n"
10607             "    2,\n"
10608             "};",
10609             format("vector<int> SomeVector = { // aaa\n"
10610                    "    1, 2, };"));
10611 
10612   // C++11 brace initializer list l-braces should not be treated any differently
10613   // when breaking before lambda bodies is enabled
10614   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
10615   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
10616   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
10617   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
10618   verifyFormat(
10619       "std::runtime_error{\n"
10620       "    \"Long string which will force a break onto the next line...\"};",
10621       BreakBeforeLambdaBody);
10622 
10623   FormatStyle ExtraSpaces = getLLVMStyle();
10624   ExtraSpaces.Cpp11BracedListStyle = false;
10625   ExtraSpaces.ColumnLimit = 75;
10626   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
10627   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
10628   verifyFormat("f({ 1, 2 });", ExtraSpaces);
10629   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
10630   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
10631   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
10632   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
10633   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
10634   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
10635   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
10636   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
10637   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
10638   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
10639   verifyFormat("class Class {\n"
10640                "  T member = { arg1, arg2 };\n"
10641                "};",
10642                ExtraSpaces);
10643   verifyFormat(
10644       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10645       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
10646       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
10647       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
10648       ExtraSpaces);
10649   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
10650   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
10651                ExtraSpaces);
10652   verifyFormat(
10653       "someFunction(OtherParam,\n"
10654       "             BracedList{ // comment 1 (Forcing interesting break)\n"
10655       "                         param1, param2,\n"
10656       "                         // comment 2\n"
10657       "                         param3, param4 });",
10658       ExtraSpaces);
10659   verifyFormat(
10660       "std::this_thread::sleep_for(\n"
10661       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
10662       ExtraSpaces);
10663   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
10664                "    aaaaaaa,\n"
10665                "    aaaaaaaaaa,\n"
10666                "    aaaaa,\n"
10667                "    aaaaaaaaaaaaaaa,\n"
10668                "    aaa,\n"
10669                "    aaaaaaaaaa,\n"
10670                "    a,\n"
10671                "    aaaaaaaaaaaaaaaaaaaaa,\n"
10672                "    aaaaaaaaaaaa,\n"
10673                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
10674                "    aaaaaaa,\n"
10675                "    a};");
10676   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
10677   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
10678   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
10679 
10680   // Avoid breaking between initializer/equal sign and opening brace
10681   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
10682   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
10683                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
10684                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
10685                "  { \"ccccccccccccccccccccc\", 2 }\n"
10686                "};",
10687                ExtraSpaces);
10688   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
10689                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
10690                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
10691                "  { \"ccccccccccccccccccccc\", 2 }\n"
10692                "};",
10693                ExtraSpaces);
10694 
10695   FormatStyle SpaceBeforeBrace = getLLVMStyle();
10696   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
10697   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
10698   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
10699 
10700   FormatStyle SpaceBetweenBraces = getLLVMStyle();
10701   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
10702   SpaceBetweenBraces.SpacesInParentheses = true;
10703   SpaceBetweenBraces.SpacesInSquareBrackets = true;
10704   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
10705   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
10706   verifyFormat("vector< int > x{ // comment 1\n"
10707                "                 1, 2, 3, 4 };",
10708                SpaceBetweenBraces);
10709   SpaceBetweenBraces.ColumnLimit = 20;
10710   EXPECT_EQ("vector< int > x{\n"
10711             "    1, 2, 3, 4 };",
10712             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
10713   SpaceBetweenBraces.ColumnLimit = 24;
10714   EXPECT_EQ("vector< int > x{ 1, 2,\n"
10715             "                 3, 4 };",
10716             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
10717   EXPECT_EQ("vector< int > x{\n"
10718             "    1,\n"
10719             "    2,\n"
10720             "    3,\n"
10721             "    4,\n"
10722             "};",
10723             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
10724   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
10725   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
10726   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
10727 }
10728 
10729 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
10730   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10731                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10732                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10733                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10734                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10735                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
10736   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
10737                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10738                "                 1, 22, 333, 4444, 55555, //\n"
10739                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10740                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
10741   verifyFormat(
10742       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
10743       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
10744       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
10745       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
10746       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
10747       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
10748       "                 7777777};");
10749   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
10750                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
10751                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
10752   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
10753                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
10754                "    // Separating comment.\n"
10755                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
10756   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
10757                "    // Leading comment\n"
10758                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
10759                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
10760   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
10761                "                 1, 1, 1, 1};",
10762                getLLVMStyleWithColumns(39));
10763   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
10764                "                 1, 1, 1, 1};",
10765                getLLVMStyleWithColumns(38));
10766   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
10767                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
10768                getLLVMStyleWithColumns(43));
10769   verifyFormat(
10770       "static unsigned SomeValues[10][3] = {\n"
10771       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
10772       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
10773   verifyFormat("static auto fields = new vector<string>{\n"
10774                "    \"aaaaaaaaaaaaa\",\n"
10775                "    \"aaaaaaaaaaaaa\",\n"
10776                "    \"aaaaaaaaaaaa\",\n"
10777                "    \"aaaaaaaaaaaaaa\",\n"
10778                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
10779                "    \"aaaaaaaaaaaa\",\n"
10780                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
10781                "};");
10782   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
10783   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
10784                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
10785                "                 3, cccccccccccccccccccccc};",
10786                getLLVMStyleWithColumns(60));
10787 
10788   // Trailing commas.
10789   verifyFormat("vector<int> x = {\n"
10790                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
10791                "};",
10792                getLLVMStyleWithColumns(39));
10793   verifyFormat("vector<int> x = {\n"
10794                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
10795                "};",
10796                getLLVMStyleWithColumns(39));
10797   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
10798                "                 1, 1, 1, 1,\n"
10799                "                 /**/ /**/};",
10800                getLLVMStyleWithColumns(39));
10801 
10802   // Trailing comment in the first line.
10803   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
10804                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
10805                "    111111111,  222222222,  3333333333,  444444444,  //\n"
10806                "    11111111,   22222222,   333333333,   44444444};");
10807   // Trailing comment in the last line.
10808   verifyFormat("int aaaaa[] = {\n"
10809                "    1, 2, 3, // comment\n"
10810                "    4, 5, 6  // comment\n"
10811                "};");
10812 
10813   // With nested lists, we should either format one item per line or all nested
10814   // lists one on line.
10815   // FIXME: For some nested lists, we can do better.
10816   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
10817                "        {aaaaaaaaaaaaaaaaaaa},\n"
10818                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
10819                "        {aaaaaaaaaaaaaaaaa}};",
10820                getLLVMStyleWithColumns(60));
10821   verifyFormat(
10822       "SomeStruct my_struct_array = {\n"
10823       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
10824       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
10825       "    {aaa, aaa},\n"
10826       "    {aaa, aaa},\n"
10827       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
10828       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
10829       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
10830 
10831   // No column layout should be used here.
10832   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
10833                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
10834 
10835   verifyNoCrash("a<,");
10836 
10837   // No braced initializer here.
10838   verifyFormat("void f() {\n"
10839                "  struct Dummy {};\n"
10840                "  f(v);\n"
10841                "}");
10842 
10843   // Long lists should be formatted in columns even if they are nested.
10844   verifyFormat(
10845       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10846       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10847       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10848       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10849       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10850       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
10851 
10852   // Allow "single-column" layout even if that violates the column limit. There
10853   // isn't going to be a better way.
10854   verifyFormat("std::vector<int> a = {\n"
10855                "    aaaaaaaa,\n"
10856                "    aaaaaaaa,\n"
10857                "    aaaaaaaa,\n"
10858                "    aaaaaaaa,\n"
10859                "    aaaaaaaaaa,\n"
10860                "    aaaaaaaa,\n"
10861                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
10862                getLLVMStyleWithColumns(30));
10863   verifyFormat("vector<int> aaaa = {\n"
10864                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10865                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10866                "    aaaaaa.aaaaaaa,\n"
10867                "    aaaaaa.aaaaaaa,\n"
10868                "    aaaaaa.aaaaaaa,\n"
10869                "    aaaaaa.aaaaaaa,\n"
10870                "};");
10871 
10872   // Don't create hanging lists.
10873   verifyFormat("someFunction(Param, {List1, List2,\n"
10874                "                     List3});",
10875                getLLVMStyleWithColumns(35));
10876   verifyFormat("someFunction(Param, Param,\n"
10877                "             {List1, List2,\n"
10878                "              List3});",
10879                getLLVMStyleWithColumns(35));
10880   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
10881                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
10882 }
10883 
10884 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
10885   FormatStyle DoNotMerge = getLLVMStyle();
10886   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10887 
10888   verifyFormat("void f() { return 42; }");
10889   verifyFormat("void f() {\n"
10890                "  return 42;\n"
10891                "}",
10892                DoNotMerge);
10893   verifyFormat("void f() {\n"
10894                "  // Comment\n"
10895                "}");
10896   verifyFormat("{\n"
10897                "#error {\n"
10898                "  int a;\n"
10899                "}");
10900   verifyFormat("{\n"
10901                "  int a;\n"
10902                "#error {\n"
10903                "}");
10904   verifyFormat("void f() {} // comment");
10905   verifyFormat("void f() { int a; } // comment");
10906   verifyFormat("void f() {\n"
10907                "} // comment",
10908                DoNotMerge);
10909   verifyFormat("void f() {\n"
10910                "  int a;\n"
10911                "} // comment",
10912                DoNotMerge);
10913   verifyFormat("void f() {\n"
10914                "} // comment",
10915                getLLVMStyleWithColumns(15));
10916 
10917   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
10918   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
10919 
10920   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
10921   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
10922   verifyFormat("class C {\n"
10923                "  C()\n"
10924                "      : iiiiiiii(nullptr),\n"
10925                "        kkkkkkk(nullptr),\n"
10926                "        mmmmmmm(nullptr),\n"
10927                "        nnnnnnn(nullptr) {}\n"
10928                "};",
10929                getGoogleStyle());
10930 
10931   FormatStyle NoColumnLimit = getLLVMStyle();
10932   NoColumnLimit.ColumnLimit = 0;
10933   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
10934   EXPECT_EQ("class C {\n"
10935             "  A() : b(0) {}\n"
10936             "};",
10937             format("class C{A():b(0){}};", NoColumnLimit));
10938   EXPECT_EQ("A()\n"
10939             "    : b(0) {\n"
10940             "}",
10941             format("A()\n:b(0)\n{\n}", NoColumnLimit));
10942 
10943   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
10944   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
10945       FormatStyle::SFS_None;
10946   EXPECT_EQ("A()\n"
10947             "    : b(0) {\n"
10948             "}",
10949             format("A():b(0){}", DoNotMergeNoColumnLimit));
10950   EXPECT_EQ("A()\n"
10951             "    : b(0) {\n"
10952             "}",
10953             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
10954 
10955   verifyFormat("#define A          \\\n"
10956                "  void f() {       \\\n"
10957                "    int i;         \\\n"
10958                "  }",
10959                getLLVMStyleWithColumns(20));
10960   verifyFormat("#define A           \\\n"
10961                "  void f() { int i; }",
10962                getLLVMStyleWithColumns(21));
10963   verifyFormat("#define A            \\\n"
10964                "  void f() {         \\\n"
10965                "    int i;           \\\n"
10966                "  }                  \\\n"
10967                "  int j;",
10968                getLLVMStyleWithColumns(22));
10969   verifyFormat("#define A             \\\n"
10970                "  void f() { int i; } \\\n"
10971                "  int j;",
10972                getLLVMStyleWithColumns(23));
10973 }
10974 
10975 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
10976   FormatStyle MergeEmptyOnly = getLLVMStyle();
10977   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
10978   verifyFormat("class C {\n"
10979                "  int f() {}\n"
10980                "};",
10981                MergeEmptyOnly);
10982   verifyFormat("class C {\n"
10983                "  int f() {\n"
10984                "    return 42;\n"
10985                "  }\n"
10986                "};",
10987                MergeEmptyOnly);
10988   verifyFormat("int f() {}", MergeEmptyOnly);
10989   verifyFormat("int f() {\n"
10990                "  return 42;\n"
10991                "}",
10992                MergeEmptyOnly);
10993 
10994   // Also verify behavior when BraceWrapping.AfterFunction = true
10995   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
10996   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
10997   verifyFormat("int f() {}", MergeEmptyOnly);
10998   verifyFormat("class C {\n"
10999                "  int f() {}\n"
11000                "};",
11001                MergeEmptyOnly);
11002 }
11003 
11004 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
11005   FormatStyle MergeInlineOnly = getLLVMStyle();
11006   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11007   verifyFormat("class C {\n"
11008                "  int f() { return 42; }\n"
11009                "};",
11010                MergeInlineOnly);
11011   verifyFormat("int f() {\n"
11012                "  return 42;\n"
11013                "}",
11014                MergeInlineOnly);
11015 
11016   // SFS_Inline implies SFS_Empty
11017   verifyFormat("class C {\n"
11018                "  int f() {}\n"
11019                "};",
11020                MergeInlineOnly);
11021   verifyFormat("int f() {}", MergeInlineOnly);
11022 
11023   // Also verify behavior when BraceWrapping.AfterFunction = true
11024   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11025   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11026   verifyFormat("class C {\n"
11027                "  int f() { return 42; }\n"
11028                "};",
11029                MergeInlineOnly);
11030   verifyFormat("int f()\n"
11031                "{\n"
11032                "  return 42;\n"
11033                "}",
11034                MergeInlineOnly);
11035 
11036   // SFS_Inline implies SFS_Empty
11037   verifyFormat("int f() {}", MergeInlineOnly);
11038   verifyFormat("class C {\n"
11039                "  int f() {}\n"
11040                "};",
11041                MergeInlineOnly);
11042 }
11043 
11044 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
11045   FormatStyle MergeInlineOnly = getLLVMStyle();
11046   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
11047       FormatStyle::SFS_InlineOnly;
11048   verifyFormat("class C {\n"
11049                "  int f() { return 42; }\n"
11050                "};",
11051                MergeInlineOnly);
11052   verifyFormat("int f() {\n"
11053                "  return 42;\n"
11054                "}",
11055                MergeInlineOnly);
11056 
11057   // SFS_InlineOnly does not imply SFS_Empty
11058   verifyFormat("class C {\n"
11059                "  int f() {}\n"
11060                "};",
11061                MergeInlineOnly);
11062   verifyFormat("int f() {\n"
11063                "}",
11064                MergeInlineOnly);
11065 
11066   // Also verify behavior when BraceWrapping.AfterFunction = true
11067   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11068   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11069   verifyFormat("class C {\n"
11070                "  int f() { return 42; }\n"
11071                "};",
11072                MergeInlineOnly);
11073   verifyFormat("int f()\n"
11074                "{\n"
11075                "  return 42;\n"
11076                "}",
11077                MergeInlineOnly);
11078 
11079   // SFS_InlineOnly does not imply SFS_Empty
11080   verifyFormat("int f()\n"
11081                "{\n"
11082                "}",
11083                MergeInlineOnly);
11084   verifyFormat("class C {\n"
11085                "  int f() {}\n"
11086                "};",
11087                MergeInlineOnly);
11088 }
11089 
11090 TEST_F(FormatTest, SplitEmptyFunction) {
11091   FormatStyle Style = getLLVMStyle();
11092   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11093   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11094   Style.BraceWrapping.AfterFunction = true;
11095   Style.BraceWrapping.SplitEmptyFunction = false;
11096   Style.ColumnLimit = 40;
11097 
11098   verifyFormat("int f()\n"
11099                "{}",
11100                Style);
11101   verifyFormat("int f()\n"
11102                "{\n"
11103                "  return 42;\n"
11104                "}",
11105                Style);
11106   verifyFormat("int f()\n"
11107                "{\n"
11108                "  // some comment\n"
11109                "}",
11110                Style);
11111 
11112   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11113   verifyFormat("int f() {}", Style);
11114   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11115                "{}",
11116                Style);
11117   verifyFormat("int f()\n"
11118                "{\n"
11119                "  return 0;\n"
11120                "}",
11121                Style);
11122 
11123   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11124   verifyFormat("class Foo {\n"
11125                "  int f() {}\n"
11126                "};\n",
11127                Style);
11128   verifyFormat("class Foo {\n"
11129                "  int f() { return 0; }\n"
11130                "};\n",
11131                Style);
11132   verifyFormat("class Foo {\n"
11133                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11134                "  {}\n"
11135                "};\n",
11136                Style);
11137   verifyFormat("class Foo {\n"
11138                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11139                "  {\n"
11140                "    return 0;\n"
11141                "  }\n"
11142                "};\n",
11143                Style);
11144 
11145   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11146   verifyFormat("int f() {}", Style);
11147   verifyFormat("int f() { return 0; }", Style);
11148   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11149                "{}",
11150                Style);
11151   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11152                "{\n"
11153                "  return 0;\n"
11154                "}",
11155                Style);
11156 }
11157 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
11158   FormatStyle Style = getLLVMStyle();
11159   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11160   verifyFormat("#ifdef A\n"
11161                "int f() {}\n"
11162                "#else\n"
11163                "int g() {}\n"
11164                "#endif",
11165                Style);
11166 }
11167 
11168 TEST_F(FormatTest, SplitEmptyClass) {
11169   FormatStyle Style = getLLVMStyle();
11170   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11171   Style.BraceWrapping.AfterClass = true;
11172   Style.BraceWrapping.SplitEmptyRecord = false;
11173 
11174   verifyFormat("class Foo\n"
11175                "{};",
11176                Style);
11177   verifyFormat("/* something */ class Foo\n"
11178                "{};",
11179                Style);
11180   verifyFormat("template <typename X> class Foo\n"
11181                "{};",
11182                Style);
11183   verifyFormat("class Foo\n"
11184                "{\n"
11185                "  Foo();\n"
11186                "};",
11187                Style);
11188   verifyFormat("typedef class Foo\n"
11189                "{\n"
11190                "} Foo_t;",
11191                Style);
11192 
11193   Style.BraceWrapping.SplitEmptyRecord = true;
11194   Style.BraceWrapping.AfterStruct = true;
11195   verifyFormat("class rep\n"
11196                "{\n"
11197                "};",
11198                Style);
11199   verifyFormat("struct rep\n"
11200                "{\n"
11201                "};",
11202                Style);
11203   verifyFormat("template <typename T> class rep\n"
11204                "{\n"
11205                "};",
11206                Style);
11207   verifyFormat("template <typename T> struct rep\n"
11208                "{\n"
11209                "};",
11210                Style);
11211   verifyFormat("class rep\n"
11212                "{\n"
11213                "  int x;\n"
11214                "};",
11215                Style);
11216   verifyFormat("struct rep\n"
11217                "{\n"
11218                "  int x;\n"
11219                "};",
11220                Style);
11221   verifyFormat("template <typename T> class rep\n"
11222                "{\n"
11223                "  int x;\n"
11224                "};",
11225                Style);
11226   verifyFormat("template <typename T> struct rep\n"
11227                "{\n"
11228                "  int x;\n"
11229                "};",
11230                Style);
11231   verifyFormat("template <typename T> class rep // Foo\n"
11232                "{\n"
11233                "  int x;\n"
11234                "};",
11235                Style);
11236   verifyFormat("template <typename T> struct rep // Bar\n"
11237                "{\n"
11238                "  int x;\n"
11239                "};",
11240                Style);
11241 
11242   verifyFormat("template <typename T> class rep<T>\n"
11243                "{\n"
11244                "  int x;\n"
11245                "};",
11246                Style);
11247 
11248   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
11249                "{\n"
11250                "  int x;\n"
11251                "};",
11252                Style);
11253   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
11254                "{\n"
11255                "};",
11256                Style);
11257 
11258   verifyFormat("#include \"stdint.h\"\n"
11259                "namespace rep {}",
11260                Style);
11261   verifyFormat("#include <stdint.h>\n"
11262                "namespace rep {}",
11263                Style);
11264   verifyFormat("#include <stdint.h>\n"
11265                "namespace rep {}",
11266                "#include <stdint.h>\n"
11267                "namespace rep {\n"
11268                "\n"
11269                "\n"
11270                "}",
11271                Style);
11272 }
11273 
11274 TEST_F(FormatTest, SplitEmptyStruct) {
11275   FormatStyle Style = getLLVMStyle();
11276   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11277   Style.BraceWrapping.AfterStruct = true;
11278   Style.BraceWrapping.SplitEmptyRecord = false;
11279 
11280   verifyFormat("struct Foo\n"
11281                "{};",
11282                Style);
11283   verifyFormat("/* something */ struct Foo\n"
11284                "{};",
11285                Style);
11286   verifyFormat("template <typename X> struct Foo\n"
11287                "{};",
11288                Style);
11289   verifyFormat("struct Foo\n"
11290                "{\n"
11291                "  Foo();\n"
11292                "};",
11293                Style);
11294   verifyFormat("typedef struct Foo\n"
11295                "{\n"
11296                "} Foo_t;",
11297                Style);
11298   // typedef struct Bar {} Bar_t;
11299 }
11300 
11301 TEST_F(FormatTest, SplitEmptyUnion) {
11302   FormatStyle Style = getLLVMStyle();
11303   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11304   Style.BraceWrapping.AfterUnion = true;
11305   Style.BraceWrapping.SplitEmptyRecord = false;
11306 
11307   verifyFormat("union Foo\n"
11308                "{};",
11309                Style);
11310   verifyFormat("/* something */ union Foo\n"
11311                "{};",
11312                Style);
11313   verifyFormat("union Foo\n"
11314                "{\n"
11315                "  A,\n"
11316                "};",
11317                Style);
11318   verifyFormat("typedef union Foo\n"
11319                "{\n"
11320                "} Foo_t;",
11321                Style);
11322 }
11323 
11324 TEST_F(FormatTest, SplitEmptyNamespace) {
11325   FormatStyle Style = getLLVMStyle();
11326   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11327   Style.BraceWrapping.AfterNamespace = true;
11328   Style.BraceWrapping.SplitEmptyNamespace = false;
11329 
11330   verifyFormat("namespace Foo\n"
11331                "{};",
11332                Style);
11333   verifyFormat("/* something */ namespace Foo\n"
11334                "{};",
11335                Style);
11336   verifyFormat("inline namespace Foo\n"
11337                "{};",
11338                Style);
11339   verifyFormat("/* something */ inline namespace Foo\n"
11340                "{};",
11341                Style);
11342   verifyFormat("export namespace Foo\n"
11343                "{};",
11344                Style);
11345   verifyFormat("namespace Foo\n"
11346                "{\n"
11347                "void Bar();\n"
11348                "};",
11349                Style);
11350 }
11351 
11352 TEST_F(FormatTest, NeverMergeShortRecords) {
11353   FormatStyle Style = getLLVMStyle();
11354 
11355   verifyFormat("class Foo {\n"
11356                "  Foo();\n"
11357                "};",
11358                Style);
11359   verifyFormat("typedef class Foo {\n"
11360                "  Foo();\n"
11361                "} Foo_t;",
11362                Style);
11363   verifyFormat("struct Foo {\n"
11364                "  Foo();\n"
11365                "};",
11366                Style);
11367   verifyFormat("typedef struct Foo {\n"
11368                "  Foo();\n"
11369                "} Foo_t;",
11370                Style);
11371   verifyFormat("union Foo {\n"
11372                "  A,\n"
11373                "};",
11374                Style);
11375   verifyFormat("typedef union Foo {\n"
11376                "  A,\n"
11377                "} Foo_t;",
11378                Style);
11379   verifyFormat("namespace Foo {\n"
11380                "void Bar();\n"
11381                "};",
11382                Style);
11383 
11384   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11385   Style.BraceWrapping.AfterClass = true;
11386   Style.BraceWrapping.AfterStruct = true;
11387   Style.BraceWrapping.AfterUnion = true;
11388   Style.BraceWrapping.AfterNamespace = true;
11389   verifyFormat("class Foo\n"
11390                "{\n"
11391                "  Foo();\n"
11392                "};",
11393                Style);
11394   verifyFormat("typedef class Foo\n"
11395                "{\n"
11396                "  Foo();\n"
11397                "} Foo_t;",
11398                Style);
11399   verifyFormat("struct Foo\n"
11400                "{\n"
11401                "  Foo();\n"
11402                "};",
11403                Style);
11404   verifyFormat("typedef struct Foo\n"
11405                "{\n"
11406                "  Foo();\n"
11407                "} Foo_t;",
11408                Style);
11409   verifyFormat("union Foo\n"
11410                "{\n"
11411                "  A,\n"
11412                "};",
11413                Style);
11414   verifyFormat("typedef union Foo\n"
11415                "{\n"
11416                "  A,\n"
11417                "} Foo_t;",
11418                Style);
11419   verifyFormat("namespace Foo\n"
11420                "{\n"
11421                "void Bar();\n"
11422                "};",
11423                Style);
11424 }
11425 
11426 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
11427   // Elaborate type variable declarations.
11428   verifyFormat("struct foo a = {bar};\nint n;");
11429   verifyFormat("class foo a = {bar};\nint n;");
11430   verifyFormat("union foo a = {bar};\nint n;");
11431 
11432   // Elaborate types inside function definitions.
11433   verifyFormat("struct foo f() {}\nint n;");
11434   verifyFormat("class foo f() {}\nint n;");
11435   verifyFormat("union foo f() {}\nint n;");
11436 
11437   // Templates.
11438   verifyFormat("template <class X> void f() {}\nint n;");
11439   verifyFormat("template <struct X> void f() {}\nint n;");
11440   verifyFormat("template <union X> void f() {}\nint n;");
11441 
11442   // Actual definitions...
11443   verifyFormat("struct {\n} n;");
11444   verifyFormat(
11445       "template <template <class T, class Y>, class Z> class X {\n} n;");
11446   verifyFormat("union Z {\n  int n;\n} x;");
11447   verifyFormat("class MACRO Z {\n} n;");
11448   verifyFormat("class MACRO(X) Z {\n} n;");
11449   verifyFormat("class __attribute__(X) Z {\n} n;");
11450   verifyFormat("class __declspec(X) Z {\n} n;");
11451   verifyFormat("class A##B##C {\n} n;");
11452   verifyFormat("class alignas(16) Z {\n} n;");
11453   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
11454   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
11455 
11456   // Redefinition from nested context:
11457   verifyFormat("class A::B::C {\n} n;");
11458 
11459   // Template definitions.
11460   verifyFormat(
11461       "template <typename F>\n"
11462       "Matcher(const Matcher<F> &Other,\n"
11463       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
11464       "                             !is_same<F, T>::value>::type * = 0)\n"
11465       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
11466 
11467   // FIXME: This is still incorrectly handled at the formatter side.
11468   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
11469   verifyFormat("int i = SomeFunction(a<b, a> b);");
11470 
11471   // FIXME:
11472   // This now gets parsed incorrectly as class definition.
11473   // verifyFormat("class A<int> f() {\n}\nint n;");
11474 
11475   // Elaborate types where incorrectly parsing the structural element would
11476   // break the indent.
11477   verifyFormat("if (true)\n"
11478                "  class X x;\n"
11479                "else\n"
11480                "  f();\n");
11481 
11482   // This is simply incomplete. Formatting is not important, but must not crash.
11483   verifyFormat("class A:");
11484 }
11485 
11486 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
11487   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
11488             format("#error Leave     all         white!!!!! space* alone!\n"));
11489   EXPECT_EQ(
11490       "#warning Leave     all         white!!!!! space* alone!\n",
11491       format("#warning Leave     all         white!!!!! space* alone!\n"));
11492   EXPECT_EQ("#error 1", format("  #  error   1"));
11493   EXPECT_EQ("#warning 1", format("  #  warning 1"));
11494 }
11495 
11496 TEST_F(FormatTest, FormatHashIfExpressions) {
11497   verifyFormat("#if AAAA && BBBB");
11498   verifyFormat("#if (AAAA && BBBB)");
11499   verifyFormat("#elif (AAAA && BBBB)");
11500   // FIXME: Come up with a better indentation for #elif.
11501   verifyFormat(
11502       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
11503       "    defined(BBBBBBBB)\n"
11504       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
11505       "    defined(BBBBBBBB)\n"
11506       "#endif",
11507       getLLVMStyleWithColumns(65));
11508 }
11509 
11510 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
11511   FormatStyle AllowsMergedIf = getGoogleStyle();
11512   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
11513       FormatStyle::SIS_WithoutElse;
11514   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
11515   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
11516   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
11517   EXPECT_EQ("if (true) return 42;",
11518             format("if (true)\nreturn 42;", AllowsMergedIf));
11519   FormatStyle ShortMergedIf = AllowsMergedIf;
11520   ShortMergedIf.ColumnLimit = 25;
11521   verifyFormat("#define A \\\n"
11522                "  if (true) return 42;",
11523                ShortMergedIf);
11524   verifyFormat("#define A \\\n"
11525                "  f();    \\\n"
11526                "  if (true)\n"
11527                "#define B",
11528                ShortMergedIf);
11529   verifyFormat("#define A \\\n"
11530                "  f();    \\\n"
11531                "  if (true)\n"
11532                "g();",
11533                ShortMergedIf);
11534   verifyFormat("{\n"
11535                "#ifdef A\n"
11536                "  // Comment\n"
11537                "  if (true) continue;\n"
11538                "#endif\n"
11539                "  // Comment\n"
11540                "  if (true) continue;\n"
11541                "}",
11542                ShortMergedIf);
11543   ShortMergedIf.ColumnLimit = 33;
11544   verifyFormat("#define A \\\n"
11545                "  if constexpr (true) return 42;",
11546                ShortMergedIf);
11547   verifyFormat("#define A \\\n"
11548                "  if CONSTEXPR (true) return 42;",
11549                ShortMergedIf);
11550   ShortMergedIf.ColumnLimit = 29;
11551   verifyFormat("#define A                   \\\n"
11552                "  if (aaaaaaaaaa) return 1; \\\n"
11553                "  return 2;",
11554                ShortMergedIf);
11555   ShortMergedIf.ColumnLimit = 28;
11556   verifyFormat("#define A         \\\n"
11557                "  if (aaaaaaaaaa) \\\n"
11558                "    return 1;     \\\n"
11559                "  return 2;",
11560                ShortMergedIf);
11561   verifyFormat("#define A                \\\n"
11562                "  if constexpr (aaaaaaa) \\\n"
11563                "    return 1;            \\\n"
11564                "  return 2;",
11565                ShortMergedIf);
11566   verifyFormat("#define A                \\\n"
11567                "  if CONSTEXPR (aaaaaaa) \\\n"
11568                "    return 1;            \\\n"
11569                "  return 2;",
11570                ShortMergedIf);
11571 }
11572 
11573 TEST_F(FormatTest, FormatStarDependingOnContext) {
11574   verifyFormat("void f(int *a);");
11575   verifyFormat("void f() { f(fint * b); }");
11576   verifyFormat("class A {\n  void f(int *a);\n};");
11577   verifyFormat("class A {\n  int *a;\n};");
11578   verifyFormat("namespace a {\n"
11579                "namespace b {\n"
11580                "class A {\n"
11581                "  void f() {}\n"
11582                "  int *a;\n"
11583                "};\n"
11584                "} // namespace b\n"
11585                "} // namespace a");
11586 }
11587 
11588 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
11589   verifyFormat("while");
11590   verifyFormat("operator");
11591 }
11592 
11593 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
11594   // This code would be painfully slow to format if we didn't skip it.
11595   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
11596                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11597                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11598                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11599                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11600                    "A(1, 1)\n"
11601                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
11602                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11603                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11604                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11605                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11606                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11607                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11608                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11609                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11610                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
11611   // Deeply nested part is untouched, rest is formatted.
11612   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
11613             format(std::string("int    i;\n") + Code + "int    j;\n",
11614                    getLLVMStyle(), SC_ExpectIncomplete));
11615 }
11616 
11617 //===----------------------------------------------------------------------===//
11618 // Objective-C tests.
11619 //===----------------------------------------------------------------------===//
11620 
11621 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
11622   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
11623   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
11624             format("-(NSUInteger)indexOfObject:(id)anObject;"));
11625   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
11626   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
11627   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
11628             format("-(NSInteger)Method3:(id)anObject;"));
11629   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
11630             format("-(NSInteger)Method4:(id)anObject;"));
11631   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
11632             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
11633   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
11634             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
11635   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
11636             "forAllCells:(BOOL)flag;",
11637             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
11638                    "forAllCells:(BOOL)flag;"));
11639 
11640   // Very long objectiveC method declaration.
11641   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
11642                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
11643   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
11644                "                    inRange:(NSRange)range\n"
11645                "                   outRange:(NSRange)out_range\n"
11646                "                  outRange1:(NSRange)out_range1\n"
11647                "                  outRange2:(NSRange)out_range2\n"
11648                "                  outRange3:(NSRange)out_range3\n"
11649                "                  outRange4:(NSRange)out_range4\n"
11650                "                  outRange5:(NSRange)out_range5\n"
11651                "                  outRange6:(NSRange)out_range6\n"
11652                "                  outRange7:(NSRange)out_range7\n"
11653                "                  outRange8:(NSRange)out_range8\n"
11654                "                  outRange9:(NSRange)out_range9;");
11655 
11656   // When the function name has to be wrapped.
11657   FormatStyle Style = getLLVMStyle();
11658   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
11659   // and always indents instead.
11660   Style.IndentWrappedFunctionNames = false;
11661   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
11662                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
11663                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
11664                "}",
11665                Style);
11666   Style.IndentWrappedFunctionNames = true;
11667   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
11668                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
11669                "               anotherName:(NSString)dddddddddddddd {\n"
11670                "}",
11671                Style);
11672 
11673   verifyFormat("- (int)sum:(vector<int>)numbers;");
11674   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
11675   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
11676   // protocol lists (but not for template classes):
11677   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
11678 
11679   verifyFormat("- (int (*)())foo:(int (*)())f;");
11680   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
11681 
11682   // If there's no return type (very rare in practice!), LLVM and Google style
11683   // agree.
11684   verifyFormat("- foo;");
11685   verifyFormat("- foo:(int)f;");
11686   verifyGoogleFormat("- foo:(int)foo;");
11687 }
11688 
11689 TEST_F(FormatTest, BreaksStringLiterals) {
11690   EXPECT_EQ("\"some text \"\n"
11691             "\"other\";",
11692             format("\"some text other\";", getLLVMStyleWithColumns(12)));
11693   EXPECT_EQ("\"some text \"\n"
11694             "\"other\";",
11695             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
11696   EXPECT_EQ(
11697       "#define A  \\\n"
11698       "  \"some \"  \\\n"
11699       "  \"text \"  \\\n"
11700       "  \"other\";",
11701       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
11702   EXPECT_EQ(
11703       "#define A  \\\n"
11704       "  \"so \"    \\\n"
11705       "  \"text \"  \\\n"
11706       "  \"other\";",
11707       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
11708 
11709   EXPECT_EQ("\"some text\"",
11710             format("\"some text\"", getLLVMStyleWithColumns(1)));
11711   EXPECT_EQ("\"some text\"",
11712             format("\"some text\"", getLLVMStyleWithColumns(11)));
11713   EXPECT_EQ("\"some \"\n"
11714             "\"text\"",
11715             format("\"some text\"", getLLVMStyleWithColumns(10)));
11716   EXPECT_EQ("\"some \"\n"
11717             "\"text\"",
11718             format("\"some text\"", getLLVMStyleWithColumns(7)));
11719   EXPECT_EQ("\"some\"\n"
11720             "\" tex\"\n"
11721             "\"t\"",
11722             format("\"some text\"", getLLVMStyleWithColumns(6)));
11723   EXPECT_EQ("\"some\"\n"
11724             "\" tex\"\n"
11725             "\" and\"",
11726             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
11727   EXPECT_EQ("\"some\"\n"
11728             "\"/tex\"\n"
11729             "\"/and\"",
11730             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
11731 
11732   EXPECT_EQ("variable =\n"
11733             "    \"long string \"\n"
11734             "    \"literal\";",
11735             format("variable = \"long string literal\";",
11736                    getLLVMStyleWithColumns(20)));
11737 
11738   EXPECT_EQ("variable = f(\n"
11739             "    \"long string \"\n"
11740             "    \"literal\",\n"
11741             "    short,\n"
11742             "    loooooooooooooooooooong);",
11743             format("variable = f(\"long string literal\", short, "
11744                    "loooooooooooooooooooong);",
11745                    getLLVMStyleWithColumns(20)));
11746 
11747   EXPECT_EQ(
11748       "f(g(\"long string \"\n"
11749       "    \"literal\"),\n"
11750       "  b);",
11751       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
11752   EXPECT_EQ("f(g(\"long string \"\n"
11753             "    \"literal\",\n"
11754             "    a),\n"
11755             "  b);",
11756             format("f(g(\"long string literal\", a), b);",
11757                    getLLVMStyleWithColumns(20)));
11758   EXPECT_EQ(
11759       "f(\"one two\".split(\n"
11760       "    variable));",
11761       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
11762   EXPECT_EQ("f(\"one two three four five six \"\n"
11763             "  \"seven\".split(\n"
11764             "      really_looooong_variable));",
11765             format("f(\"one two three four five six seven\"."
11766                    "split(really_looooong_variable));",
11767                    getLLVMStyleWithColumns(33)));
11768 
11769   EXPECT_EQ("f(\"some \"\n"
11770             "  \"text\",\n"
11771             "  other);",
11772             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
11773 
11774   // Only break as a last resort.
11775   verifyFormat(
11776       "aaaaaaaaaaaaaaaaaaaa(\n"
11777       "    aaaaaaaaaaaaaaaaaaaa,\n"
11778       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
11779 
11780   EXPECT_EQ("\"splitmea\"\n"
11781             "\"trandomp\"\n"
11782             "\"oint\"",
11783             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
11784 
11785   EXPECT_EQ("\"split/\"\n"
11786             "\"pathat/\"\n"
11787             "\"slashes\"",
11788             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
11789 
11790   EXPECT_EQ("\"split/\"\n"
11791             "\"pathat/\"\n"
11792             "\"slashes\"",
11793             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
11794   EXPECT_EQ("\"split at \"\n"
11795             "\"spaces/at/\"\n"
11796             "\"slashes.at.any$\"\n"
11797             "\"non-alphanumeric%\"\n"
11798             "\"1111111111characte\"\n"
11799             "\"rs\"",
11800             format("\"split at "
11801                    "spaces/at/"
11802                    "slashes.at."
11803                    "any$non-"
11804                    "alphanumeric%"
11805                    "1111111111characte"
11806                    "rs\"",
11807                    getLLVMStyleWithColumns(20)));
11808 
11809   // Verify that splitting the strings understands
11810   // Style::AlwaysBreakBeforeMultilineStrings.
11811   EXPECT_EQ("aaaaaaaaaaaa(\n"
11812             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
11813             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
11814             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
11815                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
11816                    "aaaaaaaaaaaaaaaaaaaaaa\");",
11817                    getGoogleStyle()));
11818   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
11819             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
11820             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
11821                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
11822                    "aaaaaaaaaaaaaaaaaaaaaa\";",
11823                    getGoogleStyle()));
11824   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
11825             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
11826             format("llvm::outs() << "
11827                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
11828                    "aaaaaaaaaaaaaaaaaaa\";"));
11829   EXPECT_EQ("ffff(\n"
11830             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
11831             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
11832             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
11833                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
11834                    getGoogleStyle()));
11835 
11836   FormatStyle Style = getLLVMStyleWithColumns(12);
11837   Style.BreakStringLiterals = false;
11838   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
11839 
11840   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
11841   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11842   EXPECT_EQ("#define A \\\n"
11843             "  \"some \" \\\n"
11844             "  \"text \" \\\n"
11845             "  \"other\";",
11846             format("#define A \"some text other\";", AlignLeft));
11847 }
11848 
11849 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
11850   EXPECT_EQ("C a = \"some more \"\n"
11851             "      \"text\";",
11852             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
11853 }
11854 
11855 TEST_F(FormatTest, FullyRemoveEmptyLines) {
11856   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
11857   NoEmptyLines.MaxEmptyLinesToKeep = 0;
11858   EXPECT_EQ("int i = a(b());",
11859             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
11860 }
11861 
11862 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
11863   EXPECT_EQ(
11864       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11865       "(\n"
11866       "    \"x\t\");",
11867       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11868              "aaaaaaa("
11869              "\"x\t\");"));
11870 }
11871 
11872 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
11873   EXPECT_EQ(
11874       "u8\"utf8 string \"\n"
11875       "u8\"literal\";",
11876       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
11877   EXPECT_EQ(
11878       "u\"utf16 string \"\n"
11879       "u\"literal\";",
11880       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
11881   EXPECT_EQ(
11882       "U\"utf32 string \"\n"
11883       "U\"literal\";",
11884       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
11885   EXPECT_EQ("L\"wide string \"\n"
11886             "L\"literal\";",
11887             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
11888   EXPECT_EQ("@\"NSString \"\n"
11889             "@\"literal\";",
11890             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
11891   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
11892 
11893   // This input makes clang-format try to split the incomplete unicode escape
11894   // sequence, which used to lead to a crasher.
11895   verifyNoCrash(
11896       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
11897       getLLVMStyleWithColumns(60));
11898 }
11899 
11900 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
11901   FormatStyle Style = getGoogleStyleWithColumns(15);
11902   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
11903   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
11904   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
11905   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
11906   EXPECT_EQ("u8R\"x(raw literal)x\";",
11907             format("u8R\"x(raw literal)x\";", Style));
11908 }
11909 
11910 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
11911   FormatStyle Style = getLLVMStyleWithColumns(20);
11912   EXPECT_EQ(
11913       "_T(\"aaaaaaaaaaaaaa\")\n"
11914       "_T(\"aaaaaaaaaaaaaa\")\n"
11915       "_T(\"aaaaaaaaaaaa\")",
11916       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
11917   EXPECT_EQ("f(x,\n"
11918             "  _T(\"aaaaaaaaaaaa\")\n"
11919             "  _T(\"aaa\"),\n"
11920             "  z);",
11921             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
11922 
11923   // FIXME: Handle embedded spaces in one iteration.
11924   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
11925   //            "_T(\"aaaaaaaaaaaaa\")\n"
11926   //            "_T(\"aaaaaaaaaaaaa\")\n"
11927   //            "_T(\"a\")",
11928   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
11929   //                   getLLVMStyleWithColumns(20)));
11930   EXPECT_EQ(
11931       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
11932       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
11933   EXPECT_EQ("f(\n"
11934             "#if !TEST\n"
11935             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
11936             "#endif\n"
11937             ");",
11938             format("f(\n"
11939                    "#if !TEST\n"
11940                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
11941                    "#endif\n"
11942                    ");"));
11943   EXPECT_EQ("f(\n"
11944             "\n"
11945             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
11946             format("f(\n"
11947                    "\n"
11948                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
11949 }
11950 
11951 TEST_F(FormatTest, BreaksStringLiteralOperands) {
11952   // In a function call with two operands, the second can be broken with no line
11953   // break before it.
11954   EXPECT_EQ(
11955       "func(a, \"long long \"\n"
11956       "        \"long long\");",
11957       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
11958   // In a function call with three operands, the second must be broken with a
11959   // line break before it.
11960   EXPECT_EQ("func(a,\n"
11961             "     \"long long long \"\n"
11962             "     \"long\",\n"
11963             "     c);",
11964             format("func(a, \"long long long long\", c);",
11965                    getLLVMStyleWithColumns(24)));
11966   // In a function call with three operands, the third must be broken with a
11967   // line break before it.
11968   EXPECT_EQ("func(a, b,\n"
11969             "     \"long long long \"\n"
11970             "     \"long\");",
11971             format("func(a, b, \"long long long long\");",
11972                    getLLVMStyleWithColumns(24)));
11973   // In a function call with three operands, both the second and the third must
11974   // be broken with a line break before them.
11975   EXPECT_EQ("func(a,\n"
11976             "     \"long long long \"\n"
11977             "     \"long\",\n"
11978             "     \"long long long \"\n"
11979             "     \"long\");",
11980             format("func(a, \"long long long long\", \"long long long long\");",
11981                    getLLVMStyleWithColumns(24)));
11982   // In a chain of << with two operands, the second can be broken with no line
11983   // break before it.
11984   EXPECT_EQ("a << \"line line \"\n"
11985             "     \"line\";",
11986             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
11987   // In a chain of << with three operands, the second can be broken with no line
11988   // break before it.
11989   EXPECT_EQ(
11990       "abcde << \"line \"\n"
11991       "         \"line line\"\n"
11992       "      << c;",
11993       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
11994   // In a chain of << with three operands, the third must be broken with a line
11995   // break before it.
11996   EXPECT_EQ(
11997       "a << b\n"
11998       "  << \"line line \"\n"
11999       "     \"line\";",
12000       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
12001   // In a chain of << with three operands, the second can be broken with no line
12002   // break before it and the third must be broken with a line break before it.
12003   EXPECT_EQ("abcd << \"line line \"\n"
12004             "        \"line\"\n"
12005             "     << \"line line \"\n"
12006             "        \"line\";",
12007             format("abcd << \"line line line\" << \"line line line\";",
12008                    getLLVMStyleWithColumns(20)));
12009   // In a chain of binary operators with two operands, the second can be broken
12010   // with no line break before it.
12011   EXPECT_EQ(
12012       "abcd + \"line line \"\n"
12013       "       \"line line\";",
12014       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
12015   // In a chain of binary operators with three operands, the second must be
12016   // broken with a line break before it.
12017   EXPECT_EQ("abcd +\n"
12018             "    \"line line \"\n"
12019             "    \"line line\" +\n"
12020             "    e;",
12021             format("abcd + \"line line line line\" + e;",
12022                    getLLVMStyleWithColumns(20)));
12023   // In a function call with two operands, with AlignAfterOpenBracket enabled,
12024   // the first must be broken with a line break before it.
12025   FormatStyle Style = getLLVMStyleWithColumns(25);
12026   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
12027   EXPECT_EQ("someFunction(\n"
12028             "    \"long long long \"\n"
12029             "    \"long\",\n"
12030             "    a);",
12031             format("someFunction(\"long long long long\", a);", Style));
12032 }
12033 
12034 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
12035   EXPECT_EQ(
12036       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12037       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12038       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
12039       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12040              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
12041              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
12042 }
12043 
12044 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
12045   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
12046             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
12047   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12048             "multiline raw string literal xxxxxxxxxxxxxx\n"
12049             ")x\",\n"
12050             "              a),\n"
12051             "            b);",
12052             format("fffffffffff(g(R\"x(\n"
12053                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12054                    ")x\", a), b);",
12055                    getGoogleStyleWithColumns(20)));
12056   EXPECT_EQ("fffffffffff(\n"
12057             "    g(R\"x(qqq\n"
12058             "multiline raw string literal xxxxxxxxxxxxxx\n"
12059             ")x\",\n"
12060             "      a),\n"
12061             "    b);",
12062             format("fffffffffff(g(R\"x(qqq\n"
12063                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12064                    ")x\", a), b);",
12065                    getGoogleStyleWithColumns(20)));
12066 
12067   EXPECT_EQ("fffffffffff(R\"x(\n"
12068             "multiline raw string literal xxxxxxxxxxxxxx\n"
12069             ")x\");",
12070             format("fffffffffff(R\"x(\n"
12071                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12072                    ")x\");",
12073                    getGoogleStyleWithColumns(20)));
12074   EXPECT_EQ("fffffffffff(R\"x(\n"
12075             "multiline raw string literal xxxxxxxxxxxxxx\n"
12076             ")x\" + bbbbbb);",
12077             format("fffffffffff(R\"x(\n"
12078                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12079                    ")x\" +   bbbbbb);",
12080                    getGoogleStyleWithColumns(20)));
12081   EXPECT_EQ("fffffffffff(\n"
12082             "    R\"x(\n"
12083             "multiline raw string literal xxxxxxxxxxxxxx\n"
12084             ")x\" +\n"
12085             "    bbbbbb);",
12086             format("fffffffffff(\n"
12087                    " R\"x(\n"
12088                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12089                    ")x\" + bbbbbb);",
12090                    getGoogleStyleWithColumns(20)));
12091   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
12092             format("fffffffffff(\n"
12093                    " R\"(single line raw string)\" + bbbbbb);"));
12094 }
12095 
12096 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
12097   verifyFormat("string a = \"unterminated;");
12098   EXPECT_EQ("function(\"unterminated,\n"
12099             "         OtherParameter);",
12100             format("function(  \"unterminated,\n"
12101                    "    OtherParameter);"));
12102 }
12103 
12104 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
12105   FormatStyle Style = getLLVMStyle();
12106   Style.Standard = FormatStyle::LS_Cpp03;
12107   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
12108             format("#define x(_a) printf(\"foo\"_a);", Style));
12109 }
12110 
12111 TEST_F(FormatTest, CppLexVersion) {
12112   FormatStyle Style = getLLVMStyle();
12113   // Formatting of x * y differs if x is a type.
12114   verifyFormat("void foo() { MACRO(a * b); }", Style);
12115   verifyFormat("void foo() { MACRO(int *b); }", Style);
12116 
12117   // LLVM style uses latest lexer.
12118   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
12119   Style.Standard = FormatStyle::LS_Cpp17;
12120   // But in c++17, char8_t isn't a keyword.
12121   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
12122 }
12123 
12124 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
12125 
12126 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
12127   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
12128             "             \"ddeeefff\");",
12129             format("someFunction(\"aaabbbcccdddeeefff\");",
12130                    getLLVMStyleWithColumns(25)));
12131   EXPECT_EQ("someFunction1234567890(\n"
12132             "    \"aaabbbcccdddeeefff\");",
12133             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12134                    getLLVMStyleWithColumns(26)));
12135   EXPECT_EQ("someFunction1234567890(\n"
12136             "    \"aaabbbcccdddeeeff\"\n"
12137             "    \"f\");",
12138             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12139                    getLLVMStyleWithColumns(25)));
12140   EXPECT_EQ("someFunction1234567890(\n"
12141             "    \"aaabbbcccdddeeeff\"\n"
12142             "    \"f\");",
12143             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12144                    getLLVMStyleWithColumns(24)));
12145   EXPECT_EQ("someFunction(\n"
12146             "    \"aaabbbcc ddde \"\n"
12147             "    \"efff\");",
12148             format("someFunction(\"aaabbbcc ddde efff\");",
12149                    getLLVMStyleWithColumns(25)));
12150   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
12151             "             \"ddeeefff\");",
12152             format("someFunction(\"aaabbbccc ddeeefff\");",
12153                    getLLVMStyleWithColumns(25)));
12154   EXPECT_EQ("someFunction1234567890(\n"
12155             "    \"aaabb \"\n"
12156             "    \"cccdddeeefff\");",
12157             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
12158                    getLLVMStyleWithColumns(25)));
12159   EXPECT_EQ("#define A          \\\n"
12160             "  string s =       \\\n"
12161             "      \"123456789\"  \\\n"
12162             "      \"0\";         \\\n"
12163             "  int i;",
12164             format("#define A string s = \"1234567890\"; int i;",
12165                    getLLVMStyleWithColumns(20)));
12166   EXPECT_EQ("someFunction(\n"
12167             "    \"aaabbbcc \"\n"
12168             "    \"dddeeefff\");",
12169             format("someFunction(\"aaabbbcc dddeeefff\");",
12170                    getLLVMStyleWithColumns(25)));
12171 }
12172 
12173 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
12174   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
12175   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
12176   EXPECT_EQ("\"test\"\n"
12177             "\"\\n\"",
12178             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
12179   EXPECT_EQ("\"tes\\\\\"\n"
12180             "\"n\"",
12181             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
12182   EXPECT_EQ("\"\\\\\\\\\"\n"
12183             "\"\\n\"",
12184             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
12185   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
12186   EXPECT_EQ("\"\\uff01\"\n"
12187             "\"test\"",
12188             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
12189   EXPECT_EQ("\"\\Uff01ff02\"",
12190             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
12191   EXPECT_EQ("\"\\x000000000001\"\n"
12192             "\"next\"",
12193             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
12194   EXPECT_EQ("\"\\x000000000001next\"",
12195             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
12196   EXPECT_EQ("\"\\x000000000001\"",
12197             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
12198   EXPECT_EQ("\"test\"\n"
12199             "\"\\000000\"\n"
12200             "\"000001\"",
12201             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
12202   EXPECT_EQ("\"test\\000\"\n"
12203             "\"00000000\"\n"
12204             "\"1\"",
12205             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
12206 }
12207 
12208 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
12209   verifyFormat("void f() {\n"
12210                "  return g() {}\n"
12211                "  void h() {}");
12212   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
12213                "g();\n"
12214                "}");
12215 }
12216 
12217 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
12218   verifyFormat(
12219       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
12220 }
12221 
12222 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
12223   verifyFormat("class X {\n"
12224                "  void f() {\n"
12225                "  }\n"
12226                "};",
12227                getLLVMStyleWithColumns(12));
12228 }
12229 
12230 TEST_F(FormatTest, ConfigurableIndentWidth) {
12231   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
12232   EightIndent.IndentWidth = 8;
12233   EightIndent.ContinuationIndentWidth = 8;
12234   verifyFormat("void f() {\n"
12235                "        someFunction();\n"
12236                "        if (true) {\n"
12237                "                f();\n"
12238                "        }\n"
12239                "}",
12240                EightIndent);
12241   verifyFormat("class X {\n"
12242                "        void f() {\n"
12243                "        }\n"
12244                "};",
12245                EightIndent);
12246   verifyFormat("int x[] = {\n"
12247                "        call(),\n"
12248                "        call()};",
12249                EightIndent);
12250 }
12251 
12252 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
12253   verifyFormat("double\n"
12254                "f();",
12255                getLLVMStyleWithColumns(8));
12256 }
12257 
12258 TEST_F(FormatTest, ConfigurableUseOfTab) {
12259   FormatStyle Tab = getLLVMStyleWithColumns(42);
12260   Tab.IndentWidth = 8;
12261   Tab.UseTab = FormatStyle::UT_Always;
12262   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12263 
12264   EXPECT_EQ("if (aaaaaaaa && // q\n"
12265             "    bb)\t\t// w\n"
12266             "\t;",
12267             format("if (aaaaaaaa &&// q\n"
12268                    "bb)// w\n"
12269                    ";",
12270                    Tab));
12271   EXPECT_EQ("if (aaa && bbb) // w\n"
12272             "\t;",
12273             format("if(aaa&&bbb)// w\n"
12274                    ";",
12275                    Tab));
12276 
12277   verifyFormat("class X {\n"
12278                "\tvoid f() {\n"
12279                "\t\tsomeFunction(parameter1,\n"
12280                "\t\t\t     parameter2);\n"
12281                "\t}\n"
12282                "};",
12283                Tab);
12284   verifyFormat("#define A                        \\\n"
12285                "\tvoid f() {               \\\n"
12286                "\t\tsomeFunction(    \\\n"
12287                "\t\t    parameter1,  \\\n"
12288                "\t\t    parameter2); \\\n"
12289                "\t}",
12290                Tab);
12291   verifyFormat("int a;\t      // x\n"
12292                "int bbbbbbbb; // x\n",
12293                Tab);
12294 
12295   Tab.TabWidth = 4;
12296   Tab.IndentWidth = 8;
12297   verifyFormat("class TabWidth4Indent8 {\n"
12298                "\t\tvoid f() {\n"
12299                "\t\t\t\tsomeFunction(parameter1,\n"
12300                "\t\t\t\t\t\t\t parameter2);\n"
12301                "\t\t}\n"
12302                "};",
12303                Tab);
12304 
12305   Tab.TabWidth = 4;
12306   Tab.IndentWidth = 4;
12307   verifyFormat("class TabWidth4Indent4 {\n"
12308                "\tvoid f() {\n"
12309                "\t\tsomeFunction(parameter1,\n"
12310                "\t\t\t\t\t parameter2);\n"
12311                "\t}\n"
12312                "};",
12313                Tab);
12314 
12315   Tab.TabWidth = 8;
12316   Tab.IndentWidth = 4;
12317   verifyFormat("class TabWidth8Indent4 {\n"
12318                "    void f() {\n"
12319                "\tsomeFunction(parameter1,\n"
12320                "\t\t     parameter2);\n"
12321                "    }\n"
12322                "};",
12323                Tab);
12324 
12325   Tab.TabWidth = 8;
12326   Tab.IndentWidth = 8;
12327   EXPECT_EQ("/*\n"
12328             "\t      a\t\tcomment\n"
12329             "\t      in multiple lines\n"
12330             "       */",
12331             format("   /*\t \t \n"
12332                    " \t \t a\t\tcomment\t \t\n"
12333                    " \t \t in multiple lines\t\n"
12334                    " \t  */",
12335                    Tab));
12336 
12337   Tab.UseTab = FormatStyle::UT_ForIndentation;
12338   verifyFormat("{\n"
12339                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12340                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12341                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12342                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12343                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12344                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12345                "};",
12346                Tab);
12347   verifyFormat("enum AA {\n"
12348                "\ta1, // Force multiple lines\n"
12349                "\ta2,\n"
12350                "\ta3\n"
12351                "};",
12352                Tab);
12353   EXPECT_EQ("if (aaaaaaaa && // q\n"
12354             "    bb)         // w\n"
12355             "\t;",
12356             format("if (aaaaaaaa &&// q\n"
12357                    "bb)// w\n"
12358                    ";",
12359                    Tab));
12360   verifyFormat("class X {\n"
12361                "\tvoid f() {\n"
12362                "\t\tsomeFunction(parameter1,\n"
12363                "\t\t             parameter2);\n"
12364                "\t}\n"
12365                "};",
12366                Tab);
12367   verifyFormat("{\n"
12368                "\tQ(\n"
12369                "\t    {\n"
12370                "\t\t    int a;\n"
12371                "\t\t    someFunction(aaaaaaaa,\n"
12372                "\t\t                 bbbbbbb);\n"
12373                "\t    },\n"
12374                "\t    p);\n"
12375                "}",
12376                Tab);
12377   EXPECT_EQ("{\n"
12378             "\t/* aaaa\n"
12379             "\t   bbbb */\n"
12380             "}",
12381             format("{\n"
12382                    "/* aaaa\n"
12383                    "   bbbb */\n"
12384                    "}",
12385                    Tab));
12386   EXPECT_EQ("{\n"
12387             "\t/*\n"
12388             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12389             "\t  bbbbbbbbbbbbb\n"
12390             "\t*/\n"
12391             "}",
12392             format("{\n"
12393                    "/*\n"
12394                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12395                    "*/\n"
12396                    "}",
12397                    Tab));
12398   EXPECT_EQ("{\n"
12399             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12400             "\t// bbbbbbbbbbbbb\n"
12401             "}",
12402             format("{\n"
12403                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12404                    "}",
12405                    Tab));
12406   EXPECT_EQ("{\n"
12407             "\t/*\n"
12408             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12409             "\t  bbbbbbbbbbbbb\n"
12410             "\t*/\n"
12411             "}",
12412             format("{\n"
12413                    "\t/*\n"
12414                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12415                    "\t*/\n"
12416                    "}",
12417                    Tab));
12418   EXPECT_EQ("{\n"
12419             "\t/*\n"
12420             "\n"
12421             "\t*/\n"
12422             "}",
12423             format("{\n"
12424                    "\t/*\n"
12425                    "\n"
12426                    "\t*/\n"
12427                    "}",
12428                    Tab));
12429   EXPECT_EQ("{\n"
12430             "\t/*\n"
12431             " asdf\n"
12432             "\t*/\n"
12433             "}",
12434             format("{\n"
12435                    "\t/*\n"
12436                    " asdf\n"
12437                    "\t*/\n"
12438                    "}",
12439                    Tab));
12440 
12441   Tab.UseTab = FormatStyle::UT_Never;
12442   EXPECT_EQ("/*\n"
12443             "              a\t\tcomment\n"
12444             "              in multiple lines\n"
12445             "       */",
12446             format("   /*\t \t \n"
12447                    " \t \t a\t\tcomment\t \t\n"
12448                    " \t \t in multiple lines\t\n"
12449                    " \t  */",
12450                    Tab));
12451   EXPECT_EQ("/* some\n"
12452             "   comment */",
12453             format(" \t \t /* some\n"
12454                    " \t \t    comment */",
12455                    Tab));
12456   EXPECT_EQ("int a; /* some\n"
12457             "   comment */",
12458             format(" \t \t int a; /* some\n"
12459                    " \t \t    comment */",
12460                    Tab));
12461 
12462   EXPECT_EQ("int a; /* some\n"
12463             "comment */",
12464             format(" \t \t int\ta; /* some\n"
12465                    " \t \t    comment */",
12466                    Tab));
12467   EXPECT_EQ("f(\"\t\t\"); /* some\n"
12468             "    comment */",
12469             format(" \t \t f(\"\t\t\"); /* some\n"
12470                    " \t \t    comment */",
12471                    Tab));
12472   EXPECT_EQ("{\n"
12473             "        /*\n"
12474             "         * Comment\n"
12475             "         */\n"
12476             "        int i;\n"
12477             "}",
12478             format("{\n"
12479                    "\t/*\n"
12480                    "\t * Comment\n"
12481                    "\t */\n"
12482                    "\t int i;\n"
12483                    "}",
12484                    Tab));
12485 
12486   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
12487   Tab.TabWidth = 8;
12488   Tab.IndentWidth = 8;
12489   EXPECT_EQ("if (aaaaaaaa && // q\n"
12490             "    bb)         // w\n"
12491             "\t;",
12492             format("if (aaaaaaaa &&// q\n"
12493                    "bb)// w\n"
12494                    ";",
12495                    Tab));
12496   EXPECT_EQ("if (aaa && bbb) // w\n"
12497             "\t;",
12498             format("if(aaa&&bbb)// w\n"
12499                    ";",
12500                    Tab));
12501   verifyFormat("class X {\n"
12502                "\tvoid f() {\n"
12503                "\t\tsomeFunction(parameter1,\n"
12504                "\t\t\t     parameter2);\n"
12505                "\t}\n"
12506                "};",
12507                Tab);
12508   verifyFormat("#define A                        \\\n"
12509                "\tvoid f() {               \\\n"
12510                "\t\tsomeFunction(    \\\n"
12511                "\t\t    parameter1,  \\\n"
12512                "\t\t    parameter2); \\\n"
12513                "\t}",
12514                Tab);
12515   Tab.TabWidth = 4;
12516   Tab.IndentWidth = 8;
12517   verifyFormat("class TabWidth4Indent8 {\n"
12518                "\t\tvoid f() {\n"
12519                "\t\t\t\tsomeFunction(parameter1,\n"
12520                "\t\t\t\t\t\t\t parameter2);\n"
12521                "\t\t}\n"
12522                "};",
12523                Tab);
12524   Tab.TabWidth = 4;
12525   Tab.IndentWidth = 4;
12526   verifyFormat("class TabWidth4Indent4 {\n"
12527                "\tvoid f() {\n"
12528                "\t\tsomeFunction(parameter1,\n"
12529                "\t\t\t\t\t parameter2);\n"
12530                "\t}\n"
12531                "};",
12532                Tab);
12533   Tab.TabWidth = 8;
12534   Tab.IndentWidth = 4;
12535   verifyFormat("class TabWidth8Indent4 {\n"
12536                "    void f() {\n"
12537                "\tsomeFunction(parameter1,\n"
12538                "\t\t     parameter2);\n"
12539                "    }\n"
12540                "};",
12541                Tab);
12542   Tab.TabWidth = 8;
12543   Tab.IndentWidth = 8;
12544   EXPECT_EQ("/*\n"
12545             "\t      a\t\tcomment\n"
12546             "\t      in multiple lines\n"
12547             "       */",
12548             format("   /*\t \t \n"
12549                    " \t \t a\t\tcomment\t \t\n"
12550                    " \t \t in multiple lines\t\n"
12551                    " \t  */",
12552                    Tab));
12553   verifyFormat("{\n"
12554                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12555                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12556                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12557                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12558                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12559                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12560                "};",
12561                Tab);
12562   verifyFormat("enum AA {\n"
12563                "\ta1, // Force multiple lines\n"
12564                "\ta2,\n"
12565                "\ta3\n"
12566                "};",
12567                Tab);
12568   EXPECT_EQ("if (aaaaaaaa && // q\n"
12569             "    bb)         // w\n"
12570             "\t;",
12571             format("if (aaaaaaaa &&// q\n"
12572                    "bb)// w\n"
12573                    ";",
12574                    Tab));
12575   verifyFormat("class X {\n"
12576                "\tvoid f() {\n"
12577                "\t\tsomeFunction(parameter1,\n"
12578                "\t\t\t     parameter2);\n"
12579                "\t}\n"
12580                "};",
12581                Tab);
12582   verifyFormat("{\n"
12583                "\tQ(\n"
12584                "\t    {\n"
12585                "\t\t    int a;\n"
12586                "\t\t    someFunction(aaaaaaaa,\n"
12587                "\t\t\t\t bbbbbbb);\n"
12588                "\t    },\n"
12589                "\t    p);\n"
12590                "}",
12591                Tab);
12592   EXPECT_EQ("{\n"
12593             "\t/* aaaa\n"
12594             "\t   bbbb */\n"
12595             "}",
12596             format("{\n"
12597                    "/* aaaa\n"
12598                    "   bbbb */\n"
12599                    "}",
12600                    Tab));
12601   EXPECT_EQ("{\n"
12602             "\t/*\n"
12603             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12604             "\t  bbbbbbbbbbbbb\n"
12605             "\t*/\n"
12606             "}",
12607             format("{\n"
12608                    "/*\n"
12609                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12610                    "*/\n"
12611                    "}",
12612                    Tab));
12613   EXPECT_EQ("{\n"
12614             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12615             "\t// bbbbbbbbbbbbb\n"
12616             "}",
12617             format("{\n"
12618                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12619                    "}",
12620                    Tab));
12621   EXPECT_EQ("{\n"
12622             "\t/*\n"
12623             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12624             "\t  bbbbbbbbbbbbb\n"
12625             "\t*/\n"
12626             "}",
12627             format("{\n"
12628                    "\t/*\n"
12629                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12630                    "\t*/\n"
12631                    "}",
12632                    Tab));
12633   EXPECT_EQ("{\n"
12634             "\t/*\n"
12635             "\n"
12636             "\t*/\n"
12637             "}",
12638             format("{\n"
12639                    "\t/*\n"
12640                    "\n"
12641                    "\t*/\n"
12642                    "}",
12643                    Tab));
12644   EXPECT_EQ("{\n"
12645             "\t/*\n"
12646             " asdf\n"
12647             "\t*/\n"
12648             "}",
12649             format("{\n"
12650                    "\t/*\n"
12651                    " asdf\n"
12652                    "\t*/\n"
12653                    "}",
12654                    Tab));
12655   EXPECT_EQ("/* some\n"
12656             "   comment */",
12657             format(" \t \t /* some\n"
12658                    " \t \t    comment */",
12659                    Tab));
12660   EXPECT_EQ("int a; /* some\n"
12661             "   comment */",
12662             format(" \t \t int a; /* some\n"
12663                    " \t \t    comment */",
12664                    Tab));
12665   EXPECT_EQ("int a; /* some\n"
12666             "comment */",
12667             format(" \t \t int\ta; /* some\n"
12668                    " \t \t    comment */",
12669                    Tab));
12670   EXPECT_EQ("f(\"\t\t\"); /* some\n"
12671             "    comment */",
12672             format(" \t \t f(\"\t\t\"); /* some\n"
12673                    " \t \t    comment */",
12674                    Tab));
12675   EXPECT_EQ("{\n"
12676             "\t/*\n"
12677             "\t * Comment\n"
12678             "\t */\n"
12679             "\tint i;\n"
12680             "}",
12681             format("{\n"
12682                    "\t/*\n"
12683                    "\t * Comment\n"
12684                    "\t */\n"
12685                    "\t int i;\n"
12686                    "}",
12687                    Tab));
12688   Tab.TabWidth = 2;
12689   Tab.IndentWidth = 2;
12690   EXPECT_EQ("{\n"
12691             "\t/* aaaa\n"
12692             "\t\t bbbb */\n"
12693             "}",
12694             format("{\n"
12695                    "/* aaaa\n"
12696                    "\t bbbb */\n"
12697                    "}",
12698                    Tab));
12699   EXPECT_EQ("{\n"
12700             "\t/*\n"
12701             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12702             "\t\tbbbbbbbbbbbbb\n"
12703             "\t*/\n"
12704             "}",
12705             format("{\n"
12706                    "/*\n"
12707                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12708                    "*/\n"
12709                    "}",
12710                    Tab));
12711   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
12712   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
12713   Tab.TabWidth = 4;
12714   Tab.IndentWidth = 4;
12715   verifyFormat("class Assign {\n"
12716                "\tvoid f() {\n"
12717                "\t\tint         x      = 123;\n"
12718                "\t\tint         random = 4;\n"
12719                "\t\tstd::string alphabet =\n"
12720                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
12721                "\t}\n"
12722                "};",
12723                Tab);
12724 
12725   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
12726   Tab.TabWidth = 8;
12727   Tab.IndentWidth = 8;
12728   EXPECT_EQ("if (aaaaaaaa && // q\n"
12729             "    bb)         // w\n"
12730             "\t;",
12731             format("if (aaaaaaaa &&// q\n"
12732                    "bb)// w\n"
12733                    ";",
12734                    Tab));
12735   EXPECT_EQ("if (aaa && bbb) // w\n"
12736             "\t;",
12737             format("if(aaa&&bbb)// w\n"
12738                    ";",
12739                    Tab));
12740   verifyFormat("class X {\n"
12741                "\tvoid f() {\n"
12742                "\t\tsomeFunction(parameter1,\n"
12743                "\t\t             parameter2);\n"
12744                "\t}\n"
12745                "};",
12746                Tab);
12747   verifyFormat("#define A                        \\\n"
12748                "\tvoid f() {               \\\n"
12749                "\t\tsomeFunction(    \\\n"
12750                "\t\t    parameter1,  \\\n"
12751                "\t\t    parameter2); \\\n"
12752                "\t}",
12753                Tab);
12754   Tab.TabWidth = 4;
12755   Tab.IndentWidth = 8;
12756   verifyFormat("class TabWidth4Indent8 {\n"
12757                "\t\tvoid f() {\n"
12758                "\t\t\t\tsomeFunction(parameter1,\n"
12759                "\t\t\t\t             parameter2);\n"
12760                "\t\t}\n"
12761                "};",
12762                Tab);
12763   Tab.TabWidth = 4;
12764   Tab.IndentWidth = 4;
12765   verifyFormat("class TabWidth4Indent4 {\n"
12766                "\tvoid f() {\n"
12767                "\t\tsomeFunction(parameter1,\n"
12768                "\t\t             parameter2);\n"
12769                "\t}\n"
12770                "};",
12771                Tab);
12772   Tab.TabWidth = 8;
12773   Tab.IndentWidth = 4;
12774   verifyFormat("class TabWidth8Indent4 {\n"
12775                "    void f() {\n"
12776                "\tsomeFunction(parameter1,\n"
12777                "\t             parameter2);\n"
12778                "    }\n"
12779                "};",
12780                Tab);
12781   Tab.TabWidth = 8;
12782   Tab.IndentWidth = 8;
12783   EXPECT_EQ("/*\n"
12784             "              a\t\tcomment\n"
12785             "              in multiple lines\n"
12786             "       */",
12787             format("   /*\t \t \n"
12788                    " \t \t a\t\tcomment\t \t\n"
12789                    " \t \t in multiple lines\t\n"
12790                    " \t  */",
12791                    Tab));
12792   verifyFormat("{\n"
12793                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12794                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12795                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12796                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12797                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12798                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12799                "};",
12800                Tab);
12801   verifyFormat("enum AA {\n"
12802                "\ta1, // Force multiple lines\n"
12803                "\ta2,\n"
12804                "\ta3\n"
12805                "};",
12806                Tab);
12807   EXPECT_EQ("if (aaaaaaaa && // q\n"
12808             "    bb)         // w\n"
12809             "\t;",
12810             format("if (aaaaaaaa &&// q\n"
12811                    "bb)// w\n"
12812                    ";",
12813                    Tab));
12814   verifyFormat("class X {\n"
12815                "\tvoid f() {\n"
12816                "\t\tsomeFunction(parameter1,\n"
12817                "\t\t             parameter2);\n"
12818                "\t}\n"
12819                "};",
12820                Tab);
12821   verifyFormat("{\n"
12822                "\tQ(\n"
12823                "\t    {\n"
12824                "\t\t    int a;\n"
12825                "\t\t    someFunction(aaaaaaaa,\n"
12826                "\t\t                 bbbbbbb);\n"
12827                "\t    },\n"
12828                "\t    p);\n"
12829                "}",
12830                Tab);
12831   EXPECT_EQ("{\n"
12832             "\t/* aaaa\n"
12833             "\t   bbbb */\n"
12834             "}",
12835             format("{\n"
12836                    "/* aaaa\n"
12837                    "   bbbb */\n"
12838                    "}",
12839                    Tab));
12840   EXPECT_EQ("{\n"
12841             "\t/*\n"
12842             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12843             "\t  bbbbbbbbbbbbb\n"
12844             "\t*/\n"
12845             "}",
12846             format("{\n"
12847                    "/*\n"
12848                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12849                    "*/\n"
12850                    "}",
12851                    Tab));
12852   EXPECT_EQ("{\n"
12853             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12854             "\t// bbbbbbbbbbbbb\n"
12855             "}",
12856             format("{\n"
12857                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12858                    "}",
12859                    Tab));
12860   EXPECT_EQ("{\n"
12861             "\t/*\n"
12862             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12863             "\t  bbbbbbbbbbbbb\n"
12864             "\t*/\n"
12865             "}",
12866             format("{\n"
12867                    "\t/*\n"
12868                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12869                    "\t*/\n"
12870                    "}",
12871                    Tab));
12872   EXPECT_EQ("{\n"
12873             "\t/*\n"
12874             "\n"
12875             "\t*/\n"
12876             "}",
12877             format("{\n"
12878                    "\t/*\n"
12879                    "\n"
12880                    "\t*/\n"
12881                    "}",
12882                    Tab));
12883   EXPECT_EQ("{\n"
12884             "\t/*\n"
12885             " asdf\n"
12886             "\t*/\n"
12887             "}",
12888             format("{\n"
12889                    "\t/*\n"
12890                    " asdf\n"
12891                    "\t*/\n"
12892                    "}",
12893                    Tab));
12894   EXPECT_EQ("/* some\n"
12895             "   comment */",
12896             format(" \t \t /* some\n"
12897                    " \t \t    comment */",
12898                    Tab));
12899   EXPECT_EQ("int a; /* some\n"
12900             "   comment */",
12901             format(" \t \t int a; /* some\n"
12902                    " \t \t    comment */",
12903                    Tab));
12904   EXPECT_EQ("int a; /* some\n"
12905             "comment */",
12906             format(" \t \t int\ta; /* some\n"
12907                    " \t \t    comment */",
12908                    Tab));
12909   EXPECT_EQ("f(\"\t\t\"); /* some\n"
12910             "    comment */",
12911             format(" \t \t f(\"\t\t\"); /* some\n"
12912                    " \t \t    comment */",
12913                    Tab));
12914   EXPECT_EQ("{\n"
12915             "\t/*\n"
12916             "\t * Comment\n"
12917             "\t */\n"
12918             "\tint i;\n"
12919             "}",
12920             format("{\n"
12921                    "\t/*\n"
12922                    "\t * Comment\n"
12923                    "\t */\n"
12924                    "\t int i;\n"
12925                    "}",
12926                    Tab));
12927   Tab.TabWidth = 2;
12928   Tab.IndentWidth = 2;
12929   EXPECT_EQ("{\n"
12930             "\t/* aaaa\n"
12931             "\t   bbbb */\n"
12932             "}",
12933             format("{\n"
12934                    "/* aaaa\n"
12935                    "   bbbb */\n"
12936                    "}",
12937                    Tab));
12938   EXPECT_EQ("{\n"
12939             "\t/*\n"
12940             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12941             "\t  bbbbbbbbbbbbb\n"
12942             "\t*/\n"
12943             "}",
12944             format("{\n"
12945                    "/*\n"
12946                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12947                    "*/\n"
12948                    "}",
12949                    Tab));
12950   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
12951   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
12952   Tab.TabWidth = 4;
12953   Tab.IndentWidth = 4;
12954   verifyFormat("class Assign {\n"
12955                "\tvoid f() {\n"
12956                "\t\tint         x      = 123;\n"
12957                "\t\tint         random = 4;\n"
12958                "\t\tstd::string alphabet =\n"
12959                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
12960                "\t}\n"
12961                "};",
12962                Tab);
12963   Tab.AlignOperands = FormatStyle::OAS_Align;
12964   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
12965                "                 cccccccccccccccccccc;",
12966                Tab);
12967   // no alignment
12968   verifyFormat("int aaaaaaaaaa =\n"
12969                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
12970                Tab);
12971   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
12972                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
12973                "                        : 333333333333333;",
12974                Tab);
12975   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
12976   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
12977   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
12978                "               + cccccccccccccccccccc;",
12979                Tab);
12980 }
12981 
12982 TEST_F(FormatTest, ZeroTabWidth) {
12983   FormatStyle Tab = getLLVMStyleWithColumns(42);
12984   Tab.IndentWidth = 8;
12985   Tab.UseTab = FormatStyle::UT_Never;
12986   Tab.TabWidth = 0;
12987   EXPECT_EQ("void a(){\n"
12988             "    // line starts with '\t'\n"
12989             "};",
12990             format("void a(){\n"
12991                    "\t// line starts with '\t'\n"
12992                    "};",
12993                    Tab));
12994 
12995   EXPECT_EQ("void a(){\n"
12996             "    // line starts with '\t'\n"
12997             "};",
12998             format("void a(){\n"
12999                    "\t\t// line starts with '\t'\n"
13000                    "};",
13001                    Tab));
13002 
13003   Tab.UseTab = FormatStyle::UT_ForIndentation;
13004   EXPECT_EQ("void a(){\n"
13005             "    // line starts with '\t'\n"
13006             "};",
13007             format("void a(){\n"
13008                    "\t// line starts with '\t'\n"
13009                    "};",
13010                    Tab));
13011 
13012   EXPECT_EQ("void a(){\n"
13013             "    // line starts with '\t'\n"
13014             "};",
13015             format("void a(){\n"
13016                    "\t\t// line starts with '\t'\n"
13017                    "};",
13018                    Tab));
13019 
13020   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
13021   EXPECT_EQ("void a(){\n"
13022             "    // line starts with '\t'\n"
13023             "};",
13024             format("void a(){\n"
13025                    "\t// line starts with '\t'\n"
13026                    "};",
13027                    Tab));
13028 
13029   EXPECT_EQ("void a(){\n"
13030             "    // line starts with '\t'\n"
13031             "};",
13032             format("void a(){\n"
13033                    "\t\t// line starts with '\t'\n"
13034                    "};",
13035                    Tab));
13036 
13037   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
13038   EXPECT_EQ("void a(){\n"
13039             "    // line starts with '\t'\n"
13040             "};",
13041             format("void a(){\n"
13042                    "\t// line starts with '\t'\n"
13043                    "};",
13044                    Tab));
13045 
13046   EXPECT_EQ("void a(){\n"
13047             "    // line starts with '\t'\n"
13048             "};",
13049             format("void a(){\n"
13050                    "\t\t// line starts with '\t'\n"
13051                    "};",
13052                    Tab));
13053 
13054   Tab.UseTab = FormatStyle::UT_Always;
13055   EXPECT_EQ("void a(){\n"
13056             "// line starts with '\t'\n"
13057             "};",
13058             format("void a(){\n"
13059                    "\t// line starts with '\t'\n"
13060                    "};",
13061                    Tab));
13062 
13063   EXPECT_EQ("void a(){\n"
13064             "// line starts with '\t'\n"
13065             "};",
13066             format("void a(){\n"
13067                    "\t\t// line starts with '\t'\n"
13068                    "};",
13069                    Tab));
13070 }
13071 
13072 TEST_F(FormatTest, CalculatesOriginalColumn) {
13073   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13074             "q\"; /* some\n"
13075             "       comment */",
13076             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13077                    "q\"; /* some\n"
13078                    "       comment */",
13079                    getLLVMStyle()));
13080   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13081             "/* some\n"
13082             "   comment */",
13083             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13084                    " /* some\n"
13085                    "    comment */",
13086                    getLLVMStyle()));
13087   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13088             "qqq\n"
13089             "/* some\n"
13090             "   comment */",
13091             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13092                    "qqq\n"
13093                    " /* some\n"
13094                    "    comment */",
13095                    getLLVMStyle()));
13096   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13097             "wwww; /* some\n"
13098             "         comment */",
13099             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13100                    "wwww; /* some\n"
13101                    "         comment */",
13102                    getLLVMStyle()));
13103 }
13104 
13105 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
13106   FormatStyle NoSpace = getLLVMStyle();
13107   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
13108 
13109   verifyFormat("while(true)\n"
13110                "  continue;",
13111                NoSpace);
13112   verifyFormat("for(;;)\n"
13113                "  continue;",
13114                NoSpace);
13115   verifyFormat("if(true)\n"
13116                "  f();\n"
13117                "else if(true)\n"
13118                "  f();",
13119                NoSpace);
13120   verifyFormat("do {\n"
13121                "  do_something();\n"
13122                "} while(something());",
13123                NoSpace);
13124   verifyFormat("switch(x) {\n"
13125                "default:\n"
13126                "  break;\n"
13127                "}",
13128                NoSpace);
13129   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
13130   verifyFormat("size_t x = sizeof(x);", NoSpace);
13131   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
13132   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
13133   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
13134   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
13135   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
13136   verifyFormat("alignas(128) char a[128];", NoSpace);
13137   verifyFormat("size_t x = alignof(MyType);", NoSpace);
13138   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
13139   verifyFormat("int f() throw(Deprecated);", NoSpace);
13140   verifyFormat("typedef void (*cb)(int);", NoSpace);
13141   verifyFormat("T A::operator()();", NoSpace);
13142   verifyFormat("X A::operator++(T);", NoSpace);
13143   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
13144 
13145   FormatStyle Space = getLLVMStyle();
13146   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
13147 
13148   verifyFormat("int f ();", Space);
13149   verifyFormat("void f (int a, T b) {\n"
13150                "  while (true)\n"
13151                "    continue;\n"
13152                "}",
13153                Space);
13154   verifyFormat("if (true)\n"
13155                "  f ();\n"
13156                "else if (true)\n"
13157                "  f ();",
13158                Space);
13159   verifyFormat("do {\n"
13160                "  do_something ();\n"
13161                "} while (something ());",
13162                Space);
13163   verifyFormat("switch (x) {\n"
13164                "default:\n"
13165                "  break;\n"
13166                "}",
13167                Space);
13168   verifyFormat("A::A () : a (1) {}", Space);
13169   verifyFormat("void f () __attribute__ ((asdf));", Space);
13170   verifyFormat("*(&a + 1);\n"
13171                "&((&a)[1]);\n"
13172                "a[(b + c) * d];\n"
13173                "(((a + 1) * 2) + 3) * 4;",
13174                Space);
13175   verifyFormat("#define A(x) x", Space);
13176   verifyFormat("#define A (x) x", Space);
13177   verifyFormat("#if defined(x)\n"
13178                "#endif",
13179                Space);
13180   verifyFormat("auto i = std::make_unique<int> (5);", Space);
13181   verifyFormat("size_t x = sizeof (x);", Space);
13182   verifyFormat("auto f (int x) -> decltype (x);", Space);
13183   verifyFormat("auto f (int x) -> typeof (x);", Space);
13184   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
13185   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
13186   verifyFormat("int f (T x) noexcept (x.create ());", Space);
13187   verifyFormat("alignas (128) char a[128];", Space);
13188   verifyFormat("size_t x = alignof (MyType);", Space);
13189   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
13190   verifyFormat("int f () throw (Deprecated);", Space);
13191   verifyFormat("typedef void (*cb) (int);", Space);
13192   verifyFormat("T A::operator() ();", Space);
13193   verifyFormat("X A::operator++ (T);", Space);
13194   verifyFormat("auto lambda = [] () { return 0; };", Space);
13195   verifyFormat("int x = int (y);", Space);
13196 
13197   FormatStyle SomeSpace = getLLVMStyle();
13198   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
13199 
13200   verifyFormat("[]() -> float {}", SomeSpace);
13201   verifyFormat("[] (auto foo) {}", SomeSpace);
13202   verifyFormat("[foo]() -> int {}", SomeSpace);
13203   verifyFormat("int f();", SomeSpace);
13204   verifyFormat("void f (int a, T b) {\n"
13205                "  while (true)\n"
13206                "    continue;\n"
13207                "}",
13208                SomeSpace);
13209   verifyFormat("if (true)\n"
13210                "  f();\n"
13211                "else if (true)\n"
13212                "  f();",
13213                SomeSpace);
13214   verifyFormat("do {\n"
13215                "  do_something();\n"
13216                "} while (something());",
13217                SomeSpace);
13218   verifyFormat("switch (x) {\n"
13219                "default:\n"
13220                "  break;\n"
13221                "}",
13222                SomeSpace);
13223   verifyFormat("A::A() : a (1) {}", SomeSpace);
13224   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
13225   verifyFormat("*(&a + 1);\n"
13226                "&((&a)[1]);\n"
13227                "a[(b + c) * d];\n"
13228                "(((a + 1) * 2) + 3) * 4;",
13229                SomeSpace);
13230   verifyFormat("#define A(x) x", SomeSpace);
13231   verifyFormat("#define A (x) x", SomeSpace);
13232   verifyFormat("#if defined(x)\n"
13233                "#endif",
13234                SomeSpace);
13235   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
13236   verifyFormat("size_t x = sizeof (x);", SomeSpace);
13237   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
13238   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
13239   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
13240   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
13241   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
13242   verifyFormat("alignas (128) char a[128];", SomeSpace);
13243   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
13244   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
13245                SomeSpace);
13246   verifyFormat("int f() throw (Deprecated);", SomeSpace);
13247   verifyFormat("typedef void (*cb) (int);", SomeSpace);
13248   verifyFormat("T A::operator()();", SomeSpace);
13249   verifyFormat("X A::operator++ (T);", SomeSpace);
13250   verifyFormat("int x = int (y);", SomeSpace);
13251   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
13252 }
13253 
13254 TEST_F(FormatTest, SpaceAfterLogicalNot) {
13255   FormatStyle Spaces = getLLVMStyle();
13256   Spaces.SpaceAfterLogicalNot = true;
13257 
13258   verifyFormat("bool x = ! y", Spaces);
13259   verifyFormat("if (! isFailure())", Spaces);
13260   verifyFormat("if (! (a && b))", Spaces);
13261   verifyFormat("\"Error!\"", Spaces);
13262   verifyFormat("! ! x", Spaces);
13263 }
13264 
13265 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
13266   FormatStyle Spaces = getLLVMStyle();
13267 
13268   Spaces.SpacesInParentheses = true;
13269   verifyFormat("do_something( ::globalVar );", Spaces);
13270   verifyFormat("call( x, y, z );", Spaces);
13271   verifyFormat("call();", Spaces);
13272   verifyFormat("std::function<void( int, int )> callback;", Spaces);
13273   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
13274                Spaces);
13275   verifyFormat("while ( (bool)1 )\n"
13276                "  continue;",
13277                Spaces);
13278   verifyFormat("for ( ;; )\n"
13279                "  continue;",
13280                Spaces);
13281   verifyFormat("if ( true )\n"
13282                "  f();\n"
13283                "else if ( true )\n"
13284                "  f();",
13285                Spaces);
13286   verifyFormat("do {\n"
13287                "  do_something( (int)i );\n"
13288                "} while ( something() );",
13289                Spaces);
13290   verifyFormat("switch ( x ) {\n"
13291                "default:\n"
13292                "  break;\n"
13293                "}",
13294                Spaces);
13295 
13296   Spaces.SpacesInParentheses = false;
13297   Spaces.SpacesInCStyleCastParentheses = true;
13298   verifyFormat("Type *A = ( Type * )P;", Spaces);
13299   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
13300   verifyFormat("x = ( int32 )y;", Spaces);
13301   verifyFormat("int a = ( int )(2.0f);", Spaces);
13302   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
13303   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
13304   verifyFormat("#define x (( int )-1)", Spaces);
13305 
13306   // Run the first set of tests again with:
13307   Spaces.SpacesInParentheses = false;
13308   Spaces.SpaceInEmptyParentheses = true;
13309   Spaces.SpacesInCStyleCastParentheses = true;
13310   verifyFormat("call(x, y, z);", Spaces);
13311   verifyFormat("call( );", Spaces);
13312   verifyFormat("std::function<void(int, int)> callback;", Spaces);
13313   verifyFormat("while (( bool )1)\n"
13314                "  continue;",
13315                Spaces);
13316   verifyFormat("for (;;)\n"
13317                "  continue;",
13318                Spaces);
13319   verifyFormat("if (true)\n"
13320                "  f( );\n"
13321                "else if (true)\n"
13322                "  f( );",
13323                Spaces);
13324   verifyFormat("do {\n"
13325                "  do_something(( int )i);\n"
13326                "} while (something( ));",
13327                Spaces);
13328   verifyFormat("switch (x) {\n"
13329                "default:\n"
13330                "  break;\n"
13331                "}",
13332                Spaces);
13333 
13334   // Run the first set of tests again with:
13335   Spaces.SpaceAfterCStyleCast = true;
13336   verifyFormat("call(x, y, z);", Spaces);
13337   verifyFormat("call( );", Spaces);
13338   verifyFormat("std::function<void(int, int)> callback;", Spaces);
13339   verifyFormat("while (( bool ) 1)\n"
13340                "  continue;",
13341                Spaces);
13342   verifyFormat("for (;;)\n"
13343                "  continue;",
13344                Spaces);
13345   verifyFormat("if (true)\n"
13346                "  f( );\n"
13347                "else if (true)\n"
13348                "  f( );",
13349                Spaces);
13350   verifyFormat("do {\n"
13351                "  do_something(( int ) i);\n"
13352                "} while (something( ));",
13353                Spaces);
13354   verifyFormat("switch (x) {\n"
13355                "default:\n"
13356                "  break;\n"
13357                "}",
13358                Spaces);
13359 
13360   // Run subset of tests again with:
13361   Spaces.SpacesInCStyleCastParentheses = false;
13362   Spaces.SpaceAfterCStyleCast = true;
13363   verifyFormat("while ((bool) 1)\n"
13364                "  continue;",
13365                Spaces);
13366   verifyFormat("do {\n"
13367                "  do_something((int) i);\n"
13368                "} while (something( ));",
13369                Spaces);
13370 
13371   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
13372   verifyFormat("size_t idx = (size_t) a;", Spaces);
13373   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
13374   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
13375   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
13376   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
13377   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
13378   Spaces.ColumnLimit = 80;
13379   Spaces.IndentWidth = 4;
13380   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13381   verifyFormat("void foo( ) {\n"
13382                "    size_t foo = (*(function))(\n"
13383                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
13384                "BarrrrrrrrrrrrLong,\n"
13385                "        FoooooooooLooooong);\n"
13386                "}",
13387                Spaces);
13388   Spaces.SpaceAfterCStyleCast = false;
13389   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
13390   verifyFormat("size_t idx = (size_t)a;", Spaces);
13391   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
13392   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
13393   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
13394   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
13395   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
13396 
13397   verifyFormat("void foo( ) {\n"
13398                "    size_t foo = (*(function))(\n"
13399                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
13400                "BarrrrrrrrrrrrLong,\n"
13401                "        FoooooooooLooooong);\n"
13402                "}",
13403                Spaces);
13404 }
13405 
13406 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
13407   verifyFormat("int a[5];");
13408   verifyFormat("a[3] += 42;");
13409 
13410   FormatStyle Spaces = getLLVMStyle();
13411   Spaces.SpacesInSquareBrackets = true;
13412   // Not lambdas.
13413   verifyFormat("int a[ 5 ];", Spaces);
13414   verifyFormat("a[ 3 ] += 42;", Spaces);
13415   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
13416   verifyFormat("double &operator[](int i) { return 0; }\n"
13417                "int i;",
13418                Spaces);
13419   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
13420   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
13421   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
13422   // Lambdas.
13423   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
13424   verifyFormat("return [ i, args... ] {};", Spaces);
13425   verifyFormat("int foo = [ &bar ]() {};", Spaces);
13426   verifyFormat("int foo = [ = ]() {};", Spaces);
13427   verifyFormat("int foo = [ & ]() {};", Spaces);
13428   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
13429   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
13430 }
13431 
13432 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
13433   FormatStyle NoSpaceStyle = getLLVMStyle();
13434   verifyFormat("int a[5];", NoSpaceStyle);
13435   verifyFormat("a[3] += 42;", NoSpaceStyle);
13436 
13437   verifyFormat("int a[1];", NoSpaceStyle);
13438   verifyFormat("int 1 [a];", NoSpaceStyle);
13439   verifyFormat("int a[1][2];", NoSpaceStyle);
13440   verifyFormat("a[7] = 5;", NoSpaceStyle);
13441   verifyFormat("int a = (f())[23];", NoSpaceStyle);
13442   verifyFormat("f([] {})", NoSpaceStyle);
13443 
13444   FormatStyle Space = getLLVMStyle();
13445   Space.SpaceBeforeSquareBrackets = true;
13446   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
13447   verifyFormat("return [i, args...] {};", Space);
13448 
13449   verifyFormat("int a [5];", Space);
13450   verifyFormat("a [3] += 42;", Space);
13451   verifyFormat("constexpr char hello []{\"hello\"};", Space);
13452   verifyFormat("double &operator[](int i) { return 0; }\n"
13453                "int i;",
13454                Space);
13455   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
13456   verifyFormat("int i = a [a][a]->f();", Space);
13457   verifyFormat("int i = (*b) [a]->f();", Space);
13458 
13459   verifyFormat("int a [1];", Space);
13460   verifyFormat("int 1 [a];", Space);
13461   verifyFormat("int a [1][2];", Space);
13462   verifyFormat("a [7] = 5;", Space);
13463   verifyFormat("int a = (f()) [23];", Space);
13464   verifyFormat("f([] {})", Space);
13465 }
13466 
13467 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
13468   verifyFormat("int a = 5;");
13469   verifyFormat("a += 42;");
13470   verifyFormat("a or_eq 8;");
13471 
13472   FormatStyle Spaces = getLLVMStyle();
13473   Spaces.SpaceBeforeAssignmentOperators = false;
13474   verifyFormat("int a= 5;", Spaces);
13475   verifyFormat("a+= 42;", Spaces);
13476   verifyFormat("a or_eq 8;", Spaces);
13477 }
13478 
13479 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
13480   verifyFormat("class Foo : public Bar {};");
13481   verifyFormat("Foo::Foo() : foo(1) {}");
13482   verifyFormat("for (auto a : b) {\n}");
13483   verifyFormat("int x = a ? b : c;");
13484   verifyFormat("{\n"
13485                "label0:\n"
13486                "  int x = 0;\n"
13487                "}");
13488   verifyFormat("switch (x) {\n"
13489                "case 1:\n"
13490                "default:\n"
13491                "}");
13492   verifyFormat("switch (allBraces) {\n"
13493                "case 1: {\n"
13494                "  break;\n"
13495                "}\n"
13496                "case 2: {\n"
13497                "  [[fallthrough]];\n"
13498                "}\n"
13499                "default: {\n"
13500                "  break;\n"
13501                "}\n"
13502                "}");
13503 
13504   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
13505   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
13506   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
13507   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
13508   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
13509   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
13510   verifyFormat("{\n"
13511                "label1:\n"
13512                "  int x = 0;\n"
13513                "}",
13514                CtorInitializerStyle);
13515   verifyFormat("switch (x) {\n"
13516                "case 1:\n"
13517                "default:\n"
13518                "}",
13519                CtorInitializerStyle);
13520   verifyFormat("switch (allBraces) {\n"
13521                "case 1: {\n"
13522                "  break;\n"
13523                "}\n"
13524                "case 2: {\n"
13525                "  [[fallthrough]];\n"
13526                "}\n"
13527                "default: {\n"
13528                "  break;\n"
13529                "}\n"
13530                "}",
13531                CtorInitializerStyle);
13532   CtorInitializerStyle.BreakConstructorInitializers =
13533       FormatStyle::BCIS_AfterColon;
13534   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
13535                "    aaaaaaaaaaaaaaaa(1),\n"
13536                "    bbbbbbbbbbbbbbbb(2) {}",
13537                CtorInitializerStyle);
13538   CtorInitializerStyle.BreakConstructorInitializers =
13539       FormatStyle::BCIS_BeforeComma;
13540   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
13541                "    : aaaaaaaaaaaaaaaa(1)\n"
13542                "    , bbbbbbbbbbbbbbbb(2) {}",
13543                CtorInitializerStyle);
13544   CtorInitializerStyle.BreakConstructorInitializers =
13545       FormatStyle::BCIS_BeforeColon;
13546   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
13547                "    : aaaaaaaaaaaaaaaa(1),\n"
13548                "      bbbbbbbbbbbbbbbb(2) {}",
13549                CtorInitializerStyle);
13550   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
13551   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
13552                ": aaaaaaaaaaaaaaaa(1),\n"
13553                "  bbbbbbbbbbbbbbbb(2) {}",
13554                CtorInitializerStyle);
13555 
13556   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
13557   InheritanceStyle.SpaceBeforeInheritanceColon = false;
13558   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
13559   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
13560   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
13561   verifyFormat("int x = a ? b : c;", InheritanceStyle);
13562   verifyFormat("{\n"
13563                "label2:\n"
13564                "  int x = 0;\n"
13565                "}",
13566                InheritanceStyle);
13567   verifyFormat("switch (x) {\n"
13568                "case 1:\n"
13569                "default:\n"
13570                "}",
13571                InheritanceStyle);
13572   verifyFormat("switch (allBraces) {\n"
13573                "case 1: {\n"
13574                "  break;\n"
13575                "}\n"
13576                "case 2: {\n"
13577                "  [[fallthrough]];\n"
13578                "}\n"
13579                "default: {\n"
13580                "  break;\n"
13581                "}\n"
13582                "}",
13583                InheritanceStyle);
13584   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
13585   verifyFormat("class Foooooooooooooooooooooo\n"
13586                "    : public aaaaaaaaaaaaaaaaaa,\n"
13587                "      public bbbbbbbbbbbbbbbbbb {\n"
13588                "}",
13589                InheritanceStyle);
13590   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
13591   verifyFormat("class Foooooooooooooooooooooo:\n"
13592                "    public aaaaaaaaaaaaaaaaaa,\n"
13593                "    public bbbbbbbbbbbbbbbbbb {\n"
13594                "}",
13595                InheritanceStyle);
13596   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
13597   verifyFormat("class Foooooooooooooooooooooo\n"
13598                "    : public aaaaaaaaaaaaaaaaaa\n"
13599                "    , public bbbbbbbbbbbbbbbbbb {\n"
13600                "}",
13601                InheritanceStyle);
13602   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
13603   verifyFormat("class Foooooooooooooooooooooo\n"
13604                "    : public aaaaaaaaaaaaaaaaaa,\n"
13605                "      public bbbbbbbbbbbbbbbbbb {\n"
13606                "}",
13607                InheritanceStyle);
13608   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
13609   verifyFormat("class Foooooooooooooooooooooo\n"
13610                ": public aaaaaaaaaaaaaaaaaa,\n"
13611                "  public bbbbbbbbbbbbbbbbbb {}",
13612                InheritanceStyle);
13613 
13614   FormatStyle ForLoopStyle = getLLVMStyle();
13615   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
13616   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
13617   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
13618   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
13619   verifyFormat("int x = a ? b : c;", ForLoopStyle);
13620   verifyFormat("{\n"
13621                "label2:\n"
13622                "  int x = 0;\n"
13623                "}",
13624                ForLoopStyle);
13625   verifyFormat("switch (x) {\n"
13626                "case 1:\n"
13627                "default:\n"
13628                "}",
13629                ForLoopStyle);
13630   verifyFormat("switch (allBraces) {\n"
13631                "case 1: {\n"
13632                "  break;\n"
13633                "}\n"
13634                "case 2: {\n"
13635                "  [[fallthrough]];\n"
13636                "}\n"
13637                "default: {\n"
13638                "  break;\n"
13639                "}\n"
13640                "}",
13641                ForLoopStyle);
13642 
13643   FormatStyle CaseStyle = getLLVMStyle();
13644   CaseStyle.SpaceBeforeCaseColon = true;
13645   verifyFormat("class Foo : public Bar {};", CaseStyle);
13646   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
13647   verifyFormat("for (auto a : b) {\n}", CaseStyle);
13648   verifyFormat("int x = a ? b : c;", CaseStyle);
13649   verifyFormat("switch (x) {\n"
13650                "case 1 :\n"
13651                "default :\n"
13652                "}",
13653                CaseStyle);
13654   verifyFormat("switch (allBraces) {\n"
13655                "case 1 : {\n"
13656                "  break;\n"
13657                "}\n"
13658                "case 2 : {\n"
13659                "  [[fallthrough]];\n"
13660                "}\n"
13661                "default : {\n"
13662                "  break;\n"
13663                "}\n"
13664                "}",
13665                CaseStyle);
13666 
13667   FormatStyle NoSpaceStyle = getLLVMStyle();
13668   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
13669   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
13670   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
13671   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
13672   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
13673   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
13674   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
13675   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
13676   verifyFormat("{\n"
13677                "label3:\n"
13678                "  int x = 0;\n"
13679                "}",
13680                NoSpaceStyle);
13681   verifyFormat("switch (x) {\n"
13682                "case 1:\n"
13683                "default:\n"
13684                "}",
13685                NoSpaceStyle);
13686   verifyFormat("switch (allBraces) {\n"
13687                "case 1: {\n"
13688                "  break;\n"
13689                "}\n"
13690                "case 2: {\n"
13691                "  [[fallthrough]];\n"
13692                "}\n"
13693                "default: {\n"
13694                "  break;\n"
13695                "}\n"
13696                "}",
13697                NoSpaceStyle);
13698 
13699   FormatStyle InvertedSpaceStyle = getLLVMStyle();
13700   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
13701   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
13702   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
13703   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
13704   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
13705   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
13706   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
13707   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
13708   verifyFormat("{\n"
13709                "label3:\n"
13710                "  int x = 0;\n"
13711                "}",
13712                InvertedSpaceStyle);
13713   verifyFormat("switch (x) {\n"
13714                "case 1 :\n"
13715                "case 2 : {\n"
13716                "  break;\n"
13717                "}\n"
13718                "default :\n"
13719                "  break;\n"
13720                "}",
13721                InvertedSpaceStyle);
13722   verifyFormat("switch (allBraces) {\n"
13723                "case 1 : {\n"
13724                "  break;\n"
13725                "}\n"
13726                "case 2 : {\n"
13727                "  [[fallthrough]];\n"
13728                "}\n"
13729                "default : {\n"
13730                "  break;\n"
13731                "}\n"
13732                "}",
13733                InvertedSpaceStyle);
13734 }
13735 
13736 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
13737   FormatStyle Style = getLLVMStyle();
13738 
13739   Style.PointerAlignment = FormatStyle::PAS_Left;
13740   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
13741   verifyFormat("void* const* x = NULL;", Style);
13742 
13743 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
13744   do {                                                                         \
13745     Style.PointerAlignment = FormatStyle::Pointers;                            \
13746     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
13747     verifyFormat(Code, Style);                                                 \
13748   } while (false)
13749 
13750   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
13751   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
13752   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
13753 
13754   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
13755   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
13756   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
13757 
13758   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
13759   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
13760   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
13761 
13762   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
13763   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
13764   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
13765 
13766   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
13767   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
13768                         SAPQ_Default);
13769   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
13770                         SAPQ_Default);
13771 
13772   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
13773   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
13774                         SAPQ_Before);
13775   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
13776                         SAPQ_Before);
13777 
13778   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
13779   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
13780   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
13781                         SAPQ_After);
13782 
13783   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
13784   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
13785   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
13786 
13787 #undef verifyQualifierSpaces
13788 
13789   FormatStyle Spaces = getLLVMStyle();
13790   Spaces.AttributeMacros.push_back("qualified");
13791   Spaces.PointerAlignment = FormatStyle::PAS_Right;
13792   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
13793   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
13794   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
13795   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
13796   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
13797   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13798   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
13799   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
13800   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
13801   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
13802   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
13803   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13804 
13805   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
13806   Spaces.PointerAlignment = FormatStyle::PAS_Left;
13807   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
13808   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
13809   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
13810   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
13811   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
13812   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13813   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
13814   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
13815   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
13816   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
13817   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
13818   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
13819   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13820 
13821   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
13822   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
13823   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
13824   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
13825   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
13826   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
13827   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
13828   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13829 }
13830 
13831 TEST_F(FormatTest, AlignConsecutiveMacros) {
13832   FormatStyle Style = getLLVMStyle();
13833   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13834   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13835   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
13836 
13837   verifyFormat("#define a 3\n"
13838                "#define bbbb 4\n"
13839                "#define ccc (5)",
13840                Style);
13841 
13842   verifyFormat("#define f(x) (x * x)\n"
13843                "#define fff(x, y, z) (x * y + z)\n"
13844                "#define ffff(x, y) (x - y)",
13845                Style);
13846 
13847   verifyFormat("#define foo(x, y) (x + y)\n"
13848                "#define bar (5, 6)(2 + 2)",
13849                Style);
13850 
13851   verifyFormat("#define a 3\n"
13852                "#define bbbb 4\n"
13853                "#define ccc (5)\n"
13854                "#define f(x) (x * x)\n"
13855                "#define fff(x, y, z) (x * y + z)\n"
13856                "#define ffff(x, y) (x - y)",
13857                Style);
13858 
13859   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
13860   verifyFormat("#define a    3\n"
13861                "#define bbbb 4\n"
13862                "#define ccc  (5)",
13863                Style);
13864 
13865   verifyFormat("#define f(x)         (x * x)\n"
13866                "#define fff(x, y, z) (x * y + z)\n"
13867                "#define ffff(x, y)   (x - y)",
13868                Style);
13869 
13870   verifyFormat("#define foo(x, y) (x + y)\n"
13871                "#define bar       (5, 6)(2 + 2)",
13872                Style);
13873 
13874   verifyFormat("#define a            3\n"
13875                "#define bbbb         4\n"
13876                "#define ccc          (5)\n"
13877                "#define f(x)         (x * x)\n"
13878                "#define fff(x, y, z) (x * y + z)\n"
13879                "#define ffff(x, y)   (x - y)",
13880                Style);
13881 
13882   verifyFormat("#define a         5\n"
13883                "#define foo(x, y) (x + y)\n"
13884                "#define CCC       (6)\n"
13885                "auto lambda = []() {\n"
13886                "  auto  ii = 0;\n"
13887                "  float j  = 0;\n"
13888                "  return 0;\n"
13889                "};\n"
13890                "int   i  = 0;\n"
13891                "float i2 = 0;\n"
13892                "auto  v  = type{\n"
13893                "    i = 1,   //\n"
13894                "    (i = 2), //\n"
13895                "    i = 3    //\n"
13896                "};",
13897                Style);
13898 
13899   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
13900   Style.ColumnLimit = 20;
13901 
13902   verifyFormat("#define a          \\\n"
13903                "  \"aabbbbbbbbbbbb\"\n"
13904                "#define D          \\\n"
13905                "  \"aabbbbbbbbbbbb\" \\\n"
13906                "  \"ccddeeeeeeeee\"\n"
13907                "#define B          \\\n"
13908                "  \"QQQQQQQQQQQQQ\"  \\\n"
13909                "  \"FFFFFFFFFFFFF\"  \\\n"
13910                "  \"LLLLLLLL\"\n",
13911                Style);
13912 
13913   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
13914   verifyFormat("#define a          \\\n"
13915                "  \"aabbbbbbbbbbbb\"\n"
13916                "#define D          \\\n"
13917                "  \"aabbbbbbbbbbbb\" \\\n"
13918                "  \"ccddeeeeeeeee\"\n"
13919                "#define B          \\\n"
13920                "  \"QQQQQQQQQQQQQ\"  \\\n"
13921                "  \"FFFFFFFFFFFFF\"  \\\n"
13922                "  \"LLLLLLLL\"\n",
13923                Style);
13924 
13925   // Test across comments
13926   Style.MaxEmptyLinesToKeep = 10;
13927   Style.ReflowComments = false;
13928   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
13929   EXPECT_EQ("#define a    3\n"
13930             "// line comment\n"
13931             "#define bbbb 4\n"
13932             "#define ccc  (5)",
13933             format("#define a 3\n"
13934                    "// line comment\n"
13935                    "#define bbbb 4\n"
13936                    "#define ccc (5)",
13937                    Style));
13938 
13939   EXPECT_EQ("#define a    3\n"
13940             "/* block comment */\n"
13941             "#define bbbb 4\n"
13942             "#define ccc  (5)",
13943             format("#define a  3\n"
13944                    "/* block comment */\n"
13945                    "#define bbbb 4\n"
13946                    "#define ccc (5)",
13947                    Style));
13948 
13949   EXPECT_EQ("#define a    3\n"
13950             "/* multi-line *\n"
13951             " * block comment */\n"
13952             "#define bbbb 4\n"
13953             "#define ccc  (5)",
13954             format("#define a 3\n"
13955                    "/* multi-line *\n"
13956                    " * block comment */\n"
13957                    "#define bbbb 4\n"
13958                    "#define ccc (5)",
13959                    Style));
13960 
13961   EXPECT_EQ("#define a    3\n"
13962             "// multi-line line comment\n"
13963             "//\n"
13964             "#define bbbb 4\n"
13965             "#define ccc  (5)",
13966             format("#define a  3\n"
13967                    "// multi-line line comment\n"
13968                    "//\n"
13969                    "#define bbbb 4\n"
13970                    "#define ccc (5)",
13971                    Style));
13972 
13973   EXPECT_EQ("#define a 3\n"
13974             "// empty lines still break.\n"
13975             "\n"
13976             "#define bbbb 4\n"
13977             "#define ccc  (5)",
13978             format("#define a     3\n"
13979                    "// empty lines still break.\n"
13980                    "\n"
13981                    "#define bbbb     4\n"
13982                    "#define ccc  (5)",
13983                    Style));
13984 
13985   // Test across empty lines
13986   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
13987   EXPECT_EQ("#define a    3\n"
13988             "\n"
13989             "#define bbbb 4\n"
13990             "#define ccc  (5)",
13991             format("#define a 3\n"
13992                    "\n"
13993                    "#define bbbb 4\n"
13994                    "#define ccc (5)",
13995                    Style));
13996 
13997   EXPECT_EQ("#define a    3\n"
13998             "\n"
13999             "\n"
14000             "\n"
14001             "#define bbbb 4\n"
14002             "#define ccc  (5)",
14003             format("#define a        3\n"
14004                    "\n"
14005                    "\n"
14006                    "\n"
14007                    "#define bbbb 4\n"
14008                    "#define ccc (5)",
14009                    Style));
14010 
14011   EXPECT_EQ("#define a 3\n"
14012             "// comments should break alignment\n"
14013             "//\n"
14014             "#define bbbb 4\n"
14015             "#define ccc  (5)",
14016             format("#define a        3\n"
14017                    "// comments should break alignment\n"
14018                    "//\n"
14019                    "#define bbbb 4\n"
14020                    "#define ccc (5)",
14021                    Style));
14022 
14023   // Test across empty lines and comments
14024   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
14025   verifyFormat("#define a    3\n"
14026                "\n"
14027                "// line comment\n"
14028                "#define bbbb 4\n"
14029                "#define ccc  (5)",
14030                Style);
14031 
14032   EXPECT_EQ("#define a    3\n"
14033             "\n"
14034             "\n"
14035             "/* multi-line *\n"
14036             " * block comment */\n"
14037             "\n"
14038             "\n"
14039             "#define bbbb 4\n"
14040             "#define ccc  (5)",
14041             format("#define a 3\n"
14042                    "\n"
14043                    "\n"
14044                    "/* multi-line *\n"
14045                    " * block comment */\n"
14046                    "\n"
14047                    "\n"
14048                    "#define bbbb 4\n"
14049                    "#define ccc (5)",
14050                    Style));
14051 
14052   EXPECT_EQ("#define a    3\n"
14053             "\n"
14054             "\n"
14055             "/* multi-line *\n"
14056             " * block comment */\n"
14057             "\n"
14058             "\n"
14059             "#define bbbb 4\n"
14060             "#define ccc  (5)",
14061             format("#define a 3\n"
14062                    "\n"
14063                    "\n"
14064                    "/* multi-line *\n"
14065                    " * block comment */\n"
14066                    "\n"
14067                    "\n"
14068                    "#define bbbb 4\n"
14069                    "#define ccc       (5)",
14070                    Style));
14071 }
14072 
14073 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
14074   FormatStyle Alignment = getLLVMStyle();
14075   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14076   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
14077 
14078   Alignment.MaxEmptyLinesToKeep = 10;
14079   /* Test alignment across empty lines */
14080   EXPECT_EQ("int a           = 5;\n"
14081             "\n"
14082             "int oneTwoThree = 123;",
14083             format("int a       = 5;\n"
14084                    "\n"
14085                    "int oneTwoThree= 123;",
14086                    Alignment));
14087   EXPECT_EQ("int a           = 5;\n"
14088             "int one         = 1;\n"
14089             "\n"
14090             "int oneTwoThree = 123;",
14091             format("int a = 5;\n"
14092                    "int one = 1;\n"
14093                    "\n"
14094                    "int oneTwoThree = 123;",
14095                    Alignment));
14096   EXPECT_EQ("int a           = 5;\n"
14097             "int one         = 1;\n"
14098             "\n"
14099             "int oneTwoThree = 123;\n"
14100             "int oneTwo      = 12;",
14101             format("int a = 5;\n"
14102                    "int one = 1;\n"
14103                    "\n"
14104                    "int oneTwoThree = 123;\n"
14105                    "int oneTwo = 12;",
14106                    Alignment));
14107 
14108   /* Test across comments */
14109   EXPECT_EQ("int a = 5;\n"
14110             "/* block comment */\n"
14111             "int oneTwoThree = 123;",
14112             format("int a = 5;\n"
14113                    "/* block comment */\n"
14114                    "int oneTwoThree=123;",
14115                    Alignment));
14116 
14117   EXPECT_EQ("int a = 5;\n"
14118             "// line comment\n"
14119             "int oneTwoThree = 123;",
14120             format("int a = 5;\n"
14121                    "// line comment\n"
14122                    "int oneTwoThree=123;",
14123                    Alignment));
14124 
14125   /* Test across comments and newlines */
14126   EXPECT_EQ("int a = 5;\n"
14127             "\n"
14128             "/* block comment */\n"
14129             "int oneTwoThree = 123;",
14130             format("int a = 5;\n"
14131                    "\n"
14132                    "/* block comment */\n"
14133                    "int oneTwoThree=123;",
14134                    Alignment));
14135 
14136   EXPECT_EQ("int a = 5;\n"
14137             "\n"
14138             "// line comment\n"
14139             "int oneTwoThree = 123;",
14140             format("int a = 5;\n"
14141                    "\n"
14142                    "// line comment\n"
14143                    "int oneTwoThree=123;",
14144                    Alignment));
14145 }
14146 
14147 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
14148   FormatStyle Alignment = getLLVMStyle();
14149   Alignment.AlignConsecutiveDeclarations =
14150       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14151   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14152 
14153   Alignment.MaxEmptyLinesToKeep = 10;
14154   /* Test alignment across empty lines */
14155   EXPECT_EQ("int         a = 5;\n"
14156             "\n"
14157             "float const oneTwoThree = 123;",
14158             format("int a = 5;\n"
14159                    "\n"
14160                    "float const oneTwoThree = 123;",
14161                    Alignment));
14162   EXPECT_EQ("int         a = 5;\n"
14163             "float const one = 1;\n"
14164             "\n"
14165             "int         oneTwoThree = 123;",
14166             format("int a = 5;\n"
14167                    "float const one = 1;\n"
14168                    "\n"
14169                    "int oneTwoThree = 123;",
14170                    Alignment));
14171 
14172   /* Test across comments */
14173   EXPECT_EQ("float const a = 5;\n"
14174             "/* block comment */\n"
14175             "int         oneTwoThree = 123;",
14176             format("float const a = 5;\n"
14177                    "/* block comment */\n"
14178                    "int oneTwoThree=123;",
14179                    Alignment));
14180 
14181   EXPECT_EQ("float const a = 5;\n"
14182             "// line comment\n"
14183             "int         oneTwoThree = 123;",
14184             format("float const a = 5;\n"
14185                    "// line comment\n"
14186                    "int oneTwoThree=123;",
14187                    Alignment));
14188 
14189   /* Test across comments and newlines */
14190   EXPECT_EQ("float const a = 5;\n"
14191             "\n"
14192             "/* block comment */\n"
14193             "int         oneTwoThree = 123;",
14194             format("float const a = 5;\n"
14195                    "\n"
14196                    "/* block comment */\n"
14197                    "int         oneTwoThree=123;",
14198                    Alignment));
14199 
14200   EXPECT_EQ("float const a = 5;\n"
14201             "\n"
14202             "// line comment\n"
14203             "int         oneTwoThree = 123;",
14204             format("float const a = 5;\n"
14205                    "\n"
14206                    "// line comment\n"
14207                    "int oneTwoThree=123;",
14208                    Alignment));
14209 }
14210 
14211 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
14212   FormatStyle Alignment = getLLVMStyle();
14213   Alignment.AlignConsecutiveBitFields =
14214       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14215 
14216   Alignment.MaxEmptyLinesToKeep = 10;
14217   /* Test alignment across empty lines */
14218   EXPECT_EQ("int a            : 5;\n"
14219             "\n"
14220             "int longbitfield : 6;",
14221             format("int a : 5;\n"
14222                    "\n"
14223                    "int longbitfield : 6;",
14224                    Alignment));
14225   EXPECT_EQ("int a            : 5;\n"
14226             "int one          : 1;\n"
14227             "\n"
14228             "int longbitfield : 6;",
14229             format("int a : 5;\n"
14230                    "int one : 1;\n"
14231                    "\n"
14232                    "int longbitfield : 6;",
14233                    Alignment));
14234 
14235   /* Test across comments */
14236   EXPECT_EQ("int a            : 5;\n"
14237             "/* block comment */\n"
14238             "int longbitfield : 6;",
14239             format("int a : 5;\n"
14240                    "/* block comment */\n"
14241                    "int longbitfield : 6;",
14242                    Alignment));
14243   EXPECT_EQ("int a            : 5;\n"
14244             "int one          : 1;\n"
14245             "// line comment\n"
14246             "int longbitfield : 6;",
14247             format("int a : 5;\n"
14248                    "int one : 1;\n"
14249                    "// line comment\n"
14250                    "int longbitfield : 6;",
14251                    Alignment));
14252 
14253   /* Test across comments and newlines */
14254   EXPECT_EQ("int a            : 5;\n"
14255             "/* block comment */\n"
14256             "\n"
14257             "int longbitfield : 6;",
14258             format("int a : 5;\n"
14259                    "/* block comment */\n"
14260                    "\n"
14261                    "int longbitfield : 6;",
14262                    Alignment));
14263   EXPECT_EQ("int a            : 5;\n"
14264             "int one          : 1;\n"
14265             "\n"
14266             "// line comment\n"
14267             "\n"
14268             "int longbitfield : 6;",
14269             format("int a : 5;\n"
14270                    "int one : 1;\n"
14271                    "\n"
14272                    "// line comment \n"
14273                    "\n"
14274                    "int longbitfield : 6;",
14275                    Alignment));
14276 }
14277 
14278 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
14279   FormatStyle Alignment = getLLVMStyle();
14280   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14281   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
14282 
14283   Alignment.MaxEmptyLinesToKeep = 10;
14284   /* Test alignment across empty lines */
14285   EXPECT_EQ("int a = 5;\n"
14286             "\n"
14287             "int oneTwoThree = 123;",
14288             format("int a       = 5;\n"
14289                    "\n"
14290                    "int oneTwoThree= 123;",
14291                    Alignment));
14292   EXPECT_EQ("int a   = 5;\n"
14293             "int one = 1;\n"
14294             "\n"
14295             "int oneTwoThree = 123;",
14296             format("int a = 5;\n"
14297                    "int one = 1;\n"
14298                    "\n"
14299                    "int oneTwoThree = 123;",
14300                    Alignment));
14301 
14302   /* Test across comments */
14303   EXPECT_EQ("int a           = 5;\n"
14304             "/* block comment */\n"
14305             "int oneTwoThree = 123;",
14306             format("int a = 5;\n"
14307                    "/* block comment */\n"
14308                    "int oneTwoThree=123;",
14309                    Alignment));
14310 
14311   EXPECT_EQ("int a           = 5;\n"
14312             "// line comment\n"
14313             "int oneTwoThree = 123;",
14314             format("int a = 5;\n"
14315                    "// line comment\n"
14316                    "int oneTwoThree=123;",
14317                    Alignment));
14318 
14319   EXPECT_EQ("int a           = 5;\n"
14320             "/*\n"
14321             " * multi-line block comment\n"
14322             " */\n"
14323             "int oneTwoThree = 123;",
14324             format("int a = 5;\n"
14325                    "/*\n"
14326                    " * multi-line block comment\n"
14327                    " */\n"
14328                    "int oneTwoThree=123;",
14329                    Alignment));
14330 
14331   EXPECT_EQ("int a           = 5;\n"
14332             "//\n"
14333             "// multi-line line comment\n"
14334             "//\n"
14335             "int oneTwoThree = 123;",
14336             format("int a = 5;\n"
14337                    "//\n"
14338                    "// multi-line line comment\n"
14339                    "//\n"
14340                    "int oneTwoThree=123;",
14341                    Alignment));
14342 
14343   /* Test across comments and newlines */
14344   EXPECT_EQ("int a = 5;\n"
14345             "\n"
14346             "/* block comment */\n"
14347             "int oneTwoThree = 123;",
14348             format("int a = 5;\n"
14349                    "\n"
14350                    "/* block comment */\n"
14351                    "int oneTwoThree=123;",
14352                    Alignment));
14353 
14354   EXPECT_EQ("int a = 5;\n"
14355             "\n"
14356             "// line comment\n"
14357             "int oneTwoThree = 123;",
14358             format("int a = 5;\n"
14359                    "\n"
14360                    "// line comment\n"
14361                    "int oneTwoThree=123;",
14362                    Alignment));
14363 }
14364 
14365 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
14366   FormatStyle Alignment = getLLVMStyle();
14367   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14368   Alignment.AlignConsecutiveAssignments =
14369       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14370   verifyFormat("int a           = 5;\n"
14371                "int oneTwoThree = 123;",
14372                Alignment);
14373   verifyFormat("int a           = method();\n"
14374                "int oneTwoThree = 133;",
14375                Alignment);
14376   verifyFormat("a &= 5;\n"
14377                "bcd *= 5;\n"
14378                "ghtyf += 5;\n"
14379                "dvfvdb -= 5;\n"
14380                "a /= 5;\n"
14381                "vdsvsv %= 5;\n"
14382                "sfdbddfbdfbb ^= 5;\n"
14383                "dvsdsv |= 5;\n"
14384                "int dsvvdvsdvvv = 123;",
14385                Alignment);
14386   verifyFormat("int i = 1, j = 10;\n"
14387                "something = 2000;",
14388                Alignment);
14389   verifyFormat("something = 2000;\n"
14390                "int i = 1, j = 10;\n",
14391                Alignment);
14392   verifyFormat("something = 2000;\n"
14393                "another   = 911;\n"
14394                "int i = 1, j = 10;\n"
14395                "oneMore = 1;\n"
14396                "i       = 2;",
14397                Alignment);
14398   verifyFormat("int a   = 5;\n"
14399                "int one = 1;\n"
14400                "method();\n"
14401                "int oneTwoThree = 123;\n"
14402                "int oneTwo      = 12;",
14403                Alignment);
14404   verifyFormat("int oneTwoThree = 123;\n"
14405                "int oneTwo      = 12;\n"
14406                "method();\n",
14407                Alignment);
14408   verifyFormat("int oneTwoThree = 123; // comment\n"
14409                "int oneTwo      = 12;  // comment",
14410                Alignment);
14411 
14412   // Bug 25167
14413   /* Uncomment when fixed
14414     verifyFormat("#if A\n"
14415                  "#else\n"
14416                  "int aaaaaaaa = 12;\n"
14417                  "#endif\n"
14418                  "#if B\n"
14419                  "#else\n"
14420                  "int a = 12;\n"
14421                  "#endif\n",
14422                  Alignment);
14423     verifyFormat("enum foo {\n"
14424                  "#if A\n"
14425                  "#else\n"
14426                  "  aaaaaaaa = 12;\n"
14427                  "#endif\n"
14428                  "#if B\n"
14429                  "#else\n"
14430                  "  a = 12;\n"
14431                  "#endif\n"
14432                  "};\n",
14433                  Alignment);
14434   */
14435 
14436   Alignment.MaxEmptyLinesToKeep = 10;
14437   /* Test alignment across empty lines */
14438   EXPECT_EQ("int a           = 5;\n"
14439             "\n"
14440             "int oneTwoThree = 123;",
14441             format("int a       = 5;\n"
14442                    "\n"
14443                    "int oneTwoThree= 123;",
14444                    Alignment));
14445   EXPECT_EQ("int a           = 5;\n"
14446             "int one         = 1;\n"
14447             "\n"
14448             "int oneTwoThree = 123;",
14449             format("int a = 5;\n"
14450                    "int one = 1;\n"
14451                    "\n"
14452                    "int oneTwoThree = 123;",
14453                    Alignment));
14454   EXPECT_EQ("int a           = 5;\n"
14455             "int one         = 1;\n"
14456             "\n"
14457             "int oneTwoThree = 123;\n"
14458             "int oneTwo      = 12;",
14459             format("int a = 5;\n"
14460                    "int one = 1;\n"
14461                    "\n"
14462                    "int oneTwoThree = 123;\n"
14463                    "int oneTwo = 12;",
14464                    Alignment));
14465 
14466   /* Test across comments */
14467   EXPECT_EQ("int a           = 5;\n"
14468             "/* block comment */\n"
14469             "int oneTwoThree = 123;",
14470             format("int a = 5;\n"
14471                    "/* block comment */\n"
14472                    "int oneTwoThree=123;",
14473                    Alignment));
14474 
14475   EXPECT_EQ("int a           = 5;\n"
14476             "// line comment\n"
14477             "int oneTwoThree = 123;",
14478             format("int a = 5;\n"
14479                    "// line comment\n"
14480                    "int oneTwoThree=123;",
14481                    Alignment));
14482 
14483   /* Test across comments and newlines */
14484   EXPECT_EQ("int a           = 5;\n"
14485             "\n"
14486             "/* block comment */\n"
14487             "int oneTwoThree = 123;",
14488             format("int a = 5;\n"
14489                    "\n"
14490                    "/* block comment */\n"
14491                    "int oneTwoThree=123;",
14492                    Alignment));
14493 
14494   EXPECT_EQ("int a           = 5;\n"
14495             "\n"
14496             "// line comment\n"
14497             "int oneTwoThree = 123;",
14498             format("int a = 5;\n"
14499                    "\n"
14500                    "// line comment\n"
14501                    "int oneTwoThree=123;",
14502                    Alignment));
14503 
14504   EXPECT_EQ("int a           = 5;\n"
14505             "//\n"
14506             "// multi-line line comment\n"
14507             "//\n"
14508             "int oneTwoThree = 123;",
14509             format("int a = 5;\n"
14510                    "//\n"
14511                    "// multi-line line comment\n"
14512                    "//\n"
14513                    "int oneTwoThree=123;",
14514                    Alignment));
14515 
14516   EXPECT_EQ("int a           = 5;\n"
14517             "/*\n"
14518             " *  multi-line block comment\n"
14519             " */\n"
14520             "int oneTwoThree = 123;",
14521             format("int a = 5;\n"
14522                    "/*\n"
14523                    " *  multi-line block comment\n"
14524                    " */\n"
14525                    "int oneTwoThree=123;",
14526                    Alignment));
14527 
14528   EXPECT_EQ("int a           = 5;\n"
14529             "\n"
14530             "/* block comment */\n"
14531             "\n"
14532             "\n"
14533             "\n"
14534             "int oneTwoThree = 123;",
14535             format("int a = 5;\n"
14536                    "\n"
14537                    "/* block comment */\n"
14538                    "\n"
14539                    "\n"
14540                    "\n"
14541                    "int oneTwoThree=123;",
14542                    Alignment));
14543 
14544   EXPECT_EQ("int a           = 5;\n"
14545             "\n"
14546             "// line comment\n"
14547             "\n"
14548             "\n"
14549             "\n"
14550             "int oneTwoThree = 123;",
14551             format("int a = 5;\n"
14552                    "\n"
14553                    "// line comment\n"
14554                    "\n"
14555                    "\n"
14556                    "\n"
14557                    "int oneTwoThree=123;",
14558                    Alignment));
14559 
14560   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
14561   verifyFormat("#define A \\\n"
14562                "  int aaaa       = 12; \\\n"
14563                "  int b          = 23; \\\n"
14564                "  int ccc        = 234; \\\n"
14565                "  int dddddddddd = 2345;",
14566                Alignment);
14567   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14568   verifyFormat("#define A               \\\n"
14569                "  int aaaa       = 12;  \\\n"
14570                "  int b          = 23;  \\\n"
14571                "  int ccc        = 234; \\\n"
14572                "  int dddddddddd = 2345;",
14573                Alignment);
14574   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
14575   verifyFormat("#define A                                                      "
14576                "                \\\n"
14577                "  int aaaa       = 12;                                         "
14578                "                \\\n"
14579                "  int b          = 23;                                         "
14580                "                \\\n"
14581                "  int ccc        = 234;                                        "
14582                "                \\\n"
14583                "  int dddddddddd = 2345;",
14584                Alignment);
14585   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
14586                "k = 4, int l = 5,\n"
14587                "                  int m = 6) {\n"
14588                "  int j      = 10;\n"
14589                "  otherThing = 1;\n"
14590                "}",
14591                Alignment);
14592   verifyFormat("void SomeFunction(int parameter = 0) {\n"
14593                "  int i   = 1;\n"
14594                "  int j   = 2;\n"
14595                "  int big = 10000;\n"
14596                "}",
14597                Alignment);
14598   verifyFormat("class C {\n"
14599                "public:\n"
14600                "  int i            = 1;\n"
14601                "  virtual void f() = 0;\n"
14602                "};",
14603                Alignment);
14604   verifyFormat("int i = 1;\n"
14605                "if (SomeType t = getSomething()) {\n"
14606                "}\n"
14607                "int j   = 2;\n"
14608                "int big = 10000;",
14609                Alignment);
14610   verifyFormat("int j = 7;\n"
14611                "for (int k = 0; k < N; ++k) {\n"
14612                "}\n"
14613                "int j   = 2;\n"
14614                "int big = 10000;\n"
14615                "}",
14616                Alignment);
14617   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14618   verifyFormat("int i = 1;\n"
14619                "LooooooooooongType loooooooooooooooooooooongVariable\n"
14620                "    = someLooooooooooooooooongFunction();\n"
14621                "int j = 2;",
14622                Alignment);
14623   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
14624   verifyFormat("int i = 1;\n"
14625                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
14626                "    someLooooooooooooooooongFunction();\n"
14627                "int j = 2;",
14628                Alignment);
14629 
14630   verifyFormat("auto lambda = []() {\n"
14631                "  auto i = 0;\n"
14632                "  return 0;\n"
14633                "};\n"
14634                "int i  = 0;\n"
14635                "auto v = type{\n"
14636                "    i = 1,   //\n"
14637                "    (i = 2), //\n"
14638                "    i = 3    //\n"
14639                "};",
14640                Alignment);
14641 
14642   verifyFormat(
14643       "int i      = 1;\n"
14644       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
14645       "                          loooooooooooooooooooooongParameterB);\n"
14646       "int j      = 2;",
14647       Alignment);
14648 
14649   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
14650                "          typename B   = very_long_type_name_1,\n"
14651                "          typename T_2 = very_long_type_name_2>\n"
14652                "auto foo() {}\n",
14653                Alignment);
14654   verifyFormat("int a, b = 1;\n"
14655                "int c  = 2;\n"
14656                "int dd = 3;\n",
14657                Alignment);
14658   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
14659                "float b[1][] = {{3.f}};\n",
14660                Alignment);
14661   verifyFormat("for (int i = 0; i < 1; i++)\n"
14662                "  int x = 1;\n",
14663                Alignment);
14664   verifyFormat("for (i = 0; i < 1; i++)\n"
14665                "  x = 1;\n"
14666                "y = 1;\n",
14667                Alignment);
14668 
14669   Alignment.ReflowComments = true;
14670   Alignment.ColumnLimit = 50;
14671   EXPECT_EQ("int x   = 0;\n"
14672             "int yy  = 1; /// specificlennospace\n"
14673             "int zzz = 2;\n",
14674             format("int x   = 0;\n"
14675                    "int yy  = 1; ///specificlennospace\n"
14676                    "int zzz = 2;\n",
14677                    Alignment));
14678 }
14679 
14680 TEST_F(FormatTest, AlignConsecutiveAssignments) {
14681   FormatStyle Alignment = getLLVMStyle();
14682   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14683   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14684   verifyFormat("int a = 5;\n"
14685                "int oneTwoThree = 123;",
14686                Alignment);
14687   verifyFormat("int a = 5;\n"
14688                "int oneTwoThree = 123;",
14689                Alignment);
14690 
14691   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14692   verifyFormat("int a           = 5;\n"
14693                "int oneTwoThree = 123;",
14694                Alignment);
14695   verifyFormat("int a           = method();\n"
14696                "int oneTwoThree = 133;",
14697                Alignment);
14698   verifyFormat("a &= 5;\n"
14699                "bcd *= 5;\n"
14700                "ghtyf += 5;\n"
14701                "dvfvdb -= 5;\n"
14702                "a /= 5;\n"
14703                "vdsvsv %= 5;\n"
14704                "sfdbddfbdfbb ^= 5;\n"
14705                "dvsdsv |= 5;\n"
14706                "int dsvvdvsdvvv = 123;",
14707                Alignment);
14708   verifyFormat("int i = 1, j = 10;\n"
14709                "something = 2000;",
14710                Alignment);
14711   verifyFormat("something = 2000;\n"
14712                "int i = 1, j = 10;\n",
14713                Alignment);
14714   verifyFormat("something = 2000;\n"
14715                "another   = 911;\n"
14716                "int i = 1, j = 10;\n"
14717                "oneMore = 1;\n"
14718                "i       = 2;",
14719                Alignment);
14720   verifyFormat("int a   = 5;\n"
14721                "int one = 1;\n"
14722                "method();\n"
14723                "int oneTwoThree = 123;\n"
14724                "int oneTwo      = 12;",
14725                Alignment);
14726   verifyFormat("int oneTwoThree = 123;\n"
14727                "int oneTwo      = 12;\n"
14728                "method();\n",
14729                Alignment);
14730   verifyFormat("int oneTwoThree = 123; // comment\n"
14731                "int oneTwo      = 12;  // comment",
14732                Alignment);
14733 
14734   // Bug 25167
14735   /* Uncomment when fixed
14736     verifyFormat("#if A\n"
14737                  "#else\n"
14738                  "int aaaaaaaa = 12;\n"
14739                  "#endif\n"
14740                  "#if B\n"
14741                  "#else\n"
14742                  "int a = 12;\n"
14743                  "#endif\n",
14744                  Alignment);
14745     verifyFormat("enum foo {\n"
14746                  "#if A\n"
14747                  "#else\n"
14748                  "  aaaaaaaa = 12;\n"
14749                  "#endif\n"
14750                  "#if B\n"
14751                  "#else\n"
14752                  "  a = 12;\n"
14753                  "#endif\n"
14754                  "};\n",
14755                  Alignment);
14756   */
14757 
14758   EXPECT_EQ("int a = 5;\n"
14759             "\n"
14760             "int oneTwoThree = 123;",
14761             format("int a       = 5;\n"
14762                    "\n"
14763                    "int oneTwoThree= 123;",
14764                    Alignment));
14765   EXPECT_EQ("int a   = 5;\n"
14766             "int one = 1;\n"
14767             "\n"
14768             "int oneTwoThree = 123;",
14769             format("int a = 5;\n"
14770                    "int one = 1;\n"
14771                    "\n"
14772                    "int oneTwoThree = 123;",
14773                    Alignment));
14774   EXPECT_EQ("int a   = 5;\n"
14775             "int one = 1;\n"
14776             "\n"
14777             "int oneTwoThree = 123;\n"
14778             "int oneTwo      = 12;",
14779             format("int a = 5;\n"
14780                    "int one = 1;\n"
14781                    "\n"
14782                    "int oneTwoThree = 123;\n"
14783                    "int oneTwo = 12;",
14784                    Alignment));
14785   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
14786   verifyFormat("#define A \\\n"
14787                "  int aaaa       = 12; \\\n"
14788                "  int b          = 23; \\\n"
14789                "  int ccc        = 234; \\\n"
14790                "  int dddddddddd = 2345;",
14791                Alignment);
14792   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14793   verifyFormat("#define A               \\\n"
14794                "  int aaaa       = 12;  \\\n"
14795                "  int b          = 23;  \\\n"
14796                "  int ccc        = 234; \\\n"
14797                "  int dddddddddd = 2345;",
14798                Alignment);
14799   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
14800   verifyFormat("#define A                                                      "
14801                "                \\\n"
14802                "  int aaaa       = 12;                                         "
14803                "                \\\n"
14804                "  int b          = 23;                                         "
14805                "                \\\n"
14806                "  int ccc        = 234;                                        "
14807                "                \\\n"
14808                "  int dddddddddd = 2345;",
14809                Alignment);
14810   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
14811                "k = 4, int l = 5,\n"
14812                "                  int m = 6) {\n"
14813                "  int j      = 10;\n"
14814                "  otherThing = 1;\n"
14815                "}",
14816                Alignment);
14817   verifyFormat("void SomeFunction(int parameter = 0) {\n"
14818                "  int i   = 1;\n"
14819                "  int j   = 2;\n"
14820                "  int big = 10000;\n"
14821                "}",
14822                Alignment);
14823   verifyFormat("class C {\n"
14824                "public:\n"
14825                "  int i            = 1;\n"
14826                "  virtual void f() = 0;\n"
14827                "};",
14828                Alignment);
14829   verifyFormat("int i = 1;\n"
14830                "if (SomeType t = getSomething()) {\n"
14831                "}\n"
14832                "int j   = 2;\n"
14833                "int big = 10000;",
14834                Alignment);
14835   verifyFormat("int j = 7;\n"
14836                "for (int k = 0; k < N; ++k) {\n"
14837                "}\n"
14838                "int j   = 2;\n"
14839                "int big = 10000;\n"
14840                "}",
14841                Alignment);
14842   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14843   verifyFormat("int i = 1;\n"
14844                "LooooooooooongType loooooooooooooooooooooongVariable\n"
14845                "    = someLooooooooooooooooongFunction();\n"
14846                "int j = 2;",
14847                Alignment);
14848   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
14849   verifyFormat("int i = 1;\n"
14850                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
14851                "    someLooooooooooooooooongFunction();\n"
14852                "int j = 2;",
14853                Alignment);
14854 
14855   verifyFormat("auto lambda = []() {\n"
14856                "  auto i = 0;\n"
14857                "  return 0;\n"
14858                "};\n"
14859                "int i  = 0;\n"
14860                "auto v = type{\n"
14861                "    i = 1,   //\n"
14862                "    (i = 2), //\n"
14863                "    i = 3    //\n"
14864                "};",
14865                Alignment);
14866 
14867   verifyFormat(
14868       "int i      = 1;\n"
14869       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
14870       "                          loooooooooooooooooooooongParameterB);\n"
14871       "int j      = 2;",
14872       Alignment);
14873 
14874   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
14875                "          typename B   = very_long_type_name_1,\n"
14876                "          typename T_2 = very_long_type_name_2>\n"
14877                "auto foo() {}\n",
14878                Alignment);
14879   verifyFormat("int a, b = 1;\n"
14880                "int c  = 2;\n"
14881                "int dd = 3;\n",
14882                Alignment);
14883   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
14884                "float b[1][] = {{3.f}};\n",
14885                Alignment);
14886   verifyFormat("for (int i = 0; i < 1; i++)\n"
14887                "  int x = 1;\n",
14888                Alignment);
14889   verifyFormat("for (i = 0; i < 1; i++)\n"
14890                "  x = 1;\n"
14891                "y = 1;\n",
14892                Alignment);
14893 
14894   Alignment.ReflowComments = true;
14895   Alignment.ColumnLimit = 50;
14896   EXPECT_EQ("int x   = 0;\n"
14897             "int yy  = 1; /// specificlennospace\n"
14898             "int zzz = 2;\n",
14899             format("int x   = 0;\n"
14900                    "int yy  = 1; ///specificlennospace\n"
14901                    "int zzz = 2;\n",
14902                    Alignment));
14903 }
14904 
14905 TEST_F(FormatTest, AlignConsecutiveBitFields) {
14906   FormatStyle Alignment = getLLVMStyle();
14907   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
14908   verifyFormat("int const a     : 5;\n"
14909                "int oneTwoThree : 23;",
14910                Alignment);
14911 
14912   // Initializers are allowed starting with c++2a
14913   verifyFormat("int const a     : 5 = 1;\n"
14914                "int oneTwoThree : 23 = 0;",
14915                Alignment);
14916 
14917   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14918   verifyFormat("int const a           : 5;\n"
14919                "int       oneTwoThree : 23;",
14920                Alignment);
14921 
14922   verifyFormat("int const a           : 5;  // comment\n"
14923                "int       oneTwoThree : 23; // comment",
14924                Alignment);
14925 
14926   verifyFormat("int const a           : 5 = 1;\n"
14927                "int       oneTwoThree : 23 = 0;",
14928                Alignment);
14929 
14930   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14931   verifyFormat("int const a           : 5  = 1;\n"
14932                "int       oneTwoThree : 23 = 0;",
14933                Alignment);
14934   verifyFormat("int const a           : 5  = {1};\n"
14935                "int       oneTwoThree : 23 = 0;",
14936                Alignment);
14937 
14938   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
14939   verifyFormat("int const a          :5;\n"
14940                "int       oneTwoThree:23;",
14941                Alignment);
14942 
14943   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
14944   verifyFormat("int const a           :5;\n"
14945                "int       oneTwoThree :23;",
14946                Alignment);
14947 
14948   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
14949   verifyFormat("int const a          : 5;\n"
14950                "int       oneTwoThree: 23;",
14951                Alignment);
14952 
14953   // Known limitations: ':' is only recognized as a bitfield colon when
14954   // followed by a number.
14955   /*
14956   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
14957                "int a           : 5;",
14958                Alignment);
14959   */
14960 }
14961 
14962 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
14963   FormatStyle Alignment = getLLVMStyle();
14964   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14965   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
14966   Alignment.PointerAlignment = FormatStyle::PAS_Right;
14967   verifyFormat("float const a = 5;\n"
14968                "int oneTwoThree = 123;",
14969                Alignment);
14970   verifyFormat("int a = 5;\n"
14971                "float const oneTwoThree = 123;",
14972                Alignment);
14973 
14974   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14975   verifyFormat("float const a = 5;\n"
14976                "int         oneTwoThree = 123;",
14977                Alignment);
14978   verifyFormat("int         a = method();\n"
14979                "float const oneTwoThree = 133;",
14980                Alignment);
14981   verifyFormat("int i = 1, j = 10;\n"
14982                "something = 2000;",
14983                Alignment);
14984   verifyFormat("something = 2000;\n"
14985                "int i = 1, j = 10;\n",
14986                Alignment);
14987   verifyFormat("float      something = 2000;\n"
14988                "double     another = 911;\n"
14989                "int        i = 1, j = 10;\n"
14990                "const int *oneMore = 1;\n"
14991                "unsigned   i = 2;",
14992                Alignment);
14993   verifyFormat("float a = 5;\n"
14994                "int   one = 1;\n"
14995                "method();\n"
14996                "const double       oneTwoThree = 123;\n"
14997                "const unsigned int oneTwo = 12;",
14998                Alignment);
14999   verifyFormat("int      oneTwoThree{0}; // comment\n"
15000                "unsigned oneTwo;         // comment",
15001                Alignment);
15002   verifyFormat("unsigned int       *a;\n"
15003                "int                *b;\n"
15004                "unsigned int Const *c;\n"
15005                "unsigned int const *d;\n"
15006                "unsigned int Const &e;\n"
15007                "unsigned int const &f;",
15008                Alignment);
15009   verifyFormat("Const unsigned int *c;\n"
15010                "const unsigned int *d;\n"
15011                "Const unsigned int &e;\n"
15012                "const unsigned int &f;\n"
15013                "const unsigned      g;\n"
15014                "Const unsigned      h;",
15015                Alignment);
15016   EXPECT_EQ("float const a = 5;\n"
15017             "\n"
15018             "int oneTwoThree = 123;",
15019             format("float const   a = 5;\n"
15020                    "\n"
15021                    "int           oneTwoThree= 123;",
15022                    Alignment));
15023   EXPECT_EQ("float a = 5;\n"
15024             "int   one = 1;\n"
15025             "\n"
15026             "unsigned oneTwoThree = 123;",
15027             format("float    a = 5;\n"
15028                    "int      one = 1;\n"
15029                    "\n"
15030                    "unsigned oneTwoThree = 123;",
15031                    Alignment));
15032   EXPECT_EQ("float a = 5;\n"
15033             "int   one = 1;\n"
15034             "\n"
15035             "unsigned oneTwoThree = 123;\n"
15036             "int      oneTwo = 12;",
15037             format("float    a = 5;\n"
15038                    "int one = 1;\n"
15039                    "\n"
15040                    "unsigned oneTwoThree = 123;\n"
15041                    "int oneTwo = 12;",
15042                    Alignment));
15043   // Function prototype alignment
15044   verifyFormat("int    a();\n"
15045                "double b();",
15046                Alignment);
15047   verifyFormat("int    a(int x);\n"
15048                "double b();",
15049                Alignment);
15050   unsigned OldColumnLimit = Alignment.ColumnLimit;
15051   // We need to set ColumnLimit to zero, in order to stress nested alignments,
15052   // otherwise the function parameters will be re-flowed onto a single line.
15053   Alignment.ColumnLimit = 0;
15054   EXPECT_EQ("int    a(int   x,\n"
15055             "         float y);\n"
15056             "double b(int    x,\n"
15057             "         double y);",
15058             format("int a(int x,\n"
15059                    " float y);\n"
15060                    "double b(int x,\n"
15061                    " double y);",
15062                    Alignment));
15063   // This ensures that function parameters of function declarations are
15064   // correctly indented when their owning functions are indented.
15065   // The failure case here is for 'double y' to not be indented enough.
15066   EXPECT_EQ("double a(int x);\n"
15067             "int    b(int    y,\n"
15068             "         double z);",
15069             format("double a(int x);\n"
15070                    "int b(int y,\n"
15071                    " double z);",
15072                    Alignment));
15073   // Set ColumnLimit low so that we induce wrapping immediately after
15074   // the function name and opening paren.
15075   Alignment.ColumnLimit = 13;
15076   verifyFormat("int function(\n"
15077                "    int  x,\n"
15078                "    bool y);",
15079                Alignment);
15080   Alignment.ColumnLimit = OldColumnLimit;
15081   // Ensure function pointers don't screw up recursive alignment
15082   verifyFormat("int    a(int x, void (*fp)(int y));\n"
15083                "double b();",
15084                Alignment);
15085   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15086   // Ensure recursive alignment is broken by function braces, so that the
15087   // "a = 1" does not align with subsequent assignments inside the function
15088   // body.
15089   verifyFormat("int func(int a = 1) {\n"
15090                "  int b  = 2;\n"
15091                "  int cc = 3;\n"
15092                "}",
15093                Alignment);
15094   verifyFormat("float      something = 2000;\n"
15095                "double     another   = 911;\n"
15096                "int        i = 1, j = 10;\n"
15097                "const int *oneMore = 1;\n"
15098                "unsigned   i       = 2;",
15099                Alignment);
15100   verifyFormat("int      oneTwoThree = {0}; // comment\n"
15101                "unsigned oneTwo      = 0;   // comment",
15102                Alignment);
15103   // Make sure that scope is correctly tracked, in the absence of braces
15104   verifyFormat("for (int i = 0; i < n; i++)\n"
15105                "  j = i;\n"
15106                "double x = 1;\n",
15107                Alignment);
15108   verifyFormat("if (int i = 0)\n"
15109                "  j = i;\n"
15110                "double x = 1;\n",
15111                Alignment);
15112   // Ensure operator[] and operator() are comprehended
15113   verifyFormat("struct test {\n"
15114                "  long long int foo();\n"
15115                "  int           operator[](int a);\n"
15116                "  double        bar();\n"
15117                "};\n",
15118                Alignment);
15119   verifyFormat("struct test {\n"
15120                "  long long int foo();\n"
15121                "  int           operator()(int a);\n"
15122                "  double        bar();\n"
15123                "};\n",
15124                Alignment);
15125 
15126   // PAS_Right
15127   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15128             "  int const i   = 1;\n"
15129             "  int      *j   = 2;\n"
15130             "  int       big = 10000;\n"
15131             "\n"
15132             "  unsigned oneTwoThree = 123;\n"
15133             "  int      oneTwo      = 12;\n"
15134             "  method();\n"
15135             "  float k  = 2;\n"
15136             "  int   ll = 10000;\n"
15137             "}",
15138             format("void SomeFunction(int parameter= 0) {\n"
15139                    " int const  i= 1;\n"
15140                    "  int *j=2;\n"
15141                    " int big  =  10000;\n"
15142                    "\n"
15143                    "unsigned oneTwoThree  =123;\n"
15144                    "int oneTwo = 12;\n"
15145                    "  method();\n"
15146                    "float k= 2;\n"
15147                    "int ll=10000;\n"
15148                    "}",
15149                    Alignment));
15150   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15151             "  int const i   = 1;\n"
15152             "  int     **j   = 2, ***k;\n"
15153             "  int      &k   = i;\n"
15154             "  int     &&l   = i + j;\n"
15155             "  int       big = 10000;\n"
15156             "\n"
15157             "  unsigned oneTwoThree = 123;\n"
15158             "  int      oneTwo      = 12;\n"
15159             "  method();\n"
15160             "  float k  = 2;\n"
15161             "  int   ll = 10000;\n"
15162             "}",
15163             format("void SomeFunction(int parameter= 0) {\n"
15164                    " int const  i= 1;\n"
15165                    "  int **j=2,***k;\n"
15166                    "int &k=i;\n"
15167                    "int &&l=i+j;\n"
15168                    " int big  =  10000;\n"
15169                    "\n"
15170                    "unsigned oneTwoThree  =123;\n"
15171                    "int oneTwo = 12;\n"
15172                    "  method();\n"
15173                    "float k= 2;\n"
15174                    "int ll=10000;\n"
15175                    "}",
15176                    Alignment));
15177   // variables are aligned at their name, pointers are at the right most
15178   // position
15179   verifyFormat("int   *a;\n"
15180                "int  **b;\n"
15181                "int ***c;\n"
15182                "int    foobar;\n",
15183                Alignment);
15184 
15185   // PAS_Left
15186   FormatStyle AlignmentLeft = Alignment;
15187   AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
15188   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15189             "  int const i   = 1;\n"
15190             "  int*      j   = 2;\n"
15191             "  int       big = 10000;\n"
15192             "\n"
15193             "  unsigned oneTwoThree = 123;\n"
15194             "  int      oneTwo      = 12;\n"
15195             "  method();\n"
15196             "  float k  = 2;\n"
15197             "  int   ll = 10000;\n"
15198             "}",
15199             format("void SomeFunction(int parameter= 0) {\n"
15200                    " int const  i= 1;\n"
15201                    "  int *j=2;\n"
15202                    " int big  =  10000;\n"
15203                    "\n"
15204                    "unsigned oneTwoThree  =123;\n"
15205                    "int oneTwo = 12;\n"
15206                    "  method();\n"
15207                    "float k= 2;\n"
15208                    "int ll=10000;\n"
15209                    "}",
15210                    AlignmentLeft));
15211   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15212             "  int const i   = 1;\n"
15213             "  int**     j   = 2;\n"
15214             "  int&      k   = i;\n"
15215             "  int&&     l   = i + j;\n"
15216             "  int       big = 10000;\n"
15217             "\n"
15218             "  unsigned oneTwoThree = 123;\n"
15219             "  int      oneTwo      = 12;\n"
15220             "  method();\n"
15221             "  float k  = 2;\n"
15222             "  int   ll = 10000;\n"
15223             "}",
15224             format("void SomeFunction(int parameter= 0) {\n"
15225                    " int const  i= 1;\n"
15226                    "  int **j=2;\n"
15227                    "int &k=i;\n"
15228                    "int &&l=i+j;\n"
15229                    " int big  =  10000;\n"
15230                    "\n"
15231                    "unsigned oneTwoThree  =123;\n"
15232                    "int oneTwo = 12;\n"
15233                    "  method();\n"
15234                    "float k= 2;\n"
15235                    "int ll=10000;\n"
15236                    "}",
15237                    AlignmentLeft));
15238   // variables are aligned at their name, pointers are at the left most position
15239   verifyFormat("int*   a;\n"
15240                "int**  b;\n"
15241                "int*** c;\n"
15242                "int    foobar;\n",
15243                AlignmentLeft);
15244 
15245   // PAS_Middle
15246   FormatStyle AlignmentMiddle = Alignment;
15247   AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
15248   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15249             "  int const i   = 1;\n"
15250             "  int *     j   = 2;\n"
15251             "  int       big = 10000;\n"
15252             "\n"
15253             "  unsigned oneTwoThree = 123;\n"
15254             "  int      oneTwo      = 12;\n"
15255             "  method();\n"
15256             "  float k  = 2;\n"
15257             "  int   ll = 10000;\n"
15258             "}",
15259             format("void SomeFunction(int parameter= 0) {\n"
15260                    " int const  i= 1;\n"
15261                    "  int *j=2;\n"
15262                    " int big  =  10000;\n"
15263                    "\n"
15264                    "unsigned oneTwoThree  =123;\n"
15265                    "int oneTwo = 12;\n"
15266                    "  method();\n"
15267                    "float k= 2;\n"
15268                    "int ll=10000;\n"
15269                    "}",
15270                    AlignmentMiddle));
15271   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15272             "  int const i   = 1;\n"
15273             "  int **    j   = 2, ***k;\n"
15274             "  int &     k   = i;\n"
15275             "  int &&    l   = i + j;\n"
15276             "  int       big = 10000;\n"
15277             "\n"
15278             "  unsigned oneTwoThree = 123;\n"
15279             "  int      oneTwo      = 12;\n"
15280             "  method();\n"
15281             "  float k  = 2;\n"
15282             "  int   ll = 10000;\n"
15283             "}",
15284             format("void SomeFunction(int parameter= 0) {\n"
15285                    " int const  i= 1;\n"
15286                    "  int **j=2,***k;\n"
15287                    "int &k=i;\n"
15288                    "int &&l=i+j;\n"
15289                    " int big  =  10000;\n"
15290                    "\n"
15291                    "unsigned oneTwoThree  =123;\n"
15292                    "int oneTwo = 12;\n"
15293                    "  method();\n"
15294                    "float k= 2;\n"
15295                    "int ll=10000;\n"
15296                    "}",
15297                    AlignmentMiddle));
15298   // variables are aligned at their name, pointers are in the middle
15299   verifyFormat("int *   a;\n"
15300                "int *   b;\n"
15301                "int *** c;\n"
15302                "int     foobar;\n",
15303                AlignmentMiddle);
15304 
15305   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15306   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15307   verifyFormat("#define A \\\n"
15308                "  int       aaaa = 12; \\\n"
15309                "  float     b = 23; \\\n"
15310                "  const int ccc = 234; \\\n"
15311                "  unsigned  dddddddddd = 2345;",
15312                Alignment);
15313   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15314   verifyFormat("#define A              \\\n"
15315                "  int       aaaa = 12; \\\n"
15316                "  float     b = 23;    \\\n"
15317                "  const int ccc = 234; \\\n"
15318                "  unsigned  dddddddddd = 2345;",
15319                Alignment);
15320   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15321   Alignment.ColumnLimit = 30;
15322   verifyFormat("#define A                    \\\n"
15323                "  int       aaaa = 12;       \\\n"
15324                "  float     b = 23;          \\\n"
15325                "  const int ccc = 234;       \\\n"
15326                "  int       dddddddddd = 2345;",
15327                Alignment);
15328   Alignment.ColumnLimit = 80;
15329   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15330                "k = 4, int l = 5,\n"
15331                "                  int m = 6) {\n"
15332                "  const int j = 10;\n"
15333                "  otherThing = 1;\n"
15334                "}",
15335                Alignment);
15336   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15337                "  int const i = 1;\n"
15338                "  int      *j = 2;\n"
15339                "  int       big = 10000;\n"
15340                "}",
15341                Alignment);
15342   verifyFormat("class C {\n"
15343                "public:\n"
15344                "  int          i = 1;\n"
15345                "  virtual void f() = 0;\n"
15346                "};",
15347                Alignment);
15348   verifyFormat("float i = 1;\n"
15349                "if (SomeType t = getSomething()) {\n"
15350                "}\n"
15351                "const unsigned j = 2;\n"
15352                "int            big = 10000;",
15353                Alignment);
15354   verifyFormat("float j = 7;\n"
15355                "for (int k = 0; k < N; ++k) {\n"
15356                "}\n"
15357                "unsigned j = 2;\n"
15358                "int      big = 10000;\n"
15359                "}",
15360                Alignment);
15361   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15362   verifyFormat("float              i = 1;\n"
15363                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15364                "    = someLooooooooooooooooongFunction();\n"
15365                "int j = 2;",
15366                Alignment);
15367   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15368   verifyFormat("int                i = 1;\n"
15369                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15370                "    someLooooooooooooooooongFunction();\n"
15371                "int j = 2;",
15372                Alignment);
15373 
15374   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15375   verifyFormat("auto lambda = []() {\n"
15376                "  auto  ii = 0;\n"
15377                "  float j  = 0;\n"
15378                "  return 0;\n"
15379                "};\n"
15380                "int   i  = 0;\n"
15381                "float i2 = 0;\n"
15382                "auto  v  = type{\n"
15383                "    i = 1,   //\n"
15384                "    (i = 2), //\n"
15385                "    i = 3    //\n"
15386                "};",
15387                Alignment);
15388   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15389 
15390   verifyFormat(
15391       "int      i = 1;\n"
15392       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15393       "                          loooooooooooooooooooooongParameterB);\n"
15394       "int      j = 2;",
15395       Alignment);
15396 
15397   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
15398   // We expect declarations and assignments to align, as long as it doesn't
15399   // exceed the column limit, starting a new alignment sequence whenever it
15400   // happens.
15401   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15402   Alignment.ColumnLimit = 30;
15403   verifyFormat("float    ii              = 1;\n"
15404                "unsigned j               = 2;\n"
15405                "int someVerylongVariable = 1;\n"
15406                "AnotherLongType  ll = 123456;\n"
15407                "VeryVeryLongType k  = 2;\n"
15408                "int              myvar = 1;",
15409                Alignment);
15410   Alignment.ColumnLimit = 80;
15411   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15412 
15413   verifyFormat(
15414       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
15415       "          typename LongType, typename B>\n"
15416       "auto foo() {}\n",
15417       Alignment);
15418   verifyFormat("float a, b = 1;\n"
15419                "int   c = 2;\n"
15420                "int   dd = 3;\n",
15421                Alignment);
15422   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
15423                "float b[1][] = {{3.f}};\n",
15424                Alignment);
15425   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15426   verifyFormat("float a, b = 1;\n"
15427                "int   c  = 2;\n"
15428                "int   dd = 3;\n",
15429                Alignment);
15430   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
15431                "float b[1][] = {{3.f}};\n",
15432                Alignment);
15433   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15434 
15435   Alignment.ColumnLimit = 30;
15436   Alignment.BinPackParameters = false;
15437   verifyFormat("void foo(float     a,\n"
15438                "         float     b,\n"
15439                "         int       c,\n"
15440                "         uint32_t *d) {\n"
15441                "  int   *e = 0;\n"
15442                "  float  f = 0;\n"
15443                "  double g = 0;\n"
15444                "}\n"
15445                "void bar(ino_t     a,\n"
15446                "         int       b,\n"
15447                "         uint32_t *c,\n"
15448                "         bool      d) {}\n",
15449                Alignment);
15450   Alignment.BinPackParameters = true;
15451   Alignment.ColumnLimit = 80;
15452 
15453   // Bug 33507
15454   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
15455   verifyFormat(
15456       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
15457       "  static const Version verVs2017;\n"
15458       "  return true;\n"
15459       "});\n",
15460       Alignment);
15461   Alignment.PointerAlignment = FormatStyle::PAS_Right;
15462 
15463   // See llvm.org/PR35641
15464   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15465   verifyFormat("int func() { //\n"
15466                "  int      b;\n"
15467                "  unsigned c;\n"
15468                "}",
15469                Alignment);
15470 
15471   // See PR37175
15472   FormatStyle Style = getMozillaStyle();
15473   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15474   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
15475             "foo(int a);",
15476             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
15477 
15478   Alignment.PointerAlignment = FormatStyle::PAS_Left;
15479   verifyFormat("unsigned int*       a;\n"
15480                "int*                b;\n"
15481                "unsigned int Const* c;\n"
15482                "unsigned int const* d;\n"
15483                "unsigned int Const& e;\n"
15484                "unsigned int const& f;",
15485                Alignment);
15486   verifyFormat("Const unsigned int* c;\n"
15487                "const unsigned int* d;\n"
15488                "Const unsigned int& e;\n"
15489                "const unsigned int& f;\n"
15490                "const unsigned      g;\n"
15491                "Const unsigned      h;",
15492                Alignment);
15493 
15494   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
15495   verifyFormat("unsigned int *       a;\n"
15496                "int *                b;\n"
15497                "unsigned int Const * c;\n"
15498                "unsigned int const * d;\n"
15499                "unsigned int Const & e;\n"
15500                "unsigned int const & f;",
15501                Alignment);
15502   verifyFormat("Const unsigned int * c;\n"
15503                "const unsigned int * d;\n"
15504                "Const unsigned int & e;\n"
15505                "const unsigned int & f;\n"
15506                "const unsigned       g;\n"
15507                "Const unsigned       h;",
15508                Alignment);
15509 }
15510 
15511 TEST_F(FormatTest, AlignWithLineBreaks) {
15512   auto Style = getLLVMStyleWithColumns(120);
15513 
15514   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
15515   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
15516   verifyFormat("void foo() {\n"
15517                "  int myVar = 5;\n"
15518                "  double x = 3.14;\n"
15519                "  auto str = \"Hello \"\n"
15520                "             \"World\";\n"
15521                "  auto s = \"Hello \"\n"
15522                "           \"Again\";\n"
15523                "}",
15524                Style);
15525 
15526   // clang-format off
15527   verifyFormat("void foo() {\n"
15528                "  const int capacityBefore = Entries.capacity();\n"
15529                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15530                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15531                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15532                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15533                "}",
15534                Style);
15535   // clang-format on
15536 
15537   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15538   verifyFormat("void foo() {\n"
15539                "  int myVar = 5;\n"
15540                "  double x  = 3.14;\n"
15541                "  auto str  = \"Hello \"\n"
15542                "              \"World\";\n"
15543                "  auto s    = \"Hello \"\n"
15544                "              \"Again\";\n"
15545                "}",
15546                Style);
15547 
15548   // clang-format off
15549   verifyFormat("void foo() {\n"
15550                "  const int capacityBefore = Entries.capacity();\n"
15551                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15552                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15553                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15554                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15555                "}",
15556                Style);
15557   // clang-format on
15558 
15559   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15560   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15561   verifyFormat("void foo() {\n"
15562                "  int    myVar = 5;\n"
15563                "  double x = 3.14;\n"
15564                "  auto   str = \"Hello \"\n"
15565                "               \"World\";\n"
15566                "  auto   s = \"Hello \"\n"
15567                "             \"Again\";\n"
15568                "}",
15569                Style);
15570 
15571   // clang-format off
15572   verifyFormat("void foo() {\n"
15573                "  const int  capacityBefore = Entries.capacity();\n"
15574                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15575                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15576                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15577                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15578                "}",
15579                Style);
15580   // clang-format on
15581 
15582   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15583   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15584 
15585   verifyFormat("void foo() {\n"
15586                "  int    myVar = 5;\n"
15587                "  double x     = 3.14;\n"
15588                "  auto   str   = \"Hello \"\n"
15589                "                 \"World\";\n"
15590                "  auto   s     = \"Hello \"\n"
15591                "                 \"Again\";\n"
15592                "}",
15593                Style);
15594 
15595   // clang-format off
15596   verifyFormat("void foo() {\n"
15597                "  const int  capacityBefore = Entries.capacity();\n"
15598                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15599                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15600                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15601                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15602                "}",
15603                Style);
15604   // clang-format on
15605 }
15606 
15607 TEST_F(FormatTest, LinuxBraceBreaking) {
15608   FormatStyle LinuxBraceStyle = getLLVMStyle();
15609   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
15610   verifyFormat("namespace a\n"
15611                "{\n"
15612                "class A\n"
15613                "{\n"
15614                "  void f()\n"
15615                "  {\n"
15616                "    if (true) {\n"
15617                "      a();\n"
15618                "      b();\n"
15619                "    } else {\n"
15620                "      a();\n"
15621                "    }\n"
15622                "  }\n"
15623                "  void g() { return; }\n"
15624                "};\n"
15625                "struct B {\n"
15626                "  int x;\n"
15627                "};\n"
15628                "} // namespace a\n",
15629                LinuxBraceStyle);
15630   verifyFormat("enum X {\n"
15631                "  Y = 0,\n"
15632                "}\n",
15633                LinuxBraceStyle);
15634   verifyFormat("struct S {\n"
15635                "  int Type;\n"
15636                "  union {\n"
15637                "    int x;\n"
15638                "    double y;\n"
15639                "  } Value;\n"
15640                "  class C\n"
15641                "  {\n"
15642                "    MyFavoriteType Value;\n"
15643                "  } Class;\n"
15644                "}\n",
15645                LinuxBraceStyle);
15646 }
15647 
15648 TEST_F(FormatTest, MozillaBraceBreaking) {
15649   FormatStyle MozillaBraceStyle = getLLVMStyle();
15650   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
15651   MozillaBraceStyle.FixNamespaceComments = false;
15652   verifyFormat("namespace a {\n"
15653                "class A\n"
15654                "{\n"
15655                "  void f()\n"
15656                "  {\n"
15657                "    if (true) {\n"
15658                "      a();\n"
15659                "      b();\n"
15660                "    }\n"
15661                "  }\n"
15662                "  void g() { return; }\n"
15663                "};\n"
15664                "enum E\n"
15665                "{\n"
15666                "  A,\n"
15667                "  // foo\n"
15668                "  B,\n"
15669                "  C\n"
15670                "};\n"
15671                "struct B\n"
15672                "{\n"
15673                "  int x;\n"
15674                "};\n"
15675                "}\n",
15676                MozillaBraceStyle);
15677   verifyFormat("struct S\n"
15678                "{\n"
15679                "  int Type;\n"
15680                "  union\n"
15681                "  {\n"
15682                "    int x;\n"
15683                "    double y;\n"
15684                "  } Value;\n"
15685                "  class C\n"
15686                "  {\n"
15687                "    MyFavoriteType Value;\n"
15688                "  } Class;\n"
15689                "}\n",
15690                MozillaBraceStyle);
15691 }
15692 
15693 TEST_F(FormatTest, StroustrupBraceBreaking) {
15694   FormatStyle StroustrupBraceStyle = getLLVMStyle();
15695   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
15696   verifyFormat("namespace a {\n"
15697                "class A {\n"
15698                "  void f()\n"
15699                "  {\n"
15700                "    if (true) {\n"
15701                "      a();\n"
15702                "      b();\n"
15703                "    }\n"
15704                "  }\n"
15705                "  void g() { return; }\n"
15706                "};\n"
15707                "struct B {\n"
15708                "  int x;\n"
15709                "};\n"
15710                "} // namespace a\n",
15711                StroustrupBraceStyle);
15712 
15713   verifyFormat("void foo()\n"
15714                "{\n"
15715                "  if (a) {\n"
15716                "    a();\n"
15717                "  }\n"
15718                "  else {\n"
15719                "    b();\n"
15720                "  }\n"
15721                "}\n",
15722                StroustrupBraceStyle);
15723 
15724   verifyFormat("#ifdef _DEBUG\n"
15725                "int foo(int i = 0)\n"
15726                "#else\n"
15727                "int foo(int i = 5)\n"
15728                "#endif\n"
15729                "{\n"
15730                "  return i;\n"
15731                "}",
15732                StroustrupBraceStyle);
15733 
15734   verifyFormat("void foo() {}\n"
15735                "void bar()\n"
15736                "#ifdef _DEBUG\n"
15737                "{\n"
15738                "  foo();\n"
15739                "}\n"
15740                "#else\n"
15741                "{\n"
15742                "}\n"
15743                "#endif",
15744                StroustrupBraceStyle);
15745 
15746   verifyFormat("void foobar() { int i = 5; }\n"
15747                "#ifdef _DEBUG\n"
15748                "void bar() {}\n"
15749                "#else\n"
15750                "void bar() { foobar(); }\n"
15751                "#endif",
15752                StroustrupBraceStyle);
15753 }
15754 
15755 TEST_F(FormatTest, AllmanBraceBreaking) {
15756   FormatStyle AllmanBraceStyle = getLLVMStyle();
15757   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
15758 
15759   EXPECT_EQ("namespace a\n"
15760             "{\n"
15761             "void f();\n"
15762             "void g();\n"
15763             "} // namespace a\n",
15764             format("namespace a\n"
15765                    "{\n"
15766                    "void f();\n"
15767                    "void g();\n"
15768                    "}\n",
15769                    AllmanBraceStyle));
15770 
15771   verifyFormat("namespace a\n"
15772                "{\n"
15773                "class A\n"
15774                "{\n"
15775                "  void f()\n"
15776                "  {\n"
15777                "    if (true)\n"
15778                "    {\n"
15779                "      a();\n"
15780                "      b();\n"
15781                "    }\n"
15782                "  }\n"
15783                "  void g() { return; }\n"
15784                "};\n"
15785                "struct B\n"
15786                "{\n"
15787                "  int x;\n"
15788                "};\n"
15789                "union C\n"
15790                "{\n"
15791                "};\n"
15792                "} // namespace a",
15793                AllmanBraceStyle);
15794 
15795   verifyFormat("void f()\n"
15796                "{\n"
15797                "  if (true)\n"
15798                "  {\n"
15799                "    a();\n"
15800                "  }\n"
15801                "  else if (false)\n"
15802                "  {\n"
15803                "    b();\n"
15804                "  }\n"
15805                "  else\n"
15806                "  {\n"
15807                "    c();\n"
15808                "  }\n"
15809                "}\n",
15810                AllmanBraceStyle);
15811 
15812   verifyFormat("void f()\n"
15813                "{\n"
15814                "  for (int i = 0; i < 10; ++i)\n"
15815                "  {\n"
15816                "    a();\n"
15817                "  }\n"
15818                "  while (false)\n"
15819                "  {\n"
15820                "    b();\n"
15821                "  }\n"
15822                "  do\n"
15823                "  {\n"
15824                "    c();\n"
15825                "  } while (false)\n"
15826                "}\n",
15827                AllmanBraceStyle);
15828 
15829   verifyFormat("void f(int a)\n"
15830                "{\n"
15831                "  switch (a)\n"
15832                "  {\n"
15833                "  case 0:\n"
15834                "    break;\n"
15835                "  case 1:\n"
15836                "  {\n"
15837                "    break;\n"
15838                "  }\n"
15839                "  case 2:\n"
15840                "  {\n"
15841                "  }\n"
15842                "  break;\n"
15843                "  default:\n"
15844                "    break;\n"
15845                "  }\n"
15846                "}\n",
15847                AllmanBraceStyle);
15848 
15849   verifyFormat("enum X\n"
15850                "{\n"
15851                "  Y = 0,\n"
15852                "}\n",
15853                AllmanBraceStyle);
15854   verifyFormat("enum X\n"
15855                "{\n"
15856                "  Y = 0\n"
15857                "}\n",
15858                AllmanBraceStyle);
15859 
15860   verifyFormat("@interface BSApplicationController ()\n"
15861                "{\n"
15862                "@private\n"
15863                "  id _extraIvar;\n"
15864                "}\n"
15865                "@end\n",
15866                AllmanBraceStyle);
15867 
15868   verifyFormat("#ifdef _DEBUG\n"
15869                "int foo(int i = 0)\n"
15870                "#else\n"
15871                "int foo(int i = 5)\n"
15872                "#endif\n"
15873                "{\n"
15874                "  return i;\n"
15875                "}",
15876                AllmanBraceStyle);
15877 
15878   verifyFormat("void foo() {}\n"
15879                "void bar()\n"
15880                "#ifdef _DEBUG\n"
15881                "{\n"
15882                "  foo();\n"
15883                "}\n"
15884                "#else\n"
15885                "{\n"
15886                "}\n"
15887                "#endif",
15888                AllmanBraceStyle);
15889 
15890   verifyFormat("void foobar() { int i = 5; }\n"
15891                "#ifdef _DEBUG\n"
15892                "void bar() {}\n"
15893                "#else\n"
15894                "void bar() { foobar(); }\n"
15895                "#endif",
15896                AllmanBraceStyle);
15897 
15898   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
15899             FormatStyle::SLS_All);
15900 
15901   verifyFormat("[](int i) { return i + 2; };\n"
15902                "[](int i, int j)\n"
15903                "{\n"
15904                "  auto x = i + j;\n"
15905                "  auto y = i * j;\n"
15906                "  return x ^ y;\n"
15907                "};\n"
15908                "void foo()\n"
15909                "{\n"
15910                "  auto shortLambda = [](int i) { return i + 2; };\n"
15911                "  auto longLambda = [](int i, int j)\n"
15912                "  {\n"
15913                "    auto x = i + j;\n"
15914                "    auto y = i * j;\n"
15915                "    return x ^ y;\n"
15916                "  };\n"
15917                "}",
15918                AllmanBraceStyle);
15919 
15920   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
15921 
15922   verifyFormat("[](int i)\n"
15923                "{\n"
15924                "  return i + 2;\n"
15925                "};\n"
15926                "[](int i, int j)\n"
15927                "{\n"
15928                "  auto x = i + j;\n"
15929                "  auto y = i * j;\n"
15930                "  return x ^ y;\n"
15931                "};\n"
15932                "void foo()\n"
15933                "{\n"
15934                "  auto shortLambda = [](int i)\n"
15935                "  {\n"
15936                "    return i + 2;\n"
15937                "  };\n"
15938                "  auto longLambda = [](int i, int j)\n"
15939                "  {\n"
15940                "    auto x = i + j;\n"
15941                "    auto y = i * j;\n"
15942                "    return x ^ y;\n"
15943                "  };\n"
15944                "}",
15945                AllmanBraceStyle);
15946 
15947   // Reset
15948   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
15949 
15950   // This shouldn't affect ObjC blocks..
15951   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
15952                "  // ...\n"
15953                "  int i;\n"
15954                "}];",
15955                AllmanBraceStyle);
15956   verifyFormat("void (^block)(void) = ^{\n"
15957                "  // ...\n"
15958                "  int i;\n"
15959                "};",
15960                AllmanBraceStyle);
15961   // .. or dict literals.
15962   verifyFormat("void f()\n"
15963                "{\n"
15964                "  // ...\n"
15965                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
15966                "}",
15967                AllmanBraceStyle);
15968   verifyFormat("void f()\n"
15969                "{\n"
15970                "  // ...\n"
15971                "  [object someMethod:@{a : @\"b\"}];\n"
15972                "}",
15973                AllmanBraceStyle);
15974   verifyFormat("int f()\n"
15975                "{ // comment\n"
15976                "  return 42;\n"
15977                "}",
15978                AllmanBraceStyle);
15979 
15980   AllmanBraceStyle.ColumnLimit = 19;
15981   verifyFormat("void f() { int i; }", AllmanBraceStyle);
15982   AllmanBraceStyle.ColumnLimit = 18;
15983   verifyFormat("void f()\n"
15984                "{\n"
15985                "  int i;\n"
15986                "}",
15987                AllmanBraceStyle);
15988   AllmanBraceStyle.ColumnLimit = 80;
15989 
15990   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
15991   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
15992       FormatStyle::SIS_WithoutElse;
15993   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
15994   verifyFormat("void f(bool b)\n"
15995                "{\n"
15996                "  if (b)\n"
15997                "  {\n"
15998                "    return;\n"
15999                "  }\n"
16000                "}\n",
16001                BreakBeforeBraceShortIfs);
16002   verifyFormat("void f(bool b)\n"
16003                "{\n"
16004                "  if constexpr (b)\n"
16005                "  {\n"
16006                "    return;\n"
16007                "  }\n"
16008                "}\n",
16009                BreakBeforeBraceShortIfs);
16010   verifyFormat("void f(bool b)\n"
16011                "{\n"
16012                "  if CONSTEXPR (b)\n"
16013                "  {\n"
16014                "    return;\n"
16015                "  }\n"
16016                "}\n",
16017                BreakBeforeBraceShortIfs);
16018   verifyFormat("void f(bool b)\n"
16019                "{\n"
16020                "  if (b) return;\n"
16021                "}\n",
16022                BreakBeforeBraceShortIfs);
16023   verifyFormat("void f(bool b)\n"
16024                "{\n"
16025                "  if constexpr (b) return;\n"
16026                "}\n",
16027                BreakBeforeBraceShortIfs);
16028   verifyFormat("void f(bool b)\n"
16029                "{\n"
16030                "  if CONSTEXPR (b) return;\n"
16031                "}\n",
16032                BreakBeforeBraceShortIfs);
16033   verifyFormat("void f(bool b)\n"
16034                "{\n"
16035                "  while (b)\n"
16036                "  {\n"
16037                "    return;\n"
16038                "  }\n"
16039                "}\n",
16040                BreakBeforeBraceShortIfs);
16041 }
16042 
16043 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
16044   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
16045   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
16046 
16047   // Make a few changes to the style for testing purposes
16048   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
16049       FormatStyle::SFS_Empty;
16050   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
16051   WhitesmithsBraceStyle.ColumnLimit = 0;
16052 
16053   // FIXME: this test case can't decide whether there should be a blank line
16054   // after the ~D() line or not. It adds one if one doesn't exist in the test
16055   // and it removes the line if one exists.
16056   /*
16057   verifyFormat("class A;\n"
16058                "namespace B\n"
16059                "  {\n"
16060                "class C;\n"
16061                "// Comment\n"
16062                "class D\n"
16063                "  {\n"
16064                "public:\n"
16065                "  D();\n"
16066                "  ~D() {}\n"
16067                "private:\n"
16068                "  enum E\n"
16069                "    {\n"
16070                "    F\n"
16071                "    }\n"
16072                "  };\n"
16073                "  } // namespace B\n",
16074                WhitesmithsBraceStyle);
16075   */
16076 
16077   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
16078   verifyFormat("namespace a\n"
16079                "  {\n"
16080                "class A\n"
16081                "  {\n"
16082                "  void f()\n"
16083                "    {\n"
16084                "    if (true)\n"
16085                "      {\n"
16086                "      a();\n"
16087                "      b();\n"
16088                "      }\n"
16089                "    }\n"
16090                "  void g()\n"
16091                "    {\n"
16092                "    return;\n"
16093                "    }\n"
16094                "  };\n"
16095                "struct B\n"
16096                "  {\n"
16097                "  int x;\n"
16098                "  };\n"
16099                "  } // namespace a",
16100                WhitesmithsBraceStyle);
16101 
16102   verifyFormat("namespace a\n"
16103                "  {\n"
16104                "namespace b\n"
16105                "  {\n"
16106                "class A\n"
16107                "  {\n"
16108                "  void f()\n"
16109                "    {\n"
16110                "    if (true)\n"
16111                "      {\n"
16112                "      a();\n"
16113                "      b();\n"
16114                "      }\n"
16115                "    }\n"
16116                "  void g()\n"
16117                "    {\n"
16118                "    return;\n"
16119                "    }\n"
16120                "  };\n"
16121                "struct B\n"
16122                "  {\n"
16123                "  int x;\n"
16124                "  };\n"
16125                "  } // namespace b\n"
16126                "  } // namespace a",
16127                WhitesmithsBraceStyle);
16128 
16129   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
16130   verifyFormat("namespace a\n"
16131                "  {\n"
16132                "namespace b\n"
16133                "  {\n"
16134                "  class A\n"
16135                "    {\n"
16136                "    void f()\n"
16137                "      {\n"
16138                "      if (true)\n"
16139                "        {\n"
16140                "        a();\n"
16141                "        b();\n"
16142                "        }\n"
16143                "      }\n"
16144                "    void g()\n"
16145                "      {\n"
16146                "      return;\n"
16147                "      }\n"
16148                "    };\n"
16149                "  struct B\n"
16150                "    {\n"
16151                "    int x;\n"
16152                "    };\n"
16153                "  } // namespace b\n"
16154                "  } // namespace a",
16155                WhitesmithsBraceStyle);
16156 
16157   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
16158   verifyFormat("namespace a\n"
16159                "  {\n"
16160                "  namespace b\n"
16161                "    {\n"
16162                "    class A\n"
16163                "      {\n"
16164                "      void f()\n"
16165                "        {\n"
16166                "        if (true)\n"
16167                "          {\n"
16168                "          a();\n"
16169                "          b();\n"
16170                "          }\n"
16171                "        }\n"
16172                "      void g()\n"
16173                "        {\n"
16174                "        return;\n"
16175                "        }\n"
16176                "      };\n"
16177                "    struct B\n"
16178                "      {\n"
16179                "      int x;\n"
16180                "      };\n"
16181                "    } // namespace b\n"
16182                "  }   // namespace a",
16183                WhitesmithsBraceStyle);
16184 
16185   verifyFormat("void f()\n"
16186                "  {\n"
16187                "  if (true)\n"
16188                "    {\n"
16189                "    a();\n"
16190                "    }\n"
16191                "  else if (false)\n"
16192                "    {\n"
16193                "    b();\n"
16194                "    }\n"
16195                "  else\n"
16196                "    {\n"
16197                "    c();\n"
16198                "    }\n"
16199                "  }\n",
16200                WhitesmithsBraceStyle);
16201 
16202   verifyFormat("void f()\n"
16203                "  {\n"
16204                "  for (int i = 0; i < 10; ++i)\n"
16205                "    {\n"
16206                "    a();\n"
16207                "    }\n"
16208                "  while (false)\n"
16209                "    {\n"
16210                "    b();\n"
16211                "    }\n"
16212                "  do\n"
16213                "    {\n"
16214                "    c();\n"
16215                "    } while (false)\n"
16216                "  }\n",
16217                WhitesmithsBraceStyle);
16218 
16219   WhitesmithsBraceStyle.IndentCaseLabels = true;
16220   verifyFormat("void switchTest1(int a)\n"
16221                "  {\n"
16222                "  switch (a)\n"
16223                "    {\n"
16224                "    case 2:\n"
16225                "      {\n"
16226                "      }\n"
16227                "      break;\n"
16228                "    }\n"
16229                "  }\n",
16230                WhitesmithsBraceStyle);
16231 
16232   verifyFormat("void switchTest2(int a)\n"
16233                "  {\n"
16234                "  switch (a)\n"
16235                "    {\n"
16236                "    case 0:\n"
16237                "      break;\n"
16238                "    case 1:\n"
16239                "      {\n"
16240                "      break;\n"
16241                "      }\n"
16242                "    case 2:\n"
16243                "      {\n"
16244                "      }\n"
16245                "      break;\n"
16246                "    default:\n"
16247                "      break;\n"
16248                "    }\n"
16249                "  }\n",
16250                WhitesmithsBraceStyle);
16251 
16252   verifyFormat("void switchTest3(int a)\n"
16253                "  {\n"
16254                "  switch (a)\n"
16255                "    {\n"
16256                "    case 0:\n"
16257                "      {\n"
16258                "      foo(x);\n"
16259                "      }\n"
16260                "      break;\n"
16261                "    default:\n"
16262                "      {\n"
16263                "      foo(1);\n"
16264                "      }\n"
16265                "      break;\n"
16266                "    }\n"
16267                "  }\n",
16268                WhitesmithsBraceStyle);
16269 
16270   WhitesmithsBraceStyle.IndentCaseLabels = false;
16271 
16272   verifyFormat("void switchTest4(int a)\n"
16273                "  {\n"
16274                "  switch (a)\n"
16275                "    {\n"
16276                "  case 2:\n"
16277                "    {\n"
16278                "    }\n"
16279                "    break;\n"
16280                "    }\n"
16281                "  }\n",
16282                WhitesmithsBraceStyle);
16283 
16284   verifyFormat("void switchTest5(int a)\n"
16285                "  {\n"
16286                "  switch (a)\n"
16287                "    {\n"
16288                "  case 0:\n"
16289                "    break;\n"
16290                "  case 1:\n"
16291                "    {\n"
16292                "    foo();\n"
16293                "    break;\n"
16294                "    }\n"
16295                "  case 2:\n"
16296                "    {\n"
16297                "    }\n"
16298                "    break;\n"
16299                "  default:\n"
16300                "    break;\n"
16301                "    }\n"
16302                "  }\n",
16303                WhitesmithsBraceStyle);
16304 
16305   verifyFormat("void switchTest6(int a)\n"
16306                "  {\n"
16307                "  switch (a)\n"
16308                "    {\n"
16309                "  case 0:\n"
16310                "    {\n"
16311                "    foo(x);\n"
16312                "    }\n"
16313                "    break;\n"
16314                "  default:\n"
16315                "    {\n"
16316                "    foo(1);\n"
16317                "    }\n"
16318                "    break;\n"
16319                "    }\n"
16320                "  }\n",
16321                WhitesmithsBraceStyle);
16322 
16323   verifyFormat("enum X\n"
16324                "  {\n"
16325                "  Y = 0, // testing\n"
16326                "  }\n",
16327                WhitesmithsBraceStyle);
16328 
16329   verifyFormat("enum X\n"
16330                "  {\n"
16331                "  Y = 0\n"
16332                "  }\n",
16333                WhitesmithsBraceStyle);
16334   verifyFormat("enum X\n"
16335                "  {\n"
16336                "  Y = 0,\n"
16337                "  Z = 1\n"
16338                "  };\n",
16339                WhitesmithsBraceStyle);
16340 
16341   verifyFormat("@interface BSApplicationController ()\n"
16342                "  {\n"
16343                "@private\n"
16344                "  id _extraIvar;\n"
16345                "  }\n"
16346                "@end\n",
16347                WhitesmithsBraceStyle);
16348 
16349   verifyFormat("#ifdef _DEBUG\n"
16350                "int foo(int i = 0)\n"
16351                "#else\n"
16352                "int foo(int i = 5)\n"
16353                "#endif\n"
16354                "  {\n"
16355                "  return i;\n"
16356                "  }",
16357                WhitesmithsBraceStyle);
16358 
16359   verifyFormat("void foo() {}\n"
16360                "void bar()\n"
16361                "#ifdef _DEBUG\n"
16362                "  {\n"
16363                "  foo();\n"
16364                "  }\n"
16365                "#else\n"
16366                "  {\n"
16367                "  }\n"
16368                "#endif",
16369                WhitesmithsBraceStyle);
16370 
16371   verifyFormat("void foobar()\n"
16372                "  {\n"
16373                "  int i = 5;\n"
16374                "  }\n"
16375                "#ifdef _DEBUG\n"
16376                "void bar()\n"
16377                "  {\n"
16378                "  }\n"
16379                "#else\n"
16380                "void bar()\n"
16381                "  {\n"
16382                "  foobar();\n"
16383                "  }\n"
16384                "#endif",
16385                WhitesmithsBraceStyle);
16386 
16387   // This shouldn't affect ObjC blocks..
16388   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
16389                "  // ...\n"
16390                "  int i;\n"
16391                "}];",
16392                WhitesmithsBraceStyle);
16393   verifyFormat("void (^block)(void) = ^{\n"
16394                "  // ...\n"
16395                "  int i;\n"
16396                "};",
16397                WhitesmithsBraceStyle);
16398   // .. or dict literals.
16399   verifyFormat("void f()\n"
16400                "  {\n"
16401                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
16402                "  }",
16403                WhitesmithsBraceStyle);
16404 
16405   verifyFormat("int f()\n"
16406                "  { // comment\n"
16407                "  return 42;\n"
16408                "  }",
16409                WhitesmithsBraceStyle);
16410 
16411   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
16412   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
16413       FormatStyle::SIS_OnlyFirstIf;
16414   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
16415   verifyFormat("void f(bool b)\n"
16416                "  {\n"
16417                "  if (b)\n"
16418                "    {\n"
16419                "    return;\n"
16420                "    }\n"
16421                "  }\n",
16422                BreakBeforeBraceShortIfs);
16423   verifyFormat("void f(bool b)\n"
16424                "  {\n"
16425                "  if (b) return;\n"
16426                "  }\n",
16427                BreakBeforeBraceShortIfs);
16428   verifyFormat("void f(bool b)\n"
16429                "  {\n"
16430                "  while (b)\n"
16431                "    {\n"
16432                "    return;\n"
16433                "    }\n"
16434                "  }\n",
16435                BreakBeforeBraceShortIfs);
16436 }
16437 
16438 TEST_F(FormatTest, GNUBraceBreaking) {
16439   FormatStyle GNUBraceStyle = getLLVMStyle();
16440   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
16441   verifyFormat("namespace a\n"
16442                "{\n"
16443                "class A\n"
16444                "{\n"
16445                "  void f()\n"
16446                "  {\n"
16447                "    int a;\n"
16448                "    {\n"
16449                "      int b;\n"
16450                "    }\n"
16451                "    if (true)\n"
16452                "      {\n"
16453                "        a();\n"
16454                "        b();\n"
16455                "      }\n"
16456                "  }\n"
16457                "  void g() { return; }\n"
16458                "}\n"
16459                "} // namespace a",
16460                GNUBraceStyle);
16461 
16462   verifyFormat("void f()\n"
16463                "{\n"
16464                "  if (true)\n"
16465                "    {\n"
16466                "      a();\n"
16467                "    }\n"
16468                "  else if (false)\n"
16469                "    {\n"
16470                "      b();\n"
16471                "    }\n"
16472                "  else\n"
16473                "    {\n"
16474                "      c();\n"
16475                "    }\n"
16476                "}\n",
16477                GNUBraceStyle);
16478 
16479   verifyFormat("void f()\n"
16480                "{\n"
16481                "  for (int i = 0; i < 10; ++i)\n"
16482                "    {\n"
16483                "      a();\n"
16484                "    }\n"
16485                "  while (false)\n"
16486                "    {\n"
16487                "      b();\n"
16488                "    }\n"
16489                "  do\n"
16490                "    {\n"
16491                "      c();\n"
16492                "    }\n"
16493                "  while (false);\n"
16494                "}\n",
16495                GNUBraceStyle);
16496 
16497   verifyFormat("void f(int a)\n"
16498                "{\n"
16499                "  switch (a)\n"
16500                "    {\n"
16501                "    case 0:\n"
16502                "      break;\n"
16503                "    case 1:\n"
16504                "      {\n"
16505                "        break;\n"
16506                "      }\n"
16507                "    case 2:\n"
16508                "      {\n"
16509                "      }\n"
16510                "      break;\n"
16511                "    default:\n"
16512                "      break;\n"
16513                "    }\n"
16514                "}\n",
16515                GNUBraceStyle);
16516 
16517   verifyFormat("enum X\n"
16518                "{\n"
16519                "  Y = 0,\n"
16520                "}\n",
16521                GNUBraceStyle);
16522 
16523   verifyFormat("@interface BSApplicationController ()\n"
16524                "{\n"
16525                "@private\n"
16526                "  id _extraIvar;\n"
16527                "}\n"
16528                "@end\n",
16529                GNUBraceStyle);
16530 
16531   verifyFormat("#ifdef _DEBUG\n"
16532                "int foo(int i = 0)\n"
16533                "#else\n"
16534                "int foo(int i = 5)\n"
16535                "#endif\n"
16536                "{\n"
16537                "  return i;\n"
16538                "}",
16539                GNUBraceStyle);
16540 
16541   verifyFormat("void foo() {}\n"
16542                "void bar()\n"
16543                "#ifdef _DEBUG\n"
16544                "{\n"
16545                "  foo();\n"
16546                "}\n"
16547                "#else\n"
16548                "{\n"
16549                "}\n"
16550                "#endif",
16551                GNUBraceStyle);
16552 
16553   verifyFormat("void foobar() { int i = 5; }\n"
16554                "#ifdef _DEBUG\n"
16555                "void bar() {}\n"
16556                "#else\n"
16557                "void bar() { foobar(); }\n"
16558                "#endif",
16559                GNUBraceStyle);
16560 }
16561 
16562 TEST_F(FormatTest, WebKitBraceBreaking) {
16563   FormatStyle WebKitBraceStyle = getLLVMStyle();
16564   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
16565   WebKitBraceStyle.FixNamespaceComments = false;
16566   verifyFormat("namespace a {\n"
16567                "class A {\n"
16568                "  void f()\n"
16569                "  {\n"
16570                "    if (true) {\n"
16571                "      a();\n"
16572                "      b();\n"
16573                "    }\n"
16574                "  }\n"
16575                "  void g() { return; }\n"
16576                "};\n"
16577                "enum E {\n"
16578                "  A,\n"
16579                "  // foo\n"
16580                "  B,\n"
16581                "  C\n"
16582                "};\n"
16583                "struct B {\n"
16584                "  int x;\n"
16585                "};\n"
16586                "}\n",
16587                WebKitBraceStyle);
16588   verifyFormat("struct S {\n"
16589                "  int Type;\n"
16590                "  union {\n"
16591                "    int x;\n"
16592                "    double y;\n"
16593                "  } Value;\n"
16594                "  class C {\n"
16595                "    MyFavoriteType Value;\n"
16596                "  } Class;\n"
16597                "};\n",
16598                WebKitBraceStyle);
16599 }
16600 
16601 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
16602   verifyFormat("void f() {\n"
16603                "  try {\n"
16604                "  } catch (const Exception &e) {\n"
16605                "  }\n"
16606                "}\n",
16607                getLLVMStyle());
16608 }
16609 
16610 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
16611   auto Style = getLLVMStyle();
16612   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
16613   Style.AlignConsecutiveAssignments =
16614       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
16615   Style.AlignConsecutiveDeclarations =
16616       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
16617   verifyFormat("struct test demo[] = {\n"
16618                "    {56,    23, \"hello\"},\n"
16619                "    {-1, 93463, \"world\"},\n"
16620                "    { 7,     5,    \"!!\"}\n"
16621                "};\n",
16622                Style);
16623 
16624   verifyFormat("struct test demo[] = {\n"
16625                "    {56,    23, \"hello\"}, // first line\n"
16626                "    {-1, 93463, \"world\"}, // second line\n"
16627                "    { 7,     5,    \"!!\"}  // third line\n"
16628                "};\n",
16629                Style);
16630 
16631   verifyFormat("struct test demo[4] = {\n"
16632                "    { 56,    23, 21,       \"oh\"}, // first line\n"
16633                "    { -1, 93463, 22,       \"my\"}, // second line\n"
16634                "    {  7,     5,  1, \"goodness\"}  // third line\n"
16635                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
16636                "};\n",
16637                Style);
16638 
16639   verifyFormat("struct test demo[3] = {\n"
16640                "    {56,    23, \"hello\"},\n"
16641                "    {-1, 93463, \"world\"},\n"
16642                "    { 7,     5,    \"!!\"}\n"
16643                "};\n",
16644                Style);
16645 
16646   verifyFormat("struct test demo[3] = {\n"
16647                "    {int{56},    23, \"hello\"},\n"
16648                "    {int{-1}, 93463, \"world\"},\n"
16649                "    { int{7},     5,    \"!!\"}\n"
16650                "};\n",
16651                Style);
16652 
16653   verifyFormat("struct test demo[] = {\n"
16654                "    {56,    23, \"hello\"},\n"
16655                "    {-1, 93463, \"world\"},\n"
16656                "    { 7,     5,    \"!!\"},\n"
16657                "};\n",
16658                Style);
16659 
16660   verifyFormat("test demo[] = {\n"
16661                "    {56,    23, \"hello\"},\n"
16662                "    {-1, 93463, \"world\"},\n"
16663                "    { 7,     5,    \"!!\"},\n"
16664                "};\n",
16665                Style);
16666 
16667   verifyFormat("demo = std::array<struct test, 3>{\n"
16668                "    test{56,    23, \"hello\"},\n"
16669                "    test{-1, 93463, \"world\"},\n"
16670                "    test{ 7,     5,    \"!!\"},\n"
16671                "};\n",
16672                Style);
16673 
16674   verifyFormat("test demo[] = {\n"
16675                "    {56,    23, \"hello\"},\n"
16676                "#if X\n"
16677                "    {-1, 93463, \"world\"},\n"
16678                "#endif\n"
16679                "    { 7,     5,    \"!!\"}\n"
16680                "};\n",
16681                Style);
16682 
16683   verifyFormat(
16684       "test demo[] = {\n"
16685       "    { 7,    23,\n"
16686       "     \"hello world i am a very long line that really, in any\"\n"
16687       "     \"just world, ought to be split over multiple lines\"},\n"
16688       "    {-1, 93463,                                  \"world\"},\n"
16689       "    {56,     5,                                     \"!!\"}\n"
16690       "};\n",
16691       Style);
16692 
16693   verifyFormat("return GradForUnaryCwise(g, {\n"
16694                "                                {{\"sign\"}, \"Sign\",  "
16695                "  {\"x\", \"dy\"}},\n"
16696                "                                {  {\"dx\"},  \"Mul\", {\"dy\""
16697                ", \"sign\"}},\n"
16698                "});\n",
16699                Style);
16700 
16701   Style.ColumnLimit = 0;
16702   EXPECT_EQ(
16703       "test demo[] = {\n"
16704       "    {56,    23, \"hello world i am a very long line that really, "
16705       "in any just world, ought to be split over multiple lines\"},\n"
16706       "    {-1, 93463,                                                  "
16707       "                                                 \"world\"},\n"
16708       "    { 7,     5,                                                  "
16709       "                                                    \"!!\"},\n"
16710       "};",
16711       format("test demo[] = {{56, 23, \"hello world i am a very long line "
16712              "that really, in any just world, ought to be split over multiple "
16713              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
16714              Style));
16715 
16716   Style.ColumnLimit = 80;
16717   verifyFormat("test demo[] = {\n"
16718                "    {56,    23, /* a comment */ \"hello\"},\n"
16719                "    {-1, 93463,                 \"world\"},\n"
16720                "    { 7,     5,                    \"!!\"}\n"
16721                "};\n",
16722                Style);
16723 
16724   verifyFormat("test demo[] = {\n"
16725                "    {56,    23,                    \"hello\"},\n"
16726                "    {-1, 93463, \"world\" /* comment here */},\n"
16727                "    { 7,     5,                       \"!!\"}\n"
16728                "};\n",
16729                Style);
16730 
16731   verifyFormat("test demo[] = {\n"
16732                "    {56, /* a comment */ 23, \"hello\"},\n"
16733                "    {-1,              93463, \"world\"},\n"
16734                "    { 7,                  5,    \"!!\"}\n"
16735                "};\n",
16736                Style);
16737 
16738   Style.ColumnLimit = 20;
16739   EXPECT_EQ(
16740       "demo = std::array<\n"
16741       "    struct test, 3>{\n"
16742       "    test{\n"
16743       "         56,    23,\n"
16744       "         \"hello \"\n"
16745       "         \"world i \"\n"
16746       "         \"am a very \"\n"
16747       "         \"long line \"\n"
16748       "         \"that \"\n"
16749       "         \"really, \"\n"
16750       "         \"in any \"\n"
16751       "         \"just \"\n"
16752       "         \"world, \"\n"
16753       "         \"ought to \"\n"
16754       "         \"be split \"\n"
16755       "         \"over \"\n"
16756       "         \"multiple \"\n"
16757       "         \"lines\"},\n"
16758       "    test{-1, 93463,\n"
16759       "         \"world\"},\n"
16760       "    test{ 7,     5,\n"
16761       "         \"!!\"   },\n"
16762       "};",
16763       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
16764              "i am a very long line that really, in any just world, ought "
16765              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
16766              "test{7, 5, \"!!\"},};",
16767              Style));
16768   // This caused a core dump by enabling Alignment in the LLVMStyle globally
16769   Style = getLLVMStyleWithColumns(50);
16770   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
16771   verifyFormat("static A x = {\n"
16772                "    {{init1, init2, init3, init4},\n"
16773                "     {init1, init2, init3, init4}}\n"
16774                "};",
16775                Style);
16776   Style.ColumnLimit = 100;
16777   EXPECT_EQ(
16778       "test demo[] = {\n"
16779       "    {56,    23,\n"
16780       "     \"hello world i am a very long line that really, in any just world"
16781       ", ought to be split over \"\n"
16782       "     \"multiple lines\"  },\n"
16783       "    {-1, 93463, \"world\"},\n"
16784       "    { 7,     5,    \"!!\"},\n"
16785       "};",
16786       format("test demo[] = {{56, 23, \"hello world i am a very long line "
16787              "that really, in any just world, ought to be split over multiple "
16788              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
16789              Style));
16790 
16791   Style = getLLVMStyleWithColumns(50);
16792   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
16793   Style.AlignConsecutiveAssignments =
16794       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
16795   Style.AlignConsecutiveDeclarations =
16796       FormatStyle::AlignConsecutiveStyle::ACS_Consecutive;
16797   verifyFormat("struct test demo[] = {\n"
16798                "    {56,    23, \"hello\"},\n"
16799                "    {-1, 93463, \"world\"},\n"
16800                "    { 7,     5,    \"!!\"}\n"
16801                "};\n"
16802                "static A x = {\n"
16803                "    {{init1, init2, init3, init4},\n"
16804                "     {init1, init2, init3, init4}}\n"
16805                "};",
16806                Style);
16807   Style.ColumnLimit = 100;
16808   Style.AlignConsecutiveAssignments =
16809       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
16810   Style.AlignConsecutiveDeclarations =
16811       FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments;
16812   verifyFormat("struct test demo[] = {\n"
16813                "    {56,    23, \"hello\"},\n"
16814                "    {-1, 93463, \"world\"},\n"
16815                "    { 7,     5,    \"!!\"}\n"
16816                "};\n"
16817                "struct test demo[4] = {\n"
16818                "    { 56,    23, 21,       \"oh\"}, // first line\n"
16819                "    { -1, 93463, 22,       \"my\"}, // second line\n"
16820                "    {  7,     5,  1, \"goodness\"}  // third line\n"
16821                "    {234,     5,  1, \"gracious\"}  // fourth line\n"
16822                "};\n",
16823                Style);
16824   EXPECT_EQ(
16825       "test demo[] = {\n"
16826       "    {56,\n"
16827       "     \"hello world i am a very long line that really, in any just world"
16828       ", ought to be split over \"\n"
16829       "     \"multiple lines\",    23},\n"
16830       "    {-1,      \"world\", 93463},\n"
16831       "    { 7,         \"!!\",     5},\n"
16832       "};",
16833       format("test demo[] = {{56, \"hello world i am a very long line "
16834              "that really, in any just world, ought to be split over multiple "
16835              "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
16836              Style));
16837 }
16838 
16839 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
16840   auto Style = getLLVMStyle();
16841   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
16842   verifyFormat("struct test demo[] = {\n"
16843                "    {56, 23,    \"hello\"},\n"
16844                "    {-1, 93463, \"world\"},\n"
16845                "    {7,  5,     \"!!\"   }\n"
16846                "};\n",
16847                Style);
16848 
16849   verifyFormat("struct test demo[] = {\n"
16850                "    {56, 23,    \"hello\"}, // first line\n"
16851                "    {-1, 93463, \"world\"}, // second line\n"
16852                "    {7,  5,     \"!!\"   }  // third line\n"
16853                "};\n",
16854                Style);
16855   verifyFormat("struct test demo[4] = {\n"
16856                "    {56,  23,    21, \"oh\"      }, // first line\n"
16857                "    {-1,  93463, 22, \"my\"      }, // second line\n"
16858                "    {7,   5,     1,  \"goodness\"}  // third line\n"
16859                "    {234, 5,     1,  \"gracious\"}  // fourth line\n"
16860                "};\n",
16861                Style);
16862   verifyFormat("struct test demo[3] = {\n"
16863                "    {56, 23,    \"hello\"},\n"
16864                "    {-1, 93463, \"world\"},\n"
16865                "    {7,  5,     \"!!\"   }\n"
16866                "};\n",
16867                Style);
16868 
16869   verifyFormat("struct test demo[3] = {\n"
16870                "    {int{56}, 23,    \"hello\"},\n"
16871                "    {int{-1}, 93463, \"world\"},\n"
16872                "    {int{7},  5,     \"!!\"   }\n"
16873                "};\n",
16874                Style);
16875   verifyFormat("struct test demo[] = {\n"
16876                "    {56, 23,    \"hello\"},\n"
16877                "    {-1, 93463, \"world\"},\n"
16878                "    {7,  5,     \"!!\"   },\n"
16879                "};\n",
16880                Style);
16881   verifyFormat("test demo[] = {\n"
16882                "    {56, 23,    \"hello\"},\n"
16883                "    {-1, 93463, \"world\"},\n"
16884                "    {7,  5,     \"!!\"   },\n"
16885                "};\n",
16886                Style);
16887   verifyFormat("demo = std::array<struct test, 3>{\n"
16888                "    test{56, 23,    \"hello\"},\n"
16889                "    test{-1, 93463, \"world\"},\n"
16890                "    test{7,  5,     \"!!\"   },\n"
16891                "};\n",
16892                Style);
16893   verifyFormat("test demo[] = {\n"
16894                "    {56, 23,    \"hello\"},\n"
16895                "#if X\n"
16896                "    {-1, 93463, \"world\"},\n"
16897                "#endif\n"
16898                "    {7,  5,     \"!!\"   }\n"
16899                "};\n",
16900                Style);
16901   verifyFormat(
16902       "test demo[] = {\n"
16903       "    {7,  23,\n"
16904       "     \"hello world i am a very long line that really, in any\"\n"
16905       "     \"just world, ought to be split over multiple lines\"},\n"
16906       "    {-1, 93463, \"world\"                                 },\n"
16907       "    {56, 5,     \"!!\"                                    }\n"
16908       "};\n",
16909       Style);
16910 
16911   verifyFormat("return GradForUnaryCwise(g, {\n"
16912                "                                {{\"sign\"}, \"Sign\", {\"x\", "
16913                "\"dy\"}   },\n"
16914                "                                {{\"dx\"},   \"Mul\",  "
16915                "{\"dy\", \"sign\"}},\n"
16916                "});\n",
16917                Style);
16918 
16919   Style.ColumnLimit = 0;
16920   EXPECT_EQ(
16921       "test demo[] = {\n"
16922       "    {56, 23,    \"hello world i am a very long line that really, in any "
16923       "just world, ought to be split over multiple lines\"},\n"
16924       "    {-1, 93463, \"world\"                                               "
16925       "                                                   },\n"
16926       "    {7,  5,     \"!!\"                                                  "
16927       "                                                   },\n"
16928       "};",
16929       format("test demo[] = {{56, 23, \"hello world i am a very long line "
16930              "that really, in any just world, ought to be split over multiple "
16931              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
16932              Style));
16933 
16934   Style.ColumnLimit = 80;
16935   verifyFormat("test demo[] = {\n"
16936                "    {56, 23,    /* a comment */ \"hello\"},\n"
16937                "    {-1, 93463, \"world\"                },\n"
16938                "    {7,  5,     \"!!\"                   }\n"
16939                "};\n",
16940                Style);
16941 
16942   verifyFormat("test demo[] = {\n"
16943                "    {56, 23,    \"hello\"                   },\n"
16944                "    {-1, 93463, \"world\" /* comment here */},\n"
16945                "    {7,  5,     \"!!\"                      }\n"
16946                "};\n",
16947                Style);
16948 
16949   verifyFormat("test demo[] = {\n"
16950                "    {56, /* a comment */ 23, \"hello\"},\n"
16951                "    {-1, 93463,              \"world\"},\n"
16952                "    {7,  5,                  \"!!\"   }\n"
16953                "};\n",
16954                Style);
16955 
16956   Style.ColumnLimit = 20;
16957   EXPECT_EQ(
16958       "demo = std::array<\n"
16959       "    struct test, 3>{\n"
16960       "    test{\n"
16961       "         56, 23,\n"
16962       "         \"hello \"\n"
16963       "         \"world i \"\n"
16964       "         \"am a very \"\n"
16965       "         \"long line \"\n"
16966       "         \"that \"\n"
16967       "         \"really, \"\n"
16968       "         \"in any \"\n"
16969       "         \"just \"\n"
16970       "         \"world, \"\n"
16971       "         \"ought to \"\n"
16972       "         \"be split \"\n"
16973       "         \"over \"\n"
16974       "         \"multiple \"\n"
16975       "         \"lines\"},\n"
16976       "    test{-1, 93463,\n"
16977       "         \"world\"},\n"
16978       "    test{7,  5,\n"
16979       "         \"!!\"   },\n"
16980       "};",
16981       format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
16982              "i am a very long line that really, in any just world, ought "
16983              "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
16984              "test{7, 5, \"!!\"},};",
16985              Style));
16986 
16987   // This caused a core dump by enabling Alignment in the LLVMStyle globally
16988   Style = getLLVMStyleWithColumns(50);
16989   Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
16990   verifyFormat("static A x = {\n"
16991                "    {{init1, init2, init3, init4},\n"
16992                "     {init1, init2, init3, init4}}\n"
16993                "};",
16994                Style);
16995   Style.ColumnLimit = 100;
16996   EXPECT_EQ(
16997       "test demo[] = {\n"
16998       "    {56, 23,\n"
16999       "     \"hello world i am a very long line that really, in any just world"
17000       ", ought to be split over \"\n"
17001       "     \"multiple lines\"  },\n"
17002       "    {-1, 93463, \"world\"},\n"
17003       "    {7,  5,     \"!!\"   },\n"
17004       "};",
17005       format("test demo[] = {{56, 23, \"hello world i am a very long line "
17006              "that really, in any just world, ought to be split over multiple "
17007              "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
17008              Style));
17009 }
17010 
17011 TEST_F(FormatTest, UnderstandsPragmas) {
17012   verifyFormat("#pragma omp reduction(| : var)");
17013   verifyFormat("#pragma omp reduction(+ : var)");
17014 
17015   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
17016             "(including parentheses).",
17017             format("#pragma    mark   Any non-hyphenated or hyphenated string "
17018                    "(including parentheses)."));
17019 }
17020 
17021 TEST_F(FormatTest, UnderstandPragmaOption) {
17022   verifyFormat("#pragma option -C -A");
17023 
17024   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
17025 }
17026 
17027 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
17028   FormatStyle Style = getLLVMStyle();
17029   Style.ColumnLimit = 20;
17030 
17031   // See PR41213
17032   EXPECT_EQ("/*\n"
17033             " *\t9012345\n"
17034             " * /8901\n"
17035             " */",
17036             format("/*\n"
17037                    " *\t9012345 /8901\n"
17038                    " */",
17039                    Style));
17040   EXPECT_EQ("/*\n"
17041             " *345678\n"
17042             " *\t/8901\n"
17043             " */",
17044             format("/*\n"
17045                    " *345678\t/8901\n"
17046                    " */",
17047                    Style));
17048 
17049   verifyFormat("int a; // the\n"
17050                "       // comment",
17051                Style);
17052   EXPECT_EQ("int a; /* first line\n"
17053             "        * second\n"
17054             "        * line third\n"
17055             "        * line\n"
17056             "        */",
17057             format("int a; /* first line\n"
17058                    "        * second\n"
17059                    "        * line third\n"
17060                    "        * line\n"
17061                    "        */",
17062                    Style));
17063   EXPECT_EQ("int a; // first line\n"
17064             "       // second\n"
17065             "       // line third\n"
17066             "       // line",
17067             format("int a; // first line\n"
17068                    "       // second line\n"
17069                    "       // third line",
17070                    Style));
17071 
17072   Style.PenaltyExcessCharacter = 90;
17073   verifyFormat("int a; // the comment", Style);
17074   EXPECT_EQ("int a; // the comment\n"
17075             "       // aaa",
17076             format("int a; // the comment aaa", Style));
17077   EXPECT_EQ("int a; /* first line\n"
17078             "        * second line\n"
17079             "        * third line\n"
17080             "        */",
17081             format("int a; /* first line\n"
17082                    "        * second line\n"
17083                    "        * third line\n"
17084                    "        */",
17085                    Style));
17086   EXPECT_EQ("int a; // first line\n"
17087             "       // second line\n"
17088             "       // third line",
17089             format("int a; // first line\n"
17090                    "       // second line\n"
17091                    "       // third line",
17092                    Style));
17093   // FIXME: Investigate why this is not getting the same layout as the test
17094   // above.
17095   EXPECT_EQ("int a; /* first line\n"
17096             "        * second line\n"
17097             "        * third line\n"
17098             "        */",
17099             format("int a; /* first line second line third line"
17100                    "\n*/",
17101                    Style));
17102 
17103   EXPECT_EQ("// foo bar baz bazfoo\n"
17104             "// foo bar foo bar\n",
17105             format("// foo bar baz bazfoo\n"
17106                    "// foo bar foo           bar\n",
17107                    Style));
17108   EXPECT_EQ("// foo bar baz bazfoo\n"
17109             "// foo bar foo bar\n",
17110             format("// foo bar baz      bazfoo\n"
17111                    "// foo            bar foo bar\n",
17112                    Style));
17113 
17114   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
17115   // next one.
17116   EXPECT_EQ("// foo bar baz bazfoo\n"
17117             "// bar foo bar\n",
17118             format("// foo bar baz      bazfoo bar\n"
17119                    "// foo            bar\n",
17120                    Style));
17121 
17122   EXPECT_EQ("// foo bar baz bazfoo\n"
17123             "// foo bar baz bazfoo\n"
17124             "// bar foo bar\n",
17125             format("// foo bar baz      bazfoo\n"
17126                    "// foo bar baz      bazfoo bar\n"
17127                    "// foo bar\n",
17128                    Style));
17129 
17130   EXPECT_EQ("// foo bar baz bazfoo\n"
17131             "// foo bar baz bazfoo\n"
17132             "// bar foo bar\n",
17133             format("// foo bar baz      bazfoo\n"
17134                    "// foo bar baz      bazfoo bar\n"
17135                    "// foo           bar\n",
17136                    Style));
17137 
17138   // Make sure we do not keep protruding characters if strict mode reflow is
17139   // cheaper than keeping protruding characters.
17140   Style.ColumnLimit = 21;
17141   EXPECT_EQ(
17142       "// foo foo foo foo\n"
17143       "// foo foo foo foo\n"
17144       "// foo foo foo foo\n",
17145       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
17146 
17147   EXPECT_EQ("int a = /* long block\n"
17148             "           comment */\n"
17149             "    42;",
17150             format("int a = /* long block comment */ 42;", Style));
17151 }
17152 
17153 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
17154   for (size_t i = 1; i < Styles.size(); ++i)                                   \
17155   EXPECT_EQ(Styles[0], Styles[i])                                              \
17156       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
17157 
17158 TEST_F(FormatTest, GetsPredefinedStyleByName) {
17159   SmallVector<FormatStyle, 3> Styles;
17160   Styles.resize(3);
17161 
17162   Styles[0] = getLLVMStyle();
17163   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
17164   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
17165   EXPECT_ALL_STYLES_EQUAL(Styles);
17166 
17167   Styles[0] = getGoogleStyle();
17168   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
17169   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
17170   EXPECT_ALL_STYLES_EQUAL(Styles);
17171 
17172   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
17173   EXPECT_TRUE(
17174       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
17175   EXPECT_TRUE(
17176       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
17177   EXPECT_ALL_STYLES_EQUAL(Styles);
17178 
17179   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
17180   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
17181   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
17182   EXPECT_ALL_STYLES_EQUAL(Styles);
17183 
17184   Styles[0] = getMozillaStyle();
17185   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
17186   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
17187   EXPECT_ALL_STYLES_EQUAL(Styles);
17188 
17189   Styles[0] = getWebKitStyle();
17190   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
17191   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
17192   EXPECT_ALL_STYLES_EQUAL(Styles);
17193 
17194   Styles[0] = getGNUStyle();
17195   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
17196   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
17197   EXPECT_ALL_STYLES_EQUAL(Styles);
17198 
17199   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
17200 }
17201 
17202 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
17203   SmallVector<FormatStyle, 8> Styles;
17204   Styles.resize(2);
17205 
17206   Styles[0] = getGoogleStyle();
17207   Styles[1] = getLLVMStyle();
17208   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
17209   EXPECT_ALL_STYLES_EQUAL(Styles);
17210 
17211   Styles.resize(5);
17212   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
17213   Styles[1] = getLLVMStyle();
17214   Styles[1].Language = FormatStyle::LK_JavaScript;
17215   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
17216 
17217   Styles[2] = getLLVMStyle();
17218   Styles[2].Language = FormatStyle::LK_JavaScript;
17219   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
17220                                   "BasedOnStyle: Google",
17221                                   &Styles[2])
17222                    .value());
17223 
17224   Styles[3] = getLLVMStyle();
17225   Styles[3].Language = FormatStyle::LK_JavaScript;
17226   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
17227                                   "Language: JavaScript",
17228                                   &Styles[3])
17229                    .value());
17230 
17231   Styles[4] = getLLVMStyle();
17232   Styles[4].Language = FormatStyle::LK_JavaScript;
17233   EXPECT_EQ(0, parseConfiguration("---\n"
17234                                   "BasedOnStyle: LLVM\n"
17235                                   "IndentWidth: 123\n"
17236                                   "---\n"
17237                                   "BasedOnStyle: Google\n"
17238                                   "Language: JavaScript",
17239                                   &Styles[4])
17240                    .value());
17241   EXPECT_ALL_STYLES_EQUAL(Styles);
17242 }
17243 
17244 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
17245   Style.FIELD = false;                                                         \
17246   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
17247   EXPECT_TRUE(Style.FIELD);                                                    \
17248   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
17249   EXPECT_FALSE(Style.FIELD);
17250 
17251 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
17252 
17253 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
17254   Style.STRUCT.FIELD = false;                                                  \
17255   EXPECT_EQ(0,                                                                 \
17256             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
17257                 .value());                                                     \
17258   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
17259   EXPECT_EQ(0,                                                                 \
17260             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
17261                 .value());                                                     \
17262   EXPECT_FALSE(Style.STRUCT.FIELD);
17263 
17264 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
17265   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
17266 
17267 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
17268   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
17269   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
17270   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
17271 
17272 TEST_F(FormatTest, ParsesConfigurationBools) {
17273   FormatStyle Style = {};
17274   Style.Language = FormatStyle::LK_Cpp;
17275   CHECK_PARSE_BOOL(AlignTrailingComments);
17276   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
17277   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
17278   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
17279   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
17280   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
17281   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
17282   CHECK_PARSE_BOOL(BinPackArguments);
17283   CHECK_PARSE_BOOL(BinPackParameters);
17284   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
17285   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
17286   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
17287   CHECK_PARSE_BOOL(BreakStringLiterals);
17288   CHECK_PARSE_BOOL(CompactNamespaces);
17289   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
17290   CHECK_PARSE_BOOL(DeriveLineEnding);
17291   CHECK_PARSE_BOOL(DerivePointerAlignment);
17292   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
17293   CHECK_PARSE_BOOL(DisableFormat);
17294   CHECK_PARSE_BOOL(IndentAccessModifiers);
17295   CHECK_PARSE_BOOL(IndentCaseLabels);
17296   CHECK_PARSE_BOOL(IndentCaseBlocks);
17297   CHECK_PARSE_BOOL(IndentGotoLabels);
17298   CHECK_PARSE_BOOL(IndentRequires);
17299   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
17300   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
17301   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
17302   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
17303   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
17304   CHECK_PARSE_BOOL(ReflowComments);
17305   CHECK_PARSE_BOOL(SortUsingDeclarations);
17306   CHECK_PARSE_BOOL(SpacesInParentheses);
17307   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
17308   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
17309   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
17310   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
17311   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
17312   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
17313   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
17314   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
17315   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
17316   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
17317   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
17318   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
17319   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
17320   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
17321   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
17322   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
17323   CHECK_PARSE_BOOL(UseCRLF);
17324 
17325   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
17326   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
17327   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
17328   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
17329   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
17330   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
17331   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
17332   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
17333   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
17334   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
17335   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
17336   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
17337   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
17338   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
17339   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
17340   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
17341   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
17342 }
17343 
17344 #undef CHECK_PARSE_BOOL
17345 
17346 TEST_F(FormatTest, ParsesConfiguration) {
17347   FormatStyle Style = {};
17348   Style.Language = FormatStyle::LK_Cpp;
17349   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
17350   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
17351               ConstructorInitializerIndentWidth, 1234u);
17352   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
17353   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
17354   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
17355   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
17356   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
17357               PenaltyBreakBeforeFirstCallParameter, 1234u);
17358   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
17359               PenaltyBreakTemplateDeclaration, 1234u);
17360   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
17361   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
17362               PenaltyReturnTypeOnItsOwnLine, 1234u);
17363   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
17364               SpacesBeforeTrailingComments, 1234u);
17365   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
17366   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
17367   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
17368 
17369   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
17370   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
17371               FormatStyle::ACS_None);
17372   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
17373               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
17374   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
17375               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
17376   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
17377               AlignConsecutiveAssignments,
17378               FormatStyle::ACS_AcrossEmptyLinesAndComments);
17379   // For backwards compability, false / true should still parse
17380   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
17381               FormatStyle::ACS_None);
17382   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
17383               FormatStyle::ACS_Consecutive);
17384 
17385   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
17386   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
17387               FormatStyle::ACS_None);
17388   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
17389               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
17390   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
17391               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
17392   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
17393               AlignConsecutiveBitFields,
17394               FormatStyle::ACS_AcrossEmptyLinesAndComments);
17395   // For backwards compability, false / true should still parse
17396   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
17397               FormatStyle::ACS_None);
17398   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
17399               FormatStyle::ACS_Consecutive);
17400 
17401   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
17402   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
17403               FormatStyle::ACS_None);
17404   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
17405               FormatStyle::ACS_Consecutive);
17406   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
17407               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
17408   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
17409               AlignConsecutiveMacros,
17410               FormatStyle::ACS_AcrossEmptyLinesAndComments);
17411   // For backwards compability, false / true should still parse
17412   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
17413               FormatStyle::ACS_None);
17414   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
17415               FormatStyle::ACS_Consecutive);
17416 
17417   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
17418   CHECK_PARSE("AlignConsecutiveDeclarations: None",
17419               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17420   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
17421               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
17422   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
17423               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
17424   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
17425               AlignConsecutiveDeclarations,
17426               FormatStyle::ACS_AcrossEmptyLinesAndComments);
17427   // For backwards compability, false / true should still parse
17428   CHECK_PARSE("AlignConsecutiveDeclarations: false",
17429               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
17430   CHECK_PARSE("AlignConsecutiveDeclarations: true",
17431               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
17432 
17433   Style.PointerAlignment = FormatStyle::PAS_Middle;
17434   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
17435               FormatStyle::PAS_Left);
17436   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
17437               FormatStyle::PAS_Right);
17438   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
17439               FormatStyle::PAS_Middle);
17440   // For backward compatibility:
17441   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
17442               FormatStyle::PAS_Left);
17443   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
17444               FormatStyle::PAS_Right);
17445   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
17446               FormatStyle::PAS_Middle);
17447 
17448   Style.Standard = FormatStyle::LS_Auto;
17449   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
17450   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
17451   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
17452   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
17453   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
17454   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
17455   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
17456   // Legacy aliases:
17457   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
17458   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
17459   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
17460   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
17461 
17462   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17463   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
17464               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
17465   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
17466               FormatStyle::BOS_None);
17467   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
17468               FormatStyle::BOS_All);
17469   // For backward compatibility:
17470   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
17471               FormatStyle::BOS_None);
17472   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
17473               FormatStyle::BOS_All);
17474 
17475   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
17476   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
17477               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
17478   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
17479               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
17480   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
17481               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
17482   // For backward compatibility:
17483   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
17484               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
17485 
17486   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17487   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
17488               FormatStyle::BILS_AfterComma);
17489   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
17490               FormatStyle::BILS_BeforeComma);
17491   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
17492               FormatStyle::BILS_AfterColon);
17493   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
17494               FormatStyle::BILS_BeforeColon);
17495   // For backward compatibility:
17496   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
17497               FormatStyle::BILS_BeforeComma);
17498 
17499   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
17500   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
17501               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
17502   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
17503               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
17504   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
17505               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
17506   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
17507               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
17508 
17509   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17510   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
17511               FormatStyle::BAS_Align);
17512   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
17513               FormatStyle::BAS_DontAlign);
17514   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
17515               FormatStyle::BAS_AlwaysBreak);
17516   // For backward compatibility:
17517   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
17518               FormatStyle::BAS_DontAlign);
17519   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
17520               FormatStyle::BAS_Align);
17521 
17522   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
17523   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
17524               FormatStyle::ENAS_DontAlign);
17525   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
17526               FormatStyle::ENAS_Left);
17527   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
17528               FormatStyle::ENAS_Right);
17529   // For backward compatibility:
17530   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
17531               FormatStyle::ENAS_Left);
17532   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
17533               FormatStyle::ENAS_Right);
17534 
17535   Style.AlignOperands = FormatStyle::OAS_Align;
17536   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
17537               FormatStyle::OAS_DontAlign);
17538   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
17539   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
17540               FormatStyle::OAS_AlignAfterOperator);
17541   // For backward compatibility:
17542   CHECK_PARSE("AlignOperands: false", AlignOperands,
17543               FormatStyle::OAS_DontAlign);
17544   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
17545 
17546   Style.UseTab = FormatStyle::UT_ForIndentation;
17547   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
17548   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
17549   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
17550   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
17551               FormatStyle::UT_ForContinuationAndIndentation);
17552   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
17553               FormatStyle::UT_AlignWithSpaces);
17554   // For backward compatibility:
17555   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
17556   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
17557 
17558   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
17559   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
17560               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
17561   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
17562               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
17563   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
17564               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
17565   // For backward compatibility:
17566   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
17567               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
17568   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
17569               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
17570 
17571   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
17572   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
17573               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
17574   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
17575               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
17576   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
17577               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
17578   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
17579               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
17580   // For backward compatibility:
17581   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
17582               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
17583   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
17584               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
17585 
17586   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
17587   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
17588               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
17589   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
17590               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
17591   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
17592               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
17593   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
17594               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
17595 
17596   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
17597   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
17598               FormatStyle::SBPO_Never);
17599   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
17600               FormatStyle::SBPO_Always);
17601   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
17602               FormatStyle::SBPO_ControlStatements);
17603   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
17604               FormatStyle::SBPO_NonEmptyParentheses);
17605   // For backward compatibility:
17606   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
17607               FormatStyle::SBPO_Never);
17608   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
17609               FormatStyle::SBPO_ControlStatements);
17610 
17611   Style.ColumnLimit = 123;
17612   FormatStyle BaseStyle = getLLVMStyle();
17613   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
17614   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
17615 
17616   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17617   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
17618               FormatStyle::BS_Attach);
17619   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
17620               FormatStyle::BS_Linux);
17621   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
17622               FormatStyle::BS_Mozilla);
17623   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
17624               FormatStyle::BS_Stroustrup);
17625   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
17626               FormatStyle::BS_Allman);
17627   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
17628               FormatStyle::BS_Whitesmiths);
17629   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
17630   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
17631               FormatStyle::BS_WebKit);
17632   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
17633               FormatStyle::BS_Custom);
17634 
17635   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
17636   CHECK_PARSE("BraceWrapping:\n"
17637               "  AfterControlStatement: MultiLine",
17638               BraceWrapping.AfterControlStatement,
17639               FormatStyle::BWACS_MultiLine);
17640   CHECK_PARSE("BraceWrapping:\n"
17641               "  AfterControlStatement: Always",
17642               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
17643   CHECK_PARSE("BraceWrapping:\n"
17644               "  AfterControlStatement: Never",
17645               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
17646   // For backward compatibility:
17647   CHECK_PARSE("BraceWrapping:\n"
17648               "  AfterControlStatement: true",
17649               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
17650   CHECK_PARSE("BraceWrapping:\n"
17651               "  AfterControlStatement: false",
17652               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
17653 
17654   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
17655   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
17656               FormatStyle::RTBS_None);
17657   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
17658               FormatStyle::RTBS_All);
17659   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
17660               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
17661   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
17662               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
17663   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
17664               AlwaysBreakAfterReturnType,
17665               FormatStyle::RTBS_TopLevelDefinitions);
17666 
17667   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
17668   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
17669               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
17670   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
17671               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
17672   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
17673               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
17674   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
17675               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
17676   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
17677               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
17678 
17679   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
17680   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
17681               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
17682   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
17683               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
17684   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
17685               AlwaysBreakAfterDefinitionReturnType,
17686               FormatStyle::DRTBS_TopLevel);
17687 
17688   Style.NamespaceIndentation = FormatStyle::NI_All;
17689   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
17690               FormatStyle::NI_None);
17691   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
17692               FormatStyle::NI_Inner);
17693   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
17694               FormatStyle::NI_All);
17695 
17696   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
17697   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
17698               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
17699   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
17700               AllowShortIfStatementsOnASingleLine,
17701               FormatStyle::SIS_WithoutElse);
17702   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
17703               AllowShortIfStatementsOnASingleLine,
17704               FormatStyle::SIS_OnlyFirstIf);
17705   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
17706               AllowShortIfStatementsOnASingleLine,
17707               FormatStyle::SIS_AllIfsAndElse);
17708   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
17709               AllowShortIfStatementsOnASingleLine,
17710               FormatStyle::SIS_OnlyFirstIf);
17711   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
17712               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
17713   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
17714               AllowShortIfStatementsOnASingleLine,
17715               FormatStyle::SIS_WithoutElse);
17716 
17717   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
17718   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
17719               FormatStyle::IEBS_AfterExternBlock);
17720   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
17721               FormatStyle::IEBS_Indent);
17722   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
17723               FormatStyle::IEBS_NoIndent);
17724   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
17725               FormatStyle::IEBS_Indent);
17726   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
17727               FormatStyle::IEBS_NoIndent);
17728 
17729   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
17730   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
17731               FormatStyle::BFCS_Both);
17732   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
17733               FormatStyle::BFCS_None);
17734   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
17735               FormatStyle::BFCS_Before);
17736   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
17737               FormatStyle::BFCS_After);
17738 
17739   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
17740   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
17741               FormatStyle::SJSIO_After);
17742   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
17743               FormatStyle::SJSIO_Before);
17744 
17745   // FIXME: This is required because parsing a configuration simply overwrites
17746   // the first N elements of the list instead of resetting it.
17747   Style.ForEachMacros.clear();
17748   std::vector<std::string> BoostForeach;
17749   BoostForeach.push_back("BOOST_FOREACH");
17750   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
17751   std::vector<std::string> BoostAndQForeach;
17752   BoostAndQForeach.push_back("BOOST_FOREACH");
17753   BoostAndQForeach.push_back("Q_FOREACH");
17754   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
17755               BoostAndQForeach);
17756 
17757   Style.AttributeMacros.clear();
17758   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
17759               std::vector<std::string>{"__capability"});
17760   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
17761               std::vector<std::string>({"attr1", "attr2"}));
17762 
17763   Style.StatementAttributeLikeMacros.clear();
17764   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
17765               StatementAttributeLikeMacros,
17766               std::vector<std::string>({"emit", "Q_EMIT"}));
17767 
17768   Style.StatementMacros.clear();
17769   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
17770               std::vector<std::string>{"QUNUSED"});
17771   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
17772               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
17773 
17774   Style.NamespaceMacros.clear();
17775   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
17776               std::vector<std::string>{"TESTSUITE"});
17777   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
17778               std::vector<std::string>({"TESTSUITE", "SUITE"}));
17779 
17780   Style.WhitespaceSensitiveMacros.clear();
17781   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
17782               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
17783   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
17784               WhitespaceSensitiveMacros,
17785               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
17786   Style.WhitespaceSensitiveMacros.clear();
17787   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
17788               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
17789   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
17790               WhitespaceSensitiveMacros,
17791               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
17792 
17793   Style.IncludeStyle.IncludeCategories.clear();
17794   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
17795       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
17796   CHECK_PARSE("IncludeCategories:\n"
17797               "  - Regex: abc/.*\n"
17798               "    Priority: 2\n"
17799               "  - Regex: .*\n"
17800               "    Priority: 1\n"
17801               "    CaseSensitive: true\n",
17802               IncludeStyle.IncludeCategories, ExpectedCategories);
17803   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
17804               "abc$");
17805   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
17806               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
17807 
17808   Style.SortIncludes = FormatStyle::SI_Never;
17809   CHECK_PARSE("SortIncludes: true", SortIncludes,
17810               FormatStyle::SI_CaseSensitive);
17811   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
17812   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
17813               FormatStyle::SI_CaseInsensitive);
17814   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
17815               FormatStyle::SI_CaseSensitive);
17816   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
17817 
17818   Style.RawStringFormats.clear();
17819   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
17820       {
17821           FormatStyle::LK_TextProto,
17822           {"pb", "proto"},
17823           {"PARSE_TEXT_PROTO"},
17824           /*CanonicalDelimiter=*/"",
17825           "llvm",
17826       },
17827       {
17828           FormatStyle::LK_Cpp,
17829           {"cc", "cpp"},
17830           {"C_CODEBLOCK", "CPPEVAL"},
17831           /*CanonicalDelimiter=*/"cc",
17832           /*BasedOnStyle=*/"",
17833       },
17834   };
17835 
17836   CHECK_PARSE("RawStringFormats:\n"
17837               "  - Language: TextProto\n"
17838               "    Delimiters:\n"
17839               "      - 'pb'\n"
17840               "      - 'proto'\n"
17841               "    EnclosingFunctions:\n"
17842               "      - 'PARSE_TEXT_PROTO'\n"
17843               "    BasedOnStyle: llvm\n"
17844               "  - Language: Cpp\n"
17845               "    Delimiters:\n"
17846               "      - 'cc'\n"
17847               "      - 'cpp'\n"
17848               "    EnclosingFunctions:\n"
17849               "      - 'C_CODEBLOCK'\n"
17850               "      - 'CPPEVAL'\n"
17851               "    CanonicalDelimiter: 'cc'",
17852               RawStringFormats, ExpectedRawStringFormats);
17853 
17854   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17855               "  Minimum: 0\n"
17856               "  Maximum: 0",
17857               SpacesInLineCommentPrefix.Minimum, 0u);
17858   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
17859   Style.SpacesInLineCommentPrefix.Minimum = 1;
17860   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17861               "  Minimum: 2",
17862               SpacesInLineCommentPrefix.Minimum, 0u);
17863   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17864               "  Maximum: -1",
17865               SpacesInLineCommentPrefix.Maximum, -1u);
17866   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17867               "  Minimum: 2",
17868               SpacesInLineCommentPrefix.Minimum, 2u);
17869   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17870               "  Maximum: 1",
17871               SpacesInLineCommentPrefix.Maximum, 1u);
17872   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
17873 
17874   Style.SpacesInAngles = FormatStyle::SIAS_Always;
17875   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
17876   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
17877               FormatStyle::SIAS_Always);
17878   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
17879   // For backward compatibility:
17880   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
17881   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
17882 }
17883 
17884 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
17885   FormatStyle Style = {};
17886   Style.Language = FormatStyle::LK_Cpp;
17887   CHECK_PARSE("Language: Cpp\n"
17888               "IndentWidth: 12",
17889               IndentWidth, 12u);
17890   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
17891                                "IndentWidth: 34",
17892                                &Style),
17893             ParseError::Unsuitable);
17894   FormatStyle BinPackedTCS = {};
17895   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
17896   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
17897                                "InsertTrailingCommas: Wrapped",
17898                                &BinPackedTCS),
17899             ParseError::BinPackTrailingCommaConflict);
17900   EXPECT_EQ(12u, Style.IndentWidth);
17901   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
17902   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
17903 
17904   Style.Language = FormatStyle::LK_JavaScript;
17905   CHECK_PARSE("Language: JavaScript\n"
17906               "IndentWidth: 12",
17907               IndentWidth, 12u);
17908   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
17909   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
17910                                "IndentWidth: 34",
17911                                &Style),
17912             ParseError::Unsuitable);
17913   EXPECT_EQ(23u, Style.IndentWidth);
17914   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
17915   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
17916 
17917   CHECK_PARSE("BasedOnStyle: LLVM\n"
17918               "IndentWidth: 67",
17919               IndentWidth, 67u);
17920 
17921   CHECK_PARSE("---\n"
17922               "Language: JavaScript\n"
17923               "IndentWidth: 12\n"
17924               "---\n"
17925               "Language: Cpp\n"
17926               "IndentWidth: 34\n"
17927               "...\n",
17928               IndentWidth, 12u);
17929 
17930   Style.Language = FormatStyle::LK_Cpp;
17931   CHECK_PARSE("---\n"
17932               "Language: JavaScript\n"
17933               "IndentWidth: 12\n"
17934               "---\n"
17935               "Language: Cpp\n"
17936               "IndentWidth: 34\n"
17937               "...\n",
17938               IndentWidth, 34u);
17939   CHECK_PARSE("---\n"
17940               "IndentWidth: 78\n"
17941               "---\n"
17942               "Language: JavaScript\n"
17943               "IndentWidth: 56\n"
17944               "...\n",
17945               IndentWidth, 78u);
17946 
17947   Style.ColumnLimit = 123;
17948   Style.IndentWidth = 234;
17949   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
17950   Style.TabWidth = 345;
17951   EXPECT_FALSE(parseConfiguration("---\n"
17952                                   "IndentWidth: 456\n"
17953                                   "BreakBeforeBraces: Allman\n"
17954                                   "---\n"
17955                                   "Language: JavaScript\n"
17956                                   "IndentWidth: 111\n"
17957                                   "TabWidth: 111\n"
17958                                   "---\n"
17959                                   "Language: Cpp\n"
17960                                   "BreakBeforeBraces: Stroustrup\n"
17961                                   "TabWidth: 789\n"
17962                                   "...\n",
17963                                   &Style));
17964   EXPECT_EQ(123u, Style.ColumnLimit);
17965   EXPECT_EQ(456u, Style.IndentWidth);
17966   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
17967   EXPECT_EQ(789u, Style.TabWidth);
17968 
17969   EXPECT_EQ(parseConfiguration("---\n"
17970                                "Language: JavaScript\n"
17971                                "IndentWidth: 56\n"
17972                                "---\n"
17973                                "IndentWidth: 78\n"
17974                                "...\n",
17975                                &Style),
17976             ParseError::Error);
17977   EXPECT_EQ(parseConfiguration("---\n"
17978                                "Language: JavaScript\n"
17979                                "IndentWidth: 56\n"
17980                                "---\n"
17981                                "Language: JavaScript\n"
17982                                "IndentWidth: 78\n"
17983                                "...\n",
17984                                &Style),
17985             ParseError::Error);
17986 
17987   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
17988 }
17989 
17990 #undef CHECK_PARSE
17991 
17992 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
17993   FormatStyle Style = {};
17994   Style.Language = FormatStyle::LK_JavaScript;
17995   Style.BreakBeforeTernaryOperators = true;
17996   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
17997   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
17998 
17999   Style.BreakBeforeTernaryOperators = true;
18000   EXPECT_EQ(0, parseConfiguration("---\n"
18001                                   "BasedOnStyle: Google\n"
18002                                   "---\n"
18003                                   "Language: JavaScript\n"
18004                                   "IndentWidth: 76\n"
18005                                   "...\n",
18006                                   &Style)
18007                    .value());
18008   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
18009   EXPECT_EQ(76u, Style.IndentWidth);
18010   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
18011 }
18012 
18013 TEST_F(FormatTest, ConfigurationRoundTripTest) {
18014   FormatStyle Style = getLLVMStyle();
18015   std::string YAML = configurationAsText(Style);
18016   FormatStyle ParsedStyle = {};
18017   ParsedStyle.Language = FormatStyle::LK_Cpp;
18018   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
18019   EXPECT_EQ(Style, ParsedStyle);
18020 }
18021 
18022 TEST_F(FormatTest, WorksFor8bitEncodings) {
18023   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
18024             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
18025             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
18026             "\"\xef\xee\xf0\xf3...\"",
18027             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
18028                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
18029                    "\xef\xee\xf0\xf3...\"",
18030                    getLLVMStyleWithColumns(12)));
18031 }
18032 
18033 TEST_F(FormatTest, HandlesUTF8BOM) {
18034   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
18035   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
18036             format("\xef\xbb\xbf#include <iostream>"));
18037   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
18038             format("\xef\xbb\xbf\n#include <iostream>"));
18039 }
18040 
18041 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
18042 #if !defined(_MSC_VER)
18043 
18044 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
18045   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
18046                getLLVMStyleWithColumns(35));
18047   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
18048                getLLVMStyleWithColumns(31));
18049   verifyFormat("// Однажды в студёную зимнюю пору...",
18050                getLLVMStyleWithColumns(36));
18051   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
18052   verifyFormat("/* Однажды в студёную зимнюю пору... */",
18053                getLLVMStyleWithColumns(39));
18054   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
18055                getLLVMStyleWithColumns(35));
18056 }
18057 
18058 TEST_F(FormatTest, SplitsUTF8Strings) {
18059   // Non-printable characters' width is currently considered to be the length in
18060   // bytes in UTF8. The characters can be displayed in very different manner
18061   // (zero-width, single width with a substitution glyph, expanded to their code
18062   // (e.g. "<8d>"), so there's no single correct way to handle them.
18063   EXPECT_EQ("\"aaaaÄ\"\n"
18064             "\"\xc2\x8d\";",
18065             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18066   EXPECT_EQ("\"aaaaaaaÄ\"\n"
18067             "\"\xc2\x8d\";",
18068             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
18069   EXPECT_EQ("\"Однажды, в \"\n"
18070             "\"студёную \"\n"
18071             "\"зимнюю \"\n"
18072             "\"пору,\"",
18073             format("\"Однажды, в студёную зимнюю пору,\"",
18074                    getLLVMStyleWithColumns(13)));
18075   EXPECT_EQ(
18076       "\"一 二 三 \"\n"
18077       "\"四 五六 \"\n"
18078       "\"七 八 九 \"\n"
18079       "\"十\"",
18080       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
18081   EXPECT_EQ("\"一\t\"\n"
18082             "\"二 \t\"\n"
18083             "\"三 四 \"\n"
18084             "\"五\t\"\n"
18085             "\"六 \t\"\n"
18086             "\"七 \"\n"
18087             "\"八九十\tqq\"",
18088             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
18089                    getLLVMStyleWithColumns(11)));
18090 
18091   // UTF8 character in an escape sequence.
18092   EXPECT_EQ("\"aaaaaa\"\n"
18093             "\"\\\xC2\x8D\"",
18094             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
18095 }
18096 
18097 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
18098   EXPECT_EQ("const char *sssss =\n"
18099             "    \"一二三四五六七八\\\n"
18100             " 九 十\";",
18101             format("const char *sssss = \"一二三四五六七八\\\n"
18102                    " 九 十\";",
18103                    getLLVMStyleWithColumns(30)));
18104 }
18105 
18106 TEST_F(FormatTest, SplitsUTF8LineComments) {
18107   EXPECT_EQ("// aaaaÄ\xc2\x8d",
18108             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
18109   EXPECT_EQ("// Я из лесу\n"
18110             "// вышел; был\n"
18111             "// сильный\n"
18112             "// мороз.",
18113             format("// Я из лесу вышел; был сильный мороз.",
18114                    getLLVMStyleWithColumns(13)));
18115   EXPECT_EQ("// 一二三\n"
18116             "// 四五六七\n"
18117             "// 八  九\n"
18118             "// 十",
18119             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
18120 }
18121 
18122 TEST_F(FormatTest, SplitsUTF8BlockComments) {
18123   EXPECT_EQ("/* Гляжу,\n"
18124             " * поднимается\n"
18125             " * медленно в\n"
18126             " * гору\n"
18127             " * Лошадка,\n"
18128             " * везущая\n"
18129             " * хворосту\n"
18130             " * воз. */",
18131             format("/* Гляжу, поднимается медленно в гору\n"
18132                    " * Лошадка, везущая хворосту воз. */",
18133                    getLLVMStyleWithColumns(13)));
18134   EXPECT_EQ(
18135       "/* 一二三\n"
18136       " * 四五六七\n"
18137       " * 八  九\n"
18138       " * 十  */",
18139       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
18140   EXPECT_EQ("/* �������� ��������\n"
18141             " * ��������\n"
18142             " * ������-�� */",
18143             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
18144 }
18145 
18146 #endif // _MSC_VER
18147 
18148 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
18149   FormatStyle Style = getLLVMStyle();
18150 
18151   Style.ConstructorInitializerIndentWidth = 4;
18152   verifyFormat(
18153       "SomeClass::Constructor()\n"
18154       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
18155       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
18156       Style);
18157 
18158   Style.ConstructorInitializerIndentWidth = 2;
18159   verifyFormat(
18160       "SomeClass::Constructor()\n"
18161       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
18162       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
18163       Style);
18164 
18165   Style.ConstructorInitializerIndentWidth = 0;
18166   verifyFormat(
18167       "SomeClass::Constructor()\n"
18168       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
18169       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
18170       Style);
18171   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18172   verifyFormat(
18173       "SomeLongTemplateVariableName<\n"
18174       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
18175       Style);
18176   verifyFormat("bool smaller = 1 < "
18177                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
18178                "                       "
18179                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
18180                Style);
18181 
18182   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
18183   verifyFormat("SomeClass::Constructor() :\n"
18184                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
18185                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
18186                Style);
18187 }
18188 
18189 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
18190   FormatStyle Style = getLLVMStyle();
18191   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
18192   Style.ConstructorInitializerIndentWidth = 4;
18193   verifyFormat("SomeClass::Constructor()\n"
18194                "    : a(a)\n"
18195                "    , b(b)\n"
18196                "    , c(c) {}",
18197                Style);
18198   verifyFormat("SomeClass::Constructor()\n"
18199                "    : a(a) {}",
18200                Style);
18201 
18202   Style.ColumnLimit = 0;
18203   verifyFormat("SomeClass::Constructor()\n"
18204                "    : a(a) {}",
18205                Style);
18206   verifyFormat("SomeClass::Constructor() noexcept\n"
18207                "    : a(a) {}",
18208                Style);
18209   verifyFormat("SomeClass::Constructor()\n"
18210                "    : a(a)\n"
18211                "    , b(b)\n"
18212                "    , c(c) {}",
18213                Style);
18214   verifyFormat("SomeClass::Constructor()\n"
18215                "    : a(a) {\n"
18216                "  foo();\n"
18217                "  bar();\n"
18218                "}",
18219                Style);
18220 
18221   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
18222   verifyFormat("SomeClass::Constructor()\n"
18223                "    : a(a)\n"
18224                "    , b(b)\n"
18225                "    , c(c) {\n}",
18226                Style);
18227   verifyFormat("SomeClass::Constructor()\n"
18228                "    : a(a) {\n}",
18229                Style);
18230 
18231   Style.ColumnLimit = 80;
18232   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
18233   Style.ConstructorInitializerIndentWidth = 2;
18234   verifyFormat("SomeClass::Constructor()\n"
18235                "  : a(a)\n"
18236                "  , b(b)\n"
18237                "  , c(c) {}",
18238                Style);
18239 
18240   Style.ConstructorInitializerIndentWidth = 0;
18241   verifyFormat("SomeClass::Constructor()\n"
18242                ": a(a)\n"
18243                ", b(b)\n"
18244                ", c(c) {}",
18245                Style);
18246 
18247   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
18248   Style.ConstructorInitializerIndentWidth = 4;
18249   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
18250   verifyFormat(
18251       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
18252       Style);
18253   verifyFormat(
18254       "SomeClass::Constructor()\n"
18255       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
18256       Style);
18257   Style.ConstructorInitializerIndentWidth = 4;
18258   Style.ColumnLimit = 60;
18259   verifyFormat("SomeClass::Constructor()\n"
18260                "    : aaaaaaaa(aaaaaaaa)\n"
18261                "    , aaaaaaaa(aaaaaaaa)\n"
18262                "    , aaaaaaaa(aaaaaaaa) {}",
18263                Style);
18264 }
18265 
18266 TEST_F(FormatTest, Destructors) {
18267   verifyFormat("void F(int &i) { i.~int(); }");
18268   verifyFormat("void F(int &i) { i->~int(); }");
18269 }
18270 
18271 TEST_F(FormatTest, FormatsWithWebKitStyle) {
18272   FormatStyle Style = getWebKitStyle();
18273 
18274   // Don't indent in outer namespaces.
18275   verifyFormat("namespace outer {\n"
18276                "int i;\n"
18277                "namespace inner {\n"
18278                "    int i;\n"
18279                "} // namespace inner\n"
18280                "} // namespace outer\n"
18281                "namespace other_outer {\n"
18282                "int i;\n"
18283                "}",
18284                Style);
18285 
18286   // Don't indent case labels.
18287   verifyFormat("switch (variable) {\n"
18288                "case 1:\n"
18289                "case 2:\n"
18290                "    doSomething();\n"
18291                "    break;\n"
18292                "default:\n"
18293                "    ++variable;\n"
18294                "}",
18295                Style);
18296 
18297   // Wrap before binary operators.
18298   EXPECT_EQ("void f()\n"
18299             "{\n"
18300             "    if (aaaaaaaaaaaaaaaa\n"
18301             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
18302             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
18303             "        return;\n"
18304             "}",
18305             format("void f() {\n"
18306                    "if (aaaaaaaaaaaaaaaa\n"
18307                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
18308                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
18309                    "return;\n"
18310                    "}",
18311                    Style));
18312 
18313   // Allow functions on a single line.
18314   verifyFormat("void f() { return; }", Style);
18315 
18316   // Allow empty blocks on a single line and insert a space in empty blocks.
18317   EXPECT_EQ("void f() { }", format("void f() {}", Style));
18318   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
18319   // However, don't merge non-empty short loops.
18320   EXPECT_EQ("while (true) {\n"
18321             "    continue;\n"
18322             "}",
18323             format("while (true) { continue; }", Style));
18324 
18325   // Constructor initializers are formatted one per line with the "," on the
18326   // new line.
18327   verifyFormat("Constructor()\n"
18328                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
18329                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
18330                "          aaaaaaaaaaaaaa)\n"
18331                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
18332                "{\n"
18333                "}",
18334                Style);
18335   verifyFormat("SomeClass::Constructor()\n"
18336                "    : a(a)\n"
18337                "{\n"
18338                "}",
18339                Style);
18340   EXPECT_EQ("SomeClass::Constructor()\n"
18341             "    : a(a)\n"
18342             "{\n"
18343             "}",
18344             format("SomeClass::Constructor():a(a){}", Style));
18345   verifyFormat("SomeClass::Constructor()\n"
18346                "    : a(a)\n"
18347                "    , b(b)\n"
18348                "    , c(c)\n"
18349                "{\n"
18350                "}",
18351                Style);
18352   verifyFormat("SomeClass::Constructor()\n"
18353                "    : a(a)\n"
18354                "{\n"
18355                "    foo();\n"
18356                "    bar();\n"
18357                "}",
18358                Style);
18359 
18360   // Access specifiers should be aligned left.
18361   verifyFormat("class C {\n"
18362                "public:\n"
18363                "    int i;\n"
18364                "};",
18365                Style);
18366 
18367   // Do not align comments.
18368   verifyFormat("int a; // Do not\n"
18369                "double b; // align comments.",
18370                Style);
18371 
18372   // Do not align operands.
18373   EXPECT_EQ("ASSERT(aaaa\n"
18374             "    || bbbb);",
18375             format("ASSERT ( aaaa\n||bbbb);", Style));
18376 
18377   // Accept input's line breaks.
18378   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
18379             "    || bbbbbbbbbbbbbbb) {\n"
18380             "    i++;\n"
18381             "}",
18382             format("if (aaaaaaaaaaaaaaa\n"
18383                    "|| bbbbbbbbbbbbbbb) { i++; }",
18384                    Style));
18385   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
18386             "    i++;\n"
18387             "}",
18388             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
18389 
18390   // Don't automatically break all macro definitions (llvm.org/PR17842).
18391   verifyFormat("#define aNumber 10", Style);
18392   // However, generally keep the line breaks that the user authored.
18393   EXPECT_EQ("#define aNumber \\\n"
18394             "    10",
18395             format("#define aNumber \\\n"
18396                    " 10",
18397                    Style));
18398 
18399   // Keep empty and one-element array literals on a single line.
18400   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
18401             "                                  copyItems:YES];",
18402             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
18403                    "copyItems:YES];",
18404                    Style));
18405   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
18406             "                                  copyItems:YES];",
18407             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
18408                    "             copyItems:YES];",
18409                    Style));
18410   // FIXME: This does not seem right, there should be more indentation before
18411   // the array literal's entries. Nested blocks have the same problem.
18412   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
18413             "    @\"a\",\n"
18414             "    @\"a\"\n"
18415             "]\n"
18416             "                                  copyItems:YES];",
18417             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
18418                    "     @\"a\",\n"
18419                    "     @\"a\"\n"
18420                    "     ]\n"
18421                    "       copyItems:YES];",
18422                    Style));
18423   EXPECT_EQ(
18424       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
18425       "                                  copyItems:YES];",
18426       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
18427              "   copyItems:YES];",
18428              Style));
18429 
18430   verifyFormat("[self.a b:c c:d];", Style);
18431   EXPECT_EQ("[self.a b:c\n"
18432             "        c:d];",
18433             format("[self.a b:c\n"
18434                    "c:d];",
18435                    Style));
18436 }
18437 
18438 TEST_F(FormatTest, FormatsLambdas) {
18439   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
18440   verifyFormat(
18441       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
18442   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
18443   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
18444   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
18445   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
18446   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
18447   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
18448   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
18449   verifyFormat("int x = f(*+[] {});");
18450   verifyFormat("void f() {\n"
18451                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
18452                "}\n");
18453   verifyFormat("void f() {\n"
18454                "  other(x.begin(), //\n"
18455                "        x.end(),   //\n"
18456                "        [&](int, int) { return 1; });\n"
18457                "}\n");
18458   verifyFormat("void f() {\n"
18459                "  other.other.other.other.other(\n"
18460                "      x.begin(), x.end(),\n"
18461                "      [something, rather](int, int, int, int, int, int, int) { "
18462                "return 1; });\n"
18463                "}\n");
18464   verifyFormat(
18465       "void f() {\n"
18466       "  other.other.other.other.other(\n"
18467       "      x.begin(), x.end(),\n"
18468       "      [something, rather](int, int, int, int, int, int, int) {\n"
18469       "        //\n"
18470       "      });\n"
18471       "}\n");
18472   verifyFormat("SomeFunction([]() { // A cool function...\n"
18473                "  return 43;\n"
18474                "});");
18475   EXPECT_EQ("SomeFunction([]() {\n"
18476             "#define A a\n"
18477             "  return 43;\n"
18478             "});",
18479             format("SomeFunction([](){\n"
18480                    "#define A a\n"
18481                    "return 43;\n"
18482                    "});"));
18483   verifyFormat("void f() {\n"
18484                "  SomeFunction([](decltype(x), A *a) {});\n"
18485                "  SomeFunction([](typeof(x), A *a) {});\n"
18486                "  SomeFunction([](_Atomic(x), A *a) {});\n"
18487                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
18488                "}");
18489   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
18490                "    [](const aaaaaaaaaa &a) { return a; });");
18491   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
18492                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
18493                "});");
18494   verifyFormat("Constructor()\n"
18495                "    : Field([] { // comment\n"
18496                "        int i;\n"
18497                "      }) {}");
18498   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
18499                "  return some_parameter.size();\n"
18500                "};");
18501   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
18502                "    [](const string &s) { return s; };");
18503   verifyFormat("int i = aaaaaa ? 1 //\n"
18504                "               : [] {\n"
18505                "                   return 2; //\n"
18506                "                 }();");
18507   verifyFormat("llvm::errs() << \"number of twos is \"\n"
18508                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
18509                "                  return x == 2; // force break\n"
18510                "                });");
18511   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
18512                "    [=](int iiiiiiiiiiii) {\n"
18513                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
18514                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
18515                "    });",
18516                getLLVMStyleWithColumns(60));
18517   verifyFormat("SomeFunction({[&] {\n"
18518                "                // comment\n"
18519                "              },\n"
18520                "              [&] {\n"
18521                "                // comment\n"
18522                "              }});");
18523   verifyFormat("SomeFunction({[&] {\n"
18524                "  // comment\n"
18525                "}});");
18526   verifyFormat(
18527       "virtual aaaaaaaaaaaaaaaa(\n"
18528       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
18529       "    aaaaa aaaaaaaaa);");
18530 
18531   // Lambdas with return types.
18532   verifyFormat("int c = []() -> int { return 2; }();\n");
18533   verifyFormat("int c = []() -> int * { return 2; }();\n");
18534   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
18535   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
18536   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
18537   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
18538   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
18539   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
18540   verifyFormat("[a, a]() -> a<1> {};");
18541   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
18542   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
18543   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
18544   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
18545   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
18546   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
18547   verifyFormat("[]() -> foo<!5> { return {}; };");
18548   verifyFormat("[]() -> foo<~5> { return {}; };");
18549   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
18550   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
18551   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
18552   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
18553   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
18554   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
18555   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
18556   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
18557   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
18558   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
18559   verifyFormat("namespace bar {\n"
18560                "// broken:\n"
18561                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
18562                "} // namespace bar");
18563   verifyFormat("namespace bar {\n"
18564                "// broken:\n"
18565                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
18566                "} // namespace bar");
18567   verifyFormat("namespace bar {\n"
18568                "// broken:\n"
18569                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
18570                "} // namespace bar");
18571   verifyFormat("namespace bar {\n"
18572                "// broken:\n"
18573                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
18574                "} // namespace bar");
18575   verifyFormat("namespace bar {\n"
18576                "// broken:\n"
18577                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
18578                "} // namespace bar");
18579   verifyFormat("namespace bar {\n"
18580                "// broken:\n"
18581                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
18582                "} // namespace bar");
18583   verifyFormat("namespace bar {\n"
18584                "// broken:\n"
18585                "auto foo{[]() -> foo<!5> { return {}; }};\n"
18586                "} // namespace bar");
18587   verifyFormat("namespace bar {\n"
18588                "// broken:\n"
18589                "auto foo{[]() -> foo<~5> { return {}; }};\n"
18590                "} // namespace bar");
18591   verifyFormat("namespace bar {\n"
18592                "// broken:\n"
18593                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
18594                "} // namespace bar");
18595   verifyFormat("namespace bar {\n"
18596                "// broken:\n"
18597                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
18598                "} // namespace bar");
18599   verifyFormat("namespace bar {\n"
18600                "// broken:\n"
18601                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
18602                "} // namespace bar");
18603   verifyFormat("namespace bar {\n"
18604                "// broken:\n"
18605                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
18606                "} // namespace bar");
18607   verifyFormat("namespace bar {\n"
18608                "// broken:\n"
18609                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
18610                "} // namespace bar");
18611   verifyFormat("namespace bar {\n"
18612                "// broken:\n"
18613                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
18614                "} // namespace bar");
18615   verifyFormat("namespace bar {\n"
18616                "// broken:\n"
18617                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
18618                "} // namespace bar");
18619   verifyFormat("namespace bar {\n"
18620                "// broken:\n"
18621                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
18622                "} // namespace bar");
18623   verifyFormat("namespace bar {\n"
18624                "// broken:\n"
18625                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
18626                "} // namespace bar");
18627   verifyFormat("namespace bar {\n"
18628                "// broken:\n"
18629                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
18630                "} // namespace bar");
18631   verifyFormat("[]() -> a<1> {};");
18632   verifyFormat("[]() -> a<1> { ; };");
18633   verifyFormat("[]() -> a<1> { ; }();");
18634   verifyFormat("[a, a]() -> a<true> {};");
18635   verifyFormat("[]() -> a<true> {};");
18636   verifyFormat("[]() -> a<true> { ; };");
18637   verifyFormat("[]() -> a<true> { ; }();");
18638   verifyFormat("[a, a]() -> a<false> {};");
18639   verifyFormat("[]() -> a<false> {};");
18640   verifyFormat("[]() -> a<false> { ; };");
18641   verifyFormat("[]() -> a<false> { ; }();");
18642   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
18643   verifyFormat("namespace bar {\n"
18644                "auto foo{[]() -> foo<false> { ; }};\n"
18645                "} // namespace bar");
18646   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
18647                "                   int j) -> int {\n"
18648                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
18649                "};");
18650   verifyFormat(
18651       "aaaaaaaaaaaaaaaaaaaaaa(\n"
18652       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
18653       "      return aaaaaaaaaaaaaaaaa;\n"
18654       "    });",
18655       getLLVMStyleWithColumns(70));
18656   verifyFormat("[]() //\n"
18657                "    -> int {\n"
18658                "  return 1; //\n"
18659                "};");
18660   verifyFormat("[]() -> Void<T...> {};");
18661   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
18662 
18663   // Lambdas with explicit template argument lists.
18664   verifyFormat(
18665       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
18666 
18667   // Multiple lambdas in the same parentheses change indentation rules. These
18668   // lambdas are forced to start on new lines.
18669   verifyFormat("SomeFunction(\n"
18670                "    []() {\n"
18671                "      //\n"
18672                "    },\n"
18673                "    []() {\n"
18674                "      //\n"
18675                "    });");
18676 
18677   // A lambda passed as arg0 is always pushed to the next line.
18678   verifyFormat("SomeFunction(\n"
18679                "    [this] {\n"
18680                "      //\n"
18681                "    },\n"
18682                "    1);\n");
18683 
18684   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
18685   // the arg0 case above.
18686   auto Style = getGoogleStyle();
18687   Style.BinPackArguments = false;
18688   verifyFormat("SomeFunction(\n"
18689                "    a,\n"
18690                "    [this] {\n"
18691                "      //\n"
18692                "    },\n"
18693                "    b);\n",
18694                Style);
18695   verifyFormat("SomeFunction(\n"
18696                "    a,\n"
18697                "    [this] {\n"
18698                "      //\n"
18699                "    },\n"
18700                "    b);\n");
18701 
18702   // A lambda with a very long line forces arg0 to be pushed out irrespective of
18703   // the BinPackArguments value (as long as the code is wide enough).
18704   verifyFormat(
18705       "something->SomeFunction(\n"
18706       "    a,\n"
18707       "    [this] {\n"
18708       "      "
18709       "D0000000000000000000000000000000000000000000000000000000000001();\n"
18710       "    },\n"
18711       "    b);\n");
18712 
18713   // A multi-line lambda is pulled up as long as the introducer fits on the
18714   // previous line and there are no further args.
18715   verifyFormat("function(1, [this, that] {\n"
18716                "  //\n"
18717                "});\n");
18718   verifyFormat("function([this, that] {\n"
18719                "  //\n"
18720                "});\n");
18721   // FIXME: this format is not ideal and we should consider forcing the first
18722   // arg onto its own line.
18723   verifyFormat("function(a, b, c, //\n"
18724                "         d, [this, that] {\n"
18725                "           //\n"
18726                "         });\n");
18727 
18728   // Multiple lambdas are treated correctly even when there is a short arg0.
18729   verifyFormat("SomeFunction(\n"
18730                "    1,\n"
18731                "    [this] {\n"
18732                "      //\n"
18733                "    },\n"
18734                "    [this] {\n"
18735                "      //\n"
18736                "    },\n"
18737                "    1);\n");
18738 
18739   // More complex introducers.
18740   verifyFormat("return [i, args...] {};");
18741 
18742   // Not lambdas.
18743   verifyFormat("constexpr char hello[]{\"hello\"};");
18744   verifyFormat("double &operator[](int i) { return 0; }\n"
18745                "int i;");
18746   verifyFormat("std::unique_ptr<int[]> foo() {}");
18747   verifyFormat("int i = a[a][a]->f();");
18748   verifyFormat("int i = (*b)[a]->f();");
18749 
18750   // Other corner cases.
18751   verifyFormat("void f() {\n"
18752                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
18753                "  );\n"
18754                "}");
18755 
18756   // Lambdas created through weird macros.
18757   verifyFormat("void f() {\n"
18758                "  MACRO((const AA &a) { return 1; });\n"
18759                "  MACRO((AA &a) { return 1; });\n"
18760                "}");
18761 
18762   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
18763                "      doo_dah();\n"
18764                "      doo_dah();\n"
18765                "    })) {\n"
18766                "}");
18767   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
18768                "                doo_dah();\n"
18769                "                doo_dah();\n"
18770                "              })) {\n"
18771                "}");
18772   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
18773                "                doo_dah();\n"
18774                "                doo_dah();\n"
18775                "              })) {\n"
18776                "}");
18777   verifyFormat("auto lambda = []() {\n"
18778                "  int a = 2\n"
18779                "#if A\n"
18780                "          + 2\n"
18781                "#endif\n"
18782                "      ;\n"
18783                "};");
18784 
18785   // Lambdas with complex multiline introducers.
18786   verifyFormat(
18787       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
18788       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
18789       "        -> ::std::unordered_set<\n"
18790       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
18791       "      //\n"
18792       "    });");
18793 
18794   FormatStyle DoNotMerge = getLLVMStyle();
18795   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
18796   verifyFormat("auto c = []() {\n"
18797                "  return b;\n"
18798                "};",
18799                "auto c = []() { return b; };", DoNotMerge);
18800   verifyFormat("auto c = []() {\n"
18801                "};",
18802                " auto c = []() {};", DoNotMerge);
18803 
18804   FormatStyle MergeEmptyOnly = getLLVMStyle();
18805   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
18806   verifyFormat("auto c = []() {\n"
18807                "  return b;\n"
18808                "};",
18809                "auto c = []() {\n"
18810                "  return b;\n"
18811                " };",
18812                MergeEmptyOnly);
18813   verifyFormat("auto c = []() {};",
18814                "auto c = []() {\n"
18815                "};",
18816                MergeEmptyOnly);
18817 
18818   FormatStyle MergeInline = getLLVMStyle();
18819   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
18820   verifyFormat("auto c = []() {\n"
18821                "  return b;\n"
18822                "};",
18823                "auto c = []() { return b; };", MergeInline);
18824   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
18825                MergeInline);
18826   verifyFormat("function([]() { return b; }, a)",
18827                "function([]() { return b; }, a)", MergeInline);
18828   verifyFormat("function(a, []() { return b; })",
18829                "function(a, []() { return b; })", MergeInline);
18830 
18831   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
18832   // AllowShortLambdasOnASingleLine
18833   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
18834   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
18835   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
18836   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18837       FormatStyle::ShortLambdaStyle::SLS_None;
18838   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
18839                "    []()\n"
18840                "    {\n"
18841                "      return 17;\n"
18842                "    });",
18843                LLVMWithBeforeLambdaBody);
18844   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
18845                "    []()\n"
18846                "    {\n"
18847                "    });",
18848                LLVMWithBeforeLambdaBody);
18849   verifyFormat("auto fct_SLS_None = []()\n"
18850                "{\n"
18851                "  return 17;\n"
18852                "};",
18853                LLVMWithBeforeLambdaBody);
18854   verifyFormat("TwoNestedLambdas_SLS_None(\n"
18855                "    []()\n"
18856                "    {\n"
18857                "      return Call(\n"
18858                "          []()\n"
18859                "          {\n"
18860                "            return 17;\n"
18861                "          });\n"
18862                "    });",
18863                LLVMWithBeforeLambdaBody);
18864   verifyFormat("void Fct()\n"
18865                "{\n"
18866                "  return {[]()\n"
18867                "          {\n"
18868                "            return 17;\n"
18869                "          }};\n"
18870                "}",
18871                LLVMWithBeforeLambdaBody);
18872 
18873   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18874       FormatStyle::ShortLambdaStyle::SLS_Empty;
18875   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
18876                "    []()\n"
18877                "    {\n"
18878                "      return 17;\n"
18879                "    });",
18880                LLVMWithBeforeLambdaBody);
18881   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
18882                LLVMWithBeforeLambdaBody);
18883   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
18884                "ongFunctionName_SLS_Empty(\n"
18885                "    []() {});",
18886                LLVMWithBeforeLambdaBody);
18887   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
18888                "                                []()\n"
18889                "                                {\n"
18890                "                                  return 17;\n"
18891                "                                });",
18892                LLVMWithBeforeLambdaBody);
18893   verifyFormat("auto fct_SLS_Empty = []()\n"
18894                "{\n"
18895                "  return 17;\n"
18896                "};",
18897                LLVMWithBeforeLambdaBody);
18898   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
18899                "    []()\n"
18900                "    {\n"
18901                "      return Call([]() {});\n"
18902                "    });",
18903                LLVMWithBeforeLambdaBody);
18904   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
18905                "                           []()\n"
18906                "                           {\n"
18907                "                             return Call([]() {});\n"
18908                "                           });",
18909                LLVMWithBeforeLambdaBody);
18910   verifyFormat(
18911       "FctWithLongLineInLambda_SLS_Empty(\n"
18912       "    []()\n"
18913       "    {\n"
18914       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18915       "                               AndShouldNotBeConsiderAsInline,\n"
18916       "                               LambdaBodyMustBeBreak);\n"
18917       "    });",
18918       LLVMWithBeforeLambdaBody);
18919 
18920   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18921       FormatStyle::ShortLambdaStyle::SLS_Inline;
18922   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
18923                LLVMWithBeforeLambdaBody);
18924   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
18925                LLVMWithBeforeLambdaBody);
18926   verifyFormat("auto fct_SLS_Inline = []()\n"
18927                "{\n"
18928                "  return 17;\n"
18929                "};",
18930                LLVMWithBeforeLambdaBody);
18931   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
18932                "17; }); });",
18933                LLVMWithBeforeLambdaBody);
18934   verifyFormat(
18935       "FctWithLongLineInLambda_SLS_Inline(\n"
18936       "    []()\n"
18937       "    {\n"
18938       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18939       "                               AndShouldNotBeConsiderAsInline,\n"
18940       "                               LambdaBodyMustBeBreak);\n"
18941       "    });",
18942       LLVMWithBeforeLambdaBody);
18943   verifyFormat("FctWithMultipleParams_SLS_Inline("
18944                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
18945                "                                 []() { return 17; });",
18946                LLVMWithBeforeLambdaBody);
18947   verifyFormat(
18948       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
18949       LLVMWithBeforeLambdaBody);
18950 
18951   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18952       FormatStyle::ShortLambdaStyle::SLS_All;
18953   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
18954                LLVMWithBeforeLambdaBody);
18955   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
18956                LLVMWithBeforeLambdaBody);
18957   verifyFormat("auto fct_SLS_All = []() { return 17; };",
18958                LLVMWithBeforeLambdaBody);
18959   verifyFormat("FctWithOneParam_SLS_All(\n"
18960                "    []()\n"
18961                "    {\n"
18962                "      // A cool function...\n"
18963                "      return 43;\n"
18964                "    });",
18965                LLVMWithBeforeLambdaBody);
18966   verifyFormat("FctWithMultipleParams_SLS_All("
18967                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
18968                "                              []() { return 17; });",
18969                LLVMWithBeforeLambdaBody);
18970   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
18971                LLVMWithBeforeLambdaBody);
18972   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
18973                LLVMWithBeforeLambdaBody);
18974   verifyFormat(
18975       "FctWithLongLineInLambda_SLS_All(\n"
18976       "    []()\n"
18977       "    {\n"
18978       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18979       "                               AndShouldNotBeConsiderAsInline,\n"
18980       "                               LambdaBodyMustBeBreak);\n"
18981       "    });",
18982       LLVMWithBeforeLambdaBody);
18983   verifyFormat(
18984       "auto fct_SLS_All = []()\n"
18985       "{\n"
18986       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18987       "                           AndShouldNotBeConsiderAsInline,\n"
18988       "                           LambdaBodyMustBeBreak);\n"
18989       "};",
18990       LLVMWithBeforeLambdaBody);
18991   LLVMWithBeforeLambdaBody.BinPackParameters = false;
18992   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
18993                LLVMWithBeforeLambdaBody);
18994   verifyFormat(
18995       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
18996       "                                FirstParam,\n"
18997       "                                SecondParam,\n"
18998       "                                ThirdParam,\n"
18999       "                                FourthParam);",
19000       LLVMWithBeforeLambdaBody);
19001   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19002                "    []() { return "
19003                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
19004                "    FirstParam,\n"
19005                "    SecondParam,\n"
19006                "    ThirdParam,\n"
19007                "    FourthParam);",
19008                LLVMWithBeforeLambdaBody);
19009   verifyFormat(
19010       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
19011       "                                SecondParam,\n"
19012       "                                ThirdParam,\n"
19013       "                                FourthParam,\n"
19014       "                                []() { return SomeValueNotSoLong; });",
19015       LLVMWithBeforeLambdaBody);
19016   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
19017                "    []()\n"
19018                "    {\n"
19019                "      return "
19020                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
19021                "eConsiderAsInline;\n"
19022                "    });",
19023                LLVMWithBeforeLambdaBody);
19024   verifyFormat(
19025       "FctWithLongLineInLambda_SLS_All(\n"
19026       "    []()\n"
19027       "    {\n"
19028       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
19029       "                               AndShouldNotBeConsiderAsInline,\n"
19030       "                               LambdaBodyMustBeBreak);\n"
19031       "    });",
19032       LLVMWithBeforeLambdaBody);
19033   verifyFormat("FctWithTwoParams_SLS_All(\n"
19034                "    []()\n"
19035                "    {\n"
19036                "      // A cool function...\n"
19037                "      return 43;\n"
19038                "    },\n"
19039                "    87);",
19040                LLVMWithBeforeLambdaBody);
19041   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
19042                LLVMWithBeforeLambdaBody);
19043   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
19044                LLVMWithBeforeLambdaBody);
19045   verifyFormat(
19046       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
19047       LLVMWithBeforeLambdaBody);
19048   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
19049                "}); }, x);",
19050                LLVMWithBeforeLambdaBody);
19051   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19052                "    []()\n"
19053                "    {\n"
19054                "      // A cool function...\n"
19055                "      return Call([]() { return 17; });\n"
19056                "    });",
19057                LLVMWithBeforeLambdaBody);
19058   verifyFormat("TwoNestedLambdas_SLS_All(\n"
19059                "    []()\n"
19060                "    {\n"
19061                "      return Call(\n"
19062                "          []()\n"
19063                "          {\n"
19064                "            // A cool function...\n"
19065                "            return 17;\n"
19066                "          });\n"
19067                "    });",
19068                LLVMWithBeforeLambdaBody);
19069 }
19070 
19071 TEST_F(FormatTest, LambdaWithLineComments) {
19072   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
19073   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
19074   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
19075   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
19076       FormatStyle::ShortLambdaStyle::SLS_All;
19077 
19078   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
19079   verifyFormat("auto k = []() // comment\n"
19080                "{ return; }",
19081                LLVMWithBeforeLambdaBody);
19082   verifyFormat("auto k = []() /* comment */ { return; }",
19083                LLVMWithBeforeLambdaBody);
19084   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
19085                LLVMWithBeforeLambdaBody);
19086   verifyFormat("auto k = []() // X\n"
19087                "{ return; }",
19088                LLVMWithBeforeLambdaBody);
19089   verifyFormat(
19090       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
19091       "{ return; }",
19092       LLVMWithBeforeLambdaBody);
19093 }
19094 
19095 TEST_F(FormatTest, EmptyLinesInLambdas) {
19096   verifyFormat("auto lambda = []() {\n"
19097                "  x(); //\n"
19098                "};",
19099                "auto lambda = []() {\n"
19100                "\n"
19101                "  x(); //\n"
19102                "\n"
19103                "};");
19104 }
19105 
19106 TEST_F(FormatTest, FormatsBlocks) {
19107   FormatStyle ShortBlocks = getLLVMStyle();
19108   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
19109   verifyFormat("int (^Block)(int, int);", ShortBlocks);
19110   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
19111   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
19112   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
19113   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
19114   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
19115 
19116   verifyFormat("foo(^{ bar(); });", ShortBlocks);
19117   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
19118   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
19119 
19120   verifyFormat("[operation setCompletionBlock:^{\n"
19121                "  [self onOperationDone];\n"
19122                "}];");
19123   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
19124                "  [self onOperationDone];\n"
19125                "}]};");
19126   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
19127                "  f();\n"
19128                "}];");
19129   verifyFormat("int a = [operation block:^int(int *i) {\n"
19130                "  return 1;\n"
19131                "}];");
19132   verifyFormat("[myObject doSomethingWith:arg1\n"
19133                "                      aaa:^int(int *a) {\n"
19134                "                        return 1;\n"
19135                "                      }\n"
19136                "                      bbb:f(a * bbbbbbbb)];");
19137 
19138   verifyFormat("[operation setCompletionBlock:^{\n"
19139                "  [self.delegate newDataAvailable];\n"
19140                "}];",
19141                getLLVMStyleWithColumns(60));
19142   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
19143                "  NSString *path = [self sessionFilePath];\n"
19144                "  if (path) {\n"
19145                "    // ...\n"
19146                "  }\n"
19147                "});");
19148   verifyFormat("[[SessionService sharedService]\n"
19149                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
19150                "      if (window) {\n"
19151                "        [self windowDidLoad:window];\n"
19152                "      } else {\n"
19153                "        [self errorLoadingWindow];\n"
19154                "      }\n"
19155                "    }];");
19156   verifyFormat("void (^largeBlock)(void) = ^{\n"
19157                "  // ...\n"
19158                "};\n",
19159                getLLVMStyleWithColumns(40));
19160   verifyFormat("[[SessionService sharedService]\n"
19161                "    loadWindowWithCompletionBlock: //\n"
19162                "        ^(SessionWindow *window) {\n"
19163                "          if (window) {\n"
19164                "            [self windowDidLoad:window];\n"
19165                "          } else {\n"
19166                "            [self errorLoadingWindow];\n"
19167                "          }\n"
19168                "        }];",
19169                getLLVMStyleWithColumns(60));
19170   verifyFormat("[myObject doSomethingWith:arg1\n"
19171                "    firstBlock:^(Foo *a) {\n"
19172                "      // ...\n"
19173                "      int i;\n"
19174                "    }\n"
19175                "    secondBlock:^(Bar *b) {\n"
19176                "      // ...\n"
19177                "      int i;\n"
19178                "    }\n"
19179                "    thirdBlock:^Foo(Bar *b) {\n"
19180                "      // ...\n"
19181                "      int i;\n"
19182                "    }];");
19183   verifyFormat("[myObject doSomethingWith:arg1\n"
19184                "               firstBlock:-1\n"
19185                "              secondBlock:^(Bar *b) {\n"
19186                "                // ...\n"
19187                "                int i;\n"
19188                "              }];");
19189 
19190   verifyFormat("f(^{\n"
19191                "  @autoreleasepool {\n"
19192                "    if (a) {\n"
19193                "      g();\n"
19194                "    }\n"
19195                "  }\n"
19196                "});");
19197   verifyFormat("Block b = ^int *(A *a, B *b) {}");
19198   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
19199                "};");
19200 
19201   FormatStyle FourIndent = getLLVMStyle();
19202   FourIndent.ObjCBlockIndentWidth = 4;
19203   verifyFormat("[operation setCompletionBlock:^{\n"
19204                "    [self onOperationDone];\n"
19205                "}];",
19206                FourIndent);
19207 }
19208 
19209 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
19210   FormatStyle ZeroColumn = getLLVMStyle();
19211   ZeroColumn.ColumnLimit = 0;
19212 
19213   verifyFormat("[[SessionService sharedService] "
19214                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
19215                "  if (window) {\n"
19216                "    [self windowDidLoad:window];\n"
19217                "  } else {\n"
19218                "    [self errorLoadingWindow];\n"
19219                "  }\n"
19220                "}];",
19221                ZeroColumn);
19222   EXPECT_EQ("[[SessionService sharedService]\n"
19223             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
19224             "      if (window) {\n"
19225             "        [self windowDidLoad:window];\n"
19226             "      } else {\n"
19227             "        [self errorLoadingWindow];\n"
19228             "      }\n"
19229             "    }];",
19230             format("[[SessionService sharedService]\n"
19231                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
19232                    "                if (window) {\n"
19233                    "    [self windowDidLoad:window];\n"
19234                    "  } else {\n"
19235                    "    [self errorLoadingWindow];\n"
19236                    "  }\n"
19237                    "}];",
19238                    ZeroColumn));
19239   verifyFormat("[myObject doSomethingWith:arg1\n"
19240                "    firstBlock:^(Foo *a) {\n"
19241                "      // ...\n"
19242                "      int i;\n"
19243                "    }\n"
19244                "    secondBlock:^(Bar *b) {\n"
19245                "      // ...\n"
19246                "      int i;\n"
19247                "    }\n"
19248                "    thirdBlock:^Foo(Bar *b) {\n"
19249                "      // ...\n"
19250                "      int i;\n"
19251                "    }];",
19252                ZeroColumn);
19253   verifyFormat("f(^{\n"
19254                "  @autoreleasepool {\n"
19255                "    if (a) {\n"
19256                "      g();\n"
19257                "    }\n"
19258                "  }\n"
19259                "});",
19260                ZeroColumn);
19261   verifyFormat("void (^largeBlock)(void) = ^{\n"
19262                "  // ...\n"
19263                "};",
19264                ZeroColumn);
19265 
19266   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
19267   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
19268             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
19269   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
19270   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
19271             "  int i;\n"
19272             "};",
19273             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
19274 }
19275 
19276 TEST_F(FormatTest, SupportsCRLF) {
19277   EXPECT_EQ("int a;\r\n"
19278             "int b;\r\n"
19279             "int c;\r\n",
19280             format("int a;\r\n"
19281                    "  int b;\r\n"
19282                    "    int c;\r\n",
19283                    getLLVMStyle()));
19284   EXPECT_EQ("int a;\r\n"
19285             "int b;\r\n"
19286             "int c;\r\n",
19287             format("int a;\r\n"
19288                    "  int b;\n"
19289                    "    int c;\r\n",
19290                    getLLVMStyle()));
19291   EXPECT_EQ("int a;\n"
19292             "int b;\n"
19293             "int c;\n",
19294             format("int a;\r\n"
19295                    "  int b;\n"
19296                    "    int c;\n",
19297                    getLLVMStyle()));
19298   EXPECT_EQ("\"aaaaaaa \"\r\n"
19299             "\"bbbbbbb\";\r\n",
19300             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
19301   EXPECT_EQ("#define A \\\r\n"
19302             "  b;      \\\r\n"
19303             "  c;      \\\r\n"
19304             "  d;\r\n",
19305             format("#define A \\\r\n"
19306                    "  b; \\\r\n"
19307                    "  c; d; \r\n",
19308                    getGoogleStyle()));
19309 
19310   EXPECT_EQ("/*\r\n"
19311             "multi line block comments\r\n"
19312             "should not introduce\r\n"
19313             "an extra carriage return\r\n"
19314             "*/\r\n",
19315             format("/*\r\n"
19316                    "multi line block comments\r\n"
19317                    "should not introduce\r\n"
19318                    "an extra carriage return\r\n"
19319                    "*/\r\n"));
19320   EXPECT_EQ("/*\r\n"
19321             "\r\n"
19322             "*/",
19323             format("/*\r\n"
19324                    "    \r\r\r\n"
19325                    "*/"));
19326 
19327   FormatStyle style = getLLVMStyle();
19328 
19329   style.DeriveLineEnding = true;
19330   style.UseCRLF = false;
19331   EXPECT_EQ("union FooBarBazQux {\n"
19332             "  int foo;\n"
19333             "  int bar;\n"
19334             "  int baz;\n"
19335             "};",
19336             format("union FooBarBazQux {\r\n"
19337                    "  int foo;\n"
19338                    "  int bar;\r\n"
19339                    "  int baz;\n"
19340                    "};",
19341                    style));
19342   style.UseCRLF = true;
19343   EXPECT_EQ("union FooBarBazQux {\r\n"
19344             "  int foo;\r\n"
19345             "  int bar;\r\n"
19346             "  int baz;\r\n"
19347             "};",
19348             format("union FooBarBazQux {\r\n"
19349                    "  int foo;\n"
19350                    "  int bar;\r\n"
19351                    "  int baz;\n"
19352                    "};",
19353                    style));
19354 
19355   style.DeriveLineEnding = false;
19356   style.UseCRLF = false;
19357   EXPECT_EQ("union FooBarBazQux {\n"
19358             "  int foo;\n"
19359             "  int bar;\n"
19360             "  int baz;\n"
19361             "  int qux;\n"
19362             "};",
19363             format("union FooBarBazQux {\r\n"
19364                    "  int foo;\n"
19365                    "  int bar;\r\n"
19366                    "  int baz;\n"
19367                    "  int qux;\r\n"
19368                    "};",
19369                    style));
19370   style.UseCRLF = true;
19371   EXPECT_EQ("union FooBarBazQux {\r\n"
19372             "  int foo;\r\n"
19373             "  int bar;\r\n"
19374             "  int baz;\r\n"
19375             "  int qux;\r\n"
19376             "};",
19377             format("union FooBarBazQux {\r\n"
19378                    "  int foo;\n"
19379                    "  int bar;\r\n"
19380                    "  int baz;\n"
19381                    "  int qux;\n"
19382                    "};",
19383                    style));
19384 
19385   style.DeriveLineEnding = true;
19386   style.UseCRLF = false;
19387   EXPECT_EQ("union FooBarBazQux {\r\n"
19388             "  int foo;\r\n"
19389             "  int bar;\r\n"
19390             "  int baz;\r\n"
19391             "  int qux;\r\n"
19392             "};",
19393             format("union FooBarBazQux {\r\n"
19394                    "  int foo;\n"
19395                    "  int bar;\r\n"
19396                    "  int baz;\n"
19397                    "  int qux;\r\n"
19398                    "};",
19399                    style));
19400   style.UseCRLF = true;
19401   EXPECT_EQ("union FooBarBazQux {\n"
19402             "  int foo;\n"
19403             "  int bar;\n"
19404             "  int baz;\n"
19405             "  int qux;\n"
19406             "};",
19407             format("union FooBarBazQux {\r\n"
19408                    "  int foo;\n"
19409                    "  int bar;\r\n"
19410                    "  int baz;\n"
19411                    "  int qux;\n"
19412                    "};",
19413                    style));
19414 }
19415 
19416 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
19417   verifyFormat("MY_CLASS(C) {\n"
19418                "  int i;\n"
19419                "  int j;\n"
19420                "};");
19421 }
19422 
19423 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
19424   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
19425   TwoIndent.ContinuationIndentWidth = 2;
19426 
19427   EXPECT_EQ("int i =\n"
19428             "  longFunction(\n"
19429             "    arg);",
19430             format("int i = longFunction(arg);", TwoIndent));
19431 
19432   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
19433   SixIndent.ContinuationIndentWidth = 6;
19434 
19435   EXPECT_EQ("int i =\n"
19436             "      longFunction(\n"
19437             "            arg);",
19438             format("int i = longFunction(arg);", SixIndent));
19439 }
19440 
19441 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
19442   FormatStyle Style = getLLVMStyle();
19443   verifyFormat("int Foo::getter(\n"
19444                "    //\n"
19445                ") const {\n"
19446                "  return foo;\n"
19447                "}",
19448                Style);
19449   verifyFormat("void Foo::setter(\n"
19450                "    //\n"
19451                ") {\n"
19452                "  foo = 1;\n"
19453                "}",
19454                Style);
19455 }
19456 
19457 TEST_F(FormatTest, SpacesInAngles) {
19458   FormatStyle Spaces = getLLVMStyle();
19459   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
19460 
19461   verifyFormat("vector< ::std::string > x1;", Spaces);
19462   verifyFormat("Foo< int, Bar > x2;", Spaces);
19463   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
19464 
19465   verifyFormat("static_cast< int >(arg);", Spaces);
19466   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
19467   verifyFormat("f< int, float >();", Spaces);
19468   verifyFormat("template <> g() {}", Spaces);
19469   verifyFormat("template < std::vector< int > > f() {}", Spaces);
19470   verifyFormat("std::function< void(int, int) > fct;", Spaces);
19471   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
19472                Spaces);
19473 
19474   Spaces.Standard = FormatStyle::LS_Cpp03;
19475   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
19476   verifyFormat("A< A< int > >();", Spaces);
19477 
19478   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
19479   verifyFormat("A<A<int> >();", Spaces);
19480 
19481   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
19482   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
19483                Spaces);
19484   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
19485                Spaces);
19486 
19487   verifyFormat("A<A<int> >();", Spaces);
19488   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
19489   verifyFormat("A< A< int > >();", Spaces);
19490 
19491   Spaces.Standard = FormatStyle::LS_Cpp11;
19492   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
19493   verifyFormat("A< A< int > >();", Spaces);
19494 
19495   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
19496   verifyFormat("vector<::std::string> x4;", Spaces);
19497   verifyFormat("vector<int> x5;", Spaces);
19498   verifyFormat("Foo<int, Bar> x6;", Spaces);
19499   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
19500 
19501   verifyFormat("A<A<int>>();", Spaces);
19502 
19503   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
19504   verifyFormat("vector<::std::string> x4;", Spaces);
19505   verifyFormat("vector< ::std::string > x4;", Spaces);
19506   verifyFormat("vector<int> x5;", Spaces);
19507   verifyFormat("vector< int > x5;", Spaces);
19508   verifyFormat("Foo<int, Bar> x6;", Spaces);
19509   verifyFormat("Foo< int, Bar > x6;", Spaces);
19510   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
19511   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
19512 
19513   verifyFormat("A<A<int>>();", Spaces);
19514   verifyFormat("A< A< int > >();", Spaces);
19515   verifyFormat("A<A<int > >();", Spaces);
19516   verifyFormat("A< A< int>>();", Spaces);
19517 }
19518 
19519 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
19520   FormatStyle Style = getLLVMStyle();
19521   Style.SpaceAfterTemplateKeyword = false;
19522   verifyFormat("template<int> void foo();", Style);
19523 }
19524 
19525 TEST_F(FormatTest, TripleAngleBrackets) {
19526   verifyFormat("f<<<1, 1>>>();");
19527   verifyFormat("f<<<1, 1, 1, s>>>();");
19528   verifyFormat("f<<<a, b, c, d>>>();");
19529   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
19530   verifyFormat("f<param><<<1, 1>>>();");
19531   verifyFormat("f<1><<<1, 1>>>();");
19532   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
19533   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19534                "aaaaaaaaaaa<<<\n    1, 1>>>();");
19535   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
19536                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
19537 }
19538 
19539 TEST_F(FormatTest, MergeLessLessAtEnd) {
19540   verifyFormat("<<");
19541   EXPECT_EQ("< < <", format("\\\n<<<"));
19542   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19543                "aaallvm::outs() <<");
19544   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19545                "aaaallvm::outs()\n    <<");
19546 }
19547 
19548 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
19549   std::string code = "#if A\n"
19550                      "#if B\n"
19551                      "a.\n"
19552                      "#endif\n"
19553                      "    a = 1;\n"
19554                      "#else\n"
19555                      "#endif\n"
19556                      "#if C\n"
19557                      "#else\n"
19558                      "#endif\n";
19559   EXPECT_EQ(code, format(code));
19560 }
19561 
19562 TEST_F(FormatTest, HandleConflictMarkers) {
19563   // Git/SVN conflict markers.
19564   EXPECT_EQ("int a;\n"
19565             "void f() {\n"
19566             "  callme(some(parameter1,\n"
19567             "<<<<<<< text by the vcs\n"
19568             "              parameter2),\n"
19569             "||||||| text by the vcs\n"
19570             "              parameter2),\n"
19571             "         parameter3,\n"
19572             "======= text by the vcs\n"
19573             "              parameter2, parameter3),\n"
19574             ">>>>>>> text by the vcs\n"
19575             "         otherparameter);\n",
19576             format("int a;\n"
19577                    "void f() {\n"
19578                    "  callme(some(parameter1,\n"
19579                    "<<<<<<< text by the vcs\n"
19580                    "  parameter2),\n"
19581                    "||||||| text by the vcs\n"
19582                    "  parameter2),\n"
19583                    "  parameter3,\n"
19584                    "======= text by the vcs\n"
19585                    "  parameter2,\n"
19586                    "  parameter3),\n"
19587                    ">>>>>>> text by the vcs\n"
19588                    "  otherparameter);\n"));
19589 
19590   // Perforce markers.
19591   EXPECT_EQ("void f() {\n"
19592             "  function(\n"
19593             ">>>> text by the vcs\n"
19594             "      parameter,\n"
19595             "==== text by the vcs\n"
19596             "      parameter,\n"
19597             "==== text by the vcs\n"
19598             "      parameter,\n"
19599             "<<<< text by the vcs\n"
19600             "      parameter);\n",
19601             format("void f() {\n"
19602                    "  function(\n"
19603                    ">>>> text by the vcs\n"
19604                    "  parameter,\n"
19605                    "==== text by the vcs\n"
19606                    "  parameter,\n"
19607                    "==== text by the vcs\n"
19608                    "  parameter,\n"
19609                    "<<<< text by the vcs\n"
19610                    "  parameter);\n"));
19611 
19612   EXPECT_EQ("<<<<<<<\n"
19613             "|||||||\n"
19614             "=======\n"
19615             ">>>>>>>",
19616             format("<<<<<<<\n"
19617                    "|||||||\n"
19618                    "=======\n"
19619                    ">>>>>>>"));
19620 
19621   EXPECT_EQ("<<<<<<<\n"
19622             "|||||||\n"
19623             "int i;\n"
19624             "=======\n"
19625             ">>>>>>>",
19626             format("<<<<<<<\n"
19627                    "|||||||\n"
19628                    "int i;\n"
19629                    "=======\n"
19630                    ">>>>>>>"));
19631 
19632   // FIXME: Handle parsing of macros around conflict markers correctly:
19633   EXPECT_EQ("#define Macro \\\n"
19634             "<<<<<<<\n"
19635             "Something \\\n"
19636             "|||||||\n"
19637             "Else \\\n"
19638             "=======\n"
19639             "Other \\\n"
19640             ">>>>>>>\n"
19641             "    End int i;\n",
19642             format("#define Macro \\\n"
19643                    "<<<<<<<\n"
19644                    "  Something \\\n"
19645                    "|||||||\n"
19646                    "  Else \\\n"
19647                    "=======\n"
19648                    "  Other \\\n"
19649                    ">>>>>>>\n"
19650                    "  End\n"
19651                    "int i;\n"));
19652 }
19653 
19654 TEST_F(FormatTest, DisableRegions) {
19655   EXPECT_EQ("int i;\n"
19656             "// clang-format off\n"
19657             "  int j;\n"
19658             "// clang-format on\n"
19659             "int k;",
19660             format(" int  i;\n"
19661                    "   // clang-format off\n"
19662                    "  int j;\n"
19663                    " // clang-format on\n"
19664                    "   int   k;"));
19665   EXPECT_EQ("int i;\n"
19666             "/* clang-format off */\n"
19667             "  int j;\n"
19668             "/* clang-format on */\n"
19669             "int k;",
19670             format(" int  i;\n"
19671                    "   /* clang-format off */\n"
19672                    "  int j;\n"
19673                    " /* clang-format on */\n"
19674                    "   int   k;"));
19675 
19676   // Don't reflow comments within disabled regions.
19677   EXPECT_EQ("// clang-format off\n"
19678             "// long long long long long long line\n"
19679             "/* clang-format on */\n"
19680             "/* long long long\n"
19681             " * long long long\n"
19682             " * line */\n"
19683             "int i;\n"
19684             "/* clang-format off */\n"
19685             "/* long long long long long long line */\n",
19686             format("// clang-format off\n"
19687                    "// long long long long long long line\n"
19688                    "/* clang-format on */\n"
19689                    "/* long long long long long long line */\n"
19690                    "int i;\n"
19691                    "/* clang-format off */\n"
19692                    "/* long long long long long long line */\n",
19693                    getLLVMStyleWithColumns(20)));
19694 }
19695 
19696 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
19697   format("? ) =");
19698   verifyNoCrash("#define a\\\n /**/}");
19699 }
19700 
19701 TEST_F(FormatTest, FormatsTableGenCode) {
19702   FormatStyle Style = getLLVMStyle();
19703   Style.Language = FormatStyle::LK_TableGen;
19704   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
19705 }
19706 
19707 TEST_F(FormatTest, ArrayOfTemplates) {
19708   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
19709             format("auto a = new unique_ptr<int > [ 10];"));
19710 
19711   FormatStyle Spaces = getLLVMStyle();
19712   Spaces.SpacesInSquareBrackets = true;
19713   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
19714             format("auto a = new unique_ptr<int > [10];", Spaces));
19715 }
19716 
19717 TEST_F(FormatTest, ArrayAsTemplateType) {
19718   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
19719             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
19720 
19721   FormatStyle Spaces = getLLVMStyle();
19722   Spaces.SpacesInSquareBrackets = true;
19723   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
19724             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
19725 }
19726 
19727 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
19728 
19729 TEST(FormatStyle, GetStyleWithEmptyFileName) {
19730   llvm::vfs::InMemoryFileSystem FS;
19731   auto Style1 = getStyle("file", "", "Google", "", &FS);
19732   ASSERT_TRUE((bool)Style1);
19733   ASSERT_EQ(*Style1, getGoogleStyle());
19734 }
19735 
19736 TEST(FormatStyle, GetStyleOfFile) {
19737   llvm::vfs::InMemoryFileSystem FS;
19738   // Test 1: format file in the same directory.
19739   ASSERT_TRUE(
19740       FS.addFile("/a/.clang-format", 0,
19741                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
19742   ASSERT_TRUE(
19743       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
19744   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
19745   ASSERT_TRUE((bool)Style1);
19746   ASSERT_EQ(*Style1, getLLVMStyle());
19747 
19748   // Test 2.1: fallback to default.
19749   ASSERT_TRUE(
19750       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
19751   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
19752   ASSERT_TRUE((bool)Style2);
19753   ASSERT_EQ(*Style2, getMozillaStyle());
19754 
19755   // Test 2.2: no format on 'none' fallback style.
19756   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
19757   ASSERT_TRUE((bool)Style2);
19758   ASSERT_EQ(*Style2, getNoStyle());
19759 
19760   // Test 2.3: format if config is found with no based style while fallback is
19761   // 'none'.
19762   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
19763                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
19764   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
19765   ASSERT_TRUE((bool)Style2);
19766   ASSERT_EQ(*Style2, getLLVMStyle());
19767 
19768   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
19769   Style2 = getStyle("{}", "a.h", "none", "", &FS);
19770   ASSERT_TRUE((bool)Style2);
19771   ASSERT_EQ(*Style2, getLLVMStyle());
19772 
19773   // Test 3: format file in parent directory.
19774   ASSERT_TRUE(
19775       FS.addFile("/c/.clang-format", 0,
19776                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
19777   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
19778                          llvm::MemoryBuffer::getMemBuffer("int i;")));
19779   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
19780   ASSERT_TRUE((bool)Style3);
19781   ASSERT_EQ(*Style3, getGoogleStyle());
19782 
19783   // Test 4: error on invalid fallback style
19784   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
19785   ASSERT_FALSE((bool)Style4);
19786   llvm::consumeError(Style4.takeError());
19787 
19788   // Test 5: error on invalid yaml on command line
19789   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
19790   ASSERT_FALSE((bool)Style5);
19791   llvm::consumeError(Style5.takeError());
19792 
19793   // Test 6: error on invalid style
19794   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
19795   ASSERT_FALSE((bool)Style6);
19796   llvm::consumeError(Style6.takeError());
19797 
19798   // Test 7: found config file, error on parsing it
19799   ASSERT_TRUE(
19800       FS.addFile("/d/.clang-format", 0,
19801                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
19802                                                   "InvalidKey: InvalidValue")));
19803   ASSERT_TRUE(
19804       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
19805   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
19806   ASSERT_FALSE((bool)Style7a);
19807   llvm::consumeError(Style7a.takeError());
19808 
19809   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
19810   ASSERT_TRUE((bool)Style7b);
19811 
19812   // Test 8: inferred per-language defaults apply.
19813   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
19814   ASSERT_TRUE((bool)StyleTd);
19815   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
19816 
19817   // Test 9.1: overwriting a file style, when parent no file exists with no
19818   // fallback style
19819   ASSERT_TRUE(FS.addFile(
19820       "/e/sub/.clang-format", 0,
19821       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
19822                                        "ColumnLimit: 20")));
19823   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
19824                          llvm::MemoryBuffer::getMemBuffer("int i;")));
19825   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
19826   ASSERT_TRUE(static_cast<bool>(Style9));
19827   ASSERT_EQ(*Style9, [] {
19828     auto Style = getNoStyle();
19829     Style.ColumnLimit = 20;
19830     return Style;
19831   }());
19832 
19833   // Test 9.2: with LLVM fallback style
19834   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
19835   ASSERT_TRUE(static_cast<bool>(Style9));
19836   ASSERT_EQ(*Style9, [] {
19837     auto Style = getLLVMStyle();
19838     Style.ColumnLimit = 20;
19839     return Style;
19840   }());
19841 
19842   // Test 9.3: with a parent file
19843   ASSERT_TRUE(
19844       FS.addFile("/e/.clang-format", 0,
19845                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
19846                                                   "UseTab: Always")));
19847   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
19848   ASSERT_TRUE(static_cast<bool>(Style9));
19849   ASSERT_EQ(*Style9, [] {
19850     auto Style = getGoogleStyle();
19851     Style.ColumnLimit = 20;
19852     Style.UseTab = FormatStyle::UT_Always;
19853     return Style;
19854   }());
19855 
19856   // Test 9.4: propagate more than one level
19857   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
19858                          llvm::MemoryBuffer::getMemBuffer("int i;")));
19859   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
19860                          llvm::MemoryBuffer::getMemBuffer(
19861                              "BasedOnStyle: InheritParentConfig\n"
19862                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
19863   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
19864 
19865   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
19866     auto Style = getGoogleStyle();
19867     Style.ColumnLimit = 20;
19868     Style.UseTab = FormatStyle::UT_Always;
19869     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
19870     return Style;
19871   }();
19872 
19873   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
19874   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
19875   ASSERT_TRUE(static_cast<bool>(Style9));
19876   ASSERT_EQ(*Style9, SubSubStyle);
19877 
19878   // Test 9.5: use InheritParentConfig as style name
19879   Style9 =
19880       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
19881   ASSERT_TRUE(static_cast<bool>(Style9));
19882   ASSERT_EQ(*Style9, SubSubStyle);
19883 
19884   // Test 9.6: use command line style with inheritance
19885   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
19886                     "none", "", &FS);
19887   ASSERT_TRUE(static_cast<bool>(Style9));
19888   ASSERT_EQ(*Style9, SubSubStyle);
19889 
19890   // Test 9.7: use command line style with inheritance and own config
19891   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
19892                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
19893                     "/e/sub/code.cpp", "none", "", &FS);
19894   ASSERT_TRUE(static_cast<bool>(Style9));
19895   ASSERT_EQ(*Style9, SubSubStyle);
19896 
19897   // Test 9.8: use inheritance from a file without BasedOnStyle
19898   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
19899                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
19900   ASSERT_TRUE(
19901       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
19902                  llvm::MemoryBuffer::getMemBuffer(
19903                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
19904   // Make sure we do not use the fallback style
19905   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
19906   ASSERT_TRUE(static_cast<bool>(Style9));
19907   ASSERT_EQ(*Style9, [] {
19908     auto Style = getLLVMStyle();
19909     Style.ColumnLimit = 123;
19910     return Style;
19911   }());
19912 
19913   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
19914   ASSERT_TRUE(static_cast<bool>(Style9));
19915   ASSERT_EQ(*Style9, [] {
19916     auto Style = getLLVMStyle();
19917     Style.ColumnLimit = 123;
19918     Style.IndentWidth = 7;
19919     return Style;
19920   }());
19921 }
19922 
19923 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
19924   // Column limit is 20.
19925   std::string Code = "Type *a =\n"
19926                      "    new Type();\n"
19927                      "g(iiiii, 0, jjjjj,\n"
19928                      "  0, kkkkk, 0, mm);\n"
19929                      "int  bad     = format   ;";
19930   std::string Expected = "auto a = new Type();\n"
19931                          "g(iiiii, nullptr,\n"
19932                          "  jjjjj, nullptr,\n"
19933                          "  kkkkk, nullptr,\n"
19934                          "  mm);\n"
19935                          "int  bad     = format   ;";
19936   FileID ID = Context.createInMemoryFile("format.cpp", Code);
19937   tooling::Replacements Replaces = toReplacements(
19938       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
19939                             "auto "),
19940        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
19941                             "nullptr"),
19942        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
19943                             "nullptr"),
19944        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
19945                             "nullptr")});
19946 
19947   format::FormatStyle Style = format::getLLVMStyle();
19948   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
19949   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
19950   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
19951       << llvm::toString(FormattedReplaces.takeError()) << "\n";
19952   auto Result = applyAllReplacements(Code, *FormattedReplaces);
19953   EXPECT_TRUE(static_cast<bool>(Result));
19954   EXPECT_EQ(Expected, *Result);
19955 }
19956 
19957 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
19958   std::string Code = "#include \"a.h\"\n"
19959                      "#include \"c.h\"\n"
19960                      "\n"
19961                      "int main() {\n"
19962                      "  return 0;\n"
19963                      "}";
19964   std::string Expected = "#include \"a.h\"\n"
19965                          "#include \"b.h\"\n"
19966                          "#include \"c.h\"\n"
19967                          "\n"
19968                          "int main() {\n"
19969                          "  return 0;\n"
19970                          "}";
19971   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
19972   tooling::Replacements Replaces = toReplacements(
19973       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
19974                             "#include \"b.h\"\n")});
19975 
19976   format::FormatStyle Style = format::getLLVMStyle();
19977   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
19978   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
19979   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
19980       << llvm::toString(FormattedReplaces.takeError()) << "\n";
19981   auto Result = applyAllReplacements(Code, *FormattedReplaces);
19982   EXPECT_TRUE(static_cast<bool>(Result));
19983   EXPECT_EQ(Expected, *Result);
19984 }
19985 
19986 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
19987   EXPECT_EQ("using std::cin;\n"
19988             "using std::cout;",
19989             format("using std::cout;\n"
19990                    "using std::cin;",
19991                    getGoogleStyle()));
19992 }
19993 
19994 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
19995   format::FormatStyle Style = format::getLLVMStyle();
19996   Style.Standard = FormatStyle::LS_Cpp03;
19997   // cpp03 recognize this string as identifier u8 and literal character 'a'
19998   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
19999 }
20000 
20001 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
20002   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
20003   // all modes, including C++11, C++14 and C++17
20004   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
20005 }
20006 
20007 TEST_F(FormatTest, DoNotFormatLikelyXml) {
20008   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
20009   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
20010 }
20011 
20012 TEST_F(FormatTest, StructuredBindings) {
20013   // Structured bindings is a C++17 feature.
20014   // all modes, including C++11, C++14 and C++17
20015   verifyFormat("auto [a, b] = f();");
20016   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
20017   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
20018   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
20019   EXPECT_EQ("auto const volatile [a, b] = f();",
20020             format("auto  const   volatile[a, b] = f();"));
20021   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
20022   EXPECT_EQ("auto &[a, b, c] = f();",
20023             format("auto   &[  a  ,  b,c   ] = f();"));
20024   EXPECT_EQ("auto &&[a, b, c] = f();",
20025             format("auto   &&[  a  ,  b,c   ] = f();"));
20026   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
20027   EXPECT_EQ("auto const volatile &&[a, b] = f();",
20028             format("auto  const  volatile  &&[a, b] = f();"));
20029   EXPECT_EQ("auto const &&[a, b] = f();",
20030             format("auto  const   &&  [a, b] = f();"));
20031   EXPECT_EQ("const auto &[a, b] = f();",
20032             format("const  auto  &  [a, b] = f();"));
20033   EXPECT_EQ("const auto volatile &&[a, b] = f();",
20034             format("const  auto   volatile  &&[a, b] = f();"));
20035   EXPECT_EQ("volatile const auto &&[a, b] = f();",
20036             format("volatile  const  auto   &&[a, b] = f();"));
20037   EXPECT_EQ("const auto &&[a, b] = f();",
20038             format("const  auto  &&  [a, b] = f();"));
20039 
20040   // Make sure we don't mistake structured bindings for lambdas.
20041   FormatStyle PointerMiddle = getLLVMStyle();
20042   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
20043   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
20044   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
20045   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
20046   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
20047   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
20048   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
20049   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
20050   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
20051   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
20052   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
20053   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
20054   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
20055 
20056   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
20057             format("for (const auto   &&   [a, b] : some_range) {\n}"));
20058   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
20059             format("for (const auto   &   [a, b] : some_range) {\n}"));
20060   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
20061             format("for (const auto[a, b] : some_range) {\n}"));
20062   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
20063   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
20064   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
20065   EXPECT_EQ("auto const &[x, y](expr);",
20066             format("auto  const  &  [x,y]  (expr);"));
20067   EXPECT_EQ("auto const &&[x, y](expr);",
20068             format("auto  const  &&  [x,y]  (expr);"));
20069   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
20070   EXPECT_EQ("auto const &[x, y]{expr};",
20071             format("auto  const  &  [x,y]  {expr};"));
20072   EXPECT_EQ("auto const &&[x, y]{expr};",
20073             format("auto  const  &&  [x,y]  {expr};"));
20074 
20075   format::FormatStyle Spaces = format::getLLVMStyle();
20076   Spaces.SpacesInSquareBrackets = true;
20077   verifyFormat("auto [ a, b ] = f();", Spaces);
20078   verifyFormat("auto &&[ a, b ] = f();", Spaces);
20079   verifyFormat("auto &[ a, b ] = f();", Spaces);
20080   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
20081   verifyFormat("auto const &[ a, b ] = f();", Spaces);
20082 }
20083 
20084 TEST_F(FormatTest, FileAndCode) {
20085   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
20086   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
20087   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
20088   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
20089   EXPECT_EQ(FormatStyle::LK_ObjC,
20090             guessLanguage("foo.h", "@interface Foo\n@end\n"));
20091   EXPECT_EQ(
20092       FormatStyle::LK_ObjC,
20093       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
20094   EXPECT_EQ(FormatStyle::LK_ObjC,
20095             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
20096   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
20097   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
20098   EXPECT_EQ(FormatStyle::LK_ObjC,
20099             guessLanguage("foo", "@interface Foo\n@end\n"));
20100   EXPECT_EQ(FormatStyle::LK_ObjC,
20101             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
20102   EXPECT_EQ(
20103       FormatStyle::LK_ObjC,
20104       guessLanguage("foo.h",
20105                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
20106   EXPECT_EQ(
20107       FormatStyle::LK_Cpp,
20108       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
20109 }
20110 
20111 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
20112   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
20113   EXPECT_EQ(FormatStyle::LK_ObjC,
20114             guessLanguage("foo.h", "array[[calculator getIndex]];"));
20115   EXPECT_EQ(FormatStyle::LK_Cpp,
20116             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
20117   EXPECT_EQ(
20118       FormatStyle::LK_Cpp,
20119       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
20120   EXPECT_EQ(FormatStyle::LK_ObjC,
20121             guessLanguage("foo.h", "[[noreturn foo] bar];"));
20122   EXPECT_EQ(FormatStyle::LK_Cpp,
20123             guessLanguage("foo.h", "[[clang::fallthrough]];"));
20124   EXPECT_EQ(FormatStyle::LK_ObjC,
20125             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
20126   EXPECT_EQ(FormatStyle::LK_Cpp,
20127             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
20128   EXPECT_EQ(FormatStyle::LK_Cpp,
20129             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
20130   EXPECT_EQ(FormatStyle::LK_ObjC,
20131             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
20132   EXPECT_EQ(FormatStyle::LK_Cpp,
20133             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
20134   EXPECT_EQ(
20135       FormatStyle::LK_Cpp,
20136       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
20137   EXPECT_EQ(
20138       FormatStyle::LK_Cpp,
20139       guessLanguage("foo.h",
20140                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
20141   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
20142 }
20143 
20144 TEST_F(FormatTest, GuessLanguageWithCaret) {
20145   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
20146   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
20147   EXPECT_EQ(FormatStyle::LK_ObjC,
20148             guessLanguage("foo.h", "int(^)(char, float);"));
20149   EXPECT_EQ(FormatStyle::LK_ObjC,
20150             guessLanguage("foo.h", "int(^foo)(char, float);"));
20151   EXPECT_EQ(FormatStyle::LK_ObjC,
20152             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
20153   EXPECT_EQ(FormatStyle::LK_ObjC,
20154             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
20155   EXPECT_EQ(
20156       FormatStyle::LK_ObjC,
20157       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
20158 }
20159 
20160 TEST_F(FormatTest, GuessLanguageWithPragmas) {
20161   EXPECT_EQ(FormatStyle::LK_Cpp,
20162             guessLanguage("foo.h", "__pragma(warning(disable:))"));
20163   EXPECT_EQ(FormatStyle::LK_Cpp,
20164             guessLanguage("foo.h", "#pragma(warning(disable:))"));
20165   EXPECT_EQ(FormatStyle::LK_Cpp,
20166             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
20167 }
20168 
20169 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
20170   // ASM symbolic names are identifiers that must be surrounded by [] without
20171   // space in between:
20172   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
20173 
20174   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
20175   verifyFormat(R"(//
20176 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
20177 )");
20178 
20179   // A list of several ASM symbolic names.
20180   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
20181 
20182   // ASM symbolic names in inline ASM with inputs and outputs.
20183   verifyFormat(R"(//
20184 asm("cmoveq %1, %2, %[result]"
20185     : [result] "=r"(result)
20186     : "r"(test), "r"(new), "[result]"(old));
20187 )");
20188 
20189   // ASM symbolic names in inline ASM with no outputs.
20190   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
20191 }
20192 
20193 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
20194   EXPECT_EQ(FormatStyle::LK_Cpp,
20195             guessLanguage("foo.h", "void f() {\n"
20196                                    "  asm (\"mov %[e], %[d]\"\n"
20197                                    "     : [d] \"=rm\" (d)\n"
20198                                    "       [e] \"rm\" (*e));\n"
20199                                    "}"));
20200   EXPECT_EQ(FormatStyle::LK_Cpp,
20201             guessLanguage("foo.h", "void f() {\n"
20202                                    "  _asm (\"mov %[e], %[d]\"\n"
20203                                    "     : [d] \"=rm\" (d)\n"
20204                                    "       [e] \"rm\" (*e));\n"
20205                                    "}"));
20206   EXPECT_EQ(FormatStyle::LK_Cpp,
20207             guessLanguage("foo.h", "void f() {\n"
20208                                    "  __asm (\"mov %[e], %[d]\"\n"
20209                                    "     : [d] \"=rm\" (d)\n"
20210                                    "       [e] \"rm\" (*e));\n"
20211                                    "}"));
20212   EXPECT_EQ(FormatStyle::LK_Cpp,
20213             guessLanguage("foo.h", "void f() {\n"
20214                                    "  __asm__ (\"mov %[e], %[d]\"\n"
20215                                    "     : [d] \"=rm\" (d)\n"
20216                                    "       [e] \"rm\" (*e));\n"
20217                                    "}"));
20218   EXPECT_EQ(FormatStyle::LK_Cpp,
20219             guessLanguage("foo.h", "void f() {\n"
20220                                    "  asm (\"mov %[e], %[d]\"\n"
20221                                    "     : [d] \"=rm\" (d),\n"
20222                                    "       [e] \"rm\" (*e));\n"
20223                                    "}"));
20224   EXPECT_EQ(FormatStyle::LK_Cpp,
20225             guessLanguage("foo.h", "void f() {\n"
20226                                    "  asm volatile (\"mov %[e], %[d]\"\n"
20227                                    "     : [d] \"=rm\" (d)\n"
20228                                    "       [e] \"rm\" (*e));\n"
20229                                    "}"));
20230 }
20231 
20232 TEST_F(FormatTest, GuessLanguageWithChildLines) {
20233   EXPECT_EQ(FormatStyle::LK_Cpp,
20234             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
20235   EXPECT_EQ(FormatStyle::LK_ObjC,
20236             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
20237   EXPECT_EQ(
20238       FormatStyle::LK_Cpp,
20239       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
20240   EXPECT_EQ(
20241       FormatStyle::LK_ObjC,
20242       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
20243 }
20244 
20245 TEST_F(FormatTest, TypenameMacros) {
20246   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
20247 
20248   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
20249   FormatStyle Google = getGoogleStyleWithColumns(0);
20250   Google.TypenameMacros = TypenameMacros;
20251   verifyFormat("struct foo {\n"
20252                "  int bar;\n"
20253                "  TAILQ_ENTRY(a) bleh;\n"
20254                "};",
20255                Google);
20256 
20257   FormatStyle Macros = getLLVMStyle();
20258   Macros.TypenameMacros = TypenameMacros;
20259 
20260   verifyFormat("STACK_OF(int) a;", Macros);
20261   verifyFormat("STACK_OF(int) *a;", Macros);
20262   verifyFormat("STACK_OF(int const *) *a;", Macros);
20263   verifyFormat("STACK_OF(int *const) *a;", Macros);
20264   verifyFormat("STACK_OF(int, string) a;", Macros);
20265   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
20266   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
20267   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
20268   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
20269   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
20270   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
20271 
20272   Macros.PointerAlignment = FormatStyle::PAS_Left;
20273   verifyFormat("STACK_OF(int)* a;", Macros);
20274   verifyFormat("STACK_OF(int*)* a;", Macros);
20275   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
20276   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
20277   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
20278 }
20279 
20280 TEST_F(FormatTest, AtomicQualifier) {
20281   // Check that we treate _Atomic as a type and not a function call
20282   FormatStyle Google = getGoogleStyleWithColumns(0);
20283   verifyFormat("struct foo {\n"
20284                "  int a1;\n"
20285                "  _Atomic(a) a2;\n"
20286                "  _Atomic(_Atomic(int) *const) a3;\n"
20287                "};",
20288                Google);
20289   verifyFormat("_Atomic(uint64_t) a;");
20290   verifyFormat("_Atomic(uint64_t) *a;");
20291   verifyFormat("_Atomic(uint64_t const *) *a;");
20292   verifyFormat("_Atomic(uint64_t *const) *a;");
20293   verifyFormat("_Atomic(const uint64_t *) *a;");
20294   verifyFormat("_Atomic(uint64_t) a;");
20295   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
20296   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
20297   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
20298   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
20299 
20300   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
20301   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
20302   FormatStyle Style = getLLVMStyle();
20303   Style.PointerAlignment = FormatStyle::PAS_Left;
20304   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
20305   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
20306   verifyFormat("_Atomic(int)* a;", Style);
20307   verifyFormat("_Atomic(int*)* a;", Style);
20308   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
20309 
20310   Style.SpacesInCStyleCastParentheses = true;
20311   Style.SpacesInParentheses = false;
20312   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
20313   Style.SpacesInCStyleCastParentheses = false;
20314   Style.SpacesInParentheses = true;
20315   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
20316   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
20317 }
20318 
20319 TEST_F(FormatTest, AmbersandInLamda) {
20320   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
20321   FormatStyle AlignStyle = getLLVMStyle();
20322   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
20323   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
20324   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
20325   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
20326 }
20327 
20328 TEST_F(FormatTest, SpacesInConditionalStatement) {
20329   FormatStyle Spaces = getLLVMStyle();
20330   Spaces.SpacesInConditionalStatement = true;
20331   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
20332   verifyFormat("if ( !a )\n  return;", Spaces);
20333   verifyFormat("if ( a )\n  return;", Spaces);
20334   verifyFormat("if constexpr ( a )\n  return;", Spaces);
20335   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
20336   verifyFormat("while ( a )\n  return;", Spaces);
20337   verifyFormat("while ( (a && b) )\n  return;", Spaces);
20338   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
20339   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
20340   // Check that space on the left of "::" is inserted as expected at beginning
20341   // of condition.
20342   verifyFormat("while ( ::func() )\n  return;", Spaces);
20343 }
20344 
20345 TEST_F(FormatTest, AlternativeOperators) {
20346   // Test case for ensuring alternate operators are not
20347   // combined with their right most neighbour.
20348   verifyFormat("int a and b;");
20349   verifyFormat("int a and_eq b;");
20350   verifyFormat("int a bitand b;");
20351   verifyFormat("int a bitor b;");
20352   verifyFormat("int a compl b;");
20353   verifyFormat("int a not b;");
20354   verifyFormat("int a not_eq b;");
20355   verifyFormat("int a or b;");
20356   verifyFormat("int a xor b;");
20357   verifyFormat("int a xor_eq b;");
20358   verifyFormat("return this not_eq bitand other;");
20359   verifyFormat("bool operator not_eq(const X bitand other)");
20360 
20361   verifyFormat("int a and 5;");
20362   verifyFormat("int a and_eq 5;");
20363   verifyFormat("int a bitand 5;");
20364   verifyFormat("int a bitor 5;");
20365   verifyFormat("int a compl 5;");
20366   verifyFormat("int a not 5;");
20367   verifyFormat("int a not_eq 5;");
20368   verifyFormat("int a or 5;");
20369   verifyFormat("int a xor 5;");
20370   verifyFormat("int a xor_eq 5;");
20371 
20372   verifyFormat("int a compl(5);");
20373   verifyFormat("int a not(5);");
20374 
20375   /* FIXME handle alternate tokens
20376    * https://en.cppreference.com/w/cpp/language/operator_alternative
20377   // alternative tokens
20378   verifyFormat("compl foo();");     //  ~foo();
20379   verifyFormat("foo() <%%>;");      // foo();
20380   verifyFormat("void foo() <%%>;"); // void foo(){}
20381   verifyFormat("int a <:1:>;");     // int a[1];[
20382   verifyFormat("%:define ABC abc"); // #define ABC abc
20383   verifyFormat("%:%:");             // ##
20384   */
20385 }
20386 
20387 TEST_F(FormatTest, STLWhileNotDefineChed) {
20388   verifyFormat("#if defined(while)\n"
20389                "#define while EMIT WARNING C4005\n"
20390                "#endif // while");
20391 }
20392 
20393 TEST_F(FormatTest, OperatorSpacing) {
20394   FormatStyle Style = getLLVMStyle();
20395   Style.PointerAlignment = FormatStyle::PAS_Right;
20396   verifyFormat("Foo::operator*();", Style);
20397   verifyFormat("Foo::operator void *();", Style);
20398   verifyFormat("Foo::operator void **();", Style);
20399   verifyFormat("Foo::operator void *&();", Style);
20400   verifyFormat("Foo::operator void *&&();", Style);
20401   verifyFormat("Foo::operator void const *();", Style);
20402   verifyFormat("Foo::operator void const **();", Style);
20403   verifyFormat("Foo::operator void const *&();", Style);
20404   verifyFormat("Foo::operator void const *&&();", Style);
20405   verifyFormat("Foo::operator()(void *);", Style);
20406   verifyFormat("Foo::operator*(void *);", Style);
20407   verifyFormat("Foo::operator*();", Style);
20408   verifyFormat("Foo::operator**();", Style);
20409   verifyFormat("Foo::operator&();", Style);
20410   verifyFormat("Foo::operator<int> *();", Style);
20411   verifyFormat("Foo::operator<Foo> *();", Style);
20412   verifyFormat("Foo::operator<int> **();", Style);
20413   verifyFormat("Foo::operator<Foo> **();", Style);
20414   verifyFormat("Foo::operator<int> &();", Style);
20415   verifyFormat("Foo::operator<Foo> &();", Style);
20416   verifyFormat("Foo::operator<int> &&();", Style);
20417   verifyFormat("Foo::operator<Foo> &&();", Style);
20418   verifyFormat("Foo::operator<int> *&();", Style);
20419   verifyFormat("Foo::operator<Foo> *&();", Style);
20420   verifyFormat("Foo::operator<int> *&&();", Style);
20421   verifyFormat("Foo::operator<Foo> *&&();", Style);
20422   verifyFormat("operator*(int (*)(), class Foo);", Style);
20423 
20424   verifyFormat("Foo::operator&();", Style);
20425   verifyFormat("Foo::operator void &();", Style);
20426   verifyFormat("Foo::operator void const &();", Style);
20427   verifyFormat("Foo::operator()(void &);", Style);
20428   verifyFormat("Foo::operator&(void &);", Style);
20429   verifyFormat("Foo::operator&();", Style);
20430   verifyFormat("operator&(int (&)(), class Foo);", Style);
20431 
20432   verifyFormat("Foo::operator&&();", Style);
20433   verifyFormat("Foo::operator**();", Style);
20434   verifyFormat("Foo::operator void &&();", Style);
20435   verifyFormat("Foo::operator void const &&();", Style);
20436   verifyFormat("Foo::operator()(void &&);", Style);
20437   verifyFormat("Foo::operator&&(void &&);", Style);
20438   verifyFormat("Foo::operator&&();", Style);
20439   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
20440   verifyFormat("operator const nsTArrayRight<E> &()", Style);
20441   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
20442                Style);
20443   verifyFormat("operator void **()", Style);
20444   verifyFormat("operator const FooRight<Object> &()", Style);
20445   verifyFormat("operator const FooRight<Object> *()", Style);
20446   verifyFormat("operator const FooRight<Object> **()", Style);
20447   verifyFormat("operator const FooRight<Object> *&()", Style);
20448   verifyFormat("operator const FooRight<Object> *&&()", Style);
20449 
20450   Style.PointerAlignment = FormatStyle::PAS_Left;
20451   verifyFormat("Foo::operator*();", Style);
20452   verifyFormat("Foo::operator**();", Style);
20453   verifyFormat("Foo::operator void*();", Style);
20454   verifyFormat("Foo::operator void**();", Style);
20455   verifyFormat("Foo::operator void*&();", Style);
20456   verifyFormat("Foo::operator void*&&();", Style);
20457   verifyFormat("Foo::operator void const*();", Style);
20458   verifyFormat("Foo::operator void const**();", Style);
20459   verifyFormat("Foo::operator void const*&();", Style);
20460   verifyFormat("Foo::operator void const*&&();", Style);
20461   verifyFormat("Foo::operator/*comment*/ void*();", Style);
20462   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
20463   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
20464   verifyFormat("Foo::operator()(void*);", Style);
20465   verifyFormat("Foo::operator*(void*);", Style);
20466   verifyFormat("Foo::operator*();", Style);
20467   verifyFormat("Foo::operator<int>*();", Style);
20468   verifyFormat("Foo::operator<Foo>*();", Style);
20469   verifyFormat("Foo::operator<int>**();", Style);
20470   verifyFormat("Foo::operator<Foo>**();", Style);
20471   verifyFormat("Foo::operator<Foo>*&();", Style);
20472   verifyFormat("Foo::operator<int>&();", Style);
20473   verifyFormat("Foo::operator<Foo>&();", Style);
20474   verifyFormat("Foo::operator<int>&&();", Style);
20475   verifyFormat("Foo::operator<Foo>&&();", Style);
20476   verifyFormat("Foo::operator<int>*&();", Style);
20477   verifyFormat("Foo::operator<Foo>*&();", Style);
20478   verifyFormat("operator*(int (*)(), class Foo);", Style);
20479 
20480   verifyFormat("Foo::operator&();", Style);
20481   verifyFormat("Foo::operator void&();", Style);
20482   verifyFormat("Foo::operator void const&();", Style);
20483   verifyFormat("Foo::operator/*comment*/ void&();", Style);
20484   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
20485   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
20486   verifyFormat("Foo::operator()(void&);", Style);
20487   verifyFormat("Foo::operator&(void&);", Style);
20488   verifyFormat("Foo::operator&();", Style);
20489   verifyFormat("operator&(int (&)(), class Foo);", Style);
20490 
20491   verifyFormat("Foo::operator&&();", Style);
20492   verifyFormat("Foo::operator void&&();", Style);
20493   verifyFormat("Foo::operator void const&&();", Style);
20494   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
20495   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
20496   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
20497   verifyFormat("Foo::operator()(void&&);", Style);
20498   verifyFormat("Foo::operator&&(void&&);", Style);
20499   verifyFormat("Foo::operator&&();", Style);
20500   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
20501   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
20502   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
20503                Style);
20504   verifyFormat("operator void**()", Style);
20505   verifyFormat("operator const FooLeft<Object>&()", Style);
20506   verifyFormat("operator const FooLeft<Object>*()", Style);
20507   verifyFormat("operator const FooLeft<Object>**()", Style);
20508   verifyFormat("operator const FooLeft<Object>*&()", Style);
20509   verifyFormat("operator const FooLeft<Object>*&&()", Style);
20510 
20511   // PR45107
20512   verifyFormat("operator Vector<String>&();", Style);
20513   verifyFormat("operator const Vector<String>&();", Style);
20514   verifyFormat("operator foo::Bar*();", Style);
20515   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
20516   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
20517                Style);
20518 
20519   Style.PointerAlignment = FormatStyle::PAS_Middle;
20520   verifyFormat("Foo::operator*();", Style);
20521   verifyFormat("Foo::operator void *();", Style);
20522   verifyFormat("Foo::operator()(void *);", Style);
20523   verifyFormat("Foo::operator*(void *);", Style);
20524   verifyFormat("Foo::operator*();", Style);
20525   verifyFormat("operator*(int (*)(), class Foo);", Style);
20526 
20527   verifyFormat("Foo::operator&();", Style);
20528   verifyFormat("Foo::operator void &();", Style);
20529   verifyFormat("Foo::operator void const &();", Style);
20530   verifyFormat("Foo::operator()(void &);", Style);
20531   verifyFormat("Foo::operator&(void &);", Style);
20532   verifyFormat("Foo::operator&();", Style);
20533   verifyFormat("operator&(int (&)(), class Foo);", Style);
20534 
20535   verifyFormat("Foo::operator&&();", Style);
20536   verifyFormat("Foo::operator void &&();", Style);
20537   verifyFormat("Foo::operator void const &&();", Style);
20538   verifyFormat("Foo::operator()(void &&);", Style);
20539   verifyFormat("Foo::operator&&(void &&);", Style);
20540   verifyFormat("Foo::operator&&();", Style);
20541   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
20542 }
20543 
20544 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
20545   FormatStyle Style = getLLVMStyle();
20546   // PR46157
20547   verifyFormat("foo(operator+, -42);", Style);
20548   verifyFormat("foo(operator++, -42);", Style);
20549   verifyFormat("foo(operator--, -42);", Style);
20550   verifyFormat("foo(-42, operator--);", Style);
20551   verifyFormat("foo(-42, operator, );", Style);
20552   verifyFormat("foo(operator, , -42);", Style);
20553 }
20554 
20555 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
20556   FormatStyle Style = getLLVMStyle();
20557   Style.WhitespaceSensitiveMacros.push_back("FOO");
20558 
20559   // Don't use the helpers here, since 'mess up' will change the whitespace
20560   // and these are all whitespace sensitive by definition
20561   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
20562             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
20563   EXPECT_EQ(
20564       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
20565       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
20566   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
20567             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
20568   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
20569             "       Still=Intentional);",
20570             format("FOO(String-ized&Messy+But,: :\n"
20571                    "       Still=Intentional);",
20572                    Style));
20573   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
20574   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
20575             "       Still=Intentional);",
20576             format("FOO(String-ized=&Messy+But,: :\n"
20577                    "       Still=Intentional);",
20578                    Style));
20579 
20580   Style.ColumnLimit = 21;
20581   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
20582             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
20583 }
20584 
20585 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
20586   // These tests are not in NamespaceFixer because that doesn't
20587   // test its interaction with line wrapping
20588   FormatStyle Style = getLLVMStyle();
20589   Style.ColumnLimit = 80;
20590   verifyFormat("namespace {\n"
20591                "int i;\n"
20592                "int j;\n"
20593                "} // namespace",
20594                Style);
20595 
20596   verifyFormat("namespace AAA {\n"
20597                "int i;\n"
20598                "int j;\n"
20599                "} // namespace AAA",
20600                Style);
20601 
20602   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
20603             "int i;\n"
20604             "int j;\n"
20605             "} // namespace Averyveryveryverylongnamespace",
20606             format("namespace Averyveryveryverylongnamespace {\n"
20607                    "int i;\n"
20608                    "int j;\n"
20609                    "}",
20610                    Style));
20611 
20612   EXPECT_EQ(
20613       "namespace "
20614       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
20615       "    went::mad::now {\n"
20616       "int i;\n"
20617       "int j;\n"
20618       "} // namespace\n"
20619       "  // "
20620       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
20621       "went::mad::now",
20622       format("namespace "
20623              "would::it::save::you::a::lot::of::time::if_::i::"
20624              "just::gave::up::and_::went::mad::now {\n"
20625              "int i;\n"
20626              "int j;\n"
20627              "}",
20628              Style));
20629 
20630   // This used to duplicate the comment again and again on subsequent runs
20631   EXPECT_EQ(
20632       "namespace "
20633       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
20634       "    went::mad::now {\n"
20635       "int i;\n"
20636       "int j;\n"
20637       "} // namespace\n"
20638       "  // "
20639       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
20640       "went::mad::now",
20641       format("namespace "
20642              "would::it::save::you::a::lot::of::time::if_::i::"
20643              "just::gave::up::and_::went::mad::now {\n"
20644              "int i;\n"
20645              "int j;\n"
20646              "} // namespace\n"
20647              "  // "
20648              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
20649              "and_::went::mad::now",
20650              Style));
20651 }
20652 
20653 TEST_F(FormatTest, LikelyUnlikely) {
20654   FormatStyle Style = getLLVMStyle();
20655 
20656   verifyFormat("if (argc > 5) [[unlikely]] {\n"
20657                "  return 29;\n"
20658                "}",
20659                Style);
20660 
20661   verifyFormat("if (argc > 5) [[likely]] {\n"
20662                "  return 29;\n"
20663                "}",
20664                Style);
20665 
20666   verifyFormat("if (argc > 5) [[unlikely]] {\n"
20667                "  return 29;\n"
20668                "} else [[likely]] {\n"
20669                "  return 42;\n"
20670                "}\n",
20671                Style);
20672 
20673   verifyFormat("if (argc > 5) [[unlikely]] {\n"
20674                "  return 29;\n"
20675                "} else if (argc > 10) [[likely]] {\n"
20676                "  return 99;\n"
20677                "} else {\n"
20678                "  return 42;\n"
20679                "}\n",
20680                Style);
20681 
20682   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
20683                "  return 29;\n"
20684                "}",
20685                Style);
20686 }
20687 
20688 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
20689   verifyFormat("Constructor()\n"
20690                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20691                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
20692                "aaaaaaaaaaaaaaaaaat))");
20693   verifyFormat("Constructor()\n"
20694                "    : aaaaaaaaaaaaa(aaaaaa), "
20695                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
20696 
20697   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
20698   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
20699   verifyFormat("Constructor()\n"
20700                "    : aaaaaa(aaaaaa),\n"
20701                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20702                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
20703                StyleWithWhitespacePenalty);
20704   verifyFormat("Constructor()\n"
20705                "    : aaaaaaaaaaaaa(aaaaaa), "
20706                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
20707                StyleWithWhitespacePenalty);
20708 }
20709 
20710 TEST_F(FormatTest, LLVMDefaultStyle) {
20711   FormatStyle Style = getLLVMStyle();
20712   verifyFormat("extern \"C\" {\n"
20713                "int foo();\n"
20714                "}",
20715                Style);
20716 }
20717 TEST_F(FormatTest, GNUDefaultStyle) {
20718   FormatStyle Style = getGNUStyle();
20719   verifyFormat("extern \"C\"\n"
20720                "{\n"
20721                "  int foo ();\n"
20722                "}",
20723                Style);
20724 }
20725 TEST_F(FormatTest, MozillaDefaultStyle) {
20726   FormatStyle Style = getMozillaStyle();
20727   verifyFormat("extern \"C\"\n"
20728                "{\n"
20729                "  int foo();\n"
20730                "}",
20731                Style);
20732 }
20733 TEST_F(FormatTest, GoogleDefaultStyle) {
20734   FormatStyle Style = getGoogleStyle();
20735   verifyFormat("extern \"C\" {\n"
20736                "int foo();\n"
20737                "}",
20738                Style);
20739 }
20740 TEST_F(FormatTest, ChromiumDefaultStyle) {
20741   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
20742   verifyFormat("extern \"C\" {\n"
20743                "int foo();\n"
20744                "}",
20745                Style);
20746 }
20747 TEST_F(FormatTest, MicrosoftDefaultStyle) {
20748   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
20749   verifyFormat("extern \"C\"\n"
20750                "{\n"
20751                "    int foo();\n"
20752                "}",
20753                Style);
20754 }
20755 TEST_F(FormatTest, WebKitDefaultStyle) {
20756   FormatStyle Style = getWebKitStyle();
20757   verifyFormat("extern \"C\" {\n"
20758                "int foo();\n"
20759                "}",
20760                Style);
20761 }
20762 
20763 TEST_F(FormatTest, ConceptsAndRequires) {
20764   FormatStyle Style = getLLVMStyle();
20765   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20766 
20767   verifyFormat("template <typename T>\n"
20768                "concept Hashable = requires(T a) {\n"
20769                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
20770                "};",
20771                Style);
20772   verifyFormat("template <typename T>\n"
20773                "concept EqualityComparable = requires(T a, T b) {\n"
20774                "  { a == b } -> bool;\n"
20775                "};",
20776                Style);
20777   verifyFormat("template <typename T>\n"
20778                "concept EqualityComparable = requires(T a, T b) {\n"
20779                "  { a == b } -> bool;\n"
20780                "  { a != b } -> bool;\n"
20781                "};",
20782                Style);
20783   verifyFormat("template <typename T>\n"
20784                "concept EqualityComparable = requires(T a, T b) {\n"
20785                "  { a == b } -> bool;\n"
20786                "  { a != b } -> bool;\n"
20787                "};",
20788                Style);
20789 
20790   verifyFormat("template <typename It>\n"
20791                "requires Iterator<It>\n"
20792                "void sort(It begin, It end) {\n"
20793                "  //....\n"
20794                "}",
20795                Style);
20796 
20797   verifyFormat("template <typename T>\n"
20798                "concept Large = sizeof(T) > 10;",
20799                Style);
20800 
20801   verifyFormat("template <typename T, typename U>\n"
20802                "concept FooableWith = requires(T t, U u) {\n"
20803                "  typename T::foo_type;\n"
20804                "  { t.foo(u) } -> typename T::foo_type;\n"
20805                "  t++;\n"
20806                "};\n"
20807                "void doFoo(FooableWith<int> auto t) {\n"
20808                "  t.foo(3);\n"
20809                "}",
20810                Style);
20811   verifyFormat("template <typename T>\n"
20812                "concept Context = sizeof(T) == 1;",
20813                Style);
20814   verifyFormat("template <typename T>\n"
20815                "concept Context = is_specialization_of_v<context, T>;",
20816                Style);
20817   verifyFormat("template <typename T>\n"
20818                "concept Node = std::is_object_v<T>;",
20819                Style);
20820   verifyFormat("template <typename T>\n"
20821                "concept Tree = true;",
20822                Style);
20823 
20824   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
20825                "  //...\n"
20826                "}",
20827                Style);
20828 
20829   verifyFormat(
20830       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
20831       "  //...\n"
20832       "}",
20833       Style);
20834 
20835   verifyFormat(
20836       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
20837       "  //...\n"
20838       "}",
20839       Style);
20840 
20841   verifyFormat("template <typename T>\n"
20842                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
20843                "Concept2<I> {\n"
20844                "  //...\n"
20845                "}",
20846                Style);
20847 
20848   verifyFormat("template <typename T>\n"
20849                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
20850                "Concept2<I> {\n"
20851                "  //...\n"
20852                "}",
20853                Style);
20854 
20855   verifyFormat(
20856       "template <typename T>\n"
20857       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
20858       "  //...\n"
20859       "}",
20860       Style);
20861 
20862   verifyFormat(
20863       "template <typename T>\n"
20864       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
20865       "  //...\n"
20866       "}",
20867       Style);
20868 
20869   verifyFormat("template <typename It>\n"
20870                "requires Foo<It>() && Bar<It> {\n"
20871                "  //....\n"
20872                "}",
20873                Style);
20874 
20875   verifyFormat("template <typename It>\n"
20876                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
20877                "  //....\n"
20878                "}",
20879                Style);
20880 
20881   verifyFormat("template <typename It>\n"
20882                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
20883                "  //....\n"
20884                "}",
20885                Style);
20886 
20887   verifyFormat(
20888       "template <typename It>\n"
20889       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
20890       "  //....\n"
20891       "}",
20892       Style);
20893 
20894   Style.IndentRequires = true;
20895   verifyFormat("template <typename It>\n"
20896                "  requires Iterator<It>\n"
20897                "void sort(It begin, It end) {\n"
20898                "  //....\n"
20899                "}",
20900                Style);
20901   verifyFormat("template <std::size index_>\n"
20902                "  requires(index_ < sizeof...(Children_))\n"
20903                "Tree auto &child() {\n"
20904                "  // ...\n"
20905                "}",
20906                Style);
20907 
20908   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
20909   verifyFormat("template <typename T>\n"
20910                "concept Hashable = requires (T a) {\n"
20911                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
20912                "};",
20913                Style);
20914 
20915   verifyFormat("template <class T = void>\n"
20916                "  requires EqualityComparable<T> || Same<T, void>\n"
20917                "struct equal_to;",
20918                Style);
20919 
20920   verifyFormat("template <class T>\n"
20921                "  requires requires {\n"
20922                "    T{};\n"
20923                "    T (int);\n"
20924                "  }\n",
20925                Style);
20926 
20927   Style.ColumnLimit = 78;
20928   verifyFormat("template <typename T>\n"
20929                "concept Context = Traits<typename T::traits_type> and\n"
20930                "    Interface<typename T::interface_type> and\n"
20931                "    Request<typename T::request_type> and\n"
20932                "    Response<typename T::response_type> and\n"
20933                "    ContextExtension<typename T::extension_type> and\n"
20934                "    ::std::is_copy_constructable<T> and "
20935                "::std::is_move_constructable<T> and\n"
20936                "    requires (T c) {\n"
20937                "  { c.response; } -> Response;\n"
20938                "} and requires (T c) {\n"
20939                "  { c.request; } -> Request;\n"
20940                "}\n",
20941                Style);
20942 
20943   verifyFormat("template <typename T>\n"
20944                "concept Context = Traits<typename T::traits_type> or\n"
20945                "    Interface<typename T::interface_type> or\n"
20946                "    Request<typename T::request_type> or\n"
20947                "    Response<typename T::response_type> or\n"
20948                "    ContextExtension<typename T::extension_type> or\n"
20949                "    ::std::is_copy_constructable<T> or "
20950                "::std::is_move_constructable<T> or\n"
20951                "    requires (T c) {\n"
20952                "  { c.response; } -> Response;\n"
20953                "} or requires (T c) {\n"
20954                "  { c.request; } -> Request;\n"
20955                "}\n",
20956                Style);
20957 
20958   verifyFormat("template <typename T>\n"
20959                "concept Context = Traits<typename T::traits_type> &&\n"
20960                "    Interface<typename T::interface_type> &&\n"
20961                "    Request<typename T::request_type> &&\n"
20962                "    Response<typename T::response_type> &&\n"
20963                "    ContextExtension<typename T::extension_type> &&\n"
20964                "    ::std::is_copy_constructable<T> && "
20965                "::std::is_move_constructable<T> &&\n"
20966                "    requires (T c) {\n"
20967                "  { c.response; } -> Response;\n"
20968                "} && requires (T c) {\n"
20969                "  { c.request; } -> Request;\n"
20970                "}\n",
20971                Style);
20972 
20973   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
20974                "Constraint2<T>;");
20975 
20976   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
20977   Style.BraceWrapping.AfterFunction = true;
20978   Style.BraceWrapping.AfterClass = true;
20979   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
20980   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
20981   verifyFormat("void Foo () requires (std::copyable<T>)\n"
20982                "{\n"
20983                "  return\n"
20984                "}\n",
20985                Style);
20986 
20987   verifyFormat("void Foo () requires std::copyable<T>\n"
20988                "{\n"
20989                "  return\n"
20990                "}\n",
20991                Style);
20992 
20993   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
20994                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
20995                "struct constant;",
20996                Style);
20997 
20998   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
20999                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
21000                "struct constant;",
21001                Style);
21002 
21003   verifyFormat("template <class T>\n"
21004                "class plane_with_very_very_very_long_name\n"
21005                "{\n"
21006                "  constexpr plane_with_very_very_very_long_name () requires "
21007                "std::copyable<T>\n"
21008                "      : plane_with_very_very_very_long_name (1)\n"
21009                "  {\n"
21010                "  }\n"
21011                "}\n",
21012                Style);
21013 
21014   verifyFormat("template <class T>\n"
21015                "class plane_with_long_name\n"
21016                "{\n"
21017                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
21018                "      : plane_with_long_name (1)\n"
21019                "  {\n"
21020                "  }\n"
21021                "}\n",
21022                Style);
21023 
21024   Style.BreakBeforeConceptDeclarations = false;
21025   verifyFormat("template <typename T> concept Tree = true;", Style);
21026 
21027   Style.IndentRequires = false;
21028   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
21029                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
21030                "struct constant;",
21031                Style);
21032 }
21033 
21034 TEST_F(FormatTest, StatementAttributeLikeMacros) {
21035   FormatStyle Style = getLLVMStyle();
21036   StringRef Source = "void Foo::slot() {\n"
21037                      "  unsigned char MyChar = 'x';\n"
21038                      "  emit signal(MyChar);\n"
21039                      "  Q_EMIT signal(MyChar);\n"
21040                      "}";
21041 
21042   EXPECT_EQ(Source, format(Source, Style));
21043 
21044   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
21045   EXPECT_EQ("void Foo::slot() {\n"
21046             "  unsigned char MyChar = 'x';\n"
21047             "  emit          signal(MyChar);\n"
21048             "  Q_EMIT signal(MyChar);\n"
21049             "}",
21050             format(Source, Style));
21051 
21052   Style.StatementAttributeLikeMacros.push_back("emit");
21053   EXPECT_EQ(Source, format(Source, Style));
21054 
21055   Style.StatementAttributeLikeMacros = {};
21056   EXPECT_EQ("void Foo::slot() {\n"
21057             "  unsigned char MyChar = 'x';\n"
21058             "  emit          signal(MyChar);\n"
21059             "  Q_EMIT        signal(MyChar);\n"
21060             "}",
21061             format(Source, Style));
21062 }
21063 
21064 TEST_F(FormatTest, IndentAccessModifiers) {
21065   FormatStyle Style = getLLVMStyle();
21066   Style.IndentAccessModifiers = true;
21067   // Members are *two* levels below the record;
21068   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
21069   verifyFormat("class C {\n"
21070                "    int i;\n"
21071                "};\n",
21072                Style);
21073   verifyFormat("union C {\n"
21074                "    int i;\n"
21075                "    unsigned u;\n"
21076                "};\n",
21077                Style);
21078   // Access modifiers should be indented one level below the record.
21079   verifyFormat("class C {\n"
21080                "  public:\n"
21081                "    int i;\n"
21082                "};\n",
21083                Style);
21084   verifyFormat("struct S {\n"
21085                "  private:\n"
21086                "    class C {\n"
21087                "        int j;\n"
21088                "\n"
21089                "      public:\n"
21090                "        C();\n"
21091                "    };\n"
21092                "\n"
21093                "  public:\n"
21094                "    int i;\n"
21095                "};\n",
21096                Style);
21097   // Enumerations are not records and should be unaffected.
21098   Style.AllowShortEnumsOnASingleLine = false;
21099   verifyFormat("enum class E\n"
21100                "{\n"
21101                "  A,\n"
21102                "  B\n"
21103                "};\n",
21104                Style);
21105   // Test with a different indentation width;
21106   // also proves that the result is Style.AccessModifierOffset agnostic.
21107   Style.IndentWidth = 3;
21108   verifyFormat("class C {\n"
21109                "   public:\n"
21110                "      int i;\n"
21111                "};\n",
21112                Style);
21113 }
21114 
21115 TEST_F(FormatTest, LimitlessStringsAndComments) {
21116   auto Style = getLLVMStyleWithColumns(0);
21117   constexpr StringRef Code =
21118       "/**\n"
21119       " * This is a multiline comment with quite some long lines, at least for "
21120       "the LLVM Style.\n"
21121       " * We will redo this with strings and line comments. Just to  check if "
21122       "everything is working.\n"
21123       " */\n"
21124       "bool foo() {\n"
21125       "  /* Single line multi line comment. */\n"
21126       "  const std::string String = \"This is a multiline string with quite "
21127       "some long lines, at least for the LLVM Style.\"\n"
21128       "                             \"We already did it with multi line "
21129       "comments, and we will do it with line comments. Just to check if "
21130       "everything is working.\";\n"
21131       "  // This is a line comment (block) with quite some long lines, at "
21132       "least for the LLVM Style.\n"
21133       "  // We already did this with multi line comments and strings. Just to "
21134       "check if everything is working.\n"
21135       "  const std::string SmallString = \"Hello World\";\n"
21136       "  // Small line comment\n"
21137       "  return String.size() > SmallString.size();\n"
21138       "}";
21139   EXPECT_EQ(Code, format(Code, Style));
21140 }
21141 } // namespace
21142 } // namespace format
21143 } // namespace clang
21144