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, IndentsPPDirectiveInReducedSpace) {
3457   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
3458   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
3459   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
3460   // FIXME: We never break before the macro name.
3461   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
3462 
3463   verifyFormat("#define A A\n#define A A");
3464   verifyFormat("#define A(X) A\n#define A A");
3465 
3466   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
3467   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
3468 }
3469 
3470 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
3471   EXPECT_EQ("// somecomment\n"
3472             "#include \"a.h\"\n"
3473             "#define A(  \\\n"
3474             "    A, B)\n"
3475             "#include \"b.h\"\n"
3476             "// somecomment\n",
3477             format("  // somecomment\n"
3478                    "  #include \"a.h\"\n"
3479                    "#define A(A,\\\n"
3480                    "    B)\n"
3481                    "    #include \"b.h\"\n"
3482                    " // somecomment\n",
3483                    getLLVMStyleWithColumns(13)));
3484 }
3485 
3486 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
3487 
3488 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
3489   EXPECT_EQ("#define A    \\\n"
3490             "  c;         \\\n"
3491             "  e;\n"
3492             "f;",
3493             format("#define A c; e;\n"
3494                    "f;",
3495                    getLLVMStyleWithColumns(14)));
3496 }
3497 
3498 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
3499 
3500 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
3501   EXPECT_EQ("int x,\n"
3502             "#define A\n"
3503             "    y;",
3504             format("int x,\n#define A\ny;"));
3505 }
3506 
3507 TEST_F(FormatTest, HashInMacroDefinition) {
3508   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
3509   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
3510   verifyFormat("#define A  \\\n"
3511                "  {        \\\n"
3512                "    f(#c); \\\n"
3513                "  }",
3514                getLLVMStyleWithColumns(11));
3515 
3516   verifyFormat("#define A(X)         \\\n"
3517                "  void function##X()",
3518                getLLVMStyleWithColumns(22));
3519 
3520   verifyFormat("#define A(a, b, c)   \\\n"
3521                "  void a##b##c()",
3522                getLLVMStyleWithColumns(22));
3523 
3524   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
3525 }
3526 
3527 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
3528   EXPECT_EQ("#define A (x)", format("#define A (x)"));
3529   EXPECT_EQ("#define A(x)", format("#define A(x)"));
3530 
3531   FormatStyle Style = getLLVMStyle();
3532   Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
3533   verifyFormat("#define true ((foo)1)", Style);
3534   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
3535   verifyFormat("#define false((foo)0)", Style);
3536 }
3537 
3538 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
3539   EXPECT_EQ("#define A b;", format("#define A \\\n"
3540                                    "          \\\n"
3541                                    "  b;",
3542                                    getLLVMStyleWithColumns(25)));
3543   EXPECT_EQ("#define A \\\n"
3544             "          \\\n"
3545             "  a;      \\\n"
3546             "  b;",
3547             format("#define A \\\n"
3548                    "          \\\n"
3549                    "  a;      \\\n"
3550                    "  b;",
3551                    getLLVMStyleWithColumns(11)));
3552   EXPECT_EQ("#define A \\\n"
3553             "  a;      \\\n"
3554             "          \\\n"
3555             "  b;",
3556             format("#define A \\\n"
3557                    "  a;      \\\n"
3558                    "          \\\n"
3559                    "  b;",
3560                    getLLVMStyleWithColumns(11)));
3561 }
3562 
3563 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
3564   verifyIncompleteFormat("#define A :");
3565   verifyFormat("#define SOMECASES  \\\n"
3566                "  case 1:          \\\n"
3567                "  case 2\n",
3568                getLLVMStyleWithColumns(20));
3569   verifyFormat("#define MACRO(a) \\\n"
3570                "  if (a)         \\\n"
3571                "    f();         \\\n"
3572                "  else           \\\n"
3573                "    g()",
3574                getLLVMStyleWithColumns(18));
3575   verifyFormat("#define A template <typename T>");
3576   verifyIncompleteFormat("#define STR(x) #x\n"
3577                          "f(STR(this_is_a_string_literal{));");
3578   verifyFormat("#pragma omp threadprivate( \\\n"
3579                "    y)), // expected-warning",
3580                getLLVMStyleWithColumns(28));
3581   verifyFormat("#d, = };");
3582   verifyFormat("#if \"a");
3583   verifyIncompleteFormat("({\n"
3584                          "#define b     \\\n"
3585                          "  }           \\\n"
3586                          "  a\n"
3587                          "a",
3588                          getLLVMStyleWithColumns(15));
3589   verifyFormat("#define A     \\\n"
3590                "  {           \\\n"
3591                "    {\n"
3592                "#define B     \\\n"
3593                "  }           \\\n"
3594                "  }",
3595                getLLVMStyleWithColumns(15));
3596   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
3597   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
3598   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
3599   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
3600 }
3601 
3602 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
3603   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
3604   EXPECT_EQ("class A : public QObject {\n"
3605             "  Q_OBJECT\n"
3606             "\n"
3607             "  A() {}\n"
3608             "};",
3609             format("class A  :  public QObject {\n"
3610                    "     Q_OBJECT\n"
3611                    "\n"
3612                    "  A() {\n}\n"
3613                    "}  ;"));
3614   EXPECT_EQ("MACRO\n"
3615             "/*static*/ int i;",
3616             format("MACRO\n"
3617                    " /*static*/ int   i;"));
3618   EXPECT_EQ("SOME_MACRO\n"
3619             "namespace {\n"
3620             "void f();\n"
3621             "} // namespace",
3622             format("SOME_MACRO\n"
3623                    "  namespace    {\n"
3624                    "void   f(  );\n"
3625                    "} // namespace"));
3626   // Only if the identifier contains at least 5 characters.
3627   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
3628   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
3629   // Only if everything is upper case.
3630   EXPECT_EQ("class A : public QObject {\n"
3631             "  Q_Object A() {}\n"
3632             "};",
3633             format("class A  :  public QObject {\n"
3634                    "     Q_Object\n"
3635                    "  A() {\n}\n"
3636                    "}  ;"));
3637 
3638   // Only if the next line can actually start an unwrapped line.
3639   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
3640             format("SOME_WEIRD_LOG_MACRO\n"
3641                    "<< SomeThing;"));
3642 
3643   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
3644                "(n, buffers))\n",
3645                getChromiumStyle(FormatStyle::LK_Cpp));
3646 
3647   // See PR41483
3648   EXPECT_EQ("/**/ FOO(a)\n"
3649             "FOO(b)",
3650             format("/**/ FOO(a)\n"
3651                    "FOO(b)"));
3652 }
3653 
3654 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
3655   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
3656             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
3657             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
3658             "class X {};\n"
3659             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
3660             "int *createScopDetectionPass() { return 0; }",
3661             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
3662                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
3663                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
3664                    "  class X {};\n"
3665                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
3666                    "  int *createScopDetectionPass() { return 0; }"));
3667   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
3668   // braces, so that inner block is indented one level more.
3669   EXPECT_EQ("int q() {\n"
3670             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
3671             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
3672             "  IPC_END_MESSAGE_MAP()\n"
3673             "}",
3674             format("int q() {\n"
3675                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
3676                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
3677                    "  IPC_END_MESSAGE_MAP()\n"
3678                    "}"));
3679 
3680   // Same inside macros.
3681   EXPECT_EQ("#define LIST(L) \\\n"
3682             "  L(A)          \\\n"
3683             "  L(B)          \\\n"
3684             "  L(C)",
3685             format("#define LIST(L) \\\n"
3686                    "  L(A) \\\n"
3687                    "  L(B) \\\n"
3688                    "  L(C)",
3689                    getGoogleStyle()));
3690 
3691   // These must not be recognized as macros.
3692   EXPECT_EQ("int q() {\n"
3693             "  f(x);\n"
3694             "  f(x) {}\n"
3695             "  f(x)->g();\n"
3696             "  f(x)->*g();\n"
3697             "  f(x).g();\n"
3698             "  f(x) = x;\n"
3699             "  f(x) += x;\n"
3700             "  f(x) -= x;\n"
3701             "  f(x) *= x;\n"
3702             "  f(x) /= x;\n"
3703             "  f(x) %= x;\n"
3704             "  f(x) &= x;\n"
3705             "  f(x) |= x;\n"
3706             "  f(x) ^= x;\n"
3707             "  f(x) >>= x;\n"
3708             "  f(x) <<= x;\n"
3709             "  f(x)[y].z();\n"
3710             "  LOG(INFO) << x;\n"
3711             "  ifstream(x) >> x;\n"
3712             "}\n",
3713             format("int q() {\n"
3714                    "  f(x)\n;\n"
3715                    "  f(x)\n {}\n"
3716                    "  f(x)\n->g();\n"
3717                    "  f(x)\n->*g();\n"
3718                    "  f(x)\n.g();\n"
3719                    "  f(x)\n = x;\n"
3720                    "  f(x)\n += x;\n"
3721                    "  f(x)\n -= x;\n"
3722                    "  f(x)\n *= x;\n"
3723                    "  f(x)\n /= x;\n"
3724                    "  f(x)\n %= x;\n"
3725                    "  f(x)\n &= x;\n"
3726                    "  f(x)\n |= x;\n"
3727                    "  f(x)\n ^= x;\n"
3728                    "  f(x)\n >>= x;\n"
3729                    "  f(x)\n <<= x;\n"
3730                    "  f(x)\n[y].z();\n"
3731                    "  LOG(INFO)\n << x;\n"
3732                    "  ifstream(x)\n >> x;\n"
3733                    "}\n"));
3734   EXPECT_EQ("int q() {\n"
3735             "  F(x)\n"
3736             "  if (1) {\n"
3737             "  }\n"
3738             "  F(x)\n"
3739             "  while (1) {\n"
3740             "  }\n"
3741             "  F(x)\n"
3742             "  G(x);\n"
3743             "  F(x)\n"
3744             "  try {\n"
3745             "    Q();\n"
3746             "  } catch (...) {\n"
3747             "  }\n"
3748             "}\n",
3749             format("int q() {\n"
3750                    "F(x)\n"
3751                    "if (1) {}\n"
3752                    "F(x)\n"
3753                    "while (1) {}\n"
3754                    "F(x)\n"
3755                    "G(x);\n"
3756                    "F(x)\n"
3757                    "try { Q(); } catch (...) {}\n"
3758                    "}\n"));
3759   EXPECT_EQ("class A {\n"
3760             "  A() : t(0) {}\n"
3761             "  A(int i) noexcept() : {}\n"
3762             "  A(X x)\n" // FIXME: function-level try blocks are broken.
3763             "  try : t(0) {\n"
3764             "  } catch (...) {\n"
3765             "  }\n"
3766             "};",
3767             format("class A {\n"
3768                    "  A()\n : t(0) {}\n"
3769                    "  A(int i)\n noexcept() : {}\n"
3770                    "  A(X x)\n"
3771                    "  try : t(0) {} catch (...) {}\n"
3772                    "};"));
3773   FormatStyle Style = getLLVMStyle();
3774   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3775   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3776   Style.BraceWrapping.AfterFunction = true;
3777   EXPECT_EQ("void f()\n"
3778             "try\n"
3779             "{\n"
3780             "}",
3781             format("void f() try {\n"
3782                    "}",
3783                    Style));
3784   EXPECT_EQ("class SomeClass {\n"
3785             "public:\n"
3786             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3787             "};",
3788             format("class SomeClass {\n"
3789                    "public:\n"
3790                    "  SomeClass()\n"
3791                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3792                    "};"));
3793   EXPECT_EQ("class SomeClass {\n"
3794             "public:\n"
3795             "  SomeClass()\n"
3796             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3797             "};",
3798             format("class SomeClass {\n"
3799                    "public:\n"
3800                    "  SomeClass()\n"
3801                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3802                    "};",
3803                    getLLVMStyleWithColumns(40)));
3804 
3805   verifyFormat("MACRO(>)");
3806 
3807   // Some macros contain an implicit semicolon.
3808   Style = getLLVMStyle();
3809   Style.StatementMacros.push_back("FOO");
3810   verifyFormat("FOO(a) int b = 0;");
3811   verifyFormat("FOO(a)\n"
3812                "int b = 0;",
3813                Style);
3814   verifyFormat("FOO(a);\n"
3815                "int b = 0;",
3816                Style);
3817   verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
3818                "int b = 0;",
3819                Style);
3820   verifyFormat("FOO()\n"
3821                "int b = 0;",
3822                Style);
3823   verifyFormat("FOO\n"
3824                "int b = 0;",
3825                Style);
3826   verifyFormat("void f() {\n"
3827                "  FOO(a)\n"
3828                "  return a;\n"
3829                "}",
3830                Style);
3831   verifyFormat("FOO(a)\n"
3832                "FOO(b)",
3833                Style);
3834   verifyFormat("int a = 0;\n"
3835                "FOO(b)\n"
3836                "int c = 0;",
3837                Style);
3838   verifyFormat("int a = 0;\n"
3839                "int x = FOO(a)\n"
3840                "int b = 0;",
3841                Style);
3842   verifyFormat("void foo(int a) { FOO(a) }\n"
3843                "uint32_t bar() {}",
3844                Style);
3845 }
3846 
3847 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
3848   verifyFormat("#define A \\\n"
3849                "  f({     \\\n"
3850                "    g();  \\\n"
3851                "  });",
3852                getLLVMStyleWithColumns(11));
3853 }
3854 
3855 TEST_F(FormatTest, IndentPreprocessorDirectives) {
3856   FormatStyle Style = getLLVMStyle();
3857   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
3858   Style.ColumnLimit = 40;
3859   verifyFormat("#ifdef _WIN32\n"
3860                "#define A 0\n"
3861                "#ifdef VAR2\n"
3862                "#define B 1\n"
3863                "#include <someheader.h>\n"
3864                "#define MACRO                          \\\n"
3865                "  some_very_long_func_aaaaaaaaaa();\n"
3866                "#endif\n"
3867                "#else\n"
3868                "#define A 1\n"
3869                "#endif",
3870                Style);
3871   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
3872   verifyFormat("#ifdef _WIN32\n"
3873                "#  define A 0\n"
3874                "#  ifdef VAR2\n"
3875                "#    define B 1\n"
3876                "#    include <someheader.h>\n"
3877                "#    define MACRO                      \\\n"
3878                "      some_very_long_func_aaaaaaaaaa();\n"
3879                "#  endif\n"
3880                "#else\n"
3881                "#  define A 1\n"
3882                "#endif",
3883                Style);
3884   verifyFormat("#if A\n"
3885                "#  define MACRO                        \\\n"
3886                "    void a(int x) {                    \\\n"
3887                "      b();                             \\\n"
3888                "      c();                             \\\n"
3889                "      d();                             \\\n"
3890                "      e();                             \\\n"
3891                "      f();                             \\\n"
3892                "    }\n"
3893                "#endif",
3894                Style);
3895   // Comments before include guard.
3896   verifyFormat("// file comment\n"
3897                "// file comment\n"
3898                "#ifndef HEADER_H\n"
3899                "#define HEADER_H\n"
3900                "code();\n"
3901                "#endif",
3902                Style);
3903   // Test with include guards.
3904   verifyFormat("#ifndef HEADER_H\n"
3905                "#define HEADER_H\n"
3906                "code();\n"
3907                "#endif",
3908                Style);
3909   // Include guards must have a #define with the same variable immediately
3910   // after #ifndef.
3911   verifyFormat("#ifndef NOT_GUARD\n"
3912                "#  define FOO\n"
3913                "code();\n"
3914                "#endif",
3915                Style);
3916 
3917   // Include guards must cover the entire file.
3918   verifyFormat("code();\n"
3919                "code();\n"
3920                "#ifndef NOT_GUARD\n"
3921                "#  define NOT_GUARD\n"
3922                "code();\n"
3923                "#endif",
3924                Style);
3925   verifyFormat("#ifndef NOT_GUARD\n"
3926                "#  define NOT_GUARD\n"
3927                "code();\n"
3928                "#endif\n"
3929                "code();",
3930                Style);
3931   // Test with trailing blank lines.
3932   verifyFormat("#ifndef HEADER_H\n"
3933                "#define HEADER_H\n"
3934                "code();\n"
3935                "#endif\n",
3936                Style);
3937   // Include guards don't have #else.
3938   verifyFormat("#ifndef NOT_GUARD\n"
3939                "#  define NOT_GUARD\n"
3940                "code();\n"
3941                "#else\n"
3942                "#endif",
3943                Style);
3944   verifyFormat("#ifndef NOT_GUARD\n"
3945                "#  define NOT_GUARD\n"
3946                "code();\n"
3947                "#elif FOO\n"
3948                "#endif",
3949                Style);
3950   // Non-identifier #define after potential include guard.
3951   verifyFormat("#ifndef FOO\n"
3952                "#  define 1\n"
3953                "#endif\n",
3954                Style);
3955   // #if closes past last non-preprocessor line.
3956   verifyFormat("#ifndef FOO\n"
3957                "#define FOO\n"
3958                "#if 1\n"
3959                "int i;\n"
3960                "#  define A 0\n"
3961                "#endif\n"
3962                "#endif\n",
3963                Style);
3964   // Don't crash if there is an #elif directive without a condition.
3965   verifyFormat("#if 1\n"
3966                "int x;\n"
3967                "#elif\n"
3968                "int y;\n"
3969                "#else\n"
3970                "int z;\n"
3971                "#endif",
3972                Style);
3973   // FIXME: This doesn't handle the case where there's code between the
3974   // #ifndef and #define but all other conditions hold. This is because when
3975   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
3976   // previous code line yet, so we can't detect it.
3977   EXPECT_EQ("#ifndef NOT_GUARD\n"
3978             "code();\n"
3979             "#define NOT_GUARD\n"
3980             "code();\n"
3981             "#endif",
3982             format("#ifndef NOT_GUARD\n"
3983                    "code();\n"
3984                    "#  define NOT_GUARD\n"
3985                    "code();\n"
3986                    "#endif",
3987                    Style));
3988   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
3989   // be outside an include guard. Examples are #pragma once and
3990   // #pragma GCC diagnostic, or anything else that does not change the meaning
3991   // of the file if it's included multiple times.
3992   EXPECT_EQ("#ifdef WIN32\n"
3993             "#  pragma once\n"
3994             "#endif\n"
3995             "#ifndef HEADER_H\n"
3996             "#  define HEADER_H\n"
3997             "code();\n"
3998             "#endif",
3999             format("#ifdef WIN32\n"
4000                    "#  pragma once\n"
4001                    "#endif\n"
4002                    "#ifndef HEADER_H\n"
4003                    "#define HEADER_H\n"
4004                    "code();\n"
4005                    "#endif",
4006                    Style));
4007   // FIXME: This does not detect when there is a single non-preprocessor line
4008   // in front of an include-guard-like structure where other conditions hold
4009   // because ScopedLineState hides the line.
4010   EXPECT_EQ("code();\n"
4011             "#ifndef HEADER_H\n"
4012             "#define HEADER_H\n"
4013             "code();\n"
4014             "#endif",
4015             format("code();\n"
4016                    "#ifndef HEADER_H\n"
4017                    "#  define HEADER_H\n"
4018                    "code();\n"
4019                    "#endif",
4020                    Style));
4021   // Keep comments aligned with #, otherwise indent comments normally. These
4022   // tests cannot use verifyFormat because messUp manipulates leading
4023   // whitespace.
4024   {
4025     const char *Expected = ""
4026                            "void f() {\n"
4027                            "#if 1\n"
4028                            "// Preprocessor aligned.\n"
4029                            "#  define A 0\n"
4030                            "  // Code. Separated by blank line.\n"
4031                            "\n"
4032                            "#  define B 0\n"
4033                            "  // Code. Not aligned with #\n"
4034                            "#  define C 0\n"
4035                            "#endif";
4036     const char *ToFormat = ""
4037                            "void f() {\n"
4038                            "#if 1\n"
4039                            "// Preprocessor aligned.\n"
4040                            "#  define A 0\n"
4041                            "// Code. Separated by blank line.\n"
4042                            "\n"
4043                            "#  define B 0\n"
4044                            "   // Code. Not aligned with #\n"
4045                            "#  define C 0\n"
4046                            "#endif";
4047     EXPECT_EQ(Expected, format(ToFormat, Style));
4048     EXPECT_EQ(Expected, format(Expected, Style));
4049   }
4050   // Keep block quotes aligned.
4051   {
4052     const char *Expected = ""
4053                            "void f() {\n"
4054                            "#if 1\n"
4055                            "/* Preprocessor aligned. */\n"
4056                            "#  define A 0\n"
4057                            "  /* Code. Separated by blank line. */\n"
4058                            "\n"
4059                            "#  define B 0\n"
4060                            "  /* Code. Not aligned with # */\n"
4061                            "#  define C 0\n"
4062                            "#endif";
4063     const char *ToFormat = ""
4064                            "void f() {\n"
4065                            "#if 1\n"
4066                            "/* Preprocessor aligned. */\n"
4067                            "#  define A 0\n"
4068                            "/* Code. Separated by blank line. */\n"
4069                            "\n"
4070                            "#  define B 0\n"
4071                            "   /* Code. Not aligned with # */\n"
4072                            "#  define C 0\n"
4073                            "#endif";
4074     EXPECT_EQ(Expected, format(ToFormat, Style));
4075     EXPECT_EQ(Expected, format(Expected, Style));
4076   }
4077   // Keep comments aligned with un-indented directives.
4078   {
4079     const char *Expected = ""
4080                            "void f() {\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     const char *ToFormat = ""
4089                            "void f() {\n"
4090                            "// Preprocessor aligned.\n"
4091                            "#define A 0\n"
4092                            "// Code. Separated by blank line.\n"
4093                            "\n"
4094                            "#define B 0\n"
4095                            "   // Code. Not aligned with #\n"
4096                            "#define C 0\n";
4097     EXPECT_EQ(Expected, format(ToFormat, Style));
4098     EXPECT_EQ(Expected, format(Expected, Style));
4099   }
4100   // Test AfterHash with tabs.
4101   {
4102     FormatStyle Tabbed = Style;
4103     Tabbed.UseTab = FormatStyle::UT_Always;
4104     Tabbed.IndentWidth = 8;
4105     Tabbed.TabWidth = 8;
4106     verifyFormat("#ifdef _WIN32\n"
4107                  "#\tdefine A 0\n"
4108                  "#\tifdef VAR2\n"
4109                  "#\t\tdefine B 1\n"
4110                  "#\t\tinclude <someheader.h>\n"
4111                  "#\t\tdefine MACRO          \\\n"
4112                  "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
4113                  "#\tendif\n"
4114                  "#else\n"
4115                  "#\tdefine A 1\n"
4116                  "#endif",
4117                  Tabbed);
4118   }
4119 
4120   // Regression test: Multiline-macro inside include guards.
4121   verifyFormat("#ifndef HEADER_H\n"
4122                "#define HEADER_H\n"
4123                "#define A()        \\\n"
4124                "  int i;           \\\n"
4125                "  int j;\n"
4126                "#endif // HEADER_H",
4127                getLLVMStyleWithColumns(20));
4128 
4129   Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
4130   // Basic before hash indent tests
4131   verifyFormat("#ifdef _WIN32\n"
4132                "  #define A 0\n"
4133                "  #ifdef VAR2\n"
4134                "    #define B 1\n"
4135                "    #include <someheader.h>\n"
4136                "    #define MACRO                      \\\n"
4137                "      some_very_long_func_aaaaaaaaaa();\n"
4138                "  #endif\n"
4139                "#else\n"
4140                "  #define A 1\n"
4141                "#endif",
4142                Style);
4143   verifyFormat("#if A\n"
4144                "  #define MACRO                        \\\n"
4145                "    void a(int x) {                    \\\n"
4146                "      b();                             \\\n"
4147                "      c();                             \\\n"
4148                "      d();                             \\\n"
4149                "      e();                             \\\n"
4150                "      f();                             \\\n"
4151                "    }\n"
4152                "#endif",
4153                Style);
4154   // Keep comments aligned with indented directives. These
4155   // tests cannot use verifyFormat because messUp manipulates leading
4156   // whitespace.
4157   {
4158     const char *Expected = "void f() {\n"
4159                            "// Aligned to preprocessor.\n"
4160                            "#if 1\n"
4161                            "  // Aligned to code.\n"
4162                            "  int a;\n"
4163                            "  #if 1\n"
4164                            "    // Aligned to preprocessor.\n"
4165                            "    #define A 0\n"
4166                            "  // Aligned to code.\n"
4167                            "  int b;\n"
4168                            "  #endif\n"
4169                            "#endif\n"
4170                            "}";
4171     const char *ToFormat = "void f() {\n"
4172                            "// Aligned to preprocessor.\n"
4173                            "#if 1\n"
4174                            "// Aligned to code.\n"
4175                            "int a;\n"
4176                            "#if 1\n"
4177                            "// Aligned to preprocessor.\n"
4178                            "#define A 0\n"
4179                            "// Aligned to code.\n"
4180                            "int b;\n"
4181                            "#endif\n"
4182                            "#endif\n"
4183                            "}";
4184     EXPECT_EQ(Expected, format(ToFormat, Style));
4185     EXPECT_EQ(Expected, format(Expected, Style));
4186   }
4187   {
4188     const char *Expected = "void f() {\n"
4189                            "/* Aligned to preprocessor. */\n"
4190                            "#if 1\n"
4191                            "  /* Aligned to code. */\n"
4192                            "  int a;\n"
4193                            "  #if 1\n"
4194                            "    /* Aligned to preprocessor. */\n"
4195                            "    #define A 0\n"
4196                            "  /* Aligned to code. */\n"
4197                            "  int b;\n"
4198                            "  #endif\n"
4199                            "#endif\n"
4200                            "}";
4201     const char *ToFormat = "void f() {\n"
4202                            "/* Aligned to preprocessor. */\n"
4203                            "#if 1\n"
4204                            "/* Aligned to code. */\n"
4205                            "int a;\n"
4206                            "#if 1\n"
4207                            "/* Aligned to preprocessor. */\n"
4208                            "#define A 0\n"
4209                            "/* Aligned to code. */\n"
4210                            "int b;\n"
4211                            "#endif\n"
4212                            "#endif\n"
4213                            "}";
4214     EXPECT_EQ(Expected, format(ToFormat, Style));
4215     EXPECT_EQ(Expected, format(Expected, Style));
4216   }
4217 
4218   // Test single comment before preprocessor
4219   verifyFormat("// Comment\n"
4220                "\n"
4221                "#if 1\n"
4222                "#endif",
4223                Style);
4224 }
4225 
4226 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
4227   verifyFormat("{\n  { a #c; }\n}");
4228 }
4229 
4230 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
4231   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
4232             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
4233   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
4234             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
4235 }
4236 
4237 TEST_F(FormatTest, EscapedNewlines) {
4238   FormatStyle Narrow = getLLVMStyleWithColumns(11);
4239   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
4240             format("#define A \\\nint i;\\\n  int j;", Narrow));
4241   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
4242   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
4243   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
4244   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
4245 
4246   FormatStyle AlignLeft = getLLVMStyle();
4247   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
4248   EXPECT_EQ("#define MACRO(x) \\\n"
4249             "private:         \\\n"
4250             "  int x(int a);\n",
4251             format("#define MACRO(x) \\\n"
4252                    "private:         \\\n"
4253                    "  int x(int a);\n",
4254                    AlignLeft));
4255 
4256   // CRLF line endings
4257   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
4258             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
4259   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
4260   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
4261   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
4262   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
4263   EXPECT_EQ("#define MACRO(x) \\\r\n"
4264             "private:         \\\r\n"
4265             "  int x(int a);\r\n",
4266             format("#define MACRO(x) \\\r\n"
4267                    "private:         \\\r\n"
4268                    "  int x(int a);\r\n",
4269                    AlignLeft));
4270 
4271   FormatStyle DontAlign = getLLVMStyle();
4272   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
4273   DontAlign.MaxEmptyLinesToKeep = 3;
4274   // FIXME: can't use verifyFormat here because the newline before
4275   // "public:" is not inserted the first time it's reformatted
4276   EXPECT_EQ("#define A \\\n"
4277             "  class Foo { \\\n"
4278             "    void bar(); \\\n"
4279             "\\\n"
4280             "\\\n"
4281             "\\\n"
4282             "  public: \\\n"
4283             "    void baz(); \\\n"
4284             "  };",
4285             format("#define A \\\n"
4286                    "  class Foo { \\\n"
4287                    "    void bar(); \\\n"
4288                    "\\\n"
4289                    "\\\n"
4290                    "\\\n"
4291                    "  public: \\\n"
4292                    "    void baz(); \\\n"
4293                    "  };",
4294                    DontAlign));
4295 }
4296 
4297 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
4298   verifyFormat("#define A \\\n"
4299                "  int v(  \\\n"
4300                "      a); \\\n"
4301                "  int i;",
4302                getLLVMStyleWithColumns(11));
4303 }
4304 
4305 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
4306   EXPECT_EQ(
4307       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
4308       "                      \\\n"
4309       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
4310       "\n"
4311       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
4312       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
4313       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
4314              "\\\n"
4315              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
4316              "  \n"
4317              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
4318              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
4319 }
4320 
4321 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
4322   EXPECT_EQ("int\n"
4323             "#define A\n"
4324             "    a;",
4325             format("int\n#define A\na;"));
4326   verifyFormat("functionCallTo(\n"
4327                "    someOtherFunction(\n"
4328                "        withSomeParameters, whichInSequence,\n"
4329                "        areLongerThanALine(andAnotherCall,\n"
4330                "#define A B\n"
4331                "                           withMoreParamters,\n"
4332                "                           whichStronglyInfluenceTheLayout),\n"
4333                "        andMoreParameters),\n"
4334                "    trailing);",
4335                getLLVMStyleWithColumns(69));
4336   verifyFormat("Foo::Foo()\n"
4337                "#ifdef BAR\n"
4338                "    : baz(0)\n"
4339                "#endif\n"
4340                "{\n"
4341                "}");
4342   verifyFormat("void f() {\n"
4343                "  if (true)\n"
4344                "#ifdef A\n"
4345                "    f(42);\n"
4346                "  x();\n"
4347                "#else\n"
4348                "    g();\n"
4349                "  x();\n"
4350                "#endif\n"
4351                "}");
4352   verifyFormat("void f(param1, param2,\n"
4353                "       param3,\n"
4354                "#ifdef A\n"
4355                "       param4(param5,\n"
4356                "#ifdef A1\n"
4357                "              param6,\n"
4358                "#ifdef A2\n"
4359                "              param7),\n"
4360                "#else\n"
4361                "              param8),\n"
4362                "       param9,\n"
4363                "#endif\n"
4364                "       param10,\n"
4365                "#endif\n"
4366                "       param11)\n"
4367                "#else\n"
4368                "       param12)\n"
4369                "#endif\n"
4370                "{\n"
4371                "  x();\n"
4372                "}",
4373                getLLVMStyleWithColumns(28));
4374   verifyFormat("#if 1\n"
4375                "int i;");
4376   verifyFormat("#if 1\n"
4377                "#endif\n"
4378                "#if 1\n"
4379                "#else\n"
4380                "#endif\n");
4381   verifyFormat("DEBUG({\n"
4382                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4383                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
4384                "});\n"
4385                "#if a\n"
4386                "#else\n"
4387                "#endif");
4388 
4389   verifyIncompleteFormat("void f(\n"
4390                          "#if A\n"
4391                          ");\n"
4392                          "#else\n"
4393                          "#endif");
4394 }
4395 
4396 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
4397   verifyFormat("#endif\n"
4398                "#if B");
4399 }
4400 
4401 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
4402   FormatStyle SingleLine = getLLVMStyle();
4403   SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
4404   verifyFormat("#if 0\n"
4405                "#elif 1\n"
4406                "#endif\n"
4407                "void foo() {\n"
4408                "  if (test) foo2();\n"
4409                "}",
4410                SingleLine);
4411 }
4412 
4413 TEST_F(FormatTest, LayoutBlockInsideParens) {
4414   verifyFormat("functionCall({ int i; });");
4415   verifyFormat("functionCall({\n"
4416                "  int i;\n"
4417                "  int j;\n"
4418                "});");
4419   verifyFormat("functionCall(\n"
4420                "    {\n"
4421                "      int i;\n"
4422                "      int j;\n"
4423                "    },\n"
4424                "    aaaa, bbbb, cccc);");
4425   verifyFormat("functionA(functionB({\n"
4426                "            int i;\n"
4427                "            int j;\n"
4428                "          }),\n"
4429                "          aaaa, bbbb, cccc);");
4430   verifyFormat("functionCall(\n"
4431                "    {\n"
4432                "      int i;\n"
4433                "      int j;\n"
4434                "    },\n"
4435                "    aaaa, bbbb, // comment\n"
4436                "    cccc);");
4437   verifyFormat("functionA(functionB({\n"
4438                "            int i;\n"
4439                "            int j;\n"
4440                "          }),\n"
4441                "          aaaa, bbbb, // comment\n"
4442                "          cccc);");
4443   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
4444   verifyFormat("functionCall(aaaa, bbbb, {\n"
4445                "  int i;\n"
4446                "  int j;\n"
4447                "});");
4448   verifyFormat(
4449       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
4450       "    {\n"
4451       "      int i; // break\n"
4452       "    },\n"
4453       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4454       "                                     ccccccccccccccccc));");
4455   verifyFormat("DEBUG({\n"
4456                "  if (a)\n"
4457                "    f();\n"
4458                "});");
4459 }
4460 
4461 TEST_F(FormatTest, LayoutBlockInsideStatement) {
4462   EXPECT_EQ("SOME_MACRO { int i; }\n"
4463             "int i;",
4464             format("  SOME_MACRO  {int i;}  int i;"));
4465 }
4466 
4467 TEST_F(FormatTest, LayoutNestedBlocks) {
4468   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
4469                "  struct s {\n"
4470                "    int i;\n"
4471                "  };\n"
4472                "  s kBitsToOs[] = {{10}};\n"
4473                "  for (int i = 0; i < 10; ++i)\n"
4474                "    return;\n"
4475                "}");
4476   verifyFormat("call(parameter, {\n"
4477                "  something();\n"
4478                "  // Comment using all columns.\n"
4479                "  somethingelse();\n"
4480                "});",
4481                getLLVMStyleWithColumns(40));
4482   verifyFormat("DEBUG( //\n"
4483                "    { f(); }, a);");
4484   verifyFormat("DEBUG( //\n"
4485                "    {\n"
4486                "      f(); //\n"
4487                "    },\n"
4488                "    a);");
4489 
4490   EXPECT_EQ("call(parameter, {\n"
4491             "  something();\n"
4492             "  // Comment too\n"
4493             "  // looooooooooong.\n"
4494             "  somethingElse();\n"
4495             "});",
4496             format("call(parameter, {\n"
4497                    "  something();\n"
4498                    "  // Comment too looooooooooong.\n"
4499                    "  somethingElse();\n"
4500                    "});",
4501                    getLLVMStyleWithColumns(29)));
4502   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
4503   EXPECT_EQ("DEBUG({ // comment\n"
4504             "  int i;\n"
4505             "});",
4506             format("DEBUG({ // comment\n"
4507                    "int  i;\n"
4508                    "});"));
4509   EXPECT_EQ("DEBUG({\n"
4510             "  int i;\n"
4511             "\n"
4512             "  // comment\n"
4513             "  int j;\n"
4514             "});",
4515             format("DEBUG({\n"
4516                    "  int  i;\n"
4517                    "\n"
4518                    "  // comment\n"
4519                    "  int  j;\n"
4520                    "});"));
4521 
4522   verifyFormat("DEBUG({\n"
4523                "  if (a)\n"
4524                "    return;\n"
4525                "});");
4526   verifyGoogleFormat("DEBUG({\n"
4527                      "  if (a) return;\n"
4528                      "});");
4529   FormatStyle Style = getGoogleStyle();
4530   Style.ColumnLimit = 45;
4531   verifyFormat("Debug(\n"
4532                "    aaaaa,\n"
4533                "    {\n"
4534                "      if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
4535                "    },\n"
4536                "    a);",
4537                Style);
4538 
4539   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
4540 
4541   verifyNoCrash("^{v^{a}}");
4542 }
4543 
4544 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
4545   EXPECT_EQ("#define MACRO()                     \\\n"
4546             "  Debug(aaa, /* force line break */ \\\n"
4547             "        {                           \\\n"
4548             "          int i;                    \\\n"
4549             "          int j;                    \\\n"
4550             "        })",
4551             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
4552                    "          {  int   i;  int  j;   })",
4553                    getGoogleStyle()));
4554 
4555   EXPECT_EQ("#define A                                       \\\n"
4556             "  [] {                                          \\\n"
4557             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
4558             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
4559             "  }",
4560             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
4561                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
4562                    getGoogleStyle()));
4563 }
4564 
4565 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
4566   EXPECT_EQ("{}", format("{}"));
4567   verifyFormat("enum E {};");
4568   verifyFormat("enum E {}");
4569   FormatStyle Style = getLLVMStyle();
4570   Style.SpaceInEmptyBlock = true;
4571   EXPECT_EQ("void f() { }", format("void f() {}", Style));
4572   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
4573   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
4574 }
4575 
4576 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
4577   FormatStyle Style = getLLVMStyle();
4578   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
4579   Style.MacroBlockEnd = "^[A-Z_]+_END$";
4580   verifyFormat("FOO_BEGIN\n"
4581                "  FOO_ENTRY\n"
4582                "FOO_END",
4583                Style);
4584   verifyFormat("FOO_BEGIN\n"
4585                "  NESTED_FOO_BEGIN\n"
4586                "    NESTED_FOO_ENTRY\n"
4587                "  NESTED_FOO_END\n"
4588                "FOO_END",
4589                Style);
4590   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
4591                "  int x;\n"
4592                "  x = 1;\n"
4593                "FOO_END(Baz)",
4594                Style);
4595 }
4596 
4597 //===----------------------------------------------------------------------===//
4598 // Line break tests.
4599 //===----------------------------------------------------------------------===//
4600 
4601 TEST_F(FormatTest, PreventConfusingIndents) {
4602   verifyFormat(
4603       "void f() {\n"
4604       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
4605       "                         parameter, parameter, parameter)),\n"
4606       "                     SecondLongCall(parameter));\n"
4607       "}");
4608   verifyFormat(
4609       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4610       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4611       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4612       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
4613   verifyFormat(
4614       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4615       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
4616       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
4617       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
4618   verifyFormat(
4619       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4620       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
4621       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
4622       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
4623   verifyFormat("int a = bbbb && ccc &&\n"
4624                "        fffff(\n"
4625                "#define A Just forcing a new line\n"
4626                "            ddd);");
4627 }
4628 
4629 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
4630   verifyFormat(
4631       "bool aaaaaaa =\n"
4632       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
4633       "    bbbbbbbb();");
4634   verifyFormat(
4635       "bool aaaaaaa =\n"
4636       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
4637       "    bbbbbbbb();");
4638 
4639   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
4640                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
4641                "    ccccccccc == ddddddddddd;");
4642   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
4643                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
4644                "    ccccccccc == ddddddddddd;");
4645   verifyFormat(
4646       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
4648       "    ccccccccc == ddddddddddd;");
4649 
4650   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
4651                "                 aaaaaa) &&\n"
4652                "         bbbbbb && cccccc;");
4653   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
4654                "                 aaaaaa) >>\n"
4655                "         bbbbbb;");
4656   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
4657                "    SourceMgr.getSpellingColumnNumber(\n"
4658                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
4659                "    1);");
4660 
4661   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4662                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
4663                "    cccccc) {\n}");
4664   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4665                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
4666                "              cccccc) {\n}");
4667   verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4668                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
4669                "              cccccc) {\n}");
4670   verifyFormat("b = a &&\n"
4671                "    // Comment\n"
4672                "    b.c && d;");
4673 
4674   // If the LHS of a comparison is not a binary expression itself, the
4675   // additional linebreak confuses many people.
4676   verifyFormat(
4677       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4678       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
4679       "}");
4680   verifyFormat(
4681       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4682       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4683       "}");
4684   verifyFormat(
4685       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
4686       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4687       "}");
4688   verifyFormat(
4689       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4690       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
4691       "}");
4692   // Even explicit parentheses stress the precedence enough to make the
4693   // additional break unnecessary.
4694   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4695                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4696                "}");
4697   // This cases is borderline, but with the indentation it is still readable.
4698   verifyFormat(
4699       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4700       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4701       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4702       "}",
4703       getLLVMStyleWithColumns(75));
4704 
4705   // If the LHS is a binary expression, we should still use the additional break
4706   // as otherwise the formatting hides the operator precedence.
4707   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4708                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4709                "    5) {\n"
4710                "}");
4711   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4712                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
4713                "    5) {\n"
4714                "}");
4715 
4716   FormatStyle OnePerLine = getLLVMStyle();
4717   OnePerLine.BinPackParameters = false;
4718   verifyFormat(
4719       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4720       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4721       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
4722       OnePerLine);
4723 
4724   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
4725                "                .aaa(aaaaaaaaaaaaa) *\n"
4726                "            aaaaaaa +\n"
4727                "        aaaaaaa;",
4728                getLLVMStyleWithColumns(40));
4729 }
4730 
4731 TEST_F(FormatTest, ExpressionIndentation) {
4732   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4733                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4734                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4735                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4736                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4737                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
4738                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4739                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
4740                "                 ccccccccccccccccccccccccccccccccccccccccc;");
4741   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4742                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4743                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4744                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4745   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4746                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4747                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4748                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4749   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4750                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4751                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4752                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4753   verifyFormat("if () {\n"
4754                "} else if (aaaaa && bbbbb > // break\n"
4755                "                        ccccc) {\n"
4756                "}");
4757   verifyFormat("if () {\n"
4758                "} else if constexpr (aaaaa && bbbbb > // break\n"
4759                "                                  ccccc) {\n"
4760                "}");
4761   verifyFormat("if () {\n"
4762                "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
4763                "                                  ccccc) {\n"
4764                "}");
4765   verifyFormat("if () {\n"
4766                "} else if (aaaaa &&\n"
4767                "           bbbbb > // break\n"
4768                "               ccccc &&\n"
4769                "           ddddd) {\n"
4770                "}");
4771 
4772   // Presence of a trailing comment used to change indentation of b.
4773   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
4774                "       b;\n"
4775                "return aaaaaaaaaaaaaaaaaaa +\n"
4776                "       b; //",
4777                getLLVMStyleWithColumns(30));
4778 }
4779 
4780 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
4781   // Not sure what the best system is here. Like this, the LHS can be found
4782   // immediately above an operator (everything with the same or a higher
4783   // indent). The RHS is aligned right of the operator and so compasses
4784   // everything until something with the same indent as the operator is found.
4785   // FIXME: Is this a good system?
4786   FormatStyle Style = getLLVMStyle();
4787   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4788   verifyFormat(
4789       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4790       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4791       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4792       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4793       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4794       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4795       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4796       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4797       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
4798       Style);
4799   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4800                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4801                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4802                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4803                Style);
4804   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4805                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4806                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4807                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4808                Style);
4809   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4810                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4811                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4812                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4813                Style);
4814   verifyFormat("if () {\n"
4815                "} else if (aaaaa\n"
4816                "           && bbbbb // break\n"
4817                "                  > ccccc) {\n"
4818                "}",
4819                Style);
4820   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4821                "       && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4822                Style);
4823   verifyFormat("return (a)\n"
4824                "       // comment\n"
4825                "       + b;",
4826                Style);
4827   verifyFormat(
4828       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4829       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4830       "             + cc;",
4831       Style);
4832 
4833   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4834                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4835                Style);
4836 
4837   // Forced by comments.
4838   verifyFormat(
4839       "unsigned ContentSize =\n"
4840       "    sizeof(int16_t)   // DWARF ARange version number\n"
4841       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4842       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
4843       "    + sizeof(int8_t); // Segment Size (in bytes)");
4844 
4845   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
4846                "       == boost::fusion::at_c<1>(iiii).second;",
4847                Style);
4848 
4849   Style.ColumnLimit = 60;
4850   verifyFormat("zzzzzzzzzz\n"
4851                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4852                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
4853                Style);
4854 
4855   Style.ColumnLimit = 80;
4856   Style.IndentWidth = 4;
4857   Style.TabWidth = 4;
4858   Style.UseTab = FormatStyle::UT_Always;
4859   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4860   Style.AlignOperands = FormatStyle::OAS_DontAlign;
4861   EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
4862             "\t&& (someOtherLongishConditionPart1\n"
4863             "\t\t|| someOtherEvenLongerNestedConditionPart2);",
4864             format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
4865                    "(someOtherLongishConditionPart1 || "
4866                    "someOtherEvenLongerNestedConditionPart2);",
4867                    Style));
4868 }
4869 
4870 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
4871   FormatStyle Style = getLLVMStyle();
4872   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4873   Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
4874 
4875   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4876                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4877                "                   + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4878                "              == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4879                "                         * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4880                "                     + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4881                "          && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4882                "                     * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4883                "                 > ccccccccccccccccccccccccccccccccccccccccc;",
4884                Style);
4885   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4886                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4887                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4888                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4889                Style);
4890   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4891                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4892                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4893                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4894                Style);
4895   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4896                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4897                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4898                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4899                Style);
4900   verifyFormat("if () {\n"
4901                "} else if (aaaaa\n"
4902                "           && bbbbb // break\n"
4903                "                  > ccccc) {\n"
4904                "}",
4905                Style);
4906   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4907                "    && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4908                Style);
4909   verifyFormat("return (a)\n"
4910                "     // comment\n"
4911                "     + b;",
4912                Style);
4913   verifyFormat(
4914       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4915       "               * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4916       "           + cc;",
4917       Style);
4918   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
4919                "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
4920                "                        : 3333333333333333;",
4921                Style);
4922   verifyFormat(
4923       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
4924       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
4925       "                                             : eeeeeeeeeeeeeeeeee)\n"
4926       "     : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
4927       "                        : 3333333333333333;",
4928       Style);
4929   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4930                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4931                Style);
4932 
4933   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
4934                "    == boost::fusion::at_c<1>(iiii).second;",
4935                Style);
4936 
4937   Style.ColumnLimit = 60;
4938   verifyFormat("zzzzzzzzzzzzz\n"
4939                "    = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4940                "   >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
4941                Style);
4942 
4943   // Forced by comments.
4944   Style.ColumnLimit = 80;
4945   verifyFormat(
4946       "unsigned ContentSize\n"
4947       "    = sizeof(int16_t) // DWARF ARange version number\n"
4948       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4949       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
4950       "    + sizeof(int8_t); // Segment Size (in bytes)",
4951       Style);
4952 
4953   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4954   verifyFormat(
4955       "unsigned ContentSize =\n"
4956       "    sizeof(int16_t)   // DWARF ARange version number\n"
4957       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4958       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
4959       "    + sizeof(int8_t); // Segment Size (in bytes)",
4960       Style);
4961 
4962   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4963   verifyFormat(
4964       "unsigned ContentSize =\n"
4965       "    sizeof(int16_t)   // DWARF ARange version number\n"
4966       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4967       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
4968       "    + sizeof(int8_t); // Segment Size (in bytes)",
4969       Style);
4970 }
4971 
4972 TEST_F(FormatTest, EnforcedOperatorWraps) {
4973   // Here we'd like to wrap after the || operators, but a comment is forcing an
4974   // earlier wrap.
4975   verifyFormat("bool x = aaaaa //\n"
4976                "         || bbbbb\n"
4977                "         //\n"
4978                "         || cccc;");
4979 }
4980 
4981 TEST_F(FormatTest, NoOperandAlignment) {
4982   FormatStyle Style = getLLVMStyle();
4983   Style.AlignOperands = FormatStyle::OAS_DontAlign;
4984   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
4985                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4986                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4987                Style);
4988   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4989   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4990                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4991                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4992                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4993                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4994                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4995                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4996                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4997                "        > ccccccccccccccccccccccccccccccccccccccccc;",
4998                Style);
4999 
5000   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5001                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5002                "    + cc;",
5003                Style);
5004   verifyFormat("int a = aa\n"
5005                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
5006                "        * cccccccccccccccccccccccccccccccccccc;\n",
5007                Style);
5008 
5009   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5010   verifyFormat("return (a > b\n"
5011                "    // comment1\n"
5012                "    // comment2\n"
5013                "    || c);",
5014                Style);
5015 }
5016 
5017 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
5018   FormatStyle Style = getLLVMStyle();
5019   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5020   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5021                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5022                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
5023                Style);
5024 }
5025 
5026 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
5027   FormatStyle Style = getLLVMStyle();
5028   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
5029   Style.BinPackArguments = false;
5030   Style.ColumnLimit = 40;
5031   verifyFormat("void test() {\n"
5032                "  someFunction(\n"
5033                "      this + argument + is + quite\n"
5034                "      + long + so + it + gets + wrapped\n"
5035                "      + but + remains + bin - packed);\n"
5036                "}",
5037                Style);
5038   verifyFormat("void test() {\n"
5039                "  someFunction(arg1,\n"
5040                "               this + argument + is\n"
5041                "                   + quite + long + so\n"
5042                "                   + it + gets + wrapped\n"
5043                "                   + but + remains + bin\n"
5044                "                   - packed,\n"
5045                "               arg3);\n"
5046                "}",
5047                Style);
5048   verifyFormat("void test() {\n"
5049                "  someFunction(\n"
5050                "      arg1,\n"
5051                "      this + argument + has\n"
5052                "          + anotherFunc(nested,\n"
5053                "                        calls + whose\n"
5054                "                            + arguments\n"
5055                "                            + are + also\n"
5056                "                            + wrapped,\n"
5057                "                        in + addition)\n"
5058                "          + to + being + bin - packed,\n"
5059                "      arg3);\n"
5060                "}",
5061                Style);
5062 
5063   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5064   verifyFormat("void test() {\n"
5065                "  someFunction(\n"
5066                "      arg1,\n"
5067                "      this + argument + has +\n"
5068                "          anotherFunc(nested,\n"
5069                "                      calls + whose +\n"
5070                "                          arguments +\n"
5071                "                          are + also +\n"
5072                "                          wrapped,\n"
5073                "                      in + addition) +\n"
5074                "          to + being + bin - packed,\n"
5075                "      arg3);\n"
5076                "}",
5077                Style);
5078 }
5079 
5080 TEST_F(FormatTest, ConstructorInitializers) {
5081   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5082   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
5083                getLLVMStyleWithColumns(45));
5084   verifyFormat("Constructor()\n"
5085                "    : Inttializer(FitsOnTheLine) {}",
5086                getLLVMStyleWithColumns(44));
5087   verifyFormat("Constructor()\n"
5088                "    : Inttializer(FitsOnTheLine) {}",
5089                getLLVMStyleWithColumns(43));
5090 
5091   verifyFormat("template <typename T>\n"
5092                "Constructor() : Initializer(FitsOnTheLine) {}",
5093                getLLVMStyleWithColumns(45));
5094 
5095   verifyFormat(
5096       "SomeClass::Constructor()\n"
5097       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5098 
5099   verifyFormat(
5100       "SomeClass::Constructor()\n"
5101       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5102       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
5103   verifyFormat(
5104       "SomeClass::Constructor()\n"
5105       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5106       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
5107   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5108                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5109                "    : aaaaaaaaaa(aaaaaa) {}");
5110 
5111   verifyFormat("Constructor()\n"
5112                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5113                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5114                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5115                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
5116 
5117   verifyFormat("Constructor()\n"
5118                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5119                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5120 
5121   verifyFormat("Constructor(int Parameter = 0)\n"
5122                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5123                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
5124   verifyFormat("Constructor()\n"
5125                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5126                "}",
5127                getLLVMStyleWithColumns(60));
5128   verifyFormat("Constructor()\n"
5129                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5130                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
5131 
5132   // Here a line could be saved by splitting the second initializer onto two
5133   // lines, but that is not desirable.
5134   verifyFormat("Constructor()\n"
5135                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5136                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
5137                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5138 
5139   FormatStyle OnePerLine = getLLVMStyle();
5140   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5141   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
5142   verifyFormat("SomeClass::Constructor()\n"
5143                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5144                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5145                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5146                OnePerLine);
5147   verifyFormat("SomeClass::Constructor()\n"
5148                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5149                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5150                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5151                OnePerLine);
5152   verifyFormat("MyClass::MyClass(int var)\n"
5153                "    : some_var_(var),            // 4 space indent\n"
5154                "      some_other_var_(var + 1) { // lined up\n"
5155                "}",
5156                OnePerLine);
5157   verifyFormat("Constructor()\n"
5158                "    : aaaaa(aaaaaa),\n"
5159                "      aaaaa(aaaaaa),\n"
5160                "      aaaaa(aaaaaa),\n"
5161                "      aaaaa(aaaaaa),\n"
5162                "      aaaaa(aaaaaa) {}",
5163                OnePerLine);
5164   verifyFormat("Constructor()\n"
5165                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5166                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
5167                OnePerLine);
5168   OnePerLine.BinPackParameters = false;
5169   verifyFormat(
5170       "Constructor()\n"
5171       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5172       "          aaaaaaaaaaa().aaa(),\n"
5173       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5174       OnePerLine);
5175   OnePerLine.ColumnLimit = 60;
5176   verifyFormat("Constructor()\n"
5177                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5178                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5179                OnePerLine);
5180 
5181   EXPECT_EQ("Constructor()\n"
5182             "    : // Comment forcing unwanted break.\n"
5183             "      aaaa(aaaa) {}",
5184             format("Constructor() :\n"
5185                    "    // Comment forcing unwanted break.\n"
5186                    "    aaaa(aaaa) {}"));
5187 }
5188 
5189 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
5190   FormatStyle Style = getLLVMStyle();
5191   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5192   Style.ColumnLimit = 60;
5193   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5194   Style.AllowAllConstructorInitializersOnNextLine = true;
5195   Style.BinPackParameters = false;
5196 
5197   for (int i = 0; i < 4; ++i) {
5198     // Test all combinations of parameters that should not have an effect.
5199     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
5200     Style.AllowAllArgumentsOnNextLine = i & 2;
5201 
5202     Style.AllowAllConstructorInitializersOnNextLine = true;
5203     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5204     verifyFormat("Constructor()\n"
5205                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5206                  Style);
5207     verifyFormat("Constructor() : a(a), b(b) {}", Style);
5208 
5209     Style.AllowAllConstructorInitializersOnNextLine = false;
5210     verifyFormat("Constructor()\n"
5211                  "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5212                  "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5213                  Style);
5214     verifyFormat("Constructor() : a(a), b(b) {}", Style);
5215 
5216     Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
5217     Style.AllowAllConstructorInitializersOnNextLine = true;
5218     verifyFormat("Constructor()\n"
5219                  "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5220                  Style);
5221 
5222     Style.AllowAllConstructorInitializersOnNextLine = false;
5223     verifyFormat("Constructor()\n"
5224                  "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5225                  "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5226                  Style);
5227 
5228     Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5229     Style.AllowAllConstructorInitializersOnNextLine = true;
5230     verifyFormat("Constructor() :\n"
5231                  "    aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5232                  Style);
5233 
5234     Style.AllowAllConstructorInitializersOnNextLine = false;
5235     verifyFormat("Constructor() :\n"
5236                  "    aaaaaaaaaaaaaaaaaa(a),\n"
5237                  "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5238                  Style);
5239   }
5240 
5241   // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
5242   // AllowAllConstructorInitializersOnNextLine in all
5243   // BreakConstructorInitializers modes
5244   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
5245   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5246   Style.AllowAllConstructorInitializersOnNextLine = false;
5247   verifyFormat("SomeClassWithALongName::Constructor(\n"
5248                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
5249                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5250                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5251                Style);
5252 
5253   Style.AllowAllConstructorInitializersOnNextLine = true;
5254   verifyFormat("SomeClassWithALongName::Constructor(\n"
5255                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5256                "    int bbbbbbbbbbbbb,\n"
5257                "    int cccccccccccccccc)\n"
5258                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5259                Style);
5260 
5261   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5262   Style.AllowAllConstructorInitializersOnNextLine = false;
5263   verifyFormat("SomeClassWithALongName::Constructor(\n"
5264                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5265                "    int bbbbbbbbbbbbb)\n"
5266                "    : aaaaaaaaaaaaaaaaaaaa(a)\n"
5267                "    , bbbbbbbbbbbbbbbbbbbbb(b) {}",
5268                Style);
5269 
5270   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
5271 
5272   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5273   verifyFormat("SomeClassWithALongName::Constructor(\n"
5274                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
5275                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5276                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5277                Style);
5278 
5279   Style.AllowAllConstructorInitializersOnNextLine = true;
5280   verifyFormat("SomeClassWithALongName::Constructor(\n"
5281                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5282                "    int bbbbbbbbbbbbb,\n"
5283                "    int cccccccccccccccc)\n"
5284                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5285                Style);
5286 
5287   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5288   Style.AllowAllConstructorInitializersOnNextLine = false;
5289   verifyFormat("SomeClassWithALongName::Constructor(\n"
5290                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5291                "    int bbbbbbbbbbbbb)\n"
5292                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
5293                "      bbbbbbbbbbbbbbbbbbbbb(b) {}",
5294                Style);
5295 
5296   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5297   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5298   verifyFormat("SomeClassWithALongName::Constructor(\n"
5299                "    int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
5300                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5301                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5302                Style);
5303 
5304   Style.AllowAllConstructorInitializersOnNextLine = true;
5305   verifyFormat("SomeClassWithALongName::Constructor(\n"
5306                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5307                "    int bbbbbbbbbbbbb,\n"
5308                "    int cccccccccccccccc) :\n"
5309                "    aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
5310                Style);
5311 
5312   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5313   Style.AllowAllConstructorInitializersOnNextLine = false;
5314   verifyFormat("SomeClassWithALongName::Constructor(\n"
5315                "    int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5316                "    int bbbbbbbbbbbbb) :\n"
5317                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5318                "    bbbbbbbbbbbbbbbbbbbbb(b) {}",
5319                Style);
5320 }
5321 
5322 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
5323   FormatStyle Style = getLLVMStyle();
5324   Style.ColumnLimit = 60;
5325   Style.BinPackArguments = false;
5326   for (int i = 0; i < 4; ++i) {
5327     // Test all combinations of parameters that should not have an effect.
5328     Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
5329     Style.AllowAllConstructorInitializersOnNextLine = i & 2;
5330 
5331     Style.AllowAllArgumentsOnNextLine = true;
5332     verifyFormat("void foo() {\n"
5333                  "  FunctionCallWithReallyLongName(\n"
5334                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
5335                  "}",
5336                  Style);
5337     Style.AllowAllArgumentsOnNextLine = false;
5338     verifyFormat("void foo() {\n"
5339                  "  FunctionCallWithReallyLongName(\n"
5340                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5341                  "      bbbbbbbbbbbb);\n"
5342                  "}",
5343                  Style);
5344 
5345     Style.AllowAllArgumentsOnNextLine = true;
5346     verifyFormat("void foo() {\n"
5347                  "  auto VariableWithReallyLongName = {\n"
5348                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
5349                  "}",
5350                  Style);
5351     Style.AllowAllArgumentsOnNextLine = false;
5352     verifyFormat("void foo() {\n"
5353                  "  auto VariableWithReallyLongName = {\n"
5354                  "      aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5355                  "      bbbbbbbbbbbb};\n"
5356                  "}",
5357                  Style);
5358   }
5359 
5360   // This parameter should not affect declarations.
5361   Style.BinPackParameters = false;
5362   Style.AllowAllArgumentsOnNextLine = false;
5363   Style.AllowAllParametersOfDeclarationOnNextLine = true;
5364   verifyFormat("void FunctionCallWithReallyLongName(\n"
5365                "    int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
5366                Style);
5367   Style.AllowAllParametersOfDeclarationOnNextLine = false;
5368   verifyFormat("void FunctionCallWithReallyLongName(\n"
5369                "    int aaaaaaaaaaaaaaaaaaaaaaa,\n"
5370                "    int bbbbbbbbbbbb);",
5371                Style);
5372 }
5373 
5374 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
5375   // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
5376   // and BAS_Align.
5377   auto Style = getLLVMStyle();
5378   Style.ColumnLimit = 35;
5379   StringRef Input = "functionCall(paramA, paramB, paramC);\n"
5380                     "void functionDecl(int A, int B, int C);";
5381   Style.AllowAllArgumentsOnNextLine = false;
5382   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5383   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
5384                       "    paramC);\n"
5385                       "void functionDecl(int A, int B,\n"
5386                       "    int C);"),
5387             format(Input, Style));
5388   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5389   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
5390                       "             paramC);\n"
5391                       "void functionDecl(int A, int B,\n"
5392                       "                  int C);"),
5393             format(Input, Style));
5394   // However, BAS_AlwaysBreak should take precedence over
5395   // AllowAllArgumentsOnNextLine.
5396   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5397   EXPECT_EQ(StringRef("functionCall(\n"
5398                       "    paramA, paramB, paramC);\n"
5399                       "void functionDecl(\n"
5400                       "    int A, int B, int C);"),
5401             format(Input, Style));
5402 
5403   // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
5404   // first argument.
5405   Style.AllowAllArgumentsOnNextLine = true;
5406   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5407   EXPECT_EQ(StringRef("functionCall(\n"
5408                       "    paramA, paramB, paramC);\n"
5409                       "void functionDecl(\n"
5410                       "    int A, int B, int C);"),
5411             format(Input, Style));
5412   // It wouldn't fit on one line with aligned parameters so this setting
5413   // doesn't change anything for BAS_Align.
5414   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5415   EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
5416                       "             paramC);\n"
5417                       "void functionDecl(int A, int B,\n"
5418                       "                  int C);"),
5419             format(Input, Style));
5420   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5421   EXPECT_EQ(StringRef("functionCall(\n"
5422                       "    paramA, paramB, paramC);\n"
5423                       "void functionDecl(\n"
5424                       "    int A, int B, int C);"),
5425             format(Input, Style));
5426 }
5427 
5428 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
5429   FormatStyle Style = getLLVMStyle();
5430   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
5431 
5432   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
5433   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
5434                getStyleWithColumns(Style, 45));
5435   verifyFormat("Constructor() :\n"
5436                "    Initializer(FitsOnTheLine) {}",
5437                getStyleWithColumns(Style, 44));
5438   verifyFormat("Constructor() :\n"
5439                "    Initializer(FitsOnTheLine) {}",
5440                getStyleWithColumns(Style, 43));
5441 
5442   verifyFormat("template <typename T>\n"
5443                "Constructor() : Initializer(FitsOnTheLine) {}",
5444                getStyleWithColumns(Style, 50));
5445   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5446   verifyFormat(
5447       "SomeClass::Constructor() :\n"
5448       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
5449       Style);
5450 
5451   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
5452   verifyFormat(
5453       "SomeClass::Constructor() :\n"
5454       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
5455       Style);
5456 
5457   verifyFormat(
5458       "SomeClass::Constructor() :\n"
5459       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5460       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5461       Style);
5462   verifyFormat(
5463       "SomeClass::Constructor() :\n"
5464       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5465       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
5466       Style);
5467   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5468                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
5469                "    aaaaaaaaaa(aaaaaa) {}",
5470                Style);
5471 
5472   verifyFormat("Constructor() :\n"
5473                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5474                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5475                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5476                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
5477                Style);
5478 
5479   verifyFormat("Constructor() :\n"
5480                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5481                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5482                Style);
5483 
5484   verifyFormat("Constructor(int Parameter = 0) :\n"
5485                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
5486                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
5487                Style);
5488   verifyFormat("Constructor() :\n"
5489                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
5490                "}",
5491                getStyleWithColumns(Style, 60));
5492   verifyFormat("Constructor() :\n"
5493                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5494                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
5495                Style);
5496 
5497   // Here a line could be saved by splitting the second initializer onto two
5498   // lines, but that is not desirable.
5499   verifyFormat("Constructor() :\n"
5500                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
5501                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
5502                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5503                Style);
5504 
5505   FormatStyle OnePerLine = Style;
5506   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5507   OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
5508   verifyFormat("SomeClass::Constructor() :\n"
5509                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5510                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5511                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5512                OnePerLine);
5513   verifyFormat("SomeClass::Constructor() :\n"
5514                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
5515                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
5516                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
5517                OnePerLine);
5518   verifyFormat("MyClass::MyClass(int var) :\n"
5519                "    some_var_(var),            // 4 space indent\n"
5520                "    some_other_var_(var + 1) { // lined up\n"
5521                "}",
5522                OnePerLine);
5523   verifyFormat("Constructor() :\n"
5524                "    aaaaa(aaaaaa),\n"
5525                "    aaaaa(aaaaaa),\n"
5526                "    aaaaa(aaaaaa),\n"
5527                "    aaaaa(aaaaaa),\n"
5528                "    aaaaa(aaaaaa) {}",
5529                OnePerLine);
5530   verifyFormat("Constructor() :\n"
5531                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5532                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
5533                OnePerLine);
5534   OnePerLine.BinPackParameters = false;
5535   verifyFormat("Constructor() :\n"
5536                "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5537                "        aaaaaaaaaaa().aaa(),\n"
5538                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5539                OnePerLine);
5540   OnePerLine.ColumnLimit = 60;
5541   verifyFormat("Constructor() :\n"
5542                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
5543                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5544                OnePerLine);
5545 
5546   EXPECT_EQ("Constructor() :\n"
5547             "    // Comment forcing unwanted break.\n"
5548             "    aaaa(aaaa) {}",
5549             format("Constructor() :\n"
5550                    "    // Comment forcing unwanted break.\n"
5551                    "    aaaa(aaaa) {}",
5552                    Style));
5553 
5554   Style.ColumnLimit = 0;
5555   verifyFormat("SomeClass::Constructor() :\n"
5556                "    a(a) {}",
5557                Style);
5558   verifyFormat("SomeClass::Constructor() noexcept :\n"
5559                "    a(a) {}",
5560                Style);
5561   verifyFormat("SomeClass::Constructor() :\n"
5562                "    a(a), b(b), c(c) {}",
5563                Style);
5564   verifyFormat("SomeClass::Constructor() :\n"
5565                "    a(a) {\n"
5566                "  foo();\n"
5567                "  bar();\n"
5568                "}",
5569                Style);
5570 
5571   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5572   verifyFormat("SomeClass::Constructor() :\n"
5573                "    a(a), b(b), c(c) {\n"
5574                "}",
5575                Style);
5576   verifyFormat("SomeClass::Constructor() :\n"
5577                "    a(a) {\n"
5578                "}",
5579                Style);
5580 
5581   Style.ColumnLimit = 80;
5582   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
5583   Style.ConstructorInitializerIndentWidth = 2;
5584   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
5585   verifyFormat("SomeClass::Constructor() :\n"
5586                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5587                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
5588                Style);
5589 
5590   // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
5591   // well
5592   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
5593   verifyFormat(
5594       "class SomeClass\n"
5595       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5596       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5597       Style);
5598   Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
5599   verifyFormat(
5600       "class SomeClass\n"
5601       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5602       "  , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5603       Style);
5604   Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
5605   verifyFormat(
5606       "class SomeClass :\n"
5607       "  public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5608       "  public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5609       Style);
5610   Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
5611   verifyFormat(
5612       "class SomeClass\n"
5613       "  : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5614       "    public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5615       Style);
5616 }
5617 
5618 #ifndef EXPENSIVE_CHECKS
5619 // Expensive checks enables libstdc++ checking which includes validating the
5620 // state of ranges used in std::priority_queue - this blows out the
5621 // runtime/scalability of the function and makes this test unacceptably slow.
5622 TEST_F(FormatTest, MemoizationTests) {
5623   // This breaks if the memoization lookup does not take \c Indent and
5624   // \c LastSpace into account.
5625   verifyFormat(
5626       "extern CFRunLoopTimerRef\n"
5627       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
5628       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
5629       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
5630       "                     CFRunLoopTimerContext *context) {}");
5631 
5632   // Deep nesting somewhat works around our memoization.
5633   verifyFormat(
5634       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5635       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5636       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5637       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5638       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
5639       getLLVMStyleWithColumns(65));
5640   verifyFormat(
5641       "aaaaa(\n"
5642       "    aaaaa,\n"
5643       "    aaaaa(\n"
5644       "        aaaaa,\n"
5645       "        aaaaa(\n"
5646       "            aaaaa,\n"
5647       "            aaaaa(\n"
5648       "                aaaaa,\n"
5649       "                aaaaa(\n"
5650       "                    aaaaa,\n"
5651       "                    aaaaa(\n"
5652       "                        aaaaa,\n"
5653       "                        aaaaa(\n"
5654       "                            aaaaa,\n"
5655       "                            aaaaa(\n"
5656       "                                aaaaa,\n"
5657       "                                aaaaa(\n"
5658       "                                    aaaaa,\n"
5659       "                                    aaaaa(\n"
5660       "                                        aaaaa,\n"
5661       "                                        aaaaa(\n"
5662       "                                            aaaaa,\n"
5663       "                                            aaaaa(\n"
5664       "                                                aaaaa,\n"
5665       "                                                aaaaa))))))))))));",
5666       getLLVMStyleWithColumns(65));
5667   verifyFormat(
5668       "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"
5669       "                                  a),\n"
5670       "                                a),\n"
5671       "                              a),\n"
5672       "                            a),\n"
5673       "                          a),\n"
5674       "                        a),\n"
5675       "                      a),\n"
5676       "                    a),\n"
5677       "                  a),\n"
5678       "                a),\n"
5679       "              a),\n"
5680       "            a),\n"
5681       "          a),\n"
5682       "        a),\n"
5683       "      a),\n"
5684       "    a),\n"
5685       "  a)",
5686       getLLVMStyleWithColumns(65));
5687 
5688   // This test takes VERY long when memoization is broken.
5689   FormatStyle OnePerLine = getLLVMStyle();
5690   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5691   OnePerLine.BinPackParameters = false;
5692   std::string input = "Constructor()\n"
5693                       "    : aaaa(a,\n";
5694   for (unsigned i = 0, e = 80; i != e; ++i) {
5695     input += "           a,\n";
5696   }
5697   input += "           a) {}";
5698   verifyFormat(input, OnePerLine);
5699 }
5700 #endif
5701 
5702 TEST_F(FormatTest, BreaksAsHighAsPossible) {
5703   verifyFormat(
5704       "void f() {\n"
5705       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
5706       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
5707       "    f();\n"
5708       "}");
5709   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
5710                "    Intervals[i - 1].getRange().getLast()) {\n}");
5711 }
5712 
5713 TEST_F(FormatTest, BreaksFunctionDeclarations) {
5714   // Principially, we break function declarations in a certain order:
5715   // 1) break amongst arguments.
5716   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
5717                "                              Cccccccccccccc cccccccccccccc);");
5718   verifyFormat("template <class TemplateIt>\n"
5719                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
5720                "                            TemplateIt *stop) {}");
5721 
5722   // 2) break after return type.
5723   verifyFormat(
5724       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5725       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
5726       getGoogleStyle());
5727 
5728   // 3) break after (.
5729   verifyFormat(
5730       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
5731       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
5732       getGoogleStyle());
5733 
5734   // 4) break before after nested name specifiers.
5735   verifyFormat(
5736       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5737       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
5738       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
5739       getGoogleStyle());
5740 
5741   // However, there are exceptions, if a sufficient amount of lines can be
5742   // saved.
5743   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
5744   // more adjusting.
5745   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
5746                "                                  Cccccccccccccc cccccccccc,\n"
5747                "                                  Cccccccccccccc cccccccccc,\n"
5748                "                                  Cccccccccccccc cccccccccc,\n"
5749                "                                  Cccccccccccccc cccccccccc);");
5750   verifyFormat(
5751       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5752       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5753       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5754       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
5755       getGoogleStyle());
5756   verifyFormat(
5757       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
5758       "                                          Cccccccccccccc cccccccccc,\n"
5759       "                                          Cccccccccccccc cccccccccc,\n"
5760       "                                          Cccccccccccccc cccccccccc,\n"
5761       "                                          Cccccccccccccc cccccccccc,\n"
5762       "                                          Cccccccccccccc cccccccccc,\n"
5763       "                                          Cccccccccccccc cccccccccc);");
5764   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
5765                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5766                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5767                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5768                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
5769 
5770   // Break after multi-line parameters.
5771   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5772                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5773                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5774                "    bbbb bbbb);");
5775   verifyFormat("void SomeLoooooooooooongFunction(\n"
5776                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5777                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5778                "    int bbbbbbbbbbbbb);");
5779 
5780   // Treat overloaded operators like other functions.
5781   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5782                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
5783   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5784                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
5785   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5786                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
5787   verifyGoogleFormat(
5788       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
5789       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
5790   verifyGoogleFormat(
5791       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
5792       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
5793   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5794                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
5795   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
5796                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
5797   verifyGoogleFormat(
5798       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
5799       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5800       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
5801   verifyGoogleFormat("template <typename T>\n"
5802                      "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5803                      "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
5804                      "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
5805 
5806   FormatStyle Style = getLLVMStyle();
5807   Style.PointerAlignment = FormatStyle::PAS_Left;
5808   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5809                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
5810                Style);
5811   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
5812                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5813                Style);
5814 }
5815 
5816 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
5817   // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
5818   // Prefer keeping `::` followed by `operator` together.
5819   EXPECT_EQ("const aaaa::bbbbbbb &\n"
5820             "ccccccccc::operator++() {\n"
5821             "  stuff();\n"
5822             "}",
5823             format("const aaaa::bbbbbbb\n"
5824                    "&ccccccccc::operator++() { stuff(); }",
5825                    getLLVMStyleWithColumns(40)));
5826 }
5827 
5828 TEST_F(FormatTest, TrailingReturnType) {
5829   verifyFormat("auto foo() -> int;\n");
5830   // correct trailing return type spacing
5831   verifyFormat("auto operator->() -> int;\n");
5832   verifyFormat("auto operator++(int) -> int;\n");
5833 
5834   verifyFormat("struct S {\n"
5835                "  auto bar() const -> int;\n"
5836                "};");
5837   verifyFormat("template <size_t Order, typename T>\n"
5838                "auto load_img(const std::string &filename)\n"
5839                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
5840   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
5841                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
5842   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
5843   verifyFormat("template <typename T>\n"
5844                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
5845                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
5846 
5847   // Not trailing return types.
5848   verifyFormat("void f() { auto a = b->c(); }");
5849 }
5850 
5851 TEST_F(FormatTest, DeductionGuides) {
5852   verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
5853   verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
5854   verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
5855   verifyFormat(
5856       "template <class... T>\n"
5857       "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
5858   verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
5859   verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
5860   verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
5861   verifyFormat("template <class T> A() -> A<(3 < 2)>;");
5862   verifyFormat("template <class T> A() -> A<((3) < (2))>;");
5863   verifyFormat("template <class T> x() -> x<1>;");
5864   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
5865 
5866   // Ensure not deduction guides.
5867   verifyFormat("c()->f<int>();");
5868   verifyFormat("x()->foo<1>;");
5869   verifyFormat("x = p->foo<3>();");
5870   verifyFormat("x()->x<1>();");
5871   verifyFormat("x()->x<1>;");
5872 }
5873 
5874 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
5875   // Avoid breaking before trailing 'const' or other trailing annotations, if
5876   // they are not function-like.
5877   FormatStyle Style = getGoogleStyle();
5878   Style.ColumnLimit = 47;
5879   verifyFormat("void someLongFunction(\n"
5880                "    int someLoooooooooooooongParameter) const {\n}",
5881                getLLVMStyleWithColumns(47));
5882   verifyFormat("LoooooongReturnType\n"
5883                "someLoooooooongFunction() const {}",
5884                getLLVMStyleWithColumns(47));
5885   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
5886                "    const {}",
5887                Style);
5888   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5889                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
5890   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5891                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
5892   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5893                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
5894   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
5895                "                   aaaaaaaaaaa aaaaa) const override;");
5896   verifyGoogleFormat(
5897       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5898       "    const override;");
5899 
5900   // Even if the first parameter has to be wrapped.
5901   verifyFormat("void someLongFunction(\n"
5902                "    int someLongParameter) const {}",
5903                getLLVMStyleWithColumns(46));
5904   verifyFormat("void someLongFunction(\n"
5905                "    int someLongParameter) const {}",
5906                Style);
5907   verifyFormat("void someLongFunction(\n"
5908                "    int someLongParameter) override {}",
5909                Style);
5910   verifyFormat("void someLongFunction(\n"
5911                "    int someLongParameter) OVERRIDE {}",
5912                Style);
5913   verifyFormat("void someLongFunction(\n"
5914                "    int someLongParameter) final {}",
5915                Style);
5916   verifyFormat("void someLongFunction(\n"
5917                "    int someLongParameter) FINAL {}",
5918                Style);
5919   verifyFormat("void someLongFunction(\n"
5920                "    int parameter) const override {}",
5921                Style);
5922 
5923   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
5924   verifyFormat("void someLongFunction(\n"
5925                "    int someLongParameter) const\n"
5926                "{\n"
5927                "}",
5928                Style);
5929 
5930   Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
5931   verifyFormat("void someLongFunction(\n"
5932                "    int someLongParameter) const\n"
5933                "  {\n"
5934                "  }",
5935                Style);
5936 
5937   // Unless these are unknown annotations.
5938   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
5939                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5940                "    LONG_AND_UGLY_ANNOTATION;");
5941 
5942   // Breaking before function-like trailing annotations is fine to keep them
5943   // close to their arguments.
5944   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5945                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
5946   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
5947                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
5948   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
5949                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
5950   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
5951                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
5952   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
5953 
5954   verifyFormat(
5955       "void aaaaaaaaaaaaaaaaaa()\n"
5956       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
5957       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
5958   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5959                "    __attribute__((unused));");
5960   verifyGoogleFormat(
5961       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5962       "    GUARDED_BY(aaaaaaaaaaaa);");
5963   verifyGoogleFormat(
5964       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5965       "    GUARDED_BY(aaaaaaaaaaaa);");
5966   verifyGoogleFormat(
5967       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
5968       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5969   verifyGoogleFormat(
5970       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
5971       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
5972 }
5973 
5974 TEST_F(FormatTest, FunctionAnnotations) {
5975   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
5976                "int OldFunction(const string &parameter) {}");
5977   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
5978                "string OldFunction(const string &parameter) {}");
5979   verifyFormat("template <typename T>\n"
5980                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
5981                "string OldFunction(const string &parameter) {}");
5982 
5983   // Not function annotations.
5984   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5985                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
5986   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
5987                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
5988   verifyFormat("MACRO(abc).function() // wrap\n"
5989                "    << abc;");
5990   verifyFormat("MACRO(abc)->function() // wrap\n"
5991                "    << abc;");
5992   verifyFormat("MACRO(abc)::function() // wrap\n"
5993                "    << abc;");
5994 }
5995 
5996 TEST_F(FormatTest, BreaksDesireably) {
5997   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
5998                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
5999                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
6000   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6001                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
6002                "}");
6003 
6004   verifyFormat(
6005       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6006       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
6007 
6008   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6009                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6010                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6011 
6012   verifyFormat(
6013       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6014       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6015       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6016       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6017       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
6018 
6019   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6020                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6021 
6022   verifyFormat(
6023       "void f() {\n"
6024       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6025       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6026       "}");
6027   verifyFormat(
6028       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6029       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6030   verifyFormat(
6031       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6032       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
6033   verifyFormat(
6034       "aaaaaa(aaa,\n"
6035       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6036       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6037       "       aaaa);");
6038   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6039                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6040                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6041 
6042   // Indent consistently independent of call expression and unary operator.
6043   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6044                "    dddddddddddddddddddddddddddddd));");
6045   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
6046                "    dddddddddddddddddddddddddddddd));");
6047   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
6048                "    dddddddddddddddddddddddddddddd));");
6049 
6050   // This test case breaks on an incorrect memoization, i.e. an optimization not
6051   // taking into account the StopAt value.
6052   verifyFormat(
6053       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6054       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6055       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
6056       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6057 
6058   verifyFormat("{\n  {\n    {\n"
6059                "      Annotation.SpaceRequiredBefore =\n"
6060                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
6061                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
6062                "    }\n  }\n}");
6063 
6064   // Break on an outer level if there was a break on an inner level.
6065   EXPECT_EQ("f(g(h(a, // comment\n"
6066             "      b, c),\n"
6067             "    d, e),\n"
6068             "  x, y);",
6069             format("f(g(h(a, // comment\n"
6070                    "    b, c), d, e), x, y);"));
6071 
6072   // Prefer breaking similar line breaks.
6073   verifyFormat(
6074       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
6075       "                             NSTrackingMouseEnteredAndExited |\n"
6076       "                             NSTrackingActiveAlways;");
6077 }
6078 
6079 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
6080   FormatStyle NoBinPacking = getGoogleStyle();
6081   NoBinPacking.BinPackParameters = false;
6082   NoBinPacking.BinPackArguments = true;
6083   verifyFormat("void f() {\n"
6084                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
6085                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
6086                "}",
6087                NoBinPacking);
6088   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
6089                "       int aaaaaaaaaaaaaaaaaaaa,\n"
6090                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6091                NoBinPacking);
6092 
6093   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6094   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6095                "                        vector<int> bbbbbbbbbbbbbbb);",
6096                NoBinPacking);
6097   // FIXME: This behavior difference is probably not wanted. However, currently
6098   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
6099   // template arguments from BreakBeforeParameter being set because of the
6100   // one-per-line formatting.
6101   verifyFormat(
6102       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6103       "                                             aaaaaaaaaa> aaaaaaaaaa);",
6104       NoBinPacking);
6105   verifyFormat(
6106       "void fffffffffff(\n"
6107       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
6108       "        aaaaaaaaaa);");
6109 }
6110 
6111 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
6112   FormatStyle NoBinPacking = getGoogleStyle();
6113   NoBinPacking.BinPackParameters = false;
6114   NoBinPacking.BinPackArguments = false;
6115   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
6116                "  aaaaaaaaaaaaaaaaaaaa,\n"
6117                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
6118                NoBinPacking);
6119   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
6120                "        aaaaaaaaaaaaa,\n"
6121                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
6122                NoBinPacking);
6123   verifyFormat(
6124       "aaaaaaaa(aaaaaaaaaaaaa,\n"
6125       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6126       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
6127       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6128       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
6129       NoBinPacking);
6130   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6131                "    .aaaaaaaaaaaaaaaaaa();",
6132                NoBinPacking);
6133   verifyFormat("void f() {\n"
6134                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6135                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
6136                "}",
6137                NoBinPacking);
6138 
6139   verifyFormat(
6140       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6141       "             aaaaaaaaaaaa,\n"
6142       "             aaaaaaaaaaaa);",
6143       NoBinPacking);
6144   verifyFormat(
6145       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
6146       "                               ddddddddddddddddddddddddddddd),\n"
6147       "             test);",
6148       NoBinPacking);
6149 
6150   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
6151                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
6152                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
6153                "    aaaaaaaaaaaaaaaaaa;",
6154                NoBinPacking);
6155   verifyFormat("a(\"a\"\n"
6156                "  \"a\",\n"
6157                "  a);");
6158 
6159   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
6160   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
6161                "                aaaaaaaaa,\n"
6162                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6163                NoBinPacking);
6164   verifyFormat(
6165       "void f() {\n"
6166       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
6167       "      .aaaaaaa();\n"
6168       "}",
6169       NoBinPacking);
6170   verifyFormat(
6171       "template <class SomeType, class SomeOtherType>\n"
6172       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
6173       NoBinPacking);
6174 }
6175 
6176 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
6177   FormatStyle Style = getLLVMStyleWithColumns(15);
6178   Style.ExperimentalAutoDetectBinPacking = true;
6179   EXPECT_EQ("aaa(aaaa,\n"
6180             "    aaaa,\n"
6181             "    aaaa);\n"
6182             "aaa(aaaa,\n"
6183             "    aaaa,\n"
6184             "    aaaa);",
6185             format("aaa(aaaa,\n" // one-per-line
6186                    "  aaaa,\n"
6187                    "    aaaa  );\n"
6188                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
6189                    Style));
6190   EXPECT_EQ("aaa(aaaa, aaaa,\n"
6191             "    aaaa);\n"
6192             "aaa(aaaa, aaaa,\n"
6193             "    aaaa);",
6194             format("aaa(aaaa,  aaaa,\n" // bin-packed
6195                    "    aaaa  );\n"
6196                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
6197                    Style));
6198 }
6199 
6200 TEST_F(FormatTest, FormatsBuilderPattern) {
6201   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
6202                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
6203                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
6204                "    .StartsWith(\".init\", ORDER_INIT)\n"
6205                "    .StartsWith(\".fini\", ORDER_FINI)\n"
6206                "    .StartsWith(\".hash\", ORDER_HASH)\n"
6207                "    .Default(ORDER_TEXT);\n");
6208 
6209   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
6210                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
6211   verifyFormat("aaaaaaa->aaaaaaa\n"
6212                "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6213                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6214                "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
6215   verifyFormat(
6216       "aaaaaaa->aaaaaaa\n"
6217       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6218       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
6219   verifyFormat(
6220       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
6221       "    aaaaaaaaaaaaaa);");
6222   verifyFormat(
6223       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
6224       "    aaaaaa->aaaaaaaaaaaa()\n"
6225       "        ->aaaaaaaaaaaaaaaa(\n"
6226       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6227       "        ->aaaaaaaaaaaaaaaaa();");
6228   verifyGoogleFormat(
6229       "void f() {\n"
6230       "  someo->Add((new util::filetools::Handler(dir))\n"
6231       "                 ->OnEvent1(NewPermanentCallback(\n"
6232       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
6233       "                 ->OnEvent2(NewPermanentCallback(\n"
6234       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
6235       "                 ->OnEvent3(NewPermanentCallback(\n"
6236       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
6237       "                 ->OnEvent5(NewPermanentCallback(\n"
6238       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
6239       "                 ->OnEvent6(NewPermanentCallback(\n"
6240       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
6241       "}");
6242 
6243   verifyFormat(
6244       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
6245   verifyFormat("aaaaaaaaaaaaaaa()\n"
6246                "    .aaaaaaaaaaaaaaa()\n"
6247                "    .aaaaaaaaaaaaaaa()\n"
6248                "    .aaaaaaaaaaaaaaa()\n"
6249                "    .aaaaaaaaaaaaaaa();");
6250   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6251                "    .aaaaaaaaaaaaaaa()\n"
6252                "    .aaaaaaaaaaaaaaa()\n"
6253                "    .aaaaaaaaaaaaaaa();");
6254   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6255                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
6256                "    .aaaaaaaaaaaaaaa();");
6257   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
6258                "    ->aaaaaaaaaaaaaae(0)\n"
6259                "    ->aaaaaaaaaaaaaaa();");
6260 
6261   // Don't linewrap after very short segments.
6262   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6263                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6264                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6265   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6266                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6267                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6268   verifyFormat("aaa()\n"
6269                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6270                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6271                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
6272 
6273   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
6274                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6275                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
6276   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
6277                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6278                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
6279 
6280   // Prefer not to break after empty parentheses.
6281   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
6282                "    First->LastNewlineOffset);");
6283 
6284   // Prefer not to create "hanging" indents.
6285   verifyFormat(
6286       "return !soooooooooooooome_map\n"
6287       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6288       "            .second;");
6289   verifyFormat(
6290       "return aaaaaaaaaaaaaaaa\n"
6291       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
6292       "    .aaaa(aaaaaaaaaaaaaa);");
6293   // No hanging indent here.
6294   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
6295                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6296   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
6297                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6298   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
6299                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6300                getLLVMStyleWithColumns(60));
6301   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
6302                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
6303                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6304                getLLVMStyleWithColumns(59));
6305   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6306                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6307                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6308 
6309   // Dont break if only closing statements before member call
6310   verifyFormat("test() {\n"
6311                "  ([]() -> {\n"
6312                "    int b = 32;\n"
6313                "    return 3;\n"
6314                "  }).foo();\n"
6315                "}");
6316   verifyFormat("test() {\n"
6317                "  (\n"
6318                "      []() -> {\n"
6319                "        int b = 32;\n"
6320                "        return 3;\n"
6321                "      },\n"
6322                "      foo, bar)\n"
6323                "      .foo();\n"
6324                "}");
6325   verifyFormat("test() {\n"
6326                "  ([]() -> {\n"
6327                "    int b = 32;\n"
6328                "    return 3;\n"
6329                "  })\n"
6330                "      .foo()\n"
6331                "      .bar();\n"
6332                "}");
6333   verifyFormat("test() {\n"
6334                "  ([]() -> {\n"
6335                "    int b = 32;\n"
6336                "    return 3;\n"
6337                "  })\n"
6338                "      .foo(\"aaaaaaaaaaaaaaaaa\"\n"
6339                "           \"bbbb\");\n"
6340                "}",
6341                getLLVMStyleWithColumns(30));
6342 }
6343 
6344 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
6345   verifyFormat(
6346       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6347       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
6348   verifyFormat(
6349       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
6350       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
6351 
6352   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
6353                "    ccccccccccccccccccccccccc) {\n}");
6354   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
6355                "    ccccccccccccccccccccccccc) {\n}");
6356 
6357   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
6358                "    ccccccccccccccccccccccccc) {\n}");
6359   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
6360                "    ccccccccccccccccccccccccc) {\n}");
6361 
6362   verifyFormat(
6363       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
6364       "    ccccccccccccccccccccccccc) {\n}");
6365   verifyFormat(
6366       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
6367       "    ccccccccccccccccccccccccc) {\n}");
6368 
6369   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
6370                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
6371                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
6372                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
6373   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
6374                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
6375                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
6376                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
6377 
6378   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
6379                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
6380                "    aaaaaaaaaaaaaaa != aa) {\n}");
6381   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
6382                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
6383                "    aaaaaaaaaaaaaaa != aa) {\n}");
6384 }
6385 
6386 TEST_F(FormatTest, BreaksAfterAssignments) {
6387   verifyFormat(
6388       "unsigned Cost =\n"
6389       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
6390       "                        SI->getPointerAddressSpaceee());\n");
6391   verifyFormat(
6392       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
6393       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
6394 
6395   verifyFormat(
6396       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
6397       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
6398   verifyFormat("unsigned OriginalStartColumn =\n"
6399                "    SourceMgr.getSpellingColumnNumber(\n"
6400                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
6401                "    1;");
6402 }
6403 
6404 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
6405   FormatStyle Style = getLLVMStyle();
6406   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6407                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
6408                Style);
6409 
6410   Style.PenaltyBreakAssignment = 20;
6411   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
6412                "                                 cccccccccccccccccccccccccc;",
6413                Style);
6414 }
6415 
6416 TEST_F(FormatTest, AlignsAfterAssignments) {
6417   verifyFormat(
6418       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6419       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
6420   verifyFormat(
6421       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6422       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
6423   verifyFormat(
6424       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6425       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
6426   verifyFormat(
6427       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6428       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
6429   verifyFormat(
6430       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
6431       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
6432       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
6433 }
6434 
6435 TEST_F(FormatTest, AlignsAfterReturn) {
6436   verifyFormat(
6437       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6438       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
6439   verifyFormat(
6440       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6441       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
6442   verifyFormat(
6443       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
6444       "       aaaaaaaaaaaaaaaaaaaaaa();");
6445   verifyFormat(
6446       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
6447       "        aaaaaaaaaaaaaaaaaaaaaa());");
6448   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6449                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6450   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6451                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
6452                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6453   verifyFormat("return\n"
6454                "    // true if code is one of a or b.\n"
6455                "    code == a || code == b;");
6456 }
6457 
6458 TEST_F(FormatTest, AlignsAfterOpenBracket) {
6459   verifyFormat(
6460       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
6461       "                                                aaaaaaaaa aaaaaaa) {}");
6462   verifyFormat(
6463       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
6464       "                                               aaaaaaaaaaa aaaaaaaaa);");
6465   verifyFormat(
6466       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
6467       "                                             aaaaaaaaaaaaaaaaaaaaa));");
6468   FormatStyle Style = getLLVMStyle();
6469   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6470   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6471                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
6472                Style);
6473   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
6474                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
6475                Style);
6476   verifyFormat("SomeLongVariableName->someFunction(\n"
6477                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
6478                Style);
6479   verifyFormat(
6480       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
6481       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6482       Style);
6483   verifyFormat(
6484       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
6485       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6486       Style);
6487   verifyFormat(
6488       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
6489       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
6490       Style);
6491 
6492   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
6493                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
6494                "        b));",
6495                Style);
6496 
6497   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6498   Style.BinPackArguments = false;
6499   Style.BinPackParameters = false;
6500   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6501                "    aaaaaaaaaaa aaaaaaaa,\n"
6502                "    aaaaaaaaa aaaaaaa,\n"
6503                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
6504                Style);
6505   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
6506                "    aaaaaaaaaaa aaaaaaaaa,\n"
6507                "    aaaaaaaaaaa aaaaaaaaa,\n"
6508                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6509                Style);
6510   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
6511                "    aaaaaaaaaaaaaaa,\n"
6512                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6513                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
6514                Style);
6515   verifyFormat(
6516       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
6517       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
6518       Style);
6519   verifyFormat(
6520       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
6521       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
6522       Style);
6523   verifyFormat(
6524       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6525       "    aaaaaaaaaaaaaaaaaaaaa(\n"
6526       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
6527       "    aaaaaaaaaaaaaaaa);",
6528       Style);
6529   verifyFormat(
6530       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6531       "    aaaaaaaaaaaaaaaaaaaaa(\n"
6532       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
6533       "    aaaaaaaaaaaaaaaa);",
6534       Style);
6535 }
6536 
6537 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
6538   FormatStyle Style = getLLVMStyleWithColumns(40);
6539   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6540                "          bbbbbbbbbbbbbbbbbbbbbb);",
6541                Style);
6542   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6543   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6544   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6545                "          bbbbbbbbbbbbbbbbbbbbbb);",
6546                Style);
6547   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6548   Style.AlignOperands = FormatStyle::OAS_Align;
6549   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6550                "          bbbbbbbbbbbbbbbbbbbbbb);",
6551                Style);
6552   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6553   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6554   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6555                "    bbbbbbbbbbbbbbbbbbbbbb);",
6556                Style);
6557 }
6558 
6559 TEST_F(FormatTest, BreaksConditionalExpressions) {
6560   verifyFormat(
6561       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6562       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6563       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6564   verifyFormat(
6565       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
6566       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6567       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6568   verifyFormat(
6569       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6570       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6571   verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
6572                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6573                "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6574   verifyFormat(
6575       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
6576       "                                                    : aaaaaaaaaaaaa);");
6577   verifyFormat(
6578       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6579       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6580       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6581       "                   aaaaaaaaaaaaa);");
6582   verifyFormat(
6583       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6584       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6585       "                   aaaaaaaaaaaaa);");
6586   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6587                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6588                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6589                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6590                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6591   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6592                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6593                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6594                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6595                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6596                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6597                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6598   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6599                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6600                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6601                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6602                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6603   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6604                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6605                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6606   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
6607                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6608                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6609                "        : aaaaaaaaaaaaaaaa;");
6610   verifyFormat(
6611       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6612       "    ? aaaaaaaaaaaaaaa\n"
6613       "    : aaaaaaaaaaaaaaa;");
6614   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
6615                "          aaaaaaaaa\n"
6616                "      ? b\n"
6617                "      : c);");
6618   verifyFormat("return aaaa == bbbb\n"
6619                "           // comment\n"
6620                "           ? aaaa\n"
6621                "           : bbbb;");
6622   verifyFormat("unsigned Indent =\n"
6623                "    format(TheLine.First,\n"
6624                "           IndentForLevel[TheLine.Level] >= 0\n"
6625                "               ? IndentForLevel[TheLine.Level]\n"
6626                "               : TheLine * 2,\n"
6627                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
6628                getLLVMStyleWithColumns(60));
6629   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
6630                "                  ? aaaaaaaaaaaaaaa\n"
6631                "                  : bbbbbbbbbbbbbbb //\n"
6632                "                        ? ccccccccccccccc\n"
6633                "                        : ddddddddddddddd;");
6634   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
6635                "                  ? aaaaaaaaaaaaaaa\n"
6636                "                  : (bbbbbbbbbbbbbbb //\n"
6637                "                         ? ccccccccccccccc\n"
6638                "                         : ddddddddddddddd);");
6639   verifyFormat(
6640       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6641       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6642       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
6643       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
6644       "                                      : aaaaaaaaaa;");
6645   verifyFormat(
6646       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6647       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
6648       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6649 
6650   FormatStyle NoBinPacking = getLLVMStyle();
6651   NoBinPacking.BinPackArguments = false;
6652   verifyFormat(
6653       "void f() {\n"
6654       "  g(aaa,\n"
6655       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
6656       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6657       "        ? aaaaaaaaaaaaaaa\n"
6658       "        : aaaaaaaaaaaaaaa);\n"
6659       "}",
6660       NoBinPacking);
6661   verifyFormat(
6662       "void f() {\n"
6663       "  g(aaa,\n"
6664       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
6665       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6666       "        ?: aaaaaaaaaaaaaaa);\n"
6667       "}",
6668       NoBinPacking);
6669 
6670   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
6671                "             // comment.\n"
6672                "             ccccccccccccccccccccccccccccccccccccccc\n"
6673                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6674                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
6675 
6676   // Assignments in conditional expressions. Apparently not uncommon :-(.
6677   verifyFormat("return a != b\n"
6678                "           // comment\n"
6679                "           ? a = b\n"
6680                "           : a = b;");
6681   verifyFormat("return a != b\n"
6682                "           // comment\n"
6683                "           ? a = a != b\n"
6684                "                     // comment\n"
6685                "                     ? a = b\n"
6686                "                     : a\n"
6687                "           : a;\n");
6688   verifyFormat("return a != b\n"
6689                "           // comment\n"
6690                "           ? a\n"
6691                "           : a = a != b\n"
6692                "                     // comment\n"
6693                "                     ? a = b\n"
6694                "                     : a;");
6695 
6696   // Chained conditionals
6697   FormatStyle Style = getLLVMStyle();
6698   Style.ColumnLimit = 70;
6699   Style.AlignOperands = FormatStyle::OAS_Align;
6700   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6701                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6702                "                        : 3333333333333333;",
6703                Style);
6704   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6705                "       : bbbbbbbbbb     ? 2222222222222222\n"
6706                "                        : 3333333333333333;",
6707                Style);
6708   verifyFormat("return aaaaaaaaaa         ? 1111111111111111\n"
6709                "       : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6710                "                          : 3333333333333333;",
6711                Style);
6712   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6713                "       : bbbbbbbbbbbbbb ? 222222\n"
6714                "                        : 333333;",
6715                Style);
6716   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6717                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6718                "       : cccccccccccccc ? 3333333333333333\n"
6719                "                        : 4444444444444444;",
6720                Style);
6721   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
6722                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6723                "                        : 3333333333333333;",
6724                Style);
6725   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6726                "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6727                "                        : (aaa ? bbb : ccc);",
6728                Style);
6729   verifyFormat(
6730       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6731       "                                             : cccccccccccccccccc)\n"
6732       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6733       "                        : 3333333333333333;",
6734       Style);
6735   verifyFormat(
6736       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6737       "                                             : cccccccccccccccccc)\n"
6738       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6739       "                        : 3333333333333333;",
6740       Style);
6741   verifyFormat(
6742       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6743       "                                             : dddddddddddddddddd)\n"
6744       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6745       "                        : 3333333333333333;",
6746       Style);
6747   verifyFormat(
6748       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6749       "                                             : dddddddddddddddddd)\n"
6750       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6751       "                        : 3333333333333333;",
6752       Style);
6753   verifyFormat(
6754       "return aaaaaaaaa        ? 1111111111111111\n"
6755       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6756       "                        : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6757       "                                             : dddddddddddddddddd)\n",
6758       Style);
6759   verifyFormat(
6760       "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6761       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6762       "                        : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6763       "                                             : cccccccccccccccccc);",
6764       Style);
6765   verifyFormat(
6766       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6767       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6768       "                                             : eeeeeeeeeeeeeeeeee)\n"
6769       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6770       "                        : 3333333333333333;",
6771       Style);
6772   verifyFormat(
6773       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa    ? bbbbbbbbbbbbbbbbbb\n"
6774       "                           : ccccccccccccccc ? dddddddddddddddddd\n"
6775       "                                             : eeeeeeeeeeeeeeeeee)\n"
6776       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6777       "                        : 3333333333333333;",
6778       Style);
6779   verifyFormat(
6780       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6781       "                           : cccccccccccc    ? dddddddddddddddddd\n"
6782       "                                             : eeeeeeeeeeeeeeeeee)\n"
6783       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6784       "                        : 3333333333333333;",
6785       Style);
6786   verifyFormat(
6787       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6788       "                                             : cccccccccccccccccc\n"
6789       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6790       "                        : 3333333333333333;",
6791       Style);
6792   verifyFormat(
6793       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6794       "                          : cccccccccccccccc ? dddddddddddddddddd\n"
6795       "                                             : eeeeeeeeeeeeeeeeee\n"
6796       "       : bbbbbbbbbbbbbb ? 2222222222222222\n"
6797       "                        : 3333333333333333;",
6798       Style);
6799   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
6800                "           ? (aaaaaaaaaaaaaaaaaa   ? bbbbbbbbbbbbbbbbbb\n"
6801                "              : cccccccccccccccccc ? dddddddddddddddddd\n"
6802                "                                   : eeeeeeeeeeeeeeeeee)\n"
6803                "       : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
6804                "                             : 3333333333333333;",
6805                Style);
6806   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
6807                "           ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6808                "             : cccccccccccccccc ? dddddddddddddddddd\n"
6809                "                                : eeeeeeeeeeeeeeeeee\n"
6810                "       : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
6811                "                                 : 3333333333333333;",
6812                Style);
6813 
6814   Style.AlignOperands = FormatStyle::OAS_DontAlign;
6815   Style.BreakBeforeTernaryOperators = false;
6816   // FIXME: Aligning the question marks is weird given DontAlign.
6817   // Consider disabling this alignment in this case. Also check whether this
6818   // will render the adjustment from https://reviews.llvm.org/D82199
6819   // unnecessary.
6820   verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
6821                "    bbbb                ? cccccccccccccccccc :\n"
6822                "                          ddddd;\n",
6823                Style);
6824 
6825   EXPECT_EQ(
6826       "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
6827       "    /*\n"
6828       "     */\n"
6829       "    function() {\n"
6830       "      try {\n"
6831       "        return JJJJJJJJJJJJJJ(\n"
6832       "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
6833       "      }\n"
6834       "    } :\n"
6835       "    function() {};",
6836       format(
6837           "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
6838           "     /*\n"
6839           "      */\n"
6840           "     function() {\n"
6841           "      try {\n"
6842           "        return JJJJJJJJJJJJJJ(\n"
6843           "            pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
6844           "      }\n"
6845           "    } :\n"
6846           "    function() {};",
6847           getGoogleStyle(FormatStyle::LK_JavaScript)));
6848 }
6849 
6850 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
6851   FormatStyle Style = getLLVMStyle();
6852   Style.BreakBeforeTernaryOperators = false;
6853   Style.ColumnLimit = 70;
6854   verifyFormat(
6855       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6856       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6857       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6858       Style);
6859   verifyFormat(
6860       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
6861       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6862       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6863       Style);
6864   verifyFormat(
6865       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6866       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6867       Style);
6868   verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
6869                "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6870                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6871                Style);
6872   verifyFormat(
6873       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
6874       "                                                      aaaaaaaaaaaaa);",
6875       Style);
6876   verifyFormat(
6877       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6878       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6879       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6880       "                   aaaaaaaaaaaaa);",
6881       Style);
6882   verifyFormat(
6883       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6884       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6885       "                   aaaaaaaaaaaaa);",
6886       Style);
6887   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6888                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6889                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6890                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6891                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6892                Style);
6893   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6894                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6895                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6896                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6897                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6898                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6899                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6900                Style);
6901   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6902                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
6903                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6904                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6905                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6906                Style);
6907   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6908                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6909                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6910                Style);
6911   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
6912                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6913                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6914                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6915                Style);
6916   verifyFormat(
6917       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6918       "    aaaaaaaaaaaaaaa :\n"
6919       "    aaaaaaaaaaaaaaa;",
6920       Style);
6921   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
6922                "          aaaaaaaaa ?\n"
6923                "      b :\n"
6924                "      c);",
6925                Style);
6926   verifyFormat("unsigned Indent =\n"
6927                "    format(TheLine.First,\n"
6928                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
6929                "               IndentForLevel[TheLine.Level] :\n"
6930                "               TheLine * 2,\n"
6931                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
6932                Style);
6933   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
6934                "                  aaaaaaaaaaaaaaa :\n"
6935                "                  bbbbbbbbbbbbbbb ? //\n"
6936                "                      ccccccccccccccc :\n"
6937                "                      ddddddddddddddd;",
6938                Style);
6939   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
6940                "                  aaaaaaaaaaaaaaa :\n"
6941                "                  (bbbbbbbbbbbbbbb ? //\n"
6942                "                       ccccccccccccccc :\n"
6943                "                       ddddddddddddddd);",
6944                Style);
6945   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6946                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
6947                "            ccccccccccccccccccccccccccc;",
6948                Style);
6949   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6950                "           aaaaa :\n"
6951                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
6952                Style);
6953 
6954   // Chained conditionals
6955   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6956                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6957                "                          3333333333333333;",
6958                Style);
6959   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6960                "       bbbbbbbbbb       ? 2222222222222222 :\n"
6961                "                          3333333333333333;",
6962                Style);
6963   verifyFormat("return aaaaaaaaaa       ? 1111111111111111 :\n"
6964                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6965                "                          3333333333333333;",
6966                Style);
6967   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6968                "       bbbbbbbbbbbbbbbb ? 222222 :\n"
6969                "                          333333;",
6970                Style);
6971   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6972                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6973                "       cccccccccccccccc ? 3333333333333333 :\n"
6974                "                          4444444444444444;",
6975                Style);
6976   verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
6977                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6978                "                          3333333333333333;",
6979                Style);
6980   verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6981                "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6982                "                          (aaa ? bbb : ccc);",
6983                Style);
6984   verifyFormat(
6985       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6986       "                                               cccccccccccccccccc) :\n"
6987       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6988       "                          3333333333333333;",
6989       Style);
6990   verifyFormat(
6991       "return aaaaaaaaa        ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6992       "                                               cccccccccccccccccc) :\n"
6993       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6994       "                          3333333333333333;",
6995       Style);
6996   verifyFormat(
6997       "return aaaaaaaaa        ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6998       "                                               dddddddddddddddddd) :\n"
6999       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7000       "                          3333333333333333;",
7001       Style);
7002   verifyFormat(
7003       "return aaaaaaaaa        ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7004       "                                               dddddddddddddddddd) :\n"
7005       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7006       "                          3333333333333333;",
7007       Style);
7008   verifyFormat(
7009       "return aaaaaaaaa        ? 1111111111111111 :\n"
7010       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7011       "                          a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7012       "                                               dddddddddddddddddd)\n",
7013       Style);
7014   verifyFormat(
7015       "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
7016       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7017       "                          (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7018       "                                               cccccccccccccccccc);",
7019       Style);
7020   verifyFormat(
7021       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7022       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7023       "                                               eeeeeeeeeeeeeeeeee) :\n"
7024       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7025       "                          3333333333333333;",
7026       Style);
7027   verifyFormat(
7028       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7029       "                           ccccccccccccc     ? dddddddddddddddddd :\n"
7030       "                                               eeeeeeeeeeeeeeeeee) :\n"
7031       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7032       "                          3333333333333333;",
7033       Style);
7034   verifyFormat(
7035       "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa     ? bbbbbbbbbbbbbbbbbb :\n"
7036       "                           ccccccccccccccccc ? dddddddddddddddddd :\n"
7037       "                                               eeeeeeeeeeeeeeeeee) :\n"
7038       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7039       "                          3333333333333333;",
7040       Style);
7041   verifyFormat(
7042       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7043       "                                               cccccccccccccccccc :\n"
7044       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7045       "                          3333333333333333;",
7046       Style);
7047   verifyFormat(
7048       "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7049       "                          cccccccccccccccccc ? dddddddddddddddddd :\n"
7050       "                                               eeeeeeeeeeeeeeeeee :\n"
7051       "       bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7052       "                          3333333333333333;",
7053       Style);
7054   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7055                "           (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7056                "            cccccccccccccccccc ? dddddddddddddddddd :\n"
7057                "                                 eeeeeeeeeeeeeeeeee) :\n"
7058                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7059                "                               3333333333333333;",
7060                Style);
7061   verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
7062                "           aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
7063                "           cccccccccccccccccccc ? dddddddddddddddddd :\n"
7064                "                                  eeeeeeeeeeeeeeeeee :\n"
7065                "       bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
7066                "                               3333333333333333;",
7067                Style);
7068 }
7069 
7070 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
7071   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
7072                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
7073   verifyFormat("bool a = true, b = false;");
7074 
7075   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7076                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
7077                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
7078                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
7079   verifyFormat(
7080       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7081       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
7082       "     d = e && f;");
7083   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
7084                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
7085   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7086                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
7087   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
7088                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
7089 
7090   FormatStyle Style = getGoogleStyle();
7091   Style.PointerAlignment = FormatStyle::PAS_Left;
7092   Style.DerivePointerAlignment = false;
7093   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7094                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
7095                "    *b = bbbbbbbbbbbbbbbbbbb;",
7096                Style);
7097   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
7098                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
7099                Style);
7100   verifyFormat("vector<int*> a, b;", Style);
7101   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
7102 }
7103 
7104 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
7105   verifyFormat("arr[foo ? bar : baz];");
7106   verifyFormat("f()[foo ? bar : baz];");
7107   verifyFormat("(a + b)[foo ? bar : baz];");
7108   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
7109 }
7110 
7111 TEST_F(FormatTest, AlignsStringLiterals) {
7112   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
7113                "                                      \"short literal\");");
7114   verifyFormat(
7115       "looooooooooooooooooooooooongFunction(\n"
7116       "    \"short literal\"\n"
7117       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
7118   verifyFormat("someFunction(\"Always break between multi-line\"\n"
7119                "             \" string literals\",\n"
7120                "             and, other, parameters);");
7121   EXPECT_EQ("fun + \"1243\" /* comment */\n"
7122             "      \"5678\";",
7123             format("fun + \"1243\" /* comment */\n"
7124                    "    \"5678\";",
7125                    getLLVMStyleWithColumns(28)));
7126   EXPECT_EQ(
7127       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7128       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
7129       "         \"aaaaaaaaaaaaaaaa\";",
7130       format("aaaaaa ="
7131              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7132              "aaaaaaaaaaaaaaaaaaaaa\" "
7133              "\"aaaaaaaaaaaaaaaa\";"));
7134   verifyFormat("a = a + \"a\"\n"
7135                "        \"a\"\n"
7136                "        \"a\";");
7137   verifyFormat("f(\"a\", \"b\"\n"
7138                "       \"c\");");
7139 
7140   verifyFormat(
7141       "#define LL_FORMAT \"ll\"\n"
7142       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
7143       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
7144 
7145   verifyFormat("#define A(X)          \\\n"
7146                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
7147                "  \"ccccc\"",
7148                getLLVMStyleWithColumns(23));
7149   verifyFormat("#define A \"def\"\n"
7150                "f(\"abc\" A \"ghi\"\n"
7151                "  \"jkl\");");
7152 
7153   verifyFormat("f(L\"a\"\n"
7154                "  L\"b\");");
7155   verifyFormat("#define A(X)            \\\n"
7156                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
7157                "  L\"ccccc\"",
7158                getLLVMStyleWithColumns(25));
7159 
7160   verifyFormat("f(@\"a\"\n"
7161                "  @\"b\");");
7162   verifyFormat("NSString s = @\"a\"\n"
7163                "             @\"b\"\n"
7164                "             @\"c\";");
7165   verifyFormat("NSString s = @\"a\"\n"
7166                "              \"b\"\n"
7167                "              \"c\";");
7168 }
7169 
7170 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
7171   FormatStyle Style = getLLVMStyle();
7172   // No declarations or definitions should be moved to own line.
7173   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
7174   verifyFormat("class A {\n"
7175                "  int f() { return 1; }\n"
7176                "  int g();\n"
7177                "};\n"
7178                "int f() { return 1; }\n"
7179                "int g();\n",
7180                Style);
7181 
7182   // All declarations and definitions should have the return type moved to its
7183   // own line.
7184   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
7185   Style.TypenameMacros = {"LIST"};
7186   verifyFormat("SomeType\n"
7187                "funcdecl(LIST(uint64_t));",
7188                Style);
7189   verifyFormat("class E {\n"
7190                "  int\n"
7191                "  f() {\n"
7192                "    return 1;\n"
7193                "  }\n"
7194                "  int\n"
7195                "  g();\n"
7196                "};\n"
7197                "int\n"
7198                "f() {\n"
7199                "  return 1;\n"
7200                "}\n"
7201                "int\n"
7202                "g();\n",
7203                Style);
7204 
7205   // Top-level definitions, and no kinds of declarations should have the
7206   // return type moved to its own line.
7207   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
7208   verifyFormat("class B {\n"
7209                "  int f() { return 1; }\n"
7210                "  int g();\n"
7211                "};\n"
7212                "int\n"
7213                "f() {\n"
7214                "  return 1;\n"
7215                "}\n"
7216                "int g();\n",
7217                Style);
7218 
7219   // Top-level definitions and declarations should have the return type moved
7220   // to its own line.
7221   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
7222   verifyFormat("class C {\n"
7223                "  int f() { return 1; }\n"
7224                "  int g();\n"
7225                "};\n"
7226                "int\n"
7227                "f() {\n"
7228                "  return 1;\n"
7229                "}\n"
7230                "int\n"
7231                "g();\n",
7232                Style);
7233 
7234   // All definitions should have the return type moved to its own line, but no
7235   // kinds of declarations.
7236   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
7237   verifyFormat("class D {\n"
7238                "  int\n"
7239                "  f() {\n"
7240                "    return 1;\n"
7241                "  }\n"
7242                "  int g();\n"
7243                "};\n"
7244                "int\n"
7245                "f() {\n"
7246                "  return 1;\n"
7247                "}\n"
7248                "int g();\n",
7249                Style);
7250   verifyFormat("const char *\n"
7251                "f(void) {\n" // Break here.
7252                "  return \"\";\n"
7253                "}\n"
7254                "const char *bar(void);\n", // No break here.
7255                Style);
7256   verifyFormat("template <class T>\n"
7257                "T *\n"
7258                "f(T &c) {\n" // Break here.
7259                "  return NULL;\n"
7260                "}\n"
7261                "template <class T> T *f(T &c);\n", // No break here.
7262                Style);
7263   verifyFormat("class C {\n"
7264                "  int\n"
7265                "  operator+() {\n"
7266                "    return 1;\n"
7267                "  }\n"
7268                "  int\n"
7269                "  operator()() {\n"
7270                "    return 1;\n"
7271                "  }\n"
7272                "};\n",
7273                Style);
7274   verifyFormat("void\n"
7275                "A::operator()() {}\n"
7276                "void\n"
7277                "A::operator>>() {}\n"
7278                "void\n"
7279                "A::operator+() {}\n"
7280                "void\n"
7281                "A::operator*() {}\n"
7282                "void\n"
7283                "A::operator->() {}\n"
7284                "void\n"
7285                "A::operator void *() {}\n"
7286                "void\n"
7287                "A::operator void &() {}\n"
7288                "void\n"
7289                "A::operator void &&() {}\n"
7290                "void\n"
7291                "A::operator char *() {}\n"
7292                "void\n"
7293                "A::operator[]() {}\n"
7294                "void\n"
7295                "A::operator!() {}\n"
7296                "void\n"
7297                "A::operator**() {}\n"
7298                "void\n"
7299                "A::operator<Foo> *() {}\n"
7300                "void\n"
7301                "A::operator<Foo> **() {}\n"
7302                "void\n"
7303                "A::operator<Foo> &() {}\n"
7304                "void\n"
7305                "A::operator void **() {}\n",
7306                Style);
7307   verifyFormat("constexpr auto\n"
7308                "operator()() const -> reference {}\n"
7309                "constexpr auto\n"
7310                "operator>>() const -> reference {}\n"
7311                "constexpr auto\n"
7312                "operator+() const -> reference {}\n"
7313                "constexpr auto\n"
7314                "operator*() const -> reference {}\n"
7315                "constexpr auto\n"
7316                "operator->() const -> reference {}\n"
7317                "constexpr auto\n"
7318                "operator++() const -> reference {}\n"
7319                "constexpr auto\n"
7320                "operator void *() const -> reference {}\n"
7321                "constexpr auto\n"
7322                "operator void **() const -> reference {}\n"
7323                "constexpr auto\n"
7324                "operator void *() const -> reference {}\n"
7325                "constexpr auto\n"
7326                "operator void &() const -> reference {}\n"
7327                "constexpr auto\n"
7328                "operator void &&() const -> reference {}\n"
7329                "constexpr auto\n"
7330                "operator char *() const -> reference {}\n"
7331                "constexpr auto\n"
7332                "operator!() const -> reference {}\n"
7333                "constexpr auto\n"
7334                "operator[]() const -> reference {}\n",
7335                Style);
7336   verifyFormat("void *operator new(std::size_t s);", // No break here.
7337                Style);
7338   verifyFormat("void *\n"
7339                "operator new(std::size_t s) {}",
7340                Style);
7341   verifyFormat("void *\n"
7342                "operator delete[](void *ptr) {}",
7343                Style);
7344   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
7345   verifyFormat("const char *\n"
7346                "f(void)\n" // Break here.
7347                "{\n"
7348                "  return \"\";\n"
7349                "}\n"
7350                "const char *bar(void);\n", // No break here.
7351                Style);
7352   verifyFormat("template <class T>\n"
7353                "T *\n"     // Problem here: no line break
7354                "f(T &c)\n" // Break here.
7355                "{\n"
7356                "  return NULL;\n"
7357                "}\n"
7358                "template <class T> T *f(T &c);\n", // No break here.
7359                Style);
7360   verifyFormat("int\n"
7361                "foo(A<bool> a)\n"
7362                "{\n"
7363                "  return a;\n"
7364                "}\n",
7365                Style);
7366   verifyFormat("int\n"
7367                "foo(A<8> a)\n"
7368                "{\n"
7369                "  return a;\n"
7370                "}\n",
7371                Style);
7372   verifyFormat("int\n"
7373                "foo(A<B<bool>, 8> a)\n"
7374                "{\n"
7375                "  return a;\n"
7376                "}\n",
7377                Style);
7378   verifyFormat("int\n"
7379                "foo(A<B<8>, bool> a)\n"
7380                "{\n"
7381                "  return a;\n"
7382                "}\n",
7383                Style);
7384   verifyFormat("int\n"
7385                "foo(A<B<bool>, bool> a)\n"
7386                "{\n"
7387                "  return a;\n"
7388                "}\n",
7389                Style);
7390   verifyFormat("int\n"
7391                "foo(A<B<8>, 8> a)\n"
7392                "{\n"
7393                "  return a;\n"
7394                "}\n",
7395                Style);
7396 
7397   Style = getGNUStyle();
7398 
7399   // Test for comments at the end of function declarations.
7400   verifyFormat("void\n"
7401                "foo (int a, /*abc*/ int b) // def\n"
7402                "{\n"
7403                "}\n",
7404                Style);
7405 
7406   verifyFormat("void\n"
7407                "foo (int a, /* abc */ int b) /* def */\n"
7408                "{\n"
7409                "}\n",
7410                Style);
7411 
7412   // Definitions that should not break after return type
7413   verifyFormat("void foo (int a, int b); // def\n", Style);
7414   verifyFormat("void foo (int a, int b); /* def */\n", Style);
7415   verifyFormat("void foo (int a, int b);\n", Style);
7416 }
7417 
7418 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
7419   FormatStyle NoBreak = getLLVMStyle();
7420   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
7421   FormatStyle Break = getLLVMStyle();
7422   Break.AlwaysBreakBeforeMultilineStrings = true;
7423   verifyFormat("aaaa = \"bbbb\"\n"
7424                "       \"cccc\";",
7425                NoBreak);
7426   verifyFormat("aaaa =\n"
7427                "    \"bbbb\"\n"
7428                "    \"cccc\";",
7429                Break);
7430   verifyFormat("aaaa(\"bbbb\"\n"
7431                "     \"cccc\");",
7432                NoBreak);
7433   verifyFormat("aaaa(\n"
7434                "    \"bbbb\"\n"
7435                "    \"cccc\");",
7436                Break);
7437   verifyFormat("aaaa(qqq, \"bbbb\"\n"
7438                "          \"cccc\");",
7439                NoBreak);
7440   verifyFormat("aaaa(qqq,\n"
7441                "     \"bbbb\"\n"
7442                "     \"cccc\");",
7443                Break);
7444   verifyFormat("aaaa(qqq,\n"
7445                "     L\"bbbb\"\n"
7446                "     L\"cccc\");",
7447                Break);
7448   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
7449                "                      \"bbbb\"));",
7450                Break);
7451   verifyFormat("string s = someFunction(\n"
7452                "    \"abc\"\n"
7453                "    \"abc\");",
7454                Break);
7455 
7456   // As we break before unary operators, breaking right after them is bad.
7457   verifyFormat("string foo = abc ? \"x\"\n"
7458                "                   \"blah blah blah blah blah blah\"\n"
7459                "                 : \"y\";",
7460                Break);
7461 
7462   // Don't break if there is no column gain.
7463   verifyFormat("f(\"aaaa\"\n"
7464                "  \"bbbb\");",
7465                Break);
7466 
7467   // Treat literals with escaped newlines like multi-line string literals.
7468   EXPECT_EQ("x = \"a\\\n"
7469             "b\\\n"
7470             "c\";",
7471             format("x = \"a\\\n"
7472                    "b\\\n"
7473                    "c\";",
7474                    NoBreak));
7475   EXPECT_EQ("xxxx =\n"
7476             "    \"a\\\n"
7477             "b\\\n"
7478             "c\";",
7479             format("xxxx = \"a\\\n"
7480                    "b\\\n"
7481                    "c\";",
7482                    Break));
7483 
7484   EXPECT_EQ("NSString *const kString =\n"
7485             "    @\"aaaa\"\n"
7486             "    @\"bbbb\";",
7487             format("NSString *const kString = @\"aaaa\"\n"
7488                    "@\"bbbb\";",
7489                    Break));
7490 
7491   Break.ColumnLimit = 0;
7492   verifyFormat("const char *hello = \"hello llvm\";", Break);
7493 }
7494 
7495 TEST_F(FormatTest, AlignsPipes) {
7496   verifyFormat(
7497       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7498       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7499       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7500   verifyFormat(
7501       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
7502       "                     << aaaaaaaaaaaaaaaaaaaa;");
7503   verifyFormat(
7504       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7505       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7506   verifyFormat(
7507       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
7508       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7509   verifyFormat(
7510       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
7511       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
7512       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
7513   verifyFormat(
7514       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7515       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7516       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7517   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7518                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7519                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7520                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
7521   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
7522                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
7523   verifyFormat(
7524       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7525       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7526   verifyFormat(
7527       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
7528       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
7529 
7530   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
7531                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
7532   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7533                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7534                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
7535                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
7536   verifyFormat("LOG_IF(aaa == //\n"
7537                "       bbb)\n"
7538                "    << a << b;");
7539 
7540   // But sometimes, breaking before the first "<<" is desirable.
7541   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
7542                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
7543   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
7544                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7545                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7546   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
7547                "    << BEF << IsTemplate << Description << E->getType();");
7548   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
7549                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7550                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7551   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
7552                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7553                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7554                "    << aaa;");
7555 
7556   verifyFormat(
7557       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7558       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7559 
7560   // Incomplete string literal.
7561   EXPECT_EQ("llvm::errs() << \"\n"
7562             "             << a;",
7563             format("llvm::errs() << \"\n<<a;"));
7564 
7565   verifyFormat("void f() {\n"
7566                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
7567                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
7568                "}");
7569 
7570   // Handle 'endl'.
7571   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
7572                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
7573   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
7574 
7575   // Handle '\n'.
7576   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
7577                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
7578   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
7579                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
7580   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
7581                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
7582   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
7583 }
7584 
7585 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
7586   verifyFormat("return out << \"somepacket = {\\n\"\n"
7587                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
7588                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
7589                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
7590                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
7591                "           << \"}\";");
7592 
7593   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
7594                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
7595                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
7596   verifyFormat(
7597       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
7598       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
7599       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
7600       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
7601       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
7602   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
7603                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
7604   verifyFormat(
7605       "void f() {\n"
7606       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
7607       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7608       "}");
7609 
7610   // Breaking before the first "<<" is generally not desirable.
7611   verifyFormat(
7612       "llvm::errs()\n"
7613       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7614       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7615       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7616       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7617       getLLVMStyleWithColumns(70));
7618   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7619                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7620                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7621                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7622                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7623                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7624                getLLVMStyleWithColumns(70));
7625 
7626   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
7627                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
7628                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
7629   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
7630                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
7631                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
7632   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
7633                "           (aaaa + aaaa);",
7634                getLLVMStyleWithColumns(40));
7635   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
7636                "                  (aaaaaaa + aaaaa));",
7637                getLLVMStyleWithColumns(40));
7638   verifyFormat(
7639       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
7640       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
7641       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
7642 }
7643 
7644 TEST_F(FormatTest, UnderstandsEquals) {
7645   verifyFormat(
7646       "aaaaaaaaaaaaaaaaa =\n"
7647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7648   verifyFormat(
7649       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7650       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
7651   verifyFormat(
7652       "if (a) {\n"
7653       "  f();\n"
7654       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7655       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7656       "}");
7657 
7658   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7659                "        100000000 + 10000000) {\n}");
7660 }
7661 
7662 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
7663   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
7664                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
7665 
7666   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
7667                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
7668 
7669   verifyFormat(
7670       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
7671       "                                                          Parameter2);");
7672 
7673   verifyFormat(
7674       "ShortObject->shortFunction(\n"
7675       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
7676       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
7677 
7678   verifyFormat("loooooooooooooongFunction(\n"
7679                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
7680 
7681   verifyFormat(
7682       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
7683       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
7684 
7685   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
7686                "    .WillRepeatedly(Return(SomeValue));");
7687   verifyFormat("void f() {\n"
7688                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
7689                "      .Times(2)\n"
7690                "      .WillRepeatedly(Return(SomeValue));\n"
7691                "}");
7692   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
7693                "    ccccccccccccccccccccccc);");
7694   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7695                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7696                "          .aaaaa(aaaaa),\n"
7697                "      aaaaaaaaaaaaaaaaaaaaa);");
7698   verifyFormat("void f() {\n"
7699                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7700                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
7701                "}");
7702   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7703                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7704                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7705                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7706                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7707   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7708                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7709                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7710                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
7711                "}");
7712 
7713   // Here, it is not necessary to wrap at "." or "->".
7714   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
7715                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
7716   verifyFormat(
7717       "aaaaaaaaaaa->aaaaaaaaa(\n"
7718       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7719       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
7720 
7721   verifyFormat(
7722       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7723       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
7724   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
7725                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
7726   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
7727                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
7728 
7729   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7730                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7731                "    .a();");
7732 
7733   FormatStyle NoBinPacking = getLLVMStyle();
7734   NoBinPacking.BinPackParameters = false;
7735   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7736                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7737                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
7738                "                         aaaaaaaaaaaaaaaaaaa,\n"
7739                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7740                NoBinPacking);
7741 
7742   // If there is a subsequent call, change to hanging indentation.
7743   verifyFormat(
7744       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7745       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
7746       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7747   verifyFormat(
7748       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7749       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
7750   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7751                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7752                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7753   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7754                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7755                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
7756 }
7757 
7758 TEST_F(FormatTest, WrapsTemplateDeclarations) {
7759   verifyFormat("template <typename T>\n"
7760                "virtual void loooooooooooongFunction(int Param1, int Param2);");
7761   verifyFormat("template <typename T>\n"
7762                "// T should be one of {A, B}.\n"
7763                "virtual void loooooooooooongFunction(int Param1, int Param2);");
7764   verifyFormat(
7765       "template <typename T>\n"
7766       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
7767   verifyFormat("template <typename T>\n"
7768                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
7769                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
7770   verifyFormat(
7771       "template <typename T>\n"
7772       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
7773       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
7774   verifyFormat(
7775       "template <typename T>\n"
7776       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
7777       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
7778       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7779   verifyFormat("template <typename T>\n"
7780                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7781                "    int aaaaaaaaaaaaaaaaaaaaaa);");
7782   verifyFormat(
7783       "template <typename T1, typename T2 = char, typename T3 = char,\n"
7784       "          typename T4 = char>\n"
7785       "void f();");
7786   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
7787                "          template <typename> class cccccccccccccccccccccc,\n"
7788                "          typename ddddddddddddd>\n"
7789                "class C {};");
7790   verifyFormat(
7791       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
7792       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7793 
7794   verifyFormat("void f() {\n"
7795                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
7796                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
7797                "}");
7798 
7799   verifyFormat("template <typename T> class C {};");
7800   verifyFormat("template <typename T> void f();");
7801   verifyFormat("template <typename T> void f() {}");
7802   verifyFormat(
7803       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
7804       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7805       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
7806       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
7807       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7808       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
7809       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
7810       getLLVMStyleWithColumns(72));
7811   EXPECT_EQ("static_cast<A< //\n"
7812             "    B> *>(\n"
7813             "\n"
7814             ");",
7815             format("static_cast<A<//\n"
7816                    "    B>*>(\n"
7817                    "\n"
7818                    "    );"));
7819   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7820                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
7821 
7822   FormatStyle AlwaysBreak = getLLVMStyle();
7823   AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
7824   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
7825   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
7826   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
7827   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7828                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
7829                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
7830   verifyFormat("template <template <typename> class Fooooooo,\n"
7831                "          template <typename> class Baaaaaaar>\n"
7832                "struct C {};",
7833                AlwaysBreak);
7834   verifyFormat("template <typename T> // T can be A, B or C.\n"
7835                "struct C {};",
7836                AlwaysBreak);
7837   verifyFormat("template <enum E> class A {\n"
7838                "public:\n"
7839                "  E *f();\n"
7840                "};");
7841 
7842   FormatStyle NeverBreak = getLLVMStyle();
7843   NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
7844   verifyFormat("template <typename T> class C {};", NeverBreak);
7845   verifyFormat("template <typename T> void f();", NeverBreak);
7846   verifyFormat("template <typename T> void f() {}", NeverBreak);
7847   verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
7848                "bbbbbbbbbbbbbbbbbbbb) {}",
7849                NeverBreak);
7850   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7851                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
7852                "    ccccccccccccccccccccccccccccccccccccccccccccccc);",
7853                NeverBreak);
7854   verifyFormat("template <template <typename> class Fooooooo,\n"
7855                "          template <typename> class Baaaaaaar>\n"
7856                "struct C {};",
7857                NeverBreak);
7858   verifyFormat("template <typename T> // T can be A, B or C.\n"
7859                "struct C {};",
7860                NeverBreak);
7861   verifyFormat("template <enum E> class A {\n"
7862                "public:\n"
7863                "  E *f();\n"
7864                "};",
7865                NeverBreak);
7866   NeverBreak.PenaltyBreakTemplateDeclaration = 100;
7867   verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
7868                "bbbbbbbbbbbbbbbbbbbb) {}",
7869                NeverBreak);
7870 }
7871 
7872 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
7873   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
7874   Style.ColumnLimit = 60;
7875   EXPECT_EQ("// Baseline - no comments.\n"
7876             "template <\n"
7877             "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
7878             "void f() {}",
7879             format("// Baseline - no comments.\n"
7880                    "template <\n"
7881                    "    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
7882                    "void f() {}",
7883                    Style));
7884 
7885   EXPECT_EQ("template <\n"
7886             "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
7887             "void f() {}",
7888             format("template <\n"
7889                    "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7890                    "void f() {}",
7891                    Style));
7892 
7893   EXPECT_EQ(
7894       "template <\n"
7895       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
7896       "void f() {}",
7897       format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */\n"
7898              "void f() {}",
7899              Style));
7900 
7901   EXPECT_EQ(
7902       "template <\n"
7903       "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing\n"
7904       "                                               // multiline\n"
7905       "void f() {}",
7906       format("template <\n"
7907              "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7908              "                                              // multiline\n"
7909              "void f() {}",
7910              Style));
7911 
7912   EXPECT_EQ(
7913       "template <typename aaaaaaaaaa<\n"
7914       "    bbbbbbbbbbbb>::value>  // trailing loooong\n"
7915       "void f() {}",
7916       format(
7917           "template <\n"
7918           "    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
7919           "void f() {}",
7920           Style));
7921 }
7922 
7923 TEST_F(FormatTest, WrapsTemplateParameters) {
7924   FormatStyle Style = getLLVMStyle();
7925   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7926   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7927   verifyFormat(
7928       "template <typename... a> struct q {};\n"
7929       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
7930       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
7931       "    y;",
7932       Style);
7933   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7934   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7935   verifyFormat(
7936       "template <typename... a> struct r {};\n"
7937       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
7938       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
7939       "    y;",
7940       Style);
7941   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7942   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7943   verifyFormat("template <typename... a> struct s {};\n"
7944                "extern s<\n"
7945                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7946                "aaaaaaaaaaaaaaaaaaaaaa,\n"
7947                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7948                "aaaaaaaaaaaaaaaaaaaaaa>\n"
7949                "    y;",
7950                Style);
7951   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7952   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7953   verifyFormat("template <typename... a> struct t {};\n"
7954                "extern t<\n"
7955                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7956                "aaaaaaaaaaaaaaaaaaaaaa,\n"
7957                "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7958                "aaaaaaaaaaaaaaaaaaaaaa>\n"
7959                "    y;",
7960                Style);
7961 }
7962 
7963 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
7964   verifyFormat(
7965       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7966       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7967   verifyFormat(
7968       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7969       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7970       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
7971 
7972   // FIXME: Should we have the extra indent after the second break?
7973   verifyFormat(
7974       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7975       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7976       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7977 
7978   verifyFormat(
7979       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
7980       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
7981 
7982   // Breaking at nested name specifiers is generally not desirable.
7983   verifyFormat(
7984       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7985       "    aaaaaaaaaaaaaaaaaaaaaaa);");
7986 
7987   verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
7988                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7989                "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7990                "                   aaaaaaaaaaaaaaaaaaaaa);",
7991                getLLVMStyleWithColumns(74));
7992 
7993   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7994                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7995                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7996 }
7997 
7998 TEST_F(FormatTest, UnderstandsTemplateParameters) {
7999   verifyFormat("A<int> a;");
8000   verifyFormat("A<A<A<int>>> a;");
8001   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
8002   verifyFormat("bool x = a < 1 || 2 > a;");
8003   verifyFormat("bool x = 5 < f<int>();");
8004   verifyFormat("bool x = f<int>() > 5;");
8005   verifyFormat("bool x = 5 < a<int>::x;");
8006   verifyFormat("bool x = a < 4 ? a > 2 : false;");
8007   verifyFormat("bool x = f() ? a < 2 : a > 2;");
8008 
8009   verifyGoogleFormat("A<A<int>> a;");
8010   verifyGoogleFormat("A<A<A<int>>> a;");
8011   verifyGoogleFormat("A<A<A<A<int>>>> a;");
8012   verifyGoogleFormat("A<A<int> > a;");
8013   verifyGoogleFormat("A<A<A<int> > > a;");
8014   verifyGoogleFormat("A<A<A<A<int> > > > a;");
8015   verifyGoogleFormat("A<::A<int>> a;");
8016   verifyGoogleFormat("A<::A> a;");
8017   verifyGoogleFormat("A< ::A> a;");
8018   verifyGoogleFormat("A< ::A<int> > a;");
8019   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
8020   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
8021   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
8022   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
8023   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
8024             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
8025 
8026   verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
8027 
8028   // template closer followed by a token that starts with > or =
8029   verifyFormat("bool b = a<1> > 1;");
8030   verifyFormat("bool b = a<1> >= 1;");
8031   verifyFormat("int i = a<1> >> 1;");
8032   FormatStyle Style = getLLVMStyle();
8033   Style.SpaceBeforeAssignmentOperators = false;
8034   verifyFormat("bool b= a<1> == 1;", Style);
8035   verifyFormat("a<int> = 1;", Style);
8036   verifyFormat("a<int> >>= 1;", Style);
8037 
8038   verifyFormat("test < a | b >> c;");
8039   verifyFormat("test<test<a | b>> c;");
8040   verifyFormat("test >> a >> b;");
8041   verifyFormat("test << a >> b;");
8042 
8043   verifyFormat("f<int>();");
8044   verifyFormat("template <typename T> void f() {}");
8045   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
8046   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
8047                "sizeof(char)>::type>;");
8048   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
8049   verifyFormat("f(a.operator()<A>());");
8050   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8051                "      .template operator()<A>());",
8052                getLLVMStyleWithColumns(35));
8053 
8054   // Not template parameters.
8055   verifyFormat("return a < b && c > d;");
8056   verifyFormat("void f() {\n"
8057                "  while (a < b && c > d) {\n"
8058                "  }\n"
8059                "}");
8060   verifyFormat("template <typename... Types>\n"
8061                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
8062 
8063   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8064                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
8065                getLLVMStyleWithColumns(60));
8066   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
8067   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
8068   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
8069   verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
8070 }
8071 
8072 TEST_F(FormatTest, UnderstandsShiftOperators) {
8073   verifyFormat("if (i < x >> 1)");
8074   verifyFormat("while (i < x >> 1)");
8075   verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
8076   verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
8077   verifyFormat(
8078       "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
8079   verifyFormat("Foo.call<Bar<Function>>()");
8080   verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
8081   verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
8082                "++i, v = v >> 1)");
8083   verifyFormat("if (w<u<v<x>>, 1>::t)");
8084 }
8085 
8086 TEST_F(FormatTest, BitshiftOperatorWidth) {
8087   EXPECT_EQ("int a = 1 << 2; /* foo\n"
8088             "                   bar */",
8089             format("int    a=1<<2;  /* foo\n"
8090                    "                   bar */"));
8091 
8092   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
8093             "                     bar */",
8094             format("int  b  =256>>1 ;  /* foo\n"
8095                    "                      bar */"));
8096 }
8097 
8098 TEST_F(FormatTest, UnderstandsBinaryOperators) {
8099   verifyFormat("COMPARE(a, ==, b);");
8100   verifyFormat("auto s = sizeof...(Ts) - 1;");
8101 }
8102 
8103 TEST_F(FormatTest, UnderstandsPointersToMembers) {
8104   verifyFormat("int A::*x;");
8105   verifyFormat("int (S::*func)(void *);");
8106   verifyFormat("void f() { int (S::*func)(void *); }");
8107   verifyFormat("typedef bool *(Class::*Member)() const;");
8108   verifyFormat("void f() {\n"
8109                "  (a->*f)();\n"
8110                "  a->*x;\n"
8111                "  (a.*f)();\n"
8112                "  ((*a).*f)();\n"
8113                "  a.*x;\n"
8114                "}");
8115   verifyFormat("void f() {\n"
8116                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
8117                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
8118                "}");
8119   verifyFormat(
8120       "(aaaaaaaaaa->*bbbbbbb)(\n"
8121       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8122   FormatStyle Style = getLLVMStyle();
8123   Style.PointerAlignment = FormatStyle::PAS_Left;
8124   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
8125 }
8126 
8127 TEST_F(FormatTest, UnderstandsUnaryOperators) {
8128   verifyFormat("int a = -2;");
8129   verifyFormat("f(-1, -2, -3);");
8130   verifyFormat("a[-1] = 5;");
8131   verifyFormat("int a = 5 + -2;");
8132   verifyFormat("if (i == -1) {\n}");
8133   verifyFormat("if (i != -1) {\n}");
8134   verifyFormat("if (i > -1) {\n}");
8135   verifyFormat("if (i < -1) {\n}");
8136   verifyFormat("++(a->f());");
8137   verifyFormat("--(a->f());");
8138   verifyFormat("(a->f())++;");
8139   verifyFormat("a[42]++;");
8140   verifyFormat("if (!(a->f())) {\n}");
8141   verifyFormat("if (!+i) {\n}");
8142   verifyFormat("~&a;");
8143 
8144   verifyFormat("a-- > b;");
8145   verifyFormat("b ? -a : c;");
8146   verifyFormat("n * sizeof char16;");
8147   verifyFormat("n * alignof char16;", getGoogleStyle());
8148   verifyFormat("sizeof(char);");
8149   verifyFormat("alignof(char);", getGoogleStyle());
8150 
8151   verifyFormat("return -1;");
8152   verifyFormat("throw -1;");
8153   verifyFormat("switch (a) {\n"
8154                "case -1:\n"
8155                "  break;\n"
8156                "}");
8157   verifyFormat("#define X -1");
8158   verifyFormat("#define X -kConstant");
8159 
8160   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
8161   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
8162 
8163   verifyFormat("int a = /* confusing comment */ -1;");
8164   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
8165   verifyFormat("int a = i /* confusing comment */++;");
8166 
8167   verifyFormat("co_yield -1;");
8168   verifyFormat("co_return -1;");
8169 
8170   // Check that * is not treated as a binary operator when we set
8171   // PointerAlignment as PAS_Left after a keyword and not a declaration.
8172   FormatStyle PASLeftStyle = getLLVMStyle();
8173   PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
8174   verifyFormat("co_return *a;", PASLeftStyle);
8175   verifyFormat("co_await *a;", PASLeftStyle);
8176   verifyFormat("co_yield *a", PASLeftStyle);
8177   verifyFormat("return *a;", PASLeftStyle);
8178 }
8179 
8180 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
8181   verifyFormat("if (!aaaaaaaaaa( // break\n"
8182                "        aaaaa)) {\n"
8183                "}");
8184   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
8185                "    aaaaa));");
8186   verifyFormat("*aaa = aaaaaaa( // break\n"
8187                "    bbbbbb);");
8188 }
8189 
8190 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
8191   verifyFormat("bool operator<();");
8192   verifyFormat("bool operator>();");
8193   verifyFormat("bool operator=();");
8194   verifyFormat("bool operator==();");
8195   verifyFormat("bool operator!=();");
8196   verifyFormat("int operator+();");
8197   verifyFormat("int operator++();");
8198   verifyFormat("int operator++(int) volatile noexcept;");
8199   verifyFormat("bool operator,();");
8200   verifyFormat("bool operator();");
8201   verifyFormat("bool operator()();");
8202   verifyFormat("bool operator[]();");
8203   verifyFormat("operator bool();");
8204   verifyFormat("operator int();");
8205   verifyFormat("operator void *();");
8206   verifyFormat("operator SomeType<int>();");
8207   verifyFormat("operator SomeType<int, int>();");
8208   verifyFormat("operator SomeType<SomeType<int>>();");
8209   verifyFormat("void *operator new(std::size_t size);");
8210   verifyFormat("void *operator new[](std::size_t size);");
8211   verifyFormat("void operator delete(void *ptr);");
8212   verifyFormat("void operator delete[](void *ptr);");
8213   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
8214                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
8215   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
8216                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
8217 
8218   verifyFormat(
8219       "ostream &operator<<(ostream &OutputStream,\n"
8220       "                    SomeReallyLongType WithSomeReallyLongValue);");
8221   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
8222                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
8223                "  return left.group < right.group;\n"
8224                "}");
8225   verifyFormat("SomeType &operator=(const SomeType &S);");
8226   verifyFormat("f.template operator()<int>();");
8227 
8228   verifyGoogleFormat("operator void*();");
8229   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
8230   verifyGoogleFormat("operator ::A();");
8231 
8232   verifyFormat("using A::operator+;");
8233   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
8234                "int i;");
8235 }
8236 
8237 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
8238   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
8239   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
8240   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
8241   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
8242   verifyFormat("Deleted &operator=(const Deleted &) &;");
8243   verifyFormat("Deleted &operator=(const Deleted &) &&;");
8244   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
8245   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
8246   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
8247   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
8248   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
8249   verifyFormat("void Fn(T const &) const &;");
8250   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
8251   verifyFormat("template <typename T>\n"
8252                "void F(T) && = delete;",
8253                getGoogleStyle());
8254 
8255   FormatStyle AlignLeft = getLLVMStyle();
8256   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
8257   verifyFormat("void A::b() && {}", AlignLeft);
8258   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
8259   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
8260                AlignLeft);
8261   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
8262   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
8263   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
8264   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
8265   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
8266   verifyFormat("auto Function(T) & -> void;", AlignLeft);
8267   verifyFormat("void Fn(T const&) const&;", AlignLeft);
8268   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
8269 
8270   FormatStyle Spaces = getLLVMStyle();
8271   Spaces.SpacesInCStyleCastParentheses = true;
8272   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
8273   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
8274   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
8275   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
8276 
8277   Spaces.SpacesInCStyleCastParentheses = false;
8278   Spaces.SpacesInParentheses = true;
8279   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
8280   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
8281                Spaces);
8282   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
8283   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
8284 
8285   FormatStyle BreakTemplate = getLLVMStyle();
8286   BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
8287 
8288   verifyFormat("struct f {\n"
8289                "  template <class T>\n"
8290                "  int &foo(const std::string &str) &noexcept {}\n"
8291                "};",
8292                BreakTemplate);
8293 
8294   verifyFormat("struct f {\n"
8295                "  template <class T>\n"
8296                "  int &foo(const std::string &str) &&noexcept {}\n"
8297                "};",
8298                BreakTemplate);
8299 
8300   verifyFormat("struct f {\n"
8301                "  template <class T>\n"
8302                "  int &foo(const std::string &str) const &noexcept {}\n"
8303                "};",
8304                BreakTemplate);
8305 
8306   verifyFormat("struct f {\n"
8307                "  template <class T>\n"
8308                "  int &foo(const std::string &str) const &noexcept {}\n"
8309                "};",
8310                BreakTemplate);
8311 
8312   verifyFormat("struct f {\n"
8313                "  template <class T>\n"
8314                "  auto foo(const std::string &str) &&noexcept -> int & {}\n"
8315                "};",
8316                BreakTemplate);
8317 
8318   FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
8319   AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
8320       FormatStyle::BTDS_Yes;
8321   AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
8322 
8323   verifyFormat("struct f {\n"
8324                "  template <class T>\n"
8325                "  int& foo(const std::string& str) & noexcept {}\n"
8326                "};",
8327                AlignLeftBreakTemplate);
8328 
8329   verifyFormat("struct f {\n"
8330                "  template <class T>\n"
8331                "  int& foo(const std::string& str) && noexcept {}\n"
8332                "};",
8333                AlignLeftBreakTemplate);
8334 
8335   verifyFormat("struct f {\n"
8336                "  template <class T>\n"
8337                "  int& foo(const std::string& str) const& noexcept {}\n"
8338                "};",
8339                AlignLeftBreakTemplate);
8340 
8341   verifyFormat("struct f {\n"
8342                "  template <class T>\n"
8343                "  int& foo(const std::string& str) const&& noexcept {}\n"
8344                "};",
8345                AlignLeftBreakTemplate);
8346 
8347   verifyFormat("struct f {\n"
8348                "  template <class T>\n"
8349                "  auto foo(const std::string& str) && noexcept -> int& {}\n"
8350                "};",
8351                AlignLeftBreakTemplate);
8352 
8353   // The `&` in `Type&` should not be confused with a trailing `&` of
8354   // DEPRECATED(reason) member function.
8355   verifyFormat("struct f {\n"
8356                "  template <class T>\n"
8357                "  DEPRECATED(reason)\n"
8358                "  Type &foo(arguments) {}\n"
8359                "};",
8360                BreakTemplate);
8361 
8362   verifyFormat("struct f {\n"
8363                "  template <class T>\n"
8364                "  DEPRECATED(reason)\n"
8365                "  Type& foo(arguments) {}\n"
8366                "};",
8367                AlignLeftBreakTemplate);
8368 
8369   verifyFormat("void (*foopt)(int) = &func;");
8370 }
8371 
8372 TEST_F(FormatTest, UnderstandsNewAndDelete) {
8373   verifyFormat("void f() {\n"
8374                "  A *a = new A;\n"
8375                "  A *a = new (placement) A;\n"
8376                "  delete a;\n"
8377                "  delete (A *)a;\n"
8378                "}");
8379   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
8380                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
8381   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8382                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
8383                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
8384   verifyFormat("delete[] h->p;");
8385 }
8386 
8387 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
8388   verifyFormat("int *f(int *a) {}");
8389   verifyFormat("int main(int argc, char **argv) {}");
8390   verifyFormat("Test::Test(int b) : a(b * b) {}");
8391   verifyIndependentOfContext("f(a, *a);");
8392   verifyFormat("void g() { f(*a); }");
8393   verifyIndependentOfContext("int a = b * 10;");
8394   verifyIndependentOfContext("int a = 10 * b;");
8395   verifyIndependentOfContext("int a = b * c;");
8396   verifyIndependentOfContext("int a += b * c;");
8397   verifyIndependentOfContext("int a -= b * c;");
8398   verifyIndependentOfContext("int a *= b * c;");
8399   verifyIndependentOfContext("int a /= b * c;");
8400   verifyIndependentOfContext("int a = *b;");
8401   verifyIndependentOfContext("int a = *b * c;");
8402   verifyIndependentOfContext("int a = b * *c;");
8403   verifyIndependentOfContext("int a = b * (10);");
8404   verifyIndependentOfContext("S << b * (10);");
8405   verifyIndependentOfContext("return 10 * b;");
8406   verifyIndependentOfContext("return *b * *c;");
8407   verifyIndependentOfContext("return a & ~b;");
8408   verifyIndependentOfContext("f(b ? *c : *d);");
8409   verifyIndependentOfContext("int a = b ? *c : *d;");
8410   verifyIndependentOfContext("*b = a;");
8411   verifyIndependentOfContext("a * ~b;");
8412   verifyIndependentOfContext("a * !b;");
8413   verifyIndependentOfContext("a * +b;");
8414   verifyIndependentOfContext("a * -b;");
8415   verifyIndependentOfContext("a * ++b;");
8416   verifyIndependentOfContext("a * --b;");
8417   verifyIndependentOfContext("a[4] * b;");
8418   verifyIndependentOfContext("a[a * a] = 1;");
8419   verifyIndependentOfContext("f() * b;");
8420   verifyIndependentOfContext("a * [self dostuff];");
8421   verifyIndependentOfContext("int x = a * (a + b);");
8422   verifyIndependentOfContext("(a *)(a + b);");
8423   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
8424   verifyIndependentOfContext("int *pa = (int *)&a;");
8425   verifyIndependentOfContext("return sizeof(int **);");
8426   verifyIndependentOfContext("return sizeof(int ******);");
8427   verifyIndependentOfContext("return (int **&)a;");
8428   verifyIndependentOfContext("f((*PointerToArray)[10]);");
8429   verifyFormat("void f(Type (*parameter)[10]) {}");
8430   verifyFormat("void f(Type (&parameter)[10]) {}");
8431   verifyGoogleFormat("return sizeof(int**);");
8432   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
8433   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
8434   verifyFormat("auto a = [](int **&, int ***) {};");
8435   verifyFormat("auto PointerBinding = [](const char *S) {};");
8436   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
8437   verifyFormat("[](const decltype(*a) &value) {}");
8438   verifyFormat("[](const typeof(*a) &value) {}");
8439   verifyFormat("[](const _Atomic(a *) &value) {}");
8440   verifyFormat("[](const __underlying_type(a) &value) {}");
8441   verifyFormat("decltype(a * b) F();");
8442   verifyFormat("typeof(a * b) F();");
8443   verifyFormat("#define MACRO() [](A *a) { return 1; }");
8444   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
8445   verifyIndependentOfContext("typedef void (*f)(int *a);");
8446   verifyIndependentOfContext("int i{a * b};");
8447   verifyIndependentOfContext("aaa && aaa->f();");
8448   verifyIndependentOfContext("int x = ~*p;");
8449   verifyFormat("Constructor() : a(a), area(width * height) {}");
8450   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
8451   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
8452   verifyFormat("void f() { f(a, c * d); }");
8453   verifyFormat("void f() { f(new a(), c * d); }");
8454   verifyFormat("void f(const MyOverride &override);");
8455   verifyFormat("void f(const MyFinal &final);");
8456   verifyIndependentOfContext("bool a = f() && override.f();");
8457   verifyIndependentOfContext("bool a = f() && final.f();");
8458 
8459   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
8460 
8461   verifyIndependentOfContext("A<int *> a;");
8462   verifyIndependentOfContext("A<int **> a;");
8463   verifyIndependentOfContext("A<int *, int *> a;");
8464   verifyIndependentOfContext("A<int *[]> a;");
8465   verifyIndependentOfContext(
8466       "const char *const p = reinterpret_cast<const char *const>(q);");
8467   verifyIndependentOfContext("A<int **, int **> a;");
8468   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
8469   verifyFormat("for (char **a = b; *a; ++a) {\n}");
8470   verifyFormat("for (; a && b;) {\n}");
8471   verifyFormat("bool foo = true && [] { return false; }();");
8472 
8473   verifyFormat(
8474       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8475       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8476 
8477   verifyGoogleFormat("int const* a = &b;");
8478   verifyGoogleFormat("**outparam = 1;");
8479   verifyGoogleFormat("*outparam = a * b;");
8480   verifyGoogleFormat("int main(int argc, char** argv) {}");
8481   verifyGoogleFormat("A<int*> a;");
8482   verifyGoogleFormat("A<int**> a;");
8483   verifyGoogleFormat("A<int*, int*> a;");
8484   verifyGoogleFormat("A<int**, int**> a;");
8485   verifyGoogleFormat("f(b ? *c : *d);");
8486   verifyGoogleFormat("int a = b ? *c : *d;");
8487   verifyGoogleFormat("Type* t = **x;");
8488   verifyGoogleFormat("Type* t = *++*x;");
8489   verifyGoogleFormat("*++*x;");
8490   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
8491   verifyGoogleFormat("Type* t = x++ * y;");
8492   verifyGoogleFormat(
8493       "const char* const p = reinterpret_cast<const char* const>(q);");
8494   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
8495   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
8496   verifyGoogleFormat("template <typename T>\n"
8497                      "void f(int i = 0, SomeType** temps = NULL);");
8498 
8499   FormatStyle Left = getLLVMStyle();
8500   Left.PointerAlignment = FormatStyle::PAS_Left;
8501   verifyFormat("x = *a(x) = *a(y);", Left);
8502   verifyFormat("for (;; *a = b) {\n}", Left);
8503   verifyFormat("return *this += 1;", Left);
8504   verifyFormat("throw *x;", Left);
8505   verifyFormat("delete *x;", Left);
8506   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
8507   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
8508   verifyFormat("[](const typeof(*a)* ptr) {}", Left);
8509   verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
8510   verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
8511   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
8512   verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
8513   verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
8514   verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
8515 
8516   verifyIndependentOfContext("a = *(x + y);");
8517   verifyIndependentOfContext("a = &(x + y);");
8518   verifyIndependentOfContext("*(x + y).call();");
8519   verifyIndependentOfContext("&(x + y)->call();");
8520   verifyFormat("void f() { &(*I).first; }");
8521 
8522   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
8523   verifyFormat(
8524       "int *MyValues = {\n"
8525       "    *A, // Operator detection might be confused by the '{'\n"
8526       "    *BB // Operator detection might be confused by previous comment\n"
8527       "};");
8528 
8529   verifyIndependentOfContext("if (int *a = &b)");
8530   verifyIndependentOfContext("if (int &a = *b)");
8531   verifyIndependentOfContext("if (a & b[i])");
8532   verifyIndependentOfContext("if constexpr (a & b[i])");
8533   verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
8534   verifyIndependentOfContext("if (a * (b * c))");
8535   verifyIndependentOfContext("if constexpr (a * (b * c))");
8536   verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
8537   verifyIndependentOfContext("if (a::b::c::d & b[i])");
8538   verifyIndependentOfContext("if (*b[i])");
8539   verifyIndependentOfContext("if (int *a = (&b))");
8540   verifyIndependentOfContext("while (int *a = &b)");
8541   verifyIndependentOfContext("while (a * (b * c))");
8542   verifyIndependentOfContext("size = sizeof *a;");
8543   verifyIndependentOfContext("if (a && (b = c))");
8544   verifyFormat("void f() {\n"
8545                "  for (const int &v : Values) {\n"
8546                "  }\n"
8547                "}");
8548   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
8549   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
8550   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
8551 
8552   verifyFormat("#define A (!a * b)");
8553   verifyFormat("#define MACRO     \\\n"
8554                "  int *i = a * b; \\\n"
8555                "  void f(a *b);",
8556                getLLVMStyleWithColumns(19));
8557 
8558   verifyIndependentOfContext("A = new SomeType *[Length];");
8559   verifyIndependentOfContext("A = new SomeType *[Length]();");
8560   verifyIndependentOfContext("T **t = new T *;");
8561   verifyIndependentOfContext("T **t = new T *();");
8562   verifyGoogleFormat("A = new SomeType*[Length]();");
8563   verifyGoogleFormat("A = new SomeType*[Length];");
8564   verifyGoogleFormat("T** t = new T*;");
8565   verifyGoogleFormat("T** t = new T*();");
8566 
8567   verifyFormat("STATIC_ASSERT((a & b) == 0);");
8568   verifyFormat("STATIC_ASSERT(0 == (a & b));");
8569   verifyFormat("template <bool a, bool b> "
8570                "typename t::if<x && y>::type f() {}");
8571   verifyFormat("template <int *y> f() {}");
8572   verifyFormat("vector<int *> v;");
8573   verifyFormat("vector<int *const> v;");
8574   verifyFormat("vector<int *const **const *> v;");
8575   verifyFormat("vector<int *volatile> v;");
8576   verifyFormat("vector<a *_Nonnull> v;");
8577   verifyFormat("vector<a *_Nullable> v;");
8578   verifyFormat("vector<a *_Null_unspecified> v;");
8579   verifyFormat("vector<a *__ptr32> v;");
8580   verifyFormat("vector<a *__ptr64> v;");
8581   verifyFormat("vector<a *__capability> v;");
8582   FormatStyle TypeMacros = getLLVMStyle();
8583   TypeMacros.TypenameMacros = {"LIST"};
8584   verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
8585   verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
8586   verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
8587   verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
8588   verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
8589 
8590   FormatStyle CustomQualifier = getLLVMStyle();
8591   // Add indentifers that should not be parsed as a qualifier by default.
8592   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
8593   CustomQualifier.AttributeMacros.push_back("_My_qualifier");
8594   CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
8595   verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
8596   verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
8597   verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
8598   verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
8599   verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
8600   verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
8601   verifyFormat("vector<a * _NotAQualifier> v;");
8602   verifyFormat("vector<a * __not_a_qualifier> v;");
8603   verifyFormat("vector<a * b> v;");
8604   verifyFormat("foo<b && false>();");
8605   verifyFormat("foo<b & 1>();");
8606   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
8607   verifyFormat("typeof(*::std::declval<const T &>()) void F();");
8608   verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
8609   verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
8610   verifyFormat(
8611       "template <class T, class = typename std::enable_if<\n"
8612       "                       std::is_integral<T>::value &&\n"
8613       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
8614       "void F();",
8615       getLLVMStyleWithColumns(70));
8616   verifyFormat("template <class T,\n"
8617                "          class = typename std::enable_if<\n"
8618                "              std::is_integral<T>::value &&\n"
8619                "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
8620                "          class U>\n"
8621                "void F();",
8622                getLLVMStyleWithColumns(70));
8623   verifyFormat(
8624       "template <class T,\n"
8625       "          class = typename ::std::enable_if<\n"
8626       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
8627       "void F();",
8628       getGoogleStyleWithColumns(68));
8629 
8630   verifyIndependentOfContext("MACRO(int *i);");
8631   verifyIndependentOfContext("MACRO(auto *a);");
8632   verifyIndependentOfContext("MACRO(const A *a);");
8633   verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
8634   verifyIndependentOfContext("MACRO(decltype(A) *a);");
8635   verifyIndependentOfContext("MACRO(typeof(A) *a);");
8636   verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
8637   verifyIndependentOfContext("MACRO(A *const a);");
8638   verifyIndependentOfContext("MACRO(A *restrict a);");
8639   verifyIndependentOfContext("MACRO(A *__restrict__ a);");
8640   verifyIndependentOfContext("MACRO(A *__restrict a);");
8641   verifyIndependentOfContext("MACRO(A *volatile a);");
8642   verifyIndependentOfContext("MACRO(A *__volatile a);");
8643   verifyIndependentOfContext("MACRO(A *__volatile__ a);");
8644   verifyIndependentOfContext("MACRO(A *_Nonnull a);");
8645   verifyIndependentOfContext("MACRO(A *_Nullable a);");
8646   verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
8647   verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
8648   verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
8649   verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
8650   verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
8651   verifyIndependentOfContext("MACRO(A *__ptr32 a);");
8652   verifyIndependentOfContext("MACRO(A *__ptr64 a);");
8653   verifyIndependentOfContext("MACRO(A *__capability);");
8654   verifyIndependentOfContext("MACRO(A &__capability);");
8655   verifyFormat("MACRO(A *__my_qualifier);");               // type declaration
8656   verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
8657   // If we add __my_qualifier to AttributeMacros it should always be parsed as
8658   // a type declaration:
8659   verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
8660   verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
8661   // Also check that TypenameMacros prevents parsing it as multiplication:
8662   verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
8663   verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
8664 
8665   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
8666   verifyFormat("void f() { f(float{1}, a * a); }");
8667   // FIXME: Is there a way to make this work?
8668   // verifyIndependentOfContext("MACRO(A *a);");
8669   verifyFormat("MACRO(A &B);");
8670   verifyFormat("MACRO(A *B);");
8671   verifyFormat("void f() { MACRO(A * B); }");
8672   verifyFormat("void f() { MACRO(A & B); }");
8673 
8674   // This lambda was mis-formatted after D88956 (treating it as a binop):
8675   verifyFormat("auto x = [](const decltype(x) &ptr) {};");
8676   verifyFormat("auto x = [](const decltype(x) *ptr) {};");
8677   verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
8678   verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
8679 
8680   verifyFormat("DatumHandle const *operator->() const { return input_; }");
8681   verifyFormat("return options != nullptr && operator==(*options);");
8682 
8683   EXPECT_EQ("#define OP(x)                                    \\\n"
8684             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
8685             "    return s << a.DebugString();                 \\\n"
8686             "  }",
8687             format("#define OP(x) \\\n"
8688                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
8689                    "    return s << a.DebugString(); \\\n"
8690                    "  }",
8691                    getLLVMStyleWithColumns(50)));
8692 
8693   // FIXME: We cannot handle this case yet; we might be able to figure out that
8694   // foo<x> d > v; doesn't make sense.
8695   verifyFormat("foo<a<b && c> d> v;");
8696 
8697   FormatStyle PointerMiddle = getLLVMStyle();
8698   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
8699   verifyFormat("delete *x;", PointerMiddle);
8700   verifyFormat("int * x;", PointerMiddle);
8701   verifyFormat("int *[] x;", PointerMiddle);
8702   verifyFormat("template <int * y> f() {}", PointerMiddle);
8703   verifyFormat("int * f(int * a) {}", PointerMiddle);
8704   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
8705   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
8706   verifyFormat("A<int *> a;", PointerMiddle);
8707   verifyFormat("A<int **> a;", PointerMiddle);
8708   verifyFormat("A<int *, int *> a;", PointerMiddle);
8709   verifyFormat("A<int *[]> a;", PointerMiddle);
8710   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
8711   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
8712   verifyFormat("T ** t = new T *;", PointerMiddle);
8713 
8714   // Member function reference qualifiers aren't binary operators.
8715   verifyFormat("string // break\n"
8716                "operator()() & {}");
8717   verifyFormat("string // break\n"
8718                "operator()() && {}");
8719   verifyGoogleFormat("template <typename T>\n"
8720                      "auto x() & -> int {}");
8721 }
8722 
8723 TEST_F(FormatTest, UnderstandsAttributes) {
8724   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
8725   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
8726                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
8727   FormatStyle AfterType = getLLVMStyle();
8728   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8729   verifyFormat("__attribute__((nodebug)) void\n"
8730                "foo() {}\n",
8731                AfterType);
8732   verifyFormat("__unused void\n"
8733                "foo() {}",
8734                AfterType);
8735 
8736   FormatStyle CustomAttrs = getLLVMStyle();
8737   CustomAttrs.AttributeMacros.push_back("__unused");
8738   CustomAttrs.AttributeMacros.push_back("__attr1");
8739   CustomAttrs.AttributeMacros.push_back("__attr2");
8740   CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
8741   verifyFormat("vector<SomeType *__attribute((foo))> v;");
8742   verifyFormat("vector<SomeType *__attribute__((foo))> v;");
8743   verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
8744   // Check that it is parsed as a multiplication without AttributeMacros and
8745   // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
8746   verifyFormat("vector<SomeType * __attr1> v;");
8747   verifyFormat("vector<SomeType __attr1 *> v;");
8748   verifyFormat("vector<SomeType __attr1 *const> v;");
8749   verifyFormat("vector<SomeType __attr1 * __attr2> v;");
8750   verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
8751   verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
8752   verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
8753   verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
8754   verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
8755   verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
8756   verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
8757 
8758   // Check that these are not parsed as function declarations:
8759   CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8760   CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
8761   verifyFormat("SomeType s(InitValue);", CustomAttrs);
8762   verifyFormat("SomeType s{InitValue};", CustomAttrs);
8763   verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
8764   verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
8765   verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
8766   verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
8767   verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
8768   verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
8769 }
8770 
8771 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
8772   // Check that qualifiers on pointers don't break parsing of casts.
8773   verifyFormat("x = (foo *const)*v;");
8774   verifyFormat("x = (foo *volatile)*v;");
8775   verifyFormat("x = (foo *restrict)*v;");
8776   verifyFormat("x = (foo *__attribute__((foo)))*v;");
8777   verifyFormat("x = (foo *_Nonnull)*v;");
8778   verifyFormat("x = (foo *_Nullable)*v;");
8779   verifyFormat("x = (foo *_Null_unspecified)*v;");
8780   verifyFormat("x = (foo *_Nonnull)*v;");
8781   verifyFormat("x = (foo *[[clang::attr]])*v;");
8782   verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
8783   verifyFormat("x = (foo *__ptr32)*v;");
8784   verifyFormat("x = (foo *__ptr64)*v;");
8785   verifyFormat("x = (foo *__capability)*v;");
8786 
8787   // Check that we handle multiple trailing qualifiers and skip them all to
8788   // determine that the expression is a cast to a pointer type.
8789   FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
8790   FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
8791   LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
8792   StringRef AllQualifiers =
8793       "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
8794       "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
8795   verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
8796   verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
8797 
8798   // Also check that address-of is not parsed as a binary bitwise-and:
8799   verifyFormat("x = (foo *const)&v;");
8800   verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
8801   verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
8802 
8803   // Check custom qualifiers:
8804   FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
8805   CustomQualifier.AttributeMacros.push_back("__my_qualifier");
8806   verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
8807   verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
8808   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
8809                CustomQualifier);
8810   verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
8811                CustomQualifier);
8812 
8813   // Check that unknown identifiers result in binary operator parsing:
8814   verifyFormat("x = (foo * __unknown_qualifier) * v;");
8815   verifyFormat("x = (foo * __unknown_qualifier) & v;");
8816 }
8817 
8818 TEST_F(FormatTest, UnderstandsSquareAttributes) {
8819   verifyFormat("SomeType s [[unused]] (InitValue);");
8820   verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
8821   verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
8822   verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
8823   verifyFormat("void f() [[deprecated(\"so sorry\")]];");
8824   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8825                "    [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
8826   verifyFormat("[[nodiscard]] bool f() { return false; }");
8827   verifyFormat("class [[nodiscard]] f {\npublic:\n  f() {}\n}");
8828   verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n  f() {}\n}");
8829   verifyFormat("class [[gnu::unused]] f {\npublic:\n  f() {}\n}");
8830 
8831   // Make sure we do not mistake attributes for array subscripts.
8832   verifyFormat("int a() {}\n"
8833                "[[unused]] int b() {}\n");
8834   verifyFormat("NSArray *arr;\n"
8835                "arr[[Foo() bar]];");
8836 
8837   // On the other hand, we still need to correctly find array subscripts.
8838   verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
8839 
8840   // Make sure that we do not mistake Objective-C method inside array literals
8841   // as attributes, even if those method names are also keywords.
8842   verifyFormat("@[ [foo bar] ];");
8843   verifyFormat("@[ [NSArray class] ];");
8844   verifyFormat("@[ [foo enum] ];");
8845 
8846   verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
8847 
8848   // Make sure we do not parse attributes as lambda introducers.
8849   FormatStyle MultiLineFunctions = getLLVMStyle();
8850   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8851   verifyFormat("[[unused]] int b() {\n"
8852                "  return 42;\n"
8853                "}\n",
8854                MultiLineFunctions);
8855 }
8856 
8857 TEST_F(FormatTest, AttributeClass) {
8858   FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
8859   verifyFormat("class S {\n"
8860                "  S(S&&) = default;\n"
8861                "};",
8862                Style);
8863   verifyFormat("class [[nodiscard]] S {\n"
8864                "  S(S&&) = default;\n"
8865                "};",
8866                Style);
8867   verifyFormat("class __attribute((maybeunused)) S {\n"
8868                "  S(S&&) = default;\n"
8869                "};",
8870                Style);
8871   verifyFormat("struct S {\n"
8872                "  S(S&&) = default;\n"
8873                "};",
8874                Style);
8875   verifyFormat("struct [[nodiscard]] S {\n"
8876                "  S(S&&) = default;\n"
8877                "};",
8878                Style);
8879 }
8880 
8881 TEST_F(FormatTest, AttributesAfterMacro) {
8882   FormatStyle Style = getLLVMStyle();
8883   verifyFormat("MACRO;\n"
8884                "__attribute__((maybe_unused)) int foo() {\n"
8885                "  //...\n"
8886                "}");
8887 
8888   verifyFormat("MACRO;\n"
8889                "[[nodiscard]] int foo() {\n"
8890                "  //...\n"
8891                "}");
8892 
8893   EXPECT_EQ("MACRO\n\n"
8894             "__attribute__((maybe_unused)) int foo() {\n"
8895             "  //...\n"
8896             "}",
8897             format("MACRO\n\n"
8898                    "__attribute__((maybe_unused)) int foo() {\n"
8899                    "  //...\n"
8900                    "}"));
8901 
8902   EXPECT_EQ("MACRO\n\n"
8903             "[[nodiscard]] int foo() {\n"
8904             "  //...\n"
8905             "}",
8906             format("MACRO\n\n"
8907                    "[[nodiscard]] int foo() {\n"
8908                    "  //...\n"
8909                    "}"));
8910 }
8911 
8912 TEST_F(FormatTest, AttributePenaltyBreaking) {
8913   FormatStyle Style = getLLVMStyle();
8914   verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
8915                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
8916                Style);
8917   verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
8918                "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
8919                Style);
8920   verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
8921                "shared_ptr<ALongTypeName> &C d) {\n}",
8922                Style);
8923 }
8924 
8925 TEST_F(FormatTest, UnderstandsEllipsis) {
8926   FormatStyle Style = getLLVMStyle();
8927   verifyFormat("int printf(const char *fmt, ...);");
8928   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
8929   verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
8930 
8931   verifyFormat("template <int *...PP> a;", Style);
8932 
8933   Style.PointerAlignment = FormatStyle::PAS_Left;
8934   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
8935 
8936   verifyFormat("template <int*... PP> a;", Style);
8937 
8938   Style.PointerAlignment = FormatStyle::PAS_Middle;
8939   verifyFormat("template <int *... PP> a;", Style);
8940 }
8941 
8942 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
8943   EXPECT_EQ("int *a;\n"
8944             "int *a;\n"
8945             "int *a;",
8946             format("int *a;\n"
8947                    "int* a;\n"
8948                    "int *a;",
8949                    getGoogleStyle()));
8950   EXPECT_EQ("int* a;\n"
8951             "int* a;\n"
8952             "int* a;",
8953             format("int* a;\n"
8954                    "int* a;\n"
8955                    "int *a;",
8956                    getGoogleStyle()));
8957   EXPECT_EQ("int *a;\n"
8958             "int *a;\n"
8959             "int *a;",
8960             format("int *a;\n"
8961                    "int * a;\n"
8962                    "int *  a;",
8963                    getGoogleStyle()));
8964   EXPECT_EQ("auto x = [] {\n"
8965             "  int *a;\n"
8966             "  int *a;\n"
8967             "  int *a;\n"
8968             "};",
8969             format("auto x=[]{int *a;\n"
8970                    "int * a;\n"
8971                    "int *  a;};",
8972                    getGoogleStyle()));
8973 }
8974 
8975 TEST_F(FormatTest, UnderstandsRvalueReferences) {
8976   verifyFormat("int f(int &&a) {}");
8977   verifyFormat("int f(int a, char &&b) {}");
8978   verifyFormat("void f() { int &&a = b; }");
8979   verifyGoogleFormat("int f(int a, char&& b) {}");
8980   verifyGoogleFormat("void f() { int&& a = b; }");
8981 
8982   verifyIndependentOfContext("A<int &&> a;");
8983   verifyIndependentOfContext("A<int &&, int &&> a;");
8984   verifyGoogleFormat("A<int&&> a;");
8985   verifyGoogleFormat("A<int&&, int&&> a;");
8986 
8987   // Not rvalue references:
8988   verifyFormat("template <bool B, bool C> class A {\n"
8989                "  static_assert(B && C, \"Something is wrong\");\n"
8990                "};");
8991   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
8992   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
8993   verifyFormat("#define A(a, b) (a && b)");
8994 }
8995 
8996 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
8997   verifyFormat("void f() {\n"
8998                "  x[aaaaaaaaa -\n"
8999                "    b] = 23;\n"
9000                "}",
9001                getLLVMStyleWithColumns(15));
9002 }
9003 
9004 TEST_F(FormatTest, FormatsCasts) {
9005   verifyFormat("Type *A = static_cast<Type *>(P);");
9006   verifyFormat("Type *A = (Type *)P;");
9007   verifyFormat("Type *A = (vector<Type *, int *>)P;");
9008   verifyFormat("int a = (int)(2.0f);");
9009   verifyFormat("int a = (int)2.0f;");
9010   verifyFormat("x[(int32)y];");
9011   verifyFormat("x = (int32)y;");
9012   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
9013   verifyFormat("int a = (int)*b;");
9014   verifyFormat("int a = (int)2.0f;");
9015   verifyFormat("int a = (int)~0;");
9016   verifyFormat("int a = (int)++a;");
9017   verifyFormat("int a = (int)sizeof(int);");
9018   verifyFormat("int a = (int)+2;");
9019   verifyFormat("my_int a = (my_int)2.0f;");
9020   verifyFormat("my_int a = (my_int)sizeof(int);");
9021   verifyFormat("return (my_int)aaa;");
9022   verifyFormat("#define x ((int)-1)");
9023   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
9024   verifyFormat("#define p(q) ((int *)&q)");
9025   verifyFormat("fn(a)(b) + 1;");
9026 
9027   verifyFormat("void f() { my_int a = (my_int)*b; }");
9028   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
9029   verifyFormat("my_int a = (my_int)~0;");
9030   verifyFormat("my_int a = (my_int)++a;");
9031   verifyFormat("my_int a = (my_int)-2;");
9032   verifyFormat("my_int a = (my_int)1;");
9033   verifyFormat("my_int a = (my_int *)1;");
9034   verifyFormat("my_int a = (const my_int)-1;");
9035   verifyFormat("my_int a = (const my_int *)-1;");
9036   verifyFormat("my_int a = (my_int)(my_int)-1;");
9037   verifyFormat("my_int a = (ns::my_int)-2;");
9038   verifyFormat("case (my_int)ONE:");
9039   verifyFormat("auto x = (X)this;");
9040   // Casts in Obj-C style calls used to not be recognized as such.
9041   verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
9042 
9043   // FIXME: single value wrapped with paren will be treated as cast.
9044   verifyFormat("void f(int i = (kValue)*kMask) {}");
9045 
9046   verifyFormat("{ (void)F; }");
9047 
9048   // Don't break after a cast's
9049   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9050                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
9051                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
9052 
9053   // These are not casts.
9054   verifyFormat("void f(int *) {}");
9055   verifyFormat("f(foo)->b;");
9056   verifyFormat("f(foo).b;");
9057   verifyFormat("f(foo)(b);");
9058   verifyFormat("f(foo)[b];");
9059   verifyFormat("[](foo) { return 4; }(bar);");
9060   verifyFormat("(*funptr)(foo)[4];");
9061   verifyFormat("funptrs[4](foo)[4];");
9062   verifyFormat("void f(int *);");
9063   verifyFormat("void f(int *) = 0;");
9064   verifyFormat("void f(SmallVector<int>) {}");
9065   verifyFormat("void f(SmallVector<int>);");
9066   verifyFormat("void f(SmallVector<int>) = 0;");
9067   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
9068   verifyFormat("int a = sizeof(int) * b;");
9069   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
9070   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
9071   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
9072   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
9073 
9074   // These are not casts, but at some point were confused with casts.
9075   verifyFormat("virtual void foo(int *) override;");
9076   verifyFormat("virtual void foo(char &) const;");
9077   verifyFormat("virtual void foo(int *a, char *) const;");
9078   verifyFormat("int a = sizeof(int *) + b;");
9079   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
9080   verifyFormat("bool b = f(g<int>) && c;");
9081   verifyFormat("typedef void (*f)(int i) func;");
9082   verifyFormat("void operator++(int) noexcept;");
9083   verifyFormat("void operator++(int &) noexcept;");
9084   verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
9085                "&) noexcept;");
9086   verifyFormat(
9087       "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
9088   verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
9089   verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
9090   verifyFormat("void operator delete(nothrow_t &) noexcept;");
9091   verifyFormat("void operator delete(foo &) noexcept;");
9092   verifyFormat("void operator delete(foo) noexcept;");
9093   verifyFormat("void operator delete(int) noexcept;");
9094   verifyFormat("void operator delete(int &) noexcept;");
9095   verifyFormat("void operator delete(int &) volatile noexcept;");
9096   verifyFormat("void operator delete(int &) const");
9097   verifyFormat("void operator delete(int &) = default");
9098   verifyFormat("void operator delete(int &) = delete");
9099   verifyFormat("void operator delete(int &) [[noreturn]]");
9100   verifyFormat("void operator delete(int &) throw();");
9101   verifyFormat("void operator delete(int &) throw(int);");
9102   verifyFormat("auto operator delete(int &) -> int;");
9103   verifyFormat("auto operator delete(int &) override");
9104   verifyFormat("auto operator delete(int &) final");
9105 
9106   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
9107                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
9108   // FIXME: The indentation here is not ideal.
9109   verifyFormat(
9110       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9111       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
9112       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
9113 }
9114 
9115 TEST_F(FormatTest, FormatsFunctionTypes) {
9116   verifyFormat("A<bool()> a;");
9117   verifyFormat("A<SomeType()> a;");
9118   verifyFormat("A<void (*)(int, std::string)> a;");
9119   verifyFormat("A<void *(int)>;");
9120   verifyFormat("void *(*a)(int *, SomeType *);");
9121   verifyFormat("int (*func)(void *);");
9122   verifyFormat("void f() { int (*func)(void *); }");
9123   verifyFormat("template <class CallbackClass>\n"
9124                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
9125 
9126   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
9127   verifyGoogleFormat("void* (*a)(int);");
9128   verifyGoogleFormat(
9129       "template <class CallbackClass>\n"
9130       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
9131 
9132   // Other constructs can look somewhat like function types:
9133   verifyFormat("A<sizeof(*x)> a;");
9134   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
9135   verifyFormat("some_var = function(*some_pointer_var)[0];");
9136   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
9137   verifyFormat("int x = f(&h)();");
9138   verifyFormat("returnsFunction(&param1, &param2)(param);");
9139   verifyFormat("std::function<\n"
9140                "    LooooooooooongTemplatedType<\n"
9141                "        SomeType>*(\n"
9142                "        LooooooooooooooooongType type)>\n"
9143                "    function;",
9144                getGoogleStyleWithColumns(40));
9145 }
9146 
9147 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
9148   verifyFormat("A (*foo_)[6];");
9149   verifyFormat("vector<int> (*foo_)[6];");
9150 }
9151 
9152 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
9153   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9154                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9155   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
9156                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
9157   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9158                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
9159 
9160   // Different ways of ()-initializiation.
9161   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9162                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
9163   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9164                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
9165   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9166                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
9167   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
9168                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
9169 
9170   // Lambdas should not confuse the variable declaration heuristic.
9171   verifyFormat("LooooooooooooooooongType\n"
9172                "    variable(nullptr, [](A *a) {});",
9173                getLLVMStyleWithColumns(40));
9174 }
9175 
9176 TEST_F(FormatTest, BreaksLongDeclarations) {
9177   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
9178                "    AnotherNameForTheLongType;");
9179   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
9180                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9181   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9182                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
9183   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
9184                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
9185   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9186                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9187   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
9188                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9189   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
9190                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9191   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
9192                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9193   verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
9194                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9195   verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
9196                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9197   verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
9198                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
9199   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9200                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
9201   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9202                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
9203   FormatStyle Indented = getLLVMStyle();
9204   Indented.IndentWrappedFunctionNames = true;
9205   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9206                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
9207                Indented);
9208   verifyFormat(
9209       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
9210       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9211       Indented);
9212   verifyFormat(
9213       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
9214       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9215       Indented);
9216   verifyFormat(
9217       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
9218       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
9219       Indented);
9220 
9221   // FIXME: Without the comment, this breaks after "(".
9222   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
9223                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
9224                getGoogleStyle());
9225 
9226   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
9227                "                  int LoooooooooooooooooooongParam2) {}");
9228   verifyFormat(
9229       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
9230       "                                   SourceLocation L, IdentifierIn *II,\n"
9231       "                                   Type *T) {}");
9232   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
9233                "ReallyReaaallyLongFunctionName(\n"
9234                "    const std::string &SomeParameter,\n"
9235                "    const SomeType<string, SomeOtherTemplateParameter>\n"
9236                "        &ReallyReallyLongParameterName,\n"
9237                "    const SomeType<string, SomeOtherTemplateParameter>\n"
9238                "        &AnotherLongParameterName) {}");
9239   verifyFormat("template <typename A>\n"
9240                "SomeLoooooooooooooooooooooongType<\n"
9241                "    typename some_namespace::SomeOtherType<A>::Type>\n"
9242                "Function() {}");
9243 
9244   verifyGoogleFormat(
9245       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
9246       "    aaaaaaaaaaaaaaaaaaaaaaa;");
9247   verifyGoogleFormat(
9248       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
9249       "                                   SourceLocation L) {}");
9250   verifyGoogleFormat(
9251       "some_namespace::LongReturnType\n"
9252       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
9253       "    int first_long_parameter, int second_parameter) {}");
9254 
9255   verifyGoogleFormat("template <typename T>\n"
9256                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
9257                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
9258   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9259                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
9260 
9261   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
9262                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9263                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9264   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9265                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
9266                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
9267   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9268                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9269                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
9270                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9271 
9272   verifyFormat("template <typename T> // Templates on own line.\n"
9273                "static int            // Some comment.\n"
9274                "MyFunction(int a);",
9275                getLLVMStyle());
9276 }
9277 
9278 TEST_F(FormatTest, FormatsAccessModifiers) {
9279   FormatStyle Style = getLLVMStyle();
9280   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
9281             FormatStyle::ELBAMS_LogicalBlock);
9282   verifyFormat("struct foo {\n"
9283                "private:\n"
9284                "  void f() {}\n"
9285                "\n"
9286                "private:\n"
9287                "  int i;\n"
9288                "\n"
9289                "protected:\n"
9290                "  int j;\n"
9291                "};\n",
9292                Style);
9293   verifyFormat("struct foo {\n"
9294                "private:\n"
9295                "  void f() {}\n"
9296                "\n"
9297                "private:\n"
9298                "  int i;\n"
9299                "\n"
9300                "protected:\n"
9301                "  int j;\n"
9302                "};\n",
9303                "struct foo {\n"
9304                "private:\n"
9305                "  void f() {}\n"
9306                "private:\n"
9307                "  int i;\n"
9308                "protected:\n"
9309                "  int j;\n"
9310                "};\n",
9311                Style);
9312   verifyFormat("struct foo { /* comment */\n"
9313                "private:\n"
9314                "  int i;\n"
9315                "  // comment\n"
9316                "private:\n"
9317                "  int j;\n"
9318                "};\n",
9319                Style);
9320   verifyFormat("struct foo {\n"
9321                "#ifdef FOO\n"
9322                "#endif\n"
9323                "private:\n"
9324                "  int i;\n"
9325                "#ifdef FOO\n"
9326                "private:\n"
9327                "#endif\n"
9328                "  int j;\n"
9329                "};\n",
9330                Style);
9331   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
9332   verifyFormat("struct foo {\n"
9333                "private:\n"
9334                "  void f() {}\n"
9335                "private:\n"
9336                "  int i;\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                "private:\n"
9345                "  int i;\n"
9346                "protected:\n"
9347                "  int j;\n"
9348                "};\n",
9349                "struct foo {\n"
9350                "\n"
9351                "private:\n"
9352                "  void f() {}\n"
9353                "\n"
9354                "private:\n"
9355                "  int i;\n"
9356                "\n"
9357                "protected:\n"
9358                "  int j;\n"
9359                "};\n",
9360                Style);
9361   verifyFormat("struct foo { /* comment */\n"
9362                "private:\n"
9363                "  int i;\n"
9364                "  // comment\n"
9365                "private:\n"
9366                "  int j;\n"
9367                "};\n",
9368                "struct foo { /* comment */\n"
9369                "\n"
9370                "private:\n"
9371                "  int i;\n"
9372                "  // comment\n"
9373                "\n"
9374                "private:\n"
9375                "  int j;\n"
9376                "};\n",
9377                Style);
9378   verifyFormat("struct foo {\n"
9379                "#ifdef FOO\n"
9380                "#endif\n"
9381                "private:\n"
9382                "  int i;\n"
9383                "#ifdef FOO\n"
9384                "private:\n"
9385                "#endif\n"
9386                "  int j;\n"
9387                "};\n",
9388                "struct foo {\n"
9389                "#ifdef FOO\n"
9390                "#endif\n"
9391                "\n"
9392                "private:\n"
9393                "  int i;\n"
9394                "#ifdef FOO\n"
9395                "\n"
9396                "private:\n"
9397                "#endif\n"
9398                "  int j;\n"
9399                "};\n",
9400                Style);
9401   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
9402   verifyFormat("struct foo {\n"
9403                "private:\n"
9404                "  void f() {}\n"
9405                "\n"
9406                "private:\n"
9407                "  int i;\n"
9408                "\n"
9409                "protected:\n"
9410                "  int j;\n"
9411                "};\n",
9412                Style);
9413   verifyFormat("struct foo {\n"
9414                "private:\n"
9415                "  void f() {}\n"
9416                "\n"
9417                "private:\n"
9418                "  int i;\n"
9419                "\n"
9420                "protected:\n"
9421                "  int j;\n"
9422                "};\n",
9423                "struct foo {\n"
9424                "private:\n"
9425                "  void f() {}\n"
9426                "private:\n"
9427                "  int i;\n"
9428                "protected:\n"
9429                "  int j;\n"
9430                "};\n",
9431                Style);
9432   verifyFormat("struct foo { /* comment */\n"
9433                "private:\n"
9434                "  int i;\n"
9435                "  // comment\n"
9436                "\n"
9437                "private:\n"
9438                "  int j;\n"
9439                "};\n",
9440                "struct foo { /* comment */\n"
9441                "private:\n"
9442                "  int i;\n"
9443                "  // comment\n"
9444                "\n"
9445                "private:\n"
9446                "  int j;\n"
9447                "};\n",
9448                Style);
9449   verifyFormat("struct foo {\n"
9450                "#ifdef FOO\n"
9451                "#endif\n"
9452                "\n"
9453                "private:\n"
9454                "  int i;\n"
9455                "#ifdef FOO\n"
9456                "\n"
9457                "private:\n"
9458                "#endif\n"
9459                "  int j;\n"
9460                "};\n",
9461                "struct foo {\n"
9462                "#ifdef FOO\n"
9463                "#endif\n"
9464                "private:\n"
9465                "  int i;\n"
9466                "#ifdef FOO\n"
9467                "private:\n"
9468                "#endif\n"
9469                "  int j;\n"
9470                "};\n",
9471                Style);
9472   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
9473   EXPECT_EQ("struct foo {\n"
9474             "\n"
9475             "private:\n"
9476             "  void f() {}\n"
9477             "\n"
9478             "private:\n"
9479             "  int i;\n"
9480             "\n"
9481             "protected:\n"
9482             "  int j;\n"
9483             "};\n",
9484             format("struct foo {\n"
9485                    "\n"
9486                    "private:\n"
9487                    "  void f() {}\n"
9488                    "\n"
9489                    "private:\n"
9490                    "  int i;\n"
9491                    "\n"
9492                    "protected:\n"
9493                    "  int j;\n"
9494                    "};\n",
9495                    Style));
9496   verifyFormat("struct foo {\n"
9497                "private:\n"
9498                "  void f() {}\n"
9499                "private:\n"
9500                "  int i;\n"
9501                "protected:\n"
9502                "  int j;\n"
9503                "};\n",
9504                Style);
9505   EXPECT_EQ("struct foo { /* comment */\n"
9506             "\n"
9507             "private:\n"
9508             "  int i;\n"
9509             "  // comment\n"
9510             "\n"
9511             "private:\n"
9512             "  int j;\n"
9513             "};\n",
9514             format("struct foo { /* comment */\n"
9515                    "\n"
9516                    "private:\n"
9517                    "  int i;\n"
9518                    "  // comment\n"
9519                    "\n"
9520                    "private:\n"
9521                    "  int j;\n"
9522                    "};\n",
9523                    Style));
9524   verifyFormat("struct foo { /* comment */\n"
9525                "private:\n"
9526                "  int i;\n"
9527                "  // comment\n"
9528                "private:\n"
9529                "  int j;\n"
9530                "};\n",
9531                Style);
9532   EXPECT_EQ("struct foo {\n"
9533             "#ifdef FOO\n"
9534             "#endif\n"
9535             "\n"
9536             "private:\n"
9537             "  int i;\n"
9538             "#ifdef FOO\n"
9539             "\n"
9540             "private:\n"
9541             "#endif\n"
9542             "  int j;\n"
9543             "};\n",
9544             format("struct foo {\n"
9545                    "#ifdef FOO\n"
9546                    "#endif\n"
9547                    "\n"
9548                    "private:\n"
9549                    "  int i;\n"
9550                    "#ifdef FOO\n"
9551                    "\n"
9552                    "private:\n"
9553                    "#endif\n"
9554                    "  int j;\n"
9555                    "};\n",
9556                    Style));
9557   verifyFormat("struct foo {\n"
9558                "#ifdef FOO\n"
9559                "#endif\n"
9560                "private:\n"
9561                "  int i;\n"
9562                "#ifdef FOO\n"
9563                "private:\n"
9564                "#endif\n"
9565                "  int j;\n"
9566                "};\n",
9567                Style);
9568 
9569   FormatStyle NoEmptyLines = getLLVMStyle();
9570   NoEmptyLines.MaxEmptyLinesToKeep = 0;
9571   verifyFormat("struct foo {\n"
9572                "private:\n"
9573                "  void f() {}\n"
9574                "\n"
9575                "private:\n"
9576                "  int i;\n"
9577                "\n"
9578                "public:\n"
9579                "protected:\n"
9580                "  int j;\n"
9581                "};\n",
9582                NoEmptyLines);
9583 
9584   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
9585   verifyFormat("struct foo {\n"
9586                "private:\n"
9587                "  void f() {}\n"
9588                "private:\n"
9589                "  int i;\n"
9590                "public:\n"
9591                "protected:\n"
9592                "  int j;\n"
9593                "};\n",
9594                NoEmptyLines);
9595 
9596   NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
9597   verifyFormat("struct foo {\n"
9598                "private:\n"
9599                "  void f() {}\n"
9600                "\n"
9601                "private:\n"
9602                "  int i;\n"
9603                "\n"
9604                "public:\n"
9605                "\n"
9606                "protected:\n"
9607                "  int j;\n"
9608                "};\n",
9609                NoEmptyLines);
9610 }
9611 
9612 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
9613 
9614   FormatStyle Style = getLLVMStyle();
9615   EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
9616   verifyFormat("struct foo {\n"
9617                "private:\n"
9618                "  void f() {}\n"
9619                "\n"
9620                "private:\n"
9621                "  int i;\n"
9622                "\n"
9623                "protected:\n"
9624                "  int j;\n"
9625                "};\n",
9626                Style);
9627 
9628   // Check if lines are removed.
9629   verifyFormat("struct foo {\n"
9630                "private:\n"
9631                "  void f() {}\n"
9632                "\n"
9633                "private:\n"
9634                "  int i;\n"
9635                "\n"
9636                "protected:\n"
9637                "  int j;\n"
9638                "};\n",
9639                "struct foo {\n"
9640                "private:\n"
9641                "\n"
9642                "  void f() {}\n"
9643                "\n"
9644                "private:\n"
9645                "\n"
9646                "  int i;\n"
9647                "\n"
9648                "protected:\n"
9649                "\n"
9650                "  int j;\n"
9651                "};\n",
9652                Style);
9653 
9654   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9655   verifyFormat("struct foo {\n"
9656                "private:\n"
9657                "\n"
9658                "  void f() {}\n"
9659                "\n"
9660                "private:\n"
9661                "\n"
9662                "  int i;\n"
9663                "\n"
9664                "protected:\n"
9665                "\n"
9666                "  int j;\n"
9667                "};\n",
9668                Style);
9669 
9670   // Check if lines are added.
9671   verifyFormat("struct foo {\n"
9672                "private:\n"
9673                "\n"
9674                "  void f() {}\n"
9675                "\n"
9676                "private:\n"
9677                "\n"
9678                "  int i;\n"
9679                "\n"
9680                "protected:\n"
9681                "\n"
9682                "  int j;\n"
9683                "};\n",
9684                "struct foo {\n"
9685                "private:\n"
9686                "  void f() {}\n"
9687                "\n"
9688                "private:\n"
9689                "  int i;\n"
9690                "\n"
9691                "protected:\n"
9692                "  int j;\n"
9693                "};\n",
9694                Style);
9695 
9696   // Leave tests rely on the code layout, test::messUp can not be used.
9697   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
9698   Style.MaxEmptyLinesToKeep = 0u;
9699   verifyFormat("struct foo {\n"
9700                "private:\n"
9701                "  void f() {}\n"
9702                "\n"
9703                "private:\n"
9704                "  int i;\n"
9705                "\n"
9706                "protected:\n"
9707                "  int j;\n"
9708                "};\n",
9709                Style);
9710 
9711   // Check if MaxEmptyLinesToKeep is respected.
9712   EXPECT_EQ("struct foo {\n"
9713             "private:\n"
9714             "  void f() {}\n"
9715             "\n"
9716             "private:\n"
9717             "  int i;\n"
9718             "\n"
9719             "protected:\n"
9720             "  int j;\n"
9721             "};\n",
9722             format("struct foo {\n"
9723                    "private:\n"
9724                    "\n\n\n"
9725                    "  void f() {}\n"
9726                    "\n"
9727                    "private:\n"
9728                    "\n\n\n"
9729                    "  int i;\n"
9730                    "\n"
9731                    "protected:\n"
9732                    "\n\n\n"
9733                    "  int j;\n"
9734                    "};\n",
9735                    Style));
9736 
9737   Style.MaxEmptyLinesToKeep = 1u;
9738   EXPECT_EQ("struct foo {\n"
9739             "private:\n"
9740             "\n"
9741             "  void f() {}\n"
9742             "\n"
9743             "private:\n"
9744             "\n"
9745             "  int i;\n"
9746             "\n"
9747             "protected:\n"
9748             "\n"
9749             "  int j;\n"
9750             "};\n",
9751             format("struct foo {\n"
9752                    "private:\n"
9753                    "\n"
9754                    "  void f() {}\n"
9755                    "\n"
9756                    "private:\n"
9757                    "\n"
9758                    "  int i;\n"
9759                    "\n"
9760                    "protected:\n"
9761                    "\n"
9762                    "  int j;\n"
9763                    "};\n",
9764                    Style));
9765   // Check if no lines are kept.
9766   EXPECT_EQ("struct foo {\n"
9767             "private:\n"
9768             "  void f() {}\n"
9769             "\n"
9770             "private:\n"
9771             "  int i;\n"
9772             "\n"
9773             "protected:\n"
9774             "  int j;\n"
9775             "};\n",
9776             format("struct foo {\n"
9777                    "private:\n"
9778                    "  void f() {}\n"
9779                    "\n"
9780                    "private:\n"
9781                    "  int i;\n"
9782                    "\n"
9783                    "protected:\n"
9784                    "  int j;\n"
9785                    "};\n",
9786                    Style));
9787   // Check if MaxEmptyLinesToKeep is respected.
9788   EXPECT_EQ("struct foo {\n"
9789             "private:\n"
9790             "\n"
9791             "  void f() {}\n"
9792             "\n"
9793             "private:\n"
9794             "\n"
9795             "  int i;\n"
9796             "\n"
9797             "protected:\n"
9798             "\n"
9799             "  int j;\n"
9800             "};\n",
9801             format("struct foo {\n"
9802                    "private:\n"
9803                    "\n\n\n"
9804                    "  void f() {}\n"
9805                    "\n"
9806                    "private:\n"
9807                    "\n\n\n"
9808                    "  int i;\n"
9809                    "\n"
9810                    "protected:\n"
9811                    "\n\n\n"
9812                    "  int j;\n"
9813                    "};\n",
9814                    Style));
9815 
9816   Style.MaxEmptyLinesToKeep = 10u;
9817   EXPECT_EQ("struct foo {\n"
9818             "private:\n"
9819             "\n\n\n"
9820             "  void f() {}\n"
9821             "\n"
9822             "private:\n"
9823             "\n\n\n"
9824             "  int i;\n"
9825             "\n"
9826             "protected:\n"
9827             "\n\n\n"
9828             "  int j;\n"
9829             "};\n",
9830             format("struct foo {\n"
9831                    "private:\n"
9832                    "\n\n\n"
9833                    "  void f() {}\n"
9834                    "\n"
9835                    "private:\n"
9836                    "\n\n\n"
9837                    "  int i;\n"
9838                    "\n"
9839                    "protected:\n"
9840                    "\n\n\n"
9841                    "  int j;\n"
9842                    "};\n",
9843                    Style));
9844 
9845   // Test with comments.
9846   Style = getLLVMStyle();
9847   verifyFormat("struct foo {\n"
9848                "private:\n"
9849                "  // comment\n"
9850                "  void f() {}\n"
9851                "\n"
9852                "private: /* comment */\n"
9853                "  int i;\n"
9854                "};\n",
9855                Style);
9856   verifyFormat("struct foo {\n"
9857                "private:\n"
9858                "  // comment\n"
9859                "  void f() {}\n"
9860                "\n"
9861                "private: /* comment */\n"
9862                "  int i;\n"
9863                "};\n",
9864                "struct foo {\n"
9865                "private:\n"
9866                "\n"
9867                "  // comment\n"
9868                "  void f() {}\n"
9869                "\n"
9870                "private: /* comment */\n"
9871                "\n"
9872                "  int i;\n"
9873                "};\n",
9874                Style);
9875 
9876   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9877   verifyFormat("struct foo {\n"
9878                "private:\n"
9879                "\n"
9880                "  // comment\n"
9881                "  void f() {}\n"
9882                "\n"
9883                "private: /* comment */\n"
9884                "\n"
9885                "  int i;\n"
9886                "};\n",
9887                "struct foo {\n"
9888                "private:\n"
9889                "  // comment\n"
9890                "  void f() {}\n"
9891                "\n"
9892                "private: /* comment */\n"
9893                "  int i;\n"
9894                "};\n",
9895                Style);
9896   verifyFormat("struct foo {\n"
9897                "private:\n"
9898                "\n"
9899                "  // comment\n"
9900                "  void f() {}\n"
9901                "\n"
9902                "private: /* comment */\n"
9903                "\n"
9904                "  int i;\n"
9905                "};\n",
9906                Style);
9907 
9908   // Test with preprocessor defines.
9909   Style = getLLVMStyle();
9910   verifyFormat("struct foo {\n"
9911                "private:\n"
9912                "#ifdef FOO\n"
9913                "#endif\n"
9914                "  void f() {}\n"
9915                "};\n",
9916                Style);
9917   verifyFormat("struct foo {\n"
9918                "private:\n"
9919                "#ifdef FOO\n"
9920                "#endif\n"
9921                "  void f() {}\n"
9922                "};\n",
9923                "struct foo {\n"
9924                "private:\n"
9925                "\n"
9926                "#ifdef FOO\n"
9927                "#endif\n"
9928                "  void f() {}\n"
9929                "};\n",
9930                Style);
9931 
9932   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9933   verifyFormat("struct foo {\n"
9934                "private:\n"
9935                "\n"
9936                "#ifdef FOO\n"
9937                "#endif\n"
9938                "  void f() {}\n"
9939                "};\n",
9940                "struct foo {\n"
9941                "private:\n"
9942                "#ifdef FOO\n"
9943                "#endif\n"
9944                "  void f() {}\n"
9945                "};\n",
9946                Style);
9947   verifyFormat("struct foo {\n"
9948                "private:\n"
9949                "\n"
9950                "#ifdef FOO\n"
9951                "#endif\n"
9952                "  void f() {}\n"
9953                "};\n",
9954                Style);
9955 }
9956 
9957 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
9958   // Combined tests of EmptyLineAfterAccessModifier and
9959   // EmptyLineBeforeAccessModifier.
9960   FormatStyle Style = getLLVMStyle();
9961   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
9962   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
9963   verifyFormat("struct foo {\n"
9964                "private:\n"
9965                "\n"
9966                "protected:\n"
9967                "};\n",
9968                Style);
9969 
9970   Style.MaxEmptyLinesToKeep = 10u;
9971   // Both remove all new lines.
9972   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
9973   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
9974   verifyFormat("struct foo {\n"
9975                "private:\n"
9976                "protected:\n"
9977                "};\n",
9978                "struct foo {\n"
9979                "private:\n"
9980                "\n\n\n"
9981                "protected:\n"
9982                "};\n",
9983                Style);
9984 
9985   // Leave tests rely on the code layout, test::messUp can not be used.
9986   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
9987   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
9988   Style.MaxEmptyLinesToKeep = 10u;
9989   EXPECT_EQ("struct foo {\n"
9990             "private:\n"
9991             "\n\n\n"
9992             "protected:\n"
9993             "};\n",
9994             format("struct foo {\n"
9995                    "private:\n"
9996                    "\n\n\n"
9997                    "protected:\n"
9998                    "};\n",
9999                    Style));
10000   Style.MaxEmptyLinesToKeep = 3u;
10001   EXPECT_EQ("struct foo {\n"
10002             "private:\n"
10003             "\n\n\n"
10004             "protected:\n"
10005             "};\n",
10006             format("struct foo {\n"
10007                    "private:\n"
10008                    "\n\n\n"
10009                    "protected:\n"
10010                    "};\n",
10011                    Style));
10012   Style.MaxEmptyLinesToKeep = 1u;
10013   EXPECT_EQ("struct foo {\n"
10014             "private:\n"
10015             "\n\n\n"
10016             "protected:\n"
10017             "};\n",
10018             format("struct foo {\n"
10019                    "private:\n"
10020                    "\n\n\n"
10021                    "protected:\n"
10022                    "};\n",
10023                    Style)); // Based on new lines in original document and not
10024                             // on the setting.
10025 
10026   Style.MaxEmptyLinesToKeep = 10u;
10027   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10028   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10029   // Newlines are kept if they are greater than zero,
10030   // test::messUp removes all new lines which changes the logic
10031   EXPECT_EQ("struct foo {\n"
10032             "private:\n"
10033             "\n\n\n"
10034             "protected:\n"
10035             "};\n",
10036             format("struct foo {\n"
10037                    "private:\n"
10038                    "\n\n\n"
10039                    "protected:\n"
10040                    "};\n",
10041                    Style));
10042 
10043   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10044   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10045   // test::messUp removes all new lines which changes the logic
10046   EXPECT_EQ("struct foo {\n"
10047             "private:\n"
10048             "\n\n\n"
10049             "protected:\n"
10050             "};\n",
10051             format("struct foo {\n"
10052                    "private:\n"
10053                    "\n\n\n"
10054                    "protected:\n"
10055                    "};\n",
10056                    Style));
10057 
10058   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
10059   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10060   EXPECT_EQ("struct foo {\n"
10061             "private:\n"
10062             "\n\n\n"
10063             "protected:\n"
10064             "};\n",
10065             format("struct foo {\n"
10066                    "private:\n"
10067                    "\n\n\n"
10068                    "protected:\n"
10069                    "};\n",
10070                    Style)); // test::messUp removes all new lines which changes
10071                             // the logic.
10072 
10073   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10074   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10075   verifyFormat("struct foo {\n"
10076                "private:\n"
10077                "protected:\n"
10078                "};\n",
10079                "struct foo {\n"
10080                "private:\n"
10081                "\n\n\n"
10082                "protected:\n"
10083                "};\n",
10084                Style);
10085 
10086   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
10087   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10088   EXPECT_EQ("struct foo {\n"
10089             "private:\n"
10090             "\n\n\n"
10091             "protected:\n"
10092             "};\n",
10093             format("struct foo {\n"
10094                    "private:\n"
10095                    "\n\n\n"
10096                    "protected:\n"
10097                    "};\n",
10098                    Style)); // test::messUp removes all new lines which changes
10099                             // the logic.
10100 
10101   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
10102   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10103   verifyFormat("struct foo {\n"
10104                "private:\n"
10105                "protected:\n"
10106                "};\n",
10107                "struct foo {\n"
10108                "private:\n"
10109                "\n\n\n"
10110                "protected:\n"
10111                "};\n",
10112                Style);
10113 
10114   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10115   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
10116   verifyFormat("struct foo {\n"
10117                "private:\n"
10118                "protected:\n"
10119                "};\n",
10120                "struct foo {\n"
10121                "private:\n"
10122                "\n\n\n"
10123                "protected:\n"
10124                "};\n",
10125                Style);
10126 
10127   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10128   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
10129   verifyFormat("struct foo {\n"
10130                "private:\n"
10131                "protected:\n"
10132                "};\n",
10133                "struct foo {\n"
10134                "private:\n"
10135                "\n\n\n"
10136                "protected:\n"
10137                "};\n",
10138                Style);
10139 
10140   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
10141   Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
10142   verifyFormat("struct foo {\n"
10143                "private:\n"
10144                "protected:\n"
10145                "};\n",
10146                "struct foo {\n"
10147                "private:\n"
10148                "\n\n\n"
10149                "protected:\n"
10150                "};\n",
10151                Style);
10152 }
10153 
10154 TEST_F(FormatTest, FormatsArrays) {
10155   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10156                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
10157   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
10158                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
10159   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
10160                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
10161   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10162                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
10163   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10164                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
10165   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10166                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10167                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
10168   verifyFormat(
10169       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
10170       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
10171       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
10172   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
10173                "    .aaaaaaaaaaaaaaaaaaaaaa();");
10174 
10175   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
10176                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
10177   verifyFormat(
10178       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
10179       "                                  .aaaaaaa[0]\n"
10180       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
10181   verifyFormat("a[::b::c];");
10182 
10183   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
10184 
10185   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
10186   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
10187 }
10188 
10189 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
10190   verifyFormat("(a)->b();");
10191   verifyFormat("--a;");
10192 }
10193 
10194 TEST_F(FormatTest, HandlesIncludeDirectives) {
10195   verifyFormat("#include <string>\n"
10196                "#include <a/b/c.h>\n"
10197                "#include \"a/b/string\"\n"
10198                "#include \"string.h\"\n"
10199                "#include \"string.h\"\n"
10200                "#include <a-a>\n"
10201                "#include < path with space >\n"
10202                "#include_next <test.h>"
10203                "#include \"abc.h\" // this is included for ABC\n"
10204                "#include \"some long include\" // with a comment\n"
10205                "#include \"some very long include path\"\n"
10206                "#include <some/very/long/include/path>\n",
10207                getLLVMStyleWithColumns(35));
10208   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
10209   EXPECT_EQ("#include <a>", format("#include<a>"));
10210 
10211   verifyFormat("#import <string>");
10212   verifyFormat("#import <a/b/c.h>");
10213   verifyFormat("#import \"a/b/string\"");
10214   verifyFormat("#import \"string.h\"");
10215   verifyFormat("#import \"string.h\"");
10216   verifyFormat("#if __has_include(<strstream>)\n"
10217                "#include <strstream>\n"
10218                "#endif");
10219 
10220   verifyFormat("#define MY_IMPORT <a/b>");
10221 
10222   verifyFormat("#if __has_include(<a/b>)");
10223   verifyFormat("#if __has_include_next(<a/b>)");
10224   verifyFormat("#define F __has_include(<a/b>)");
10225   verifyFormat("#define F __has_include_next(<a/b>)");
10226 
10227   // Protocol buffer definition or missing "#".
10228   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
10229                getLLVMStyleWithColumns(30));
10230 
10231   FormatStyle Style = getLLVMStyle();
10232   Style.AlwaysBreakBeforeMultilineStrings = true;
10233   Style.ColumnLimit = 0;
10234   verifyFormat("#import \"abc.h\"", Style);
10235 
10236   // But 'import' might also be a regular C++ namespace.
10237   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10238                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10239 }
10240 
10241 //===----------------------------------------------------------------------===//
10242 // Error recovery tests.
10243 //===----------------------------------------------------------------------===//
10244 
10245 TEST_F(FormatTest, IncompleteParameterLists) {
10246   FormatStyle NoBinPacking = getLLVMStyle();
10247   NoBinPacking.BinPackParameters = false;
10248   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
10249                "                        double *min_x,\n"
10250                "                        double *max_x,\n"
10251                "                        double *min_y,\n"
10252                "                        double *max_y,\n"
10253                "                        double *min_z,\n"
10254                "                        double *max_z, ) {}",
10255                NoBinPacking);
10256 }
10257 
10258 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
10259   verifyFormat("void f() { return; }\n42");
10260   verifyFormat("void f() {\n"
10261                "  if (0)\n"
10262                "    return;\n"
10263                "}\n"
10264                "42");
10265   verifyFormat("void f() { return }\n42");
10266   verifyFormat("void f() {\n"
10267                "  if (0)\n"
10268                "    return\n"
10269                "}\n"
10270                "42");
10271 }
10272 
10273 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
10274   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
10275   EXPECT_EQ("void f() {\n"
10276             "  if (a)\n"
10277             "    return\n"
10278             "}",
10279             format("void  f  (  )  {  if  ( a )  return  }"));
10280   EXPECT_EQ("namespace N {\n"
10281             "void f()\n"
10282             "}",
10283             format("namespace  N  {  void f()  }"));
10284   EXPECT_EQ("namespace N {\n"
10285             "void f() {}\n"
10286             "void g()\n"
10287             "} // namespace N",
10288             format("namespace N  { void f( ) { } void g( ) }"));
10289 }
10290 
10291 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
10292   verifyFormat("int aaaaaaaa =\n"
10293                "    // Overlylongcomment\n"
10294                "    b;",
10295                getLLVMStyleWithColumns(20));
10296   verifyFormat("function(\n"
10297                "    ShortArgument,\n"
10298                "    LoooooooooooongArgument);\n",
10299                getLLVMStyleWithColumns(20));
10300 }
10301 
10302 TEST_F(FormatTest, IncorrectAccessSpecifier) {
10303   verifyFormat("public:");
10304   verifyFormat("class A {\n"
10305                "public\n"
10306                "  void f() {}\n"
10307                "};");
10308   verifyFormat("public\n"
10309                "int qwerty;");
10310   verifyFormat("public\n"
10311                "B {}");
10312   verifyFormat("public\n"
10313                "{}");
10314   verifyFormat("public\n"
10315                "B { int x; }");
10316 }
10317 
10318 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
10319   verifyFormat("{");
10320   verifyFormat("#})");
10321   verifyNoCrash("(/**/[:!] ?[).");
10322 }
10323 
10324 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
10325   // Found by oss-fuzz:
10326   // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
10327   FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
10328   Style.ColumnLimit = 60;
10329   verifyNoCrash(
10330       "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
10331       "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
10332       "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
10333       Style);
10334 }
10335 
10336 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
10337   verifyFormat("do {\n}");
10338   verifyFormat("do {\n}\n"
10339                "f();");
10340   verifyFormat("do {\n}\n"
10341                "wheeee(fun);");
10342   verifyFormat("do {\n"
10343                "  f();\n"
10344                "}");
10345 }
10346 
10347 TEST_F(FormatTest, IncorrectCodeMissingParens) {
10348   verifyFormat("if {\n  foo;\n  foo();\n}");
10349   verifyFormat("switch {\n  foo;\n  foo();\n}");
10350   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
10351   verifyFormat("while {\n  foo;\n  foo();\n}");
10352   verifyFormat("do {\n  foo;\n  foo();\n} while;");
10353 }
10354 
10355 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
10356   verifyIncompleteFormat("namespace {\n"
10357                          "class Foo { Foo (\n"
10358                          "};\n"
10359                          "} // namespace");
10360 }
10361 
10362 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
10363   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
10364   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
10365   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
10366   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
10367 
10368   EXPECT_EQ("{\n"
10369             "  {\n"
10370             "    breakme(\n"
10371             "        qwe);\n"
10372             "  }\n",
10373             format("{\n"
10374                    "    {\n"
10375                    " breakme(qwe);\n"
10376                    "}\n",
10377                    getLLVMStyleWithColumns(10)));
10378 }
10379 
10380 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
10381   verifyFormat("int x = {\n"
10382                "    avariable,\n"
10383                "    b(alongervariable)};",
10384                getLLVMStyleWithColumns(25));
10385 }
10386 
10387 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
10388   verifyFormat("return (a)(b){1, 2, 3};");
10389 }
10390 
10391 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
10392   verifyFormat("vector<int> x{1, 2, 3, 4};");
10393   verifyFormat("vector<int> x{\n"
10394                "    1,\n"
10395                "    2,\n"
10396                "    3,\n"
10397                "    4,\n"
10398                "};");
10399   verifyFormat("vector<T> x{{}, {}, {}, {}};");
10400   verifyFormat("f({1, 2});");
10401   verifyFormat("auto v = Foo{-1};");
10402   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
10403   verifyFormat("Class::Class : member{1, 2, 3} {}");
10404   verifyFormat("new vector<int>{1, 2, 3};");
10405   verifyFormat("new int[3]{1, 2, 3};");
10406   verifyFormat("new int{1};");
10407   verifyFormat("return {arg1, arg2};");
10408   verifyFormat("return {arg1, SomeType{parameter}};");
10409   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
10410   verifyFormat("new T{arg1, arg2};");
10411   verifyFormat("f(MyMap[{composite, key}]);");
10412   verifyFormat("class Class {\n"
10413                "  T member = {arg1, arg2};\n"
10414                "};");
10415   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
10416   verifyFormat("const struct A a = {.a = 1, .b = 2};");
10417   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
10418   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
10419   verifyFormat("int a = std::is_integral<int>{} + 0;");
10420 
10421   verifyFormat("int foo(int i) { return fo1{}(i); }");
10422   verifyFormat("int foo(int i) { return fo1{}(i); }");
10423   verifyFormat("auto i = decltype(x){};");
10424   verifyFormat("auto i = typeof(x){};");
10425   verifyFormat("auto i = _Atomic(x){};");
10426   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
10427   verifyFormat("Node n{1, Node{1000}, //\n"
10428                "       2};");
10429   verifyFormat("Aaaa aaaaaaa{\n"
10430                "    {\n"
10431                "        aaaa,\n"
10432                "    },\n"
10433                "};");
10434   verifyFormat("class C : public D {\n"
10435                "  SomeClass SC{2};\n"
10436                "};");
10437   verifyFormat("class C : public A {\n"
10438                "  class D : public B {\n"
10439                "    void f() { int i{2}; }\n"
10440                "  };\n"
10441                "};");
10442   verifyFormat("#define A {a, a},");
10443 
10444   // Avoid breaking between equal sign and opening brace
10445   FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
10446   AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
10447   verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
10448                "    {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
10449                "     {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
10450                "     {\"ccccccccccccccccccccc\", 2}};",
10451                AvoidBreakingFirstArgument);
10452 
10453   // Binpacking only if there is no trailing comma
10454   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
10455                "                      cccccccccc, dddddddddd};",
10456                getLLVMStyleWithColumns(50));
10457   verifyFormat("const Aaaaaa aaaaa = {\n"
10458                "    aaaaaaaaaaa,\n"
10459                "    bbbbbbbbbbb,\n"
10460                "    ccccccccccc,\n"
10461                "    ddddddddddd,\n"
10462                "};",
10463                getLLVMStyleWithColumns(50));
10464 
10465   // Cases where distinguising braced lists and blocks is hard.
10466   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
10467   verifyFormat("void f() {\n"
10468                "  return; // comment\n"
10469                "}\n"
10470                "SomeType t;");
10471   verifyFormat("void f() {\n"
10472                "  if (a) {\n"
10473                "    f();\n"
10474                "  }\n"
10475                "}\n"
10476                "SomeType t;");
10477 
10478   // In combination with BinPackArguments = false.
10479   FormatStyle NoBinPacking = getLLVMStyle();
10480   NoBinPacking.BinPackArguments = false;
10481   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
10482                "                      bbbbb,\n"
10483                "                      ccccc,\n"
10484                "                      ddddd,\n"
10485                "                      eeeee,\n"
10486                "                      ffffff,\n"
10487                "                      ggggg,\n"
10488                "                      hhhhhh,\n"
10489                "                      iiiiii,\n"
10490                "                      jjjjjj,\n"
10491                "                      kkkkkk};",
10492                NoBinPacking);
10493   verifyFormat("const Aaaaaa aaaaa = {\n"
10494                "    aaaaa,\n"
10495                "    bbbbb,\n"
10496                "    ccccc,\n"
10497                "    ddddd,\n"
10498                "    eeeee,\n"
10499                "    ffffff,\n"
10500                "    ggggg,\n"
10501                "    hhhhhh,\n"
10502                "    iiiiii,\n"
10503                "    jjjjjj,\n"
10504                "    kkkkkk,\n"
10505                "};",
10506                NoBinPacking);
10507   verifyFormat(
10508       "const Aaaaaa aaaaa = {\n"
10509       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
10510       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
10511       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
10512       "};",
10513       NoBinPacking);
10514 
10515   NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10516   EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
10517             "    CDDDP83848_BMCR_REGISTER,\n"
10518             "    CDDDP83848_BMSR_REGISTER,\n"
10519             "    CDDDP83848_RBR_REGISTER};",
10520             format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
10521                    "                                CDDDP83848_BMSR_REGISTER,\n"
10522                    "                                CDDDP83848_RBR_REGISTER};",
10523                    NoBinPacking));
10524 
10525   // FIXME: The alignment of these trailing comments might be bad. Then again,
10526   // this might be utterly useless in real code.
10527   verifyFormat("Constructor::Constructor()\n"
10528                "    : some_value{         //\n"
10529                "                 aaaaaaa, //\n"
10530                "                 bbbbbbb} {}");
10531 
10532   // In braced lists, the first comment is always assumed to belong to the
10533   // first element. Thus, it can be moved to the next or previous line as
10534   // appropriate.
10535   EXPECT_EQ("function({// First element:\n"
10536             "          1,\n"
10537             "          // Second element:\n"
10538             "          2});",
10539             format("function({\n"
10540                    "    // First element:\n"
10541                    "    1,\n"
10542                    "    // Second element:\n"
10543                    "    2});"));
10544   EXPECT_EQ("std::vector<int> MyNumbers{\n"
10545             "    // First element:\n"
10546             "    1,\n"
10547             "    // Second element:\n"
10548             "    2};",
10549             format("std::vector<int> MyNumbers{// First element:\n"
10550                    "                           1,\n"
10551                    "                           // Second element:\n"
10552                    "                           2};",
10553                    getLLVMStyleWithColumns(30)));
10554   // A trailing comma should still lead to an enforced line break and no
10555   // binpacking.
10556   EXPECT_EQ("vector<int> SomeVector = {\n"
10557             "    // aaa\n"
10558             "    1,\n"
10559             "    2,\n"
10560             "};",
10561             format("vector<int> SomeVector = { // aaa\n"
10562                    "    1, 2, };"));
10563 
10564   // C++11 brace initializer list l-braces should not be treated any differently
10565   // when breaking before lambda bodies is enabled
10566   FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
10567   BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
10568   BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
10569   BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
10570   verifyFormat(
10571       "std::runtime_error{\n"
10572       "    \"Long string which will force a break onto the next line...\"};",
10573       BreakBeforeLambdaBody);
10574 
10575   FormatStyle ExtraSpaces = getLLVMStyle();
10576   ExtraSpaces.Cpp11BracedListStyle = false;
10577   ExtraSpaces.ColumnLimit = 75;
10578   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
10579   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
10580   verifyFormat("f({ 1, 2 });", ExtraSpaces);
10581   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
10582   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
10583   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
10584   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
10585   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
10586   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
10587   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
10588   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
10589   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
10590   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
10591   verifyFormat("class Class {\n"
10592                "  T member = { arg1, arg2 };\n"
10593                "};",
10594                ExtraSpaces);
10595   verifyFormat(
10596       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10597       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
10598       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
10599       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
10600       ExtraSpaces);
10601   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
10602   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
10603                ExtraSpaces);
10604   verifyFormat(
10605       "someFunction(OtherParam,\n"
10606       "             BracedList{ // comment 1 (Forcing interesting break)\n"
10607       "                         param1, param2,\n"
10608       "                         // comment 2\n"
10609       "                         param3, param4 });",
10610       ExtraSpaces);
10611   verifyFormat(
10612       "std::this_thread::sleep_for(\n"
10613       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
10614       ExtraSpaces);
10615   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
10616                "    aaaaaaa,\n"
10617                "    aaaaaaaaaa,\n"
10618                "    aaaaa,\n"
10619                "    aaaaaaaaaaaaaaa,\n"
10620                "    aaa,\n"
10621                "    aaaaaaaaaa,\n"
10622                "    a,\n"
10623                "    aaaaaaaaaaaaaaaaaaaaa,\n"
10624                "    aaaaaaaaaaaa,\n"
10625                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
10626                "    aaaaaaa,\n"
10627                "    a};");
10628   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
10629   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
10630   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
10631 
10632   // Avoid breaking between initializer/equal sign and opening brace
10633   ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
10634   verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
10635                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
10636                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
10637                "  { \"ccccccccccccccccccccc\", 2 }\n"
10638                "};",
10639                ExtraSpaces);
10640   verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
10641                "  { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
10642                "  { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
10643                "  { \"ccccccccccccccccccccc\", 2 }\n"
10644                "};",
10645                ExtraSpaces);
10646 
10647   FormatStyle SpaceBeforeBrace = getLLVMStyle();
10648   SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
10649   verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
10650   verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
10651 
10652   FormatStyle SpaceBetweenBraces = getLLVMStyle();
10653   SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
10654   SpaceBetweenBraces.SpacesInParentheses = true;
10655   SpaceBetweenBraces.SpacesInSquareBrackets = true;
10656   verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
10657   verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
10658   verifyFormat("vector< int > x{ // comment 1\n"
10659                "                 1, 2, 3, 4 };",
10660                SpaceBetweenBraces);
10661   SpaceBetweenBraces.ColumnLimit = 20;
10662   EXPECT_EQ("vector< int > x{\n"
10663             "    1, 2, 3, 4 };",
10664             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
10665   SpaceBetweenBraces.ColumnLimit = 24;
10666   EXPECT_EQ("vector< int > x{ 1, 2,\n"
10667             "                 3, 4 };",
10668             format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
10669   EXPECT_EQ("vector< int > x{\n"
10670             "    1,\n"
10671             "    2,\n"
10672             "    3,\n"
10673             "    4,\n"
10674             "};",
10675             format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
10676   verifyFormat("vector< int > x{};", SpaceBetweenBraces);
10677   SpaceBetweenBraces.SpaceInEmptyParentheses = true;
10678   verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
10679 }
10680 
10681 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
10682   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10683                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10684                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10685                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10686                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10687                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
10688   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
10689                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10690                "                 1, 22, 333, 4444, 55555, //\n"
10691                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10692                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
10693   verifyFormat(
10694       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
10695       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
10696       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
10697       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
10698       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
10699       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
10700       "                 7777777};");
10701   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
10702                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
10703                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
10704   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
10705                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
10706                "    // Separating comment.\n"
10707                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
10708   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
10709                "    // Leading comment\n"
10710                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
10711                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
10712   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
10713                "                 1, 1, 1, 1};",
10714                getLLVMStyleWithColumns(39));
10715   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
10716                "                 1, 1, 1, 1};",
10717                getLLVMStyleWithColumns(38));
10718   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
10719                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
10720                getLLVMStyleWithColumns(43));
10721   verifyFormat(
10722       "static unsigned SomeValues[10][3] = {\n"
10723       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
10724       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
10725   verifyFormat("static auto fields = new vector<string>{\n"
10726                "    \"aaaaaaaaaaaaa\",\n"
10727                "    \"aaaaaaaaaaaaa\",\n"
10728                "    \"aaaaaaaaaaaa\",\n"
10729                "    \"aaaaaaaaaaaaaa\",\n"
10730                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
10731                "    \"aaaaaaaaaaaa\",\n"
10732                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
10733                "};");
10734   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
10735   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
10736                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
10737                "                 3, cccccccccccccccccccccc};",
10738                getLLVMStyleWithColumns(60));
10739 
10740   // Trailing commas.
10741   verifyFormat("vector<int> x = {\n"
10742                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
10743                "};",
10744                getLLVMStyleWithColumns(39));
10745   verifyFormat("vector<int> x = {\n"
10746                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
10747                "};",
10748                getLLVMStyleWithColumns(39));
10749   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
10750                "                 1, 1, 1, 1,\n"
10751                "                 /**/ /**/};",
10752                getLLVMStyleWithColumns(39));
10753 
10754   // Trailing comment in the first line.
10755   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
10756                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
10757                "    111111111,  222222222,  3333333333,  444444444,  //\n"
10758                "    11111111,   22222222,   333333333,   44444444};");
10759   // Trailing comment in the last line.
10760   verifyFormat("int aaaaa[] = {\n"
10761                "    1, 2, 3, // comment\n"
10762                "    4, 5, 6  // comment\n"
10763                "};");
10764 
10765   // With nested lists, we should either format one item per line or all nested
10766   // lists one on line.
10767   // FIXME: For some nested lists, we can do better.
10768   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
10769                "        {aaaaaaaaaaaaaaaaaaa},\n"
10770                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
10771                "        {aaaaaaaaaaaaaaaaa}};",
10772                getLLVMStyleWithColumns(60));
10773   verifyFormat(
10774       "SomeStruct my_struct_array = {\n"
10775       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
10776       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
10777       "    {aaa, aaa},\n"
10778       "    {aaa, aaa},\n"
10779       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
10780       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
10781       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
10782 
10783   // No column layout should be used here.
10784   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
10785                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
10786 
10787   verifyNoCrash("a<,");
10788 
10789   // No braced initializer here.
10790   verifyFormat("void f() {\n"
10791                "  struct Dummy {};\n"
10792                "  f(v);\n"
10793                "}");
10794 
10795   // Long lists should be formatted in columns even if they are nested.
10796   verifyFormat(
10797       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10798       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10799       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10800       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10801       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
10802       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
10803 
10804   // Allow "single-column" layout even if that violates the column limit. There
10805   // isn't going to be a better way.
10806   verifyFormat("std::vector<int> a = {\n"
10807                "    aaaaaaaa,\n"
10808                "    aaaaaaaa,\n"
10809                "    aaaaaaaa,\n"
10810                "    aaaaaaaa,\n"
10811                "    aaaaaaaaaa,\n"
10812                "    aaaaaaaa,\n"
10813                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
10814                getLLVMStyleWithColumns(30));
10815   verifyFormat("vector<int> aaaa = {\n"
10816                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10817                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10818                "    aaaaaa.aaaaaaa,\n"
10819                "    aaaaaa.aaaaaaa,\n"
10820                "    aaaaaa.aaaaaaa,\n"
10821                "    aaaaaa.aaaaaaa,\n"
10822                "};");
10823 
10824   // Don't create hanging lists.
10825   verifyFormat("someFunction(Param, {List1, List2,\n"
10826                "                     List3});",
10827                getLLVMStyleWithColumns(35));
10828   verifyFormat("someFunction(Param, Param,\n"
10829                "             {List1, List2,\n"
10830                "              List3});",
10831                getLLVMStyleWithColumns(35));
10832   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
10833                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
10834 }
10835 
10836 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
10837   FormatStyle DoNotMerge = getLLVMStyle();
10838   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10839 
10840   verifyFormat("void f() { return 42; }");
10841   verifyFormat("void f() {\n"
10842                "  return 42;\n"
10843                "}",
10844                DoNotMerge);
10845   verifyFormat("void f() {\n"
10846                "  // Comment\n"
10847                "}");
10848   verifyFormat("{\n"
10849                "#error {\n"
10850                "  int a;\n"
10851                "}");
10852   verifyFormat("{\n"
10853                "  int a;\n"
10854                "#error {\n"
10855                "}");
10856   verifyFormat("void f() {} // comment");
10857   verifyFormat("void f() { int a; } // comment");
10858   verifyFormat("void f() {\n"
10859                "} // comment",
10860                DoNotMerge);
10861   verifyFormat("void f() {\n"
10862                "  int a;\n"
10863                "} // comment",
10864                DoNotMerge);
10865   verifyFormat("void f() {\n"
10866                "} // comment",
10867                getLLVMStyleWithColumns(15));
10868 
10869   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
10870   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
10871 
10872   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
10873   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
10874   verifyFormat("class C {\n"
10875                "  C()\n"
10876                "      : iiiiiiii(nullptr),\n"
10877                "        kkkkkkk(nullptr),\n"
10878                "        mmmmmmm(nullptr),\n"
10879                "        nnnnnnn(nullptr) {}\n"
10880                "};",
10881                getGoogleStyle());
10882 
10883   FormatStyle NoColumnLimit = getLLVMStyle();
10884   NoColumnLimit.ColumnLimit = 0;
10885   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
10886   EXPECT_EQ("class C {\n"
10887             "  A() : b(0) {}\n"
10888             "};",
10889             format("class C{A():b(0){}};", NoColumnLimit));
10890   EXPECT_EQ("A()\n"
10891             "    : b(0) {\n"
10892             "}",
10893             format("A()\n:b(0)\n{\n}", NoColumnLimit));
10894 
10895   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
10896   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
10897       FormatStyle::SFS_None;
10898   EXPECT_EQ("A()\n"
10899             "    : b(0) {\n"
10900             "}",
10901             format("A():b(0){}", DoNotMergeNoColumnLimit));
10902   EXPECT_EQ("A()\n"
10903             "    : b(0) {\n"
10904             "}",
10905             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
10906 
10907   verifyFormat("#define A          \\\n"
10908                "  void f() {       \\\n"
10909                "    int i;         \\\n"
10910                "  }",
10911                getLLVMStyleWithColumns(20));
10912   verifyFormat("#define A           \\\n"
10913                "  void f() { int i; }",
10914                getLLVMStyleWithColumns(21));
10915   verifyFormat("#define A            \\\n"
10916                "  void f() {         \\\n"
10917                "    int i;           \\\n"
10918                "  }                  \\\n"
10919                "  int j;",
10920                getLLVMStyleWithColumns(22));
10921   verifyFormat("#define A             \\\n"
10922                "  void f() { int i; } \\\n"
10923                "  int j;",
10924                getLLVMStyleWithColumns(23));
10925 }
10926 
10927 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
10928   FormatStyle MergeEmptyOnly = getLLVMStyle();
10929   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
10930   verifyFormat("class C {\n"
10931                "  int f() {}\n"
10932                "};",
10933                MergeEmptyOnly);
10934   verifyFormat("class C {\n"
10935                "  int f() {\n"
10936                "    return 42;\n"
10937                "  }\n"
10938                "};",
10939                MergeEmptyOnly);
10940   verifyFormat("int f() {}", MergeEmptyOnly);
10941   verifyFormat("int f() {\n"
10942                "  return 42;\n"
10943                "}",
10944                MergeEmptyOnly);
10945 
10946   // Also verify behavior when BraceWrapping.AfterFunction = true
10947   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
10948   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
10949   verifyFormat("int f() {}", MergeEmptyOnly);
10950   verifyFormat("class C {\n"
10951                "  int f() {}\n"
10952                "};",
10953                MergeEmptyOnly);
10954 }
10955 
10956 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
10957   FormatStyle MergeInlineOnly = getLLVMStyle();
10958   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10959   verifyFormat("class C {\n"
10960                "  int f() { return 42; }\n"
10961                "};",
10962                MergeInlineOnly);
10963   verifyFormat("int f() {\n"
10964                "  return 42;\n"
10965                "}",
10966                MergeInlineOnly);
10967 
10968   // SFS_Inline implies SFS_Empty
10969   verifyFormat("class C {\n"
10970                "  int f() {}\n"
10971                "};",
10972                MergeInlineOnly);
10973   verifyFormat("int f() {}", MergeInlineOnly);
10974 
10975   // Also verify behavior when BraceWrapping.AfterFunction = true
10976   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
10977   MergeInlineOnly.BraceWrapping.AfterFunction = true;
10978   verifyFormat("class C {\n"
10979                "  int f() { return 42; }\n"
10980                "};",
10981                MergeInlineOnly);
10982   verifyFormat("int f()\n"
10983                "{\n"
10984                "  return 42;\n"
10985                "}",
10986                MergeInlineOnly);
10987 
10988   // SFS_Inline implies SFS_Empty
10989   verifyFormat("int f() {}", MergeInlineOnly);
10990   verifyFormat("class C {\n"
10991                "  int f() {}\n"
10992                "};",
10993                MergeInlineOnly);
10994 }
10995 
10996 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
10997   FormatStyle MergeInlineOnly = getLLVMStyle();
10998   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
10999       FormatStyle::SFS_InlineOnly;
11000   verifyFormat("class C {\n"
11001                "  int f() { return 42; }\n"
11002                "};",
11003                MergeInlineOnly);
11004   verifyFormat("int f() {\n"
11005                "  return 42;\n"
11006                "}",
11007                MergeInlineOnly);
11008 
11009   // SFS_InlineOnly does not imply SFS_Empty
11010   verifyFormat("class C {\n"
11011                "  int f() {}\n"
11012                "};",
11013                MergeInlineOnly);
11014   verifyFormat("int f() {\n"
11015                "}",
11016                MergeInlineOnly);
11017 
11018   // Also verify behavior when BraceWrapping.AfterFunction = true
11019   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
11020   MergeInlineOnly.BraceWrapping.AfterFunction = true;
11021   verifyFormat("class C {\n"
11022                "  int f() { return 42; }\n"
11023                "};",
11024                MergeInlineOnly);
11025   verifyFormat("int f()\n"
11026                "{\n"
11027                "  return 42;\n"
11028                "}",
11029                MergeInlineOnly);
11030 
11031   // SFS_InlineOnly does not imply SFS_Empty
11032   verifyFormat("int f()\n"
11033                "{\n"
11034                "}",
11035                MergeInlineOnly);
11036   verifyFormat("class C {\n"
11037                "  int f() {}\n"
11038                "};",
11039                MergeInlineOnly);
11040 }
11041 
11042 TEST_F(FormatTest, SplitEmptyFunction) {
11043   FormatStyle Style = getLLVMStyle();
11044   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11045   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11046   Style.BraceWrapping.AfterFunction = true;
11047   Style.BraceWrapping.SplitEmptyFunction = false;
11048   Style.ColumnLimit = 40;
11049 
11050   verifyFormat("int f()\n"
11051                "{}",
11052                Style);
11053   verifyFormat("int f()\n"
11054                "{\n"
11055                "  return 42;\n"
11056                "}",
11057                Style);
11058   verifyFormat("int f()\n"
11059                "{\n"
11060                "  // some comment\n"
11061                "}",
11062                Style);
11063 
11064   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
11065   verifyFormat("int f() {}", Style);
11066   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11067                "{}",
11068                Style);
11069   verifyFormat("int f()\n"
11070                "{\n"
11071                "  return 0;\n"
11072                "}",
11073                Style);
11074 
11075   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
11076   verifyFormat("class Foo {\n"
11077                "  int f() {}\n"
11078                "};\n",
11079                Style);
11080   verifyFormat("class Foo {\n"
11081                "  int f() { return 0; }\n"
11082                "};\n",
11083                Style);
11084   verifyFormat("class Foo {\n"
11085                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11086                "  {}\n"
11087                "};\n",
11088                Style);
11089   verifyFormat("class Foo {\n"
11090                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11091                "  {\n"
11092                "    return 0;\n"
11093                "  }\n"
11094                "};\n",
11095                Style);
11096 
11097   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11098   verifyFormat("int f() {}", Style);
11099   verifyFormat("int f() { return 0; }", Style);
11100   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11101                "{}",
11102                Style);
11103   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
11104                "{\n"
11105                "  return 0;\n"
11106                "}",
11107                Style);
11108 }
11109 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
11110   FormatStyle Style = getLLVMStyle();
11111   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
11112   verifyFormat("#ifdef A\n"
11113                "int f() {}\n"
11114                "#else\n"
11115                "int g() {}\n"
11116                "#endif",
11117                Style);
11118 }
11119 
11120 TEST_F(FormatTest, SplitEmptyClass) {
11121   FormatStyle Style = getLLVMStyle();
11122   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11123   Style.BraceWrapping.AfterClass = true;
11124   Style.BraceWrapping.SplitEmptyRecord = false;
11125 
11126   verifyFormat("class Foo\n"
11127                "{};",
11128                Style);
11129   verifyFormat("/* something */ class Foo\n"
11130                "{};",
11131                Style);
11132   verifyFormat("template <typename X> class Foo\n"
11133                "{};",
11134                Style);
11135   verifyFormat("class Foo\n"
11136                "{\n"
11137                "  Foo();\n"
11138                "};",
11139                Style);
11140   verifyFormat("typedef class Foo\n"
11141                "{\n"
11142                "} Foo_t;",
11143                Style);
11144 
11145   Style.BraceWrapping.SplitEmptyRecord = true;
11146   Style.BraceWrapping.AfterStruct = true;
11147   verifyFormat("class rep\n"
11148                "{\n"
11149                "};",
11150                Style);
11151   verifyFormat("struct rep\n"
11152                "{\n"
11153                "};",
11154                Style);
11155   verifyFormat("template <typename T> class rep\n"
11156                "{\n"
11157                "};",
11158                Style);
11159   verifyFormat("template <typename T> struct rep\n"
11160                "{\n"
11161                "};",
11162                Style);
11163   verifyFormat("class rep\n"
11164                "{\n"
11165                "  int x;\n"
11166                "};",
11167                Style);
11168   verifyFormat("struct rep\n"
11169                "{\n"
11170                "  int x;\n"
11171                "};",
11172                Style);
11173   verifyFormat("template <typename T> class rep\n"
11174                "{\n"
11175                "  int x;\n"
11176                "};",
11177                Style);
11178   verifyFormat("template <typename T> struct rep\n"
11179                "{\n"
11180                "  int x;\n"
11181                "};",
11182                Style);
11183   verifyFormat("template <typename T> class rep // Foo\n"
11184                "{\n"
11185                "  int x;\n"
11186                "};",
11187                Style);
11188   verifyFormat("template <typename T> struct rep // Bar\n"
11189                "{\n"
11190                "  int x;\n"
11191                "};",
11192                Style);
11193 
11194   verifyFormat("template <typename T> class rep<T>\n"
11195                "{\n"
11196                "  int x;\n"
11197                "};",
11198                Style);
11199 
11200   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
11201                "{\n"
11202                "  int x;\n"
11203                "};",
11204                Style);
11205   verifyFormat("template <typename T> class rep<std::complex<T>>\n"
11206                "{\n"
11207                "};",
11208                Style);
11209 
11210   verifyFormat("#include \"stdint.h\"\n"
11211                "namespace rep {}",
11212                Style);
11213   verifyFormat("#include <stdint.h>\n"
11214                "namespace rep {}",
11215                Style);
11216   verifyFormat("#include <stdint.h>\n"
11217                "namespace rep {}",
11218                "#include <stdint.h>\n"
11219                "namespace rep {\n"
11220                "\n"
11221                "\n"
11222                "}",
11223                Style);
11224 }
11225 
11226 TEST_F(FormatTest, SplitEmptyStruct) {
11227   FormatStyle Style = getLLVMStyle();
11228   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11229   Style.BraceWrapping.AfterStruct = true;
11230   Style.BraceWrapping.SplitEmptyRecord = false;
11231 
11232   verifyFormat("struct Foo\n"
11233                "{};",
11234                Style);
11235   verifyFormat("/* something */ struct Foo\n"
11236                "{};",
11237                Style);
11238   verifyFormat("template <typename X> struct Foo\n"
11239                "{};",
11240                Style);
11241   verifyFormat("struct Foo\n"
11242                "{\n"
11243                "  Foo();\n"
11244                "};",
11245                Style);
11246   verifyFormat("typedef struct Foo\n"
11247                "{\n"
11248                "} Foo_t;",
11249                Style);
11250   // typedef struct Bar {} Bar_t;
11251 }
11252 
11253 TEST_F(FormatTest, SplitEmptyUnion) {
11254   FormatStyle Style = getLLVMStyle();
11255   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11256   Style.BraceWrapping.AfterUnion = true;
11257   Style.BraceWrapping.SplitEmptyRecord = false;
11258 
11259   verifyFormat("union Foo\n"
11260                "{};",
11261                Style);
11262   verifyFormat("/* something */ union Foo\n"
11263                "{};",
11264                Style);
11265   verifyFormat("union Foo\n"
11266                "{\n"
11267                "  A,\n"
11268                "};",
11269                Style);
11270   verifyFormat("typedef union Foo\n"
11271                "{\n"
11272                "} Foo_t;",
11273                Style);
11274 }
11275 
11276 TEST_F(FormatTest, SplitEmptyNamespace) {
11277   FormatStyle Style = getLLVMStyle();
11278   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11279   Style.BraceWrapping.AfterNamespace = true;
11280   Style.BraceWrapping.SplitEmptyNamespace = false;
11281 
11282   verifyFormat("namespace Foo\n"
11283                "{};",
11284                Style);
11285   verifyFormat("/* something */ namespace Foo\n"
11286                "{};",
11287                Style);
11288   verifyFormat("inline namespace Foo\n"
11289                "{};",
11290                Style);
11291   verifyFormat("/* something */ inline namespace Foo\n"
11292                "{};",
11293                Style);
11294   verifyFormat("export namespace Foo\n"
11295                "{};",
11296                Style);
11297   verifyFormat("namespace Foo\n"
11298                "{\n"
11299                "void Bar();\n"
11300                "};",
11301                Style);
11302 }
11303 
11304 TEST_F(FormatTest, NeverMergeShortRecords) {
11305   FormatStyle Style = getLLVMStyle();
11306 
11307   verifyFormat("class Foo {\n"
11308                "  Foo();\n"
11309                "};",
11310                Style);
11311   verifyFormat("typedef class Foo {\n"
11312                "  Foo();\n"
11313                "} Foo_t;",
11314                Style);
11315   verifyFormat("struct Foo {\n"
11316                "  Foo();\n"
11317                "};",
11318                Style);
11319   verifyFormat("typedef struct Foo {\n"
11320                "  Foo();\n"
11321                "} Foo_t;",
11322                Style);
11323   verifyFormat("union Foo {\n"
11324                "  A,\n"
11325                "};",
11326                Style);
11327   verifyFormat("typedef union Foo {\n"
11328                "  A,\n"
11329                "} Foo_t;",
11330                Style);
11331   verifyFormat("namespace Foo {\n"
11332                "void Bar();\n"
11333                "};",
11334                Style);
11335 
11336   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
11337   Style.BraceWrapping.AfterClass = true;
11338   Style.BraceWrapping.AfterStruct = true;
11339   Style.BraceWrapping.AfterUnion = true;
11340   Style.BraceWrapping.AfterNamespace = true;
11341   verifyFormat("class Foo\n"
11342                "{\n"
11343                "  Foo();\n"
11344                "};",
11345                Style);
11346   verifyFormat("typedef class Foo\n"
11347                "{\n"
11348                "  Foo();\n"
11349                "} Foo_t;",
11350                Style);
11351   verifyFormat("struct Foo\n"
11352                "{\n"
11353                "  Foo();\n"
11354                "};",
11355                Style);
11356   verifyFormat("typedef struct Foo\n"
11357                "{\n"
11358                "  Foo();\n"
11359                "} Foo_t;",
11360                Style);
11361   verifyFormat("union Foo\n"
11362                "{\n"
11363                "  A,\n"
11364                "};",
11365                Style);
11366   verifyFormat("typedef union Foo\n"
11367                "{\n"
11368                "  A,\n"
11369                "} Foo_t;",
11370                Style);
11371   verifyFormat("namespace Foo\n"
11372                "{\n"
11373                "void Bar();\n"
11374                "};",
11375                Style);
11376 }
11377 
11378 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
11379   // Elaborate type variable declarations.
11380   verifyFormat("struct foo a = {bar};\nint n;");
11381   verifyFormat("class foo a = {bar};\nint n;");
11382   verifyFormat("union foo a = {bar};\nint n;");
11383 
11384   // Elaborate types inside function definitions.
11385   verifyFormat("struct foo f() {}\nint n;");
11386   verifyFormat("class foo f() {}\nint n;");
11387   verifyFormat("union foo f() {}\nint n;");
11388 
11389   // Templates.
11390   verifyFormat("template <class X> void f() {}\nint n;");
11391   verifyFormat("template <struct X> void f() {}\nint n;");
11392   verifyFormat("template <union X> void f() {}\nint n;");
11393 
11394   // Actual definitions...
11395   verifyFormat("struct {\n} n;");
11396   verifyFormat(
11397       "template <template <class T, class Y>, class Z> class X {\n} n;");
11398   verifyFormat("union Z {\n  int n;\n} x;");
11399   verifyFormat("class MACRO Z {\n} n;");
11400   verifyFormat("class MACRO(X) Z {\n} n;");
11401   verifyFormat("class __attribute__(X) Z {\n} n;");
11402   verifyFormat("class __declspec(X) Z {\n} n;");
11403   verifyFormat("class A##B##C {\n} n;");
11404   verifyFormat("class alignas(16) Z {\n} n;");
11405   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
11406   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
11407 
11408   // Redefinition from nested context:
11409   verifyFormat("class A::B::C {\n} n;");
11410 
11411   // Template definitions.
11412   verifyFormat(
11413       "template <typename F>\n"
11414       "Matcher(const Matcher<F> &Other,\n"
11415       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
11416       "                             !is_same<F, T>::value>::type * = 0)\n"
11417       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
11418 
11419   // FIXME: This is still incorrectly handled at the formatter side.
11420   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
11421   verifyFormat("int i = SomeFunction(a<b, a> b);");
11422 
11423   // FIXME:
11424   // This now gets parsed incorrectly as class definition.
11425   // verifyFormat("class A<int> f() {\n}\nint n;");
11426 
11427   // Elaborate types where incorrectly parsing the structural element would
11428   // break the indent.
11429   verifyFormat("if (true)\n"
11430                "  class X x;\n"
11431                "else\n"
11432                "  f();\n");
11433 
11434   // This is simply incomplete. Formatting is not important, but must not crash.
11435   verifyFormat("class A:");
11436 }
11437 
11438 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
11439   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
11440             format("#error Leave     all         white!!!!! space* alone!\n"));
11441   EXPECT_EQ(
11442       "#warning Leave     all         white!!!!! space* alone!\n",
11443       format("#warning Leave     all         white!!!!! space* alone!\n"));
11444   EXPECT_EQ("#error 1", format("  #  error   1"));
11445   EXPECT_EQ("#warning 1", format("  #  warning 1"));
11446 }
11447 
11448 TEST_F(FormatTest, FormatHashIfExpressions) {
11449   verifyFormat("#if AAAA && BBBB");
11450   verifyFormat("#if (AAAA && BBBB)");
11451   verifyFormat("#elif (AAAA && BBBB)");
11452   // FIXME: Come up with a better indentation for #elif.
11453   verifyFormat(
11454       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
11455       "    defined(BBBBBBBB)\n"
11456       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
11457       "    defined(BBBBBBBB)\n"
11458       "#endif",
11459       getLLVMStyleWithColumns(65));
11460 }
11461 
11462 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
11463   FormatStyle AllowsMergedIf = getGoogleStyle();
11464   AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
11465       FormatStyle::SIS_WithoutElse;
11466   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
11467   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
11468   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
11469   EXPECT_EQ("if (true) return 42;",
11470             format("if (true)\nreturn 42;", AllowsMergedIf));
11471   FormatStyle ShortMergedIf = AllowsMergedIf;
11472   ShortMergedIf.ColumnLimit = 25;
11473   verifyFormat("#define A \\\n"
11474                "  if (true) return 42;",
11475                ShortMergedIf);
11476   verifyFormat("#define A \\\n"
11477                "  f();    \\\n"
11478                "  if (true)\n"
11479                "#define B",
11480                ShortMergedIf);
11481   verifyFormat("#define A \\\n"
11482                "  f();    \\\n"
11483                "  if (true)\n"
11484                "g();",
11485                ShortMergedIf);
11486   verifyFormat("{\n"
11487                "#ifdef A\n"
11488                "  // Comment\n"
11489                "  if (true) continue;\n"
11490                "#endif\n"
11491                "  // Comment\n"
11492                "  if (true) continue;\n"
11493                "}",
11494                ShortMergedIf);
11495   ShortMergedIf.ColumnLimit = 33;
11496   verifyFormat("#define A \\\n"
11497                "  if constexpr (true) return 42;",
11498                ShortMergedIf);
11499   verifyFormat("#define A \\\n"
11500                "  if CONSTEXPR (true) return 42;",
11501                ShortMergedIf);
11502   ShortMergedIf.ColumnLimit = 29;
11503   verifyFormat("#define A                   \\\n"
11504                "  if (aaaaaaaaaa) return 1; \\\n"
11505                "  return 2;",
11506                ShortMergedIf);
11507   ShortMergedIf.ColumnLimit = 28;
11508   verifyFormat("#define A         \\\n"
11509                "  if (aaaaaaaaaa) \\\n"
11510                "    return 1;     \\\n"
11511                "  return 2;",
11512                ShortMergedIf);
11513   verifyFormat("#define A                \\\n"
11514                "  if constexpr (aaaaaaa) \\\n"
11515                "    return 1;            \\\n"
11516                "  return 2;",
11517                ShortMergedIf);
11518   verifyFormat("#define A                \\\n"
11519                "  if CONSTEXPR (aaaaaaa) \\\n"
11520                "    return 1;            \\\n"
11521                "  return 2;",
11522                ShortMergedIf);
11523 }
11524 
11525 TEST_F(FormatTest, FormatStarDependingOnContext) {
11526   verifyFormat("void f(int *a);");
11527   verifyFormat("void f() { f(fint * b); }");
11528   verifyFormat("class A {\n  void f(int *a);\n};");
11529   verifyFormat("class A {\n  int *a;\n};");
11530   verifyFormat("namespace a {\n"
11531                "namespace b {\n"
11532                "class A {\n"
11533                "  void f() {}\n"
11534                "  int *a;\n"
11535                "};\n"
11536                "} // namespace b\n"
11537                "} // namespace a");
11538 }
11539 
11540 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
11541   verifyFormat("while");
11542   verifyFormat("operator");
11543 }
11544 
11545 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
11546   // This code would be painfully slow to format if we didn't skip it.
11547   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
11548                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11549                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11550                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11551                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
11552                    "A(1, 1)\n"
11553                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
11554                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11555                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11556                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11557                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11558                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11559                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11560                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11561                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
11562                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
11563   // Deeply nested part is untouched, rest is formatted.
11564   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
11565             format(std::string("int    i;\n") + Code + "int    j;\n",
11566                    getLLVMStyle(), SC_ExpectIncomplete));
11567 }
11568 
11569 //===----------------------------------------------------------------------===//
11570 // Objective-C tests.
11571 //===----------------------------------------------------------------------===//
11572 
11573 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
11574   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
11575   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
11576             format("-(NSUInteger)indexOfObject:(id)anObject;"));
11577   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
11578   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
11579   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
11580             format("-(NSInteger)Method3:(id)anObject;"));
11581   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
11582             format("-(NSInteger)Method4:(id)anObject;"));
11583   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
11584             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
11585   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
11586             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
11587   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
11588             "forAllCells:(BOOL)flag;",
11589             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
11590                    "forAllCells:(BOOL)flag;"));
11591 
11592   // Very long objectiveC method declaration.
11593   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
11594                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
11595   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
11596                "                    inRange:(NSRange)range\n"
11597                "                   outRange:(NSRange)out_range\n"
11598                "                  outRange1:(NSRange)out_range1\n"
11599                "                  outRange2:(NSRange)out_range2\n"
11600                "                  outRange3:(NSRange)out_range3\n"
11601                "                  outRange4:(NSRange)out_range4\n"
11602                "                  outRange5:(NSRange)out_range5\n"
11603                "                  outRange6:(NSRange)out_range6\n"
11604                "                  outRange7:(NSRange)out_range7\n"
11605                "                  outRange8:(NSRange)out_range8\n"
11606                "                  outRange9:(NSRange)out_range9;");
11607 
11608   // When the function name has to be wrapped.
11609   FormatStyle Style = getLLVMStyle();
11610   // ObjC ignores IndentWrappedFunctionNames when wrapping methods
11611   // and always indents instead.
11612   Style.IndentWrappedFunctionNames = false;
11613   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
11614                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
11615                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
11616                "}",
11617                Style);
11618   Style.IndentWrappedFunctionNames = true;
11619   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
11620                "    veryLooooooooooongName:(NSString)cccccccccccccc\n"
11621                "               anotherName:(NSString)dddddddddddddd {\n"
11622                "}",
11623                Style);
11624 
11625   verifyFormat("- (int)sum:(vector<int>)numbers;");
11626   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
11627   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
11628   // protocol lists (but not for template classes):
11629   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
11630 
11631   verifyFormat("- (int (*)())foo:(int (*)())f;");
11632   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
11633 
11634   // If there's no return type (very rare in practice!), LLVM and Google style
11635   // agree.
11636   verifyFormat("- foo;");
11637   verifyFormat("- foo:(int)f;");
11638   verifyGoogleFormat("- foo:(int)foo;");
11639 }
11640 
11641 TEST_F(FormatTest, BreaksStringLiterals) {
11642   EXPECT_EQ("\"some text \"\n"
11643             "\"other\";",
11644             format("\"some text other\";", getLLVMStyleWithColumns(12)));
11645   EXPECT_EQ("\"some text \"\n"
11646             "\"other\";",
11647             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
11648   EXPECT_EQ(
11649       "#define A  \\\n"
11650       "  \"some \"  \\\n"
11651       "  \"text \"  \\\n"
11652       "  \"other\";",
11653       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
11654   EXPECT_EQ(
11655       "#define A  \\\n"
11656       "  \"so \"    \\\n"
11657       "  \"text \"  \\\n"
11658       "  \"other\";",
11659       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
11660 
11661   EXPECT_EQ("\"some text\"",
11662             format("\"some text\"", getLLVMStyleWithColumns(1)));
11663   EXPECT_EQ("\"some text\"",
11664             format("\"some text\"", getLLVMStyleWithColumns(11)));
11665   EXPECT_EQ("\"some \"\n"
11666             "\"text\"",
11667             format("\"some text\"", getLLVMStyleWithColumns(10)));
11668   EXPECT_EQ("\"some \"\n"
11669             "\"text\"",
11670             format("\"some text\"", getLLVMStyleWithColumns(7)));
11671   EXPECT_EQ("\"some\"\n"
11672             "\" tex\"\n"
11673             "\"t\"",
11674             format("\"some text\"", getLLVMStyleWithColumns(6)));
11675   EXPECT_EQ("\"some\"\n"
11676             "\" tex\"\n"
11677             "\" and\"",
11678             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
11679   EXPECT_EQ("\"some\"\n"
11680             "\"/tex\"\n"
11681             "\"/and\"",
11682             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
11683 
11684   EXPECT_EQ("variable =\n"
11685             "    \"long string \"\n"
11686             "    \"literal\";",
11687             format("variable = \"long string literal\";",
11688                    getLLVMStyleWithColumns(20)));
11689 
11690   EXPECT_EQ("variable = f(\n"
11691             "    \"long string \"\n"
11692             "    \"literal\",\n"
11693             "    short,\n"
11694             "    loooooooooooooooooooong);",
11695             format("variable = f(\"long string literal\", short, "
11696                    "loooooooooooooooooooong);",
11697                    getLLVMStyleWithColumns(20)));
11698 
11699   EXPECT_EQ(
11700       "f(g(\"long string \"\n"
11701       "    \"literal\"),\n"
11702       "  b);",
11703       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
11704   EXPECT_EQ("f(g(\"long string \"\n"
11705             "    \"literal\",\n"
11706             "    a),\n"
11707             "  b);",
11708             format("f(g(\"long string literal\", a), b);",
11709                    getLLVMStyleWithColumns(20)));
11710   EXPECT_EQ(
11711       "f(\"one two\".split(\n"
11712       "    variable));",
11713       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
11714   EXPECT_EQ("f(\"one two three four five six \"\n"
11715             "  \"seven\".split(\n"
11716             "      really_looooong_variable));",
11717             format("f(\"one two three four five six seven\"."
11718                    "split(really_looooong_variable));",
11719                    getLLVMStyleWithColumns(33)));
11720 
11721   EXPECT_EQ("f(\"some \"\n"
11722             "  \"text\",\n"
11723             "  other);",
11724             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
11725 
11726   // Only break as a last resort.
11727   verifyFormat(
11728       "aaaaaaaaaaaaaaaaaaaa(\n"
11729       "    aaaaaaaaaaaaaaaaaaaa,\n"
11730       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
11731 
11732   EXPECT_EQ("\"splitmea\"\n"
11733             "\"trandomp\"\n"
11734             "\"oint\"",
11735             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
11736 
11737   EXPECT_EQ("\"split/\"\n"
11738             "\"pathat/\"\n"
11739             "\"slashes\"",
11740             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
11741 
11742   EXPECT_EQ("\"split/\"\n"
11743             "\"pathat/\"\n"
11744             "\"slashes\"",
11745             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
11746   EXPECT_EQ("\"split at \"\n"
11747             "\"spaces/at/\"\n"
11748             "\"slashes.at.any$\"\n"
11749             "\"non-alphanumeric%\"\n"
11750             "\"1111111111characte\"\n"
11751             "\"rs\"",
11752             format("\"split at "
11753                    "spaces/at/"
11754                    "slashes.at."
11755                    "any$non-"
11756                    "alphanumeric%"
11757                    "1111111111characte"
11758                    "rs\"",
11759                    getLLVMStyleWithColumns(20)));
11760 
11761   // Verify that splitting the strings understands
11762   // Style::AlwaysBreakBeforeMultilineStrings.
11763   EXPECT_EQ("aaaaaaaaaaaa(\n"
11764             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
11765             "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
11766             format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
11767                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
11768                    "aaaaaaaaaaaaaaaaaaaaaa\");",
11769                    getGoogleStyle()));
11770   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
11771             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
11772             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
11773                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
11774                    "aaaaaaaaaaaaaaaaaaaaaa\";",
11775                    getGoogleStyle()));
11776   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
11777             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
11778             format("llvm::outs() << "
11779                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
11780                    "aaaaaaaaaaaaaaaaaaa\";"));
11781   EXPECT_EQ("ffff(\n"
11782             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
11783             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
11784             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
11785                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
11786                    getGoogleStyle()));
11787 
11788   FormatStyle Style = getLLVMStyleWithColumns(12);
11789   Style.BreakStringLiterals = false;
11790   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
11791 
11792   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
11793   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
11794   EXPECT_EQ("#define A \\\n"
11795             "  \"some \" \\\n"
11796             "  \"text \" \\\n"
11797             "  \"other\";",
11798             format("#define A \"some text other\";", AlignLeft));
11799 }
11800 
11801 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
11802   EXPECT_EQ("C a = \"some more \"\n"
11803             "      \"text\";",
11804             format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
11805 }
11806 
11807 TEST_F(FormatTest, FullyRemoveEmptyLines) {
11808   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
11809   NoEmptyLines.MaxEmptyLinesToKeep = 0;
11810   EXPECT_EQ("int i = a(b());",
11811             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
11812 }
11813 
11814 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
11815   EXPECT_EQ(
11816       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11817       "(\n"
11818       "    \"x\t\");",
11819       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11820              "aaaaaaa("
11821              "\"x\t\");"));
11822 }
11823 
11824 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
11825   EXPECT_EQ(
11826       "u8\"utf8 string \"\n"
11827       "u8\"literal\";",
11828       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
11829   EXPECT_EQ(
11830       "u\"utf16 string \"\n"
11831       "u\"literal\";",
11832       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
11833   EXPECT_EQ(
11834       "U\"utf32 string \"\n"
11835       "U\"literal\";",
11836       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
11837   EXPECT_EQ("L\"wide string \"\n"
11838             "L\"literal\";",
11839             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
11840   EXPECT_EQ("@\"NSString \"\n"
11841             "@\"literal\";",
11842             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
11843   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
11844 
11845   // This input makes clang-format try to split the incomplete unicode escape
11846   // sequence, which used to lead to a crasher.
11847   verifyNoCrash(
11848       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
11849       getLLVMStyleWithColumns(60));
11850 }
11851 
11852 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
11853   FormatStyle Style = getGoogleStyleWithColumns(15);
11854   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
11855   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
11856   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
11857   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
11858   EXPECT_EQ("u8R\"x(raw literal)x\";",
11859             format("u8R\"x(raw literal)x\";", Style));
11860 }
11861 
11862 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
11863   FormatStyle Style = getLLVMStyleWithColumns(20);
11864   EXPECT_EQ(
11865       "_T(\"aaaaaaaaaaaaaa\")\n"
11866       "_T(\"aaaaaaaaaaaaaa\")\n"
11867       "_T(\"aaaaaaaaaaaa\")",
11868       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
11869   EXPECT_EQ("f(x,\n"
11870             "  _T(\"aaaaaaaaaaaa\")\n"
11871             "  _T(\"aaa\"),\n"
11872             "  z);",
11873             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
11874 
11875   // FIXME: Handle embedded spaces in one iteration.
11876   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
11877   //            "_T(\"aaaaaaaaaaaaa\")\n"
11878   //            "_T(\"aaaaaaaaaaaaa\")\n"
11879   //            "_T(\"a\")",
11880   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
11881   //                   getLLVMStyleWithColumns(20)));
11882   EXPECT_EQ(
11883       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
11884       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
11885   EXPECT_EQ("f(\n"
11886             "#if !TEST\n"
11887             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
11888             "#endif\n"
11889             ");",
11890             format("f(\n"
11891                    "#if !TEST\n"
11892                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
11893                    "#endif\n"
11894                    ");"));
11895   EXPECT_EQ("f(\n"
11896             "\n"
11897             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
11898             format("f(\n"
11899                    "\n"
11900                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
11901 }
11902 
11903 TEST_F(FormatTest, BreaksStringLiteralOperands) {
11904   // In a function call with two operands, the second can be broken with no line
11905   // break before it.
11906   EXPECT_EQ(
11907       "func(a, \"long long \"\n"
11908       "        \"long long\");",
11909       format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
11910   // In a function call with three operands, the second must be broken with a
11911   // line break before it.
11912   EXPECT_EQ("func(a,\n"
11913             "     \"long long long \"\n"
11914             "     \"long\",\n"
11915             "     c);",
11916             format("func(a, \"long long long long\", c);",
11917                    getLLVMStyleWithColumns(24)));
11918   // In a function call with three operands, the third must be broken with a
11919   // line break before it.
11920   EXPECT_EQ("func(a, b,\n"
11921             "     \"long long long \"\n"
11922             "     \"long\");",
11923             format("func(a, b, \"long long long long\");",
11924                    getLLVMStyleWithColumns(24)));
11925   // In a function call with three operands, both the second and the third must
11926   // be broken with a line break before them.
11927   EXPECT_EQ("func(a,\n"
11928             "     \"long long long \"\n"
11929             "     \"long\",\n"
11930             "     \"long long long \"\n"
11931             "     \"long\");",
11932             format("func(a, \"long long long long\", \"long long long long\");",
11933                    getLLVMStyleWithColumns(24)));
11934   // In a chain of << with two operands, the second can be broken with no line
11935   // break before it.
11936   EXPECT_EQ("a << \"line line \"\n"
11937             "     \"line\";",
11938             format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
11939   // In a chain of << with three operands, the second can be broken with no line
11940   // break before it.
11941   EXPECT_EQ(
11942       "abcde << \"line \"\n"
11943       "         \"line line\"\n"
11944       "      << c;",
11945       format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
11946   // In a chain of << with three operands, the third must be broken with a line
11947   // break before it.
11948   EXPECT_EQ(
11949       "a << b\n"
11950       "  << \"line line \"\n"
11951       "     \"line\";",
11952       format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
11953   // In a chain of << with three operands, the second can be broken with no line
11954   // break before it and the third must be broken with a line break before it.
11955   EXPECT_EQ("abcd << \"line line \"\n"
11956             "        \"line\"\n"
11957             "     << \"line line \"\n"
11958             "        \"line\";",
11959             format("abcd << \"line line line\" << \"line line line\";",
11960                    getLLVMStyleWithColumns(20)));
11961   // In a chain of binary operators with two operands, the second can be broken
11962   // with no line break before it.
11963   EXPECT_EQ(
11964       "abcd + \"line line \"\n"
11965       "       \"line line\";",
11966       format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
11967   // In a chain of binary operators with three operands, the second must be
11968   // broken with a line break before it.
11969   EXPECT_EQ("abcd +\n"
11970             "    \"line line \"\n"
11971             "    \"line line\" +\n"
11972             "    e;",
11973             format("abcd + \"line line line line\" + e;",
11974                    getLLVMStyleWithColumns(20)));
11975   // In a function call with two operands, with AlignAfterOpenBracket enabled,
11976   // the first must be broken with a line break before it.
11977   FormatStyle Style = getLLVMStyleWithColumns(25);
11978   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11979   EXPECT_EQ("someFunction(\n"
11980             "    \"long long long \"\n"
11981             "    \"long\",\n"
11982             "    a);",
11983             format("someFunction(\"long long long long\", a);", Style));
11984 }
11985 
11986 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
11987   EXPECT_EQ(
11988       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
11989       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
11990       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
11991       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
11992              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
11993              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
11994 }
11995 
11996 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
11997   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
11998             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
11999   EXPECT_EQ("fffffffffff(g(R\"x(\n"
12000             "multiline raw string literal xxxxxxxxxxxxxx\n"
12001             ")x\",\n"
12002             "              a),\n"
12003             "            b);",
12004             format("fffffffffff(g(R\"x(\n"
12005                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12006                    ")x\", a), b);",
12007                    getGoogleStyleWithColumns(20)));
12008   EXPECT_EQ("fffffffffff(\n"
12009             "    g(R\"x(qqq\n"
12010             "multiline raw string literal xxxxxxxxxxxxxx\n"
12011             ")x\",\n"
12012             "      a),\n"
12013             "    b);",
12014             format("fffffffffff(g(R\"x(qqq\n"
12015                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12016                    ")x\", a), b);",
12017                    getGoogleStyleWithColumns(20)));
12018 
12019   EXPECT_EQ("fffffffffff(R\"x(\n"
12020             "multiline raw string literal xxxxxxxxxxxxxx\n"
12021             ")x\");",
12022             format("fffffffffff(R\"x(\n"
12023                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12024                    ")x\");",
12025                    getGoogleStyleWithColumns(20)));
12026   EXPECT_EQ("fffffffffff(R\"x(\n"
12027             "multiline raw string literal xxxxxxxxxxxxxx\n"
12028             ")x\" + bbbbbb);",
12029             format("fffffffffff(R\"x(\n"
12030                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12031                    ")x\" +   bbbbbb);",
12032                    getGoogleStyleWithColumns(20)));
12033   EXPECT_EQ("fffffffffff(\n"
12034             "    R\"x(\n"
12035             "multiline raw string literal xxxxxxxxxxxxxx\n"
12036             ")x\" +\n"
12037             "    bbbbbb);",
12038             format("fffffffffff(\n"
12039                    " R\"x(\n"
12040                    "multiline raw string literal xxxxxxxxxxxxxx\n"
12041                    ")x\" + bbbbbb);",
12042                    getGoogleStyleWithColumns(20)));
12043   EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
12044             format("fffffffffff(\n"
12045                    " R\"(single line raw string)\" + bbbbbb);"));
12046 }
12047 
12048 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
12049   verifyFormat("string a = \"unterminated;");
12050   EXPECT_EQ("function(\"unterminated,\n"
12051             "         OtherParameter);",
12052             format("function(  \"unterminated,\n"
12053                    "    OtherParameter);"));
12054 }
12055 
12056 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
12057   FormatStyle Style = getLLVMStyle();
12058   Style.Standard = FormatStyle::LS_Cpp03;
12059   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
12060             format("#define x(_a) printf(\"foo\"_a);", Style));
12061 }
12062 
12063 TEST_F(FormatTest, CppLexVersion) {
12064   FormatStyle Style = getLLVMStyle();
12065   // Formatting of x * y differs if x is a type.
12066   verifyFormat("void foo() { MACRO(a * b); }", Style);
12067   verifyFormat("void foo() { MACRO(int *b); }", Style);
12068 
12069   // LLVM style uses latest lexer.
12070   verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
12071   Style.Standard = FormatStyle::LS_Cpp17;
12072   // But in c++17, char8_t isn't a keyword.
12073   verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
12074 }
12075 
12076 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
12077 
12078 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
12079   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
12080             "             \"ddeeefff\");",
12081             format("someFunction(\"aaabbbcccdddeeefff\");",
12082                    getLLVMStyleWithColumns(25)));
12083   EXPECT_EQ("someFunction1234567890(\n"
12084             "    \"aaabbbcccdddeeefff\");",
12085             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12086                    getLLVMStyleWithColumns(26)));
12087   EXPECT_EQ("someFunction1234567890(\n"
12088             "    \"aaabbbcccdddeeeff\"\n"
12089             "    \"f\");",
12090             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12091                    getLLVMStyleWithColumns(25)));
12092   EXPECT_EQ("someFunction1234567890(\n"
12093             "    \"aaabbbcccdddeeeff\"\n"
12094             "    \"f\");",
12095             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
12096                    getLLVMStyleWithColumns(24)));
12097   EXPECT_EQ("someFunction(\n"
12098             "    \"aaabbbcc ddde \"\n"
12099             "    \"efff\");",
12100             format("someFunction(\"aaabbbcc ddde efff\");",
12101                    getLLVMStyleWithColumns(25)));
12102   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
12103             "             \"ddeeefff\");",
12104             format("someFunction(\"aaabbbccc ddeeefff\");",
12105                    getLLVMStyleWithColumns(25)));
12106   EXPECT_EQ("someFunction1234567890(\n"
12107             "    \"aaabb \"\n"
12108             "    \"cccdddeeefff\");",
12109             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
12110                    getLLVMStyleWithColumns(25)));
12111   EXPECT_EQ("#define A          \\\n"
12112             "  string s =       \\\n"
12113             "      \"123456789\"  \\\n"
12114             "      \"0\";         \\\n"
12115             "  int i;",
12116             format("#define A string s = \"1234567890\"; int i;",
12117                    getLLVMStyleWithColumns(20)));
12118   EXPECT_EQ("someFunction(\n"
12119             "    \"aaabbbcc \"\n"
12120             "    \"dddeeefff\");",
12121             format("someFunction(\"aaabbbcc dddeeefff\");",
12122                    getLLVMStyleWithColumns(25)));
12123 }
12124 
12125 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
12126   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
12127   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
12128   EXPECT_EQ("\"test\"\n"
12129             "\"\\n\"",
12130             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
12131   EXPECT_EQ("\"tes\\\\\"\n"
12132             "\"n\"",
12133             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
12134   EXPECT_EQ("\"\\\\\\\\\"\n"
12135             "\"\\n\"",
12136             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
12137   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
12138   EXPECT_EQ("\"\\uff01\"\n"
12139             "\"test\"",
12140             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
12141   EXPECT_EQ("\"\\Uff01ff02\"",
12142             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
12143   EXPECT_EQ("\"\\x000000000001\"\n"
12144             "\"next\"",
12145             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
12146   EXPECT_EQ("\"\\x000000000001next\"",
12147             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
12148   EXPECT_EQ("\"\\x000000000001\"",
12149             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
12150   EXPECT_EQ("\"test\"\n"
12151             "\"\\000000\"\n"
12152             "\"000001\"",
12153             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
12154   EXPECT_EQ("\"test\\000\"\n"
12155             "\"00000000\"\n"
12156             "\"1\"",
12157             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
12158 }
12159 
12160 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
12161   verifyFormat("void f() {\n"
12162                "  return g() {}\n"
12163                "  void h() {}");
12164   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
12165                "g();\n"
12166                "}");
12167 }
12168 
12169 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
12170   verifyFormat(
12171       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
12172 }
12173 
12174 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
12175   verifyFormat("class X {\n"
12176                "  void f() {\n"
12177                "  }\n"
12178                "};",
12179                getLLVMStyleWithColumns(12));
12180 }
12181 
12182 TEST_F(FormatTest, ConfigurableIndentWidth) {
12183   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
12184   EightIndent.IndentWidth = 8;
12185   EightIndent.ContinuationIndentWidth = 8;
12186   verifyFormat("void f() {\n"
12187                "        someFunction();\n"
12188                "        if (true) {\n"
12189                "                f();\n"
12190                "        }\n"
12191                "}",
12192                EightIndent);
12193   verifyFormat("class X {\n"
12194                "        void f() {\n"
12195                "        }\n"
12196                "};",
12197                EightIndent);
12198   verifyFormat("int x[] = {\n"
12199                "        call(),\n"
12200                "        call()};",
12201                EightIndent);
12202 }
12203 
12204 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
12205   verifyFormat("double\n"
12206                "f();",
12207                getLLVMStyleWithColumns(8));
12208 }
12209 
12210 TEST_F(FormatTest, ConfigurableUseOfTab) {
12211   FormatStyle Tab = getLLVMStyleWithColumns(42);
12212   Tab.IndentWidth = 8;
12213   Tab.UseTab = FormatStyle::UT_Always;
12214   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12215 
12216   EXPECT_EQ("if (aaaaaaaa && // q\n"
12217             "    bb)\t\t// w\n"
12218             "\t;",
12219             format("if (aaaaaaaa &&// q\n"
12220                    "bb)// w\n"
12221                    ";",
12222                    Tab));
12223   EXPECT_EQ("if (aaa && bbb) // w\n"
12224             "\t;",
12225             format("if(aaa&&bbb)// w\n"
12226                    ";",
12227                    Tab));
12228 
12229   verifyFormat("class X {\n"
12230                "\tvoid f() {\n"
12231                "\t\tsomeFunction(parameter1,\n"
12232                "\t\t\t     parameter2);\n"
12233                "\t}\n"
12234                "};",
12235                Tab);
12236   verifyFormat("#define A                        \\\n"
12237                "\tvoid f() {               \\\n"
12238                "\t\tsomeFunction(    \\\n"
12239                "\t\t    parameter1,  \\\n"
12240                "\t\t    parameter2); \\\n"
12241                "\t}",
12242                Tab);
12243   verifyFormat("int a;\t      // x\n"
12244                "int bbbbbbbb; // x\n",
12245                Tab);
12246 
12247   Tab.TabWidth = 4;
12248   Tab.IndentWidth = 8;
12249   verifyFormat("class TabWidth4Indent8 {\n"
12250                "\t\tvoid f() {\n"
12251                "\t\t\t\tsomeFunction(parameter1,\n"
12252                "\t\t\t\t\t\t\t parameter2);\n"
12253                "\t\t}\n"
12254                "};",
12255                Tab);
12256 
12257   Tab.TabWidth = 4;
12258   Tab.IndentWidth = 4;
12259   verifyFormat("class TabWidth4Indent4 {\n"
12260                "\tvoid f() {\n"
12261                "\t\tsomeFunction(parameter1,\n"
12262                "\t\t\t\t\t parameter2);\n"
12263                "\t}\n"
12264                "};",
12265                Tab);
12266 
12267   Tab.TabWidth = 8;
12268   Tab.IndentWidth = 4;
12269   verifyFormat("class TabWidth8Indent4 {\n"
12270                "    void f() {\n"
12271                "\tsomeFunction(parameter1,\n"
12272                "\t\t     parameter2);\n"
12273                "    }\n"
12274                "};",
12275                Tab);
12276 
12277   Tab.TabWidth = 8;
12278   Tab.IndentWidth = 8;
12279   EXPECT_EQ("/*\n"
12280             "\t      a\t\tcomment\n"
12281             "\t      in multiple lines\n"
12282             "       */",
12283             format("   /*\t \t \n"
12284                    " \t \t a\t\tcomment\t \t\n"
12285                    " \t \t in multiple lines\t\n"
12286                    " \t  */",
12287                    Tab));
12288 
12289   Tab.UseTab = FormatStyle::UT_ForIndentation;
12290   verifyFormat("{\n"
12291                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12292                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12293                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12294                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12295                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12296                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12297                "};",
12298                Tab);
12299   verifyFormat("enum AA {\n"
12300                "\ta1, // Force multiple lines\n"
12301                "\ta2,\n"
12302                "\ta3\n"
12303                "};",
12304                Tab);
12305   EXPECT_EQ("if (aaaaaaaa && // q\n"
12306             "    bb)         // w\n"
12307             "\t;",
12308             format("if (aaaaaaaa &&// q\n"
12309                    "bb)// w\n"
12310                    ";",
12311                    Tab));
12312   verifyFormat("class X {\n"
12313                "\tvoid f() {\n"
12314                "\t\tsomeFunction(parameter1,\n"
12315                "\t\t             parameter2);\n"
12316                "\t}\n"
12317                "};",
12318                Tab);
12319   verifyFormat("{\n"
12320                "\tQ(\n"
12321                "\t    {\n"
12322                "\t\t    int a;\n"
12323                "\t\t    someFunction(aaaaaaaa,\n"
12324                "\t\t                 bbbbbbb);\n"
12325                "\t    },\n"
12326                "\t    p);\n"
12327                "}",
12328                Tab);
12329   EXPECT_EQ("{\n"
12330             "\t/* aaaa\n"
12331             "\t   bbbb */\n"
12332             "}",
12333             format("{\n"
12334                    "/* aaaa\n"
12335                    "   bbbb */\n"
12336                    "}",
12337                    Tab));
12338   EXPECT_EQ("{\n"
12339             "\t/*\n"
12340             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12341             "\t  bbbbbbbbbbbbb\n"
12342             "\t*/\n"
12343             "}",
12344             format("{\n"
12345                    "/*\n"
12346                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12347                    "*/\n"
12348                    "}",
12349                    Tab));
12350   EXPECT_EQ("{\n"
12351             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12352             "\t// bbbbbbbbbbbbb\n"
12353             "}",
12354             format("{\n"
12355                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12356                    "}",
12357                    Tab));
12358   EXPECT_EQ("{\n"
12359             "\t/*\n"
12360             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12361             "\t  bbbbbbbbbbbbb\n"
12362             "\t*/\n"
12363             "}",
12364             format("{\n"
12365                    "\t/*\n"
12366                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12367                    "\t*/\n"
12368                    "}",
12369                    Tab));
12370   EXPECT_EQ("{\n"
12371             "\t/*\n"
12372             "\n"
12373             "\t*/\n"
12374             "}",
12375             format("{\n"
12376                    "\t/*\n"
12377                    "\n"
12378                    "\t*/\n"
12379                    "}",
12380                    Tab));
12381   EXPECT_EQ("{\n"
12382             "\t/*\n"
12383             " asdf\n"
12384             "\t*/\n"
12385             "}",
12386             format("{\n"
12387                    "\t/*\n"
12388                    " asdf\n"
12389                    "\t*/\n"
12390                    "}",
12391                    Tab));
12392 
12393   Tab.UseTab = FormatStyle::UT_Never;
12394   EXPECT_EQ("/*\n"
12395             "              a\t\tcomment\n"
12396             "              in multiple lines\n"
12397             "       */",
12398             format("   /*\t \t \n"
12399                    " \t \t a\t\tcomment\t \t\n"
12400                    " \t \t in multiple lines\t\n"
12401                    " \t  */",
12402                    Tab));
12403   EXPECT_EQ("/* some\n"
12404             "   comment */",
12405             format(" \t \t /* some\n"
12406                    " \t \t    comment */",
12407                    Tab));
12408   EXPECT_EQ("int a; /* some\n"
12409             "   comment */",
12410             format(" \t \t int a; /* some\n"
12411                    " \t \t    comment */",
12412                    Tab));
12413 
12414   EXPECT_EQ("int a; /* some\n"
12415             "comment */",
12416             format(" \t \t int\ta; /* some\n"
12417                    " \t \t    comment */",
12418                    Tab));
12419   EXPECT_EQ("f(\"\t\t\"); /* some\n"
12420             "    comment */",
12421             format(" \t \t f(\"\t\t\"); /* some\n"
12422                    " \t \t    comment */",
12423                    Tab));
12424   EXPECT_EQ("{\n"
12425             "        /*\n"
12426             "         * Comment\n"
12427             "         */\n"
12428             "        int i;\n"
12429             "}",
12430             format("{\n"
12431                    "\t/*\n"
12432                    "\t * Comment\n"
12433                    "\t */\n"
12434                    "\t int i;\n"
12435                    "}",
12436                    Tab));
12437 
12438   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
12439   Tab.TabWidth = 8;
12440   Tab.IndentWidth = 8;
12441   EXPECT_EQ("if (aaaaaaaa && // q\n"
12442             "    bb)         // w\n"
12443             "\t;",
12444             format("if (aaaaaaaa &&// q\n"
12445                    "bb)// w\n"
12446                    ";",
12447                    Tab));
12448   EXPECT_EQ("if (aaa && bbb) // w\n"
12449             "\t;",
12450             format("if(aaa&&bbb)// w\n"
12451                    ";",
12452                    Tab));
12453   verifyFormat("class X {\n"
12454                "\tvoid f() {\n"
12455                "\t\tsomeFunction(parameter1,\n"
12456                "\t\t\t     parameter2);\n"
12457                "\t}\n"
12458                "};",
12459                Tab);
12460   verifyFormat("#define A                        \\\n"
12461                "\tvoid f() {               \\\n"
12462                "\t\tsomeFunction(    \\\n"
12463                "\t\t    parameter1,  \\\n"
12464                "\t\t    parameter2); \\\n"
12465                "\t}",
12466                Tab);
12467   Tab.TabWidth = 4;
12468   Tab.IndentWidth = 8;
12469   verifyFormat("class TabWidth4Indent8 {\n"
12470                "\t\tvoid f() {\n"
12471                "\t\t\t\tsomeFunction(parameter1,\n"
12472                "\t\t\t\t\t\t\t parameter2);\n"
12473                "\t\t}\n"
12474                "};",
12475                Tab);
12476   Tab.TabWidth = 4;
12477   Tab.IndentWidth = 4;
12478   verifyFormat("class TabWidth4Indent4 {\n"
12479                "\tvoid f() {\n"
12480                "\t\tsomeFunction(parameter1,\n"
12481                "\t\t\t\t\t parameter2);\n"
12482                "\t}\n"
12483                "};",
12484                Tab);
12485   Tab.TabWidth = 8;
12486   Tab.IndentWidth = 4;
12487   verifyFormat("class TabWidth8Indent4 {\n"
12488                "    void f() {\n"
12489                "\tsomeFunction(parameter1,\n"
12490                "\t\t     parameter2);\n"
12491                "    }\n"
12492                "};",
12493                Tab);
12494   Tab.TabWidth = 8;
12495   Tab.IndentWidth = 8;
12496   EXPECT_EQ("/*\n"
12497             "\t      a\t\tcomment\n"
12498             "\t      in multiple lines\n"
12499             "       */",
12500             format("   /*\t \t \n"
12501                    " \t \t a\t\tcomment\t \t\n"
12502                    " \t \t in multiple lines\t\n"
12503                    " \t  */",
12504                    Tab));
12505   verifyFormat("{\n"
12506                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12507                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12508                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12509                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12510                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12511                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12512                "};",
12513                Tab);
12514   verifyFormat("enum AA {\n"
12515                "\ta1, // Force multiple lines\n"
12516                "\ta2,\n"
12517                "\ta3\n"
12518                "};",
12519                Tab);
12520   EXPECT_EQ("if (aaaaaaaa && // q\n"
12521             "    bb)         // w\n"
12522             "\t;",
12523             format("if (aaaaaaaa &&// q\n"
12524                    "bb)// w\n"
12525                    ";",
12526                    Tab));
12527   verifyFormat("class X {\n"
12528                "\tvoid f() {\n"
12529                "\t\tsomeFunction(parameter1,\n"
12530                "\t\t\t     parameter2);\n"
12531                "\t}\n"
12532                "};",
12533                Tab);
12534   verifyFormat("{\n"
12535                "\tQ(\n"
12536                "\t    {\n"
12537                "\t\t    int a;\n"
12538                "\t\t    someFunction(aaaaaaaa,\n"
12539                "\t\t\t\t bbbbbbb);\n"
12540                "\t    },\n"
12541                "\t    p);\n"
12542                "}",
12543                Tab);
12544   EXPECT_EQ("{\n"
12545             "\t/* aaaa\n"
12546             "\t   bbbb */\n"
12547             "}",
12548             format("{\n"
12549                    "/* aaaa\n"
12550                    "   bbbb */\n"
12551                    "}",
12552                    Tab));
12553   EXPECT_EQ("{\n"
12554             "\t/*\n"
12555             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12556             "\t  bbbbbbbbbbbbb\n"
12557             "\t*/\n"
12558             "}",
12559             format("{\n"
12560                    "/*\n"
12561                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12562                    "*/\n"
12563                    "}",
12564                    Tab));
12565   EXPECT_EQ("{\n"
12566             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12567             "\t// bbbbbbbbbbbbb\n"
12568             "}",
12569             format("{\n"
12570                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12571                    "}",
12572                    Tab));
12573   EXPECT_EQ("{\n"
12574             "\t/*\n"
12575             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12576             "\t  bbbbbbbbbbbbb\n"
12577             "\t*/\n"
12578             "}",
12579             format("{\n"
12580                    "\t/*\n"
12581                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12582                    "\t*/\n"
12583                    "}",
12584                    Tab));
12585   EXPECT_EQ("{\n"
12586             "\t/*\n"
12587             "\n"
12588             "\t*/\n"
12589             "}",
12590             format("{\n"
12591                    "\t/*\n"
12592                    "\n"
12593                    "\t*/\n"
12594                    "}",
12595                    Tab));
12596   EXPECT_EQ("{\n"
12597             "\t/*\n"
12598             " asdf\n"
12599             "\t*/\n"
12600             "}",
12601             format("{\n"
12602                    "\t/*\n"
12603                    " asdf\n"
12604                    "\t*/\n"
12605                    "}",
12606                    Tab));
12607   EXPECT_EQ("/* some\n"
12608             "   comment */",
12609             format(" \t \t /* some\n"
12610                    " \t \t    comment */",
12611                    Tab));
12612   EXPECT_EQ("int a; /* some\n"
12613             "   comment */",
12614             format(" \t \t int a; /* some\n"
12615                    " \t \t    comment */",
12616                    Tab));
12617   EXPECT_EQ("int a; /* some\n"
12618             "comment */",
12619             format(" \t \t int\ta; /* some\n"
12620                    " \t \t    comment */",
12621                    Tab));
12622   EXPECT_EQ("f(\"\t\t\"); /* some\n"
12623             "    comment */",
12624             format(" \t \t f(\"\t\t\"); /* some\n"
12625                    " \t \t    comment */",
12626                    Tab));
12627   EXPECT_EQ("{\n"
12628             "\t/*\n"
12629             "\t * Comment\n"
12630             "\t */\n"
12631             "\tint i;\n"
12632             "}",
12633             format("{\n"
12634                    "\t/*\n"
12635                    "\t * Comment\n"
12636                    "\t */\n"
12637                    "\t int i;\n"
12638                    "}",
12639                    Tab));
12640   Tab.TabWidth = 2;
12641   Tab.IndentWidth = 2;
12642   EXPECT_EQ("{\n"
12643             "\t/* aaaa\n"
12644             "\t\t bbbb */\n"
12645             "}",
12646             format("{\n"
12647                    "/* aaaa\n"
12648                    "\t bbbb */\n"
12649                    "}",
12650                    Tab));
12651   EXPECT_EQ("{\n"
12652             "\t/*\n"
12653             "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12654             "\t\tbbbbbbbbbbbbb\n"
12655             "\t*/\n"
12656             "}",
12657             format("{\n"
12658                    "/*\n"
12659                    "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12660                    "*/\n"
12661                    "}",
12662                    Tab));
12663   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
12664   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
12665   Tab.TabWidth = 4;
12666   Tab.IndentWidth = 4;
12667   verifyFormat("class Assign {\n"
12668                "\tvoid f() {\n"
12669                "\t\tint         x      = 123;\n"
12670                "\t\tint         random = 4;\n"
12671                "\t\tstd::string alphabet =\n"
12672                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
12673                "\t}\n"
12674                "};",
12675                Tab);
12676 
12677   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
12678   Tab.TabWidth = 8;
12679   Tab.IndentWidth = 8;
12680   EXPECT_EQ("if (aaaaaaaa && // q\n"
12681             "    bb)         // w\n"
12682             "\t;",
12683             format("if (aaaaaaaa &&// q\n"
12684                    "bb)// w\n"
12685                    ";",
12686                    Tab));
12687   EXPECT_EQ("if (aaa && bbb) // w\n"
12688             "\t;",
12689             format("if(aaa&&bbb)// w\n"
12690                    ";",
12691                    Tab));
12692   verifyFormat("class X {\n"
12693                "\tvoid f() {\n"
12694                "\t\tsomeFunction(parameter1,\n"
12695                "\t\t             parameter2);\n"
12696                "\t}\n"
12697                "};",
12698                Tab);
12699   verifyFormat("#define A                        \\\n"
12700                "\tvoid f() {               \\\n"
12701                "\t\tsomeFunction(    \\\n"
12702                "\t\t    parameter1,  \\\n"
12703                "\t\t    parameter2); \\\n"
12704                "\t}",
12705                Tab);
12706   Tab.TabWidth = 4;
12707   Tab.IndentWidth = 8;
12708   verifyFormat("class TabWidth4Indent8 {\n"
12709                "\t\tvoid f() {\n"
12710                "\t\t\t\tsomeFunction(parameter1,\n"
12711                "\t\t\t\t             parameter2);\n"
12712                "\t\t}\n"
12713                "};",
12714                Tab);
12715   Tab.TabWidth = 4;
12716   Tab.IndentWidth = 4;
12717   verifyFormat("class TabWidth4Indent4 {\n"
12718                "\tvoid f() {\n"
12719                "\t\tsomeFunction(parameter1,\n"
12720                "\t\t             parameter2);\n"
12721                "\t}\n"
12722                "};",
12723                Tab);
12724   Tab.TabWidth = 8;
12725   Tab.IndentWidth = 4;
12726   verifyFormat("class TabWidth8Indent4 {\n"
12727                "    void f() {\n"
12728                "\tsomeFunction(parameter1,\n"
12729                "\t             parameter2);\n"
12730                "    }\n"
12731                "};",
12732                Tab);
12733   Tab.TabWidth = 8;
12734   Tab.IndentWidth = 8;
12735   EXPECT_EQ("/*\n"
12736             "              a\t\tcomment\n"
12737             "              in multiple lines\n"
12738             "       */",
12739             format("   /*\t \t \n"
12740                    " \t \t a\t\tcomment\t \t\n"
12741                    " \t \t in multiple lines\t\n"
12742                    " \t  */",
12743                    Tab));
12744   verifyFormat("{\n"
12745                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12746                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12747                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12748                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12749                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12750                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
12751                "};",
12752                Tab);
12753   verifyFormat("enum AA {\n"
12754                "\ta1, // Force multiple lines\n"
12755                "\ta2,\n"
12756                "\ta3\n"
12757                "};",
12758                Tab);
12759   EXPECT_EQ("if (aaaaaaaa && // q\n"
12760             "    bb)         // w\n"
12761             "\t;",
12762             format("if (aaaaaaaa &&// q\n"
12763                    "bb)// w\n"
12764                    ";",
12765                    Tab));
12766   verifyFormat("class X {\n"
12767                "\tvoid f() {\n"
12768                "\t\tsomeFunction(parameter1,\n"
12769                "\t\t             parameter2);\n"
12770                "\t}\n"
12771                "};",
12772                Tab);
12773   verifyFormat("{\n"
12774                "\tQ(\n"
12775                "\t    {\n"
12776                "\t\t    int a;\n"
12777                "\t\t    someFunction(aaaaaaaa,\n"
12778                "\t\t                 bbbbbbb);\n"
12779                "\t    },\n"
12780                "\t    p);\n"
12781                "}",
12782                Tab);
12783   EXPECT_EQ("{\n"
12784             "\t/* aaaa\n"
12785             "\t   bbbb */\n"
12786             "}",
12787             format("{\n"
12788                    "/* aaaa\n"
12789                    "   bbbb */\n"
12790                    "}",
12791                    Tab));
12792   EXPECT_EQ("{\n"
12793             "\t/*\n"
12794             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12795             "\t  bbbbbbbbbbbbb\n"
12796             "\t*/\n"
12797             "}",
12798             format("{\n"
12799                    "/*\n"
12800                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12801                    "*/\n"
12802                    "}",
12803                    Tab));
12804   EXPECT_EQ("{\n"
12805             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12806             "\t// bbbbbbbbbbbbb\n"
12807             "}",
12808             format("{\n"
12809                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12810                    "}",
12811                    Tab));
12812   EXPECT_EQ("{\n"
12813             "\t/*\n"
12814             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12815             "\t  bbbbbbbbbbbbb\n"
12816             "\t*/\n"
12817             "}",
12818             format("{\n"
12819                    "\t/*\n"
12820                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12821                    "\t*/\n"
12822                    "}",
12823                    Tab));
12824   EXPECT_EQ("{\n"
12825             "\t/*\n"
12826             "\n"
12827             "\t*/\n"
12828             "}",
12829             format("{\n"
12830                    "\t/*\n"
12831                    "\n"
12832                    "\t*/\n"
12833                    "}",
12834                    Tab));
12835   EXPECT_EQ("{\n"
12836             "\t/*\n"
12837             " asdf\n"
12838             "\t*/\n"
12839             "}",
12840             format("{\n"
12841                    "\t/*\n"
12842                    " asdf\n"
12843                    "\t*/\n"
12844                    "}",
12845                    Tab));
12846   EXPECT_EQ("/* some\n"
12847             "   comment */",
12848             format(" \t \t /* some\n"
12849                    " \t \t    comment */",
12850                    Tab));
12851   EXPECT_EQ("int a; /* some\n"
12852             "   comment */",
12853             format(" \t \t int a; /* some\n"
12854                    " \t \t    comment */",
12855                    Tab));
12856   EXPECT_EQ("int a; /* some\n"
12857             "comment */",
12858             format(" \t \t int\ta; /* some\n"
12859                    " \t \t    comment */",
12860                    Tab));
12861   EXPECT_EQ("f(\"\t\t\"); /* some\n"
12862             "    comment */",
12863             format(" \t \t f(\"\t\t\"); /* some\n"
12864                    " \t \t    comment */",
12865                    Tab));
12866   EXPECT_EQ("{\n"
12867             "\t/*\n"
12868             "\t * Comment\n"
12869             "\t */\n"
12870             "\tint i;\n"
12871             "}",
12872             format("{\n"
12873                    "\t/*\n"
12874                    "\t * Comment\n"
12875                    "\t */\n"
12876                    "\t int i;\n"
12877                    "}",
12878                    Tab));
12879   Tab.TabWidth = 2;
12880   Tab.IndentWidth = 2;
12881   EXPECT_EQ("{\n"
12882             "\t/* aaaa\n"
12883             "\t   bbbb */\n"
12884             "}",
12885             format("{\n"
12886                    "/* aaaa\n"
12887                    "   bbbb */\n"
12888                    "}",
12889                    Tab));
12890   EXPECT_EQ("{\n"
12891             "\t/*\n"
12892             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12893             "\t  bbbbbbbbbbbbb\n"
12894             "\t*/\n"
12895             "}",
12896             format("{\n"
12897                    "/*\n"
12898                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
12899                    "*/\n"
12900                    "}",
12901                    Tab));
12902   Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
12903   Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
12904   Tab.TabWidth = 4;
12905   Tab.IndentWidth = 4;
12906   verifyFormat("class Assign {\n"
12907                "\tvoid f() {\n"
12908                "\t\tint         x      = 123;\n"
12909                "\t\tint         random = 4;\n"
12910                "\t\tstd::string alphabet =\n"
12911                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
12912                "\t}\n"
12913                "};",
12914                Tab);
12915   Tab.AlignOperands = FormatStyle::OAS_Align;
12916   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
12917                "                 cccccccccccccccccccc;",
12918                Tab);
12919   // no alignment
12920   verifyFormat("int aaaaaaaaaa =\n"
12921                "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
12922                Tab);
12923   verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
12924                "       : bbbbbbbbbbbbbb ? 222222222222222\n"
12925                "                        : 333333333333333;",
12926                Tab);
12927   Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
12928   Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
12929   verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
12930                "               + cccccccccccccccccccc;",
12931                Tab);
12932 }
12933 
12934 TEST_F(FormatTest, ZeroTabWidth) {
12935   FormatStyle Tab = getLLVMStyleWithColumns(42);
12936   Tab.IndentWidth = 8;
12937   Tab.UseTab = FormatStyle::UT_Never;
12938   Tab.TabWidth = 0;
12939   EXPECT_EQ("void a(){\n"
12940             "    // line starts with '\t'\n"
12941             "};",
12942             format("void a(){\n"
12943                    "\t// line starts with '\t'\n"
12944                    "};",
12945                    Tab));
12946 
12947   EXPECT_EQ("void a(){\n"
12948             "    // line starts with '\t'\n"
12949             "};",
12950             format("void a(){\n"
12951                    "\t\t// line starts with '\t'\n"
12952                    "};",
12953                    Tab));
12954 
12955   Tab.UseTab = FormatStyle::UT_ForIndentation;
12956   EXPECT_EQ("void a(){\n"
12957             "    // line starts with '\t'\n"
12958             "};",
12959             format("void a(){\n"
12960                    "\t// line starts with '\t'\n"
12961                    "};",
12962                    Tab));
12963 
12964   EXPECT_EQ("void a(){\n"
12965             "    // line starts with '\t'\n"
12966             "};",
12967             format("void a(){\n"
12968                    "\t\t// line starts with '\t'\n"
12969                    "};",
12970                    Tab));
12971 
12972   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
12973   EXPECT_EQ("void a(){\n"
12974             "    // line starts with '\t'\n"
12975             "};",
12976             format("void a(){\n"
12977                    "\t// line starts with '\t'\n"
12978                    "};",
12979                    Tab));
12980 
12981   EXPECT_EQ("void a(){\n"
12982             "    // line starts with '\t'\n"
12983             "};",
12984             format("void a(){\n"
12985                    "\t\t// line starts with '\t'\n"
12986                    "};",
12987                    Tab));
12988 
12989   Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
12990   EXPECT_EQ("void a(){\n"
12991             "    // line starts with '\t'\n"
12992             "};",
12993             format("void a(){\n"
12994                    "\t// line starts with '\t'\n"
12995                    "};",
12996                    Tab));
12997 
12998   EXPECT_EQ("void a(){\n"
12999             "    // line starts with '\t'\n"
13000             "};",
13001             format("void a(){\n"
13002                    "\t\t// line starts with '\t'\n"
13003                    "};",
13004                    Tab));
13005 
13006   Tab.UseTab = FormatStyle::UT_Always;
13007   EXPECT_EQ("void a(){\n"
13008             "// line starts with '\t'\n"
13009             "};",
13010             format("void a(){\n"
13011                    "\t// line starts with '\t'\n"
13012                    "};",
13013                    Tab));
13014 
13015   EXPECT_EQ("void a(){\n"
13016             "// line starts with '\t'\n"
13017             "};",
13018             format("void a(){\n"
13019                    "\t\t// line starts with '\t'\n"
13020                    "};",
13021                    Tab));
13022 }
13023 
13024 TEST_F(FormatTest, CalculatesOriginalColumn) {
13025   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13026             "q\"; /* some\n"
13027             "       comment */",
13028             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13029                    "q\"; /* some\n"
13030                    "       comment */",
13031                    getLLVMStyle()));
13032   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13033             "/* some\n"
13034             "   comment */",
13035             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
13036                    " /* some\n"
13037                    "    comment */",
13038                    getLLVMStyle()));
13039   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13040             "qqq\n"
13041             "/* some\n"
13042             "   comment */",
13043             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13044                    "qqq\n"
13045                    " /* some\n"
13046                    "    comment */",
13047                    getLLVMStyle()));
13048   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13049             "wwww; /* some\n"
13050             "         comment */",
13051             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
13052                    "wwww; /* some\n"
13053                    "         comment */",
13054                    getLLVMStyle()));
13055 }
13056 
13057 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
13058   FormatStyle NoSpace = getLLVMStyle();
13059   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
13060 
13061   verifyFormat("while(true)\n"
13062                "  continue;",
13063                NoSpace);
13064   verifyFormat("for(;;)\n"
13065                "  continue;",
13066                NoSpace);
13067   verifyFormat("if(true)\n"
13068                "  f();\n"
13069                "else if(true)\n"
13070                "  f();",
13071                NoSpace);
13072   verifyFormat("do {\n"
13073                "  do_something();\n"
13074                "} while(something());",
13075                NoSpace);
13076   verifyFormat("switch(x) {\n"
13077                "default:\n"
13078                "  break;\n"
13079                "}",
13080                NoSpace);
13081   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
13082   verifyFormat("size_t x = sizeof(x);", NoSpace);
13083   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
13084   verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
13085   verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
13086   verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
13087   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
13088   verifyFormat("alignas(128) char a[128];", NoSpace);
13089   verifyFormat("size_t x = alignof(MyType);", NoSpace);
13090   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
13091   verifyFormat("int f() throw(Deprecated);", NoSpace);
13092   verifyFormat("typedef void (*cb)(int);", NoSpace);
13093   verifyFormat("T A::operator()();", NoSpace);
13094   verifyFormat("X A::operator++(T);", NoSpace);
13095   verifyFormat("auto lambda = []() { return 0; };", NoSpace);
13096 
13097   FormatStyle Space = getLLVMStyle();
13098   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
13099 
13100   verifyFormat("int f ();", Space);
13101   verifyFormat("void f (int a, T b) {\n"
13102                "  while (true)\n"
13103                "    continue;\n"
13104                "}",
13105                Space);
13106   verifyFormat("if (true)\n"
13107                "  f ();\n"
13108                "else if (true)\n"
13109                "  f ();",
13110                Space);
13111   verifyFormat("do {\n"
13112                "  do_something ();\n"
13113                "} while (something ());",
13114                Space);
13115   verifyFormat("switch (x) {\n"
13116                "default:\n"
13117                "  break;\n"
13118                "}",
13119                Space);
13120   verifyFormat("A::A () : a (1) {}", Space);
13121   verifyFormat("void f () __attribute__ ((asdf));", Space);
13122   verifyFormat("*(&a + 1);\n"
13123                "&((&a)[1]);\n"
13124                "a[(b + c) * d];\n"
13125                "(((a + 1) * 2) + 3) * 4;",
13126                Space);
13127   verifyFormat("#define A(x) x", Space);
13128   verifyFormat("#define A (x) x", Space);
13129   verifyFormat("#if defined(x)\n"
13130                "#endif",
13131                Space);
13132   verifyFormat("auto i = std::make_unique<int> (5);", Space);
13133   verifyFormat("size_t x = sizeof (x);", Space);
13134   verifyFormat("auto f (int x) -> decltype (x);", Space);
13135   verifyFormat("auto f (int x) -> typeof (x);", Space);
13136   verifyFormat("auto f (int x) -> _Atomic (x);", Space);
13137   verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
13138   verifyFormat("int f (T x) noexcept (x.create ());", Space);
13139   verifyFormat("alignas (128) char a[128];", Space);
13140   verifyFormat("size_t x = alignof (MyType);", Space);
13141   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
13142   verifyFormat("int f () throw (Deprecated);", Space);
13143   verifyFormat("typedef void (*cb) (int);", Space);
13144   verifyFormat("T A::operator() ();", Space);
13145   verifyFormat("X A::operator++ (T);", Space);
13146   verifyFormat("auto lambda = [] () { return 0; };", Space);
13147   verifyFormat("int x = int (y);", Space);
13148 
13149   FormatStyle SomeSpace = getLLVMStyle();
13150   SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
13151 
13152   verifyFormat("[]() -> float {}", SomeSpace);
13153   verifyFormat("[] (auto foo) {}", SomeSpace);
13154   verifyFormat("[foo]() -> int {}", SomeSpace);
13155   verifyFormat("int f();", SomeSpace);
13156   verifyFormat("void f (int a, T b) {\n"
13157                "  while (true)\n"
13158                "    continue;\n"
13159                "}",
13160                SomeSpace);
13161   verifyFormat("if (true)\n"
13162                "  f();\n"
13163                "else if (true)\n"
13164                "  f();",
13165                SomeSpace);
13166   verifyFormat("do {\n"
13167                "  do_something();\n"
13168                "} while (something());",
13169                SomeSpace);
13170   verifyFormat("switch (x) {\n"
13171                "default:\n"
13172                "  break;\n"
13173                "}",
13174                SomeSpace);
13175   verifyFormat("A::A() : a (1) {}", SomeSpace);
13176   verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
13177   verifyFormat("*(&a + 1);\n"
13178                "&((&a)[1]);\n"
13179                "a[(b + c) * d];\n"
13180                "(((a + 1) * 2) + 3) * 4;",
13181                SomeSpace);
13182   verifyFormat("#define A(x) x", SomeSpace);
13183   verifyFormat("#define A (x) x", SomeSpace);
13184   verifyFormat("#if defined(x)\n"
13185                "#endif",
13186                SomeSpace);
13187   verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
13188   verifyFormat("size_t x = sizeof (x);", SomeSpace);
13189   verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
13190   verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
13191   verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
13192   verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
13193   verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
13194   verifyFormat("alignas (128) char a[128];", SomeSpace);
13195   verifyFormat("size_t x = alignof (MyType);", SomeSpace);
13196   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
13197                SomeSpace);
13198   verifyFormat("int f() throw (Deprecated);", SomeSpace);
13199   verifyFormat("typedef void (*cb) (int);", SomeSpace);
13200   verifyFormat("T A::operator()();", SomeSpace);
13201   verifyFormat("X A::operator++ (T);", SomeSpace);
13202   verifyFormat("int x = int (y);", SomeSpace);
13203   verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
13204 }
13205 
13206 TEST_F(FormatTest, SpaceAfterLogicalNot) {
13207   FormatStyle Spaces = getLLVMStyle();
13208   Spaces.SpaceAfterLogicalNot = true;
13209 
13210   verifyFormat("bool x = ! y", Spaces);
13211   verifyFormat("if (! isFailure())", Spaces);
13212   verifyFormat("if (! (a && b))", Spaces);
13213   verifyFormat("\"Error!\"", Spaces);
13214   verifyFormat("! ! x", Spaces);
13215 }
13216 
13217 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
13218   FormatStyle Spaces = getLLVMStyle();
13219 
13220   Spaces.SpacesInParentheses = true;
13221   verifyFormat("do_something( ::globalVar );", Spaces);
13222   verifyFormat("call( x, y, z );", Spaces);
13223   verifyFormat("call();", Spaces);
13224   verifyFormat("std::function<void( int, int )> callback;", Spaces);
13225   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
13226                Spaces);
13227   verifyFormat("while ( (bool)1 )\n"
13228                "  continue;",
13229                Spaces);
13230   verifyFormat("for ( ;; )\n"
13231                "  continue;",
13232                Spaces);
13233   verifyFormat("if ( true )\n"
13234                "  f();\n"
13235                "else if ( true )\n"
13236                "  f();",
13237                Spaces);
13238   verifyFormat("do {\n"
13239                "  do_something( (int)i );\n"
13240                "} while ( something() );",
13241                Spaces);
13242   verifyFormat("switch ( x ) {\n"
13243                "default:\n"
13244                "  break;\n"
13245                "}",
13246                Spaces);
13247 
13248   Spaces.SpacesInParentheses = false;
13249   Spaces.SpacesInCStyleCastParentheses = true;
13250   verifyFormat("Type *A = ( Type * )P;", Spaces);
13251   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
13252   verifyFormat("x = ( int32 )y;", Spaces);
13253   verifyFormat("int a = ( int )(2.0f);", Spaces);
13254   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
13255   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
13256   verifyFormat("#define x (( int )-1)", Spaces);
13257 
13258   // Run the first set of tests again with:
13259   Spaces.SpacesInParentheses = false;
13260   Spaces.SpaceInEmptyParentheses = true;
13261   Spaces.SpacesInCStyleCastParentheses = true;
13262   verifyFormat("call(x, y, z);", Spaces);
13263   verifyFormat("call( );", Spaces);
13264   verifyFormat("std::function<void(int, int)> callback;", Spaces);
13265   verifyFormat("while (( bool )1)\n"
13266                "  continue;",
13267                Spaces);
13268   verifyFormat("for (;;)\n"
13269                "  continue;",
13270                Spaces);
13271   verifyFormat("if (true)\n"
13272                "  f( );\n"
13273                "else if (true)\n"
13274                "  f( );",
13275                Spaces);
13276   verifyFormat("do {\n"
13277                "  do_something(( int )i);\n"
13278                "} while (something( ));",
13279                Spaces);
13280   verifyFormat("switch (x) {\n"
13281                "default:\n"
13282                "  break;\n"
13283                "}",
13284                Spaces);
13285 
13286   // Run the first set of tests again with:
13287   Spaces.SpaceAfterCStyleCast = true;
13288   verifyFormat("call(x, y, z);", Spaces);
13289   verifyFormat("call( );", Spaces);
13290   verifyFormat("std::function<void(int, int)> callback;", Spaces);
13291   verifyFormat("while (( bool ) 1)\n"
13292                "  continue;",
13293                Spaces);
13294   verifyFormat("for (;;)\n"
13295                "  continue;",
13296                Spaces);
13297   verifyFormat("if (true)\n"
13298                "  f( );\n"
13299                "else if (true)\n"
13300                "  f( );",
13301                Spaces);
13302   verifyFormat("do {\n"
13303                "  do_something(( int ) i);\n"
13304                "} while (something( ));",
13305                Spaces);
13306   verifyFormat("switch (x) {\n"
13307                "default:\n"
13308                "  break;\n"
13309                "}",
13310                Spaces);
13311 
13312   // Run subset of tests again with:
13313   Spaces.SpacesInCStyleCastParentheses = false;
13314   Spaces.SpaceAfterCStyleCast = true;
13315   verifyFormat("while ((bool) 1)\n"
13316                "  continue;",
13317                Spaces);
13318   verifyFormat("do {\n"
13319                "  do_something((int) i);\n"
13320                "} while (something( ));",
13321                Spaces);
13322 
13323   verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
13324   verifyFormat("size_t idx = (size_t) a;", Spaces);
13325   verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
13326   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
13327   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
13328   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
13329   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
13330   Spaces.ColumnLimit = 80;
13331   Spaces.IndentWidth = 4;
13332   Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13333   verifyFormat("void foo( ) {\n"
13334                "    size_t foo = (*(function))(\n"
13335                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
13336                "BarrrrrrrrrrrrLong,\n"
13337                "        FoooooooooLooooong);\n"
13338                "}",
13339                Spaces);
13340   Spaces.SpaceAfterCStyleCast = false;
13341   verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
13342   verifyFormat("size_t idx = (size_t)a;", Spaces);
13343   verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
13344   verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
13345   verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
13346   verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
13347   verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
13348 
13349   verifyFormat("void foo( ) {\n"
13350                "    size_t foo = (*(function))(\n"
13351                "        Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
13352                "BarrrrrrrrrrrrLong,\n"
13353                "        FoooooooooLooooong);\n"
13354                "}",
13355                Spaces);
13356 }
13357 
13358 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
13359   verifyFormat("int a[5];");
13360   verifyFormat("a[3] += 42;");
13361 
13362   FormatStyle Spaces = getLLVMStyle();
13363   Spaces.SpacesInSquareBrackets = true;
13364   // Not lambdas.
13365   verifyFormat("int a[ 5 ];", Spaces);
13366   verifyFormat("a[ 3 ] += 42;", Spaces);
13367   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
13368   verifyFormat("double &operator[](int i) { return 0; }\n"
13369                "int i;",
13370                Spaces);
13371   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
13372   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
13373   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
13374   // Lambdas.
13375   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
13376   verifyFormat("return [ i, args... ] {};", Spaces);
13377   verifyFormat("int foo = [ &bar ]() {};", Spaces);
13378   verifyFormat("int foo = [ = ]() {};", Spaces);
13379   verifyFormat("int foo = [ & ]() {};", Spaces);
13380   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
13381   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
13382 }
13383 
13384 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
13385   FormatStyle NoSpaceStyle = getLLVMStyle();
13386   verifyFormat("int a[5];", NoSpaceStyle);
13387   verifyFormat("a[3] += 42;", NoSpaceStyle);
13388 
13389   verifyFormat("int a[1];", NoSpaceStyle);
13390   verifyFormat("int 1 [a];", NoSpaceStyle);
13391   verifyFormat("int a[1][2];", NoSpaceStyle);
13392   verifyFormat("a[7] = 5;", NoSpaceStyle);
13393   verifyFormat("int a = (f())[23];", NoSpaceStyle);
13394   verifyFormat("f([] {})", NoSpaceStyle);
13395 
13396   FormatStyle Space = getLLVMStyle();
13397   Space.SpaceBeforeSquareBrackets = true;
13398   verifyFormat("int c = []() -> int { return 2; }();\n", Space);
13399   verifyFormat("return [i, args...] {};", Space);
13400 
13401   verifyFormat("int a [5];", Space);
13402   verifyFormat("a [3] += 42;", Space);
13403   verifyFormat("constexpr char hello []{\"hello\"};", Space);
13404   verifyFormat("double &operator[](int i) { return 0; }\n"
13405                "int i;",
13406                Space);
13407   verifyFormat("std::unique_ptr<int []> foo() {}", Space);
13408   verifyFormat("int i = a [a][a]->f();", Space);
13409   verifyFormat("int i = (*b) [a]->f();", Space);
13410 
13411   verifyFormat("int a [1];", Space);
13412   verifyFormat("int 1 [a];", Space);
13413   verifyFormat("int a [1][2];", Space);
13414   verifyFormat("a [7] = 5;", Space);
13415   verifyFormat("int a = (f()) [23];", Space);
13416   verifyFormat("f([] {})", Space);
13417 }
13418 
13419 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
13420   verifyFormat("int a = 5;");
13421   verifyFormat("a += 42;");
13422   verifyFormat("a or_eq 8;");
13423 
13424   FormatStyle Spaces = getLLVMStyle();
13425   Spaces.SpaceBeforeAssignmentOperators = false;
13426   verifyFormat("int a= 5;", Spaces);
13427   verifyFormat("a+= 42;", Spaces);
13428   verifyFormat("a or_eq 8;", Spaces);
13429 }
13430 
13431 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
13432   verifyFormat("class Foo : public Bar {};");
13433   verifyFormat("Foo::Foo() : foo(1) {}");
13434   verifyFormat("for (auto a : b) {\n}");
13435   verifyFormat("int x = a ? b : c;");
13436   verifyFormat("{\n"
13437                "label0:\n"
13438                "  int x = 0;\n"
13439                "}");
13440   verifyFormat("switch (x) {\n"
13441                "case 1:\n"
13442                "default:\n"
13443                "}");
13444   verifyFormat("switch (allBraces) {\n"
13445                "case 1: {\n"
13446                "  break;\n"
13447                "}\n"
13448                "case 2: {\n"
13449                "  [[fallthrough]];\n"
13450                "}\n"
13451                "default: {\n"
13452                "  break;\n"
13453                "}\n"
13454                "}");
13455 
13456   FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
13457   CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
13458   verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
13459   verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
13460   verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
13461   verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
13462   verifyFormat("{\n"
13463                "label1:\n"
13464                "  int x = 0;\n"
13465                "}",
13466                CtorInitializerStyle);
13467   verifyFormat("switch (x) {\n"
13468                "case 1:\n"
13469                "default:\n"
13470                "}",
13471                CtorInitializerStyle);
13472   verifyFormat("switch (allBraces) {\n"
13473                "case 1: {\n"
13474                "  break;\n"
13475                "}\n"
13476                "case 2: {\n"
13477                "  [[fallthrough]];\n"
13478                "}\n"
13479                "default: {\n"
13480                "  break;\n"
13481                "}\n"
13482                "}",
13483                CtorInitializerStyle);
13484   CtorInitializerStyle.BreakConstructorInitializers =
13485       FormatStyle::BCIS_AfterColon;
13486   verifyFormat("Fooooooooooo::Fooooooooooo():\n"
13487                "    aaaaaaaaaaaaaaaa(1),\n"
13488                "    bbbbbbbbbbbbbbbb(2) {}",
13489                CtorInitializerStyle);
13490   CtorInitializerStyle.BreakConstructorInitializers =
13491       FormatStyle::BCIS_BeforeComma;
13492   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
13493                "    : aaaaaaaaaaaaaaaa(1)\n"
13494                "    , bbbbbbbbbbbbbbbb(2) {}",
13495                CtorInitializerStyle);
13496   CtorInitializerStyle.BreakConstructorInitializers =
13497       FormatStyle::BCIS_BeforeColon;
13498   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
13499                "    : aaaaaaaaaaaaaaaa(1),\n"
13500                "      bbbbbbbbbbbbbbbb(2) {}",
13501                CtorInitializerStyle);
13502   CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
13503   verifyFormat("Fooooooooooo::Fooooooooooo()\n"
13504                ": aaaaaaaaaaaaaaaa(1),\n"
13505                "  bbbbbbbbbbbbbbbb(2) {}",
13506                CtorInitializerStyle);
13507 
13508   FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
13509   InheritanceStyle.SpaceBeforeInheritanceColon = false;
13510   verifyFormat("class Foo: public Bar {};", InheritanceStyle);
13511   verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
13512   verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
13513   verifyFormat("int x = a ? b : c;", InheritanceStyle);
13514   verifyFormat("{\n"
13515                "label2:\n"
13516                "  int x = 0;\n"
13517                "}",
13518                InheritanceStyle);
13519   verifyFormat("switch (x) {\n"
13520                "case 1:\n"
13521                "default:\n"
13522                "}",
13523                InheritanceStyle);
13524   verifyFormat("switch (allBraces) {\n"
13525                "case 1: {\n"
13526                "  break;\n"
13527                "}\n"
13528                "case 2: {\n"
13529                "  [[fallthrough]];\n"
13530                "}\n"
13531                "default: {\n"
13532                "  break;\n"
13533                "}\n"
13534                "}",
13535                InheritanceStyle);
13536   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
13537   verifyFormat("class Foooooooooooooooooooooo\n"
13538                "    : public aaaaaaaaaaaaaaaaaa,\n"
13539                "      public bbbbbbbbbbbbbbbbbb {\n"
13540                "}",
13541                InheritanceStyle);
13542   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
13543   verifyFormat("class Foooooooooooooooooooooo:\n"
13544                "    public aaaaaaaaaaaaaaaaaa,\n"
13545                "    public bbbbbbbbbbbbbbbbbb {\n"
13546                "}",
13547                InheritanceStyle);
13548   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
13549   verifyFormat("class Foooooooooooooooooooooo\n"
13550                "    : public aaaaaaaaaaaaaaaaaa\n"
13551                "    , public bbbbbbbbbbbbbbbbbb {\n"
13552                "}",
13553                InheritanceStyle);
13554   InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
13555   verifyFormat("class Foooooooooooooooooooooo\n"
13556                "    : public aaaaaaaaaaaaaaaaaa,\n"
13557                "      public bbbbbbbbbbbbbbbbbb {\n"
13558                "}",
13559                InheritanceStyle);
13560   InheritanceStyle.ConstructorInitializerIndentWidth = 0;
13561   verifyFormat("class Foooooooooooooooooooooo\n"
13562                ": public aaaaaaaaaaaaaaaaaa,\n"
13563                "  public bbbbbbbbbbbbbbbbbb {}",
13564                InheritanceStyle);
13565 
13566   FormatStyle ForLoopStyle = getLLVMStyle();
13567   ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
13568   verifyFormat("class Foo : public Bar {};", ForLoopStyle);
13569   verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
13570   verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
13571   verifyFormat("int x = a ? b : c;", ForLoopStyle);
13572   verifyFormat("{\n"
13573                "label2:\n"
13574                "  int x = 0;\n"
13575                "}",
13576                ForLoopStyle);
13577   verifyFormat("switch (x) {\n"
13578                "case 1:\n"
13579                "default:\n"
13580                "}",
13581                ForLoopStyle);
13582   verifyFormat("switch (allBraces) {\n"
13583                "case 1: {\n"
13584                "  break;\n"
13585                "}\n"
13586                "case 2: {\n"
13587                "  [[fallthrough]];\n"
13588                "}\n"
13589                "default: {\n"
13590                "  break;\n"
13591                "}\n"
13592                "}",
13593                ForLoopStyle);
13594 
13595   FormatStyle CaseStyle = getLLVMStyle();
13596   CaseStyle.SpaceBeforeCaseColon = true;
13597   verifyFormat("class Foo : public Bar {};", CaseStyle);
13598   verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
13599   verifyFormat("for (auto a : b) {\n}", CaseStyle);
13600   verifyFormat("int x = a ? b : c;", CaseStyle);
13601   verifyFormat("switch (x) {\n"
13602                "case 1 :\n"
13603                "default :\n"
13604                "}",
13605                CaseStyle);
13606   verifyFormat("switch (allBraces) {\n"
13607                "case 1 : {\n"
13608                "  break;\n"
13609                "}\n"
13610                "case 2 : {\n"
13611                "  [[fallthrough]];\n"
13612                "}\n"
13613                "default : {\n"
13614                "  break;\n"
13615                "}\n"
13616                "}",
13617                CaseStyle);
13618 
13619   FormatStyle NoSpaceStyle = getLLVMStyle();
13620   EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
13621   NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
13622   NoSpaceStyle.SpaceBeforeInheritanceColon = false;
13623   NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
13624   verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
13625   verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
13626   verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
13627   verifyFormat("int x = a ? b : c;", NoSpaceStyle);
13628   verifyFormat("{\n"
13629                "label3:\n"
13630                "  int x = 0;\n"
13631                "}",
13632                NoSpaceStyle);
13633   verifyFormat("switch (x) {\n"
13634                "case 1:\n"
13635                "default:\n"
13636                "}",
13637                NoSpaceStyle);
13638   verifyFormat("switch (allBraces) {\n"
13639                "case 1: {\n"
13640                "  break;\n"
13641                "}\n"
13642                "case 2: {\n"
13643                "  [[fallthrough]];\n"
13644                "}\n"
13645                "default: {\n"
13646                "  break;\n"
13647                "}\n"
13648                "}",
13649                NoSpaceStyle);
13650 
13651   FormatStyle InvertedSpaceStyle = getLLVMStyle();
13652   InvertedSpaceStyle.SpaceBeforeCaseColon = true;
13653   InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
13654   InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
13655   InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
13656   verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
13657   verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
13658   verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
13659   verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
13660   verifyFormat("{\n"
13661                "label3:\n"
13662                "  int x = 0;\n"
13663                "}",
13664                InvertedSpaceStyle);
13665   verifyFormat("switch (x) {\n"
13666                "case 1 :\n"
13667                "case 2 : {\n"
13668                "  break;\n"
13669                "}\n"
13670                "default :\n"
13671                "  break;\n"
13672                "}",
13673                InvertedSpaceStyle);
13674   verifyFormat("switch (allBraces) {\n"
13675                "case 1 : {\n"
13676                "  break;\n"
13677                "}\n"
13678                "case 2 : {\n"
13679                "  [[fallthrough]];\n"
13680                "}\n"
13681                "default : {\n"
13682                "  break;\n"
13683                "}\n"
13684                "}",
13685                InvertedSpaceStyle);
13686 }
13687 
13688 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
13689   FormatStyle Style = getLLVMStyle();
13690 
13691   Style.PointerAlignment = FormatStyle::PAS_Left;
13692   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
13693   verifyFormat("void* const* x = NULL;", Style);
13694 
13695 #define verifyQualifierSpaces(Code, Pointers, Qualifiers)                      \
13696   do {                                                                         \
13697     Style.PointerAlignment = FormatStyle::Pointers;                            \
13698     Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers;              \
13699     verifyFormat(Code, Style);                                                 \
13700   } while (false)
13701 
13702   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
13703   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
13704   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
13705 
13706   verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
13707   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
13708   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
13709 
13710   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
13711   verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
13712   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
13713 
13714   verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
13715   verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
13716   verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
13717 
13718   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
13719   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
13720                         SAPQ_Default);
13721   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
13722                         SAPQ_Default);
13723 
13724   verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
13725   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
13726                         SAPQ_Before);
13727   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
13728                         SAPQ_Before);
13729 
13730   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
13731   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
13732   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
13733                         SAPQ_After);
13734 
13735   verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
13736   verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
13737   verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
13738 
13739 #undef verifyQualifierSpaces
13740 
13741   FormatStyle Spaces = getLLVMStyle();
13742   Spaces.AttributeMacros.push_back("qualified");
13743   Spaces.PointerAlignment = FormatStyle::PAS_Right;
13744   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
13745   verifyFormat("SomeType *volatile *a = NULL;", Spaces);
13746   verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
13747   verifyFormat("std::vector<SomeType *const *> x;", Spaces);
13748   verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
13749   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13750   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
13751   verifyFormat("SomeType * volatile *a = NULL;", Spaces);
13752   verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
13753   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
13754   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
13755   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13756 
13757   // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
13758   Spaces.PointerAlignment = FormatStyle::PAS_Left;
13759   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
13760   verifyFormat("SomeType* volatile* a = NULL;", Spaces);
13761   verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
13762   verifyFormat("std::vector<SomeType* const*> x;", Spaces);
13763   verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
13764   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13765   // However, setting it to SAPQ_After should add spaces after __attribute, etc.
13766   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
13767   verifyFormat("SomeType* volatile * a = NULL;", Spaces);
13768   verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
13769   verifyFormat("std::vector<SomeType* const *> x;", Spaces);
13770   verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
13771   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13772 
13773   // PAS_Middle should not have any noticeable changes even for SAPQ_Both
13774   Spaces.PointerAlignment = FormatStyle::PAS_Middle;
13775   Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
13776   verifyFormat("SomeType * volatile * a = NULL;", Spaces);
13777   verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
13778   verifyFormat("std::vector<SomeType * const *> x;", Spaces);
13779   verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
13780   verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
13781 }
13782 
13783 TEST_F(FormatTest, AlignConsecutiveMacros) {
13784   FormatStyle Style = getLLVMStyle();
13785   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
13786   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
13787   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
13788 
13789   verifyFormat("#define a 3\n"
13790                "#define bbbb 4\n"
13791                "#define ccc (5)",
13792                Style);
13793 
13794   verifyFormat("#define f(x) (x * x)\n"
13795                "#define fff(x, y, z) (x * y + z)\n"
13796                "#define ffff(x, y) (x - y)",
13797                Style);
13798 
13799   verifyFormat("#define foo(x, y) (x + y)\n"
13800                "#define bar (5, 6)(2 + 2)",
13801                Style);
13802 
13803   verifyFormat("#define a 3\n"
13804                "#define bbbb 4\n"
13805                "#define ccc (5)\n"
13806                "#define f(x) (x * x)\n"
13807                "#define fff(x, y, z) (x * y + z)\n"
13808                "#define ffff(x, y) (x - y)",
13809                Style);
13810 
13811   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
13812   verifyFormat("#define a    3\n"
13813                "#define bbbb 4\n"
13814                "#define ccc  (5)",
13815                Style);
13816 
13817   verifyFormat("#define f(x)         (x * x)\n"
13818                "#define fff(x, y, z) (x * y + z)\n"
13819                "#define ffff(x, y)   (x - y)",
13820                Style);
13821 
13822   verifyFormat("#define foo(x, y) (x + y)\n"
13823                "#define bar       (5, 6)(2 + 2)",
13824                Style);
13825 
13826   verifyFormat("#define a            3\n"
13827                "#define bbbb         4\n"
13828                "#define ccc          (5)\n"
13829                "#define f(x)         (x * x)\n"
13830                "#define fff(x, y, z) (x * y + z)\n"
13831                "#define ffff(x, y)   (x - y)",
13832                Style);
13833 
13834   verifyFormat("#define a         5\n"
13835                "#define foo(x, y) (x + y)\n"
13836                "#define CCC       (6)\n"
13837                "auto lambda = []() {\n"
13838                "  auto  ii = 0;\n"
13839                "  float j  = 0;\n"
13840                "  return 0;\n"
13841                "};\n"
13842                "int   i  = 0;\n"
13843                "float i2 = 0;\n"
13844                "auto  v  = type{\n"
13845                "    i = 1,   //\n"
13846                "    (i = 2), //\n"
13847                "    i = 3    //\n"
13848                "};",
13849                Style);
13850 
13851   Style.AlignConsecutiveMacros = FormatStyle::ACS_None;
13852   Style.ColumnLimit = 20;
13853 
13854   verifyFormat("#define a          \\\n"
13855                "  \"aabbbbbbbbbbbb\"\n"
13856                "#define D          \\\n"
13857                "  \"aabbbbbbbbbbbb\" \\\n"
13858                "  \"ccddeeeeeeeee\"\n"
13859                "#define B          \\\n"
13860                "  \"QQQQQQQQQQQQQ\"  \\\n"
13861                "  \"FFFFFFFFFFFFF\"  \\\n"
13862                "  \"LLLLLLLL\"\n",
13863                Style);
13864 
13865   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
13866   verifyFormat("#define a          \\\n"
13867                "  \"aabbbbbbbbbbbb\"\n"
13868                "#define D          \\\n"
13869                "  \"aabbbbbbbbbbbb\" \\\n"
13870                "  \"ccddeeeeeeeee\"\n"
13871                "#define B          \\\n"
13872                "  \"QQQQQQQQQQQQQ\"  \\\n"
13873                "  \"FFFFFFFFFFFFF\"  \\\n"
13874                "  \"LLLLLLLL\"\n",
13875                Style);
13876 
13877   // Test across comments
13878   Style.MaxEmptyLinesToKeep = 10;
13879   Style.ReflowComments = false;
13880   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments;
13881   EXPECT_EQ("#define a    3\n"
13882             "// line comment\n"
13883             "#define bbbb 4\n"
13884             "#define ccc  (5)",
13885             format("#define a 3\n"
13886                    "// line comment\n"
13887                    "#define bbbb 4\n"
13888                    "#define ccc (5)",
13889                    Style));
13890 
13891   EXPECT_EQ("#define a    3\n"
13892             "/* block comment */\n"
13893             "#define bbbb 4\n"
13894             "#define ccc  (5)",
13895             format("#define a  3\n"
13896                    "/* block comment */\n"
13897                    "#define bbbb 4\n"
13898                    "#define ccc (5)",
13899                    Style));
13900 
13901   EXPECT_EQ("#define a    3\n"
13902             "/* multi-line *\n"
13903             " * block comment */\n"
13904             "#define bbbb 4\n"
13905             "#define ccc  (5)",
13906             format("#define a 3\n"
13907                    "/* multi-line *\n"
13908                    " * block comment */\n"
13909                    "#define bbbb 4\n"
13910                    "#define ccc (5)",
13911                    Style));
13912 
13913   EXPECT_EQ("#define a    3\n"
13914             "// multi-line line comment\n"
13915             "//\n"
13916             "#define bbbb 4\n"
13917             "#define ccc  (5)",
13918             format("#define a  3\n"
13919                    "// multi-line line comment\n"
13920                    "//\n"
13921                    "#define bbbb 4\n"
13922                    "#define ccc (5)",
13923                    Style));
13924 
13925   EXPECT_EQ("#define a 3\n"
13926             "// empty lines still break.\n"
13927             "\n"
13928             "#define bbbb 4\n"
13929             "#define ccc  (5)",
13930             format("#define a     3\n"
13931                    "// empty lines still break.\n"
13932                    "\n"
13933                    "#define bbbb     4\n"
13934                    "#define ccc  (5)",
13935                    Style));
13936 
13937   // Test across empty lines
13938   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines;
13939   EXPECT_EQ("#define a    3\n"
13940             "\n"
13941             "#define bbbb 4\n"
13942             "#define ccc  (5)",
13943             format("#define a 3\n"
13944                    "\n"
13945                    "#define bbbb 4\n"
13946                    "#define ccc (5)",
13947                    Style));
13948 
13949   EXPECT_EQ("#define a    3\n"
13950             "\n"
13951             "\n"
13952             "\n"
13953             "#define bbbb 4\n"
13954             "#define ccc  (5)",
13955             format("#define a        3\n"
13956                    "\n"
13957                    "\n"
13958                    "\n"
13959                    "#define bbbb 4\n"
13960                    "#define ccc (5)",
13961                    Style));
13962 
13963   EXPECT_EQ("#define a 3\n"
13964             "// comments should break alignment\n"
13965             "//\n"
13966             "#define bbbb 4\n"
13967             "#define ccc  (5)",
13968             format("#define a        3\n"
13969                    "// comments should break alignment\n"
13970                    "//\n"
13971                    "#define bbbb 4\n"
13972                    "#define ccc (5)",
13973                    Style));
13974 
13975   // Test across empty lines and comments
13976   Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments;
13977   verifyFormat("#define a    3\n"
13978                "\n"
13979                "// line comment\n"
13980                "#define bbbb 4\n"
13981                "#define ccc  (5)",
13982                Style);
13983 
13984   EXPECT_EQ("#define a    3\n"
13985             "\n"
13986             "\n"
13987             "/* multi-line *\n"
13988             " * block comment */\n"
13989             "\n"
13990             "\n"
13991             "#define bbbb 4\n"
13992             "#define ccc  (5)",
13993             format("#define a 3\n"
13994                    "\n"
13995                    "\n"
13996                    "/* multi-line *\n"
13997                    " * block comment */\n"
13998                    "\n"
13999                    "\n"
14000                    "#define bbbb 4\n"
14001                    "#define ccc (5)",
14002                    Style));
14003 
14004   EXPECT_EQ("#define a    3\n"
14005             "\n"
14006             "\n"
14007             "/* multi-line *\n"
14008             " * block comment */\n"
14009             "\n"
14010             "\n"
14011             "#define bbbb 4\n"
14012             "#define ccc  (5)",
14013             format("#define a 3\n"
14014                    "\n"
14015                    "\n"
14016                    "/* multi-line *\n"
14017                    " * block comment */\n"
14018                    "\n"
14019                    "\n"
14020                    "#define bbbb 4\n"
14021                    "#define ccc       (5)",
14022                    Style));
14023 }
14024 
14025 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
14026   FormatStyle Alignment = getLLVMStyle();
14027   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14028   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines;
14029 
14030   Alignment.MaxEmptyLinesToKeep = 10;
14031   /* Test alignment across empty lines */
14032   EXPECT_EQ("int a           = 5;\n"
14033             "\n"
14034             "int oneTwoThree = 123;",
14035             format("int a       = 5;\n"
14036                    "\n"
14037                    "int oneTwoThree= 123;",
14038                    Alignment));
14039   EXPECT_EQ("int a           = 5;\n"
14040             "int one         = 1;\n"
14041             "\n"
14042             "int oneTwoThree = 123;",
14043             format("int a = 5;\n"
14044                    "int one = 1;\n"
14045                    "\n"
14046                    "int oneTwoThree = 123;",
14047                    Alignment));
14048   EXPECT_EQ("int a           = 5;\n"
14049             "int one         = 1;\n"
14050             "\n"
14051             "int oneTwoThree = 123;\n"
14052             "int oneTwo      = 12;",
14053             format("int a = 5;\n"
14054                    "int one = 1;\n"
14055                    "\n"
14056                    "int oneTwoThree = 123;\n"
14057                    "int oneTwo = 12;",
14058                    Alignment));
14059 
14060   /* Test across comments */
14061   EXPECT_EQ("int a = 5;\n"
14062             "/* block comment */\n"
14063             "int oneTwoThree = 123;",
14064             format("int a = 5;\n"
14065                    "/* block comment */\n"
14066                    "int oneTwoThree=123;",
14067                    Alignment));
14068 
14069   EXPECT_EQ("int a = 5;\n"
14070             "// line comment\n"
14071             "int oneTwoThree = 123;",
14072             format("int a = 5;\n"
14073                    "// line comment\n"
14074                    "int oneTwoThree=123;",
14075                    Alignment));
14076 
14077   /* Test across comments and newlines */
14078   EXPECT_EQ("int a = 5;\n"
14079             "\n"
14080             "/* block comment */\n"
14081             "int oneTwoThree = 123;",
14082             format("int a = 5;\n"
14083                    "\n"
14084                    "/* block comment */\n"
14085                    "int oneTwoThree=123;",
14086                    Alignment));
14087 
14088   EXPECT_EQ("int a = 5;\n"
14089             "\n"
14090             "// line comment\n"
14091             "int oneTwoThree = 123;",
14092             format("int a = 5;\n"
14093                    "\n"
14094                    "// line comment\n"
14095                    "int oneTwoThree=123;",
14096                    Alignment));
14097 }
14098 
14099 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
14100   FormatStyle Alignment = getLLVMStyle();
14101   Alignment.AlignConsecutiveDeclarations =
14102       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14103   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14104 
14105   Alignment.MaxEmptyLinesToKeep = 10;
14106   /* Test alignment across empty lines */
14107   EXPECT_EQ("int         a = 5;\n"
14108             "\n"
14109             "float const oneTwoThree = 123;",
14110             format("int a = 5;\n"
14111                    "\n"
14112                    "float const oneTwoThree = 123;",
14113                    Alignment));
14114   EXPECT_EQ("int         a = 5;\n"
14115             "float const one = 1;\n"
14116             "\n"
14117             "int         oneTwoThree = 123;",
14118             format("int a = 5;\n"
14119                    "float const one = 1;\n"
14120                    "\n"
14121                    "int oneTwoThree = 123;",
14122                    Alignment));
14123 
14124   /* Test across comments */
14125   EXPECT_EQ("float const a = 5;\n"
14126             "/* block comment */\n"
14127             "int         oneTwoThree = 123;",
14128             format("float const a = 5;\n"
14129                    "/* block comment */\n"
14130                    "int oneTwoThree=123;",
14131                    Alignment));
14132 
14133   EXPECT_EQ("float const a = 5;\n"
14134             "// line comment\n"
14135             "int         oneTwoThree = 123;",
14136             format("float const a = 5;\n"
14137                    "// line comment\n"
14138                    "int oneTwoThree=123;",
14139                    Alignment));
14140 
14141   /* Test across comments and newlines */
14142   EXPECT_EQ("float const a = 5;\n"
14143             "\n"
14144             "/* block comment */\n"
14145             "int         oneTwoThree = 123;",
14146             format("float const a = 5;\n"
14147                    "\n"
14148                    "/* block comment */\n"
14149                    "int         oneTwoThree=123;",
14150                    Alignment));
14151 
14152   EXPECT_EQ("float const a = 5;\n"
14153             "\n"
14154             "// line comment\n"
14155             "int         oneTwoThree = 123;",
14156             format("float const a = 5;\n"
14157                    "\n"
14158                    "// line comment\n"
14159                    "int oneTwoThree=123;",
14160                    Alignment));
14161 }
14162 
14163 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
14164   FormatStyle Alignment = getLLVMStyle();
14165   Alignment.AlignConsecutiveBitFields =
14166       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14167 
14168   Alignment.MaxEmptyLinesToKeep = 10;
14169   /* Test alignment across empty lines */
14170   EXPECT_EQ("int a            : 5;\n"
14171             "\n"
14172             "int longbitfield : 6;",
14173             format("int a : 5;\n"
14174                    "\n"
14175                    "int longbitfield : 6;",
14176                    Alignment));
14177   EXPECT_EQ("int a            : 5;\n"
14178             "int one          : 1;\n"
14179             "\n"
14180             "int longbitfield : 6;",
14181             format("int a : 5;\n"
14182                    "int one : 1;\n"
14183                    "\n"
14184                    "int longbitfield : 6;",
14185                    Alignment));
14186 
14187   /* Test across comments */
14188   EXPECT_EQ("int a            : 5;\n"
14189             "/* block comment */\n"
14190             "int longbitfield : 6;",
14191             format("int a : 5;\n"
14192                    "/* block comment */\n"
14193                    "int longbitfield : 6;",
14194                    Alignment));
14195   EXPECT_EQ("int a            : 5;\n"
14196             "int one          : 1;\n"
14197             "// line comment\n"
14198             "int longbitfield : 6;",
14199             format("int a : 5;\n"
14200                    "int one : 1;\n"
14201                    "// line comment\n"
14202                    "int longbitfield : 6;",
14203                    Alignment));
14204 
14205   /* Test across comments and newlines */
14206   EXPECT_EQ("int a            : 5;\n"
14207             "/* block comment */\n"
14208             "\n"
14209             "int longbitfield : 6;",
14210             format("int a : 5;\n"
14211                    "/* block comment */\n"
14212                    "\n"
14213                    "int longbitfield : 6;",
14214                    Alignment));
14215   EXPECT_EQ("int a            : 5;\n"
14216             "int one          : 1;\n"
14217             "\n"
14218             "// line comment\n"
14219             "\n"
14220             "int longbitfield : 6;",
14221             format("int a : 5;\n"
14222                    "int one : 1;\n"
14223                    "\n"
14224                    "// line comment \n"
14225                    "\n"
14226                    "int longbitfield : 6;",
14227                    Alignment));
14228 }
14229 
14230 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
14231   FormatStyle Alignment = getLLVMStyle();
14232   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14233   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments;
14234 
14235   Alignment.MaxEmptyLinesToKeep = 10;
14236   /* Test alignment across empty lines */
14237   EXPECT_EQ("int a = 5;\n"
14238             "\n"
14239             "int oneTwoThree = 123;",
14240             format("int a       = 5;\n"
14241                    "\n"
14242                    "int oneTwoThree= 123;",
14243                    Alignment));
14244   EXPECT_EQ("int a   = 5;\n"
14245             "int one = 1;\n"
14246             "\n"
14247             "int oneTwoThree = 123;",
14248             format("int a = 5;\n"
14249                    "int one = 1;\n"
14250                    "\n"
14251                    "int oneTwoThree = 123;",
14252                    Alignment));
14253 
14254   /* Test across comments */
14255   EXPECT_EQ("int a           = 5;\n"
14256             "/* block comment */\n"
14257             "int oneTwoThree = 123;",
14258             format("int a = 5;\n"
14259                    "/* block comment */\n"
14260                    "int oneTwoThree=123;",
14261                    Alignment));
14262 
14263   EXPECT_EQ("int a           = 5;\n"
14264             "// line comment\n"
14265             "int oneTwoThree = 123;",
14266             format("int a = 5;\n"
14267                    "// line comment\n"
14268                    "int oneTwoThree=123;",
14269                    Alignment));
14270 
14271   EXPECT_EQ("int a           = 5;\n"
14272             "/*\n"
14273             " * multi-line block comment\n"
14274             " */\n"
14275             "int oneTwoThree = 123;",
14276             format("int a = 5;\n"
14277                    "/*\n"
14278                    " * multi-line block comment\n"
14279                    " */\n"
14280                    "int oneTwoThree=123;",
14281                    Alignment));
14282 
14283   EXPECT_EQ("int a           = 5;\n"
14284             "//\n"
14285             "// multi-line line comment\n"
14286             "//\n"
14287             "int oneTwoThree = 123;",
14288             format("int a = 5;\n"
14289                    "//\n"
14290                    "// multi-line line comment\n"
14291                    "//\n"
14292                    "int oneTwoThree=123;",
14293                    Alignment));
14294 
14295   /* Test across comments and newlines */
14296   EXPECT_EQ("int a = 5;\n"
14297             "\n"
14298             "/* block comment */\n"
14299             "int oneTwoThree = 123;",
14300             format("int a = 5;\n"
14301                    "\n"
14302                    "/* block comment */\n"
14303                    "int oneTwoThree=123;",
14304                    Alignment));
14305 
14306   EXPECT_EQ("int a = 5;\n"
14307             "\n"
14308             "// line comment\n"
14309             "int oneTwoThree = 123;",
14310             format("int a = 5;\n"
14311                    "\n"
14312                    "// line comment\n"
14313                    "int oneTwoThree=123;",
14314                    Alignment));
14315 }
14316 
14317 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
14318   FormatStyle Alignment = getLLVMStyle();
14319   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14320   Alignment.AlignConsecutiveAssignments =
14321       FormatStyle::ACS_AcrossEmptyLinesAndComments;
14322   verifyFormat("int a           = 5;\n"
14323                "int oneTwoThree = 123;",
14324                Alignment);
14325   verifyFormat("int a           = method();\n"
14326                "int oneTwoThree = 133;",
14327                Alignment);
14328   verifyFormat("a &= 5;\n"
14329                "bcd *= 5;\n"
14330                "ghtyf += 5;\n"
14331                "dvfvdb -= 5;\n"
14332                "a /= 5;\n"
14333                "vdsvsv %= 5;\n"
14334                "sfdbddfbdfbb ^= 5;\n"
14335                "dvsdsv |= 5;\n"
14336                "int dsvvdvsdvvv = 123;",
14337                Alignment);
14338   verifyFormat("int i = 1, j = 10;\n"
14339                "something = 2000;",
14340                Alignment);
14341   verifyFormat("something = 2000;\n"
14342                "int i = 1, j = 10;\n",
14343                Alignment);
14344   verifyFormat("something = 2000;\n"
14345                "another   = 911;\n"
14346                "int i = 1, j = 10;\n"
14347                "oneMore = 1;\n"
14348                "i       = 2;",
14349                Alignment);
14350   verifyFormat("int a   = 5;\n"
14351                "int one = 1;\n"
14352                "method();\n"
14353                "int oneTwoThree = 123;\n"
14354                "int oneTwo      = 12;",
14355                Alignment);
14356   verifyFormat("int oneTwoThree = 123;\n"
14357                "int oneTwo      = 12;\n"
14358                "method();\n",
14359                Alignment);
14360   verifyFormat("int oneTwoThree = 123; // comment\n"
14361                "int oneTwo      = 12;  // comment",
14362                Alignment);
14363 
14364   // Bug 25167
14365   /* Uncomment when fixed
14366     verifyFormat("#if A\n"
14367                  "#else\n"
14368                  "int aaaaaaaa = 12;\n"
14369                  "#endif\n"
14370                  "#if B\n"
14371                  "#else\n"
14372                  "int a = 12;\n"
14373                  "#endif\n",
14374                  Alignment);
14375     verifyFormat("enum foo {\n"
14376                  "#if A\n"
14377                  "#else\n"
14378                  "  aaaaaaaa = 12;\n"
14379                  "#endif\n"
14380                  "#if B\n"
14381                  "#else\n"
14382                  "  a = 12;\n"
14383                  "#endif\n"
14384                  "};\n",
14385                  Alignment);
14386   */
14387 
14388   Alignment.MaxEmptyLinesToKeep = 10;
14389   /* Test alignment across empty lines */
14390   EXPECT_EQ("int a           = 5;\n"
14391             "\n"
14392             "int oneTwoThree = 123;",
14393             format("int a       = 5;\n"
14394                    "\n"
14395                    "int oneTwoThree= 123;",
14396                    Alignment));
14397   EXPECT_EQ("int a           = 5;\n"
14398             "int one         = 1;\n"
14399             "\n"
14400             "int oneTwoThree = 123;",
14401             format("int a = 5;\n"
14402                    "int one = 1;\n"
14403                    "\n"
14404                    "int oneTwoThree = 123;",
14405                    Alignment));
14406   EXPECT_EQ("int a           = 5;\n"
14407             "int one         = 1;\n"
14408             "\n"
14409             "int oneTwoThree = 123;\n"
14410             "int oneTwo      = 12;",
14411             format("int a = 5;\n"
14412                    "int one = 1;\n"
14413                    "\n"
14414                    "int oneTwoThree = 123;\n"
14415                    "int oneTwo = 12;",
14416                    Alignment));
14417 
14418   /* Test across comments */
14419   EXPECT_EQ("int a           = 5;\n"
14420             "/* block comment */\n"
14421             "int oneTwoThree = 123;",
14422             format("int a = 5;\n"
14423                    "/* block comment */\n"
14424                    "int oneTwoThree=123;",
14425                    Alignment));
14426 
14427   EXPECT_EQ("int a           = 5;\n"
14428             "// line comment\n"
14429             "int oneTwoThree = 123;",
14430             format("int a = 5;\n"
14431                    "// line comment\n"
14432                    "int oneTwoThree=123;",
14433                    Alignment));
14434 
14435   /* Test across comments and newlines */
14436   EXPECT_EQ("int a           = 5;\n"
14437             "\n"
14438             "/* block comment */\n"
14439             "int oneTwoThree = 123;",
14440             format("int a = 5;\n"
14441                    "\n"
14442                    "/* block comment */\n"
14443                    "int oneTwoThree=123;",
14444                    Alignment));
14445 
14446   EXPECT_EQ("int a           = 5;\n"
14447             "\n"
14448             "// line comment\n"
14449             "int oneTwoThree = 123;",
14450             format("int a = 5;\n"
14451                    "\n"
14452                    "// line comment\n"
14453                    "int oneTwoThree=123;",
14454                    Alignment));
14455 
14456   EXPECT_EQ("int a           = 5;\n"
14457             "//\n"
14458             "// multi-line line comment\n"
14459             "//\n"
14460             "int oneTwoThree = 123;",
14461             format("int a = 5;\n"
14462                    "//\n"
14463                    "// multi-line line comment\n"
14464                    "//\n"
14465                    "int oneTwoThree=123;",
14466                    Alignment));
14467 
14468   EXPECT_EQ("int a           = 5;\n"
14469             "/*\n"
14470             " *  multi-line block comment\n"
14471             " */\n"
14472             "int oneTwoThree = 123;",
14473             format("int a = 5;\n"
14474                    "/*\n"
14475                    " *  multi-line block comment\n"
14476                    " */\n"
14477                    "int oneTwoThree=123;",
14478                    Alignment));
14479 
14480   EXPECT_EQ("int a           = 5;\n"
14481             "\n"
14482             "/* block comment */\n"
14483             "\n"
14484             "\n"
14485             "\n"
14486             "int oneTwoThree = 123;",
14487             format("int a = 5;\n"
14488                    "\n"
14489                    "/* block comment */\n"
14490                    "\n"
14491                    "\n"
14492                    "\n"
14493                    "int oneTwoThree=123;",
14494                    Alignment));
14495 
14496   EXPECT_EQ("int a           = 5;\n"
14497             "\n"
14498             "// line comment\n"
14499             "\n"
14500             "\n"
14501             "\n"
14502             "int oneTwoThree = 123;",
14503             format("int a = 5;\n"
14504                    "\n"
14505                    "// line comment\n"
14506                    "\n"
14507                    "\n"
14508                    "\n"
14509                    "int oneTwoThree=123;",
14510                    Alignment));
14511 
14512   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
14513   verifyFormat("#define A \\\n"
14514                "  int aaaa       = 12; \\\n"
14515                "  int b          = 23; \\\n"
14516                "  int ccc        = 234; \\\n"
14517                "  int dddddddddd = 2345;",
14518                Alignment);
14519   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14520   verifyFormat("#define A               \\\n"
14521                "  int aaaa       = 12;  \\\n"
14522                "  int b          = 23;  \\\n"
14523                "  int ccc        = 234; \\\n"
14524                "  int dddddddddd = 2345;",
14525                Alignment);
14526   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
14527   verifyFormat("#define A                                                      "
14528                "                \\\n"
14529                "  int aaaa       = 12;                                         "
14530                "                \\\n"
14531                "  int b          = 23;                                         "
14532                "                \\\n"
14533                "  int ccc        = 234;                                        "
14534                "                \\\n"
14535                "  int dddddddddd = 2345;",
14536                Alignment);
14537   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
14538                "k = 4, int l = 5,\n"
14539                "                  int m = 6) {\n"
14540                "  int j      = 10;\n"
14541                "  otherThing = 1;\n"
14542                "}",
14543                Alignment);
14544   verifyFormat("void SomeFunction(int parameter = 0) {\n"
14545                "  int i   = 1;\n"
14546                "  int j   = 2;\n"
14547                "  int big = 10000;\n"
14548                "}",
14549                Alignment);
14550   verifyFormat("class C {\n"
14551                "public:\n"
14552                "  int i            = 1;\n"
14553                "  virtual void f() = 0;\n"
14554                "};",
14555                Alignment);
14556   verifyFormat("int i = 1;\n"
14557                "if (SomeType t = getSomething()) {\n"
14558                "}\n"
14559                "int j   = 2;\n"
14560                "int big = 10000;",
14561                Alignment);
14562   verifyFormat("int j = 7;\n"
14563                "for (int k = 0; k < N; ++k) {\n"
14564                "}\n"
14565                "int j   = 2;\n"
14566                "int big = 10000;\n"
14567                "}",
14568                Alignment);
14569   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14570   verifyFormat("int i = 1;\n"
14571                "LooooooooooongType loooooooooooooooooooooongVariable\n"
14572                "    = someLooooooooooooooooongFunction();\n"
14573                "int j = 2;",
14574                Alignment);
14575   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
14576   verifyFormat("int i = 1;\n"
14577                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
14578                "    someLooooooooooooooooongFunction();\n"
14579                "int j = 2;",
14580                Alignment);
14581 
14582   verifyFormat("auto lambda = []() {\n"
14583                "  auto i = 0;\n"
14584                "  return 0;\n"
14585                "};\n"
14586                "int i  = 0;\n"
14587                "auto v = type{\n"
14588                "    i = 1,   //\n"
14589                "    (i = 2), //\n"
14590                "    i = 3    //\n"
14591                "};",
14592                Alignment);
14593 
14594   verifyFormat(
14595       "int i      = 1;\n"
14596       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
14597       "                          loooooooooooooooooooooongParameterB);\n"
14598       "int j      = 2;",
14599       Alignment);
14600 
14601   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
14602                "          typename B   = very_long_type_name_1,\n"
14603                "          typename T_2 = very_long_type_name_2>\n"
14604                "auto foo() {}\n",
14605                Alignment);
14606   verifyFormat("int a, b = 1;\n"
14607                "int c  = 2;\n"
14608                "int dd = 3;\n",
14609                Alignment);
14610   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
14611                "float b[1][] = {{3.f}};\n",
14612                Alignment);
14613   verifyFormat("for (int i = 0; i < 1; i++)\n"
14614                "  int x = 1;\n",
14615                Alignment);
14616   verifyFormat("for (i = 0; i < 1; i++)\n"
14617                "  x = 1;\n"
14618                "y = 1;\n",
14619                Alignment);
14620 
14621   Alignment.ReflowComments = true;
14622   Alignment.ColumnLimit = 50;
14623   EXPECT_EQ("int x   = 0;\n"
14624             "int yy  = 1; /// specificlennospace\n"
14625             "int zzz = 2;\n",
14626             format("int x   = 0;\n"
14627                    "int yy  = 1; ///specificlennospace\n"
14628                    "int zzz = 2;\n",
14629                    Alignment));
14630 }
14631 
14632 TEST_F(FormatTest, AlignConsecutiveAssignments) {
14633   FormatStyle Alignment = getLLVMStyle();
14634   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14635   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
14636   verifyFormat("int a = 5;\n"
14637                "int oneTwoThree = 123;",
14638                Alignment);
14639   verifyFormat("int a = 5;\n"
14640                "int oneTwoThree = 123;",
14641                Alignment);
14642 
14643   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14644   verifyFormat("int a           = 5;\n"
14645                "int oneTwoThree = 123;",
14646                Alignment);
14647   verifyFormat("int a           = method();\n"
14648                "int oneTwoThree = 133;",
14649                Alignment);
14650   verifyFormat("a &= 5;\n"
14651                "bcd *= 5;\n"
14652                "ghtyf += 5;\n"
14653                "dvfvdb -= 5;\n"
14654                "a /= 5;\n"
14655                "vdsvsv %= 5;\n"
14656                "sfdbddfbdfbb ^= 5;\n"
14657                "dvsdsv |= 5;\n"
14658                "int dsvvdvsdvvv = 123;",
14659                Alignment);
14660   verifyFormat("int i = 1, j = 10;\n"
14661                "something = 2000;",
14662                Alignment);
14663   verifyFormat("something = 2000;\n"
14664                "int i = 1, j = 10;\n",
14665                Alignment);
14666   verifyFormat("something = 2000;\n"
14667                "another   = 911;\n"
14668                "int i = 1, j = 10;\n"
14669                "oneMore = 1;\n"
14670                "i       = 2;",
14671                Alignment);
14672   verifyFormat("int a   = 5;\n"
14673                "int one = 1;\n"
14674                "method();\n"
14675                "int oneTwoThree = 123;\n"
14676                "int oneTwo      = 12;",
14677                Alignment);
14678   verifyFormat("int oneTwoThree = 123;\n"
14679                "int oneTwo      = 12;\n"
14680                "method();\n",
14681                Alignment);
14682   verifyFormat("int oneTwoThree = 123; // comment\n"
14683                "int oneTwo      = 12;  // comment",
14684                Alignment);
14685 
14686   // Bug 25167
14687   /* Uncomment when fixed
14688     verifyFormat("#if A\n"
14689                  "#else\n"
14690                  "int aaaaaaaa = 12;\n"
14691                  "#endif\n"
14692                  "#if B\n"
14693                  "#else\n"
14694                  "int a = 12;\n"
14695                  "#endif\n",
14696                  Alignment);
14697     verifyFormat("enum foo {\n"
14698                  "#if A\n"
14699                  "#else\n"
14700                  "  aaaaaaaa = 12;\n"
14701                  "#endif\n"
14702                  "#if B\n"
14703                  "#else\n"
14704                  "  a = 12;\n"
14705                  "#endif\n"
14706                  "};\n",
14707                  Alignment);
14708   */
14709 
14710   EXPECT_EQ("int a = 5;\n"
14711             "\n"
14712             "int oneTwoThree = 123;",
14713             format("int a       = 5;\n"
14714                    "\n"
14715                    "int oneTwoThree= 123;",
14716                    Alignment));
14717   EXPECT_EQ("int a   = 5;\n"
14718             "int one = 1;\n"
14719             "\n"
14720             "int oneTwoThree = 123;",
14721             format("int a = 5;\n"
14722                    "int one = 1;\n"
14723                    "\n"
14724                    "int oneTwoThree = 123;",
14725                    Alignment));
14726   EXPECT_EQ("int a   = 5;\n"
14727             "int one = 1;\n"
14728             "\n"
14729             "int oneTwoThree = 123;\n"
14730             "int oneTwo      = 12;",
14731             format("int a = 5;\n"
14732                    "int one = 1;\n"
14733                    "\n"
14734                    "int oneTwoThree = 123;\n"
14735                    "int oneTwo = 12;",
14736                    Alignment));
14737   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
14738   verifyFormat("#define A \\\n"
14739                "  int aaaa       = 12; \\\n"
14740                "  int b          = 23; \\\n"
14741                "  int ccc        = 234; \\\n"
14742                "  int dddddddddd = 2345;",
14743                Alignment);
14744   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14745   verifyFormat("#define A               \\\n"
14746                "  int aaaa       = 12;  \\\n"
14747                "  int b          = 23;  \\\n"
14748                "  int ccc        = 234; \\\n"
14749                "  int dddddddddd = 2345;",
14750                Alignment);
14751   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
14752   verifyFormat("#define A                                                      "
14753                "                \\\n"
14754                "  int aaaa       = 12;                                         "
14755                "                \\\n"
14756                "  int b          = 23;                                         "
14757                "                \\\n"
14758                "  int ccc        = 234;                                        "
14759                "                \\\n"
14760                "  int dddddddddd = 2345;",
14761                Alignment);
14762   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
14763                "k = 4, int l = 5,\n"
14764                "                  int m = 6) {\n"
14765                "  int j      = 10;\n"
14766                "  otherThing = 1;\n"
14767                "}",
14768                Alignment);
14769   verifyFormat("void SomeFunction(int parameter = 0) {\n"
14770                "  int i   = 1;\n"
14771                "  int j   = 2;\n"
14772                "  int big = 10000;\n"
14773                "}",
14774                Alignment);
14775   verifyFormat("class C {\n"
14776                "public:\n"
14777                "  int i            = 1;\n"
14778                "  virtual void f() = 0;\n"
14779                "};",
14780                Alignment);
14781   verifyFormat("int i = 1;\n"
14782                "if (SomeType t = getSomething()) {\n"
14783                "}\n"
14784                "int j   = 2;\n"
14785                "int big = 10000;",
14786                Alignment);
14787   verifyFormat("int j = 7;\n"
14788                "for (int k = 0; k < N; ++k) {\n"
14789                "}\n"
14790                "int j   = 2;\n"
14791                "int big = 10000;\n"
14792                "}",
14793                Alignment);
14794   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
14795   verifyFormat("int i = 1;\n"
14796                "LooooooooooongType loooooooooooooooooooooongVariable\n"
14797                "    = someLooooooooooooooooongFunction();\n"
14798                "int j = 2;",
14799                Alignment);
14800   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
14801   verifyFormat("int i = 1;\n"
14802                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
14803                "    someLooooooooooooooooongFunction();\n"
14804                "int j = 2;",
14805                Alignment);
14806 
14807   verifyFormat("auto lambda = []() {\n"
14808                "  auto i = 0;\n"
14809                "  return 0;\n"
14810                "};\n"
14811                "int i  = 0;\n"
14812                "auto v = type{\n"
14813                "    i = 1,   //\n"
14814                "    (i = 2), //\n"
14815                "    i = 3    //\n"
14816                "};",
14817                Alignment);
14818 
14819   verifyFormat(
14820       "int i      = 1;\n"
14821       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
14822       "                          loooooooooooooooooooooongParameterB);\n"
14823       "int j      = 2;",
14824       Alignment);
14825 
14826   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
14827                "          typename B   = very_long_type_name_1,\n"
14828                "          typename T_2 = very_long_type_name_2>\n"
14829                "auto foo() {}\n",
14830                Alignment);
14831   verifyFormat("int a, b = 1;\n"
14832                "int c  = 2;\n"
14833                "int dd = 3;\n",
14834                Alignment);
14835   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
14836                "float b[1][] = {{3.f}};\n",
14837                Alignment);
14838   verifyFormat("for (int i = 0; i < 1; i++)\n"
14839                "  int x = 1;\n",
14840                Alignment);
14841   verifyFormat("for (i = 0; i < 1; i++)\n"
14842                "  x = 1;\n"
14843                "y = 1;\n",
14844                Alignment);
14845 
14846   Alignment.ReflowComments = true;
14847   Alignment.ColumnLimit = 50;
14848   EXPECT_EQ("int x   = 0;\n"
14849             "int yy  = 1; /// specificlennospace\n"
14850             "int zzz = 2;\n",
14851             format("int x   = 0;\n"
14852                    "int yy  = 1; ///specificlennospace\n"
14853                    "int zzz = 2;\n",
14854                    Alignment));
14855 }
14856 
14857 TEST_F(FormatTest, AlignConsecutiveBitFields) {
14858   FormatStyle Alignment = getLLVMStyle();
14859   Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
14860   verifyFormat("int const a     : 5;\n"
14861                "int oneTwoThree : 23;",
14862                Alignment);
14863 
14864   // Initializers are allowed starting with c++2a
14865   verifyFormat("int const a     : 5 = 1;\n"
14866                "int oneTwoThree : 23 = 0;",
14867                Alignment);
14868 
14869   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14870   verifyFormat("int const a           : 5;\n"
14871                "int       oneTwoThree : 23;",
14872                Alignment);
14873 
14874   verifyFormat("int const a           : 5;  // comment\n"
14875                "int       oneTwoThree : 23; // comment",
14876                Alignment);
14877 
14878   verifyFormat("int const a           : 5 = 1;\n"
14879                "int       oneTwoThree : 23 = 0;",
14880                Alignment);
14881 
14882   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
14883   verifyFormat("int const a           : 5  = 1;\n"
14884                "int       oneTwoThree : 23 = 0;",
14885                Alignment);
14886   verifyFormat("int const a           : 5  = {1};\n"
14887                "int       oneTwoThree : 23 = 0;",
14888                Alignment);
14889 
14890   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
14891   verifyFormat("int const a          :5;\n"
14892                "int       oneTwoThree:23;",
14893                Alignment);
14894 
14895   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
14896   verifyFormat("int const a           :5;\n"
14897                "int       oneTwoThree :23;",
14898                Alignment);
14899 
14900   Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
14901   verifyFormat("int const a          : 5;\n"
14902                "int       oneTwoThree: 23;",
14903                Alignment);
14904 
14905   // Known limitations: ':' is only recognized as a bitfield colon when
14906   // followed by a number.
14907   /*
14908   verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
14909                "int a           : 5;",
14910                Alignment);
14911   */
14912 }
14913 
14914 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
14915   FormatStyle Alignment = getLLVMStyle();
14916   Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
14917   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None;
14918   verifyFormat("float const a = 5;\n"
14919                "int oneTwoThree = 123;",
14920                Alignment);
14921   verifyFormat("int a = 5;\n"
14922                "float const oneTwoThree = 123;",
14923                Alignment);
14924 
14925   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
14926   verifyFormat("float const a = 5;\n"
14927                "int         oneTwoThree = 123;",
14928                Alignment);
14929   verifyFormat("int         a = method();\n"
14930                "float const oneTwoThree = 133;",
14931                Alignment);
14932   verifyFormat("int i = 1, j = 10;\n"
14933                "something = 2000;",
14934                Alignment);
14935   verifyFormat("something = 2000;\n"
14936                "int i = 1, j = 10;\n",
14937                Alignment);
14938   verifyFormat("float      something = 2000;\n"
14939                "double     another = 911;\n"
14940                "int        i = 1, j = 10;\n"
14941                "const int *oneMore = 1;\n"
14942                "unsigned   i = 2;",
14943                Alignment);
14944   verifyFormat("float a = 5;\n"
14945                "int   one = 1;\n"
14946                "method();\n"
14947                "const double       oneTwoThree = 123;\n"
14948                "const unsigned int oneTwo = 12;",
14949                Alignment);
14950   verifyFormat("int      oneTwoThree{0}; // comment\n"
14951                "unsigned oneTwo;         // comment",
14952                Alignment);
14953   verifyFormat("unsigned int *      a;\n"
14954                "int *               b;\n"
14955                "unsigned int Const *c;\n"
14956                "unsigned int const *d;\n"
14957                "unsigned int Const &e;\n"
14958                "unsigned int const &f;",
14959                Alignment);
14960   verifyFormat("Const unsigned int *c;\n"
14961                "const unsigned int *d;\n"
14962                "Const unsigned int &e;\n"
14963                "const unsigned int &f;\n"
14964                "const unsigned      g;\n"
14965                "Const unsigned      h;",
14966                Alignment);
14967   EXPECT_EQ("float const a = 5;\n"
14968             "\n"
14969             "int oneTwoThree = 123;",
14970             format("float const   a = 5;\n"
14971                    "\n"
14972                    "int           oneTwoThree= 123;",
14973                    Alignment));
14974   EXPECT_EQ("float a = 5;\n"
14975             "int   one = 1;\n"
14976             "\n"
14977             "unsigned oneTwoThree = 123;",
14978             format("float    a = 5;\n"
14979                    "int      one = 1;\n"
14980                    "\n"
14981                    "unsigned oneTwoThree = 123;",
14982                    Alignment));
14983   EXPECT_EQ("float a = 5;\n"
14984             "int   one = 1;\n"
14985             "\n"
14986             "unsigned oneTwoThree = 123;\n"
14987             "int      oneTwo = 12;",
14988             format("float    a = 5;\n"
14989                    "int one = 1;\n"
14990                    "\n"
14991                    "unsigned oneTwoThree = 123;\n"
14992                    "int oneTwo = 12;",
14993                    Alignment));
14994   // Function prototype alignment
14995   verifyFormat("int    a();\n"
14996                "double b();",
14997                Alignment);
14998   verifyFormat("int    a(int x);\n"
14999                "double b();",
15000                Alignment);
15001   unsigned OldColumnLimit = Alignment.ColumnLimit;
15002   // We need to set ColumnLimit to zero, in order to stress nested alignments,
15003   // otherwise the function parameters will be re-flowed onto a single line.
15004   Alignment.ColumnLimit = 0;
15005   EXPECT_EQ("int    a(int   x,\n"
15006             "         float y);\n"
15007             "double b(int    x,\n"
15008             "         double y);",
15009             format("int a(int x,\n"
15010                    " float y);\n"
15011                    "double b(int x,\n"
15012                    " double y);",
15013                    Alignment));
15014   // This ensures that function parameters of function declarations are
15015   // correctly indented when their owning functions are indented.
15016   // The failure case here is for 'double y' to not be indented enough.
15017   EXPECT_EQ("double a(int x);\n"
15018             "int    b(int    y,\n"
15019             "         double z);",
15020             format("double a(int x);\n"
15021                    "int b(int y,\n"
15022                    " double z);",
15023                    Alignment));
15024   // Set ColumnLimit low so that we induce wrapping immediately after
15025   // the function name and opening paren.
15026   Alignment.ColumnLimit = 13;
15027   verifyFormat("int function(\n"
15028                "    int  x,\n"
15029                "    bool y);",
15030                Alignment);
15031   Alignment.ColumnLimit = OldColumnLimit;
15032   // Ensure function pointers don't screw up recursive alignment
15033   verifyFormat("int    a(int x, void (*fp)(int y));\n"
15034                "double b();",
15035                Alignment);
15036   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15037   // Ensure recursive alignment is broken by function braces, so that the
15038   // "a = 1" does not align with subsequent assignments inside the function
15039   // body.
15040   verifyFormat("int func(int a = 1) {\n"
15041                "  int b  = 2;\n"
15042                "  int cc = 3;\n"
15043                "}",
15044                Alignment);
15045   verifyFormat("float      something = 2000;\n"
15046                "double     another   = 911;\n"
15047                "int        i = 1, j = 10;\n"
15048                "const int *oneMore = 1;\n"
15049                "unsigned   i       = 2;",
15050                Alignment);
15051   verifyFormat("int      oneTwoThree = {0}; // comment\n"
15052                "unsigned oneTwo      = 0;   // comment",
15053                Alignment);
15054   // Make sure that scope is correctly tracked, in the absence of braces
15055   verifyFormat("for (int i = 0; i < n; i++)\n"
15056                "  j = i;\n"
15057                "double x = 1;\n",
15058                Alignment);
15059   verifyFormat("if (int i = 0)\n"
15060                "  j = i;\n"
15061                "double x = 1;\n",
15062                Alignment);
15063   // Ensure operator[] and operator() are comprehended
15064   verifyFormat("struct test {\n"
15065                "  long long int foo();\n"
15066                "  int           operator[](int a);\n"
15067                "  double        bar();\n"
15068                "};\n",
15069                Alignment);
15070   verifyFormat("struct test {\n"
15071                "  long long int foo();\n"
15072                "  int           operator()(int a);\n"
15073                "  double        bar();\n"
15074                "};\n",
15075                Alignment);
15076   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
15077             "  int const i   = 1;\n"
15078             "  int *     j   = 2;\n"
15079             "  int       big = 10000;\n"
15080             "\n"
15081             "  unsigned oneTwoThree = 123;\n"
15082             "  int      oneTwo      = 12;\n"
15083             "  method();\n"
15084             "  float k  = 2;\n"
15085             "  int   ll = 10000;\n"
15086             "}",
15087             format("void SomeFunction(int parameter= 0) {\n"
15088                    " int const  i= 1;\n"
15089                    "  int *j=2;\n"
15090                    " int big  =  10000;\n"
15091                    "\n"
15092                    "unsigned oneTwoThree  =123;\n"
15093                    "int oneTwo = 12;\n"
15094                    "  method();\n"
15095                    "float k= 2;\n"
15096                    "int ll=10000;\n"
15097                    "}",
15098                    Alignment));
15099   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15100   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
15101   verifyFormat("#define A \\\n"
15102                "  int       aaaa = 12; \\\n"
15103                "  float     b = 23; \\\n"
15104                "  const int ccc = 234; \\\n"
15105                "  unsigned  dddddddddd = 2345;",
15106                Alignment);
15107   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15108   verifyFormat("#define A              \\\n"
15109                "  int       aaaa = 12; \\\n"
15110                "  float     b = 23;    \\\n"
15111                "  const int ccc = 234; \\\n"
15112                "  unsigned  dddddddddd = 2345;",
15113                Alignment);
15114   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
15115   Alignment.ColumnLimit = 30;
15116   verifyFormat("#define A                    \\\n"
15117                "  int       aaaa = 12;       \\\n"
15118                "  float     b = 23;          \\\n"
15119                "  const int ccc = 234;       \\\n"
15120                "  int       dddddddddd = 2345;",
15121                Alignment);
15122   Alignment.ColumnLimit = 80;
15123   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
15124                "k = 4, int l = 5,\n"
15125                "                  int m = 6) {\n"
15126                "  const int j = 10;\n"
15127                "  otherThing = 1;\n"
15128                "}",
15129                Alignment);
15130   verifyFormat("void SomeFunction(int parameter = 0) {\n"
15131                "  int const i = 1;\n"
15132                "  int *     j = 2;\n"
15133                "  int       big = 10000;\n"
15134                "}",
15135                Alignment);
15136   verifyFormat("class C {\n"
15137                "public:\n"
15138                "  int          i = 1;\n"
15139                "  virtual void f() = 0;\n"
15140                "};",
15141                Alignment);
15142   verifyFormat("float i = 1;\n"
15143                "if (SomeType t = getSomething()) {\n"
15144                "}\n"
15145                "const unsigned j = 2;\n"
15146                "int            big = 10000;",
15147                Alignment);
15148   verifyFormat("float j = 7;\n"
15149                "for (int k = 0; k < N; ++k) {\n"
15150                "}\n"
15151                "unsigned j = 2;\n"
15152                "int      big = 10000;\n"
15153                "}",
15154                Alignment);
15155   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
15156   verifyFormat("float              i = 1;\n"
15157                "LooooooooooongType loooooooooooooooooooooongVariable\n"
15158                "    = someLooooooooooooooooongFunction();\n"
15159                "int j = 2;",
15160                Alignment);
15161   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
15162   verifyFormat("int                i = 1;\n"
15163                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
15164                "    someLooooooooooooooooongFunction();\n"
15165                "int j = 2;",
15166                Alignment);
15167 
15168   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15169   verifyFormat("auto lambda = []() {\n"
15170                "  auto  ii = 0;\n"
15171                "  float j  = 0;\n"
15172                "  return 0;\n"
15173                "};\n"
15174                "int   i  = 0;\n"
15175                "float i2 = 0;\n"
15176                "auto  v  = type{\n"
15177                "    i = 1,   //\n"
15178                "    (i = 2), //\n"
15179                "    i = 3    //\n"
15180                "};",
15181                Alignment);
15182   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15183 
15184   verifyFormat(
15185       "int      i = 1;\n"
15186       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
15187       "                          loooooooooooooooooooooongParameterB);\n"
15188       "int      j = 2;",
15189       Alignment);
15190 
15191   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
15192   // We expect declarations and assignments to align, as long as it doesn't
15193   // exceed the column limit, starting a new alignment sequence whenever it
15194   // happens.
15195   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15196   Alignment.ColumnLimit = 30;
15197   verifyFormat("float    ii              = 1;\n"
15198                "unsigned j               = 2;\n"
15199                "int someVerylongVariable = 1;\n"
15200                "AnotherLongType  ll = 123456;\n"
15201                "VeryVeryLongType k  = 2;\n"
15202                "int              myvar = 1;",
15203                Alignment);
15204   Alignment.ColumnLimit = 80;
15205   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15206 
15207   verifyFormat(
15208       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
15209       "          typename LongType, typename B>\n"
15210       "auto foo() {}\n",
15211       Alignment);
15212   verifyFormat("float a, b = 1;\n"
15213                "int   c = 2;\n"
15214                "int   dd = 3;\n",
15215                Alignment);
15216   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
15217                "float b[1][] = {{3.f}};\n",
15218                Alignment);
15219   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15220   verifyFormat("float a, b = 1;\n"
15221                "int   c  = 2;\n"
15222                "int   dd = 3;\n",
15223                Alignment);
15224   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
15225                "float b[1][] = {{3.f}};\n",
15226                Alignment);
15227   Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15228 
15229   Alignment.ColumnLimit = 30;
15230   Alignment.BinPackParameters = false;
15231   verifyFormat("void foo(float     a,\n"
15232                "         float     b,\n"
15233                "         int       c,\n"
15234                "         uint32_t *d) {\n"
15235                "  int *  e = 0;\n"
15236                "  float  f = 0;\n"
15237                "  double g = 0;\n"
15238                "}\n"
15239                "void bar(ino_t     a,\n"
15240                "         int       b,\n"
15241                "         uint32_t *c,\n"
15242                "         bool      d) {}\n",
15243                Alignment);
15244   Alignment.BinPackParameters = true;
15245   Alignment.ColumnLimit = 80;
15246 
15247   // Bug 33507
15248   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
15249   verifyFormat(
15250       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
15251       "  static const Version verVs2017;\n"
15252       "  return true;\n"
15253       "});\n",
15254       Alignment);
15255   Alignment.PointerAlignment = FormatStyle::PAS_Right;
15256 
15257   // See llvm.org/PR35641
15258   Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15259   verifyFormat("int func() { //\n"
15260                "  int      b;\n"
15261                "  unsigned c;\n"
15262                "}",
15263                Alignment);
15264 
15265   // See PR37175
15266   FormatStyle Style = getMozillaStyle();
15267   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15268   EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
15269             "foo(int a);",
15270             format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
15271 
15272   Alignment.PointerAlignment = FormatStyle::PAS_Left;
15273   verifyFormat("unsigned int*       a;\n"
15274                "int*                b;\n"
15275                "unsigned int Const* c;\n"
15276                "unsigned int const* d;\n"
15277                "unsigned int Const& e;\n"
15278                "unsigned int const& f;",
15279                Alignment);
15280   verifyFormat("Const unsigned int* c;\n"
15281                "const unsigned int* d;\n"
15282                "Const unsigned int& e;\n"
15283                "const unsigned int& f;\n"
15284                "const unsigned      g;\n"
15285                "Const unsigned      h;",
15286                Alignment);
15287 
15288   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
15289   verifyFormat("unsigned int *       a;\n"
15290                "int *                b;\n"
15291                "unsigned int Const * c;\n"
15292                "unsigned int const * d;\n"
15293                "unsigned int Const & e;\n"
15294                "unsigned int const & f;",
15295                Alignment);
15296   verifyFormat("Const unsigned int * c;\n"
15297                "const unsigned int * d;\n"
15298                "Const unsigned int & e;\n"
15299                "const unsigned int & f;\n"
15300                "const unsigned       g;\n"
15301                "Const unsigned       h;",
15302                Alignment);
15303 }
15304 
15305 TEST_F(FormatTest, AlignWithLineBreaks) {
15306   auto Style = getLLVMStyleWithColumns(120);
15307 
15308   EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None);
15309   EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None);
15310   verifyFormat("void foo() {\n"
15311                "  int myVar = 5;\n"
15312                "  double x = 3.14;\n"
15313                "  auto str = \"Hello \"\n"
15314                "             \"World\";\n"
15315                "  auto s = \"Hello \"\n"
15316                "           \"Again\";\n"
15317                "}",
15318                Style);
15319 
15320   // clang-format off
15321   verifyFormat("void foo() {\n"
15322                "  const int capacityBefore = Entries.capacity();\n"
15323                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15324                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15325                "  const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15326                "                                          std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15327                "}",
15328                Style);
15329   // clang-format on
15330 
15331   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15332   verifyFormat("void foo() {\n"
15333                "  int myVar = 5;\n"
15334                "  double x  = 3.14;\n"
15335                "  auto str  = \"Hello \"\n"
15336                "              \"World\";\n"
15337                "  auto s    = \"Hello \"\n"
15338                "              \"Again\";\n"
15339                "}",
15340                Style);
15341 
15342   // clang-format off
15343   verifyFormat("void foo() {\n"
15344                "  const int capacityBefore = Entries.capacity();\n"
15345                "  const auto newEntry      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15346                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15347                "  const X newEntry2        = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15348                "                                                 std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15349                "}",
15350                Style);
15351   // clang-format on
15352 
15353   Style.AlignConsecutiveAssignments = FormatStyle::ACS_None;
15354   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15355   verifyFormat("void foo() {\n"
15356                "  int    myVar = 5;\n"
15357                "  double x = 3.14;\n"
15358                "  auto   str = \"Hello \"\n"
15359                "               \"World\";\n"
15360                "  auto   s = \"Hello \"\n"
15361                "             \"Again\";\n"
15362                "}",
15363                Style);
15364 
15365   // clang-format off
15366   verifyFormat("void foo() {\n"
15367                "  const int  capacityBefore = Entries.capacity();\n"
15368                "  const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15369                "                                            std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15370                "  const X    newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15371                "                                             std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15372                "}",
15373                Style);
15374   // clang-format on
15375 
15376   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
15377   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
15378 
15379   verifyFormat("void foo() {\n"
15380                "  int    myVar = 5;\n"
15381                "  double x     = 3.14;\n"
15382                "  auto   str   = \"Hello \"\n"
15383                "                 \"World\";\n"
15384                "  auto   s     = \"Hello \"\n"
15385                "                 \"Again\";\n"
15386                "}",
15387                Style);
15388 
15389   // clang-format off
15390   verifyFormat("void foo() {\n"
15391                "  const int  capacityBefore = Entries.capacity();\n"
15392                "  const auto newEntry       = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15393                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15394                "  const X    newEntry2      = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
15395                "                                                  std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
15396                "}",
15397                Style);
15398   // clang-format on
15399 }
15400 
15401 TEST_F(FormatTest, LinuxBraceBreaking) {
15402   FormatStyle LinuxBraceStyle = getLLVMStyle();
15403   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
15404   verifyFormat("namespace a\n"
15405                "{\n"
15406                "class A\n"
15407                "{\n"
15408                "  void f()\n"
15409                "  {\n"
15410                "    if (true) {\n"
15411                "      a();\n"
15412                "      b();\n"
15413                "    } else {\n"
15414                "      a();\n"
15415                "    }\n"
15416                "  }\n"
15417                "  void g() { return; }\n"
15418                "};\n"
15419                "struct B {\n"
15420                "  int x;\n"
15421                "};\n"
15422                "} // namespace a\n",
15423                LinuxBraceStyle);
15424   verifyFormat("enum X {\n"
15425                "  Y = 0,\n"
15426                "}\n",
15427                LinuxBraceStyle);
15428   verifyFormat("struct S {\n"
15429                "  int Type;\n"
15430                "  union {\n"
15431                "    int x;\n"
15432                "    double y;\n"
15433                "  } Value;\n"
15434                "  class C\n"
15435                "  {\n"
15436                "    MyFavoriteType Value;\n"
15437                "  } Class;\n"
15438                "}\n",
15439                LinuxBraceStyle);
15440 }
15441 
15442 TEST_F(FormatTest, MozillaBraceBreaking) {
15443   FormatStyle MozillaBraceStyle = getLLVMStyle();
15444   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
15445   MozillaBraceStyle.FixNamespaceComments = false;
15446   verifyFormat("namespace a {\n"
15447                "class A\n"
15448                "{\n"
15449                "  void f()\n"
15450                "  {\n"
15451                "    if (true) {\n"
15452                "      a();\n"
15453                "      b();\n"
15454                "    }\n"
15455                "  }\n"
15456                "  void g() { return; }\n"
15457                "};\n"
15458                "enum E\n"
15459                "{\n"
15460                "  A,\n"
15461                "  // foo\n"
15462                "  B,\n"
15463                "  C\n"
15464                "};\n"
15465                "struct B\n"
15466                "{\n"
15467                "  int x;\n"
15468                "};\n"
15469                "}\n",
15470                MozillaBraceStyle);
15471   verifyFormat("struct S\n"
15472                "{\n"
15473                "  int Type;\n"
15474                "  union\n"
15475                "  {\n"
15476                "    int x;\n"
15477                "    double y;\n"
15478                "  } Value;\n"
15479                "  class C\n"
15480                "  {\n"
15481                "    MyFavoriteType Value;\n"
15482                "  } Class;\n"
15483                "}\n",
15484                MozillaBraceStyle);
15485 }
15486 
15487 TEST_F(FormatTest, StroustrupBraceBreaking) {
15488   FormatStyle StroustrupBraceStyle = getLLVMStyle();
15489   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
15490   verifyFormat("namespace a {\n"
15491                "class A {\n"
15492                "  void f()\n"
15493                "  {\n"
15494                "    if (true) {\n"
15495                "      a();\n"
15496                "      b();\n"
15497                "    }\n"
15498                "  }\n"
15499                "  void g() { return; }\n"
15500                "};\n"
15501                "struct B {\n"
15502                "  int x;\n"
15503                "};\n"
15504                "} // namespace a\n",
15505                StroustrupBraceStyle);
15506 
15507   verifyFormat("void foo()\n"
15508                "{\n"
15509                "  if (a) {\n"
15510                "    a();\n"
15511                "  }\n"
15512                "  else {\n"
15513                "    b();\n"
15514                "  }\n"
15515                "}\n",
15516                StroustrupBraceStyle);
15517 
15518   verifyFormat("#ifdef _DEBUG\n"
15519                "int foo(int i = 0)\n"
15520                "#else\n"
15521                "int foo(int i = 5)\n"
15522                "#endif\n"
15523                "{\n"
15524                "  return i;\n"
15525                "}",
15526                StroustrupBraceStyle);
15527 
15528   verifyFormat("void foo() {}\n"
15529                "void bar()\n"
15530                "#ifdef _DEBUG\n"
15531                "{\n"
15532                "  foo();\n"
15533                "}\n"
15534                "#else\n"
15535                "{\n"
15536                "}\n"
15537                "#endif",
15538                StroustrupBraceStyle);
15539 
15540   verifyFormat("void foobar() { int i = 5; }\n"
15541                "#ifdef _DEBUG\n"
15542                "void bar() {}\n"
15543                "#else\n"
15544                "void bar() { foobar(); }\n"
15545                "#endif",
15546                StroustrupBraceStyle);
15547 }
15548 
15549 TEST_F(FormatTest, AllmanBraceBreaking) {
15550   FormatStyle AllmanBraceStyle = getLLVMStyle();
15551   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
15552 
15553   EXPECT_EQ("namespace a\n"
15554             "{\n"
15555             "void f();\n"
15556             "void g();\n"
15557             "} // namespace a\n",
15558             format("namespace a\n"
15559                    "{\n"
15560                    "void f();\n"
15561                    "void g();\n"
15562                    "}\n",
15563                    AllmanBraceStyle));
15564 
15565   verifyFormat("namespace a\n"
15566                "{\n"
15567                "class A\n"
15568                "{\n"
15569                "  void f()\n"
15570                "  {\n"
15571                "    if (true)\n"
15572                "    {\n"
15573                "      a();\n"
15574                "      b();\n"
15575                "    }\n"
15576                "  }\n"
15577                "  void g() { return; }\n"
15578                "};\n"
15579                "struct B\n"
15580                "{\n"
15581                "  int x;\n"
15582                "};\n"
15583                "union C\n"
15584                "{\n"
15585                "};\n"
15586                "} // namespace a",
15587                AllmanBraceStyle);
15588 
15589   verifyFormat("void f()\n"
15590                "{\n"
15591                "  if (true)\n"
15592                "  {\n"
15593                "    a();\n"
15594                "  }\n"
15595                "  else if (false)\n"
15596                "  {\n"
15597                "    b();\n"
15598                "  }\n"
15599                "  else\n"
15600                "  {\n"
15601                "    c();\n"
15602                "  }\n"
15603                "}\n",
15604                AllmanBraceStyle);
15605 
15606   verifyFormat("void f()\n"
15607                "{\n"
15608                "  for (int i = 0; i < 10; ++i)\n"
15609                "  {\n"
15610                "    a();\n"
15611                "  }\n"
15612                "  while (false)\n"
15613                "  {\n"
15614                "    b();\n"
15615                "  }\n"
15616                "  do\n"
15617                "  {\n"
15618                "    c();\n"
15619                "  } while (false)\n"
15620                "}\n",
15621                AllmanBraceStyle);
15622 
15623   verifyFormat("void f(int a)\n"
15624                "{\n"
15625                "  switch (a)\n"
15626                "  {\n"
15627                "  case 0:\n"
15628                "    break;\n"
15629                "  case 1:\n"
15630                "  {\n"
15631                "    break;\n"
15632                "  }\n"
15633                "  case 2:\n"
15634                "  {\n"
15635                "  }\n"
15636                "  break;\n"
15637                "  default:\n"
15638                "    break;\n"
15639                "  }\n"
15640                "}\n",
15641                AllmanBraceStyle);
15642 
15643   verifyFormat("enum X\n"
15644                "{\n"
15645                "  Y = 0,\n"
15646                "}\n",
15647                AllmanBraceStyle);
15648   verifyFormat("enum X\n"
15649                "{\n"
15650                "  Y = 0\n"
15651                "}\n",
15652                AllmanBraceStyle);
15653 
15654   verifyFormat("@interface BSApplicationController ()\n"
15655                "{\n"
15656                "@private\n"
15657                "  id _extraIvar;\n"
15658                "}\n"
15659                "@end\n",
15660                AllmanBraceStyle);
15661 
15662   verifyFormat("#ifdef _DEBUG\n"
15663                "int foo(int i = 0)\n"
15664                "#else\n"
15665                "int foo(int i = 5)\n"
15666                "#endif\n"
15667                "{\n"
15668                "  return i;\n"
15669                "}",
15670                AllmanBraceStyle);
15671 
15672   verifyFormat("void foo() {}\n"
15673                "void bar()\n"
15674                "#ifdef _DEBUG\n"
15675                "{\n"
15676                "  foo();\n"
15677                "}\n"
15678                "#else\n"
15679                "{\n"
15680                "}\n"
15681                "#endif",
15682                AllmanBraceStyle);
15683 
15684   verifyFormat("void foobar() { int i = 5; }\n"
15685                "#ifdef _DEBUG\n"
15686                "void bar() {}\n"
15687                "#else\n"
15688                "void bar() { foobar(); }\n"
15689                "#endif",
15690                AllmanBraceStyle);
15691 
15692   EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
15693             FormatStyle::SLS_All);
15694 
15695   verifyFormat("[](int i) { return i + 2; };\n"
15696                "[](int i, int j)\n"
15697                "{\n"
15698                "  auto x = i + j;\n"
15699                "  auto y = i * j;\n"
15700                "  return x ^ y;\n"
15701                "};\n"
15702                "void foo()\n"
15703                "{\n"
15704                "  auto shortLambda = [](int i) { return i + 2; };\n"
15705                "  auto longLambda = [](int i, int j)\n"
15706                "  {\n"
15707                "    auto x = i + j;\n"
15708                "    auto y = i * j;\n"
15709                "    return x ^ y;\n"
15710                "  };\n"
15711                "}",
15712                AllmanBraceStyle);
15713 
15714   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
15715 
15716   verifyFormat("[](int i)\n"
15717                "{\n"
15718                "  return i + 2;\n"
15719                "};\n"
15720                "[](int i, int j)\n"
15721                "{\n"
15722                "  auto x = i + j;\n"
15723                "  auto y = i * j;\n"
15724                "  return x ^ y;\n"
15725                "};\n"
15726                "void foo()\n"
15727                "{\n"
15728                "  auto shortLambda = [](int i)\n"
15729                "  {\n"
15730                "    return i + 2;\n"
15731                "  };\n"
15732                "  auto longLambda = [](int i, int j)\n"
15733                "  {\n"
15734                "    auto x = i + j;\n"
15735                "    auto y = i * j;\n"
15736                "    return x ^ y;\n"
15737                "  };\n"
15738                "}",
15739                AllmanBraceStyle);
15740 
15741   // Reset
15742   AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
15743 
15744   // This shouldn't affect ObjC blocks..
15745   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
15746                "  // ...\n"
15747                "  int i;\n"
15748                "}];",
15749                AllmanBraceStyle);
15750   verifyFormat("void (^block)(void) = ^{\n"
15751                "  // ...\n"
15752                "  int i;\n"
15753                "};",
15754                AllmanBraceStyle);
15755   // .. or dict literals.
15756   verifyFormat("void f()\n"
15757                "{\n"
15758                "  // ...\n"
15759                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
15760                "}",
15761                AllmanBraceStyle);
15762   verifyFormat("void f()\n"
15763                "{\n"
15764                "  // ...\n"
15765                "  [object someMethod:@{a : @\"b\"}];\n"
15766                "}",
15767                AllmanBraceStyle);
15768   verifyFormat("int f()\n"
15769                "{ // comment\n"
15770                "  return 42;\n"
15771                "}",
15772                AllmanBraceStyle);
15773 
15774   AllmanBraceStyle.ColumnLimit = 19;
15775   verifyFormat("void f() { int i; }", AllmanBraceStyle);
15776   AllmanBraceStyle.ColumnLimit = 18;
15777   verifyFormat("void f()\n"
15778                "{\n"
15779                "  int i;\n"
15780                "}",
15781                AllmanBraceStyle);
15782   AllmanBraceStyle.ColumnLimit = 80;
15783 
15784   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
15785   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
15786       FormatStyle::SIS_WithoutElse;
15787   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
15788   verifyFormat("void f(bool b)\n"
15789                "{\n"
15790                "  if (b)\n"
15791                "  {\n"
15792                "    return;\n"
15793                "  }\n"
15794                "}\n",
15795                BreakBeforeBraceShortIfs);
15796   verifyFormat("void f(bool b)\n"
15797                "{\n"
15798                "  if constexpr (b)\n"
15799                "  {\n"
15800                "    return;\n"
15801                "  }\n"
15802                "}\n",
15803                BreakBeforeBraceShortIfs);
15804   verifyFormat("void f(bool b)\n"
15805                "{\n"
15806                "  if CONSTEXPR (b)\n"
15807                "  {\n"
15808                "    return;\n"
15809                "  }\n"
15810                "}\n",
15811                BreakBeforeBraceShortIfs);
15812   verifyFormat("void f(bool b)\n"
15813                "{\n"
15814                "  if (b) return;\n"
15815                "}\n",
15816                BreakBeforeBraceShortIfs);
15817   verifyFormat("void f(bool b)\n"
15818                "{\n"
15819                "  if constexpr (b) return;\n"
15820                "}\n",
15821                BreakBeforeBraceShortIfs);
15822   verifyFormat("void f(bool b)\n"
15823                "{\n"
15824                "  if CONSTEXPR (b) return;\n"
15825                "}\n",
15826                BreakBeforeBraceShortIfs);
15827   verifyFormat("void f(bool b)\n"
15828                "{\n"
15829                "  while (b)\n"
15830                "  {\n"
15831                "    return;\n"
15832                "  }\n"
15833                "}\n",
15834                BreakBeforeBraceShortIfs);
15835 }
15836 
15837 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
15838   FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
15839   WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
15840 
15841   // Make a few changes to the style for testing purposes
15842   WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
15843       FormatStyle::SFS_Empty;
15844   WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
15845   WhitesmithsBraceStyle.ColumnLimit = 0;
15846 
15847   // FIXME: this test case can't decide whether there should be a blank line
15848   // after the ~D() line or not. It adds one if one doesn't exist in the test
15849   // and it removes the line if one exists.
15850   /*
15851   verifyFormat("class A;\n"
15852                "namespace B\n"
15853                "  {\n"
15854                "class C;\n"
15855                "// Comment\n"
15856                "class D\n"
15857                "  {\n"
15858                "public:\n"
15859                "  D();\n"
15860                "  ~D() {}\n"
15861                "private:\n"
15862                "  enum E\n"
15863                "    {\n"
15864                "    F\n"
15865                "    }\n"
15866                "  };\n"
15867                "  } // namespace B\n",
15868                WhitesmithsBraceStyle);
15869   */
15870 
15871   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
15872   verifyFormat("namespace a\n"
15873                "  {\n"
15874                "class A\n"
15875                "  {\n"
15876                "  void f()\n"
15877                "    {\n"
15878                "    if (true)\n"
15879                "      {\n"
15880                "      a();\n"
15881                "      b();\n"
15882                "      }\n"
15883                "    }\n"
15884                "  void g()\n"
15885                "    {\n"
15886                "    return;\n"
15887                "    }\n"
15888                "  };\n"
15889                "struct B\n"
15890                "  {\n"
15891                "  int x;\n"
15892                "  };\n"
15893                "  } // namespace a",
15894                WhitesmithsBraceStyle);
15895 
15896   verifyFormat("namespace a\n"
15897                "  {\n"
15898                "namespace b\n"
15899                "  {\n"
15900                "class A\n"
15901                "  {\n"
15902                "  void f()\n"
15903                "    {\n"
15904                "    if (true)\n"
15905                "      {\n"
15906                "      a();\n"
15907                "      b();\n"
15908                "      }\n"
15909                "    }\n"
15910                "  void g()\n"
15911                "    {\n"
15912                "    return;\n"
15913                "    }\n"
15914                "  };\n"
15915                "struct B\n"
15916                "  {\n"
15917                "  int x;\n"
15918                "  };\n"
15919                "  } // namespace b\n"
15920                "  } // namespace a",
15921                WhitesmithsBraceStyle);
15922 
15923   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
15924   verifyFormat("namespace a\n"
15925                "  {\n"
15926                "namespace b\n"
15927                "  {\n"
15928                "  class A\n"
15929                "    {\n"
15930                "    void f()\n"
15931                "      {\n"
15932                "      if (true)\n"
15933                "        {\n"
15934                "        a();\n"
15935                "        b();\n"
15936                "        }\n"
15937                "      }\n"
15938                "    void g()\n"
15939                "      {\n"
15940                "      return;\n"
15941                "      }\n"
15942                "    };\n"
15943                "  struct B\n"
15944                "    {\n"
15945                "    int x;\n"
15946                "    };\n"
15947                "  } // namespace b\n"
15948                "  } // namespace a",
15949                WhitesmithsBraceStyle);
15950 
15951   WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
15952   verifyFormat("namespace a\n"
15953                "  {\n"
15954                "  namespace b\n"
15955                "    {\n"
15956                "    class A\n"
15957                "      {\n"
15958                "      void f()\n"
15959                "        {\n"
15960                "        if (true)\n"
15961                "          {\n"
15962                "          a();\n"
15963                "          b();\n"
15964                "          }\n"
15965                "        }\n"
15966                "      void g()\n"
15967                "        {\n"
15968                "        return;\n"
15969                "        }\n"
15970                "      };\n"
15971                "    struct B\n"
15972                "      {\n"
15973                "      int x;\n"
15974                "      };\n"
15975                "    } // namespace b\n"
15976                "  }   // namespace a",
15977                WhitesmithsBraceStyle);
15978 
15979   verifyFormat("void f()\n"
15980                "  {\n"
15981                "  if (true)\n"
15982                "    {\n"
15983                "    a();\n"
15984                "    }\n"
15985                "  else if (false)\n"
15986                "    {\n"
15987                "    b();\n"
15988                "    }\n"
15989                "  else\n"
15990                "    {\n"
15991                "    c();\n"
15992                "    }\n"
15993                "  }\n",
15994                WhitesmithsBraceStyle);
15995 
15996   verifyFormat("void f()\n"
15997                "  {\n"
15998                "  for (int i = 0; i < 10; ++i)\n"
15999                "    {\n"
16000                "    a();\n"
16001                "    }\n"
16002                "  while (false)\n"
16003                "    {\n"
16004                "    b();\n"
16005                "    }\n"
16006                "  do\n"
16007                "    {\n"
16008                "    c();\n"
16009                "    } while (false)\n"
16010                "  }\n",
16011                WhitesmithsBraceStyle);
16012 
16013   WhitesmithsBraceStyle.IndentCaseLabels = true;
16014   verifyFormat("void switchTest1(int a)\n"
16015                "  {\n"
16016                "  switch (a)\n"
16017                "    {\n"
16018                "    case 2:\n"
16019                "      {\n"
16020                "      }\n"
16021                "      break;\n"
16022                "    }\n"
16023                "  }\n",
16024                WhitesmithsBraceStyle);
16025 
16026   verifyFormat("void switchTest2(int a)\n"
16027                "  {\n"
16028                "  switch (a)\n"
16029                "    {\n"
16030                "    case 0:\n"
16031                "      break;\n"
16032                "    case 1:\n"
16033                "      {\n"
16034                "      break;\n"
16035                "      }\n"
16036                "    case 2:\n"
16037                "      {\n"
16038                "      }\n"
16039                "      break;\n"
16040                "    default:\n"
16041                "      break;\n"
16042                "    }\n"
16043                "  }\n",
16044                WhitesmithsBraceStyle);
16045 
16046   verifyFormat("void switchTest3(int a)\n"
16047                "  {\n"
16048                "  switch (a)\n"
16049                "    {\n"
16050                "    case 0:\n"
16051                "      {\n"
16052                "      foo(x);\n"
16053                "      }\n"
16054                "      break;\n"
16055                "    default:\n"
16056                "      {\n"
16057                "      foo(1);\n"
16058                "      }\n"
16059                "      break;\n"
16060                "    }\n"
16061                "  }\n",
16062                WhitesmithsBraceStyle);
16063 
16064   WhitesmithsBraceStyle.IndentCaseLabels = false;
16065 
16066   verifyFormat("void switchTest4(int a)\n"
16067                "  {\n"
16068                "  switch (a)\n"
16069                "    {\n"
16070                "  case 2:\n"
16071                "    {\n"
16072                "    }\n"
16073                "    break;\n"
16074                "    }\n"
16075                "  }\n",
16076                WhitesmithsBraceStyle);
16077 
16078   verifyFormat("void switchTest5(int a)\n"
16079                "  {\n"
16080                "  switch (a)\n"
16081                "    {\n"
16082                "  case 0:\n"
16083                "    break;\n"
16084                "  case 1:\n"
16085                "    {\n"
16086                "    foo();\n"
16087                "    break;\n"
16088                "    }\n"
16089                "  case 2:\n"
16090                "    {\n"
16091                "    }\n"
16092                "    break;\n"
16093                "  default:\n"
16094                "    break;\n"
16095                "    }\n"
16096                "  }\n",
16097                WhitesmithsBraceStyle);
16098 
16099   verifyFormat("void switchTest6(int a)\n"
16100                "  {\n"
16101                "  switch (a)\n"
16102                "    {\n"
16103                "  case 0:\n"
16104                "    {\n"
16105                "    foo(x);\n"
16106                "    }\n"
16107                "    break;\n"
16108                "  default:\n"
16109                "    {\n"
16110                "    foo(1);\n"
16111                "    }\n"
16112                "    break;\n"
16113                "    }\n"
16114                "  }\n",
16115                WhitesmithsBraceStyle);
16116 
16117   verifyFormat("enum X\n"
16118                "  {\n"
16119                "  Y = 0, // testing\n"
16120                "  }\n",
16121                WhitesmithsBraceStyle);
16122 
16123   verifyFormat("enum X\n"
16124                "  {\n"
16125                "  Y = 0\n"
16126                "  }\n",
16127                WhitesmithsBraceStyle);
16128   verifyFormat("enum X\n"
16129                "  {\n"
16130                "  Y = 0,\n"
16131                "  Z = 1\n"
16132                "  };\n",
16133                WhitesmithsBraceStyle);
16134 
16135   verifyFormat("@interface BSApplicationController ()\n"
16136                "  {\n"
16137                "@private\n"
16138                "  id _extraIvar;\n"
16139                "  }\n"
16140                "@end\n",
16141                WhitesmithsBraceStyle);
16142 
16143   verifyFormat("#ifdef _DEBUG\n"
16144                "int foo(int i = 0)\n"
16145                "#else\n"
16146                "int foo(int i = 5)\n"
16147                "#endif\n"
16148                "  {\n"
16149                "  return i;\n"
16150                "  }",
16151                WhitesmithsBraceStyle);
16152 
16153   verifyFormat("void foo() {}\n"
16154                "void bar()\n"
16155                "#ifdef _DEBUG\n"
16156                "  {\n"
16157                "  foo();\n"
16158                "  }\n"
16159                "#else\n"
16160                "  {\n"
16161                "  }\n"
16162                "#endif",
16163                WhitesmithsBraceStyle);
16164 
16165   verifyFormat("void foobar()\n"
16166                "  {\n"
16167                "  int i = 5;\n"
16168                "  }\n"
16169                "#ifdef _DEBUG\n"
16170                "void bar()\n"
16171                "  {\n"
16172                "  }\n"
16173                "#else\n"
16174                "void bar()\n"
16175                "  {\n"
16176                "  foobar();\n"
16177                "  }\n"
16178                "#endif",
16179                WhitesmithsBraceStyle);
16180 
16181   // This shouldn't affect ObjC blocks..
16182   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
16183                "  // ...\n"
16184                "  int i;\n"
16185                "}];",
16186                WhitesmithsBraceStyle);
16187   verifyFormat("void (^block)(void) = ^{\n"
16188                "  // ...\n"
16189                "  int i;\n"
16190                "};",
16191                WhitesmithsBraceStyle);
16192   // .. or dict literals.
16193   verifyFormat("void f()\n"
16194                "  {\n"
16195                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
16196                "  }",
16197                WhitesmithsBraceStyle);
16198 
16199   verifyFormat("int f()\n"
16200                "  { // comment\n"
16201                "  return 42;\n"
16202                "  }",
16203                WhitesmithsBraceStyle);
16204 
16205   FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
16206   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
16207       FormatStyle::SIS_OnlyFirstIf;
16208   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
16209   verifyFormat("void f(bool b)\n"
16210                "  {\n"
16211                "  if (b)\n"
16212                "    {\n"
16213                "    return;\n"
16214                "    }\n"
16215                "  }\n",
16216                BreakBeforeBraceShortIfs);
16217   verifyFormat("void f(bool b)\n"
16218                "  {\n"
16219                "  if (b) return;\n"
16220                "  }\n",
16221                BreakBeforeBraceShortIfs);
16222   verifyFormat("void f(bool b)\n"
16223                "  {\n"
16224                "  while (b)\n"
16225                "    {\n"
16226                "    return;\n"
16227                "    }\n"
16228                "  }\n",
16229                BreakBeforeBraceShortIfs);
16230 }
16231 
16232 TEST_F(FormatTest, GNUBraceBreaking) {
16233   FormatStyle GNUBraceStyle = getLLVMStyle();
16234   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
16235   verifyFormat("namespace a\n"
16236                "{\n"
16237                "class A\n"
16238                "{\n"
16239                "  void f()\n"
16240                "  {\n"
16241                "    int a;\n"
16242                "    {\n"
16243                "      int b;\n"
16244                "    }\n"
16245                "    if (true)\n"
16246                "      {\n"
16247                "        a();\n"
16248                "        b();\n"
16249                "      }\n"
16250                "  }\n"
16251                "  void g() { return; }\n"
16252                "}\n"
16253                "} // namespace a",
16254                GNUBraceStyle);
16255 
16256   verifyFormat("void f()\n"
16257                "{\n"
16258                "  if (true)\n"
16259                "    {\n"
16260                "      a();\n"
16261                "    }\n"
16262                "  else if (false)\n"
16263                "    {\n"
16264                "      b();\n"
16265                "    }\n"
16266                "  else\n"
16267                "    {\n"
16268                "      c();\n"
16269                "    }\n"
16270                "}\n",
16271                GNUBraceStyle);
16272 
16273   verifyFormat("void f()\n"
16274                "{\n"
16275                "  for (int i = 0; i < 10; ++i)\n"
16276                "    {\n"
16277                "      a();\n"
16278                "    }\n"
16279                "  while (false)\n"
16280                "    {\n"
16281                "      b();\n"
16282                "    }\n"
16283                "  do\n"
16284                "    {\n"
16285                "      c();\n"
16286                "    }\n"
16287                "  while (false);\n"
16288                "}\n",
16289                GNUBraceStyle);
16290 
16291   verifyFormat("void f(int a)\n"
16292                "{\n"
16293                "  switch (a)\n"
16294                "    {\n"
16295                "    case 0:\n"
16296                "      break;\n"
16297                "    case 1:\n"
16298                "      {\n"
16299                "        break;\n"
16300                "      }\n"
16301                "    case 2:\n"
16302                "      {\n"
16303                "      }\n"
16304                "      break;\n"
16305                "    default:\n"
16306                "      break;\n"
16307                "    }\n"
16308                "}\n",
16309                GNUBraceStyle);
16310 
16311   verifyFormat("enum X\n"
16312                "{\n"
16313                "  Y = 0,\n"
16314                "}\n",
16315                GNUBraceStyle);
16316 
16317   verifyFormat("@interface BSApplicationController ()\n"
16318                "{\n"
16319                "@private\n"
16320                "  id _extraIvar;\n"
16321                "}\n"
16322                "@end\n",
16323                GNUBraceStyle);
16324 
16325   verifyFormat("#ifdef _DEBUG\n"
16326                "int foo(int i = 0)\n"
16327                "#else\n"
16328                "int foo(int i = 5)\n"
16329                "#endif\n"
16330                "{\n"
16331                "  return i;\n"
16332                "}",
16333                GNUBraceStyle);
16334 
16335   verifyFormat("void foo() {}\n"
16336                "void bar()\n"
16337                "#ifdef _DEBUG\n"
16338                "{\n"
16339                "  foo();\n"
16340                "}\n"
16341                "#else\n"
16342                "{\n"
16343                "}\n"
16344                "#endif",
16345                GNUBraceStyle);
16346 
16347   verifyFormat("void foobar() { int i = 5; }\n"
16348                "#ifdef _DEBUG\n"
16349                "void bar() {}\n"
16350                "#else\n"
16351                "void bar() { foobar(); }\n"
16352                "#endif",
16353                GNUBraceStyle);
16354 }
16355 
16356 TEST_F(FormatTest, WebKitBraceBreaking) {
16357   FormatStyle WebKitBraceStyle = getLLVMStyle();
16358   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
16359   WebKitBraceStyle.FixNamespaceComments = false;
16360   verifyFormat("namespace a {\n"
16361                "class A {\n"
16362                "  void f()\n"
16363                "  {\n"
16364                "    if (true) {\n"
16365                "      a();\n"
16366                "      b();\n"
16367                "    }\n"
16368                "  }\n"
16369                "  void g() { return; }\n"
16370                "};\n"
16371                "enum E {\n"
16372                "  A,\n"
16373                "  // foo\n"
16374                "  B,\n"
16375                "  C\n"
16376                "};\n"
16377                "struct B {\n"
16378                "  int x;\n"
16379                "};\n"
16380                "}\n",
16381                WebKitBraceStyle);
16382   verifyFormat("struct S {\n"
16383                "  int Type;\n"
16384                "  union {\n"
16385                "    int x;\n"
16386                "    double y;\n"
16387                "  } Value;\n"
16388                "  class C {\n"
16389                "    MyFavoriteType Value;\n"
16390                "  } Class;\n"
16391                "};\n",
16392                WebKitBraceStyle);
16393 }
16394 
16395 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
16396   verifyFormat("void f() {\n"
16397                "  try {\n"
16398                "  } catch (const Exception &e) {\n"
16399                "  }\n"
16400                "}\n",
16401                getLLVMStyle());
16402 }
16403 
16404 TEST_F(FormatTest, UnderstandsPragmas) {
16405   verifyFormat("#pragma omp reduction(| : var)");
16406   verifyFormat("#pragma omp reduction(+ : var)");
16407 
16408   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
16409             "(including parentheses).",
16410             format("#pragma    mark   Any non-hyphenated or hyphenated string "
16411                    "(including parentheses)."));
16412 }
16413 
16414 TEST_F(FormatTest, UnderstandPragmaOption) {
16415   verifyFormat("#pragma option -C -A");
16416 
16417   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
16418 }
16419 
16420 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
16421   FormatStyle Style = getLLVMStyle();
16422   Style.ColumnLimit = 20;
16423 
16424   // See PR41213
16425   EXPECT_EQ("/*\n"
16426             " *\t9012345\n"
16427             " * /8901\n"
16428             " */",
16429             format("/*\n"
16430                    " *\t9012345 /8901\n"
16431                    " */",
16432                    Style));
16433   EXPECT_EQ("/*\n"
16434             " *345678\n"
16435             " *\t/8901\n"
16436             " */",
16437             format("/*\n"
16438                    " *345678\t/8901\n"
16439                    " */",
16440                    Style));
16441 
16442   verifyFormat("int a; // the\n"
16443                "       // comment",
16444                Style);
16445   EXPECT_EQ("int a; /* first line\n"
16446             "        * second\n"
16447             "        * line third\n"
16448             "        * line\n"
16449             "        */",
16450             format("int a; /* first line\n"
16451                    "        * second\n"
16452                    "        * line third\n"
16453                    "        * line\n"
16454                    "        */",
16455                    Style));
16456   EXPECT_EQ("int a; // first line\n"
16457             "       // second\n"
16458             "       // line third\n"
16459             "       // line",
16460             format("int a; // first line\n"
16461                    "       // second line\n"
16462                    "       // third line",
16463                    Style));
16464 
16465   Style.PenaltyExcessCharacter = 90;
16466   verifyFormat("int a; // the comment", Style);
16467   EXPECT_EQ("int a; // the comment\n"
16468             "       // aaa",
16469             format("int a; // the comment aaa", Style));
16470   EXPECT_EQ("int a; /* first line\n"
16471             "        * second line\n"
16472             "        * third line\n"
16473             "        */",
16474             format("int a; /* first line\n"
16475                    "        * second line\n"
16476                    "        * third line\n"
16477                    "        */",
16478                    Style));
16479   EXPECT_EQ("int a; // first line\n"
16480             "       // second line\n"
16481             "       // third line",
16482             format("int a; // first line\n"
16483                    "       // second line\n"
16484                    "       // third line",
16485                    Style));
16486   // FIXME: Investigate why this is not getting the same layout as the test
16487   // above.
16488   EXPECT_EQ("int a; /* first line\n"
16489             "        * second line\n"
16490             "        * third line\n"
16491             "        */",
16492             format("int a; /* first line second line third line"
16493                    "\n*/",
16494                    Style));
16495 
16496   EXPECT_EQ("// foo bar baz bazfoo\n"
16497             "// foo bar foo bar\n",
16498             format("// foo bar baz bazfoo\n"
16499                    "// foo bar foo           bar\n",
16500                    Style));
16501   EXPECT_EQ("// foo bar baz bazfoo\n"
16502             "// foo bar foo bar\n",
16503             format("// foo bar baz      bazfoo\n"
16504                    "// foo            bar foo bar\n",
16505                    Style));
16506 
16507   // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
16508   // next one.
16509   EXPECT_EQ("// foo bar baz bazfoo\n"
16510             "// bar foo bar\n",
16511             format("// foo bar baz      bazfoo bar\n"
16512                    "// foo            bar\n",
16513                    Style));
16514 
16515   EXPECT_EQ("// foo bar baz bazfoo\n"
16516             "// foo bar baz bazfoo\n"
16517             "// bar foo bar\n",
16518             format("// foo bar baz      bazfoo\n"
16519                    "// foo bar baz      bazfoo bar\n"
16520                    "// foo bar\n",
16521                    Style));
16522 
16523   EXPECT_EQ("// foo bar baz bazfoo\n"
16524             "// foo bar baz bazfoo\n"
16525             "// bar foo bar\n",
16526             format("// foo bar baz      bazfoo\n"
16527                    "// foo bar baz      bazfoo bar\n"
16528                    "// foo           bar\n",
16529                    Style));
16530 
16531   // Make sure we do not keep protruding characters if strict mode reflow is
16532   // cheaper than keeping protruding characters.
16533   Style.ColumnLimit = 21;
16534   EXPECT_EQ(
16535       "// foo foo foo foo\n"
16536       "// foo foo foo foo\n"
16537       "// foo foo foo foo\n",
16538       format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
16539 
16540   EXPECT_EQ("int a = /* long block\n"
16541             "           comment */\n"
16542             "    42;",
16543             format("int a = /* long block comment */ 42;", Style));
16544 }
16545 
16546 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
16547   for (size_t i = 1; i < Styles.size(); ++i)                                   \
16548   EXPECT_EQ(Styles[0], Styles[i])                                              \
16549       << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
16550 
16551 TEST_F(FormatTest, GetsPredefinedStyleByName) {
16552   SmallVector<FormatStyle, 3> Styles;
16553   Styles.resize(3);
16554 
16555   Styles[0] = getLLVMStyle();
16556   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
16557   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
16558   EXPECT_ALL_STYLES_EQUAL(Styles);
16559 
16560   Styles[0] = getGoogleStyle();
16561   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
16562   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
16563   EXPECT_ALL_STYLES_EQUAL(Styles);
16564 
16565   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
16566   EXPECT_TRUE(
16567       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
16568   EXPECT_TRUE(
16569       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
16570   EXPECT_ALL_STYLES_EQUAL(Styles);
16571 
16572   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
16573   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
16574   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
16575   EXPECT_ALL_STYLES_EQUAL(Styles);
16576 
16577   Styles[0] = getMozillaStyle();
16578   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
16579   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
16580   EXPECT_ALL_STYLES_EQUAL(Styles);
16581 
16582   Styles[0] = getWebKitStyle();
16583   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
16584   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
16585   EXPECT_ALL_STYLES_EQUAL(Styles);
16586 
16587   Styles[0] = getGNUStyle();
16588   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
16589   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
16590   EXPECT_ALL_STYLES_EQUAL(Styles);
16591 
16592   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
16593 }
16594 
16595 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
16596   SmallVector<FormatStyle, 8> Styles;
16597   Styles.resize(2);
16598 
16599   Styles[0] = getGoogleStyle();
16600   Styles[1] = getLLVMStyle();
16601   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
16602   EXPECT_ALL_STYLES_EQUAL(Styles);
16603 
16604   Styles.resize(5);
16605   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
16606   Styles[1] = getLLVMStyle();
16607   Styles[1].Language = FormatStyle::LK_JavaScript;
16608   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
16609 
16610   Styles[2] = getLLVMStyle();
16611   Styles[2].Language = FormatStyle::LK_JavaScript;
16612   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
16613                                   "BasedOnStyle: Google",
16614                                   &Styles[2])
16615                    .value());
16616 
16617   Styles[3] = getLLVMStyle();
16618   Styles[3].Language = FormatStyle::LK_JavaScript;
16619   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
16620                                   "Language: JavaScript",
16621                                   &Styles[3])
16622                    .value());
16623 
16624   Styles[4] = getLLVMStyle();
16625   Styles[4].Language = FormatStyle::LK_JavaScript;
16626   EXPECT_EQ(0, parseConfiguration("---\n"
16627                                   "BasedOnStyle: LLVM\n"
16628                                   "IndentWidth: 123\n"
16629                                   "---\n"
16630                                   "BasedOnStyle: Google\n"
16631                                   "Language: JavaScript",
16632                                   &Styles[4])
16633                    .value());
16634   EXPECT_ALL_STYLES_EQUAL(Styles);
16635 }
16636 
16637 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
16638   Style.FIELD = false;                                                         \
16639   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
16640   EXPECT_TRUE(Style.FIELD);                                                    \
16641   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
16642   EXPECT_FALSE(Style.FIELD);
16643 
16644 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
16645 
16646 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
16647   Style.STRUCT.FIELD = false;                                                  \
16648   EXPECT_EQ(0,                                                                 \
16649             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
16650                 .value());                                                     \
16651   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
16652   EXPECT_EQ(0,                                                                 \
16653             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
16654                 .value());                                                     \
16655   EXPECT_FALSE(Style.STRUCT.FIELD);
16656 
16657 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
16658   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
16659 
16660 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
16661   EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!";          \
16662   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
16663   EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!"
16664 
16665 TEST_F(FormatTest, ParsesConfigurationBools) {
16666   FormatStyle Style = {};
16667   Style.Language = FormatStyle::LK_Cpp;
16668   CHECK_PARSE_BOOL(AlignTrailingComments);
16669   CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
16670   CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
16671   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
16672   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
16673   CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
16674   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
16675   CHECK_PARSE_BOOL(BinPackArguments);
16676   CHECK_PARSE_BOOL(BinPackParameters);
16677   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
16678   CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations);
16679   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
16680   CHECK_PARSE_BOOL(BreakStringLiterals);
16681   CHECK_PARSE_BOOL(CompactNamespaces);
16682   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
16683   CHECK_PARSE_BOOL(DeriveLineEnding);
16684   CHECK_PARSE_BOOL(DerivePointerAlignment);
16685   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
16686   CHECK_PARSE_BOOL(DisableFormat);
16687   CHECK_PARSE_BOOL(IndentAccessModifiers);
16688   CHECK_PARSE_BOOL(IndentCaseLabels);
16689   CHECK_PARSE_BOOL(IndentCaseBlocks);
16690   CHECK_PARSE_BOOL(IndentGotoLabels);
16691   CHECK_PARSE_BOOL(IndentRequires);
16692   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
16693   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
16694   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
16695   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
16696   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
16697   CHECK_PARSE_BOOL(ReflowComments);
16698   CHECK_PARSE_BOOL(SortUsingDeclarations);
16699   CHECK_PARSE_BOOL(SpacesInParentheses);
16700   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
16701   CHECK_PARSE_BOOL(SpacesInConditionalStatement);
16702   CHECK_PARSE_BOOL(SpaceInEmptyBlock);
16703   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
16704   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
16705   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
16706   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
16707   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
16708   CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
16709   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
16710   CHECK_PARSE_BOOL(SpaceBeforeCaseColon);
16711   CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
16712   CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
16713   CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
16714   CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
16715   CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
16716   CHECK_PARSE_BOOL(UseCRLF);
16717 
16718   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
16719   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
16720   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
16721   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
16722   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
16723   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
16724   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
16725   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
16726   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
16727   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
16728   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
16729   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
16730   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
16731   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
16732   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
16733   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
16734   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
16735 }
16736 
16737 #undef CHECK_PARSE_BOOL
16738 
16739 TEST_F(FormatTest, ParsesConfiguration) {
16740   FormatStyle Style = {};
16741   Style.Language = FormatStyle::LK_Cpp;
16742   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
16743   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
16744               ConstructorInitializerIndentWidth, 1234u);
16745   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
16746   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
16747   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
16748   CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
16749   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
16750               PenaltyBreakBeforeFirstCallParameter, 1234u);
16751   CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
16752               PenaltyBreakTemplateDeclaration, 1234u);
16753   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
16754   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
16755               PenaltyReturnTypeOnItsOwnLine, 1234u);
16756   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
16757               SpacesBeforeTrailingComments, 1234u);
16758   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
16759   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
16760   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
16761 
16762   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
16763   CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments,
16764               FormatStyle::ACS_None);
16765   CHECK_PARSE("AlignConsecutiveAssignments: Consecutive",
16766               AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive);
16767   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines",
16768               AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines);
16769   CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments",
16770               AlignConsecutiveAssignments,
16771               FormatStyle::ACS_AcrossEmptyLinesAndComments);
16772   // For backwards compability, false / true should still parse
16773   CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments,
16774               FormatStyle::ACS_None);
16775   CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments,
16776               FormatStyle::ACS_Consecutive);
16777 
16778   Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive;
16779   CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields,
16780               FormatStyle::ACS_None);
16781   CHECK_PARSE("AlignConsecutiveBitFields: Consecutive",
16782               AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive);
16783   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines",
16784               AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines);
16785   CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments",
16786               AlignConsecutiveBitFields,
16787               FormatStyle::ACS_AcrossEmptyLinesAndComments);
16788   // For backwards compability, false / true should still parse
16789   CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields,
16790               FormatStyle::ACS_None);
16791   CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields,
16792               FormatStyle::ACS_Consecutive);
16793 
16794   Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive;
16795   CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros,
16796               FormatStyle::ACS_None);
16797   CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros,
16798               FormatStyle::ACS_Consecutive);
16799   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines",
16800               AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines);
16801   CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments",
16802               AlignConsecutiveMacros,
16803               FormatStyle::ACS_AcrossEmptyLinesAndComments);
16804   // For backwards compability, false / true should still parse
16805   CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros,
16806               FormatStyle::ACS_None);
16807   CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros,
16808               FormatStyle::ACS_Consecutive);
16809 
16810   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
16811   CHECK_PARSE("AlignConsecutiveDeclarations: None",
16812               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16813   CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive",
16814               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
16815   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines",
16816               AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines);
16817   CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments",
16818               AlignConsecutiveDeclarations,
16819               FormatStyle::ACS_AcrossEmptyLinesAndComments);
16820   // For backwards compability, false / true should still parse
16821   CHECK_PARSE("AlignConsecutiveDeclarations: false",
16822               AlignConsecutiveDeclarations, FormatStyle::ACS_None);
16823   CHECK_PARSE("AlignConsecutiveDeclarations: true",
16824               AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive);
16825 
16826   Style.PointerAlignment = FormatStyle::PAS_Middle;
16827   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
16828               FormatStyle::PAS_Left);
16829   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
16830               FormatStyle::PAS_Right);
16831   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
16832               FormatStyle::PAS_Middle);
16833   // For backward compatibility:
16834   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
16835               FormatStyle::PAS_Left);
16836   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
16837               FormatStyle::PAS_Right);
16838   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
16839               FormatStyle::PAS_Middle);
16840 
16841   Style.Standard = FormatStyle::LS_Auto;
16842   CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
16843   CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
16844   CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
16845   CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
16846   CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
16847   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
16848   CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
16849   // Legacy aliases:
16850   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
16851   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
16852   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
16853   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
16854 
16855   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16856   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
16857               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
16858   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
16859               FormatStyle::BOS_None);
16860   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
16861               FormatStyle::BOS_All);
16862   // For backward compatibility:
16863   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
16864               FormatStyle::BOS_None);
16865   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
16866               FormatStyle::BOS_All);
16867 
16868   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
16869   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
16870               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
16871   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
16872               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
16873   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
16874               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
16875   // For backward compatibility:
16876   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
16877               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
16878 
16879   Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
16880   CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList,
16881               FormatStyle::BILS_AfterComma);
16882   CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
16883               FormatStyle::BILS_BeforeComma);
16884   CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
16885               FormatStyle::BILS_AfterColon);
16886   CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
16887               FormatStyle::BILS_BeforeColon);
16888   // For backward compatibility:
16889   CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
16890               FormatStyle::BILS_BeforeComma);
16891 
16892   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
16893   CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
16894               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);
16895   CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave",
16896               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave);
16897   CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock",
16898               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock);
16899   CHECK_PARSE("EmptyLineBeforeAccessModifier: Always",
16900               EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always);
16901 
16902   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
16903   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
16904               FormatStyle::BAS_Align);
16905   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
16906               FormatStyle::BAS_DontAlign);
16907   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
16908               FormatStyle::BAS_AlwaysBreak);
16909   // For backward compatibility:
16910   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
16911               FormatStyle::BAS_DontAlign);
16912   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
16913               FormatStyle::BAS_Align);
16914 
16915   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16916   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
16917               FormatStyle::ENAS_DontAlign);
16918   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
16919               FormatStyle::ENAS_Left);
16920   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
16921               FormatStyle::ENAS_Right);
16922   // For backward compatibility:
16923   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
16924               FormatStyle::ENAS_Left);
16925   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
16926               FormatStyle::ENAS_Right);
16927 
16928   Style.AlignOperands = FormatStyle::OAS_Align;
16929   CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
16930               FormatStyle::OAS_DontAlign);
16931   CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
16932   CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
16933               FormatStyle::OAS_AlignAfterOperator);
16934   // For backward compatibility:
16935   CHECK_PARSE("AlignOperands: false", AlignOperands,
16936               FormatStyle::OAS_DontAlign);
16937   CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
16938 
16939   Style.UseTab = FormatStyle::UT_ForIndentation;
16940   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
16941   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
16942   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
16943   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
16944               FormatStyle::UT_ForContinuationAndIndentation);
16945   CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
16946               FormatStyle::UT_AlignWithSpaces);
16947   // For backward compatibility:
16948   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
16949   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
16950 
16951   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
16952   CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
16953               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
16954   CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
16955               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
16956   CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
16957               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
16958   // For backward compatibility:
16959   CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
16960               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
16961   CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
16962               AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
16963 
16964   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
16965   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
16966               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
16967   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
16968               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
16969   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
16970               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
16971   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
16972               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
16973   // For backward compatibility:
16974   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
16975               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
16976   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
16977               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
16978 
16979   Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both;
16980   CHECK_PARSE("SpaceAroundPointerQualifiers: Default",
16981               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default);
16982   CHECK_PARSE("SpaceAroundPointerQualifiers: Before",
16983               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before);
16984   CHECK_PARSE("SpaceAroundPointerQualifiers: After",
16985               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After);
16986   CHECK_PARSE("SpaceAroundPointerQualifiers: Both",
16987               SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);
16988 
16989   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
16990   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
16991               FormatStyle::SBPO_Never);
16992   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
16993               FormatStyle::SBPO_Always);
16994   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
16995               FormatStyle::SBPO_ControlStatements);
16996   CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
16997               FormatStyle::SBPO_NonEmptyParentheses);
16998   // For backward compatibility:
16999   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
17000               FormatStyle::SBPO_Never);
17001   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
17002               FormatStyle::SBPO_ControlStatements);
17003 
17004   Style.ColumnLimit = 123;
17005   FormatStyle BaseStyle = getLLVMStyle();
17006   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
17007   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
17008 
17009   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
17010   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
17011               FormatStyle::BS_Attach);
17012   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
17013               FormatStyle::BS_Linux);
17014   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
17015               FormatStyle::BS_Mozilla);
17016   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
17017               FormatStyle::BS_Stroustrup);
17018   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
17019               FormatStyle::BS_Allman);
17020   CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
17021               FormatStyle::BS_Whitesmiths);
17022   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
17023   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
17024               FormatStyle::BS_WebKit);
17025   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
17026               FormatStyle::BS_Custom);
17027 
17028   Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
17029   CHECK_PARSE("BraceWrapping:\n"
17030               "  AfterControlStatement: MultiLine",
17031               BraceWrapping.AfterControlStatement,
17032               FormatStyle::BWACS_MultiLine);
17033   CHECK_PARSE("BraceWrapping:\n"
17034               "  AfterControlStatement: Always",
17035               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
17036   CHECK_PARSE("BraceWrapping:\n"
17037               "  AfterControlStatement: Never",
17038               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
17039   // For backward compatibility:
17040   CHECK_PARSE("BraceWrapping:\n"
17041               "  AfterControlStatement: true",
17042               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
17043   CHECK_PARSE("BraceWrapping:\n"
17044               "  AfterControlStatement: false",
17045               BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
17046 
17047   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
17048   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
17049               FormatStyle::RTBS_None);
17050   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
17051               FormatStyle::RTBS_All);
17052   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
17053               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
17054   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
17055               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
17056   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
17057               AlwaysBreakAfterReturnType,
17058               FormatStyle::RTBS_TopLevelDefinitions);
17059 
17060   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
17061   CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
17062               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
17063   CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
17064               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
17065   CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
17066               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
17067   CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
17068               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
17069   CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
17070               AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
17071 
17072   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
17073   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
17074               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
17075   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
17076               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
17077   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
17078               AlwaysBreakAfterDefinitionReturnType,
17079               FormatStyle::DRTBS_TopLevel);
17080 
17081   Style.NamespaceIndentation = FormatStyle::NI_All;
17082   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
17083               FormatStyle::NI_None);
17084   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
17085               FormatStyle::NI_Inner);
17086   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
17087               FormatStyle::NI_All);
17088 
17089   Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf;
17090   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
17091               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
17092   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
17093               AllowShortIfStatementsOnASingleLine,
17094               FormatStyle::SIS_WithoutElse);
17095   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf",
17096               AllowShortIfStatementsOnASingleLine,
17097               FormatStyle::SIS_OnlyFirstIf);
17098   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse",
17099               AllowShortIfStatementsOnASingleLine,
17100               FormatStyle::SIS_AllIfsAndElse);
17101   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
17102               AllowShortIfStatementsOnASingleLine,
17103               FormatStyle::SIS_OnlyFirstIf);
17104   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
17105               AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
17106   CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
17107               AllowShortIfStatementsOnASingleLine,
17108               FormatStyle::SIS_WithoutElse);
17109 
17110   Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
17111   CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
17112               FormatStyle::IEBS_AfterExternBlock);
17113   CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
17114               FormatStyle::IEBS_Indent);
17115   CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
17116               FormatStyle::IEBS_NoIndent);
17117   CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
17118               FormatStyle::IEBS_Indent);
17119   CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
17120               FormatStyle::IEBS_NoIndent);
17121 
17122   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
17123   CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing,
17124               FormatStyle::BFCS_Both);
17125   CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing,
17126               FormatStyle::BFCS_None);
17127   CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing,
17128               FormatStyle::BFCS_Before);
17129   CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing,
17130               FormatStyle::BFCS_After);
17131 
17132   Style.SortJavaStaticImport = FormatStyle::SJSIO_Before;
17133   CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport,
17134               FormatStyle::SJSIO_After);
17135   CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport,
17136               FormatStyle::SJSIO_Before);
17137 
17138   // FIXME: This is required because parsing a configuration simply overwrites
17139   // the first N elements of the list instead of resetting it.
17140   Style.ForEachMacros.clear();
17141   std::vector<std::string> BoostForeach;
17142   BoostForeach.push_back("BOOST_FOREACH");
17143   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
17144   std::vector<std::string> BoostAndQForeach;
17145   BoostAndQForeach.push_back("BOOST_FOREACH");
17146   BoostAndQForeach.push_back("Q_FOREACH");
17147   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
17148               BoostAndQForeach);
17149 
17150   Style.AttributeMacros.clear();
17151   CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros,
17152               std::vector<std::string>{"__capability"});
17153   CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros,
17154               std::vector<std::string>({"attr1", "attr2"}));
17155 
17156   Style.StatementAttributeLikeMacros.clear();
17157   CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]",
17158               StatementAttributeLikeMacros,
17159               std::vector<std::string>({"emit", "Q_EMIT"}));
17160 
17161   Style.StatementMacros.clear();
17162   CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
17163               std::vector<std::string>{"QUNUSED"});
17164   CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
17165               std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
17166 
17167   Style.NamespaceMacros.clear();
17168   CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
17169               std::vector<std::string>{"TESTSUITE"});
17170   CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
17171               std::vector<std::string>({"TESTSUITE", "SUITE"}));
17172 
17173   Style.WhitespaceSensitiveMacros.clear();
17174   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
17175               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
17176   CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
17177               WhitespaceSensitiveMacros,
17178               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
17179   Style.WhitespaceSensitiveMacros.clear();
17180   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
17181               WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
17182   CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
17183               WhitespaceSensitiveMacros,
17184               std::vector<std::string>({"STRINGIZE", "ASSERT"}));
17185 
17186   Style.IncludeStyle.IncludeCategories.clear();
17187   std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
17188       {"abc/.*", 2, 0, false}, {".*", 1, 0, true}};
17189   CHECK_PARSE("IncludeCategories:\n"
17190               "  - Regex: abc/.*\n"
17191               "    Priority: 2\n"
17192               "  - Regex: .*\n"
17193               "    Priority: 1\n"
17194               "    CaseSensitive: true\n",
17195               IncludeStyle.IncludeCategories, ExpectedCategories);
17196   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
17197               "abc$");
17198   CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
17199               IncludeStyle.IncludeIsMainSourceRegex, "abc$");
17200 
17201   Style.SortIncludes = FormatStyle::SI_Never;
17202   CHECK_PARSE("SortIncludes: true", SortIncludes,
17203               FormatStyle::SI_CaseSensitive);
17204   CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
17205   CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
17206               FormatStyle::SI_CaseInsensitive);
17207   CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
17208               FormatStyle::SI_CaseSensitive);
17209   CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
17210 
17211   Style.RawStringFormats.clear();
17212   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
17213       {
17214           FormatStyle::LK_TextProto,
17215           {"pb", "proto"},
17216           {"PARSE_TEXT_PROTO"},
17217           /*CanonicalDelimiter=*/"",
17218           "llvm",
17219       },
17220       {
17221           FormatStyle::LK_Cpp,
17222           {"cc", "cpp"},
17223           {"C_CODEBLOCK", "CPPEVAL"},
17224           /*CanonicalDelimiter=*/"cc",
17225           /*BasedOnStyle=*/"",
17226       },
17227   };
17228 
17229   CHECK_PARSE("RawStringFormats:\n"
17230               "  - Language: TextProto\n"
17231               "    Delimiters:\n"
17232               "      - 'pb'\n"
17233               "      - 'proto'\n"
17234               "    EnclosingFunctions:\n"
17235               "      - 'PARSE_TEXT_PROTO'\n"
17236               "    BasedOnStyle: llvm\n"
17237               "  - Language: Cpp\n"
17238               "    Delimiters:\n"
17239               "      - 'cc'\n"
17240               "      - 'cpp'\n"
17241               "    EnclosingFunctions:\n"
17242               "      - 'C_CODEBLOCK'\n"
17243               "      - 'CPPEVAL'\n"
17244               "    CanonicalDelimiter: 'cc'",
17245               RawStringFormats, ExpectedRawStringFormats);
17246 
17247   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17248               "  Minimum: 0\n"
17249               "  Maximum: 0",
17250               SpacesInLineCommentPrefix.Minimum, 0u);
17251   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u);
17252   Style.SpacesInLineCommentPrefix.Minimum = 1;
17253   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17254               "  Minimum: 2",
17255               SpacesInLineCommentPrefix.Minimum, 0u);
17256   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17257               "  Maximum: -1",
17258               SpacesInLineCommentPrefix.Maximum, -1u);
17259   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17260               "  Minimum: 2",
17261               SpacesInLineCommentPrefix.Minimum, 2u);
17262   CHECK_PARSE("SpacesInLineCommentPrefix:\n"
17263               "  Maximum: 1",
17264               SpacesInLineCommentPrefix.Maximum, 1u);
17265   EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u);
17266 
17267   Style.SpacesInAngles = FormatStyle::SIAS_Always;
17268   CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never);
17269   CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles,
17270               FormatStyle::SIAS_Always);
17271   CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave);
17272   // For backward compatibility:
17273   CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never);
17274   CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always);
17275 }
17276 
17277 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
17278   FormatStyle Style = {};
17279   Style.Language = FormatStyle::LK_Cpp;
17280   CHECK_PARSE("Language: Cpp\n"
17281               "IndentWidth: 12",
17282               IndentWidth, 12u);
17283   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
17284                                "IndentWidth: 34",
17285                                &Style),
17286             ParseError::Unsuitable);
17287   FormatStyle BinPackedTCS = {};
17288   BinPackedTCS.Language = FormatStyle::LK_JavaScript;
17289   EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
17290                                "InsertTrailingCommas: Wrapped",
17291                                &BinPackedTCS),
17292             ParseError::BinPackTrailingCommaConflict);
17293   EXPECT_EQ(12u, Style.IndentWidth);
17294   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
17295   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
17296 
17297   Style.Language = FormatStyle::LK_JavaScript;
17298   CHECK_PARSE("Language: JavaScript\n"
17299               "IndentWidth: 12",
17300               IndentWidth, 12u);
17301   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
17302   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
17303                                "IndentWidth: 34",
17304                                &Style),
17305             ParseError::Unsuitable);
17306   EXPECT_EQ(23u, Style.IndentWidth);
17307   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
17308   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
17309 
17310   CHECK_PARSE("BasedOnStyle: LLVM\n"
17311               "IndentWidth: 67",
17312               IndentWidth, 67u);
17313 
17314   CHECK_PARSE("---\n"
17315               "Language: JavaScript\n"
17316               "IndentWidth: 12\n"
17317               "---\n"
17318               "Language: Cpp\n"
17319               "IndentWidth: 34\n"
17320               "...\n",
17321               IndentWidth, 12u);
17322 
17323   Style.Language = FormatStyle::LK_Cpp;
17324   CHECK_PARSE("---\n"
17325               "Language: JavaScript\n"
17326               "IndentWidth: 12\n"
17327               "---\n"
17328               "Language: Cpp\n"
17329               "IndentWidth: 34\n"
17330               "...\n",
17331               IndentWidth, 34u);
17332   CHECK_PARSE("---\n"
17333               "IndentWidth: 78\n"
17334               "---\n"
17335               "Language: JavaScript\n"
17336               "IndentWidth: 56\n"
17337               "...\n",
17338               IndentWidth, 78u);
17339 
17340   Style.ColumnLimit = 123;
17341   Style.IndentWidth = 234;
17342   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
17343   Style.TabWidth = 345;
17344   EXPECT_FALSE(parseConfiguration("---\n"
17345                                   "IndentWidth: 456\n"
17346                                   "BreakBeforeBraces: Allman\n"
17347                                   "---\n"
17348                                   "Language: JavaScript\n"
17349                                   "IndentWidth: 111\n"
17350                                   "TabWidth: 111\n"
17351                                   "---\n"
17352                                   "Language: Cpp\n"
17353                                   "BreakBeforeBraces: Stroustrup\n"
17354                                   "TabWidth: 789\n"
17355                                   "...\n",
17356                                   &Style));
17357   EXPECT_EQ(123u, Style.ColumnLimit);
17358   EXPECT_EQ(456u, Style.IndentWidth);
17359   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
17360   EXPECT_EQ(789u, Style.TabWidth);
17361 
17362   EXPECT_EQ(parseConfiguration("---\n"
17363                                "Language: JavaScript\n"
17364                                "IndentWidth: 56\n"
17365                                "---\n"
17366                                "IndentWidth: 78\n"
17367                                "...\n",
17368                                &Style),
17369             ParseError::Error);
17370   EXPECT_EQ(parseConfiguration("---\n"
17371                                "Language: JavaScript\n"
17372                                "IndentWidth: 56\n"
17373                                "---\n"
17374                                "Language: JavaScript\n"
17375                                "IndentWidth: 78\n"
17376                                "...\n",
17377                                &Style),
17378             ParseError::Error);
17379 
17380   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
17381 }
17382 
17383 #undef CHECK_PARSE
17384 
17385 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
17386   FormatStyle Style = {};
17387   Style.Language = FormatStyle::LK_JavaScript;
17388   Style.BreakBeforeTernaryOperators = true;
17389   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
17390   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
17391 
17392   Style.BreakBeforeTernaryOperators = true;
17393   EXPECT_EQ(0, parseConfiguration("---\n"
17394                                   "BasedOnStyle: Google\n"
17395                                   "---\n"
17396                                   "Language: JavaScript\n"
17397                                   "IndentWidth: 76\n"
17398                                   "...\n",
17399                                   &Style)
17400                    .value());
17401   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
17402   EXPECT_EQ(76u, Style.IndentWidth);
17403   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
17404 }
17405 
17406 TEST_F(FormatTest, ConfigurationRoundTripTest) {
17407   FormatStyle Style = getLLVMStyle();
17408   std::string YAML = configurationAsText(Style);
17409   FormatStyle ParsedStyle = {};
17410   ParsedStyle.Language = FormatStyle::LK_Cpp;
17411   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
17412   EXPECT_EQ(Style, ParsedStyle);
17413 }
17414 
17415 TEST_F(FormatTest, WorksFor8bitEncodings) {
17416   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
17417             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
17418             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
17419             "\"\xef\xee\xf0\xf3...\"",
17420             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
17421                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
17422                    "\xef\xee\xf0\xf3...\"",
17423                    getLLVMStyleWithColumns(12)));
17424 }
17425 
17426 TEST_F(FormatTest, HandlesUTF8BOM) {
17427   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
17428   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
17429             format("\xef\xbb\xbf#include <iostream>"));
17430   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
17431             format("\xef\xbb\xbf\n#include <iostream>"));
17432 }
17433 
17434 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
17435 #if !defined(_MSC_VER)
17436 
17437 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
17438   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
17439                getLLVMStyleWithColumns(35));
17440   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
17441                getLLVMStyleWithColumns(31));
17442   verifyFormat("// Однажды в студёную зимнюю пору...",
17443                getLLVMStyleWithColumns(36));
17444   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
17445   verifyFormat("/* Однажды в студёную зимнюю пору... */",
17446                getLLVMStyleWithColumns(39));
17447   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
17448                getLLVMStyleWithColumns(35));
17449 }
17450 
17451 TEST_F(FormatTest, SplitsUTF8Strings) {
17452   // Non-printable characters' width is currently considered to be the length in
17453   // bytes in UTF8. The characters can be displayed in very different manner
17454   // (zero-width, single width with a substitution glyph, expanded to their code
17455   // (e.g. "<8d>"), so there's no single correct way to handle them.
17456   EXPECT_EQ("\"aaaaÄ\"\n"
17457             "\"\xc2\x8d\";",
17458             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
17459   EXPECT_EQ("\"aaaaaaaÄ\"\n"
17460             "\"\xc2\x8d\";",
17461             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
17462   EXPECT_EQ("\"Однажды, в \"\n"
17463             "\"студёную \"\n"
17464             "\"зимнюю \"\n"
17465             "\"пору,\"",
17466             format("\"Однажды, в студёную зимнюю пору,\"",
17467                    getLLVMStyleWithColumns(13)));
17468   EXPECT_EQ(
17469       "\"一 二 三 \"\n"
17470       "\"四 五六 \"\n"
17471       "\"七 八 九 \"\n"
17472       "\"十\"",
17473       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
17474   EXPECT_EQ("\"一\t\"\n"
17475             "\"二 \t\"\n"
17476             "\"三 四 \"\n"
17477             "\"五\t\"\n"
17478             "\"六 \t\"\n"
17479             "\"七 \"\n"
17480             "\"八九十\tqq\"",
17481             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
17482                    getLLVMStyleWithColumns(11)));
17483 
17484   // UTF8 character in an escape sequence.
17485   EXPECT_EQ("\"aaaaaa\"\n"
17486             "\"\\\xC2\x8D\"",
17487             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
17488 }
17489 
17490 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
17491   EXPECT_EQ("const char *sssss =\n"
17492             "    \"一二三四五六七八\\\n"
17493             " 九 十\";",
17494             format("const char *sssss = \"一二三四五六七八\\\n"
17495                    " 九 十\";",
17496                    getLLVMStyleWithColumns(30)));
17497 }
17498 
17499 TEST_F(FormatTest, SplitsUTF8LineComments) {
17500   EXPECT_EQ("// aaaaÄ\xc2\x8d",
17501             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
17502   EXPECT_EQ("// Я из лесу\n"
17503             "// вышел; был\n"
17504             "// сильный\n"
17505             "// мороз.",
17506             format("// Я из лесу вышел; был сильный мороз.",
17507                    getLLVMStyleWithColumns(13)));
17508   EXPECT_EQ("// 一二三\n"
17509             "// 四五六七\n"
17510             "// 八  九\n"
17511             "// 十",
17512             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
17513 }
17514 
17515 TEST_F(FormatTest, SplitsUTF8BlockComments) {
17516   EXPECT_EQ("/* Гляжу,\n"
17517             " * поднимается\n"
17518             " * медленно в\n"
17519             " * гору\n"
17520             " * Лошадка,\n"
17521             " * везущая\n"
17522             " * хворосту\n"
17523             " * воз. */",
17524             format("/* Гляжу, поднимается медленно в гору\n"
17525                    " * Лошадка, везущая хворосту воз. */",
17526                    getLLVMStyleWithColumns(13)));
17527   EXPECT_EQ(
17528       "/* 一二三\n"
17529       " * 四五六七\n"
17530       " * 八  九\n"
17531       " * 十  */",
17532       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
17533   EXPECT_EQ("/* �������� ��������\n"
17534             " * ��������\n"
17535             " * ������-�� */",
17536             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
17537 }
17538 
17539 #endif // _MSC_VER
17540 
17541 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
17542   FormatStyle Style = getLLVMStyle();
17543 
17544   Style.ConstructorInitializerIndentWidth = 4;
17545   verifyFormat(
17546       "SomeClass::Constructor()\n"
17547       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
17548       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
17549       Style);
17550 
17551   Style.ConstructorInitializerIndentWidth = 2;
17552   verifyFormat(
17553       "SomeClass::Constructor()\n"
17554       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
17555       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
17556       Style);
17557 
17558   Style.ConstructorInitializerIndentWidth = 0;
17559   verifyFormat(
17560       "SomeClass::Constructor()\n"
17561       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
17562       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
17563       Style);
17564   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17565   verifyFormat(
17566       "SomeLongTemplateVariableName<\n"
17567       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
17568       Style);
17569   verifyFormat("bool smaller = 1 < "
17570                "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
17571                "                       "
17572                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
17573                Style);
17574 
17575   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
17576   verifyFormat("SomeClass::Constructor() :\n"
17577                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
17578                "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
17579                Style);
17580 }
17581 
17582 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
17583   FormatStyle Style = getLLVMStyle();
17584   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
17585   Style.ConstructorInitializerIndentWidth = 4;
17586   verifyFormat("SomeClass::Constructor()\n"
17587                "    : a(a)\n"
17588                "    , b(b)\n"
17589                "    , c(c) {}",
17590                Style);
17591   verifyFormat("SomeClass::Constructor()\n"
17592                "    : a(a) {}",
17593                Style);
17594 
17595   Style.ColumnLimit = 0;
17596   verifyFormat("SomeClass::Constructor()\n"
17597                "    : a(a) {}",
17598                Style);
17599   verifyFormat("SomeClass::Constructor() noexcept\n"
17600                "    : a(a) {}",
17601                Style);
17602   verifyFormat("SomeClass::Constructor()\n"
17603                "    : a(a)\n"
17604                "    , b(b)\n"
17605                "    , c(c) {}",
17606                Style);
17607   verifyFormat("SomeClass::Constructor()\n"
17608                "    : a(a) {\n"
17609                "  foo();\n"
17610                "  bar();\n"
17611                "}",
17612                Style);
17613 
17614   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
17615   verifyFormat("SomeClass::Constructor()\n"
17616                "    : a(a)\n"
17617                "    , b(b)\n"
17618                "    , c(c) {\n}",
17619                Style);
17620   verifyFormat("SomeClass::Constructor()\n"
17621                "    : a(a) {\n}",
17622                Style);
17623 
17624   Style.ColumnLimit = 80;
17625   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
17626   Style.ConstructorInitializerIndentWidth = 2;
17627   verifyFormat("SomeClass::Constructor()\n"
17628                "  : a(a)\n"
17629                "  , b(b)\n"
17630                "  , c(c) {}",
17631                Style);
17632 
17633   Style.ConstructorInitializerIndentWidth = 0;
17634   verifyFormat("SomeClass::Constructor()\n"
17635                ": a(a)\n"
17636                ", b(b)\n"
17637                ", c(c) {}",
17638                Style);
17639 
17640   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
17641   Style.ConstructorInitializerIndentWidth = 4;
17642   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
17643   verifyFormat(
17644       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
17645       Style);
17646   verifyFormat(
17647       "SomeClass::Constructor()\n"
17648       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
17649       Style);
17650   Style.ConstructorInitializerIndentWidth = 4;
17651   Style.ColumnLimit = 60;
17652   verifyFormat("SomeClass::Constructor()\n"
17653                "    : aaaaaaaa(aaaaaaaa)\n"
17654                "    , aaaaaaaa(aaaaaaaa)\n"
17655                "    , aaaaaaaa(aaaaaaaa) {}",
17656                Style);
17657 }
17658 
17659 TEST_F(FormatTest, Destructors) {
17660   verifyFormat("void F(int &i) { i.~int(); }");
17661   verifyFormat("void F(int &i) { i->~int(); }");
17662 }
17663 
17664 TEST_F(FormatTest, FormatsWithWebKitStyle) {
17665   FormatStyle Style = getWebKitStyle();
17666 
17667   // Don't indent in outer namespaces.
17668   verifyFormat("namespace outer {\n"
17669                "int i;\n"
17670                "namespace inner {\n"
17671                "    int i;\n"
17672                "} // namespace inner\n"
17673                "} // namespace outer\n"
17674                "namespace other_outer {\n"
17675                "int i;\n"
17676                "}",
17677                Style);
17678 
17679   // Don't indent case labels.
17680   verifyFormat("switch (variable) {\n"
17681                "case 1:\n"
17682                "case 2:\n"
17683                "    doSomething();\n"
17684                "    break;\n"
17685                "default:\n"
17686                "    ++variable;\n"
17687                "}",
17688                Style);
17689 
17690   // Wrap before binary operators.
17691   EXPECT_EQ("void f()\n"
17692             "{\n"
17693             "    if (aaaaaaaaaaaaaaaa\n"
17694             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
17695             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
17696             "        return;\n"
17697             "}",
17698             format("void f() {\n"
17699                    "if (aaaaaaaaaaaaaaaa\n"
17700                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
17701                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
17702                    "return;\n"
17703                    "}",
17704                    Style));
17705 
17706   // Allow functions on a single line.
17707   verifyFormat("void f() { return; }", Style);
17708 
17709   // Allow empty blocks on a single line and insert a space in empty blocks.
17710   EXPECT_EQ("void f() { }", format("void f() {}", Style));
17711   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
17712   // However, don't merge non-empty short loops.
17713   EXPECT_EQ("while (true) {\n"
17714             "    continue;\n"
17715             "}",
17716             format("while (true) { continue; }", Style));
17717 
17718   // Constructor initializers are formatted one per line with the "," on the
17719   // new line.
17720   verifyFormat("Constructor()\n"
17721                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
17722                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
17723                "          aaaaaaaaaaaaaa)\n"
17724                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
17725                "{\n"
17726                "}",
17727                Style);
17728   verifyFormat("SomeClass::Constructor()\n"
17729                "    : a(a)\n"
17730                "{\n"
17731                "}",
17732                Style);
17733   EXPECT_EQ("SomeClass::Constructor()\n"
17734             "    : a(a)\n"
17735             "{\n"
17736             "}",
17737             format("SomeClass::Constructor():a(a){}", Style));
17738   verifyFormat("SomeClass::Constructor()\n"
17739                "    : a(a)\n"
17740                "    , b(b)\n"
17741                "    , c(c)\n"
17742                "{\n"
17743                "}",
17744                Style);
17745   verifyFormat("SomeClass::Constructor()\n"
17746                "    : a(a)\n"
17747                "{\n"
17748                "    foo();\n"
17749                "    bar();\n"
17750                "}",
17751                Style);
17752 
17753   // Access specifiers should be aligned left.
17754   verifyFormat("class C {\n"
17755                "public:\n"
17756                "    int i;\n"
17757                "};",
17758                Style);
17759 
17760   // Do not align comments.
17761   verifyFormat("int a; // Do not\n"
17762                "double b; // align comments.",
17763                Style);
17764 
17765   // Do not align operands.
17766   EXPECT_EQ("ASSERT(aaaa\n"
17767             "    || bbbb);",
17768             format("ASSERT ( aaaa\n||bbbb);", Style));
17769 
17770   // Accept input's line breaks.
17771   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
17772             "    || bbbbbbbbbbbbbbb) {\n"
17773             "    i++;\n"
17774             "}",
17775             format("if (aaaaaaaaaaaaaaa\n"
17776                    "|| bbbbbbbbbbbbbbb) { i++; }",
17777                    Style));
17778   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
17779             "    i++;\n"
17780             "}",
17781             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
17782 
17783   // Don't automatically break all macro definitions (llvm.org/PR17842).
17784   verifyFormat("#define aNumber 10", Style);
17785   // However, generally keep the line breaks that the user authored.
17786   EXPECT_EQ("#define aNumber \\\n"
17787             "    10",
17788             format("#define aNumber \\\n"
17789                    " 10",
17790                    Style));
17791 
17792   // Keep empty and one-element array literals on a single line.
17793   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
17794             "                                  copyItems:YES];",
17795             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
17796                    "copyItems:YES];",
17797                    Style));
17798   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
17799             "                                  copyItems:YES];",
17800             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
17801                    "             copyItems:YES];",
17802                    Style));
17803   // FIXME: This does not seem right, there should be more indentation before
17804   // the array literal's entries. Nested blocks have the same problem.
17805   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
17806             "    @\"a\",\n"
17807             "    @\"a\"\n"
17808             "]\n"
17809             "                                  copyItems:YES];",
17810             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
17811                    "     @\"a\",\n"
17812                    "     @\"a\"\n"
17813                    "     ]\n"
17814                    "       copyItems:YES];",
17815                    Style));
17816   EXPECT_EQ(
17817       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
17818       "                                  copyItems:YES];",
17819       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
17820              "   copyItems:YES];",
17821              Style));
17822 
17823   verifyFormat("[self.a b:c c:d];", Style);
17824   EXPECT_EQ("[self.a b:c\n"
17825             "        c:d];",
17826             format("[self.a b:c\n"
17827                    "c:d];",
17828                    Style));
17829 }
17830 
17831 TEST_F(FormatTest, FormatsLambdas) {
17832   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
17833   verifyFormat(
17834       "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
17835   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
17836   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
17837   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
17838   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
17839   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
17840   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
17841   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
17842   verifyFormat("int x = f(*+[] {});");
17843   verifyFormat("void f() {\n"
17844                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
17845                "}\n");
17846   verifyFormat("void f() {\n"
17847                "  other(x.begin(), //\n"
17848                "        x.end(),   //\n"
17849                "        [&](int, int) { return 1; });\n"
17850                "}\n");
17851   verifyFormat("void f() {\n"
17852                "  other.other.other.other.other(\n"
17853                "      x.begin(), x.end(),\n"
17854                "      [something, rather](int, int, int, int, int, int, int) { "
17855                "return 1; });\n"
17856                "}\n");
17857   verifyFormat(
17858       "void f() {\n"
17859       "  other.other.other.other.other(\n"
17860       "      x.begin(), x.end(),\n"
17861       "      [something, rather](int, int, int, int, int, int, int) {\n"
17862       "        //\n"
17863       "      });\n"
17864       "}\n");
17865   verifyFormat("SomeFunction([]() { // A cool function...\n"
17866                "  return 43;\n"
17867                "});");
17868   EXPECT_EQ("SomeFunction([]() {\n"
17869             "#define A a\n"
17870             "  return 43;\n"
17871             "});",
17872             format("SomeFunction([](){\n"
17873                    "#define A a\n"
17874                    "return 43;\n"
17875                    "});"));
17876   verifyFormat("void f() {\n"
17877                "  SomeFunction([](decltype(x), A *a) {});\n"
17878                "  SomeFunction([](typeof(x), A *a) {});\n"
17879                "  SomeFunction([](_Atomic(x), A *a) {});\n"
17880                "  SomeFunction([](__underlying_type(x), A *a) {});\n"
17881                "}");
17882   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
17883                "    [](const aaaaaaaaaa &a) { return a; });");
17884   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
17885                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
17886                "});");
17887   verifyFormat("Constructor()\n"
17888                "    : Field([] { // comment\n"
17889                "        int i;\n"
17890                "      }) {}");
17891   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
17892                "  return some_parameter.size();\n"
17893                "};");
17894   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
17895                "    [](const string &s) { return s; };");
17896   verifyFormat("int i = aaaaaa ? 1 //\n"
17897                "               : [] {\n"
17898                "                   return 2; //\n"
17899                "                 }();");
17900   verifyFormat("llvm::errs() << \"number of twos is \"\n"
17901                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
17902                "                  return x == 2; // force break\n"
17903                "                });");
17904   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
17905                "    [=](int iiiiiiiiiiii) {\n"
17906                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
17907                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
17908                "    });",
17909                getLLVMStyleWithColumns(60));
17910   verifyFormat("SomeFunction({[&] {\n"
17911                "                // comment\n"
17912                "              },\n"
17913                "              [&] {\n"
17914                "                // comment\n"
17915                "              }});");
17916   verifyFormat("SomeFunction({[&] {\n"
17917                "  // comment\n"
17918                "}});");
17919   verifyFormat(
17920       "virtual aaaaaaaaaaaaaaaa(\n"
17921       "    std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
17922       "    aaaaa aaaaaaaaa);");
17923 
17924   // Lambdas with return types.
17925   verifyFormat("int c = []() -> int { return 2; }();\n");
17926   verifyFormat("int c = []() -> int * { return 2; }();\n");
17927   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
17928   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
17929   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
17930   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
17931   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
17932   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
17933   verifyFormat("[a, a]() -> a<1> {};");
17934   verifyFormat("[]() -> foo<5 + 2> { return {}; };");
17935   verifyFormat("[]() -> foo<5 - 2> { return {}; };");
17936   verifyFormat("[]() -> foo<5 / 2> { return {}; };");
17937   verifyFormat("[]() -> foo<5 * 2> { return {}; };");
17938   verifyFormat("[]() -> foo<5 % 2> { return {}; };");
17939   verifyFormat("[]() -> foo<5 << 2> { return {}; };");
17940   verifyFormat("[]() -> foo<!5> { return {}; };");
17941   verifyFormat("[]() -> foo<~5> { return {}; };");
17942   verifyFormat("[]() -> foo<5 | 2> { return {}; };");
17943   verifyFormat("[]() -> foo<5 || 2> { return {}; };");
17944   verifyFormat("[]() -> foo<5 & 2> { return {}; };");
17945   verifyFormat("[]() -> foo<5 && 2> { return {}; };");
17946   verifyFormat("[]() -> foo<5 == 2> { return {}; };");
17947   verifyFormat("[]() -> foo<5 != 2> { return {}; };");
17948   verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
17949   verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
17950   verifyFormat("[]() -> foo<5 < 2> { return {}; };");
17951   verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
17952   verifyFormat("namespace bar {\n"
17953                "// broken:\n"
17954                "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
17955                "} // namespace bar");
17956   verifyFormat("namespace bar {\n"
17957                "// broken:\n"
17958                "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
17959                "} // namespace bar");
17960   verifyFormat("namespace bar {\n"
17961                "// broken:\n"
17962                "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
17963                "} // namespace bar");
17964   verifyFormat("namespace bar {\n"
17965                "// broken:\n"
17966                "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
17967                "} // namespace bar");
17968   verifyFormat("namespace bar {\n"
17969                "// broken:\n"
17970                "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
17971                "} // namespace bar");
17972   verifyFormat("namespace bar {\n"
17973                "// broken:\n"
17974                "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
17975                "} // namespace bar");
17976   verifyFormat("namespace bar {\n"
17977                "// broken:\n"
17978                "auto foo{[]() -> foo<!5> { return {}; }};\n"
17979                "} // namespace bar");
17980   verifyFormat("namespace bar {\n"
17981                "// broken:\n"
17982                "auto foo{[]() -> foo<~5> { return {}; }};\n"
17983                "} // namespace bar");
17984   verifyFormat("namespace bar {\n"
17985                "// broken:\n"
17986                "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
17987                "} // namespace bar");
17988   verifyFormat("namespace bar {\n"
17989                "// broken:\n"
17990                "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
17991                "} // namespace bar");
17992   verifyFormat("namespace bar {\n"
17993                "// broken:\n"
17994                "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
17995                "} // namespace bar");
17996   verifyFormat("namespace bar {\n"
17997                "// broken:\n"
17998                "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
17999                "} // namespace bar");
18000   verifyFormat("namespace bar {\n"
18001                "// broken:\n"
18002                "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
18003                "} // namespace bar");
18004   verifyFormat("namespace bar {\n"
18005                "// broken:\n"
18006                "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
18007                "} // namespace bar");
18008   verifyFormat("namespace bar {\n"
18009                "// broken:\n"
18010                "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
18011                "} // namespace bar");
18012   verifyFormat("namespace bar {\n"
18013                "// broken:\n"
18014                "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
18015                "} // namespace bar");
18016   verifyFormat("namespace bar {\n"
18017                "// broken:\n"
18018                "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
18019                "} // namespace bar");
18020   verifyFormat("namespace bar {\n"
18021                "// broken:\n"
18022                "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
18023                "} // namespace bar");
18024   verifyFormat("[]() -> a<1> {};");
18025   verifyFormat("[]() -> a<1> { ; };");
18026   verifyFormat("[]() -> a<1> { ; }();");
18027   verifyFormat("[a, a]() -> a<true> {};");
18028   verifyFormat("[]() -> a<true> {};");
18029   verifyFormat("[]() -> a<true> { ; };");
18030   verifyFormat("[]() -> a<true> { ; }();");
18031   verifyFormat("[a, a]() -> a<false> {};");
18032   verifyFormat("[]() -> a<false> {};");
18033   verifyFormat("[]() -> a<false> { ; };");
18034   verifyFormat("[]() -> a<false> { ; }();");
18035   verifyFormat("auto foo{[]() -> foo<false> { ; }};");
18036   verifyFormat("namespace bar {\n"
18037                "auto foo{[]() -> foo<false> { ; }};\n"
18038                "} // namespace bar");
18039   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
18040                "                   int j) -> int {\n"
18041                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
18042                "};");
18043   verifyFormat(
18044       "aaaaaaaaaaaaaaaaaaaaaa(\n"
18045       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
18046       "      return aaaaaaaaaaaaaaaaa;\n"
18047       "    });",
18048       getLLVMStyleWithColumns(70));
18049   verifyFormat("[]() //\n"
18050                "    -> int {\n"
18051                "  return 1; //\n"
18052                "};");
18053   verifyFormat("[]() -> Void<T...> {};");
18054   verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
18055 
18056   // Lambdas with explicit template argument lists.
18057   verifyFormat(
18058       "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
18059 
18060   // Multiple lambdas in the same parentheses change indentation rules. These
18061   // lambdas are forced to start on new lines.
18062   verifyFormat("SomeFunction(\n"
18063                "    []() {\n"
18064                "      //\n"
18065                "    },\n"
18066                "    []() {\n"
18067                "      //\n"
18068                "    });");
18069 
18070   // A lambda passed as arg0 is always pushed to the next line.
18071   verifyFormat("SomeFunction(\n"
18072                "    [this] {\n"
18073                "      //\n"
18074                "    },\n"
18075                "    1);\n");
18076 
18077   // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
18078   // the arg0 case above.
18079   auto Style = getGoogleStyle();
18080   Style.BinPackArguments = false;
18081   verifyFormat("SomeFunction(\n"
18082                "    a,\n"
18083                "    [this] {\n"
18084                "      //\n"
18085                "    },\n"
18086                "    b);\n",
18087                Style);
18088   verifyFormat("SomeFunction(\n"
18089                "    a,\n"
18090                "    [this] {\n"
18091                "      //\n"
18092                "    },\n"
18093                "    b);\n");
18094 
18095   // A lambda with a very long line forces arg0 to be pushed out irrespective of
18096   // the BinPackArguments value (as long as the code is wide enough).
18097   verifyFormat(
18098       "something->SomeFunction(\n"
18099       "    a,\n"
18100       "    [this] {\n"
18101       "      "
18102       "D0000000000000000000000000000000000000000000000000000000000001();\n"
18103       "    },\n"
18104       "    b);\n");
18105 
18106   // A multi-line lambda is pulled up as long as the introducer fits on the
18107   // previous line and there are no further args.
18108   verifyFormat("function(1, [this, that] {\n"
18109                "  //\n"
18110                "});\n");
18111   verifyFormat("function([this, that] {\n"
18112                "  //\n"
18113                "});\n");
18114   // FIXME: this format is not ideal and we should consider forcing the first
18115   // arg onto its own line.
18116   verifyFormat("function(a, b, c, //\n"
18117                "         d, [this, that] {\n"
18118                "           //\n"
18119                "         });\n");
18120 
18121   // Multiple lambdas are treated correctly even when there is a short arg0.
18122   verifyFormat("SomeFunction(\n"
18123                "    1,\n"
18124                "    [this] {\n"
18125                "      //\n"
18126                "    },\n"
18127                "    [this] {\n"
18128                "      //\n"
18129                "    },\n"
18130                "    1);\n");
18131 
18132   // More complex introducers.
18133   verifyFormat("return [i, args...] {};");
18134 
18135   // Not lambdas.
18136   verifyFormat("constexpr char hello[]{\"hello\"};");
18137   verifyFormat("double &operator[](int i) { return 0; }\n"
18138                "int i;");
18139   verifyFormat("std::unique_ptr<int[]> foo() {}");
18140   verifyFormat("int i = a[a][a]->f();");
18141   verifyFormat("int i = (*b)[a]->f();");
18142 
18143   // Other corner cases.
18144   verifyFormat("void f() {\n"
18145                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
18146                "  );\n"
18147                "}");
18148 
18149   // Lambdas created through weird macros.
18150   verifyFormat("void f() {\n"
18151                "  MACRO((const AA &a) { return 1; });\n"
18152                "  MACRO((AA &a) { return 1; });\n"
18153                "}");
18154 
18155   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
18156                "      doo_dah();\n"
18157                "      doo_dah();\n"
18158                "    })) {\n"
18159                "}");
18160   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
18161                "                doo_dah();\n"
18162                "                doo_dah();\n"
18163                "              })) {\n"
18164                "}");
18165   verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
18166                "                doo_dah();\n"
18167                "                doo_dah();\n"
18168                "              })) {\n"
18169                "}");
18170   verifyFormat("auto lambda = []() {\n"
18171                "  int a = 2\n"
18172                "#if A\n"
18173                "          + 2\n"
18174                "#endif\n"
18175                "      ;\n"
18176                "};");
18177 
18178   // Lambdas with complex multiline introducers.
18179   verifyFormat(
18180       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
18181       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
18182       "        -> ::std::unordered_set<\n"
18183       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
18184       "      //\n"
18185       "    });");
18186 
18187   FormatStyle DoNotMerge = getLLVMStyle();
18188   DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
18189   verifyFormat("auto c = []() {\n"
18190                "  return b;\n"
18191                "};",
18192                "auto c = []() { return b; };", DoNotMerge);
18193   verifyFormat("auto c = []() {\n"
18194                "};",
18195                " auto c = []() {};", DoNotMerge);
18196 
18197   FormatStyle MergeEmptyOnly = getLLVMStyle();
18198   MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
18199   verifyFormat("auto c = []() {\n"
18200                "  return b;\n"
18201                "};",
18202                "auto c = []() {\n"
18203                "  return b;\n"
18204                " };",
18205                MergeEmptyOnly);
18206   verifyFormat("auto c = []() {};",
18207                "auto c = []() {\n"
18208                "};",
18209                MergeEmptyOnly);
18210 
18211   FormatStyle MergeInline = getLLVMStyle();
18212   MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
18213   verifyFormat("auto c = []() {\n"
18214                "  return b;\n"
18215                "};",
18216                "auto c = []() { return b; };", MergeInline);
18217   verifyFormat("function([]() { return b; })", "function([]() { return b; })",
18218                MergeInline);
18219   verifyFormat("function([]() { return b; }, a)",
18220                "function([]() { return b; }, a)", MergeInline);
18221   verifyFormat("function(a, []() { return b; })",
18222                "function(a, []() { return b; })", MergeInline);
18223 
18224   // Check option "BraceWrapping.BeforeLambdaBody" and different state of
18225   // AllowShortLambdasOnASingleLine
18226   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
18227   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
18228   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
18229   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18230       FormatStyle::ShortLambdaStyle::SLS_None;
18231   verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
18232                "    []()\n"
18233                "    {\n"
18234                "      return 17;\n"
18235                "    });",
18236                LLVMWithBeforeLambdaBody);
18237   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
18238                "    []()\n"
18239                "    {\n"
18240                "    });",
18241                LLVMWithBeforeLambdaBody);
18242   verifyFormat("auto fct_SLS_None = []()\n"
18243                "{\n"
18244                "  return 17;\n"
18245                "};",
18246                LLVMWithBeforeLambdaBody);
18247   verifyFormat("TwoNestedLambdas_SLS_None(\n"
18248                "    []()\n"
18249                "    {\n"
18250                "      return Call(\n"
18251                "          []()\n"
18252                "          {\n"
18253                "            return 17;\n"
18254                "          });\n"
18255                "    });",
18256                LLVMWithBeforeLambdaBody);
18257   verifyFormat("void Fct()\n"
18258                "{\n"
18259                "  return {[]()\n"
18260                "          {\n"
18261                "            return 17;\n"
18262                "          }};\n"
18263                "}",
18264                LLVMWithBeforeLambdaBody);
18265 
18266   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18267       FormatStyle::ShortLambdaStyle::SLS_Empty;
18268   verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
18269                "    []()\n"
18270                "    {\n"
18271                "      return 17;\n"
18272                "    });",
18273                LLVMWithBeforeLambdaBody);
18274   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
18275                LLVMWithBeforeLambdaBody);
18276   verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
18277                "ongFunctionName_SLS_Empty(\n"
18278                "    []() {});",
18279                LLVMWithBeforeLambdaBody);
18280   verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
18281                "                                []()\n"
18282                "                                {\n"
18283                "                                  return 17;\n"
18284                "                                });",
18285                LLVMWithBeforeLambdaBody);
18286   verifyFormat("auto fct_SLS_Empty = []()\n"
18287                "{\n"
18288                "  return 17;\n"
18289                "};",
18290                LLVMWithBeforeLambdaBody);
18291   verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
18292                "    []()\n"
18293                "    {\n"
18294                "      return Call([]() {});\n"
18295                "    });",
18296                LLVMWithBeforeLambdaBody);
18297   verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
18298                "                           []()\n"
18299                "                           {\n"
18300                "                             return Call([]() {});\n"
18301                "                           });",
18302                LLVMWithBeforeLambdaBody);
18303   verifyFormat(
18304       "FctWithLongLineInLambda_SLS_Empty(\n"
18305       "    []()\n"
18306       "    {\n"
18307       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18308       "                               AndShouldNotBeConsiderAsInline,\n"
18309       "                               LambdaBodyMustBeBreak);\n"
18310       "    });",
18311       LLVMWithBeforeLambdaBody);
18312 
18313   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18314       FormatStyle::ShortLambdaStyle::SLS_Inline;
18315   verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
18316                LLVMWithBeforeLambdaBody);
18317   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
18318                LLVMWithBeforeLambdaBody);
18319   verifyFormat("auto fct_SLS_Inline = []()\n"
18320                "{\n"
18321                "  return 17;\n"
18322                "};",
18323                LLVMWithBeforeLambdaBody);
18324   verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
18325                "17; }); });",
18326                LLVMWithBeforeLambdaBody);
18327   verifyFormat(
18328       "FctWithLongLineInLambda_SLS_Inline(\n"
18329       "    []()\n"
18330       "    {\n"
18331       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18332       "                               AndShouldNotBeConsiderAsInline,\n"
18333       "                               LambdaBodyMustBeBreak);\n"
18334       "    });",
18335       LLVMWithBeforeLambdaBody);
18336   verifyFormat("FctWithMultipleParams_SLS_Inline("
18337                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
18338                "                                 []() { return 17; });",
18339                LLVMWithBeforeLambdaBody);
18340   verifyFormat(
18341       "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
18342       LLVMWithBeforeLambdaBody);
18343 
18344   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18345       FormatStyle::ShortLambdaStyle::SLS_All;
18346   verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
18347                LLVMWithBeforeLambdaBody);
18348   verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
18349                LLVMWithBeforeLambdaBody);
18350   verifyFormat("auto fct_SLS_All = []() { return 17; };",
18351                LLVMWithBeforeLambdaBody);
18352   verifyFormat("FctWithOneParam_SLS_All(\n"
18353                "    []()\n"
18354                "    {\n"
18355                "      // A cool function...\n"
18356                "      return 43;\n"
18357                "    });",
18358                LLVMWithBeforeLambdaBody);
18359   verifyFormat("FctWithMultipleParams_SLS_All("
18360                "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
18361                "                              []() { return 17; });",
18362                LLVMWithBeforeLambdaBody);
18363   verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
18364                LLVMWithBeforeLambdaBody);
18365   verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
18366                LLVMWithBeforeLambdaBody);
18367   verifyFormat(
18368       "FctWithLongLineInLambda_SLS_All(\n"
18369       "    []()\n"
18370       "    {\n"
18371       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18372       "                               AndShouldNotBeConsiderAsInline,\n"
18373       "                               LambdaBodyMustBeBreak);\n"
18374       "    });",
18375       LLVMWithBeforeLambdaBody);
18376   verifyFormat(
18377       "auto fct_SLS_All = []()\n"
18378       "{\n"
18379       "  return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18380       "                           AndShouldNotBeConsiderAsInline,\n"
18381       "                           LambdaBodyMustBeBreak);\n"
18382       "};",
18383       LLVMWithBeforeLambdaBody);
18384   LLVMWithBeforeLambdaBody.BinPackParameters = false;
18385   verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
18386                LLVMWithBeforeLambdaBody);
18387   verifyFormat(
18388       "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
18389       "                                FirstParam,\n"
18390       "                                SecondParam,\n"
18391       "                                ThirdParam,\n"
18392       "                                FourthParam);",
18393       LLVMWithBeforeLambdaBody);
18394   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
18395                "    []() { return "
18396                "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
18397                "    FirstParam,\n"
18398                "    SecondParam,\n"
18399                "    ThirdParam,\n"
18400                "    FourthParam);",
18401                LLVMWithBeforeLambdaBody);
18402   verifyFormat(
18403       "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
18404       "                                SecondParam,\n"
18405       "                                ThirdParam,\n"
18406       "                                FourthParam,\n"
18407       "                                []() { return SomeValueNotSoLong; });",
18408       LLVMWithBeforeLambdaBody);
18409   verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
18410                "    []()\n"
18411                "    {\n"
18412                "      return "
18413                "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
18414                "eConsiderAsInline;\n"
18415                "    });",
18416                LLVMWithBeforeLambdaBody);
18417   verifyFormat(
18418       "FctWithLongLineInLambda_SLS_All(\n"
18419       "    []()\n"
18420       "    {\n"
18421       "      return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
18422       "                               AndShouldNotBeConsiderAsInline,\n"
18423       "                               LambdaBodyMustBeBreak);\n"
18424       "    });",
18425       LLVMWithBeforeLambdaBody);
18426   verifyFormat("FctWithTwoParams_SLS_All(\n"
18427                "    []()\n"
18428                "    {\n"
18429                "      // A cool function...\n"
18430                "      return 43;\n"
18431                "    },\n"
18432                "    87);",
18433                LLVMWithBeforeLambdaBody);
18434   verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
18435                LLVMWithBeforeLambdaBody);
18436   verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
18437                LLVMWithBeforeLambdaBody);
18438   verifyFormat(
18439       "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
18440       LLVMWithBeforeLambdaBody);
18441   verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
18442                "}); }, x);",
18443                LLVMWithBeforeLambdaBody);
18444   verifyFormat("TwoNestedLambdas_SLS_All(\n"
18445                "    []()\n"
18446                "    {\n"
18447                "      // A cool function...\n"
18448                "      return Call([]() { return 17; });\n"
18449                "    });",
18450                LLVMWithBeforeLambdaBody);
18451   verifyFormat("TwoNestedLambdas_SLS_All(\n"
18452                "    []()\n"
18453                "    {\n"
18454                "      return Call(\n"
18455                "          []()\n"
18456                "          {\n"
18457                "            // A cool function...\n"
18458                "            return 17;\n"
18459                "          });\n"
18460                "    });",
18461                LLVMWithBeforeLambdaBody);
18462 }
18463 
18464 TEST_F(FormatTest, LambdaWithLineComments) {
18465   FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
18466   LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
18467   LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
18468   LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
18469       FormatStyle::ShortLambdaStyle::SLS_All;
18470 
18471   verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
18472   verifyFormat("auto k = []() // comment\n"
18473                "{ return; }",
18474                LLVMWithBeforeLambdaBody);
18475   verifyFormat("auto k = []() /* comment */ { return; }",
18476                LLVMWithBeforeLambdaBody);
18477   verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
18478                LLVMWithBeforeLambdaBody);
18479   verifyFormat("auto k = []() // X\n"
18480                "{ return; }",
18481                LLVMWithBeforeLambdaBody);
18482   verifyFormat(
18483       "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
18484       "{ return; }",
18485       LLVMWithBeforeLambdaBody);
18486 }
18487 
18488 TEST_F(FormatTest, EmptyLinesInLambdas) {
18489   verifyFormat("auto lambda = []() {\n"
18490                "  x(); //\n"
18491                "};",
18492                "auto lambda = []() {\n"
18493                "\n"
18494                "  x(); //\n"
18495                "\n"
18496                "};");
18497 }
18498 
18499 TEST_F(FormatTest, FormatsBlocks) {
18500   FormatStyle ShortBlocks = getLLVMStyle();
18501   ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
18502   verifyFormat("int (^Block)(int, int);", ShortBlocks);
18503   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
18504   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
18505   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
18506   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
18507   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
18508 
18509   verifyFormat("foo(^{ bar(); });", ShortBlocks);
18510   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
18511   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
18512 
18513   verifyFormat("[operation setCompletionBlock:^{\n"
18514                "  [self onOperationDone];\n"
18515                "}];");
18516   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
18517                "  [self onOperationDone];\n"
18518                "}]};");
18519   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
18520                "  f();\n"
18521                "}];");
18522   verifyFormat("int a = [operation block:^int(int *i) {\n"
18523                "  return 1;\n"
18524                "}];");
18525   verifyFormat("[myObject doSomethingWith:arg1\n"
18526                "                      aaa:^int(int *a) {\n"
18527                "                        return 1;\n"
18528                "                      }\n"
18529                "                      bbb:f(a * bbbbbbbb)];");
18530 
18531   verifyFormat("[operation setCompletionBlock:^{\n"
18532                "  [self.delegate newDataAvailable];\n"
18533                "}];",
18534                getLLVMStyleWithColumns(60));
18535   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
18536                "  NSString *path = [self sessionFilePath];\n"
18537                "  if (path) {\n"
18538                "    // ...\n"
18539                "  }\n"
18540                "});");
18541   verifyFormat("[[SessionService sharedService]\n"
18542                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
18543                "      if (window) {\n"
18544                "        [self windowDidLoad:window];\n"
18545                "      } else {\n"
18546                "        [self errorLoadingWindow];\n"
18547                "      }\n"
18548                "    }];");
18549   verifyFormat("void (^largeBlock)(void) = ^{\n"
18550                "  // ...\n"
18551                "};\n",
18552                getLLVMStyleWithColumns(40));
18553   verifyFormat("[[SessionService sharedService]\n"
18554                "    loadWindowWithCompletionBlock: //\n"
18555                "        ^(SessionWindow *window) {\n"
18556                "          if (window) {\n"
18557                "            [self windowDidLoad:window];\n"
18558                "          } else {\n"
18559                "            [self errorLoadingWindow];\n"
18560                "          }\n"
18561                "        }];",
18562                getLLVMStyleWithColumns(60));
18563   verifyFormat("[myObject doSomethingWith:arg1\n"
18564                "    firstBlock:^(Foo *a) {\n"
18565                "      // ...\n"
18566                "      int i;\n"
18567                "    }\n"
18568                "    secondBlock:^(Bar *b) {\n"
18569                "      // ...\n"
18570                "      int i;\n"
18571                "    }\n"
18572                "    thirdBlock:^Foo(Bar *b) {\n"
18573                "      // ...\n"
18574                "      int i;\n"
18575                "    }];");
18576   verifyFormat("[myObject doSomethingWith:arg1\n"
18577                "               firstBlock:-1\n"
18578                "              secondBlock:^(Bar *b) {\n"
18579                "                // ...\n"
18580                "                int i;\n"
18581                "              }];");
18582 
18583   verifyFormat("f(^{\n"
18584                "  @autoreleasepool {\n"
18585                "    if (a) {\n"
18586                "      g();\n"
18587                "    }\n"
18588                "  }\n"
18589                "});");
18590   verifyFormat("Block b = ^int *(A *a, B *b) {}");
18591   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
18592                "};");
18593 
18594   FormatStyle FourIndent = getLLVMStyle();
18595   FourIndent.ObjCBlockIndentWidth = 4;
18596   verifyFormat("[operation setCompletionBlock:^{\n"
18597                "    [self onOperationDone];\n"
18598                "}];",
18599                FourIndent);
18600 }
18601 
18602 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
18603   FormatStyle ZeroColumn = getLLVMStyle();
18604   ZeroColumn.ColumnLimit = 0;
18605 
18606   verifyFormat("[[SessionService sharedService] "
18607                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
18608                "  if (window) {\n"
18609                "    [self windowDidLoad:window];\n"
18610                "  } else {\n"
18611                "    [self errorLoadingWindow];\n"
18612                "  }\n"
18613                "}];",
18614                ZeroColumn);
18615   EXPECT_EQ("[[SessionService sharedService]\n"
18616             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
18617             "      if (window) {\n"
18618             "        [self windowDidLoad:window];\n"
18619             "      } else {\n"
18620             "        [self errorLoadingWindow];\n"
18621             "      }\n"
18622             "    }];",
18623             format("[[SessionService sharedService]\n"
18624                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
18625                    "                if (window) {\n"
18626                    "    [self windowDidLoad:window];\n"
18627                    "  } else {\n"
18628                    "    [self errorLoadingWindow];\n"
18629                    "  }\n"
18630                    "}];",
18631                    ZeroColumn));
18632   verifyFormat("[myObject doSomethingWith:arg1\n"
18633                "    firstBlock:^(Foo *a) {\n"
18634                "      // ...\n"
18635                "      int i;\n"
18636                "    }\n"
18637                "    secondBlock:^(Bar *b) {\n"
18638                "      // ...\n"
18639                "      int i;\n"
18640                "    }\n"
18641                "    thirdBlock:^Foo(Bar *b) {\n"
18642                "      // ...\n"
18643                "      int i;\n"
18644                "    }];",
18645                ZeroColumn);
18646   verifyFormat("f(^{\n"
18647                "  @autoreleasepool {\n"
18648                "    if (a) {\n"
18649                "      g();\n"
18650                "    }\n"
18651                "  }\n"
18652                "});",
18653                ZeroColumn);
18654   verifyFormat("void (^largeBlock)(void) = ^{\n"
18655                "  // ...\n"
18656                "};",
18657                ZeroColumn);
18658 
18659   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
18660   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
18661             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
18662   ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
18663   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
18664             "  int i;\n"
18665             "};",
18666             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
18667 }
18668 
18669 TEST_F(FormatTest, SupportsCRLF) {
18670   EXPECT_EQ("int a;\r\n"
18671             "int b;\r\n"
18672             "int c;\r\n",
18673             format("int a;\r\n"
18674                    "  int b;\r\n"
18675                    "    int c;\r\n",
18676                    getLLVMStyle()));
18677   EXPECT_EQ("int a;\r\n"
18678             "int b;\r\n"
18679             "int c;\r\n",
18680             format("int a;\r\n"
18681                    "  int b;\n"
18682                    "    int c;\r\n",
18683                    getLLVMStyle()));
18684   EXPECT_EQ("int a;\n"
18685             "int b;\n"
18686             "int c;\n",
18687             format("int a;\r\n"
18688                    "  int b;\n"
18689                    "    int c;\n",
18690                    getLLVMStyle()));
18691   EXPECT_EQ("\"aaaaaaa \"\r\n"
18692             "\"bbbbbbb\";\r\n",
18693             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
18694   EXPECT_EQ("#define A \\\r\n"
18695             "  b;      \\\r\n"
18696             "  c;      \\\r\n"
18697             "  d;\r\n",
18698             format("#define A \\\r\n"
18699                    "  b; \\\r\n"
18700                    "  c; d; \r\n",
18701                    getGoogleStyle()));
18702 
18703   EXPECT_EQ("/*\r\n"
18704             "multi line block comments\r\n"
18705             "should not introduce\r\n"
18706             "an extra carriage return\r\n"
18707             "*/\r\n",
18708             format("/*\r\n"
18709                    "multi line block comments\r\n"
18710                    "should not introduce\r\n"
18711                    "an extra carriage return\r\n"
18712                    "*/\r\n"));
18713   EXPECT_EQ("/*\r\n"
18714             "\r\n"
18715             "*/",
18716             format("/*\r\n"
18717                    "    \r\r\r\n"
18718                    "*/"));
18719 
18720   FormatStyle style = getLLVMStyle();
18721 
18722   style.DeriveLineEnding = true;
18723   style.UseCRLF = false;
18724   EXPECT_EQ("union FooBarBazQux {\n"
18725             "  int foo;\n"
18726             "  int bar;\n"
18727             "  int baz;\n"
18728             "};",
18729             format("union FooBarBazQux {\r\n"
18730                    "  int foo;\n"
18731                    "  int bar;\r\n"
18732                    "  int baz;\n"
18733                    "};",
18734                    style));
18735   style.UseCRLF = true;
18736   EXPECT_EQ("union FooBarBazQux {\r\n"
18737             "  int foo;\r\n"
18738             "  int bar;\r\n"
18739             "  int baz;\r\n"
18740             "};",
18741             format("union FooBarBazQux {\r\n"
18742                    "  int foo;\n"
18743                    "  int bar;\r\n"
18744                    "  int baz;\n"
18745                    "};",
18746                    style));
18747 
18748   style.DeriveLineEnding = false;
18749   style.UseCRLF = false;
18750   EXPECT_EQ("union FooBarBazQux {\n"
18751             "  int foo;\n"
18752             "  int bar;\n"
18753             "  int baz;\n"
18754             "  int qux;\n"
18755             "};",
18756             format("union FooBarBazQux {\r\n"
18757                    "  int foo;\n"
18758                    "  int bar;\r\n"
18759                    "  int baz;\n"
18760                    "  int qux;\r\n"
18761                    "};",
18762                    style));
18763   style.UseCRLF = true;
18764   EXPECT_EQ("union FooBarBazQux {\r\n"
18765             "  int foo;\r\n"
18766             "  int bar;\r\n"
18767             "  int baz;\r\n"
18768             "  int qux;\r\n"
18769             "};",
18770             format("union FooBarBazQux {\r\n"
18771                    "  int foo;\n"
18772                    "  int bar;\r\n"
18773                    "  int baz;\n"
18774                    "  int qux;\n"
18775                    "};",
18776                    style));
18777 
18778   style.DeriveLineEnding = true;
18779   style.UseCRLF = false;
18780   EXPECT_EQ("union FooBarBazQux {\r\n"
18781             "  int foo;\r\n"
18782             "  int bar;\r\n"
18783             "  int baz;\r\n"
18784             "  int qux;\r\n"
18785             "};",
18786             format("union FooBarBazQux {\r\n"
18787                    "  int foo;\n"
18788                    "  int bar;\r\n"
18789                    "  int baz;\n"
18790                    "  int qux;\r\n"
18791                    "};",
18792                    style));
18793   style.UseCRLF = true;
18794   EXPECT_EQ("union FooBarBazQux {\n"
18795             "  int foo;\n"
18796             "  int bar;\n"
18797             "  int baz;\n"
18798             "  int qux;\n"
18799             "};",
18800             format("union FooBarBazQux {\r\n"
18801                    "  int foo;\n"
18802                    "  int bar;\r\n"
18803                    "  int baz;\n"
18804                    "  int qux;\n"
18805                    "};",
18806                    style));
18807 }
18808 
18809 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
18810   verifyFormat("MY_CLASS(C) {\n"
18811                "  int i;\n"
18812                "  int j;\n"
18813                "};");
18814 }
18815 
18816 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
18817   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
18818   TwoIndent.ContinuationIndentWidth = 2;
18819 
18820   EXPECT_EQ("int i =\n"
18821             "  longFunction(\n"
18822             "    arg);",
18823             format("int i = longFunction(arg);", TwoIndent));
18824 
18825   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
18826   SixIndent.ContinuationIndentWidth = 6;
18827 
18828   EXPECT_EQ("int i =\n"
18829             "      longFunction(\n"
18830             "            arg);",
18831             format("int i = longFunction(arg);", SixIndent));
18832 }
18833 
18834 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
18835   FormatStyle Style = getLLVMStyle();
18836   verifyFormat("int Foo::getter(\n"
18837                "    //\n"
18838                ") const {\n"
18839                "  return foo;\n"
18840                "}",
18841                Style);
18842   verifyFormat("void Foo::setter(\n"
18843                "    //\n"
18844                ") {\n"
18845                "  foo = 1;\n"
18846                "}",
18847                Style);
18848 }
18849 
18850 TEST_F(FormatTest, SpacesInAngles) {
18851   FormatStyle Spaces = getLLVMStyle();
18852   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
18853 
18854   verifyFormat("vector< ::std::string > x1;", Spaces);
18855   verifyFormat("Foo< int, Bar > x2;", Spaces);
18856   verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
18857 
18858   verifyFormat("static_cast< int >(arg);", Spaces);
18859   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
18860   verifyFormat("f< int, float >();", Spaces);
18861   verifyFormat("template <> g() {}", Spaces);
18862   verifyFormat("template < std::vector< int > > f() {}", Spaces);
18863   verifyFormat("std::function< void(int, int) > fct;", Spaces);
18864   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
18865                Spaces);
18866 
18867   Spaces.Standard = FormatStyle::LS_Cpp03;
18868   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
18869   verifyFormat("A< A< int > >();", Spaces);
18870 
18871   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
18872   verifyFormat("A<A<int> >();", Spaces);
18873 
18874   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
18875   verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
18876                Spaces);
18877   verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
18878                Spaces);
18879 
18880   verifyFormat("A<A<int> >();", Spaces);
18881   verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
18882   verifyFormat("A< A< int > >();", Spaces);
18883 
18884   Spaces.Standard = FormatStyle::LS_Cpp11;
18885   Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
18886   verifyFormat("A< A< int > >();", Spaces);
18887 
18888   Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
18889   verifyFormat("vector<::std::string> x4;", Spaces);
18890   verifyFormat("vector<int> x5;", Spaces);
18891   verifyFormat("Foo<int, Bar> x6;", Spaces);
18892   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
18893 
18894   verifyFormat("A<A<int>>();", Spaces);
18895 
18896   Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
18897   verifyFormat("vector<::std::string> x4;", Spaces);
18898   verifyFormat("vector< ::std::string > x4;", Spaces);
18899   verifyFormat("vector<int> x5;", Spaces);
18900   verifyFormat("vector< int > x5;", Spaces);
18901   verifyFormat("Foo<int, Bar> x6;", Spaces);
18902   verifyFormat("Foo< int, Bar > x6;", Spaces);
18903   verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
18904   verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
18905 
18906   verifyFormat("A<A<int>>();", Spaces);
18907   verifyFormat("A< A< int > >();", Spaces);
18908   verifyFormat("A<A<int > >();", Spaces);
18909   verifyFormat("A< A< int>>();", Spaces);
18910 }
18911 
18912 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
18913   FormatStyle Style = getLLVMStyle();
18914   Style.SpaceAfterTemplateKeyword = false;
18915   verifyFormat("template<int> void foo();", Style);
18916 }
18917 
18918 TEST_F(FormatTest, TripleAngleBrackets) {
18919   verifyFormat("f<<<1, 1>>>();");
18920   verifyFormat("f<<<1, 1, 1, s>>>();");
18921   verifyFormat("f<<<a, b, c, d>>>();");
18922   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
18923   verifyFormat("f<param><<<1, 1>>>();");
18924   verifyFormat("f<1><<<1, 1>>>();");
18925   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
18926   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
18927                "aaaaaaaaaaa<<<\n    1, 1>>>();");
18928   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
18929                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
18930 }
18931 
18932 TEST_F(FormatTest, MergeLessLessAtEnd) {
18933   verifyFormat("<<");
18934   EXPECT_EQ("< < <", format("\\\n<<<"));
18935   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
18936                "aaallvm::outs() <<");
18937   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
18938                "aaaallvm::outs()\n    <<");
18939 }
18940 
18941 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
18942   std::string code = "#if A\n"
18943                      "#if B\n"
18944                      "a.\n"
18945                      "#endif\n"
18946                      "    a = 1;\n"
18947                      "#else\n"
18948                      "#endif\n"
18949                      "#if C\n"
18950                      "#else\n"
18951                      "#endif\n";
18952   EXPECT_EQ(code, format(code));
18953 }
18954 
18955 TEST_F(FormatTest, HandleConflictMarkers) {
18956   // Git/SVN conflict markers.
18957   EXPECT_EQ("int a;\n"
18958             "void f() {\n"
18959             "  callme(some(parameter1,\n"
18960             "<<<<<<< text by the vcs\n"
18961             "              parameter2),\n"
18962             "||||||| text by the vcs\n"
18963             "              parameter2),\n"
18964             "         parameter3,\n"
18965             "======= text by the vcs\n"
18966             "              parameter2, parameter3),\n"
18967             ">>>>>>> text by the vcs\n"
18968             "         otherparameter);\n",
18969             format("int a;\n"
18970                    "void f() {\n"
18971                    "  callme(some(parameter1,\n"
18972                    "<<<<<<< text by the vcs\n"
18973                    "  parameter2),\n"
18974                    "||||||| text by the vcs\n"
18975                    "  parameter2),\n"
18976                    "  parameter3,\n"
18977                    "======= text by the vcs\n"
18978                    "  parameter2,\n"
18979                    "  parameter3),\n"
18980                    ">>>>>>> text by the vcs\n"
18981                    "  otherparameter);\n"));
18982 
18983   // Perforce markers.
18984   EXPECT_EQ("void f() {\n"
18985             "  function(\n"
18986             ">>>> text by the vcs\n"
18987             "      parameter,\n"
18988             "==== text by the vcs\n"
18989             "      parameter,\n"
18990             "==== text by the vcs\n"
18991             "      parameter,\n"
18992             "<<<< text by the vcs\n"
18993             "      parameter);\n",
18994             format("void f() {\n"
18995                    "  function(\n"
18996                    ">>>> text by the vcs\n"
18997                    "  parameter,\n"
18998                    "==== text by the vcs\n"
18999                    "  parameter,\n"
19000                    "==== text by the vcs\n"
19001                    "  parameter,\n"
19002                    "<<<< text by the vcs\n"
19003                    "  parameter);\n"));
19004 
19005   EXPECT_EQ("<<<<<<<\n"
19006             "|||||||\n"
19007             "=======\n"
19008             ">>>>>>>",
19009             format("<<<<<<<\n"
19010                    "|||||||\n"
19011                    "=======\n"
19012                    ">>>>>>>"));
19013 
19014   EXPECT_EQ("<<<<<<<\n"
19015             "|||||||\n"
19016             "int i;\n"
19017             "=======\n"
19018             ">>>>>>>",
19019             format("<<<<<<<\n"
19020                    "|||||||\n"
19021                    "int i;\n"
19022                    "=======\n"
19023                    ">>>>>>>"));
19024 
19025   // FIXME: Handle parsing of macros around conflict markers correctly:
19026   EXPECT_EQ("#define Macro \\\n"
19027             "<<<<<<<\n"
19028             "Something \\\n"
19029             "|||||||\n"
19030             "Else \\\n"
19031             "=======\n"
19032             "Other \\\n"
19033             ">>>>>>>\n"
19034             "    End int i;\n",
19035             format("#define Macro \\\n"
19036                    "<<<<<<<\n"
19037                    "  Something \\\n"
19038                    "|||||||\n"
19039                    "  Else \\\n"
19040                    "=======\n"
19041                    "  Other \\\n"
19042                    ">>>>>>>\n"
19043                    "  End\n"
19044                    "int i;\n"));
19045 }
19046 
19047 TEST_F(FormatTest, DisableRegions) {
19048   EXPECT_EQ("int i;\n"
19049             "// clang-format off\n"
19050             "  int j;\n"
19051             "// clang-format on\n"
19052             "int k;",
19053             format(" int  i;\n"
19054                    "   // clang-format off\n"
19055                    "  int j;\n"
19056                    " // clang-format on\n"
19057                    "   int   k;"));
19058   EXPECT_EQ("int i;\n"
19059             "/* clang-format off */\n"
19060             "  int j;\n"
19061             "/* clang-format on */\n"
19062             "int k;",
19063             format(" int  i;\n"
19064                    "   /* clang-format off */\n"
19065                    "  int j;\n"
19066                    " /* clang-format on */\n"
19067                    "   int   k;"));
19068 
19069   // Don't reflow comments within disabled regions.
19070   EXPECT_EQ("// clang-format off\n"
19071             "// long long long long long long line\n"
19072             "/* clang-format on */\n"
19073             "/* long long long\n"
19074             " * long long long\n"
19075             " * line */\n"
19076             "int i;\n"
19077             "/* clang-format off */\n"
19078             "/* long long long long long long line */\n",
19079             format("// clang-format off\n"
19080                    "// long long long long long long line\n"
19081                    "/* clang-format on */\n"
19082                    "/* long long long long long long line */\n"
19083                    "int i;\n"
19084                    "/* clang-format off */\n"
19085                    "/* long long long long long long line */\n",
19086                    getLLVMStyleWithColumns(20)));
19087 }
19088 
19089 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
19090   format("? ) =");
19091   verifyNoCrash("#define a\\\n /**/}");
19092 }
19093 
19094 TEST_F(FormatTest, FormatsTableGenCode) {
19095   FormatStyle Style = getLLVMStyle();
19096   Style.Language = FormatStyle::LK_TableGen;
19097   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
19098 }
19099 
19100 TEST_F(FormatTest, ArrayOfTemplates) {
19101   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
19102             format("auto a = new unique_ptr<int > [ 10];"));
19103 
19104   FormatStyle Spaces = getLLVMStyle();
19105   Spaces.SpacesInSquareBrackets = true;
19106   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
19107             format("auto a = new unique_ptr<int > [10];", Spaces));
19108 }
19109 
19110 TEST_F(FormatTest, ArrayAsTemplateType) {
19111   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
19112             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
19113 
19114   FormatStyle Spaces = getLLVMStyle();
19115   Spaces.SpacesInSquareBrackets = true;
19116   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
19117             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
19118 }
19119 
19120 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
19121 
19122 TEST(FormatStyle, GetStyleWithEmptyFileName) {
19123   llvm::vfs::InMemoryFileSystem FS;
19124   auto Style1 = getStyle("file", "", "Google", "", &FS);
19125   ASSERT_TRUE((bool)Style1);
19126   ASSERT_EQ(*Style1, getGoogleStyle());
19127 }
19128 
19129 TEST(FormatStyle, GetStyleOfFile) {
19130   llvm::vfs::InMemoryFileSystem FS;
19131   // Test 1: format file in the same directory.
19132   ASSERT_TRUE(
19133       FS.addFile("/a/.clang-format", 0,
19134                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
19135   ASSERT_TRUE(
19136       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
19137   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
19138   ASSERT_TRUE((bool)Style1);
19139   ASSERT_EQ(*Style1, getLLVMStyle());
19140 
19141   // Test 2.1: fallback to default.
19142   ASSERT_TRUE(
19143       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
19144   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
19145   ASSERT_TRUE((bool)Style2);
19146   ASSERT_EQ(*Style2, getMozillaStyle());
19147 
19148   // Test 2.2: no format on 'none' fallback style.
19149   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
19150   ASSERT_TRUE((bool)Style2);
19151   ASSERT_EQ(*Style2, getNoStyle());
19152 
19153   // Test 2.3: format if config is found with no based style while fallback is
19154   // 'none'.
19155   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
19156                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
19157   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
19158   ASSERT_TRUE((bool)Style2);
19159   ASSERT_EQ(*Style2, getLLVMStyle());
19160 
19161   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
19162   Style2 = getStyle("{}", "a.h", "none", "", &FS);
19163   ASSERT_TRUE((bool)Style2);
19164   ASSERT_EQ(*Style2, getLLVMStyle());
19165 
19166   // Test 3: format file in parent directory.
19167   ASSERT_TRUE(
19168       FS.addFile("/c/.clang-format", 0,
19169                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
19170   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
19171                          llvm::MemoryBuffer::getMemBuffer("int i;")));
19172   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
19173   ASSERT_TRUE((bool)Style3);
19174   ASSERT_EQ(*Style3, getGoogleStyle());
19175 
19176   // Test 4: error on invalid fallback style
19177   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
19178   ASSERT_FALSE((bool)Style4);
19179   llvm::consumeError(Style4.takeError());
19180 
19181   // Test 5: error on invalid yaml on command line
19182   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
19183   ASSERT_FALSE((bool)Style5);
19184   llvm::consumeError(Style5.takeError());
19185 
19186   // Test 6: error on invalid style
19187   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
19188   ASSERT_FALSE((bool)Style6);
19189   llvm::consumeError(Style6.takeError());
19190 
19191   // Test 7: found config file, error on parsing it
19192   ASSERT_TRUE(
19193       FS.addFile("/d/.clang-format", 0,
19194                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
19195                                                   "InvalidKey: InvalidValue")));
19196   ASSERT_TRUE(
19197       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
19198   auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
19199   ASSERT_FALSE((bool)Style7a);
19200   llvm::consumeError(Style7a.takeError());
19201 
19202   auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true);
19203   ASSERT_TRUE((bool)Style7b);
19204 
19205   // Test 8: inferred per-language defaults apply.
19206   auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
19207   ASSERT_TRUE((bool)StyleTd);
19208   ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
19209 
19210   // Test 9.1: overwriting a file style, when parent no file exists with no
19211   // fallback style
19212   ASSERT_TRUE(FS.addFile(
19213       "/e/sub/.clang-format", 0,
19214       llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n"
19215                                        "ColumnLimit: 20")));
19216   ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0,
19217                          llvm::MemoryBuffer::getMemBuffer("int i;")));
19218   auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
19219   ASSERT_TRUE(static_cast<bool>(Style9));
19220   ASSERT_EQ(*Style9, [] {
19221     auto Style = getNoStyle();
19222     Style.ColumnLimit = 20;
19223     return Style;
19224   }());
19225 
19226   // Test 9.2: with LLVM fallback style
19227   Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS);
19228   ASSERT_TRUE(static_cast<bool>(Style9));
19229   ASSERT_EQ(*Style9, [] {
19230     auto Style = getLLVMStyle();
19231     Style.ColumnLimit = 20;
19232     return Style;
19233   }());
19234 
19235   // Test 9.3: with a parent file
19236   ASSERT_TRUE(
19237       FS.addFile("/e/.clang-format", 0,
19238                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n"
19239                                                   "UseTab: Always")));
19240   Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS);
19241   ASSERT_TRUE(static_cast<bool>(Style9));
19242   ASSERT_EQ(*Style9, [] {
19243     auto Style = getGoogleStyle();
19244     Style.ColumnLimit = 20;
19245     Style.UseTab = FormatStyle::UT_Always;
19246     return Style;
19247   }());
19248 
19249   // Test 9.4: propagate more than one level
19250   ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0,
19251                          llvm::MemoryBuffer::getMemBuffer("int i;")));
19252   ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0,
19253                          llvm::MemoryBuffer::getMemBuffer(
19254                              "BasedOnStyle: InheritParentConfig\n"
19255                              "WhitespaceSensitiveMacros: ['FOO', 'BAR']")));
19256   std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"};
19257 
19258   const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] {
19259     auto Style = getGoogleStyle();
19260     Style.ColumnLimit = 20;
19261     Style.UseTab = FormatStyle::UT_Always;
19262     Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros;
19263     return Style;
19264   }();
19265 
19266   ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros);
19267   Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS);
19268   ASSERT_TRUE(static_cast<bool>(Style9));
19269   ASSERT_EQ(*Style9, SubSubStyle);
19270 
19271   // Test 9.5: use InheritParentConfig as style name
19272   Style9 =
19273       getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS);
19274   ASSERT_TRUE(static_cast<bool>(Style9));
19275   ASSERT_EQ(*Style9, SubSubStyle);
19276 
19277   // Test 9.6: use command line style with inheritance
19278   Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp",
19279                     "none", "", &FS);
19280   ASSERT_TRUE(static_cast<bool>(Style9));
19281   ASSERT_EQ(*Style9, SubSubStyle);
19282 
19283   // Test 9.7: use command line style with inheritance and own config
19284   Style9 = getStyle("{BasedOnStyle: InheritParentConfig, "
19285                     "WhitespaceSensitiveMacros: ['FOO', 'BAR']}",
19286                     "/e/sub/code.cpp", "none", "", &FS);
19287   ASSERT_TRUE(static_cast<bool>(Style9));
19288   ASSERT_EQ(*Style9, SubSubStyle);
19289 
19290   // Test 9.8: use inheritance from a file without BasedOnStyle
19291   ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
19292                          llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
19293   ASSERT_TRUE(
19294       FS.addFile("/e/withoutbase/sub/.clang-format", 0,
19295                  llvm::MemoryBuffer::getMemBuffer(
19296                      "BasedOnStyle: InheritParentConfig\nIndentWidth: 7")));
19297   // Make sure we do not use the fallback style
19298   Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS);
19299   ASSERT_TRUE(static_cast<bool>(Style9));
19300   ASSERT_EQ(*Style9, [] {
19301     auto Style = getLLVMStyle();
19302     Style.ColumnLimit = 123;
19303     return Style;
19304   }());
19305 
19306   Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS);
19307   ASSERT_TRUE(static_cast<bool>(Style9));
19308   ASSERT_EQ(*Style9, [] {
19309     auto Style = getLLVMStyle();
19310     Style.ColumnLimit = 123;
19311     Style.IndentWidth = 7;
19312     return Style;
19313   }());
19314 }
19315 
19316 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
19317   // Column limit is 20.
19318   std::string Code = "Type *a =\n"
19319                      "    new Type();\n"
19320                      "g(iiiii, 0, jjjjj,\n"
19321                      "  0, kkkkk, 0, mm);\n"
19322                      "int  bad     = format   ;";
19323   std::string Expected = "auto a = new Type();\n"
19324                          "g(iiiii, nullptr,\n"
19325                          "  jjjjj, nullptr,\n"
19326                          "  kkkkk, nullptr,\n"
19327                          "  mm);\n"
19328                          "int  bad     = format   ;";
19329   FileID ID = Context.createInMemoryFile("format.cpp", Code);
19330   tooling::Replacements Replaces = toReplacements(
19331       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
19332                             "auto "),
19333        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
19334                             "nullptr"),
19335        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
19336                             "nullptr"),
19337        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
19338                             "nullptr")});
19339 
19340   format::FormatStyle Style = format::getLLVMStyle();
19341   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
19342   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
19343   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
19344       << llvm::toString(FormattedReplaces.takeError()) << "\n";
19345   auto Result = applyAllReplacements(Code, *FormattedReplaces);
19346   EXPECT_TRUE(static_cast<bool>(Result));
19347   EXPECT_EQ(Expected, *Result);
19348 }
19349 
19350 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
19351   std::string Code = "#include \"a.h\"\n"
19352                      "#include \"c.h\"\n"
19353                      "\n"
19354                      "int main() {\n"
19355                      "  return 0;\n"
19356                      "}";
19357   std::string Expected = "#include \"a.h\"\n"
19358                          "#include \"b.h\"\n"
19359                          "#include \"c.h\"\n"
19360                          "\n"
19361                          "int main() {\n"
19362                          "  return 0;\n"
19363                          "}";
19364   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
19365   tooling::Replacements Replaces = toReplacements(
19366       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
19367                             "#include \"b.h\"\n")});
19368 
19369   format::FormatStyle Style = format::getLLVMStyle();
19370   Style.SortIncludes = FormatStyle::SI_CaseSensitive;
19371   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
19372   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
19373       << llvm::toString(FormattedReplaces.takeError()) << "\n";
19374   auto Result = applyAllReplacements(Code, *FormattedReplaces);
19375   EXPECT_TRUE(static_cast<bool>(Result));
19376   EXPECT_EQ(Expected, *Result);
19377 }
19378 
19379 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
19380   EXPECT_EQ("using std::cin;\n"
19381             "using std::cout;",
19382             format("using std::cout;\n"
19383                    "using std::cin;",
19384                    getGoogleStyle()));
19385 }
19386 
19387 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
19388   format::FormatStyle Style = format::getLLVMStyle();
19389   Style.Standard = FormatStyle::LS_Cpp03;
19390   // cpp03 recognize this string as identifier u8 and literal character 'a'
19391   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
19392 }
19393 
19394 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
19395   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
19396   // all modes, including C++11, C++14 and C++17
19397   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
19398 }
19399 
19400 TEST_F(FormatTest, DoNotFormatLikelyXml) {
19401   EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
19402   EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
19403 }
19404 
19405 TEST_F(FormatTest, StructuredBindings) {
19406   // Structured bindings is a C++17 feature.
19407   // all modes, including C++11, C++14 and C++17
19408   verifyFormat("auto [a, b] = f();");
19409   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
19410   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
19411   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
19412   EXPECT_EQ("auto const volatile [a, b] = f();",
19413             format("auto  const   volatile[a, b] = f();"));
19414   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
19415   EXPECT_EQ("auto &[a, b, c] = f();",
19416             format("auto   &[  a  ,  b,c   ] = f();"));
19417   EXPECT_EQ("auto &&[a, b, c] = f();",
19418             format("auto   &&[  a  ,  b,c   ] = f();"));
19419   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
19420   EXPECT_EQ("auto const volatile &&[a, b] = f();",
19421             format("auto  const  volatile  &&[a, b] = f();"));
19422   EXPECT_EQ("auto const &&[a, b] = f();",
19423             format("auto  const   &&  [a, b] = f();"));
19424   EXPECT_EQ("const auto &[a, b] = f();",
19425             format("const  auto  &  [a, b] = f();"));
19426   EXPECT_EQ("const auto volatile &&[a, b] = f();",
19427             format("const  auto   volatile  &&[a, b] = f();"));
19428   EXPECT_EQ("volatile const auto &&[a, b] = f();",
19429             format("volatile  const  auto   &&[a, b] = f();"));
19430   EXPECT_EQ("const auto &&[a, b] = f();",
19431             format("const  auto  &&  [a, b] = f();"));
19432 
19433   // Make sure we don't mistake structured bindings for lambdas.
19434   FormatStyle PointerMiddle = getLLVMStyle();
19435   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
19436   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
19437   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
19438   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
19439   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
19440   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
19441   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
19442   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
19443   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
19444   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
19445   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
19446   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
19447   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
19448 
19449   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
19450             format("for (const auto   &&   [a, b] : some_range) {\n}"));
19451   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
19452             format("for (const auto   &   [a, b] : some_range) {\n}"));
19453   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
19454             format("for (const auto[a, b] : some_range) {\n}"));
19455   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
19456   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
19457   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
19458   EXPECT_EQ("auto const &[x, y](expr);",
19459             format("auto  const  &  [x,y]  (expr);"));
19460   EXPECT_EQ("auto const &&[x, y](expr);",
19461             format("auto  const  &&  [x,y]  (expr);"));
19462   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
19463   EXPECT_EQ("auto const &[x, y]{expr};",
19464             format("auto  const  &  [x,y]  {expr};"));
19465   EXPECT_EQ("auto const &&[x, y]{expr};",
19466             format("auto  const  &&  [x,y]  {expr};"));
19467 
19468   format::FormatStyle Spaces = format::getLLVMStyle();
19469   Spaces.SpacesInSquareBrackets = true;
19470   verifyFormat("auto [ a, b ] = f();", Spaces);
19471   verifyFormat("auto &&[ a, b ] = f();", Spaces);
19472   verifyFormat("auto &[ a, b ] = f();", Spaces);
19473   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
19474   verifyFormat("auto const &[ a, b ] = f();", Spaces);
19475 }
19476 
19477 TEST_F(FormatTest, FileAndCode) {
19478   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
19479   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
19480   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
19481   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
19482   EXPECT_EQ(FormatStyle::LK_ObjC,
19483             guessLanguage("foo.h", "@interface Foo\n@end\n"));
19484   EXPECT_EQ(
19485       FormatStyle::LK_ObjC,
19486       guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
19487   EXPECT_EQ(FormatStyle::LK_ObjC,
19488             guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
19489   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
19490   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
19491   EXPECT_EQ(FormatStyle::LK_ObjC,
19492             guessLanguage("foo", "@interface Foo\n@end\n"));
19493   EXPECT_EQ(FormatStyle::LK_ObjC,
19494             guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
19495   EXPECT_EQ(
19496       FormatStyle::LK_ObjC,
19497       guessLanguage("foo.h",
19498                     "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
19499   EXPECT_EQ(
19500       FormatStyle::LK_Cpp,
19501       guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
19502 }
19503 
19504 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
19505   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
19506   EXPECT_EQ(FormatStyle::LK_ObjC,
19507             guessLanguage("foo.h", "array[[calculator getIndex]];"));
19508   EXPECT_EQ(FormatStyle::LK_Cpp,
19509             guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
19510   EXPECT_EQ(
19511       FormatStyle::LK_Cpp,
19512       guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
19513   EXPECT_EQ(FormatStyle::LK_ObjC,
19514             guessLanguage("foo.h", "[[noreturn foo] bar];"));
19515   EXPECT_EQ(FormatStyle::LK_Cpp,
19516             guessLanguage("foo.h", "[[clang::fallthrough]];"));
19517   EXPECT_EQ(FormatStyle::LK_ObjC,
19518             guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
19519   EXPECT_EQ(FormatStyle::LK_Cpp,
19520             guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
19521   EXPECT_EQ(FormatStyle::LK_Cpp,
19522             guessLanguage("foo.h", "[[using clang: fallthrough]];"));
19523   EXPECT_EQ(FormatStyle::LK_ObjC,
19524             guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
19525   EXPECT_EQ(FormatStyle::LK_Cpp,
19526             guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
19527   EXPECT_EQ(
19528       FormatStyle::LK_Cpp,
19529       guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
19530   EXPECT_EQ(
19531       FormatStyle::LK_Cpp,
19532       guessLanguage("foo.h",
19533                     "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
19534   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
19535 }
19536 
19537 TEST_F(FormatTest, GuessLanguageWithCaret) {
19538   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
19539   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
19540   EXPECT_EQ(FormatStyle::LK_ObjC,
19541             guessLanguage("foo.h", "int(^)(char, float);"));
19542   EXPECT_EQ(FormatStyle::LK_ObjC,
19543             guessLanguage("foo.h", "int(^foo)(char, float);"));
19544   EXPECT_EQ(FormatStyle::LK_ObjC,
19545             guessLanguage("foo.h", "int(^foo[10])(char, float);"));
19546   EXPECT_EQ(FormatStyle::LK_ObjC,
19547             guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
19548   EXPECT_EQ(
19549       FormatStyle::LK_ObjC,
19550       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
19551 }
19552 
19553 TEST_F(FormatTest, GuessLanguageWithPragmas) {
19554   EXPECT_EQ(FormatStyle::LK_Cpp,
19555             guessLanguage("foo.h", "__pragma(warning(disable:))"));
19556   EXPECT_EQ(FormatStyle::LK_Cpp,
19557             guessLanguage("foo.h", "#pragma(warning(disable:))"));
19558   EXPECT_EQ(FormatStyle::LK_Cpp,
19559             guessLanguage("foo.h", "_Pragma(warning(disable:))"));
19560 }
19561 
19562 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
19563   // ASM symbolic names are identifiers that must be surrounded by [] without
19564   // space in between:
19565   // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
19566 
19567   // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
19568   verifyFormat(R"(//
19569 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
19570 )");
19571 
19572   // A list of several ASM symbolic names.
19573   verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
19574 
19575   // ASM symbolic names in inline ASM with inputs and outputs.
19576   verifyFormat(R"(//
19577 asm("cmoveq %1, %2, %[result]"
19578     : [result] "=r"(result)
19579     : "r"(test), "r"(new), "[result]"(old));
19580 )");
19581 
19582   // ASM symbolic names in inline ASM with no outputs.
19583   verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
19584 }
19585 
19586 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
19587   EXPECT_EQ(FormatStyle::LK_Cpp,
19588             guessLanguage("foo.h", "void f() {\n"
19589                                    "  asm (\"mov %[e], %[d]\"\n"
19590                                    "     : [d] \"=rm\" (d)\n"
19591                                    "       [e] \"rm\" (*e));\n"
19592                                    "}"));
19593   EXPECT_EQ(FormatStyle::LK_Cpp,
19594             guessLanguage("foo.h", "void f() {\n"
19595                                    "  _asm (\"mov %[e], %[d]\"\n"
19596                                    "     : [d] \"=rm\" (d)\n"
19597                                    "       [e] \"rm\" (*e));\n"
19598                                    "}"));
19599   EXPECT_EQ(FormatStyle::LK_Cpp,
19600             guessLanguage("foo.h", "void f() {\n"
19601                                    "  __asm (\"mov %[e], %[d]\"\n"
19602                                    "     : [d] \"=rm\" (d)\n"
19603                                    "       [e] \"rm\" (*e));\n"
19604                                    "}"));
19605   EXPECT_EQ(FormatStyle::LK_Cpp,
19606             guessLanguage("foo.h", "void f() {\n"
19607                                    "  __asm__ (\"mov %[e], %[d]\"\n"
19608                                    "     : [d] \"=rm\" (d)\n"
19609                                    "       [e] \"rm\" (*e));\n"
19610                                    "}"));
19611   EXPECT_EQ(FormatStyle::LK_Cpp,
19612             guessLanguage("foo.h", "void f() {\n"
19613                                    "  asm (\"mov %[e], %[d]\"\n"
19614                                    "     : [d] \"=rm\" (d),\n"
19615                                    "       [e] \"rm\" (*e));\n"
19616                                    "}"));
19617   EXPECT_EQ(FormatStyle::LK_Cpp,
19618             guessLanguage("foo.h", "void f() {\n"
19619                                    "  asm volatile (\"mov %[e], %[d]\"\n"
19620                                    "     : [d] \"=rm\" (d)\n"
19621                                    "       [e] \"rm\" (*e));\n"
19622                                    "}"));
19623 }
19624 
19625 TEST_F(FormatTest, GuessLanguageWithChildLines) {
19626   EXPECT_EQ(FormatStyle::LK_Cpp,
19627             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
19628   EXPECT_EQ(FormatStyle::LK_ObjC,
19629             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
19630   EXPECT_EQ(
19631       FormatStyle::LK_Cpp,
19632       guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
19633   EXPECT_EQ(
19634       FormatStyle::LK_ObjC,
19635       guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
19636 }
19637 
19638 TEST_F(FormatTest, TypenameMacros) {
19639   std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
19640 
19641   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
19642   FormatStyle Google = getGoogleStyleWithColumns(0);
19643   Google.TypenameMacros = TypenameMacros;
19644   verifyFormat("struct foo {\n"
19645                "  int bar;\n"
19646                "  TAILQ_ENTRY(a) bleh;\n"
19647                "};",
19648                Google);
19649 
19650   FormatStyle Macros = getLLVMStyle();
19651   Macros.TypenameMacros = TypenameMacros;
19652 
19653   verifyFormat("STACK_OF(int) a;", Macros);
19654   verifyFormat("STACK_OF(int) *a;", Macros);
19655   verifyFormat("STACK_OF(int const *) *a;", Macros);
19656   verifyFormat("STACK_OF(int *const) *a;", Macros);
19657   verifyFormat("STACK_OF(int, string) a;", Macros);
19658   verifyFormat("STACK_OF(LIST(int)) a;", Macros);
19659   verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
19660   verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
19661   verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
19662   verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
19663   verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
19664 
19665   Macros.PointerAlignment = FormatStyle::PAS_Left;
19666   verifyFormat("STACK_OF(int)* a;", Macros);
19667   verifyFormat("STACK_OF(int*)* a;", Macros);
19668   verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
19669   verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
19670   verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
19671 }
19672 
19673 TEST_F(FormatTest, AtomicQualifier) {
19674   // Check that we treate _Atomic as a type and not a function call
19675   FormatStyle Google = getGoogleStyleWithColumns(0);
19676   verifyFormat("struct foo {\n"
19677                "  int a1;\n"
19678                "  _Atomic(a) a2;\n"
19679                "  _Atomic(_Atomic(int) *const) a3;\n"
19680                "};",
19681                Google);
19682   verifyFormat("_Atomic(uint64_t) a;");
19683   verifyFormat("_Atomic(uint64_t) *a;");
19684   verifyFormat("_Atomic(uint64_t const *) *a;");
19685   verifyFormat("_Atomic(uint64_t *const) *a;");
19686   verifyFormat("_Atomic(const uint64_t *) *a;");
19687   verifyFormat("_Atomic(uint64_t) a;");
19688   verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
19689   verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
19690   verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
19691   verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
19692 
19693   verifyFormat("_Atomic(uint64_t) *s(InitValue);");
19694   verifyFormat("_Atomic(uint64_t) *s{InitValue};");
19695   FormatStyle Style = getLLVMStyle();
19696   Style.PointerAlignment = FormatStyle::PAS_Left;
19697   verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
19698   verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
19699   verifyFormat("_Atomic(int)* a;", Style);
19700   verifyFormat("_Atomic(int*)* a;", Style);
19701   verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
19702 
19703   Style.SpacesInCStyleCastParentheses = true;
19704   Style.SpacesInParentheses = false;
19705   verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
19706   Style.SpacesInCStyleCastParentheses = false;
19707   Style.SpacesInParentheses = true;
19708   verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
19709   verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
19710 }
19711 
19712 TEST_F(FormatTest, AmbersandInLamda) {
19713   // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
19714   FormatStyle AlignStyle = getLLVMStyle();
19715   AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
19716   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
19717   AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
19718   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
19719 }
19720 
19721 TEST_F(FormatTest, SpacesInConditionalStatement) {
19722   FormatStyle Spaces = getLLVMStyle();
19723   Spaces.SpacesInConditionalStatement = true;
19724   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);
19725   verifyFormat("if ( !a )\n  return;", Spaces);
19726   verifyFormat("if ( a )\n  return;", Spaces);
19727   verifyFormat("if constexpr ( a )\n  return;", Spaces);
19728   verifyFormat("switch ( a )\ncase 1:\n  return;", Spaces);
19729   verifyFormat("while ( a )\n  return;", Spaces);
19730   verifyFormat("while ( (a && b) )\n  return;", Spaces);
19731   verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
19732   verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
19733   // Check that space on the left of "::" is inserted as expected at beginning
19734   // of condition.
19735   verifyFormat("while ( ::func() )\n  return;", Spaces);
19736 }
19737 
19738 TEST_F(FormatTest, AlternativeOperators) {
19739   // Test case for ensuring alternate operators are not
19740   // combined with their right most neighbour.
19741   verifyFormat("int a and b;");
19742   verifyFormat("int a and_eq b;");
19743   verifyFormat("int a bitand b;");
19744   verifyFormat("int a bitor b;");
19745   verifyFormat("int a compl b;");
19746   verifyFormat("int a not b;");
19747   verifyFormat("int a not_eq b;");
19748   verifyFormat("int a or b;");
19749   verifyFormat("int a xor b;");
19750   verifyFormat("int a xor_eq b;");
19751   verifyFormat("return this not_eq bitand other;");
19752   verifyFormat("bool operator not_eq(const X bitand other)");
19753 
19754   verifyFormat("int a and 5;");
19755   verifyFormat("int a and_eq 5;");
19756   verifyFormat("int a bitand 5;");
19757   verifyFormat("int a bitor 5;");
19758   verifyFormat("int a compl 5;");
19759   verifyFormat("int a not 5;");
19760   verifyFormat("int a not_eq 5;");
19761   verifyFormat("int a or 5;");
19762   verifyFormat("int a xor 5;");
19763   verifyFormat("int a xor_eq 5;");
19764 
19765   verifyFormat("int a compl(5);");
19766   verifyFormat("int a not(5);");
19767 
19768   /* FIXME handle alternate tokens
19769    * https://en.cppreference.com/w/cpp/language/operator_alternative
19770   // alternative tokens
19771   verifyFormat("compl foo();");     //  ~foo();
19772   verifyFormat("foo() <%%>;");      // foo();
19773   verifyFormat("void foo() <%%>;"); // void foo(){}
19774   verifyFormat("int a <:1:>;");     // int a[1];[
19775   verifyFormat("%:define ABC abc"); // #define ABC abc
19776   verifyFormat("%:%:");             // ##
19777   */
19778 }
19779 
19780 TEST_F(FormatTest, STLWhileNotDefineChed) {
19781   verifyFormat("#if defined(while)\n"
19782                "#define while EMIT WARNING C4005\n"
19783                "#endif // while");
19784 }
19785 
19786 TEST_F(FormatTest, OperatorSpacing) {
19787   FormatStyle Style = getLLVMStyle();
19788   Style.PointerAlignment = FormatStyle::PAS_Right;
19789   verifyFormat("Foo::operator*();", Style);
19790   verifyFormat("Foo::operator void *();", Style);
19791   verifyFormat("Foo::operator void **();", Style);
19792   verifyFormat("Foo::operator void *&();", Style);
19793   verifyFormat("Foo::operator void *&&();", Style);
19794   verifyFormat("Foo::operator void const *();", Style);
19795   verifyFormat("Foo::operator void const **();", Style);
19796   verifyFormat("Foo::operator void const *&();", Style);
19797   verifyFormat("Foo::operator void const *&&();", Style);
19798   verifyFormat("Foo::operator()(void *);", Style);
19799   verifyFormat("Foo::operator*(void *);", Style);
19800   verifyFormat("Foo::operator*();", Style);
19801   verifyFormat("Foo::operator**();", Style);
19802   verifyFormat("Foo::operator&();", Style);
19803   verifyFormat("Foo::operator<int> *();", Style);
19804   verifyFormat("Foo::operator<Foo> *();", Style);
19805   verifyFormat("Foo::operator<int> **();", Style);
19806   verifyFormat("Foo::operator<Foo> **();", Style);
19807   verifyFormat("Foo::operator<int> &();", Style);
19808   verifyFormat("Foo::operator<Foo> &();", Style);
19809   verifyFormat("Foo::operator<int> &&();", Style);
19810   verifyFormat("Foo::operator<Foo> &&();", Style);
19811   verifyFormat("Foo::operator<int> *&();", Style);
19812   verifyFormat("Foo::operator<Foo> *&();", Style);
19813   verifyFormat("Foo::operator<int> *&&();", Style);
19814   verifyFormat("Foo::operator<Foo> *&&();", Style);
19815   verifyFormat("operator*(int (*)(), class Foo);", Style);
19816 
19817   verifyFormat("Foo::operator&();", Style);
19818   verifyFormat("Foo::operator void &();", Style);
19819   verifyFormat("Foo::operator void const &();", Style);
19820   verifyFormat("Foo::operator()(void &);", Style);
19821   verifyFormat("Foo::operator&(void &);", Style);
19822   verifyFormat("Foo::operator&();", Style);
19823   verifyFormat("operator&(int (&)(), class Foo);", Style);
19824 
19825   verifyFormat("Foo::operator&&();", Style);
19826   verifyFormat("Foo::operator**();", Style);
19827   verifyFormat("Foo::operator void &&();", Style);
19828   verifyFormat("Foo::operator void const &&();", Style);
19829   verifyFormat("Foo::operator()(void &&);", Style);
19830   verifyFormat("Foo::operator&&(void &&);", Style);
19831   verifyFormat("Foo::operator&&();", Style);
19832   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
19833   verifyFormat("operator const nsTArrayRight<E> &()", Style);
19834   verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
19835                Style);
19836   verifyFormat("operator void **()", Style);
19837   verifyFormat("operator const FooRight<Object> &()", Style);
19838   verifyFormat("operator const FooRight<Object> *()", Style);
19839   verifyFormat("operator const FooRight<Object> **()", Style);
19840   verifyFormat("operator const FooRight<Object> *&()", Style);
19841   verifyFormat("operator const FooRight<Object> *&&()", Style);
19842 
19843   Style.PointerAlignment = FormatStyle::PAS_Left;
19844   verifyFormat("Foo::operator*();", Style);
19845   verifyFormat("Foo::operator**();", Style);
19846   verifyFormat("Foo::operator void*();", Style);
19847   verifyFormat("Foo::operator void**();", Style);
19848   verifyFormat("Foo::operator void*&();", Style);
19849   verifyFormat("Foo::operator void*&&();", Style);
19850   verifyFormat("Foo::operator void const*();", Style);
19851   verifyFormat("Foo::operator void const**();", Style);
19852   verifyFormat("Foo::operator void const*&();", Style);
19853   verifyFormat("Foo::operator void const*&&();", Style);
19854   verifyFormat("Foo::operator/*comment*/ void*();", Style);
19855   verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
19856   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
19857   verifyFormat("Foo::operator()(void*);", Style);
19858   verifyFormat("Foo::operator*(void*);", Style);
19859   verifyFormat("Foo::operator*();", Style);
19860   verifyFormat("Foo::operator<int>*();", Style);
19861   verifyFormat("Foo::operator<Foo>*();", Style);
19862   verifyFormat("Foo::operator<int>**();", Style);
19863   verifyFormat("Foo::operator<Foo>**();", Style);
19864   verifyFormat("Foo::operator<Foo>*&();", Style);
19865   verifyFormat("Foo::operator<int>&();", Style);
19866   verifyFormat("Foo::operator<Foo>&();", Style);
19867   verifyFormat("Foo::operator<int>&&();", Style);
19868   verifyFormat("Foo::operator<Foo>&&();", Style);
19869   verifyFormat("Foo::operator<int>*&();", Style);
19870   verifyFormat("Foo::operator<Foo>*&();", Style);
19871   verifyFormat("operator*(int (*)(), class Foo);", Style);
19872 
19873   verifyFormat("Foo::operator&();", Style);
19874   verifyFormat("Foo::operator void&();", Style);
19875   verifyFormat("Foo::operator void const&();", Style);
19876   verifyFormat("Foo::operator/*comment*/ void&();", Style);
19877   verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
19878   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
19879   verifyFormat("Foo::operator()(void&);", Style);
19880   verifyFormat("Foo::operator&(void&);", Style);
19881   verifyFormat("Foo::operator&();", Style);
19882   verifyFormat("operator&(int (&)(), class Foo);", Style);
19883 
19884   verifyFormat("Foo::operator&&();", Style);
19885   verifyFormat("Foo::operator void&&();", Style);
19886   verifyFormat("Foo::operator void const&&();", Style);
19887   verifyFormat("Foo::operator/*comment*/ void&&();", Style);
19888   verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
19889   verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
19890   verifyFormat("Foo::operator()(void&&);", Style);
19891   verifyFormat("Foo::operator&&(void&&);", Style);
19892   verifyFormat("Foo::operator&&();", Style);
19893   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
19894   verifyFormat("operator const nsTArrayLeft<E>&()", Style);
19895   verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
19896                Style);
19897   verifyFormat("operator void**()", Style);
19898   verifyFormat("operator const FooLeft<Object>&()", Style);
19899   verifyFormat("operator const FooLeft<Object>*()", Style);
19900   verifyFormat("operator const FooLeft<Object>**()", Style);
19901   verifyFormat("operator const FooLeft<Object>*&()", Style);
19902   verifyFormat("operator const FooLeft<Object>*&&()", Style);
19903 
19904   // PR45107
19905   verifyFormat("operator Vector<String>&();", Style);
19906   verifyFormat("operator const Vector<String>&();", Style);
19907   verifyFormat("operator foo::Bar*();", Style);
19908   verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
19909   verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
19910                Style);
19911 
19912   Style.PointerAlignment = FormatStyle::PAS_Middle;
19913   verifyFormat("Foo::operator*();", Style);
19914   verifyFormat("Foo::operator void *();", Style);
19915   verifyFormat("Foo::operator()(void *);", Style);
19916   verifyFormat("Foo::operator*(void *);", Style);
19917   verifyFormat("Foo::operator*();", Style);
19918   verifyFormat("operator*(int (*)(), class Foo);", Style);
19919 
19920   verifyFormat("Foo::operator&();", Style);
19921   verifyFormat("Foo::operator void &();", Style);
19922   verifyFormat("Foo::operator void const &();", Style);
19923   verifyFormat("Foo::operator()(void &);", Style);
19924   verifyFormat("Foo::operator&(void &);", Style);
19925   verifyFormat("Foo::operator&();", Style);
19926   verifyFormat("operator&(int (&)(), class Foo);", Style);
19927 
19928   verifyFormat("Foo::operator&&();", Style);
19929   verifyFormat("Foo::operator void &&();", Style);
19930   verifyFormat("Foo::operator void const &&();", Style);
19931   verifyFormat("Foo::operator()(void &&);", Style);
19932   verifyFormat("Foo::operator&&(void &&);", Style);
19933   verifyFormat("Foo::operator&&();", Style);
19934   verifyFormat("operator&&(int(&&)(), class Foo);", Style);
19935 }
19936 
19937 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
19938   FormatStyle Style = getLLVMStyle();
19939   // PR46157
19940   verifyFormat("foo(operator+, -42);", Style);
19941   verifyFormat("foo(operator++, -42);", Style);
19942   verifyFormat("foo(operator--, -42);", Style);
19943   verifyFormat("foo(-42, operator--);", Style);
19944   verifyFormat("foo(-42, operator, );", Style);
19945   verifyFormat("foo(operator, , -42);", Style);
19946 }
19947 
19948 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
19949   FormatStyle Style = getLLVMStyle();
19950   Style.WhitespaceSensitiveMacros.push_back("FOO");
19951 
19952   // Don't use the helpers here, since 'mess up' will change the whitespace
19953   // and these are all whitespace sensitive by definition
19954   EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
19955             format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
19956   EXPECT_EQ(
19957       "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
19958       format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
19959   EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
19960             format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
19961   EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
19962             "       Still=Intentional);",
19963             format("FOO(String-ized&Messy+But,: :\n"
19964                    "       Still=Intentional);",
19965                    Style));
19966   Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
19967   EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
19968             "       Still=Intentional);",
19969             format("FOO(String-ized=&Messy+But,: :\n"
19970                    "       Still=Intentional);",
19971                    Style));
19972 
19973   Style.ColumnLimit = 21;
19974   EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
19975             format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
19976 }
19977 
19978 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
19979   // These tests are not in NamespaceFixer because that doesn't
19980   // test its interaction with line wrapping
19981   FormatStyle Style = getLLVMStyle();
19982   Style.ColumnLimit = 80;
19983   verifyFormat("namespace {\n"
19984                "int i;\n"
19985                "int j;\n"
19986                "} // namespace",
19987                Style);
19988 
19989   verifyFormat("namespace AAA {\n"
19990                "int i;\n"
19991                "int j;\n"
19992                "} // namespace AAA",
19993                Style);
19994 
19995   EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
19996             "int i;\n"
19997             "int j;\n"
19998             "} // namespace Averyveryveryverylongnamespace",
19999             format("namespace Averyveryveryverylongnamespace {\n"
20000                    "int i;\n"
20001                    "int j;\n"
20002                    "}",
20003                    Style));
20004 
20005   EXPECT_EQ(
20006       "namespace "
20007       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
20008       "    went::mad::now {\n"
20009       "int i;\n"
20010       "int j;\n"
20011       "} // namespace\n"
20012       "  // "
20013       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
20014       "went::mad::now",
20015       format("namespace "
20016              "would::it::save::you::a::lot::of::time::if_::i::"
20017              "just::gave::up::and_::went::mad::now {\n"
20018              "int i;\n"
20019              "int j;\n"
20020              "}",
20021              Style));
20022 
20023   // This used to duplicate the comment again and again on subsequent runs
20024   EXPECT_EQ(
20025       "namespace "
20026       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
20027       "    went::mad::now {\n"
20028       "int i;\n"
20029       "int j;\n"
20030       "} // namespace\n"
20031       "  // "
20032       "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
20033       "went::mad::now",
20034       format("namespace "
20035              "would::it::save::you::a::lot::of::time::if_::i::"
20036              "just::gave::up::and_::went::mad::now {\n"
20037              "int i;\n"
20038              "int j;\n"
20039              "} // namespace\n"
20040              "  // "
20041              "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
20042              "and_::went::mad::now",
20043              Style));
20044 }
20045 
20046 TEST_F(FormatTest, LikelyUnlikely) {
20047   FormatStyle Style = getLLVMStyle();
20048 
20049   verifyFormat("if (argc > 5) [[unlikely]] {\n"
20050                "  return 29;\n"
20051                "}",
20052                Style);
20053 
20054   verifyFormat("if (argc > 5) [[likely]] {\n"
20055                "  return 29;\n"
20056                "}",
20057                Style);
20058 
20059   verifyFormat("if (argc > 5) [[unlikely]] {\n"
20060                "  return 29;\n"
20061                "} else [[likely]] {\n"
20062                "  return 42;\n"
20063                "}\n",
20064                Style);
20065 
20066   verifyFormat("if (argc > 5) [[unlikely]] {\n"
20067                "  return 29;\n"
20068                "} else if (argc > 10) [[likely]] {\n"
20069                "  return 99;\n"
20070                "} else {\n"
20071                "  return 42;\n"
20072                "}\n",
20073                Style);
20074 
20075   verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
20076                "  return 29;\n"
20077                "}",
20078                Style);
20079 }
20080 
20081 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
20082   verifyFormat("Constructor()\n"
20083                "    : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20084                "                          aaaa(aaaaaaaaaaaaaaaaaa, "
20085                "aaaaaaaaaaaaaaaaaat))");
20086   verifyFormat("Constructor()\n"
20087                "    : aaaaaaaaaaaaa(aaaaaa), "
20088                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
20089 
20090   FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
20091   StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
20092   verifyFormat("Constructor()\n"
20093                "    : aaaaaa(aaaaaa),\n"
20094                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
20095                "          aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
20096                StyleWithWhitespacePenalty);
20097   verifyFormat("Constructor()\n"
20098                "    : aaaaaaaaaaaaa(aaaaaa), "
20099                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
20100                StyleWithWhitespacePenalty);
20101 }
20102 
20103 TEST_F(FormatTest, LLVMDefaultStyle) {
20104   FormatStyle Style = getLLVMStyle();
20105   verifyFormat("extern \"C\" {\n"
20106                "int foo();\n"
20107                "}",
20108                Style);
20109 }
20110 TEST_F(FormatTest, GNUDefaultStyle) {
20111   FormatStyle Style = getGNUStyle();
20112   verifyFormat("extern \"C\"\n"
20113                "{\n"
20114                "  int foo ();\n"
20115                "}",
20116                Style);
20117 }
20118 TEST_F(FormatTest, MozillaDefaultStyle) {
20119   FormatStyle Style = getMozillaStyle();
20120   verifyFormat("extern \"C\"\n"
20121                "{\n"
20122                "  int foo();\n"
20123                "}",
20124                Style);
20125 }
20126 TEST_F(FormatTest, GoogleDefaultStyle) {
20127   FormatStyle Style = getGoogleStyle();
20128   verifyFormat("extern \"C\" {\n"
20129                "int foo();\n"
20130                "}",
20131                Style);
20132 }
20133 TEST_F(FormatTest, ChromiumDefaultStyle) {
20134   FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
20135   verifyFormat("extern \"C\" {\n"
20136                "int foo();\n"
20137                "}",
20138                Style);
20139 }
20140 TEST_F(FormatTest, MicrosoftDefaultStyle) {
20141   FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
20142   verifyFormat("extern \"C\"\n"
20143                "{\n"
20144                "    int foo();\n"
20145                "}",
20146                Style);
20147 }
20148 TEST_F(FormatTest, WebKitDefaultStyle) {
20149   FormatStyle Style = getWebKitStyle();
20150   verifyFormat("extern \"C\" {\n"
20151                "int foo();\n"
20152                "}",
20153                Style);
20154 }
20155 
20156 TEST_F(FormatTest, ConceptsAndRequires) {
20157   FormatStyle Style = getLLVMStyle();
20158   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
20159 
20160   verifyFormat("template <typename T>\n"
20161                "concept Hashable = requires(T a) {\n"
20162                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
20163                "};",
20164                Style);
20165   verifyFormat("template <typename T>\n"
20166                "concept EqualityComparable = requires(T a, T b) {\n"
20167                "  { a == b } -> bool;\n"
20168                "};",
20169                Style);
20170   verifyFormat("template <typename T>\n"
20171                "concept EqualityComparable = requires(T a, T b) {\n"
20172                "  { a == b } -> bool;\n"
20173                "  { a != b } -> bool;\n"
20174                "};",
20175                Style);
20176   verifyFormat("template <typename T>\n"
20177                "concept EqualityComparable = requires(T a, T b) {\n"
20178                "  { a == b } -> bool;\n"
20179                "  { a != b } -> bool;\n"
20180                "};",
20181                Style);
20182 
20183   verifyFormat("template <typename It>\n"
20184                "requires Iterator<It>\n"
20185                "void sort(It begin, It end) {\n"
20186                "  //....\n"
20187                "}",
20188                Style);
20189 
20190   verifyFormat("template <typename T>\n"
20191                "concept Large = sizeof(T) > 10;",
20192                Style);
20193 
20194   verifyFormat("template <typename T, typename U>\n"
20195                "concept FooableWith = requires(T t, U u) {\n"
20196                "  typename T::foo_type;\n"
20197                "  { t.foo(u) } -> typename T::foo_type;\n"
20198                "  t++;\n"
20199                "};\n"
20200                "void doFoo(FooableWith<int> auto t) {\n"
20201                "  t.foo(3);\n"
20202                "}",
20203                Style);
20204   verifyFormat("template <typename T>\n"
20205                "concept Context = sizeof(T) == 1;",
20206                Style);
20207   verifyFormat("template <typename T>\n"
20208                "concept Context = is_specialization_of_v<context, T>;",
20209                Style);
20210   verifyFormat("template <typename T>\n"
20211                "concept Node = std::is_object_v<T>;",
20212                Style);
20213   verifyFormat("template <typename T>\n"
20214                "concept Tree = true;",
20215                Style);
20216 
20217   verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n"
20218                "  //...\n"
20219                "}",
20220                Style);
20221 
20222   verifyFormat(
20223       "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n"
20224       "  //...\n"
20225       "}",
20226       Style);
20227 
20228   verifyFormat(
20229       "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n"
20230       "  //...\n"
20231       "}",
20232       Style);
20233 
20234   verifyFormat("template <typename T>\n"
20235                "veryveryvery_long_return_type g(T i) requires Concept1<I> || "
20236                "Concept2<I> {\n"
20237                "  //...\n"
20238                "}",
20239                Style);
20240 
20241   verifyFormat("template <typename T>\n"
20242                "veryveryvery_long_return_type g(T i) requires Concept1<I> && "
20243                "Concept2<I> {\n"
20244                "  //...\n"
20245                "}",
20246                Style);
20247 
20248   verifyFormat(
20249       "template <typename T>\n"
20250       "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n"
20251       "  //...\n"
20252       "}",
20253       Style);
20254 
20255   verifyFormat(
20256       "template <typename T>\n"
20257       "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n"
20258       "  //...\n"
20259       "}",
20260       Style);
20261 
20262   verifyFormat("template <typename It>\n"
20263                "requires Foo<It>() && Bar<It> {\n"
20264                "  //....\n"
20265                "}",
20266                Style);
20267 
20268   verifyFormat("template <typename It>\n"
20269                "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n"
20270                "  //....\n"
20271                "}",
20272                Style);
20273 
20274   verifyFormat("template <typename It>\n"
20275                "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n"
20276                "  //....\n"
20277                "}",
20278                Style);
20279 
20280   verifyFormat(
20281       "template <typename It>\n"
20282       "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n"
20283       "  //....\n"
20284       "}",
20285       Style);
20286 
20287   Style.IndentRequires = true;
20288   verifyFormat("template <typename It>\n"
20289                "  requires Iterator<It>\n"
20290                "void sort(It begin, It end) {\n"
20291                "  //....\n"
20292                "}",
20293                Style);
20294   verifyFormat("template <std::size index_>\n"
20295                "  requires(index_ < sizeof...(Children_))\n"
20296                "Tree auto &child() {\n"
20297                "  // ...\n"
20298                "}",
20299                Style);
20300 
20301   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
20302   verifyFormat("template <typename T>\n"
20303                "concept Hashable = requires (T a) {\n"
20304                "  { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n"
20305                "};",
20306                Style);
20307 
20308   verifyFormat("template <class T = void>\n"
20309                "  requires EqualityComparable<T> || Same<T, void>\n"
20310                "struct equal_to;",
20311                Style);
20312 
20313   verifyFormat("template <class T>\n"
20314                "  requires requires {\n"
20315                "    T{};\n"
20316                "    T (int);\n"
20317                "  }\n",
20318                Style);
20319 
20320   Style.ColumnLimit = 78;
20321   verifyFormat("template <typename T>\n"
20322                "concept Context = Traits<typename T::traits_type> and\n"
20323                "    Interface<typename T::interface_type> and\n"
20324                "    Request<typename T::request_type> and\n"
20325                "    Response<typename T::response_type> and\n"
20326                "    ContextExtension<typename T::extension_type> and\n"
20327                "    ::std::is_copy_constructable<T> and "
20328                "::std::is_move_constructable<T> and\n"
20329                "    requires (T c) {\n"
20330                "  { c.response; } -> Response;\n"
20331                "} and requires (T c) {\n"
20332                "  { c.request; } -> Request;\n"
20333                "}\n",
20334                Style);
20335 
20336   verifyFormat("template <typename T>\n"
20337                "concept Context = Traits<typename T::traits_type> or\n"
20338                "    Interface<typename T::interface_type> or\n"
20339                "    Request<typename T::request_type> or\n"
20340                "    Response<typename T::response_type> or\n"
20341                "    ContextExtension<typename T::extension_type> or\n"
20342                "    ::std::is_copy_constructable<T> or "
20343                "::std::is_move_constructable<T> or\n"
20344                "    requires (T c) {\n"
20345                "  { c.response; } -> Response;\n"
20346                "} or requires (T c) {\n"
20347                "  { c.request; } -> Request;\n"
20348                "}\n",
20349                Style);
20350 
20351   verifyFormat("template <typename T>\n"
20352                "concept Context = Traits<typename T::traits_type> &&\n"
20353                "    Interface<typename T::interface_type> &&\n"
20354                "    Request<typename T::request_type> &&\n"
20355                "    Response<typename T::response_type> &&\n"
20356                "    ContextExtension<typename T::extension_type> &&\n"
20357                "    ::std::is_copy_constructable<T> && "
20358                "::std::is_move_constructable<T> &&\n"
20359                "    requires (T c) {\n"
20360                "  { c.response; } -> Response;\n"
20361                "} && requires (T c) {\n"
20362                "  { c.request; } -> Request;\n"
20363                "}\n",
20364                Style);
20365 
20366   verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && "
20367                "Constraint2<T>;");
20368 
20369   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
20370   Style.BraceWrapping.AfterFunction = true;
20371   Style.BraceWrapping.AfterClass = true;
20372   Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
20373   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
20374   verifyFormat("void Foo () requires (std::copyable<T>)\n"
20375                "{\n"
20376                "  return\n"
20377                "}\n",
20378                Style);
20379 
20380   verifyFormat("void Foo () requires std::copyable<T>\n"
20381                "{\n"
20382                "  return\n"
20383                "}\n",
20384                Style);
20385 
20386   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
20387                "  requires (std::invocable<F, std::invoke_result_t<Args>...>)\n"
20388                "struct constant;",
20389                Style);
20390 
20391   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
20392                "  requires std::invocable<F, std::invoke_result_t<Args>...>\n"
20393                "struct constant;",
20394                Style);
20395 
20396   verifyFormat("template <class T>\n"
20397                "class plane_with_very_very_very_long_name\n"
20398                "{\n"
20399                "  constexpr plane_with_very_very_very_long_name () requires "
20400                "std::copyable<T>\n"
20401                "      : plane_with_very_very_very_long_name (1)\n"
20402                "  {\n"
20403                "  }\n"
20404                "}\n",
20405                Style);
20406 
20407   verifyFormat("template <class T>\n"
20408                "class plane_with_long_name\n"
20409                "{\n"
20410                "  constexpr plane_with_long_name () requires std::copyable<T>\n"
20411                "      : plane_with_long_name (1)\n"
20412                "  {\n"
20413                "  }\n"
20414                "}\n",
20415                Style);
20416 
20417   Style.BreakBeforeConceptDeclarations = false;
20418   verifyFormat("template <typename T> concept Tree = true;", Style);
20419 
20420   Style.IndentRequires = false;
20421   verifyFormat("template <std::semiregular F, std::semiregular... Args>\n"
20422                "requires (std::invocable<F, std::invoke_result_t<Args>...>) "
20423                "struct constant;",
20424                Style);
20425 }
20426 
20427 TEST_F(FormatTest, StatementAttributeLikeMacros) {
20428   FormatStyle Style = getLLVMStyle();
20429   StringRef Source = "void Foo::slot() {\n"
20430                      "  unsigned char MyChar = 'x';\n"
20431                      "  emit signal(MyChar);\n"
20432                      "  Q_EMIT signal(MyChar);\n"
20433                      "}";
20434 
20435   EXPECT_EQ(Source, format(Source, Style));
20436 
20437   Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
20438   EXPECT_EQ("void Foo::slot() {\n"
20439             "  unsigned char MyChar = 'x';\n"
20440             "  emit          signal(MyChar);\n"
20441             "  Q_EMIT signal(MyChar);\n"
20442             "}",
20443             format(Source, Style));
20444 
20445   Style.StatementAttributeLikeMacros.push_back("emit");
20446   EXPECT_EQ(Source, format(Source, Style));
20447 
20448   Style.StatementAttributeLikeMacros = {};
20449   EXPECT_EQ("void Foo::slot() {\n"
20450             "  unsigned char MyChar = 'x';\n"
20451             "  emit          signal(MyChar);\n"
20452             "  Q_EMIT        signal(MyChar);\n"
20453             "}",
20454             format(Source, Style));
20455 }
20456 
20457 TEST_F(FormatTest, IndentAccessModifiers) {
20458   FormatStyle Style = getLLVMStyle();
20459   Style.IndentAccessModifiers = true;
20460   // Members are *two* levels below the record;
20461   // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
20462   verifyFormat("class C {\n"
20463                "    int i;\n"
20464                "};\n",
20465                Style);
20466   verifyFormat("union C {\n"
20467                "    int i;\n"
20468                "    unsigned u;\n"
20469                "};\n",
20470                Style);
20471   // Access modifiers should be indented one level below the record.
20472   verifyFormat("class C {\n"
20473                "  public:\n"
20474                "    int i;\n"
20475                "};\n",
20476                Style);
20477   verifyFormat("struct S {\n"
20478                "  private:\n"
20479                "    class C {\n"
20480                "        int j;\n"
20481                "\n"
20482                "      public:\n"
20483                "        C();\n"
20484                "    };\n"
20485                "\n"
20486                "  public:\n"
20487                "    int i;\n"
20488                "};\n",
20489                Style);
20490   // Enumerations are not records and should be unaffected.
20491   Style.AllowShortEnumsOnASingleLine = false;
20492   verifyFormat("enum class E\n"
20493                "{\n"
20494                "  A,\n"
20495                "  B\n"
20496                "};\n",
20497                Style);
20498   // Test with a different indentation width;
20499   // also proves that the result is Style.AccessModifierOffset agnostic.
20500   Style.IndentWidth = 3;
20501   verifyFormat("class C {\n"
20502                "   public:\n"
20503                "      int i;\n"
20504                "};\n",
20505                Style);
20506 }
20507 
20508 TEST_F(FormatTest, LimitlessStringsAndComments) {
20509   auto Style = getLLVMStyleWithColumns(0);
20510   constexpr StringRef Code =
20511       "/**\n"
20512       " * This is a multiline comment with quite some long lines, at least for "
20513       "the LLVM Style.\n"
20514       " * We will redo this with strings and line comments. Just to  check if "
20515       "everything is working.\n"
20516       " */\n"
20517       "bool foo() {\n"
20518       "  /* Single line multi line comment. */\n"
20519       "  const std::string String = \"This is a multiline string with quite "
20520       "some long lines, at least for the LLVM Style.\"\n"
20521       "                             \"We already did it with multi line "
20522       "comments, and we will do it with line comments. Just to check if "
20523       "everything is working.\";\n"
20524       "  // This is a line comment (block) with quite some long lines, at "
20525       "least for the LLVM Style.\n"
20526       "  // We already did this with multi line comments and strings. Just to "
20527       "check if everything is working.\n"
20528       "  const std::string SmallString = \"Hello World\";\n"
20529       "  // Small line comment\n"
20530       "  return String.size() > SmallString.size();\n"
20531       "}";
20532   EXPECT_EQ(Code, format(Code, Style));
20533 }
20534 } // namespace
20535 } // namespace format
20536 } // namespace clang
20537