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 "clang/Frontend/TextDiagnosticPrinter.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "gtest/gtest.h"
18
19 #define DEBUG_TYPE "format-test"
20
21 using clang::tooling::ReplacementTest;
22 using clang::tooling::toReplacements;
23
24 namespace clang {
25 namespace format {
26 namespace {
27
getGoogleStyle()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
format(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle (),StatusCheck CheckComplete=SC_ExpectComplete)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
getStyleWithColumns(FormatStyle Style,unsigned ColumnLimit)55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
56 Style.ColumnLimit = ColumnLimit;
57 return Style;
58 }
59
getLLVMStyleWithColumns(unsigned ColumnLimit)60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
62 }
63
getGoogleStyleWithColumns(unsigned ColumnLimit)64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
66 }
67
verifyFormat(llvm::StringRef Expected,llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())68 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
69 const FormatStyle &Style = getLLVMStyle()) {
70 EXPECT_EQ(Expected.str(), format(Expected, Style))
71 << "Expected code is not stable";
72 EXPECT_EQ(Expected.str(), format(Code, Style));
73 if (Style.Language == FormatStyle::LK_Cpp) {
74 // Objective-C++ is a superset of C++, so everything checked for C++
75 // needs to be checked for Objective-C++ as well.
76 FormatStyle ObjCStyle = Style;
77 ObjCStyle.Language = FormatStyle::LK_ObjC;
78 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle));
79 }
80 }
81
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())82 void verifyFormat(llvm::StringRef Code,
83 const FormatStyle &Style = getLLVMStyle()) {
84 verifyFormat(Code, test::messUp(Code), Style);
85 }
86
verifyIncompleteFormat(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())87 void verifyIncompleteFormat(llvm::StringRef Code,
88 const FormatStyle &Style = getLLVMStyle()) {
89 EXPECT_EQ(Code.str(),
90 format(test::messUp(Code), Style, SC_ExpectIncomplete));
91 }
92
verifyGoogleFormat(llvm::StringRef Code)93 void verifyGoogleFormat(llvm::StringRef Code) {
94 verifyFormat(Code, getGoogleStyle());
95 }
96
verifyIndependentOfContext(llvm::StringRef text)97 void verifyIndependentOfContext(llvm::StringRef text) {
98 verifyFormat(text);
99 verifyFormat(llvm::Twine("void f() { " + text + " }").str());
100 }
101
102 /// \brief Verify that clang-format does not crash on the given input.
verifyNoCrash(llvm::StringRef Code,const FormatStyle & Style=getLLVMStyle ())103 void verifyNoCrash(llvm::StringRef Code,
104 const FormatStyle &Style = getLLVMStyle()) {
105 format(Code, Style, SC_DoNotCheck);
106 }
107
108 int ReplacementCount;
109 };
110
TEST_F(FormatTest,MessUp)111 TEST_F(FormatTest, MessUp) {
112 EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
113 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
114 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
115 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
116 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
117 }
118
TEST_F(FormatTest,DefaultLLVMStyleIsCpp)119 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
120 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
121 }
122
TEST_F(FormatTest,LLVMStyleOverride)123 TEST_F(FormatTest, LLVMStyleOverride) {
124 EXPECT_EQ(FormatStyle::LK_Proto,
125 getLLVMStyle(FormatStyle::LK_Proto).Language);
126 }
127
128 //===----------------------------------------------------------------------===//
129 // Basic function tests.
130 //===----------------------------------------------------------------------===//
131
TEST_F(FormatTest,DoesNotChangeCorrectlyFormattedCode)132 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
133 EXPECT_EQ(";", format(";"));
134 }
135
TEST_F(FormatTest,FormatsGlobalStatementsAt0)136 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
137 EXPECT_EQ("int i;", format(" int i;"));
138 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
139 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
140 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
141 }
142
TEST_F(FormatTest,FormatsUnwrappedLinesAtFirstFormat)143 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
144 EXPECT_EQ("int i;", format("int\ni;"));
145 }
146
TEST_F(FormatTest,FormatsNestedBlockStatements)147 TEST_F(FormatTest, FormatsNestedBlockStatements) {
148 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
149 }
150
TEST_F(FormatTest,FormatsNestedCall)151 TEST_F(FormatTest, FormatsNestedCall) {
152 verifyFormat("Method(f1, f2(f3));");
153 verifyFormat("Method(f1(f2, f3()));");
154 verifyFormat("Method(f1(f2, (f3())));");
155 }
156
TEST_F(FormatTest,NestedNameSpecifiers)157 TEST_F(FormatTest, NestedNameSpecifiers) {
158 verifyFormat("vector<::Type> v;");
159 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
160 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
161 verifyFormat("bool a = 2 < ::SomeFunction();");
162 verifyFormat("ALWAYS_INLINE ::std::string getName();");
163 verifyFormat("some::string getName();");
164 }
165
TEST_F(FormatTest,OnlyGeneratesNecessaryReplacements)166 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
167 EXPECT_EQ("if (a) {\n"
168 " f();\n"
169 "}",
170 format("if(a){f();}"));
171 EXPECT_EQ(4, ReplacementCount);
172 EXPECT_EQ("if (a) {\n"
173 " f();\n"
174 "}",
175 format("if (a) {\n"
176 " f();\n"
177 "}"));
178 EXPECT_EQ(0, ReplacementCount);
179 EXPECT_EQ("/*\r\n"
180 "\r\n"
181 "*/\r\n",
182 format("/*\r\n"
183 "\r\n"
184 "*/\r\n"));
185 EXPECT_EQ(0, ReplacementCount);
186 }
187
TEST_F(FormatTest,RemovesEmptyLines)188 TEST_F(FormatTest, RemovesEmptyLines) {
189 EXPECT_EQ("class C {\n"
190 " int i;\n"
191 "};",
192 format("class C {\n"
193 " int i;\n"
194 "\n"
195 "};"));
196
197 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
198 EXPECT_EQ("namespace N {\n"
199 "\n"
200 "int i;\n"
201 "}",
202 format("namespace N {\n"
203 "\n"
204 "int i;\n"
205 "}",
206 getGoogleStyle()));
207 EXPECT_EQ("/* something */ namespace N {\n"
208 "\n"
209 "int i;\n"
210 "}",
211 format("/* something */ namespace N {\n"
212 "\n"
213 "int i;\n"
214 "}",
215 getGoogleStyle()));
216 EXPECT_EQ("inline namespace N {\n"
217 "\n"
218 "int i;\n"
219 "}",
220 format("inline namespace N {\n"
221 "\n"
222 "int i;\n"
223 "}",
224 getGoogleStyle()));
225 EXPECT_EQ("/* something */ inline namespace N {\n"
226 "\n"
227 "int i;\n"
228 "}",
229 format("/* something */ inline namespace N {\n"
230 "\n"
231 "int i;\n"
232 "}",
233 getGoogleStyle()));
234 EXPECT_EQ("export namespace N {\n"
235 "\n"
236 "int i;\n"
237 "}",
238 format("export namespace N {\n"
239 "\n"
240 "int i;\n"
241 "}",
242 getGoogleStyle()));
243 EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
244 "\n"
245 "int i;\n"
246 "}",
247 format("extern /**/ \"C\" /**/ {\n"
248 "\n"
249 "int i;\n"
250 "}",
251 getGoogleStyle()));
252
253 // ...but do keep inlining and removing empty lines for non-block extern "C"
254 // functions.
255 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
256 EXPECT_EQ("extern \"C\" int f() {\n"
257 " int i = 42;\n"
258 " return i;\n"
259 "}",
260 format("extern \"C\" int f() {\n"
261 "\n"
262 " int i = 42;\n"
263 " return i;\n"
264 "}",
265 getGoogleStyle()));
266
267 // Remove empty lines at the beginning and end of blocks.
268 EXPECT_EQ("void f() {\n"
269 "\n"
270 " if (a) {\n"
271 "\n"
272 " f();\n"
273 " }\n"
274 "}",
275 format("void f() {\n"
276 "\n"
277 " if (a) {\n"
278 "\n"
279 " f();\n"
280 "\n"
281 " }\n"
282 "\n"
283 "}",
284 getLLVMStyle()));
285 EXPECT_EQ("void f() {\n"
286 " if (a) {\n"
287 " f();\n"
288 " }\n"
289 "}",
290 format("void f() {\n"
291 "\n"
292 " if (a) {\n"
293 "\n"
294 " f();\n"
295 "\n"
296 " }\n"
297 "\n"
298 "}",
299 getGoogleStyle()));
300
301 // Don't remove empty lines in more complex control statements.
302 EXPECT_EQ("void f() {\n"
303 " if (a) {\n"
304 " f();\n"
305 "\n"
306 " } else if (b) {\n"
307 " f();\n"
308 " }\n"
309 "}",
310 format("void f() {\n"
311 " if (a) {\n"
312 " f();\n"
313 "\n"
314 " } else if (b) {\n"
315 " f();\n"
316 "\n"
317 " }\n"
318 "\n"
319 "}"));
320
321 // Don't remove empty lines before namespace endings.
322 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
323 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
324 EXPECT_EQ("namespace {\n"
325 "int i;\n"
326 "\n"
327 "}",
328 format("namespace {\n"
329 "int i;\n"
330 "\n"
331 "}",
332 LLVMWithNoNamespaceFix));
333 EXPECT_EQ("namespace {\n"
334 "int i;\n"
335 "}",
336 format("namespace {\n"
337 "int i;\n"
338 "}",
339 LLVMWithNoNamespaceFix));
340 EXPECT_EQ("namespace {\n"
341 "int i;\n"
342 "\n"
343 "};",
344 format("namespace {\n"
345 "int i;\n"
346 "\n"
347 "};",
348 LLVMWithNoNamespaceFix));
349 EXPECT_EQ("namespace {\n"
350 "int i;\n"
351 "};",
352 format("namespace {\n"
353 "int i;\n"
354 "};",
355 LLVMWithNoNamespaceFix));
356 EXPECT_EQ("namespace {\n"
357 "int i;\n"
358 "\n"
359 "}",
360 format("namespace {\n"
361 "int i;\n"
362 "\n"
363 "}"));
364 EXPECT_EQ("namespace {\n"
365 "int i;\n"
366 "\n"
367 "} // namespace",
368 format("namespace {\n"
369 "int i;\n"
370 "\n"
371 "} // namespace"));
372
373 FormatStyle Style = getLLVMStyle();
374 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
375 Style.MaxEmptyLinesToKeep = 2;
376 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
377 Style.BraceWrapping.AfterClass = true;
378 Style.BraceWrapping.AfterFunction = true;
379 Style.KeepEmptyLinesAtTheStartOfBlocks = false;
380
381 EXPECT_EQ("class Foo\n"
382 "{\n"
383 " Foo() {}\n"
384 "\n"
385 " void funk() {}\n"
386 "};",
387 format("class Foo\n"
388 "{\n"
389 " Foo()\n"
390 " {\n"
391 " }\n"
392 "\n"
393 " void funk() {}\n"
394 "};",
395 Style));
396 }
397
TEST_F(FormatTest,RecognizesBinaryOperatorKeywords)398 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
399 verifyFormat("x = (a) and (b);");
400 verifyFormat("x = (a) or (b);");
401 verifyFormat("x = (a) bitand (b);");
402 verifyFormat("x = (a) bitor (b);");
403 verifyFormat("x = (a) not_eq (b);");
404 verifyFormat("x = (a) and_eq (b);");
405 verifyFormat("x = (a) or_eq (b);");
406 verifyFormat("x = (a) xor (b);");
407 }
408
TEST_F(FormatTest,RecognizesUnaryOperatorKeywords)409 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
410 verifyFormat("x = compl(a);");
411 verifyFormat("x = not(a);");
412 verifyFormat("x = bitand(a);");
413 // Unary operator must not be merged with the next identifier
414 verifyFormat("x = compl a;");
415 verifyFormat("x = not a;");
416 verifyFormat("x = bitand a;");
417 }
418
419 //===----------------------------------------------------------------------===//
420 // Tests for control statements.
421 //===----------------------------------------------------------------------===//
422
TEST_F(FormatTest,FormatIfWithoutCompoundStatement)423 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
424 verifyFormat("if (true)\n f();\ng();");
425 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
426 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
427 verifyFormat("if constexpr (true)\n"
428 " f();\ng();");
429 verifyFormat("if CONSTEXPR (true)\n"
430 " f();\ng();");
431 verifyFormat("if constexpr (a)\n"
432 " if constexpr (b)\n"
433 " if constexpr (c)\n"
434 " g();\n"
435 "h();");
436 verifyFormat("if CONSTEXPR (a)\n"
437 " if CONSTEXPR (b)\n"
438 " if CONSTEXPR (c)\n"
439 " g();\n"
440 "h();");
441 verifyFormat("if constexpr (a)\n"
442 " if constexpr (b) {\n"
443 " f();\n"
444 " }\n"
445 "g();");
446 verifyFormat("if CONSTEXPR (a)\n"
447 " if CONSTEXPR (b) {\n"
448 " f();\n"
449 " }\n"
450 "g();");
451
452 FormatStyle AllowsMergedIf = getLLVMStyle();
453 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
454 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
455 FormatStyle::SIS_WithoutElse;
456 verifyFormat("if (a)\n"
457 " // comment\n"
458 " f();",
459 AllowsMergedIf);
460 verifyFormat("{\n"
461 " if (a)\n"
462 " label:\n"
463 " f();\n"
464 "}",
465 AllowsMergedIf);
466 verifyFormat("#define A \\\n"
467 " if (a) \\\n"
468 " label: \\\n"
469 " f()",
470 AllowsMergedIf);
471 verifyFormat("if (a)\n"
472 " ;",
473 AllowsMergedIf);
474 verifyFormat("if (a)\n"
475 " if (b) return;",
476 AllowsMergedIf);
477
478 verifyFormat("if (a) // Can't merge this\n"
479 " f();\n",
480 AllowsMergedIf);
481 verifyFormat("if (a) /* still don't merge */\n"
482 " f();",
483 AllowsMergedIf);
484 verifyFormat("if (a) { // Never merge this\n"
485 " f();\n"
486 "}",
487 AllowsMergedIf);
488 verifyFormat("if (a) { /* Never merge this */\n"
489 " f();\n"
490 "}",
491 AllowsMergedIf);
492
493 AllowsMergedIf.ColumnLimit = 14;
494 verifyFormat("if (a) return;", AllowsMergedIf);
495 verifyFormat("if (aaaaaaaaa)\n"
496 " return;",
497 AllowsMergedIf);
498
499 AllowsMergedIf.ColumnLimit = 13;
500 verifyFormat("if (a)\n return;", AllowsMergedIf);
501 }
502
TEST_F(FormatTest,FormatIfWithoutCompoundStatementButElseWith)503 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
504 FormatStyle AllowsMergedIf = getLLVMStyle();
505 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
506 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
507 FormatStyle::SIS_WithoutElse;
508 verifyFormat("if (a)\n"
509 " f();\n"
510 "else {\n"
511 " g();\n"
512 "}",
513 AllowsMergedIf);
514 verifyFormat("if (a)\n"
515 " f();\n"
516 "else\n"
517 " g();\n",
518 AllowsMergedIf);
519
520 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
521
522 verifyFormat("if (a) f();\n"
523 "else {\n"
524 " g();\n"
525 "}",
526 AllowsMergedIf);
527 verifyFormat("if (a) f();\n"
528 "else {\n"
529 " if (a) f();\n"
530 " else {\n"
531 " g();\n"
532 " }\n"
533 " g();\n"
534 "}",
535 AllowsMergedIf);
536 }
537
TEST_F(FormatTest,FormatLoopsWithoutCompoundStatement)538 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
539 FormatStyle AllowsMergedLoops = getLLVMStyle();
540 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
541 verifyFormat("while (true) continue;", AllowsMergedLoops);
542 verifyFormat("for (;;) continue;", AllowsMergedLoops);
543 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
544 verifyFormat("while (true)\n"
545 " ;",
546 AllowsMergedLoops);
547 verifyFormat("for (;;)\n"
548 " ;",
549 AllowsMergedLoops);
550 verifyFormat("for (;;)\n"
551 " for (;;) continue;",
552 AllowsMergedLoops);
553 verifyFormat("for (;;) // Can't merge this\n"
554 " continue;",
555 AllowsMergedLoops);
556 verifyFormat("for (;;) /* still don't merge */\n"
557 " continue;",
558 AllowsMergedLoops);
559 verifyFormat("do a++;\n"
560 "while (true);",
561 AllowsMergedLoops);
562 verifyFormat("do /* Don't merge */\n"
563 " a++;\n"
564 "while (true);",
565 AllowsMergedLoops);
566 verifyFormat("do // Don't merge\n"
567 " a++;\n"
568 "while (true);",
569 AllowsMergedLoops);
570 verifyFormat("do\n"
571 " // Don't merge\n"
572 " a++;\n"
573 "while (true);",
574 AllowsMergedLoops);
575 // Without braces labels are interpreted differently.
576 verifyFormat("{\n"
577 " do\n"
578 " label:\n"
579 " a++;\n"
580 " while (true);\n"
581 "}",
582 AllowsMergedLoops);
583 }
584
TEST_F(FormatTest,FormatShortBracedStatements)585 TEST_F(FormatTest, FormatShortBracedStatements) {
586 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
587 AllowSimpleBracedStatements.ColumnLimit = 40;
588 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
589 FormatStyle::SBS_Always;
590
591 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
592 FormatStyle::SIS_WithoutElse;
593 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
594
595 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
596 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
597 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
598
599 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
600 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
601 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
602 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
603 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
604 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
605 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
606 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
607 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
608 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
609 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
610 AllowSimpleBracedStatements);
611 verifyFormat("if (true) {\n"
612 " ffffffffffffffffffffffff();\n"
613 "}",
614 AllowSimpleBracedStatements);
615 verifyFormat("if (true) {\n"
616 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
617 "}",
618 AllowSimpleBracedStatements);
619 verifyFormat("if (true) { //\n"
620 " f();\n"
621 "}",
622 AllowSimpleBracedStatements);
623 verifyFormat("if (true) {\n"
624 " f();\n"
625 " f();\n"
626 "}",
627 AllowSimpleBracedStatements);
628 verifyFormat("if (true) {\n"
629 " f();\n"
630 "} else {\n"
631 " f();\n"
632 "}",
633 AllowSimpleBracedStatements);
634
635 verifyFormat("struct A2 {\n"
636 " int X;\n"
637 "};",
638 AllowSimpleBracedStatements);
639 verifyFormat("typedef struct A2 {\n"
640 " int X;\n"
641 "} A2_t;",
642 AllowSimpleBracedStatements);
643 verifyFormat("template <int> struct A2 {\n"
644 " struct B {};\n"
645 "};",
646 AllowSimpleBracedStatements);
647
648 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
649 FormatStyle::SIS_Never;
650 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
651 verifyFormat("if (true) {\n"
652 " f();\n"
653 "}",
654 AllowSimpleBracedStatements);
655 verifyFormat("if (true) {\n"
656 " f();\n"
657 "} else {\n"
658 " f();\n"
659 "}",
660 AllowSimpleBracedStatements);
661
662 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
663 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
664 verifyFormat("while (true) {\n"
665 " f();\n"
666 "}",
667 AllowSimpleBracedStatements);
668 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
669 verifyFormat("for (;;) {\n"
670 " f();\n"
671 "}",
672 AllowSimpleBracedStatements);
673
674 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
675 FormatStyle::SIS_WithoutElse;
676 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
677 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
678 FormatStyle::BWACS_Always;
679
680 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
681 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
682 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
683 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
684 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
685 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
686 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
687 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
688 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
689 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
690 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
691 AllowSimpleBracedStatements);
692 verifyFormat("if (true)\n"
693 "{\n"
694 " ffffffffffffffffffffffff();\n"
695 "}",
696 AllowSimpleBracedStatements);
697 verifyFormat("if (true)\n"
698 "{\n"
699 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
700 "}",
701 AllowSimpleBracedStatements);
702 verifyFormat("if (true)\n"
703 "{ //\n"
704 " f();\n"
705 "}",
706 AllowSimpleBracedStatements);
707 verifyFormat("if (true)\n"
708 "{\n"
709 " f();\n"
710 " f();\n"
711 "}",
712 AllowSimpleBracedStatements);
713 verifyFormat("if (true)\n"
714 "{\n"
715 " f();\n"
716 "} else\n"
717 "{\n"
718 " f();\n"
719 "}",
720 AllowSimpleBracedStatements);
721
722 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
723 FormatStyle::SIS_Never;
724 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
725 verifyFormat("if (true)\n"
726 "{\n"
727 " f();\n"
728 "}",
729 AllowSimpleBracedStatements);
730 verifyFormat("if (true)\n"
731 "{\n"
732 " f();\n"
733 "} else\n"
734 "{\n"
735 " f();\n"
736 "}",
737 AllowSimpleBracedStatements);
738
739 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
740 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
741 verifyFormat("while (true)\n"
742 "{\n"
743 " f();\n"
744 "}",
745 AllowSimpleBracedStatements);
746 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
747 verifyFormat("for (;;)\n"
748 "{\n"
749 " f();\n"
750 "}",
751 AllowSimpleBracedStatements);
752 }
753
TEST_F(FormatTest,ShortBlocksInMacrosDontMergeWithCodeAfterMacro)754 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
755 FormatStyle Style = getLLVMStyleWithColumns(60);
756 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
757 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
758 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
759 EXPECT_EQ("#define A \\\n"
760 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
761 " { \\\n"
762 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
763 " }\n"
764 "X;",
765 format("#define A \\\n"
766 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
767 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
768 " }\n"
769 "X;",
770 Style));
771 }
772
TEST_F(FormatTest,ParseIfElse)773 TEST_F(FormatTest, ParseIfElse) {
774 verifyFormat("if (true)\n"
775 " if (true)\n"
776 " if (true)\n"
777 " f();\n"
778 " else\n"
779 " g();\n"
780 " else\n"
781 " h();\n"
782 "else\n"
783 " i();");
784 verifyFormat("if (true)\n"
785 " if (true)\n"
786 " if (true) {\n"
787 " if (true)\n"
788 " f();\n"
789 " } else {\n"
790 " g();\n"
791 " }\n"
792 " else\n"
793 " h();\n"
794 "else {\n"
795 " i();\n"
796 "}");
797 verifyFormat("if (true)\n"
798 " if constexpr (true)\n"
799 " if (true) {\n"
800 " if constexpr (true)\n"
801 " f();\n"
802 " } else {\n"
803 " g();\n"
804 " }\n"
805 " else\n"
806 " h();\n"
807 "else {\n"
808 " i();\n"
809 "}");
810 verifyFormat("if (true)\n"
811 " if CONSTEXPR (true)\n"
812 " if (true) {\n"
813 " if CONSTEXPR (true)\n"
814 " f();\n"
815 " } else {\n"
816 " g();\n"
817 " }\n"
818 " else\n"
819 " h();\n"
820 "else {\n"
821 " i();\n"
822 "}");
823 verifyFormat("void f() {\n"
824 " if (a) {\n"
825 " } else {\n"
826 " }\n"
827 "}");
828 }
829
TEST_F(FormatTest,ElseIf)830 TEST_F(FormatTest, ElseIf) {
831 verifyFormat("if (a) {\n} else if (b) {\n}");
832 verifyFormat("if (a)\n"
833 " f();\n"
834 "else if (b)\n"
835 " g();\n"
836 "else\n"
837 " h();");
838 verifyFormat("if constexpr (a)\n"
839 " f();\n"
840 "else if constexpr (b)\n"
841 " g();\n"
842 "else\n"
843 " h();");
844 verifyFormat("if CONSTEXPR (a)\n"
845 " f();\n"
846 "else if CONSTEXPR (b)\n"
847 " g();\n"
848 "else\n"
849 " h();");
850 verifyFormat("if (a) {\n"
851 " f();\n"
852 "}\n"
853 "// or else ..\n"
854 "else {\n"
855 " g()\n"
856 "}");
857
858 verifyFormat("if (a) {\n"
859 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
861 "}");
862 verifyFormat("if (a) {\n"
863 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
865 "}");
866 verifyFormat("if (a) {\n"
867 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
868 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
869 "}");
870 verifyFormat("if (a) {\n"
871 "} else if (\n"
872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
873 "}",
874 getLLVMStyleWithColumns(62));
875 verifyFormat("if (a) {\n"
876 "} else if constexpr (\n"
877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
878 "}",
879 getLLVMStyleWithColumns(62));
880 verifyFormat("if (a) {\n"
881 "} else if CONSTEXPR (\n"
882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
883 "}",
884 getLLVMStyleWithColumns(62));
885 }
886
TEST_F(FormatTest,FormatsForLoop)887 TEST_F(FormatTest, FormatsForLoop) {
888 verifyFormat(
889 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
890 " ++VeryVeryLongLoopVariable)\n"
891 " ;");
892 verifyFormat("for (;;)\n"
893 " f();");
894 verifyFormat("for (;;) {\n}");
895 verifyFormat("for (;;) {\n"
896 " f();\n"
897 "}");
898 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
899
900 verifyFormat(
901 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
902 " E = UnwrappedLines.end();\n"
903 " I != E; ++I) {\n}");
904
905 verifyFormat(
906 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
907 " ++IIIII) {\n}");
908 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
909 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
910 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
911 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
912 " I = FD->getDeclsInPrototypeScope().begin(),\n"
913 " E = FD->getDeclsInPrototypeScope().end();\n"
914 " I != E; ++I) {\n}");
915 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
916 " I = Container.begin(),\n"
917 " E = Container.end();\n"
918 " I != E; ++I) {\n}",
919 getLLVMStyleWithColumns(76));
920
921 verifyFormat(
922 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
924 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
926 " ++aaaaaaaaaaa) {\n}");
927 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
928 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
929 " ++i) {\n}");
930 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
931 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
932 "}");
933 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
934 " aaaaaaaaaa);\n"
935 " iter; ++iter) {\n"
936 "}");
937 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
939 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
940 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
941
942 // These should not be formatted as Objective-C for-in loops.
943 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
944 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
945 verifyFormat("Foo *x;\nfor (x in y) {\n}");
946 verifyFormat(
947 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
948
949 FormatStyle NoBinPacking = getLLVMStyle();
950 NoBinPacking.BinPackParameters = false;
951 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
952 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
953 " aaaaaaaaaaaaaaaa,\n"
954 " aaaaaaaaaaaaaaaa,\n"
955 " aaaaaaaaaaaaaaaa);\n"
956 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
957 "}",
958 NoBinPacking);
959 verifyFormat(
960 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
961 " E = UnwrappedLines.end();\n"
962 " I != E;\n"
963 " ++I) {\n}",
964 NoBinPacking);
965
966 FormatStyle AlignLeft = getLLVMStyle();
967 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
968 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
969 }
970
TEST_F(FormatTest,RangeBasedForLoops)971 TEST_F(FormatTest, RangeBasedForLoops) {
972 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
974 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
975 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
976 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
978 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
979 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
980 }
981
TEST_F(FormatTest,ForEachLoops)982 TEST_F(FormatTest, ForEachLoops) {
983 verifyFormat("void f() {\n"
984 " foreach (Item *item, itemlist) {}\n"
985 " Q_FOREACH (Item *item, itemlist) {}\n"
986 " BOOST_FOREACH (Item *item, itemlist) {}\n"
987 " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
988 "}");
989
990 FormatStyle Style = getLLVMStyle();
991 Style.SpaceBeforeParens =
992 FormatStyle::SBPO_ControlStatementsExceptForEachMacros;
993 verifyFormat("void f() {\n"
994 " foreach(Item *item, itemlist) {}\n"
995 " Q_FOREACH(Item *item, itemlist) {}\n"
996 " BOOST_FOREACH(Item *item, itemlist) {}\n"
997 " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
998 "}",
999 Style);
1000
1001 // As function-like macros.
1002 verifyFormat("#define foreach(x, y)\n"
1003 "#define Q_FOREACH(x, y)\n"
1004 "#define BOOST_FOREACH(x, y)\n"
1005 "#define UNKNOWN_FOREACH(x, y)\n");
1006
1007 // Not as function-like macros.
1008 verifyFormat("#define foreach (x, y)\n"
1009 "#define Q_FOREACH (x, y)\n"
1010 "#define BOOST_FOREACH (x, y)\n"
1011 "#define UNKNOWN_FOREACH (x, y)\n");
1012
1013 // handle microsoft non standard extension
1014 verifyFormat("for each (char c in x->MyStringProperty)");
1015 }
1016
TEST_F(FormatTest,FormatsWhileLoop)1017 TEST_F(FormatTest, FormatsWhileLoop) {
1018 verifyFormat("while (true) {\n}");
1019 verifyFormat("while (true)\n"
1020 " f();");
1021 verifyFormat("while () {\n}");
1022 verifyFormat("while () {\n"
1023 " f();\n"
1024 "}");
1025 }
1026
TEST_F(FormatTest,FormatsDoWhile)1027 TEST_F(FormatTest, FormatsDoWhile) {
1028 verifyFormat("do {\n"
1029 " do_something();\n"
1030 "} while (something());");
1031 verifyFormat("do\n"
1032 " do_something();\n"
1033 "while (something());");
1034 }
1035
TEST_F(FormatTest,FormatsSwitchStatement)1036 TEST_F(FormatTest, FormatsSwitchStatement) {
1037 verifyFormat("switch (x) {\n"
1038 "case 1:\n"
1039 " f();\n"
1040 " break;\n"
1041 "case kFoo:\n"
1042 "case ns::kBar:\n"
1043 "case kBaz:\n"
1044 " break;\n"
1045 "default:\n"
1046 " g();\n"
1047 " break;\n"
1048 "}");
1049 verifyFormat("switch (x) {\n"
1050 "case 1: {\n"
1051 " f();\n"
1052 " break;\n"
1053 "}\n"
1054 "case 2: {\n"
1055 " break;\n"
1056 "}\n"
1057 "}");
1058 verifyFormat("switch (x) {\n"
1059 "case 1: {\n"
1060 " f();\n"
1061 " {\n"
1062 " g();\n"
1063 " h();\n"
1064 " }\n"
1065 " break;\n"
1066 "}\n"
1067 "}");
1068 verifyFormat("switch (x) {\n"
1069 "case 1: {\n"
1070 " f();\n"
1071 " if (foo) {\n"
1072 " g();\n"
1073 " h();\n"
1074 " }\n"
1075 " break;\n"
1076 "}\n"
1077 "}");
1078 verifyFormat("switch (x) {\n"
1079 "case 1: {\n"
1080 " f();\n"
1081 " g();\n"
1082 "} break;\n"
1083 "}");
1084 verifyFormat("switch (test)\n"
1085 " ;");
1086 verifyFormat("switch (x) {\n"
1087 "default: {\n"
1088 " // Do nothing.\n"
1089 "}\n"
1090 "}");
1091 verifyFormat("switch (x) {\n"
1092 "// comment\n"
1093 "// if 1, do f()\n"
1094 "case 1:\n"
1095 " f();\n"
1096 "}");
1097 verifyFormat("switch (x) {\n"
1098 "case 1:\n"
1099 " // Do amazing stuff\n"
1100 " {\n"
1101 " f();\n"
1102 " g();\n"
1103 " }\n"
1104 " break;\n"
1105 "}");
1106 verifyFormat("#define A \\\n"
1107 " switch (x) { \\\n"
1108 " case a: \\\n"
1109 " foo = b; \\\n"
1110 " }",
1111 getLLVMStyleWithColumns(20));
1112 verifyFormat("#define OPERATION_CASE(name) \\\n"
1113 " case OP_name: \\\n"
1114 " return operations::Operation##name\n",
1115 getLLVMStyleWithColumns(40));
1116 verifyFormat("switch (x) {\n"
1117 "case 1:;\n"
1118 "default:;\n"
1119 " int i;\n"
1120 "}");
1121
1122 verifyGoogleFormat("switch (x) {\n"
1123 " case 1:\n"
1124 " f();\n"
1125 " break;\n"
1126 " case kFoo:\n"
1127 " case ns::kBar:\n"
1128 " case kBaz:\n"
1129 " break;\n"
1130 " default:\n"
1131 " g();\n"
1132 " break;\n"
1133 "}");
1134 verifyGoogleFormat("switch (x) {\n"
1135 " case 1: {\n"
1136 " f();\n"
1137 " break;\n"
1138 " }\n"
1139 "}");
1140 verifyGoogleFormat("switch (test)\n"
1141 " ;");
1142
1143 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
1144 " case OP_name: \\\n"
1145 " return operations::Operation##name\n");
1146 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
1147 " // Get the correction operation class.\n"
1148 " switch (OpCode) {\n"
1149 " CASE(Add);\n"
1150 " CASE(Subtract);\n"
1151 " default:\n"
1152 " return operations::Unknown;\n"
1153 " }\n"
1154 "#undef OPERATION_CASE\n"
1155 "}");
1156 verifyFormat("DEBUG({\n"
1157 " switch (x) {\n"
1158 " case A:\n"
1159 " f();\n"
1160 " break;\n"
1161 " // fallthrough\n"
1162 " case B:\n"
1163 " g();\n"
1164 " break;\n"
1165 " }\n"
1166 "});");
1167 EXPECT_EQ("DEBUG({\n"
1168 " switch (x) {\n"
1169 " case A:\n"
1170 " f();\n"
1171 " break;\n"
1172 " // On B:\n"
1173 " case B:\n"
1174 " g();\n"
1175 " break;\n"
1176 " }\n"
1177 "});",
1178 format("DEBUG({\n"
1179 " switch (x) {\n"
1180 " case A:\n"
1181 " f();\n"
1182 " break;\n"
1183 " // On B:\n"
1184 " case B:\n"
1185 " g();\n"
1186 " break;\n"
1187 " }\n"
1188 "});",
1189 getLLVMStyle()));
1190 EXPECT_EQ("switch (n) {\n"
1191 "case 0: {\n"
1192 " return false;\n"
1193 "}\n"
1194 "default: {\n"
1195 " return true;\n"
1196 "}\n"
1197 "}",
1198 format("switch (n)\n"
1199 "{\n"
1200 "case 0: {\n"
1201 " return false;\n"
1202 "}\n"
1203 "default: {\n"
1204 " return true;\n"
1205 "}\n"
1206 "}",
1207 getLLVMStyle()));
1208 verifyFormat("switch (a) {\n"
1209 "case (b):\n"
1210 " return;\n"
1211 "}");
1212
1213 verifyFormat("switch (a) {\n"
1214 "case some_namespace::\n"
1215 " some_constant:\n"
1216 " return;\n"
1217 "}",
1218 getLLVMStyleWithColumns(34));
1219
1220 FormatStyle Style = getLLVMStyle();
1221 Style.IndentCaseLabels = true;
1222 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
1223 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1224 Style.BraceWrapping.AfterCaseLabel = true;
1225 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1226 EXPECT_EQ("switch (n)\n"
1227 "{\n"
1228 " case 0:\n"
1229 " {\n"
1230 " return false;\n"
1231 " }\n"
1232 " default:\n"
1233 " {\n"
1234 " return true;\n"
1235 " }\n"
1236 "}",
1237 format("switch (n) {\n"
1238 " case 0: {\n"
1239 " return false;\n"
1240 " }\n"
1241 " default: {\n"
1242 " return true;\n"
1243 " }\n"
1244 "}",
1245 Style));
1246 Style.BraceWrapping.AfterCaseLabel = false;
1247 EXPECT_EQ("switch (n)\n"
1248 "{\n"
1249 " case 0: {\n"
1250 " return false;\n"
1251 " }\n"
1252 " default: {\n"
1253 " return true;\n"
1254 " }\n"
1255 "}",
1256 format("switch (n) {\n"
1257 " case 0:\n"
1258 " {\n"
1259 " return false;\n"
1260 " }\n"
1261 " default:\n"
1262 " {\n"
1263 " return true;\n"
1264 " }\n"
1265 "}",
1266 Style));
1267 Style.IndentCaseLabels = false;
1268 Style.IndentCaseBlocks = true;
1269 EXPECT_EQ("switch (n)\n"
1270 "{\n"
1271 "case 0:\n"
1272 " {\n"
1273 " return false;\n"
1274 " }\n"
1275 "case 1:\n"
1276 " break;\n"
1277 "default:\n"
1278 " {\n"
1279 " return true;\n"
1280 " }\n"
1281 "}",
1282 format("switch (n) {\n"
1283 "case 0: {\n"
1284 " return false;\n"
1285 "}\n"
1286 "case 1:\n"
1287 " break;\n"
1288 "default: {\n"
1289 " return true;\n"
1290 "}\n"
1291 "}",
1292 Style));
1293 Style.IndentCaseLabels = true;
1294 Style.IndentCaseBlocks = true;
1295 EXPECT_EQ("switch (n)\n"
1296 "{\n"
1297 " case 0:\n"
1298 " {\n"
1299 " return false;\n"
1300 " }\n"
1301 " case 1:\n"
1302 " break;\n"
1303 " default:\n"
1304 " {\n"
1305 " return true;\n"
1306 " }\n"
1307 "}",
1308 format("switch (n) {\n"
1309 "case 0: {\n"
1310 " return false;\n"
1311 "}\n"
1312 "case 1:\n"
1313 " break;\n"
1314 "default: {\n"
1315 " return true;\n"
1316 "}\n"
1317 "}",
1318 Style));
1319 }
1320
TEST_F(FormatTest,CaseRanges)1321 TEST_F(FormatTest, CaseRanges) {
1322 verifyFormat("switch (x) {\n"
1323 "case 'A' ... 'Z':\n"
1324 "case 1 ... 5:\n"
1325 "case a ... b:\n"
1326 " break;\n"
1327 "}");
1328 }
1329
TEST_F(FormatTest,ShortEnums)1330 TEST_F(FormatTest, ShortEnums) {
1331 FormatStyle Style = getLLVMStyle();
1332 Style.AllowShortEnumsOnASingleLine = true;
1333 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
1334 Style.AllowShortEnumsOnASingleLine = false;
1335 verifyFormat("enum\n"
1336 "{\n"
1337 " A,\n"
1338 " B,\n"
1339 " C\n"
1340 "} ShortEnum1, ShortEnum2;",
1341 Style);
1342 }
1343
TEST_F(FormatTest,ShortCaseLabels)1344 TEST_F(FormatTest, ShortCaseLabels) {
1345 FormatStyle Style = getLLVMStyle();
1346 Style.AllowShortCaseLabelsOnASingleLine = true;
1347 verifyFormat("switch (a) {\n"
1348 "case 1: x = 1; break;\n"
1349 "case 2: return;\n"
1350 "case 3:\n"
1351 "case 4:\n"
1352 "case 5: return;\n"
1353 "case 6: // comment\n"
1354 " return;\n"
1355 "case 7:\n"
1356 " // comment\n"
1357 " return;\n"
1358 "case 8:\n"
1359 " x = 8; // comment\n"
1360 " break;\n"
1361 "default: y = 1; break;\n"
1362 "}",
1363 Style);
1364 verifyFormat("switch (a) {\n"
1365 "case 0: return; // comment\n"
1366 "case 1: break; // comment\n"
1367 "case 2: return;\n"
1368 "// comment\n"
1369 "case 3: return;\n"
1370 "// comment 1\n"
1371 "// comment 2\n"
1372 "// comment 3\n"
1373 "case 4: break; /* comment */\n"
1374 "case 5:\n"
1375 " // comment\n"
1376 " break;\n"
1377 "case 6: /* comment */ x = 1; break;\n"
1378 "case 7: x = /* comment */ 1; break;\n"
1379 "case 8:\n"
1380 " x = 1; /* comment */\n"
1381 " break;\n"
1382 "case 9:\n"
1383 " break; // comment line 1\n"
1384 " // comment line 2\n"
1385 "}",
1386 Style);
1387 EXPECT_EQ("switch (a) {\n"
1388 "case 1:\n"
1389 " x = 8;\n"
1390 " // fall through\n"
1391 "case 2: x = 8;\n"
1392 "// comment\n"
1393 "case 3:\n"
1394 " return; /* comment line 1\n"
1395 " * comment line 2 */\n"
1396 "case 4: i = 8;\n"
1397 "// something else\n"
1398 "#if FOO\n"
1399 "case 5: break;\n"
1400 "#endif\n"
1401 "}",
1402 format("switch (a) {\n"
1403 "case 1: x = 8;\n"
1404 " // fall through\n"
1405 "case 2:\n"
1406 " x = 8;\n"
1407 "// comment\n"
1408 "case 3:\n"
1409 " return; /* comment line 1\n"
1410 " * comment line 2 */\n"
1411 "case 4:\n"
1412 " i = 8;\n"
1413 "// something else\n"
1414 "#if FOO\n"
1415 "case 5: break;\n"
1416 "#endif\n"
1417 "}",
1418 Style));
1419 EXPECT_EQ("switch (a) {\n"
1420 "case 0:\n"
1421 " return; // long long long long long long long long long long "
1422 "long long comment\n"
1423 " // line\n"
1424 "}",
1425 format("switch (a) {\n"
1426 "case 0: return; // long long long long long long long long "
1427 "long long long long comment line\n"
1428 "}",
1429 Style));
1430 EXPECT_EQ("switch (a) {\n"
1431 "case 0:\n"
1432 " return; /* long long long long long long long long long long "
1433 "long long comment\n"
1434 " line */\n"
1435 "}",
1436 format("switch (a) {\n"
1437 "case 0: return; /* long long long long long long long long "
1438 "long long long long comment line */\n"
1439 "}",
1440 Style));
1441 verifyFormat("switch (a) {\n"
1442 "#if FOO\n"
1443 "case 0: return 0;\n"
1444 "#endif\n"
1445 "}",
1446 Style);
1447 verifyFormat("switch (a) {\n"
1448 "case 1: {\n"
1449 "}\n"
1450 "case 2: {\n"
1451 " return;\n"
1452 "}\n"
1453 "case 3: {\n"
1454 " x = 1;\n"
1455 " return;\n"
1456 "}\n"
1457 "case 4:\n"
1458 " if (x)\n"
1459 " return;\n"
1460 "}",
1461 Style);
1462 Style.ColumnLimit = 21;
1463 verifyFormat("switch (a) {\n"
1464 "case 1: x = 1; break;\n"
1465 "case 2: return;\n"
1466 "case 3:\n"
1467 "case 4:\n"
1468 "case 5: return;\n"
1469 "default:\n"
1470 " y = 1;\n"
1471 " break;\n"
1472 "}",
1473 Style);
1474 Style.ColumnLimit = 80;
1475 Style.AllowShortCaseLabelsOnASingleLine = false;
1476 Style.IndentCaseLabels = true;
1477 EXPECT_EQ("switch (n) {\n"
1478 " default /*comments*/:\n"
1479 " return true;\n"
1480 " case 0:\n"
1481 " return false;\n"
1482 "}",
1483 format("switch (n) {\n"
1484 "default/*comments*/:\n"
1485 " return true;\n"
1486 "case 0:\n"
1487 " return false;\n"
1488 "}",
1489 Style));
1490 Style.AllowShortCaseLabelsOnASingleLine = true;
1491 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1492 Style.BraceWrapping.AfterCaseLabel = true;
1493 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1494 EXPECT_EQ("switch (n)\n"
1495 "{\n"
1496 " case 0:\n"
1497 " {\n"
1498 " return false;\n"
1499 " }\n"
1500 " default:\n"
1501 " {\n"
1502 " return true;\n"
1503 " }\n"
1504 "}",
1505 format("switch (n) {\n"
1506 " case 0: {\n"
1507 " return false;\n"
1508 " }\n"
1509 " default:\n"
1510 " {\n"
1511 " return true;\n"
1512 " }\n"
1513 "}",
1514 Style));
1515 }
1516
TEST_F(FormatTest,FormatsLabels)1517 TEST_F(FormatTest, FormatsLabels) {
1518 verifyFormat("void f() {\n"
1519 " some_code();\n"
1520 "test_label:\n"
1521 " some_other_code();\n"
1522 " {\n"
1523 " some_more_code();\n"
1524 " another_label:\n"
1525 " some_more_code();\n"
1526 " }\n"
1527 "}");
1528 verifyFormat("{\n"
1529 " some_code();\n"
1530 "test_label:\n"
1531 " some_other_code();\n"
1532 "}");
1533 verifyFormat("{\n"
1534 " some_code();\n"
1535 "test_label:;\n"
1536 " int i = 0;\n"
1537 "}");
1538 FormatStyle Style = getLLVMStyle();
1539 Style.IndentGotoLabels = false;
1540 verifyFormat("void f() {\n"
1541 " some_code();\n"
1542 "test_label:\n"
1543 " some_other_code();\n"
1544 " {\n"
1545 " some_more_code();\n"
1546 "another_label:\n"
1547 " some_more_code();\n"
1548 " }\n"
1549 "}",
1550 Style);
1551 verifyFormat("{\n"
1552 " some_code();\n"
1553 "test_label:\n"
1554 " some_other_code();\n"
1555 "}",
1556 Style);
1557 verifyFormat("{\n"
1558 " some_code();\n"
1559 "test_label:;\n"
1560 " int i = 0;\n"
1561 "}");
1562 }
1563
TEST_F(FormatTest,MultiLineControlStatements)1564 TEST_F(FormatTest, MultiLineControlStatements) {
1565 FormatStyle Style = getLLVMStyle();
1566 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
1567 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
1568 Style.ColumnLimit = 20;
1569 // Short lines should keep opening brace on same line.
1570 EXPECT_EQ("if (foo) {\n"
1571 " bar();\n"
1572 "}",
1573 format("if(foo){bar();}", Style));
1574 EXPECT_EQ("if (foo) {\n"
1575 " bar();\n"
1576 "} else {\n"
1577 " baz();\n"
1578 "}",
1579 format("if(foo){bar();}else{baz();}", Style));
1580 EXPECT_EQ("if (foo && bar) {\n"
1581 " baz();\n"
1582 "}",
1583 format("if(foo&&bar){baz();}", Style));
1584 EXPECT_EQ("if (foo) {\n"
1585 " bar();\n"
1586 "} else if (baz) {\n"
1587 " quux();\n"
1588 "}",
1589 format("if(foo){bar();}else if(baz){quux();}", Style));
1590 EXPECT_EQ(
1591 "if (foo) {\n"
1592 " bar();\n"
1593 "} else if (baz) {\n"
1594 " quux();\n"
1595 "} else {\n"
1596 " foobar();\n"
1597 "}",
1598 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style));
1599 EXPECT_EQ("for (;;) {\n"
1600 " foo();\n"
1601 "}",
1602 format("for(;;){foo();}"));
1603 EXPECT_EQ("while (1) {\n"
1604 " foo();\n"
1605 "}",
1606 format("while(1){foo();}", Style));
1607 EXPECT_EQ("switch (foo) {\n"
1608 "case bar:\n"
1609 " return;\n"
1610 "}",
1611 format("switch(foo){case bar:return;}", Style));
1612 EXPECT_EQ("try {\n"
1613 " foo();\n"
1614 "} catch (...) {\n"
1615 " bar();\n"
1616 "}",
1617 format("try{foo();}catch(...){bar();}", Style));
1618 EXPECT_EQ("do {\n"
1619 " foo();\n"
1620 "} while (bar &&\n"
1621 " baz);",
1622 format("do{foo();}while(bar&&baz);", Style));
1623 // Long lines should put opening brace on new line.
1624 EXPECT_EQ("if (foo && bar &&\n"
1625 " baz)\n"
1626 "{\n"
1627 " quux();\n"
1628 "}",
1629 format("if(foo&&bar&&baz){quux();}", Style));
1630 EXPECT_EQ("if (foo && bar &&\n"
1631 " baz)\n"
1632 "{\n"
1633 " quux();\n"
1634 "}",
1635 format("if (foo && bar &&\n"
1636 " baz) {\n"
1637 " quux();\n"
1638 "}",
1639 Style));
1640 EXPECT_EQ("if (foo) {\n"
1641 " bar();\n"
1642 "} else if (baz ||\n"
1643 " quux)\n"
1644 "{\n"
1645 " foobar();\n"
1646 "}",
1647 format("if(foo){bar();}else if(baz||quux){foobar();}", Style));
1648 EXPECT_EQ(
1649 "if (foo) {\n"
1650 " bar();\n"
1651 "} else if (baz ||\n"
1652 " quux)\n"
1653 "{\n"
1654 " foobar();\n"
1655 "} else {\n"
1656 " barbaz();\n"
1657 "}",
1658 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
1659 Style));
1660 EXPECT_EQ("for (int i = 0;\n"
1661 " i < 10; ++i)\n"
1662 "{\n"
1663 " foo();\n"
1664 "}",
1665 format("for(int i=0;i<10;++i){foo();}", Style));
1666 EXPECT_EQ("while (foo || bar ||\n"
1667 " baz)\n"
1668 "{\n"
1669 " quux();\n"
1670 "}",
1671 format("while(foo||bar||baz){quux();}", Style));
1672 EXPECT_EQ("switch (\n"
1673 " foo = barbaz)\n"
1674 "{\n"
1675 "case quux:\n"
1676 " return;\n"
1677 "}",
1678 format("switch(foo=barbaz){case quux:return;}", Style));
1679 EXPECT_EQ("try {\n"
1680 " foo();\n"
1681 "} catch (\n"
1682 " Exception &bar)\n"
1683 "{\n"
1684 " baz();\n"
1685 "}",
1686 format("try{foo();}catch(Exception&bar){baz();}", Style));
1687 Style.ColumnLimit =
1688 40; // to concentrate at brace wrapping, not line wrap due to column limit
1689 EXPECT_EQ("try {\n"
1690 " foo();\n"
1691 "} catch (Exception &bar) {\n"
1692 " baz();\n"
1693 "}",
1694 format("try{foo();}catch(Exception&bar){baz();}", Style));
1695 Style.ColumnLimit =
1696 20; // to concentrate at brace wrapping, not line wrap due to column limit
1697
1698 Style.BraceWrapping.BeforeElse = true;
1699 EXPECT_EQ(
1700 "if (foo) {\n"
1701 " bar();\n"
1702 "}\n"
1703 "else if (baz ||\n"
1704 " quux)\n"
1705 "{\n"
1706 " foobar();\n"
1707 "}\n"
1708 "else {\n"
1709 " barbaz();\n"
1710 "}",
1711 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
1712 Style));
1713
1714 Style.BraceWrapping.BeforeCatch = true;
1715 EXPECT_EQ("try {\n"
1716 " foo();\n"
1717 "}\n"
1718 "catch (...) {\n"
1719 " baz();\n"
1720 "}",
1721 format("try{foo();}catch(...){baz();}", Style));
1722 }
1723
TEST_F(FormatTest,BeforeWhile)1724 TEST_F(FormatTest, BeforeWhile) {
1725 FormatStyle Style = getLLVMStyle();
1726 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
1727
1728 verifyFormat("do {\n"
1729 " foo();\n"
1730 "} while (1);",
1731 Style);
1732 Style.BraceWrapping.BeforeWhile = true;
1733 verifyFormat("do {\n"
1734 " foo();\n"
1735 "}\n"
1736 "while (1);",
1737 Style);
1738 }
1739
1740 //===----------------------------------------------------------------------===//
1741 // Tests for classes, namespaces, etc.
1742 //===----------------------------------------------------------------------===//
1743
TEST_F(FormatTest,DoesNotBreakSemiAfterClassDecl)1744 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1745 verifyFormat("class A {};");
1746 }
1747
TEST_F(FormatTest,UnderstandsAccessSpecifiers)1748 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1749 verifyFormat("class A {\n"
1750 "public:\n"
1751 "public: // comment\n"
1752 "protected:\n"
1753 "private:\n"
1754 " void f() {}\n"
1755 "};");
1756 verifyFormat("export class A {\n"
1757 "public:\n"
1758 "public: // comment\n"
1759 "protected:\n"
1760 "private:\n"
1761 " void f() {}\n"
1762 "};");
1763 verifyGoogleFormat("class A {\n"
1764 " public:\n"
1765 " protected:\n"
1766 " private:\n"
1767 " void f() {}\n"
1768 "};");
1769 verifyGoogleFormat("export class A {\n"
1770 " public:\n"
1771 " protected:\n"
1772 " private:\n"
1773 " void f() {}\n"
1774 "};");
1775 verifyFormat("class A {\n"
1776 "public slots:\n"
1777 " void f1() {}\n"
1778 "public Q_SLOTS:\n"
1779 " void f2() {}\n"
1780 "protected slots:\n"
1781 " void f3() {}\n"
1782 "protected Q_SLOTS:\n"
1783 " void f4() {}\n"
1784 "private slots:\n"
1785 " void f5() {}\n"
1786 "private Q_SLOTS:\n"
1787 " void f6() {}\n"
1788 "signals:\n"
1789 " void g1();\n"
1790 "Q_SIGNALS:\n"
1791 " void g2();\n"
1792 "};");
1793
1794 // Don't interpret 'signals' the wrong way.
1795 verifyFormat("signals.set();");
1796 verifyFormat("for (Signals signals : f()) {\n}");
1797 verifyFormat("{\n"
1798 " signals.set(); // This needs indentation.\n"
1799 "}");
1800 verifyFormat("void f() {\n"
1801 "label:\n"
1802 " signals.baz();\n"
1803 "}");
1804 }
1805
TEST_F(FormatTest,SeparatesLogicalBlocks)1806 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1807 EXPECT_EQ("class A {\n"
1808 "public:\n"
1809 " void f();\n"
1810 "\n"
1811 "private:\n"
1812 " void g() {}\n"
1813 " // test\n"
1814 "protected:\n"
1815 " int h;\n"
1816 "};",
1817 format("class A {\n"
1818 "public:\n"
1819 "void f();\n"
1820 "private:\n"
1821 "void g() {}\n"
1822 "// test\n"
1823 "protected:\n"
1824 "int h;\n"
1825 "};"));
1826 EXPECT_EQ("class A {\n"
1827 "protected:\n"
1828 "public:\n"
1829 " void f();\n"
1830 "};",
1831 format("class A {\n"
1832 "protected:\n"
1833 "\n"
1834 "public:\n"
1835 "\n"
1836 " void f();\n"
1837 "};"));
1838
1839 // Even ensure proper spacing inside macros.
1840 EXPECT_EQ("#define B \\\n"
1841 " class A { \\\n"
1842 " protected: \\\n"
1843 " public: \\\n"
1844 " void f(); \\\n"
1845 " };",
1846 format("#define B \\\n"
1847 " class A { \\\n"
1848 " protected: \\\n"
1849 " \\\n"
1850 " public: \\\n"
1851 " \\\n"
1852 " void f(); \\\n"
1853 " };",
1854 getGoogleStyle()));
1855 // But don't remove empty lines after macros ending in access specifiers.
1856 EXPECT_EQ("#define A private:\n"
1857 "\n"
1858 "int i;",
1859 format("#define A private:\n"
1860 "\n"
1861 "int i;"));
1862 }
1863
TEST_F(FormatTest,FormatsClasses)1864 TEST_F(FormatTest, FormatsClasses) {
1865 verifyFormat("class A : public B {};");
1866 verifyFormat("class A : public ::B {};");
1867
1868 verifyFormat(
1869 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1870 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1871 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1872 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1873 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1874 verifyFormat(
1875 "class A : public B, public C, public D, public E, public F {};");
1876 verifyFormat("class AAAAAAAAAAAA : public B,\n"
1877 " public C,\n"
1878 " public D,\n"
1879 " public E,\n"
1880 " public F,\n"
1881 " public G {};");
1882
1883 verifyFormat("class\n"
1884 " ReallyReallyLongClassName {\n"
1885 " int i;\n"
1886 "};",
1887 getLLVMStyleWithColumns(32));
1888 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1889 " aaaaaaaaaaaaaaaa> {};");
1890 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1891 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1892 " aaaaaaaaaaaaaaaaaaaaaa> {};");
1893 verifyFormat("template <class R, class C>\n"
1894 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1895 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1896 verifyFormat("class ::A::B {};");
1897 }
1898
TEST_F(FormatTest,BreakInheritanceStyle)1899 TEST_F(FormatTest, BreakInheritanceStyle) {
1900 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
1901 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
1902 FormatStyle::BILS_BeforeComma;
1903 verifyFormat("class MyClass : public X {};",
1904 StyleWithInheritanceBreakBeforeComma);
1905 verifyFormat("class MyClass\n"
1906 " : public X\n"
1907 " , public Y {};",
1908 StyleWithInheritanceBreakBeforeComma);
1909 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
1910 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
1911 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1912 StyleWithInheritanceBreakBeforeComma);
1913 verifyFormat("struct aaaaaaaaaaaaa\n"
1914 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
1915 " aaaaaaaaaaaaaaaa> {};",
1916 StyleWithInheritanceBreakBeforeComma);
1917
1918 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
1919 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
1920 FormatStyle::BILS_AfterColon;
1921 verifyFormat("class MyClass : public X {};",
1922 StyleWithInheritanceBreakAfterColon);
1923 verifyFormat("class MyClass : public X, public Y {};",
1924 StyleWithInheritanceBreakAfterColon);
1925 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
1926 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1927 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
1928 StyleWithInheritanceBreakAfterColon);
1929 verifyFormat("struct aaaaaaaaaaaaa :\n"
1930 " public aaaaaaaaaaaaaaaaaaa< // break\n"
1931 " aaaaaaaaaaaaaaaa> {};",
1932 StyleWithInheritanceBreakAfterColon);
1933 }
1934
TEST_F(FormatTest,FormatsVariableDeclarationsAfterStructOrClass)1935 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1936 verifyFormat("class A {\n} a, b;");
1937 verifyFormat("struct A {\n} a, b;");
1938 verifyFormat("union A {\n} a;");
1939 }
1940
TEST_F(FormatTest,FormatsEnum)1941 TEST_F(FormatTest, FormatsEnum) {
1942 verifyFormat("enum {\n"
1943 " Zero,\n"
1944 " One = 1,\n"
1945 " Two = One + 1,\n"
1946 " Three = (One + Two),\n"
1947 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
1948 " Five = (One, Two, Three, Four, 5)\n"
1949 "};");
1950 verifyGoogleFormat("enum {\n"
1951 " Zero,\n"
1952 " One = 1,\n"
1953 " Two = One + 1,\n"
1954 " Three = (One + Two),\n"
1955 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
1956 " Five = (One, Two, Three, Four, 5)\n"
1957 "};");
1958 verifyFormat("enum Enum {};");
1959 verifyFormat("enum {};");
1960 verifyFormat("enum X E {} d;");
1961 verifyFormat("enum __attribute__((...)) E {} d;");
1962 verifyFormat("enum __declspec__((...)) E {} d;");
1963 verifyFormat("enum {\n"
1964 " Bar = Foo<int, int>::value\n"
1965 "};",
1966 getLLVMStyleWithColumns(30));
1967
1968 verifyFormat("enum ShortEnum { A, B, C };");
1969 verifyGoogleFormat("enum ShortEnum { A, B, C };");
1970
1971 EXPECT_EQ("enum KeepEmptyLines {\n"
1972 " ONE,\n"
1973 "\n"
1974 " TWO,\n"
1975 "\n"
1976 " THREE\n"
1977 "}",
1978 format("enum KeepEmptyLines {\n"
1979 " ONE,\n"
1980 "\n"
1981 " TWO,\n"
1982 "\n"
1983 "\n"
1984 " THREE\n"
1985 "}"));
1986 verifyFormat("enum E { // comment\n"
1987 " ONE,\n"
1988 " TWO\n"
1989 "};\n"
1990 "int i;");
1991
1992 FormatStyle EightIndent = getLLVMStyle();
1993 EightIndent.IndentWidth = 8;
1994 verifyFormat("enum {\n"
1995 " VOID,\n"
1996 " CHAR,\n"
1997 " SHORT,\n"
1998 " INT,\n"
1999 " LONG,\n"
2000 " SIGNED,\n"
2001 " UNSIGNED,\n"
2002 " BOOL,\n"
2003 " FLOAT,\n"
2004 " DOUBLE,\n"
2005 " COMPLEX\n"
2006 "};",
2007 EightIndent);
2008
2009 // Not enums.
2010 verifyFormat("enum X f() {\n"
2011 " a();\n"
2012 " return 42;\n"
2013 "}");
2014 verifyFormat("enum X Type::f() {\n"
2015 " a();\n"
2016 " return 42;\n"
2017 "}");
2018 verifyFormat("enum ::X f() {\n"
2019 " a();\n"
2020 " return 42;\n"
2021 "}");
2022 verifyFormat("enum ns::X f() {\n"
2023 " a();\n"
2024 " return 42;\n"
2025 "}");
2026 }
2027
TEST_F(FormatTest,FormatsEnumsWithErrors)2028 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2029 verifyFormat("enum Type {\n"
2030 " One = 0; // These semicolons should be commas.\n"
2031 " Two = 1;\n"
2032 "};");
2033 verifyFormat("namespace n {\n"
2034 "enum Type {\n"
2035 " One,\n"
2036 " Two, // missing };\n"
2037 " int i;\n"
2038 "}\n"
2039 "void g() {}");
2040 }
2041
TEST_F(FormatTest,FormatsEnumStruct)2042 TEST_F(FormatTest, FormatsEnumStruct) {
2043 verifyFormat("enum struct {\n"
2044 " Zero,\n"
2045 " One = 1,\n"
2046 " Two = One + 1,\n"
2047 " Three = (One + Two),\n"
2048 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2049 " Five = (One, Two, Three, Four, 5)\n"
2050 "};");
2051 verifyFormat("enum struct Enum {};");
2052 verifyFormat("enum struct {};");
2053 verifyFormat("enum struct X E {} d;");
2054 verifyFormat("enum struct __attribute__((...)) E {} d;");
2055 verifyFormat("enum struct __declspec__((...)) E {} d;");
2056 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
2057 }
2058
TEST_F(FormatTest,FormatsEnumClass)2059 TEST_F(FormatTest, FormatsEnumClass) {
2060 verifyFormat("enum class {\n"
2061 " Zero,\n"
2062 " One = 1,\n"
2063 " Two = One + 1,\n"
2064 " Three = (One + Two),\n"
2065 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2066 " Five = (One, Two, Three, Four, 5)\n"
2067 "};");
2068 verifyFormat("enum class Enum {};");
2069 verifyFormat("enum class {};");
2070 verifyFormat("enum class X E {} d;");
2071 verifyFormat("enum class __attribute__((...)) E {} d;");
2072 verifyFormat("enum class __declspec__((...)) E {} d;");
2073 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
2074 }
2075
TEST_F(FormatTest,FormatsEnumTypes)2076 TEST_F(FormatTest, FormatsEnumTypes) {
2077 verifyFormat("enum X : int {\n"
2078 " A, // Force multiple lines.\n"
2079 " B\n"
2080 "};");
2081 verifyFormat("enum X : int { A, B };");
2082 verifyFormat("enum X : std::uint32_t { A, B };");
2083 }
2084
TEST_F(FormatTest,FormatsTypedefEnum)2085 TEST_F(FormatTest, FormatsTypedefEnum) {
2086 FormatStyle Style = getLLVMStyle();
2087 Style.ColumnLimit = 40;
2088 verifyFormat("typedef enum {} EmptyEnum;");
2089 verifyFormat("typedef enum { A, B, C } ShortEnum;");
2090 verifyFormat("typedef enum {\n"
2091 " ZERO = 0,\n"
2092 " ONE = 1,\n"
2093 " TWO = 2,\n"
2094 " THREE = 3\n"
2095 "} LongEnum;",
2096 Style);
2097 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2098 Style.BraceWrapping.AfterEnum = true;
2099 verifyFormat("typedef enum {} EmptyEnum;");
2100 verifyFormat("typedef enum { A, B, C } ShortEnum;");
2101 verifyFormat("typedef enum\n"
2102 "{\n"
2103 " ZERO = 0,\n"
2104 " ONE = 1,\n"
2105 " TWO = 2,\n"
2106 " THREE = 3\n"
2107 "} LongEnum;",
2108 Style);
2109 }
2110
TEST_F(FormatTest,FormatsNSEnums)2111 TEST_F(FormatTest, FormatsNSEnums) {
2112 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2113 verifyGoogleFormat(
2114 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
2115 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2116 " // Information about someDecentlyLongValue.\n"
2117 " someDecentlyLongValue,\n"
2118 " // Information about anotherDecentlyLongValue.\n"
2119 " anotherDecentlyLongValue,\n"
2120 " // Information about aThirdDecentlyLongValue.\n"
2121 " aThirdDecentlyLongValue\n"
2122 "};");
2123 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
2124 " // Information about someDecentlyLongValue.\n"
2125 " someDecentlyLongValue,\n"
2126 " // Information about anotherDecentlyLongValue.\n"
2127 " anotherDecentlyLongValue,\n"
2128 " // Information about aThirdDecentlyLongValue.\n"
2129 " aThirdDecentlyLongValue\n"
2130 "};");
2131 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2132 " a = 1,\n"
2133 " b = 2,\n"
2134 " c = 3,\n"
2135 "};");
2136 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2137 " a = 1,\n"
2138 " b = 2,\n"
2139 " c = 3,\n"
2140 "};");
2141 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
2142 " a = 1,\n"
2143 " b = 2,\n"
2144 " c = 3,\n"
2145 "};");
2146 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2147 " a = 1,\n"
2148 " b = 2,\n"
2149 " c = 3,\n"
2150 "};");
2151 }
2152
TEST_F(FormatTest,FormatsBitfields)2153 TEST_F(FormatTest, FormatsBitfields) {
2154 verifyFormat("struct Bitfields {\n"
2155 " unsigned sClass : 8;\n"
2156 " unsigned ValueKind : 2;\n"
2157 "};");
2158 verifyFormat("struct A {\n"
2159 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2160 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2161 "};");
2162 verifyFormat("struct MyStruct {\n"
2163 " uchar data;\n"
2164 " uchar : 8;\n"
2165 " uchar : 8;\n"
2166 " uchar other;\n"
2167 "};");
2168 }
2169
TEST_F(FormatTest,FormatsNamespaces)2170 TEST_F(FormatTest, FormatsNamespaces) {
2171 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
2172 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
2173
2174 verifyFormat("namespace some_namespace {\n"
2175 "class A {};\n"
2176 "void f() { f(); }\n"
2177 "}",
2178 LLVMWithNoNamespaceFix);
2179 verifyFormat("namespace N::inline D {\n"
2180 "class A {};\n"
2181 "void f() { f(); }\n"
2182 "}",
2183 LLVMWithNoNamespaceFix);
2184 verifyFormat("namespace N::inline D::E {\n"
2185 "class A {};\n"
2186 "void f() { f(); }\n"
2187 "}",
2188 LLVMWithNoNamespaceFix);
2189 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
2190 "class A {};\n"
2191 "void f() { f(); }\n"
2192 "}",
2193 LLVMWithNoNamespaceFix);
2194 verifyFormat("/* something */ namespace some_namespace {\n"
2195 "class A {};\n"
2196 "void f() { f(); }\n"
2197 "}",
2198 LLVMWithNoNamespaceFix);
2199 verifyFormat("namespace {\n"
2200 "class A {};\n"
2201 "void f() { f(); }\n"
2202 "}",
2203 LLVMWithNoNamespaceFix);
2204 verifyFormat("/* something */ namespace {\n"
2205 "class A {};\n"
2206 "void f() { f(); }\n"
2207 "}",
2208 LLVMWithNoNamespaceFix);
2209 verifyFormat("inline namespace X {\n"
2210 "class A {};\n"
2211 "void f() { f(); }\n"
2212 "}",
2213 LLVMWithNoNamespaceFix);
2214 verifyFormat("/* something */ inline namespace X {\n"
2215 "class A {};\n"
2216 "void f() { f(); }\n"
2217 "}",
2218 LLVMWithNoNamespaceFix);
2219 verifyFormat("export namespace X {\n"
2220 "class A {};\n"
2221 "void f() { f(); }\n"
2222 "}",
2223 LLVMWithNoNamespaceFix);
2224 verifyFormat("using namespace some_namespace;\n"
2225 "class A {};\n"
2226 "void f() { f(); }",
2227 LLVMWithNoNamespaceFix);
2228
2229 // This code is more common than we thought; if we
2230 // layout this correctly the semicolon will go into
2231 // its own line, which is undesirable.
2232 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
2233 verifyFormat("namespace {\n"
2234 "class A {};\n"
2235 "};",
2236 LLVMWithNoNamespaceFix);
2237
2238 verifyFormat("namespace {\n"
2239 "int SomeVariable = 0; // comment\n"
2240 "} // namespace",
2241 LLVMWithNoNamespaceFix);
2242 EXPECT_EQ("#ifndef HEADER_GUARD\n"
2243 "#define HEADER_GUARD\n"
2244 "namespace my_namespace {\n"
2245 "int i;\n"
2246 "} // my_namespace\n"
2247 "#endif // HEADER_GUARD",
2248 format("#ifndef HEADER_GUARD\n"
2249 " #define HEADER_GUARD\n"
2250 " namespace my_namespace {\n"
2251 "int i;\n"
2252 "} // my_namespace\n"
2253 "#endif // HEADER_GUARD",
2254 LLVMWithNoNamespaceFix));
2255
2256 EXPECT_EQ("namespace A::B {\n"
2257 "class C {};\n"
2258 "}",
2259 format("namespace A::B {\n"
2260 "class C {};\n"
2261 "}",
2262 LLVMWithNoNamespaceFix));
2263
2264 FormatStyle Style = getLLVMStyle();
2265 Style.NamespaceIndentation = FormatStyle::NI_All;
2266 EXPECT_EQ("namespace out {\n"
2267 " int i;\n"
2268 " namespace in {\n"
2269 " int i;\n"
2270 " } // namespace in\n"
2271 "} // namespace out",
2272 format("namespace out {\n"
2273 "int i;\n"
2274 "namespace in {\n"
2275 "int i;\n"
2276 "} // namespace in\n"
2277 "} // namespace out",
2278 Style));
2279
2280 Style.NamespaceIndentation = FormatStyle::NI_Inner;
2281 EXPECT_EQ("namespace out {\n"
2282 "int i;\n"
2283 "namespace in {\n"
2284 " int i;\n"
2285 "} // namespace in\n"
2286 "} // namespace out",
2287 format("namespace out {\n"
2288 "int i;\n"
2289 "namespace in {\n"
2290 "int i;\n"
2291 "} // namespace in\n"
2292 "} // namespace out",
2293 Style));
2294 }
2295
TEST_F(FormatTest,NamespaceMacros)2296 TEST_F(FormatTest, NamespaceMacros) {
2297 FormatStyle Style = getLLVMStyle();
2298 Style.NamespaceMacros.push_back("TESTSUITE");
2299
2300 verifyFormat("TESTSUITE(A) {\n"
2301 "int foo();\n"
2302 "} // TESTSUITE(A)",
2303 Style);
2304
2305 verifyFormat("TESTSUITE(A, B) {\n"
2306 "int foo();\n"
2307 "} // TESTSUITE(A)",
2308 Style);
2309
2310 // Properly indent according to NamespaceIndentation style
2311 Style.NamespaceIndentation = FormatStyle::NI_All;
2312 verifyFormat("TESTSUITE(A) {\n"
2313 " int foo();\n"
2314 "} // TESTSUITE(A)",
2315 Style);
2316 verifyFormat("TESTSUITE(A) {\n"
2317 " namespace B {\n"
2318 " int foo();\n"
2319 " } // namespace B\n"
2320 "} // TESTSUITE(A)",
2321 Style);
2322 verifyFormat("namespace A {\n"
2323 " TESTSUITE(B) {\n"
2324 " int foo();\n"
2325 " } // TESTSUITE(B)\n"
2326 "} // namespace A",
2327 Style);
2328
2329 Style.NamespaceIndentation = FormatStyle::NI_Inner;
2330 verifyFormat("TESTSUITE(A) {\n"
2331 "TESTSUITE(B) {\n"
2332 " int foo();\n"
2333 "} // TESTSUITE(B)\n"
2334 "} // TESTSUITE(A)",
2335 Style);
2336 verifyFormat("TESTSUITE(A) {\n"
2337 "namespace B {\n"
2338 " int foo();\n"
2339 "} // namespace B\n"
2340 "} // TESTSUITE(A)",
2341 Style);
2342 verifyFormat("namespace A {\n"
2343 "TESTSUITE(B) {\n"
2344 " int foo();\n"
2345 "} // TESTSUITE(B)\n"
2346 "} // namespace A",
2347 Style);
2348
2349 // Properly merge namespace-macros blocks in CompactNamespaces mode
2350 Style.NamespaceIndentation = FormatStyle::NI_None;
2351 Style.CompactNamespaces = true;
2352 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
2353 "}} // TESTSUITE(A::B)",
2354 Style);
2355
2356 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
2357 "}} // TESTSUITE(out::in)",
2358 format("TESTSUITE(out) {\n"
2359 "TESTSUITE(in) {\n"
2360 "} // TESTSUITE(in)\n"
2361 "} // TESTSUITE(out)",
2362 Style));
2363
2364 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
2365 "}} // TESTSUITE(out::in)",
2366 format("TESTSUITE(out) {\n"
2367 "TESTSUITE(in) {\n"
2368 "} // TESTSUITE(in)\n"
2369 "} // TESTSUITE(out)",
2370 Style));
2371
2372 // Do not merge different namespaces/macros
2373 EXPECT_EQ("namespace out {\n"
2374 "TESTSUITE(in) {\n"
2375 "} // TESTSUITE(in)\n"
2376 "} // namespace out",
2377 format("namespace out {\n"
2378 "TESTSUITE(in) {\n"
2379 "} // TESTSUITE(in)\n"
2380 "} // namespace out",
2381 Style));
2382 EXPECT_EQ("TESTSUITE(out) {\n"
2383 "namespace in {\n"
2384 "} // namespace in\n"
2385 "} // TESTSUITE(out)",
2386 format("TESTSUITE(out) {\n"
2387 "namespace in {\n"
2388 "} // namespace in\n"
2389 "} // TESTSUITE(out)",
2390 Style));
2391 Style.NamespaceMacros.push_back("FOOBAR");
2392 EXPECT_EQ("TESTSUITE(out) {\n"
2393 "FOOBAR(in) {\n"
2394 "} // FOOBAR(in)\n"
2395 "} // TESTSUITE(out)",
2396 format("TESTSUITE(out) {\n"
2397 "FOOBAR(in) {\n"
2398 "} // FOOBAR(in)\n"
2399 "} // TESTSUITE(out)",
2400 Style));
2401 }
2402
TEST_F(FormatTest,FormatsCompactNamespaces)2403 TEST_F(FormatTest, FormatsCompactNamespaces) {
2404 FormatStyle Style = getLLVMStyle();
2405 Style.CompactNamespaces = true;
2406 Style.NamespaceMacros.push_back("TESTSUITE");
2407
2408 verifyFormat("namespace A { namespace B {\n"
2409 "}} // namespace A::B",
2410 Style);
2411
2412 EXPECT_EQ("namespace out { namespace in {\n"
2413 "}} // namespace out::in",
2414 format("namespace out {\n"
2415 "namespace in {\n"
2416 "} // namespace in\n"
2417 "} // namespace out",
2418 Style));
2419
2420 // Only namespaces which have both consecutive opening and end get compacted
2421 EXPECT_EQ("namespace out {\n"
2422 "namespace in1 {\n"
2423 "} // namespace in1\n"
2424 "namespace in2 {\n"
2425 "} // namespace in2\n"
2426 "} // namespace out",
2427 format("namespace out {\n"
2428 "namespace in1 {\n"
2429 "} // namespace in1\n"
2430 "namespace in2 {\n"
2431 "} // namespace in2\n"
2432 "} // namespace out",
2433 Style));
2434
2435 EXPECT_EQ("namespace out {\n"
2436 "int i;\n"
2437 "namespace in {\n"
2438 "int j;\n"
2439 "} // namespace in\n"
2440 "int k;\n"
2441 "} // namespace out",
2442 format("namespace out { int i;\n"
2443 "namespace in { int j; } // namespace in\n"
2444 "int k; } // namespace out",
2445 Style));
2446
2447 EXPECT_EQ("namespace A { namespace B { namespace C {\n"
2448 "}}} // namespace A::B::C\n",
2449 format("namespace A { namespace B {\n"
2450 "namespace C {\n"
2451 "}} // namespace B::C\n"
2452 "} // namespace A\n",
2453 Style));
2454
2455 Style.ColumnLimit = 40;
2456 EXPECT_EQ("namespace aaaaaaaaaa {\n"
2457 "namespace bbbbbbbbbb {\n"
2458 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
2459 format("namespace aaaaaaaaaa {\n"
2460 "namespace bbbbbbbbbb {\n"
2461 "} // namespace bbbbbbbbbb\n"
2462 "} // namespace aaaaaaaaaa",
2463 Style));
2464
2465 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
2466 "namespace cccccc {\n"
2467 "}}} // namespace aaaaaa::bbbbbb::cccccc",
2468 format("namespace aaaaaa {\n"
2469 "namespace bbbbbb {\n"
2470 "namespace cccccc {\n"
2471 "} // namespace cccccc\n"
2472 "} // namespace bbbbbb\n"
2473 "} // namespace aaaaaa",
2474 Style));
2475 Style.ColumnLimit = 80;
2476
2477 // Extra semicolon after 'inner' closing brace prevents merging
2478 EXPECT_EQ("namespace out { namespace in {\n"
2479 "}; } // namespace out::in",
2480 format("namespace out {\n"
2481 "namespace in {\n"
2482 "}; // namespace in\n"
2483 "} // namespace out",
2484 Style));
2485
2486 // Extra semicolon after 'outer' closing brace is conserved
2487 EXPECT_EQ("namespace out { namespace in {\n"
2488 "}}; // namespace out::in",
2489 format("namespace out {\n"
2490 "namespace in {\n"
2491 "} // namespace in\n"
2492 "}; // namespace out",
2493 Style));
2494
2495 Style.NamespaceIndentation = FormatStyle::NI_All;
2496 EXPECT_EQ("namespace out { namespace in {\n"
2497 " int i;\n"
2498 "}} // namespace out::in",
2499 format("namespace out {\n"
2500 "namespace in {\n"
2501 "int i;\n"
2502 "} // namespace in\n"
2503 "} // namespace out",
2504 Style));
2505 EXPECT_EQ("namespace out { namespace mid {\n"
2506 " namespace in {\n"
2507 " int j;\n"
2508 " } // namespace in\n"
2509 " int k;\n"
2510 "}} // namespace out::mid",
2511 format("namespace out { namespace mid {\n"
2512 "namespace in { int j; } // namespace in\n"
2513 "int k; }} // namespace out::mid",
2514 Style));
2515
2516 Style.NamespaceIndentation = FormatStyle::NI_Inner;
2517 EXPECT_EQ("namespace out { namespace in {\n"
2518 " int i;\n"
2519 "}} // namespace out::in",
2520 format("namespace out {\n"
2521 "namespace in {\n"
2522 "int i;\n"
2523 "} // namespace in\n"
2524 "} // namespace out",
2525 Style));
2526 EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
2527 " int i;\n"
2528 "}}} // namespace out::mid::in",
2529 format("namespace out {\n"
2530 "namespace mid {\n"
2531 "namespace in {\n"
2532 "int i;\n"
2533 "} // namespace in\n"
2534 "} // namespace mid\n"
2535 "} // namespace out",
2536 Style));
2537 }
2538
TEST_F(FormatTest,FormatsExternC)2539 TEST_F(FormatTest, FormatsExternC) {
2540 verifyFormat("extern \"C\" {\nint a;");
2541 verifyFormat("extern \"C\" {}");
2542 verifyFormat("extern \"C\" {\n"
2543 "int foo();\n"
2544 "}");
2545 verifyFormat("extern \"C\" int foo() {}");
2546 verifyFormat("extern \"C\" int foo();");
2547 verifyFormat("extern \"C\" int foo() {\n"
2548 " int i = 42;\n"
2549 " return i;\n"
2550 "}");
2551
2552 FormatStyle Style = getLLVMStyle();
2553 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2554 Style.BraceWrapping.AfterFunction = true;
2555 verifyFormat("extern \"C\" int foo() {}", Style);
2556 verifyFormat("extern \"C\" int foo();", Style);
2557 verifyFormat("extern \"C\" int foo()\n"
2558 "{\n"
2559 " int i = 42;\n"
2560 " return i;\n"
2561 "}",
2562 Style);
2563
2564 Style.BraceWrapping.AfterExternBlock = true;
2565 Style.BraceWrapping.SplitEmptyRecord = false;
2566 verifyFormat("extern \"C\"\n"
2567 "{}",
2568 Style);
2569 verifyFormat("extern \"C\"\n"
2570 "{\n"
2571 " int foo();\n"
2572 "}",
2573 Style);
2574 }
2575
TEST_F(FormatTest,IndentExternBlockStyle)2576 TEST_F(FormatTest, IndentExternBlockStyle) {
2577 FormatStyle Style = getLLVMStyle();
2578 Style.IndentWidth = 2;
2579
2580 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
2581 verifyFormat("extern \"C\" { /*9*/\n}", Style);
2582 verifyFormat("extern \"C\" {\n"
2583 " int foo10();\n"
2584 "}",
2585 Style);
2586
2587 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
2588 verifyFormat("extern \"C\" { /*11*/\n}", Style);
2589 verifyFormat("extern \"C\" {\n"
2590 "int foo12();\n"
2591 "}",
2592 Style);
2593
2594 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
2595 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2596 Style.BraceWrapping.AfterExternBlock = true;
2597 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style);
2598 verifyFormat("extern \"C\"\n{\n"
2599 " int foo14();\n"
2600 "}",
2601 Style);
2602
2603 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock;
2604 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2605 Style.BraceWrapping.AfterExternBlock = false;
2606 verifyFormat("extern \"C\" { /*15*/\n}", Style);
2607 verifyFormat("extern \"C\" {\n"
2608 "int foo16();\n"
2609 "}",
2610 Style);
2611 }
2612
TEST_F(FormatTest,FormatsInlineASM)2613 TEST_F(FormatTest, FormatsInlineASM) {
2614 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2615 verifyFormat("asm(\"nop\" ::: \"memory\");");
2616 verifyFormat(
2617 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2618 " \"cpuid\\n\\t\"\n"
2619 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2620 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2621 " : \"a\"(value));");
2622 EXPECT_EQ(
2623 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2624 " __asm {\n"
2625 " mov edx,[that] // vtable in edx\n"
2626 " mov eax,methodIndex\n"
2627 " call [edx][eax*4] // stdcall\n"
2628 " }\n"
2629 "}",
2630 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2631 " __asm {\n"
2632 " mov edx,[that] // vtable in edx\n"
2633 " mov eax,methodIndex\n"
2634 " call [edx][eax*4] // stdcall\n"
2635 " }\n"
2636 "}"));
2637 EXPECT_EQ("_asm {\n"
2638 " xor eax, eax;\n"
2639 " cpuid;\n"
2640 "}",
2641 format("_asm {\n"
2642 " xor eax, eax;\n"
2643 " cpuid;\n"
2644 "}"));
2645 verifyFormat("void function() {\n"
2646 " // comment\n"
2647 " asm(\"\");\n"
2648 "}");
2649 EXPECT_EQ("__asm {\n"
2650 "}\n"
2651 "int i;",
2652 format("__asm {\n"
2653 "}\n"
2654 "int i;"));
2655 }
2656
TEST_F(FormatTest,FormatTryCatch)2657 TEST_F(FormatTest, FormatTryCatch) {
2658 verifyFormat("try {\n"
2659 " throw a * b;\n"
2660 "} catch (int a) {\n"
2661 " // Do nothing.\n"
2662 "} catch (...) {\n"
2663 " exit(42);\n"
2664 "}");
2665
2666 // Function-level try statements.
2667 verifyFormat("int f() try { return 4; } catch (...) {\n"
2668 " return 5;\n"
2669 "}");
2670 verifyFormat("class A {\n"
2671 " int a;\n"
2672 " A() try : a(0) {\n"
2673 " } catch (...) {\n"
2674 " throw;\n"
2675 " }\n"
2676 "};\n");
2677
2678 // Incomplete try-catch blocks.
2679 verifyIncompleteFormat("try {} catch (");
2680 }
2681
TEST_F(FormatTest,FormatTryAsAVariable)2682 TEST_F(FormatTest, FormatTryAsAVariable) {
2683 verifyFormat("int try;");
2684 verifyFormat("int try, size;");
2685 verifyFormat("try = foo();");
2686 verifyFormat("if (try < size) {\n return true;\n}");
2687
2688 verifyFormat("int catch;");
2689 verifyFormat("int catch, size;");
2690 verifyFormat("catch = foo();");
2691 verifyFormat("if (catch < size) {\n return true;\n}");
2692 }
2693
TEST_F(FormatTest,FormatSEHTryCatch)2694 TEST_F(FormatTest, FormatSEHTryCatch) {
2695 verifyFormat("__try {\n"
2696 " int a = b * c;\n"
2697 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2698 " // Do nothing.\n"
2699 "}");
2700
2701 verifyFormat("__try {\n"
2702 " int a = b * c;\n"
2703 "} __finally {\n"
2704 " // Do nothing.\n"
2705 "}");
2706
2707 verifyFormat("DEBUG({\n"
2708 " __try {\n"
2709 " } __finally {\n"
2710 " }\n"
2711 "});\n");
2712 }
2713
TEST_F(FormatTest,IncompleteTryCatchBlocks)2714 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2715 verifyFormat("try {\n"
2716 " f();\n"
2717 "} catch {\n"
2718 " g();\n"
2719 "}");
2720 verifyFormat("try {\n"
2721 " f();\n"
2722 "} catch (A a) MACRO(x) {\n"
2723 " g();\n"
2724 "} catch (B b) MACRO(x) {\n"
2725 " g();\n"
2726 "}");
2727 }
2728
TEST_F(FormatTest,FormatTryCatchBraceStyles)2729 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2730 FormatStyle Style = getLLVMStyle();
2731 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2732 FormatStyle::BS_WebKit}) {
2733 Style.BreakBeforeBraces = BraceStyle;
2734 verifyFormat("try {\n"
2735 " // something\n"
2736 "} catch (...) {\n"
2737 " // something\n"
2738 "}",
2739 Style);
2740 }
2741 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2742 verifyFormat("try {\n"
2743 " // something\n"
2744 "}\n"
2745 "catch (...) {\n"
2746 " // something\n"
2747 "}",
2748 Style);
2749 verifyFormat("__try {\n"
2750 " // something\n"
2751 "}\n"
2752 "__finally {\n"
2753 " // something\n"
2754 "}",
2755 Style);
2756 verifyFormat("@try {\n"
2757 " // something\n"
2758 "}\n"
2759 "@finally {\n"
2760 " // something\n"
2761 "}",
2762 Style);
2763 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2764 verifyFormat("try\n"
2765 "{\n"
2766 " // something\n"
2767 "}\n"
2768 "catch (...)\n"
2769 "{\n"
2770 " // something\n"
2771 "}",
2772 Style);
2773 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
2774 verifyFormat("try\n"
2775 " {\n"
2776 " // something white\n"
2777 " }\n"
2778 "catch (...)\n"
2779 " {\n"
2780 " // something white\n"
2781 " }",
2782 Style);
2783 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2784 verifyFormat("try\n"
2785 " {\n"
2786 " // something\n"
2787 " }\n"
2788 "catch (...)\n"
2789 " {\n"
2790 " // something\n"
2791 " }",
2792 Style);
2793 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2794 Style.BraceWrapping.BeforeCatch = true;
2795 verifyFormat("try {\n"
2796 " // something\n"
2797 "}\n"
2798 "catch (...) {\n"
2799 " // something\n"
2800 "}",
2801 Style);
2802 }
2803
TEST_F(FormatTest,StaticInitializers)2804 TEST_F(FormatTest, StaticInitializers) {
2805 verifyFormat("static SomeClass SC = {1, 'a'};");
2806
2807 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2808 " 100000000, "
2809 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2810
2811 // Here, everything other than the "}" would fit on a line.
2812 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2813 " 10000000000000000000000000};");
2814 EXPECT_EQ("S s = {a,\n"
2815 "\n"
2816 " b};",
2817 format("S s = {\n"
2818 " a,\n"
2819 "\n"
2820 " b\n"
2821 "};"));
2822
2823 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2824 // line. However, the formatting looks a bit off and this probably doesn't
2825 // happen often in practice.
2826 verifyFormat("static int Variable[1] = {\n"
2827 " {1000000000000000000000000000000000000}};",
2828 getLLVMStyleWithColumns(40));
2829 }
2830
TEST_F(FormatTest,DesignatedInitializers)2831 TEST_F(FormatTest, DesignatedInitializers) {
2832 verifyFormat("const struct A a = {.a = 1, .b = 2};");
2833 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2834 " .bbbbbbbbbb = 2,\n"
2835 " .cccccccccc = 3,\n"
2836 " .dddddddddd = 4,\n"
2837 " .eeeeeeeeee = 5};");
2838 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2839 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2840 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2841 " .ccccccccccccccccccccccccccc = 3,\n"
2842 " .ddddddddddddddddddddddddddd = 4,\n"
2843 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2844
2845 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2846
2847 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
2848 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
2849 " [2] = bbbbbbbbbb,\n"
2850 " [3] = cccccccccc,\n"
2851 " [4] = dddddddddd,\n"
2852 " [5] = eeeeeeeeee};");
2853 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2854 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2855 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2856 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
2857 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
2858 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
2859 }
2860
TEST_F(FormatTest,NestedStaticInitializers)2861 TEST_F(FormatTest, NestedStaticInitializers) {
2862 verifyFormat("static A x = {{{}}};\n");
2863 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2864 " {init1, init2, init3, init4}}};",
2865 getLLVMStyleWithColumns(50));
2866
2867 verifyFormat("somes Status::global_reps[3] = {\n"
2868 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2869 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2870 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2871 getLLVMStyleWithColumns(60));
2872 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2873 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2874 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2875 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2876 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2877 " {rect.fRight - rect.fLeft, rect.fBottom - "
2878 "rect.fTop}};");
2879
2880 verifyFormat(
2881 "SomeArrayOfSomeType a = {\n"
2882 " {{1, 2, 3},\n"
2883 " {1, 2, 3},\n"
2884 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
2885 " 333333333333333333333333333333},\n"
2886 " {1, 2, 3},\n"
2887 " {1, 2, 3}}};");
2888 verifyFormat(
2889 "SomeArrayOfSomeType a = {\n"
2890 " {{1, 2, 3}},\n"
2891 " {{1, 2, 3}},\n"
2892 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2893 " 333333333333333333333333333333}},\n"
2894 " {{1, 2, 3}},\n"
2895 " {{1, 2, 3}}};");
2896
2897 verifyFormat("struct {\n"
2898 " unsigned bit;\n"
2899 " const char *const name;\n"
2900 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2901 " {kOsWin, \"Windows\"},\n"
2902 " {kOsLinux, \"Linux\"},\n"
2903 " {kOsCrOS, \"Chrome OS\"}};");
2904 verifyFormat("struct {\n"
2905 " unsigned bit;\n"
2906 " const char *const name;\n"
2907 "} kBitsToOs[] = {\n"
2908 " {kOsMac, \"Mac\"},\n"
2909 " {kOsWin, \"Windows\"},\n"
2910 " {kOsLinux, \"Linux\"},\n"
2911 " {kOsCrOS, \"Chrome OS\"},\n"
2912 "};");
2913 }
2914
TEST_F(FormatTest,FormatsSmallMacroDefinitionsInSingleLine)2915 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2916 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2917 " \\\n"
2918 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2919 }
2920
TEST_F(FormatTest,DoesNotBreakPureVirtualFunctionDefinition)2921 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2922 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2923 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
2924
2925 // Do break defaulted and deleted functions.
2926 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2927 " default;",
2928 getLLVMStyleWithColumns(40));
2929 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2930 " delete;",
2931 getLLVMStyleWithColumns(40));
2932 }
2933
TEST_F(FormatTest,BreaksStringLiteralsOnlyInDefine)2934 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2935 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2936 getLLVMStyleWithColumns(40));
2937 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2938 getLLVMStyleWithColumns(40));
2939 EXPECT_EQ("#define Q \\\n"
2940 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
2941 " \"aaaaaaaa.cpp\"",
2942 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2943 getLLVMStyleWithColumns(40)));
2944 }
2945
TEST_F(FormatTest,UnderstandsLinePPDirective)2946 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2947 EXPECT_EQ("# 123 \"A string literal\"",
2948 format(" # 123 \"A string literal\""));
2949 }
2950
TEST_F(FormatTest,LayoutUnknownPPDirective)2951 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2952 EXPECT_EQ("#;", format("#;"));
2953 verifyFormat("#\n;\n;\n;");
2954 }
2955
TEST_F(FormatTest,UnescapedEndOfLineEndsPPDirective)2956 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2957 EXPECT_EQ("#line 42 \"test\"\n",
2958 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
2959 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
2960 getLLVMStyleWithColumns(12)));
2961 }
2962
TEST_F(FormatTest,EndOfFileEndsPPDirective)2963 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2964 EXPECT_EQ("#line 42 \"test\"",
2965 format("# \\\n line \\\n 42 \\\n \"test\""));
2966 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
2967 }
2968
TEST_F(FormatTest,DoesntRemoveUnknownTokens)2969 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2970 verifyFormat("#define A \\x20");
2971 verifyFormat("#define A \\ x20");
2972 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
2973 verifyFormat("#define A ''");
2974 verifyFormat("#define A ''qqq");
2975 verifyFormat("#define A `qqq");
2976 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2977 EXPECT_EQ("const char *c = STRINGIFY(\n"
2978 "\\na : b);",
2979 format("const char * c = STRINGIFY(\n"
2980 "\\na : b);"));
2981
2982 verifyFormat("a\r\\");
2983 verifyFormat("a\v\\");
2984 verifyFormat("a\f\\");
2985 }
2986
TEST_F(FormatTest,IndentsPPDirectiveInReducedSpace)2987 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2988 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2989 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
2990 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
2991 // FIXME: We never break before the macro name.
2992 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
2993
2994 verifyFormat("#define A A\n#define A A");
2995 verifyFormat("#define A(X) A\n#define A A");
2996
2997 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2998 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
2999 }
3000
TEST_F(FormatTest,HandlePreprocessorDirectiveContext)3001 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
3002 EXPECT_EQ("// somecomment\n"
3003 "#include \"a.h\"\n"
3004 "#define A( \\\n"
3005 " A, B)\n"
3006 "#include \"b.h\"\n"
3007 "// somecomment\n",
3008 format(" // somecomment\n"
3009 " #include \"a.h\"\n"
3010 "#define A(A,\\\n"
3011 " B)\n"
3012 " #include \"b.h\"\n"
3013 " // somecomment\n",
3014 getLLVMStyleWithColumns(13)));
3015 }
3016
TEST_F(FormatTest,LayoutSingleHash)3017 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
3018
TEST_F(FormatTest,LayoutCodeInMacroDefinitions)3019 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
3020 EXPECT_EQ("#define A \\\n"
3021 " c; \\\n"
3022 " e;\n"
3023 "f;",
3024 format("#define A c; e;\n"
3025 "f;",
3026 getLLVMStyleWithColumns(14)));
3027 }
3028
TEST_F(FormatTest,LayoutRemainingTokens)3029 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
3030
TEST_F(FormatTest,MacroDefinitionInsideStatement)3031 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
3032 EXPECT_EQ("int x,\n"
3033 "#define A\n"
3034 " y;",
3035 format("int x,\n#define A\ny;"));
3036 }
3037
TEST_F(FormatTest,HashInMacroDefinition)3038 TEST_F(FormatTest, HashInMacroDefinition) {
3039 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
3040 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
3041 verifyFormat("#define A \\\n"
3042 " { \\\n"
3043 " f(#c); \\\n"
3044 " }",
3045 getLLVMStyleWithColumns(11));
3046
3047 verifyFormat("#define A(X) \\\n"
3048 " void function##X()",
3049 getLLVMStyleWithColumns(22));
3050
3051 verifyFormat("#define A(a, b, c) \\\n"
3052 " void a##b##c()",
3053 getLLVMStyleWithColumns(22));
3054
3055 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
3056 }
3057
TEST_F(FormatTest,RespectWhitespaceInMacroDefinitions)3058 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
3059 EXPECT_EQ("#define A (x)", format("#define A (x)"));
3060 EXPECT_EQ("#define A(x)", format("#define A(x)"));
3061
3062 FormatStyle Style = getLLVMStyle();
3063 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
3064 verifyFormat("#define true ((foo)1)", Style);
3065 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
3066 verifyFormat("#define false((foo)0)", Style);
3067 }
3068
TEST_F(FormatTest,EmptyLinesInMacroDefinitions)3069 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
3070 EXPECT_EQ("#define A b;", format("#define A \\\n"
3071 " \\\n"
3072 " b;",
3073 getLLVMStyleWithColumns(25)));
3074 EXPECT_EQ("#define A \\\n"
3075 " \\\n"
3076 " a; \\\n"
3077 " b;",
3078 format("#define A \\\n"
3079 " \\\n"
3080 " a; \\\n"
3081 " b;",
3082 getLLVMStyleWithColumns(11)));
3083 EXPECT_EQ("#define A \\\n"
3084 " a; \\\n"
3085 " \\\n"
3086 " b;",
3087 format("#define A \\\n"
3088 " a; \\\n"
3089 " \\\n"
3090 " b;",
3091 getLLVMStyleWithColumns(11)));
3092 }
3093
TEST_F(FormatTest,MacroDefinitionsWithIncompleteCode)3094 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
3095 verifyIncompleteFormat("#define A :");
3096 verifyFormat("#define SOMECASES \\\n"
3097 " case 1: \\\n"
3098 " case 2\n",
3099 getLLVMStyleWithColumns(20));
3100 verifyFormat("#define MACRO(a) \\\n"
3101 " if (a) \\\n"
3102 " f(); \\\n"
3103 " else \\\n"
3104 " g()",
3105 getLLVMStyleWithColumns(18));
3106 verifyFormat("#define A template <typename T>");
3107 verifyIncompleteFormat("#define STR(x) #x\n"
3108 "f(STR(this_is_a_string_literal{));");
3109 verifyFormat("#pragma omp threadprivate( \\\n"
3110 " y)), // expected-warning",
3111 getLLVMStyleWithColumns(28));
3112 verifyFormat("#d, = };");
3113 verifyFormat("#if \"a");
3114 verifyIncompleteFormat("({\n"
3115 "#define b \\\n"
3116 " } \\\n"
3117 " a\n"
3118 "a",
3119 getLLVMStyleWithColumns(15));
3120 verifyFormat("#define A \\\n"
3121 " { \\\n"
3122 " {\n"
3123 "#define B \\\n"
3124 " } \\\n"
3125 " }",
3126 getLLVMStyleWithColumns(15));
3127 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
3128 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
3129 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
3130 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
3131 }
3132
TEST_F(FormatTest,MacrosWithoutTrailingSemicolon)3133 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
3134 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
3135 EXPECT_EQ("class A : public QObject {\n"
3136 " Q_OBJECT\n"
3137 "\n"
3138 " A() {}\n"
3139 "};",
3140 format("class A : public QObject {\n"
3141 " Q_OBJECT\n"
3142 "\n"
3143 " A() {\n}\n"
3144 "} ;"));
3145 EXPECT_EQ("MACRO\n"
3146 "/*static*/ int i;",
3147 format("MACRO\n"
3148 " /*static*/ int i;"));
3149 EXPECT_EQ("SOME_MACRO\n"
3150 "namespace {\n"
3151 "void f();\n"
3152 "} // namespace",
3153 format("SOME_MACRO\n"
3154 " namespace {\n"
3155 "void f( );\n"
3156 "} // namespace"));
3157 // Only if the identifier contains at least 5 characters.
3158 EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
3159 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
3160 // Only if everything is upper case.
3161 EXPECT_EQ("class A : public QObject {\n"
3162 " Q_Object A() {}\n"
3163 "};",
3164 format("class A : public QObject {\n"
3165 " Q_Object\n"
3166 " A() {\n}\n"
3167 "} ;"));
3168
3169 // Only if the next line can actually start an unwrapped line.
3170 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
3171 format("SOME_WEIRD_LOG_MACRO\n"
3172 "<< SomeThing;"));
3173
3174 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
3175 "(n, buffers))\n",
3176 getChromiumStyle(FormatStyle::LK_Cpp));
3177
3178 // See PR41483
3179 EXPECT_EQ("/**/ FOO(a)\n"
3180 "FOO(b)",
3181 format("/**/ FOO(a)\n"
3182 "FOO(b)"));
3183 }
3184
TEST_F(FormatTest,MacroCallsWithoutTrailingSemicolon)3185 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
3186 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
3187 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
3188 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
3189 "class X {};\n"
3190 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
3191 "int *createScopDetectionPass() { return 0; }",
3192 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
3193 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
3194 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
3195 " class X {};\n"
3196 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
3197 " int *createScopDetectionPass() { return 0; }"));
3198 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
3199 // braces, so that inner block is indented one level more.
3200 EXPECT_EQ("int q() {\n"
3201 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
3202 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
3203 " IPC_END_MESSAGE_MAP()\n"
3204 "}",
3205 format("int q() {\n"
3206 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
3207 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
3208 " IPC_END_MESSAGE_MAP()\n"
3209 "}"));
3210
3211 // Same inside macros.
3212 EXPECT_EQ("#define LIST(L) \\\n"
3213 " L(A) \\\n"
3214 " L(B) \\\n"
3215 " L(C)",
3216 format("#define LIST(L) \\\n"
3217 " L(A) \\\n"
3218 " L(B) \\\n"
3219 " L(C)",
3220 getGoogleStyle()));
3221
3222 // These must not be recognized as macros.
3223 EXPECT_EQ("int q() {\n"
3224 " f(x);\n"
3225 " f(x) {}\n"
3226 " f(x)->g();\n"
3227 " f(x)->*g();\n"
3228 " f(x).g();\n"
3229 " f(x) = x;\n"
3230 " f(x) += x;\n"
3231 " f(x) -= x;\n"
3232 " f(x) *= x;\n"
3233 " f(x) /= x;\n"
3234 " f(x) %= x;\n"
3235 " f(x) &= x;\n"
3236 " f(x) |= x;\n"
3237 " f(x) ^= x;\n"
3238 " f(x) >>= x;\n"
3239 " f(x) <<= x;\n"
3240 " f(x)[y].z();\n"
3241 " LOG(INFO) << x;\n"
3242 " ifstream(x) >> x;\n"
3243 "}\n",
3244 format("int q() {\n"
3245 " f(x)\n;\n"
3246 " f(x)\n {}\n"
3247 " f(x)\n->g();\n"
3248 " f(x)\n->*g();\n"
3249 " f(x)\n.g();\n"
3250 " f(x)\n = x;\n"
3251 " f(x)\n += x;\n"
3252 " f(x)\n -= x;\n"
3253 " f(x)\n *= x;\n"
3254 " f(x)\n /= x;\n"
3255 " f(x)\n %= x;\n"
3256 " f(x)\n &= x;\n"
3257 " f(x)\n |= x;\n"
3258 " f(x)\n ^= x;\n"
3259 " f(x)\n >>= x;\n"
3260 " f(x)\n <<= x;\n"
3261 " f(x)\n[y].z();\n"
3262 " LOG(INFO)\n << x;\n"
3263 " ifstream(x)\n >> x;\n"
3264 "}\n"));
3265 EXPECT_EQ("int q() {\n"
3266 " F(x)\n"
3267 " if (1) {\n"
3268 " }\n"
3269 " F(x)\n"
3270 " while (1) {\n"
3271 " }\n"
3272 " F(x)\n"
3273 " G(x);\n"
3274 " F(x)\n"
3275 " try {\n"
3276 " Q();\n"
3277 " } catch (...) {\n"
3278 " }\n"
3279 "}\n",
3280 format("int q() {\n"
3281 "F(x)\n"
3282 "if (1) {}\n"
3283 "F(x)\n"
3284 "while (1) {}\n"
3285 "F(x)\n"
3286 "G(x);\n"
3287 "F(x)\n"
3288 "try { Q(); } catch (...) {}\n"
3289 "}\n"));
3290 EXPECT_EQ("class A {\n"
3291 " A() : t(0) {}\n"
3292 " A(int i) noexcept() : {}\n"
3293 " A(X x)\n" // FIXME: function-level try blocks are broken.
3294 " try : t(0) {\n"
3295 " } catch (...) {\n"
3296 " }\n"
3297 "};",
3298 format("class A {\n"
3299 " A()\n : t(0) {}\n"
3300 " A(int i)\n noexcept() : {}\n"
3301 " A(X x)\n"
3302 " try : t(0) {} catch (...) {}\n"
3303 "};"));
3304 FormatStyle Style = getLLVMStyle();
3305 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3306 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3307 Style.BraceWrapping.AfterFunction = true;
3308 EXPECT_EQ("void f()\n"
3309 "try\n"
3310 "{\n"
3311 "}",
3312 format("void f() try {\n"
3313 "}",
3314 Style));
3315 EXPECT_EQ("class SomeClass {\n"
3316 "public:\n"
3317 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3318 "};",
3319 format("class SomeClass {\n"
3320 "public:\n"
3321 " SomeClass()\n"
3322 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3323 "};"));
3324 EXPECT_EQ("class SomeClass {\n"
3325 "public:\n"
3326 " SomeClass()\n"
3327 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3328 "};",
3329 format("class SomeClass {\n"
3330 "public:\n"
3331 " SomeClass()\n"
3332 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
3333 "};",
3334 getLLVMStyleWithColumns(40)));
3335
3336 verifyFormat("MACRO(>)");
3337
3338 // Some macros contain an implicit semicolon.
3339 Style = getLLVMStyle();
3340 Style.StatementMacros.push_back("FOO");
3341 verifyFormat("FOO(a) int b = 0;");
3342 verifyFormat("FOO(a)\n"
3343 "int b = 0;",
3344 Style);
3345 verifyFormat("FOO(a);\n"
3346 "int b = 0;",
3347 Style);
3348 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
3349 "int b = 0;",
3350 Style);
3351 verifyFormat("FOO()\n"
3352 "int b = 0;",
3353 Style);
3354 verifyFormat("FOO\n"
3355 "int b = 0;",
3356 Style);
3357 verifyFormat("void f() {\n"
3358 " FOO(a)\n"
3359 " return a;\n"
3360 "}",
3361 Style);
3362 verifyFormat("FOO(a)\n"
3363 "FOO(b)",
3364 Style);
3365 verifyFormat("int a = 0;\n"
3366 "FOO(b)\n"
3367 "int c = 0;",
3368 Style);
3369 verifyFormat("int a = 0;\n"
3370 "int x = FOO(a)\n"
3371 "int b = 0;",
3372 Style);
3373 verifyFormat("void foo(int a) { FOO(a) }\n"
3374 "uint32_t bar() {}",
3375 Style);
3376 }
3377
TEST_F(FormatTest,LayoutMacroDefinitionsStatementsSpanningBlocks)3378 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
3379 verifyFormat("#define A \\\n"
3380 " f({ \\\n"
3381 " g(); \\\n"
3382 " });",
3383 getLLVMStyleWithColumns(11));
3384 }
3385
TEST_F(FormatTest,IndentPreprocessorDirectives)3386 TEST_F(FormatTest, IndentPreprocessorDirectives) {
3387 FormatStyle Style = getLLVMStyle();
3388 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
3389 Style.ColumnLimit = 40;
3390 verifyFormat("#ifdef _WIN32\n"
3391 "#define A 0\n"
3392 "#ifdef VAR2\n"
3393 "#define B 1\n"
3394 "#include <someheader.h>\n"
3395 "#define MACRO \\\n"
3396 " some_very_long_func_aaaaaaaaaa();\n"
3397 "#endif\n"
3398 "#else\n"
3399 "#define A 1\n"
3400 "#endif",
3401 Style);
3402 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
3403 verifyFormat("#ifdef _WIN32\n"
3404 "# define A 0\n"
3405 "# ifdef VAR2\n"
3406 "# define B 1\n"
3407 "# include <someheader.h>\n"
3408 "# define MACRO \\\n"
3409 " some_very_long_func_aaaaaaaaaa();\n"
3410 "# endif\n"
3411 "#else\n"
3412 "# define A 1\n"
3413 "#endif",
3414 Style);
3415 verifyFormat("#if A\n"
3416 "# define MACRO \\\n"
3417 " void a(int x) { \\\n"
3418 " b(); \\\n"
3419 " c(); \\\n"
3420 " d(); \\\n"
3421 " e(); \\\n"
3422 " f(); \\\n"
3423 " }\n"
3424 "#endif",
3425 Style);
3426 // Comments before include guard.
3427 verifyFormat("// file comment\n"
3428 "// file comment\n"
3429 "#ifndef HEADER_H\n"
3430 "#define HEADER_H\n"
3431 "code();\n"
3432 "#endif",
3433 Style);
3434 // Test with include guards.
3435 verifyFormat("#ifndef HEADER_H\n"
3436 "#define HEADER_H\n"
3437 "code();\n"
3438 "#endif",
3439 Style);
3440 // Include guards must have a #define with the same variable immediately
3441 // after #ifndef.
3442 verifyFormat("#ifndef NOT_GUARD\n"
3443 "# define FOO\n"
3444 "code();\n"
3445 "#endif",
3446 Style);
3447
3448 // Include guards must cover the entire file.
3449 verifyFormat("code();\n"
3450 "code();\n"
3451 "#ifndef NOT_GUARD\n"
3452 "# define NOT_GUARD\n"
3453 "code();\n"
3454 "#endif",
3455 Style);
3456 verifyFormat("#ifndef NOT_GUARD\n"
3457 "# define NOT_GUARD\n"
3458 "code();\n"
3459 "#endif\n"
3460 "code();",
3461 Style);
3462 // Test with trailing blank lines.
3463 verifyFormat("#ifndef HEADER_H\n"
3464 "#define HEADER_H\n"
3465 "code();\n"
3466 "#endif\n",
3467 Style);
3468 // Include guards don't have #else.
3469 verifyFormat("#ifndef NOT_GUARD\n"
3470 "# define NOT_GUARD\n"
3471 "code();\n"
3472 "#else\n"
3473 "#endif",
3474 Style);
3475 verifyFormat("#ifndef NOT_GUARD\n"
3476 "# define NOT_GUARD\n"
3477 "code();\n"
3478 "#elif FOO\n"
3479 "#endif",
3480 Style);
3481 // Non-identifier #define after potential include guard.
3482 verifyFormat("#ifndef FOO\n"
3483 "# define 1\n"
3484 "#endif\n",
3485 Style);
3486 // #if closes past last non-preprocessor line.
3487 verifyFormat("#ifndef FOO\n"
3488 "#define FOO\n"
3489 "#if 1\n"
3490 "int i;\n"
3491 "# define A 0\n"
3492 "#endif\n"
3493 "#endif\n",
3494 Style);
3495 // Don't crash if there is an #elif directive without a condition.
3496 verifyFormat("#if 1\n"
3497 "int x;\n"
3498 "#elif\n"
3499 "int y;\n"
3500 "#else\n"
3501 "int z;\n"
3502 "#endif",
3503 Style);
3504 // FIXME: This doesn't handle the case where there's code between the
3505 // #ifndef and #define but all other conditions hold. This is because when
3506 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
3507 // previous code line yet, so we can't detect it.
3508 EXPECT_EQ("#ifndef NOT_GUARD\n"
3509 "code();\n"
3510 "#define NOT_GUARD\n"
3511 "code();\n"
3512 "#endif",
3513 format("#ifndef NOT_GUARD\n"
3514 "code();\n"
3515 "# define NOT_GUARD\n"
3516 "code();\n"
3517 "#endif",
3518 Style));
3519 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
3520 // be outside an include guard. Examples are #pragma once and
3521 // #pragma GCC diagnostic, or anything else that does not change the meaning
3522 // of the file if it's included multiple times.
3523 EXPECT_EQ("#ifdef WIN32\n"
3524 "# pragma once\n"
3525 "#endif\n"
3526 "#ifndef HEADER_H\n"
3527 "# define HEADER_H\n"
3528 "code();\n"
3529 "#endif",
3530 format("#ifdef WIN32\n"
3531 "# pragma once\n"
3532 "#endif\n"
3533 "#ifndef HEADER_H\n"
3534 "#define HEADER_H\n"
3535 "code();\n"
3536 "#endif",
3537 Style));
3538 // FIXME: This does not detect when there is a single non-preprocessor line
3539 // in front of an include-guard-like structure where other conditions hold
3540 // because ScopedLineState hides the line.
3541 EXPECT_EQ("code();\n"
3542 "#ifndef HEADER_H\n"
3543 "#define HEADER_H\n"
3544 "code();\n"
3545 "#endif",
3546 format("code();\n"
3547 "#ifndef HEADER_H\n"
3548 "# define HEADER_H\n"
3549 "code();\n"
3550 "#endif",
3551 Style));
3552 // Keep comments aligned with #, otherwise indent comments normally. These
3553 // tests cannot use verifyFormat because messUp manipulates leading
3554 // whitespace.
3555 {
3556 const char *Expected = ""
3557 "void f() {\n"
3558 "#if 1\n"
3559 "// Preprocessor aligned.\n"
3560 "# define A 0\n"
3561 " // Code. Separated by blank line.\n"
3562 "\n"
3563 "# define B 0\n"
3564 " // Code. Not aligned with #\n"
3565 "# define C 0\n"
3566 "#endif";
3567 const char *ToFormat = ""
3568 "void f() {\n"
3569 "#if 1\n"
3570 "// Preprocessor aligned.\n"
3571 "# define A 0\n"
3572 "// Code. Separated by blank line.\n"
3573 "\n"
3574 "# define B 0\n"
3575 " // Code. Not aligned with #\n"
3576 "# define C 0\n"
3577 "#endif";
3578 EXPECT_EQ(Expected, format(ToFormat, Style));
3579 EXPECT_EQ(Expected, format(Expected, Style));
3580 }
3581 // Keep block quotes aligned.
3582 {
3583 const char *Expected = ""
3584 "void f() {\n"
3585 "#if 1\n"
3586 "/* Preprocessor aligned. */\n"
3587 "# define A 0\n"
3588 " /* Code. Separated by blank line. */\n"
3589 "\n"
3590 "# define B 0\n"
3591 " /* Code. Not aligned with # */\n"
3592 "# define C 0\n"
3593 "#endif";
3594 const char *ToFormat = ""
3595 "void f() {\n"
3596 "#if 1\n"
3597 "/* Preprocessor aligned. */\n"
3598 "# define A 0\n"
3599 "/* Code. Separated by blank line. */\n"
3600 "\n"
3601 "# define B 0\n"
3602 " /* Code. Not aligned with # */\n"
3603 "# define C 0\n"
3604 "#endif";
3605 EXPECT_EQ(Expected, format(ToFormat, Style));
3606 EXPECT_EQ(Expected, format(Expected, Style));
3607 }
3608 // Keep comments aligned with un-indented directives.
3609 {
3610 const char *Expected = ""
3611 "void f() {\n"
3612 "// Preprocessor aligned.\n"
3613 "#define A 0\n"
3614 " // Code. Separated by blank line.\n"
3615 "\n"
3616 "#define B 0\n"
3617 " // Code. Not aligned with #\n"
3618 "#define C 0\n";
3619 const char *ToFormat = ""
3620 "void f() {\n"
3621 "// Preprocessor aligned.\n"
3622 "#define A 0\n"
3623 "// Code. Separated by blank line.\n"
3624 "\n"
3625 "#define B 0\n"
3626 " // Code. Not aligned with #\n"
3627 "#define C 0\n";
3628 EXPECT_EQ(Expected, format(ToFormat, Style));
3629 EXPECT_EQ(Expected, format(Expected, Style));
3630 }
3631 // Test AfterHash with tabs.
3632 {
3633 FormatStyle Tabbed = Style;
3634 Tabbed.UseTab = FormatStyle::UT_Always;
3635 Tabbed.IndentWidth = 8;
3636 Tabbed.TabWidth = 8;
3637 verifyFormat("#ifdef _WIN32\n"
3638 "#\tdefine A 0\n"
3639 "#\tifdef VAR2\n"
3640 "#\t\tdefine B 1\n"
3641 "#\t\tinclude <someheader.h>\n"
3642 "#\t\tdefine MACRO \\\n"
3643 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
3644 "#\tendif\n"
3645 "#else\n"
3646 "#\tdefine A 1\n"
3647 "#endif",
3648 Tabbed);
3649 }
3650
3651 // Regression test: Multiline-macro inside include guards.
3652 verifyFormat("#ifndef HEADER_H\n"
3653 "#define HEADER_H\n"
3654 "#define A() \\\n"
3655 " int i; \\\n"
3656 " int j;\n"
3657 "#endif // HEADER_H",
3658 getLLVMStyleWithColumns(20));
3659
3660 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
3661 // Basic before hash indent tests
3662 verifyFormat("#ifdef _WIN32\n"
3663 " #define A 0\n"
3664 " #ifdef VAR2\n"
3665 " #define B 1\n"
3666 " #include <someheader.h>\n"
3667 " #define MACRO \\\n"
3668 " some_very_long_func_aaaaaaaaaa();\n"
3669 " #endif\n"
3670 "#else\n"
3671 " #define A 1\n"
3672 "#endif",
3673 Style);
3674 verifyFormat("#if A\n"
3675 " #define MACRO \\\n"
3676 " void a(int x) { \\\n"
3677 " b(); \\\n"
3678 " c(); \\\n"
3679 " d(); \\\n"
3680 " e(); \\\n"
3681 " f(); \\\n"
3682 " }\n"
3683 "#endif",
3684 Style);
3685 // Keep comments aligned with indented directives. These
3686 // tests cannot use verifyFormat because messUp manipulates leading
3687 // whitespace.
3688 {
3689 const char *Expected = "void f() {\n"
3690 "// Aligned to preprocessor.\n"
3691 "#if 1\n"
3692 " // Aligned to code.\n"
3693 " int a;\n"
3694 " #if 1\n"
3695 " // Aligned to preprocessor.\n"
3696 " #define A 0\n"
3697 " // Aligned to code.\n"
3698 " int b;\n"
3699 " #endif\n"
3700 "#endif\n"
3701 "}";
3702 const char *ToFormat = "void f() {\n"
3703 "// Aligned to preprocessor.\n"
3704 "#if 1\n"
3705 "// Aligned to code.\n"
3706 "int a;\n"
3707 "#if 1\n"
3708 "// Aligned to preprocessor.\n"
3709 "#define A 0\n"
3710 "// Aligned to code.\n"
3711 "int b;\n"
3712 "#endif\n"
3713 "#endif\n"
3714 "}";
3715 EXPECT_EQ(Expected, format(ToFormat, Style));
3716 EXPECT_EQ(Expected, format(Expected, Style));
3717 }
3718 {
3719 const char *Expected = "void f() {\n"
3720 "/* Aligned to preprocessor. */\n"
3721 "#if 1\n"
3722 " /* Aligned to code. */\n"
3723 " int a;\n"
3724 " #if 1\n"
3725 " /* Aligned to preprocessor. */\n"
3726 " #define A 0\n"
3727 " /* Aligned to code. */\n"
3728 " int b;\n"
3729 " #endif\n"
3730 "#endif\n"
3731 "}";
3732 const char *ToFormat = "void f() {\n"
3733 "/* Aligned to preprocessor. */\n"
3734 "#if 1\n"
3735 "/* Aligned to code. */\n"
3736 "int a;\n"
3737 "#if 1\n"
3738 "/* Aligned to preprocessor. */\n"
3739 "#define A 0\n"
3740 "/* Aligned to code. */\n"
3741 "int b;\n"
3742 "#endif\n"
3743 "#endif\n"
3744 "}";
3745 EXPECT_EQ(Expected, format(ToFormat, Style));
3746 EXPECT_EQ(Expected, format(Expected, Style));
3747 }
3748
3749 // Test single comment before preprocessor
3750 verifyFormat("// Comment\n"
3751 "\n"
3752 "#if 1\n"
3753 "#endif",
3754 Style);
3755 }
3756
TEST_F(FormatTest,FormatHashIfNotAtStartOfLine)3757 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3758 verifyFormat("{\n { a #c; }\n}");
3759 }
3760
TEST_F(FormatTest,FormatUnbalancedStructuralElements)3761 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3762 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
3763 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3764 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
3765 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3766 }
3767
TEST_F(FormatTest,EscapedNewlines)3768 TEST_F(FormatTest, EscapedNewlines) {
3769 FormatStyle Narrow = getLLVMStyleWithColumns(11);
3770 EXPECT_EQ("#define A \\\n int i; \\\n int j;",
3771 format("#define A \\\nint i;\\\n int j;", Narrow));
3772 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3773 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3774 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */"));
3775 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3776
3777 FormatStyle AlignLeft = getLLVMStyle();
3778 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
3779 EXPECT_EQ("#define MACRO(x) \\\n"
3780 "private: \\\n"
3781 " int x(int a);\n",
3782 format("#define MACRO(x) \\\n"
3783 "private: \\\n"
3784 " int x(int a);\n",
3785 AlignLeft));
3786
3787 // CRLF line endings
3788 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;",
3789 format("#define A \\\r\nint i;\\\r\n int j;", Narrow));
3790 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
3791 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3792 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */"));
3793 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
3794 EXPECT_EQ("#define MACRO(x) \\\r\n"
3795 "private: \\\r\n"
3796 " int x(int a);\r\n",
3797 format("#define MACRO(x) \\\r\n"
3798 "private: \\\r\n"
3799 " int x(int a);\r\n",
3800 AlignLeft));
3801
3802 FormatStyle DontAlign = getLLVMStyle();
3803 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
3804 DontAlign.MaxEmptyLinesToKeep = 3;
3805 // FIXME: can't use verifyFormat here because the newline before
3806 // "public:" is not inserted the first time it's reformatted
3807 EXPECT_EQ("#define A \\\n"
3808 " class Foo { \\\n"
3809 " void bar(); \\\n"
3810 "\\\n"
3811 "\\\n"
3812 "\\\n"
3813 " public: \\\n"
3814 " void baz(); \\\n"
3815 " };",
3816 format("#define A \\\n"
3817 " class Foo { \\\n"
3818 " void bar(); \\\n"
3819 "\\\n"
3820 "\\\n"
3821 "\\\n"
3822 " public: \\\n"
3823 " void baz(); \\\n"
3824 " };",
3825 DontAlign));
3826 }
3827
TEST_F(FormatTest,CalculateSpaceOnConsecutiveLinesInMacro)3828 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3829 verifyFormat("#define A \\\n"
3830 " int v( \\\n"
3831 " a); \\\n"
3832 " int i;",
3833 getLLVMStyleWithColumns(11));
3834 }
3835
TEST_F(FormatTest,MixingPreprocessorDirectivesAndNormalCode)3836 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3837 EXPECT_EQ(
3838 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3839 " \\\n"
3840 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3841 "\n"
3842 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3843 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3844 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
3845 "\\\n"
3846 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3847 " \n"
3848 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3849 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3850 }
3851
TEST_F(FormatTest,LayoutStatementsAroundPreprocessorDirectives)3852 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3853 EXPECT_EQ("int\n"
3854 "#define A\n"
3855 " a;",
3856 format("int\n#define A\na;"));
3857 verifyFormat("functionCallTo(\n"
3858 " someOtherFunction(\n"
3859 " withSomeParameters, whichInSequence,\n"
3860 " areLongerThanALine(andAnotherCall,\n"
3861 "#define A B\n"
3862 " withMoreParamters,\n"
3863 " whichStronglyInfluenceTheLayout),\n"
3864 " andMoreParameters),\n"
3865 " trailing);",
3866 getLLVMStyleWithColumns(69));
3867 verifyFormat("Foo::Foo()\n"
3868 "#ifdef BAR\n"
3869 " : baz(0)\n"
3870 "#endif\n"
3871 "{\n"
3872 "}");
3873 verifyFormat("void f() {\n"
3874 " if (true)\n"
3875 "#ifdef A\n"
3876 " f(42);\n"
3877 " x();\n"
3878 "#else\n"
3879 " g();\n"
3880 " x();\n"
3881 "#endif\n"
3882 "}");
3883 verifyFormat("void f(param1, param2,\n"
3884 " param3,\n"
3885 "#ifdef A\n"
3886 " param4(param5,\n"
3887 "#ifdef A1\n"
3888 " param6,\n"
3889 "#ifdef A2\n"
3890 " param7),\n"
3891 "#else\n"
3892 " param8),\n"
3893 " param9,\n"
3894 "#endif\n"
3895 " param10,\n"
3896 "#endif\n"
3897 " param11)\n"
3898 "#else\n"
3899 " param12)\n"
3900 "#endif\n"
3901 "{\n"
3902 " x();\n"
3903 "}",
3904 getLLVMStyleWithColumns(28));
3905 verifyFormat("#if 1\n"
3906 "int i;");
3907 verifyFormat("#if 1\n"
3908 "#endif\n"
3909 "#if 1\n"
3910 "#else\n"
3911 "#endif\n");
3912 verifyFormat("DEBUG({\n"
3913 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3915 "});\n"
3916 "#if a\n"
3917 "#else\n"
3918 "#endif");
3919
3920 verifyIncompleteFormat("void f(\n"
3921 "#if A\n"
3922 ");\n"
3923 "#else\n"
3924 "#endif");
3925 }
3926
TEST_F(FormatTest,GraciouslyHandleIncorrectPreprocessorConditions)3927 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3928 verifyFormat("#endif\n"
3929 "#if B");
3930 }
3931
TEST_F(FormatTest,FormatsJoinedLinesOnSubsequentRuns)3932 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3933 FormatStyle SingleLine = getLLVMStyle();
3934 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
3935 verifyFormat("#if 0\n"
3936 "#elif 1\n"
3937 "#endif\n"
3938 "void foo() {\n"
3939 " if (test) foo2();\n"
3940 "}",
3941 SingleLine);
3942 }
3943
TEST_F(FormatTest,LayoutBlockInsideParens)3944 TEST_F(FormatTest, LayoutBlockInsideParens) {
3945 verifyFormat("functionCall({ int i; });");
3946 verifyFormat("functionCall({\n"
3947 " int i;\n"
3948 " int j;\n"
3949 "});");
3950 verifyFormat("functionCall(\n"
3951 " {\n"
3952 " int i;\n"
3953 " int j;\n"
3954 " },\n"
3955 " aaaa, bbbb, cccc);");
3956 verifyFormat("functionA(functionB({\n"
3957 " int i;\n"
3958 " int j;\n"
3959 " }),\n"
3960 " aaaa, bbbb, cccc);");
3961 verifyFormat("functionCall(\n"
3962 " {\n"
3963 " int i;\n"
3964 " int j;\n"
3965 " },\n"
3966 " aaaa, bbbb, // comment\n"
3967 " cccc);");
3968 verifyFormat("functionA(functionB({\n"
3969 " int i;\n"
3970 " int j;\n"
3971 " }),\n"
3972 " aaaa, bbbb, // comment\n"
3973 " cccc);");
3974 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3975 verifyFormat("functionCall(aaaa, bbbb, {\n"
3976 " int i;\n"
3977 " int j;\n"
3978 "});");
3979 verifyFormat(
3980 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3981 " {\n"
3982 " int i; // break\n"
3983 " },\n"
3984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3985 " ccccccccccccccccc));");
3986 verifyFormat("DEBUG({\n"
3987 " if (a)\n"
3988 " f();\n"
3989 "});");
3990 }
3991
TEST_F(FormatTest,LayoutBlockInsideStatement)3992 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3993 EXPECT_EQ("SOME_MACRO { int i; }\n"
3994 "int i;",
3995 format(" SOME_MACRO {int i;} int i;"));
3996 }
3997
TEST_F(FormatTest,LayoutNestedBlocks)3998 TEST_F(FormatTest, LayoutNestedBlocks) {
3999 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
4000 " struct s {\n"
4001 " int i;\n"
4002 " };\n"
4003 " s kBitsToOs[] = {{10}};\n"
4004 " for (int i = 0; i < 10; ++i)\n"
4005 " return;\n"
4006 "}");
4007 verifyFormat("call(parameter, {\n"
4008 " something();\n"
4009 " // Comment using all columns.\n"
4010 " somethingelse();\n"
4011 "});",
4012 getLLVMStyleWithColumns(40));
4013 verifyFormat("DEBUG( //\n"
4014 " { f(); }, a);");
4015 verifyFormat("DEBUG( //\n"
4016 " {\n"
4017 " f(); //\n"
4018 " },\n"
4019 " a);");
4020
4021 EXPECT_EQ("call(parameter, {\n"
4022 " something();\n"
4023 " // Comment too\n"
4024 " // looooooooooong.\n"
4025 " somethingElse();\n"
4026 "});",
4027 format("call(parameter, {\n"
4028 " something();\n"
4029 " // Comment too looooooooooong.\n"
4030 " somethingElse();\n"
4031 "});",
4032 getLLVMStyleWithColumns(29)));
4033 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
4034 EXPECT_EQ("DEBUG({ // comment\n"
4035 " int i;\n"
4036 "});",
4037 format("DEBUG({ // comment\n"
4038 "int i;\n"
4039 "});"));
4040 EXPECT_EQ("DEBUG({\n"
4041 " int i;\n"
4042 "\n"
4043 " // comment\n"
4044 " int j;\n"
4045 "});",
4046 format("DEBUG({\n"
4047 " int i;\n"
4048 "\n"
4049 " // comment\n"
4050 " int j;\n"
4051 "});"));
4052
4053 verifyFormat("DEBUG({\n"
4054 " if (a)\n"
4055 " return;\n"
4056 "});");
4057 verifyGoogleFormat("DEBUG({\n"
4058 " if (a) return;\n"
4059 "});");
4060 FormatStyle Style = getGoogleStyle();
4061 Style.ColumnLimit = 45;
4062 verifyFormat("Debug(\n"
4063 " aaaaa,\n"
4064 " {\n"
4065 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
4066 " },\n"
4067 " a);",
4068 Style);
4069
4070 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
4071
4072 verifyNoCrash("^{v^{a}}");
4073 }
4074
TEST_F(FormatTest,FormatNestedBlocksInMacros)4075 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
4076 EXPECT_EQ("#define MACRO() \\\n"
4077 " Debug(aaa, /* force line break */ \\\n"
4078 " { \\\n"
4079 " int i; \\\n"
4080 " int j; \\\n"
4081 " })",
4082 format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
4083 " { int i; int j; })",
4084 getGoogleStyle()));
4085
4086 EXPECT_EQ("#define A \\\n"
4087 " [] { \\\n"
4088 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
4089 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
4090 " }",
4091 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
4092 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
4093 getGoogleStyle()));
4094 }
4095
TEST_F(FormatTest,PutEmptyBlocksIntoOneLine)4096 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
4097 EXPECT_EQ("{}", format("{}"));
4098 verifyFormat("enum E {};");
4099 verifyFormat("enum E {}");
4100 FormatStyle Style = getLLVMStyle();
4101 Style.SpaceInEmptyBlock = true;
4102 EXPECT_EQ("void f() { }", format("void f() {}", Style));
4103 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
4104 EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
4105 }
4106
TEST_F(FormatTest,FormatBeginBlockEndMacros)4107 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
4108 FormatStyle Style = getLLVMStyle();
4109 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
4110 Style.MacroBlockEnd = "^[A-Z_]+_END$";
4111 verifyFormat("FOO_BEGIN\n"
4112 " FOO_ENTRY\n"
4113 "FOO_END",
4114 Style);
4115 verifyFormat("FOO_BEGIN\n"
4116 " NESTED_FOO_BEGIN\n"
4117 " NESTED_FOO_ENTRY\n"
4118 " NESTED_FOO_END\n"
4119 "FOO_END",
4120 Style);
4121 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
4122 " int x;\n"
4123 " x = 1;\n"
4124 "FOO_END(Baz)",
4125 Style);
4126 }
4127
4128 //===----------------------------------------------------------------------===//
4129 // Line break tests.
4130 //===----------------------------------------------------------------------===//
4131
TEST_F(FormatTest,PreventConfusingIndents)4132 TEST_F(FormatTest, PreventConfusingIndents) {
4133 verifyFormat(
4134 "void f() {\n"
4135 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
4136 " parameter, parameter, parameter)),\n"
4137 " SecondLongCall(parameter));\n"
4138 "}");
4139 verifyFormat(
4140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4141 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4143 " aaaaaaaaaaaaaaaaaaaaaaaa);");
4144 verifyFormat(
4145 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4146 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
4147 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
4148 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
4149 verifyFormat(
4150 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4151 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
4152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
4153 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
4154 verifyFormat("int a = bbbb && ccc &&\n"
4155 " fffff(\n"
4156 "#define A Just forcing a new line\n"
4157 " ddd);");
4158 }
4159
TEST_F(FormatTest,LineBreakingInBinaryExpressions)4160 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
4161 verifyFormat(
4162 "bool aaaaaaa =\n"
4163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
4164 " bbbbbbbb();");
4165 verifyFormat(
4166 "bool aaaaaaa =\n"
4167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
4168 " bbbbbbbb();");
4169
4170 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
4171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
4172 " ccccccccc == ddddddddddd;");
4173 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
4174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
4175 " ccccccccc == ddddddddddd;");
4176 verifyFormat(
4177 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
4179 " ccccccccc == ddddddddddd;");
4180
4181 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
4182 " aaaaaa) &&\n"
4183 " bbbbbb && cccccc;");
4184 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
4185 " aaaaaa) >>\n"
4186 " bbbbbb;");
4187 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
4188 " SourceMgr.getSpellingColumnNumber(\n"
4189 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
4190 " 1);");
4191
4192 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4193 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
4194 " cccccc) {\n}");
4195 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4196 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
4197 " cccccc) {\n}");
4198 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4199 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
4200 " cccccc) {\n}");
4201 verifyFormat("b = a &&\n"
4202 " // Comment\n"
4203 " b.c && d;");
4204
4205 // If the LHS of a comparison is not a binary expression itself, the
4206 // additional linebreak confuses many people.
4207 verifyFormat(
4208 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
4210 "}");
4211 verifyFormat(
4212 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4214 "}");
4215 verifyFormat(
4216 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
4217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4218 "}");
4219 verifyFormat(
4220 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
4222 "}");
4223 // Even explicit parentheses stress the precedence enough to make the
4224 // additional break unnecessary.
4225 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
4227 "}");
4228 // This cases is borderline, but with the indentation it is still readable.
4229 verifyFormat(
4230 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4231 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4233 "}",
4234 getLLVMStyleWithColumns(75));
4235
4236 // If the LHS is a binary expression, we should still use the additional break
4237 // as otherwise the formatting hides the operator precedence.
4238 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4240 " 5) {\n"
4241 "}");
4242 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
4244 " 5) {\n"
4245 "}");
4246
4247 FormatStyle OnePerLine = getLLVMStyle();
4248 OnePerLine.BinPackParameters = false;
4249 verifyFormat(
4250 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
4253 OnePerLine);
4254
4255 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
4256 " .aaa(aaaaaaaaaaaaa) *\n"
4257 " aaaaaaa +\n"
4258 " aaaaaaa;",
4259 getLLVMStyleWithColumns(40));
4260 }
4261
TEST_F(FormatTest,ExpressionIndentation)4262 TEST_F(FormatTest, ExpressionIndentation) {
4263 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4267 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4268 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
4269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
4271 " ccccccccccccccccccccccccccccccccccccccccc;");
4272 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4275 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4276 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4279 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4280 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
4281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
4282 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4283 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
4284 verifyFormat("if () {\n"
4285 "} else if (aaaaa && bbbbb > // break\n"
4286 " ccccc) {\n"
4287 "}");
4288 verifyFormat("if () {\n"
4289 "} else if constexpr (aaaaa && bbbbb > // break\n"
4290 " ccccc) {\n"
4291 "}");
4292 verifyFormat("if () {\n"
4293 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
4294 " ccccc) {\n"
4295 "}");
4296 verifyFormat("if () {\n"
4297 "} else if (aaaaa &&\n"
4298 " bbbbb > // break\n"
4299 " ccccc &&\n"
4300 " ddddd) {\n"
4301 "}");
4302
4303 // Presence of a trailing comment used to change indentation of b.
4304 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
4305 " b;\n"
4306 "return aaaaaaaaaaaaaaaaaaa +\n"
4307 " b; //",
4308 getLLVMStyleWithColumns(30));
4309 }
4310
TEST_F(FormatTest,ExpressionIndentationBreakingBeforeOperators)4311 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
4312 // Not sure what the best system is here. Like this, the LHS can be found
4313 // immediately above an operator (everything with the same or a higher
4314 // indent). The RHS is aligned right of the operator and so compasses
4315 // everything until something with the same indent as the operator is found.
4316 // FIXME: Is this a good system?
4317 FormatStyle Style = getLLVMStyle();
4318 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4319 verifyFormat(
4320 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4321 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4322 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4323 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4324 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4325 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4326 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4327 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4328 " > ccccccccccccccccccccccccccccccccccccccccc;",
4329 Style);
4330 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4331 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4332 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4333 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4334 Style);
4335 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4336 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4337 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4338 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4339 Style);
4340 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4341 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4342 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4343 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4344 Style);
4345 verifyFormat("if () {\n"
4346 "} else if (aaaaa\n"
4347 " && bbbbb // break\n"
4348 " > ccccc) {\n"
4349 "}",
4350 Style);
4351 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4352 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4353 Style);
4354 verifyFormat("return (a)\n"
4355 " // comment\n"
4356 " + b;",
4357 Style);
4358 verifyFormat(
4359 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4360 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4361 " + cc;",
4362 Style);
4363
4364 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4365 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4366 Style);
4367
4368 // Forced by comments.
4369 verifyFormat(
4370 "unsigned ContentSize =\n"
4371 " sizeof(int16_t) // DWARF ARange version number\n"
4372 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4373 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
4374 " + sizeof(int8_t); // Segment Size (in bytes)");
4375
4376 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
4377 " == boost::fusion::at_c<1>(iiii).second;",
4378 Style);
4379
4380 Style.ColumnLimit = 60;
4381 verifyFormat("zzzzzzzzzz\n"
4382 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4383 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
4384 Style);
4385
4386 Style.ColumnLimit = 80;
4387 Style.IndentWidth = 4;
4388 Style.TabWidth = 4;
4389 Style.UseTab = FormatStyle::UT_Always;
4390 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4391 Style.AlignOperands = FormatStyle::OAS_DontAlign;
4392 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
4393 "\t&& (someOtherLongishConditionPart1\n"
4394 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
4395 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && "
4396 "(someOtherLongishConditionPart1 || "
4397 "someOtherEvenLongerNestedConditionPart2);",
4398 Style));
4399 }
4400
TEST_F(FormatTest,ExpressionIndentationStrictAlign)4401 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
4402 FormatStyle Style = getLLVMStyle();
4403 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4404 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
4405
4406 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4407 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4408 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4409 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4410 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4411 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4412 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4413 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4414 " > ccccccccccccccccccccccccccccccccccccccccc;",
4415 Style);
4416 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4417 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4418 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4419 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4420 Style);
4421 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4422 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4423 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4424 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4425 Style);
4426 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4427 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4428 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4429 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
4430 Style);
4431 verifyFormat("if () {\n"
4432 "} else if (aaaaa\n"
4433 " && bbbbb // break\n"
4434 " > ccccc) {\n"
4435 "}",
4436 Style);
4437 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4438 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4439 Style);
4440 verifyFormat("return (a)\n"
4441 " // comment\n"
4442 " + b;",
4443 Style);
4444 verifyFormat(
4445 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4446 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4447 " + cc;",
4448 Style);
4449 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
4450 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
4451 " : 3333333333333333;",
4452 Style);
4453 verifyFormat(
4454 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
4455 " : ccccccccccccccc ? dddddddddddddddddd\n"
4456 " : eeeeeeeeeeeeeeeeee)\n"
4457 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
4458 " : 3333333333333333;",
4459 Style);
4460 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4461 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4462 Style);
4463
4464 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
4465 " == boost::fusion::at_c<1>(iiii).second;",
4466 Style);
4467
4468 Style.ColumnLimit = 60;
4469 verifyFormat("zzzzzzzzzzzzz\n"
4470 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4471 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
4472 Style);
4473
4474 // Forced by comments.
4475 Style.ColumnLimit = 80;
4476 verifyFormat(
4477 "unsigned ContentSize\n"
4478 " = sizeof(int16_t) // DWARF ARange version number\n"
4479 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4480 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
4481 " + sizeof(int8_t); // Segment Size (in bytes)",
4482 Style);
4483
4484 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4485 verifyFormat(
4486 "unsigned ContentSize =\n"
4487 " sizeof(int16_t) // DWARF ARange version number\n"
4488 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4489 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
4490 " + sizeof(int8_t); // Segment Size (in bytes)",
4491 Style);
4492
4493 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4494 verifyFormat(
4495 "unsigned ContentSize =\n"
4496 " sizeof(int16_t) // DWARF ARange version number\n"
4497 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
4498 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
4499 " + sizeof(int8_t); // Segment Size (in bytes)",
4500 Style);
4501 }
4502
TEST_F(FormatTest,EnforcedOperatorWraps)4503 TEST_F(FormatTest, EnforcedOperatorWraps) {
4504 // Here we'd like to wrap after the || operators, but a comment is forcing an
4505 // earlier wrap.
4506 verifyFormat("bool x = aaaaa //\n"
4507 " || bbbbb\n"
4508 " //\n"
4509 " || cccc;");
4510 }
4511
TEST_F(FormatTest,NoOperandAlignment)4512 TEST_F(FormatTest, NoOperandAlignment) {
4513 FormatStyle Style = getLLVMStyle();
4514 Style.AlignOperands = FormatStyle::OAS_DontAlign;
4515 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
4516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4518 Style);
4519 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4520 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4521 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4522 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4523 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4524 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4525 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4526 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4527 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4528 " > ccccccccccccccccccccccccccccccccccccccccc;",
4529 Style);
4530
4531 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4532 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4533 " + cc;",
4534 Style);
4535 verifyFormat("int a = aa\n"
4536 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
4537 " * cccccccccccccccccccccccccccccccccccc;\n",
4538 Style);
4539
4540 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4541 verifyFormat("return (a > b\n"
4542 " // comment1\n"
4543 " // comment2\n"
4544 " || c);",
4545 Style);
4546 }
4547
TEST_F(FormatTest,BreakingBeforeNonAssigmentOperators)4548 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
4549 FormatStyle Style = getLLVMStyle();
4550 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4551 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4553 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
4554 Style);
4555 }
4556
TEST_F(FormatTest,AllowBinPackingInsideArguments)4557 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
4558 FormatStyle Style = getLLVMStyle();
4559 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
4560 Style.BinPackArguments = false;
4561 Style.ColumnLimit = 40;
4562 verifyFormat("void test() {\n"
4563 " someFunction(\n"
4564 " this + argument + is + quite\n"
4565 " + long + so + it + gets + wrapped\n"
4566 " + but + remains + bin - packed);\n"
4567 "}",
4568 Style);
4569 verifyFormat("void test() {\n"
4570 " someFunction(arg1,\n"
4571 " this + argument + is\n"
4572 " + quite + long + so\n"
4573 " + it + gets + wrapped\n"
4574 " + but + remains + bin\n"
4575 " - packed,\n"
4576 " arg3);\n"
4577 "}",
4578 Style);
4579 verifyFormat("void test() {\n"
4580 " someFunction(\n"
4581 " arg1,\n"
4582 " this + argument + has\n"
4583 " + anotherFunc(nested,\n"
4584 " calls + whose\n"
4585 " + arguments\n"
4586 " + are + also\n"
4587 " + wrapped,\n"
4588 " in + addition)\n"
4589 " + to + being + bin - packed,\n"
4590 " arg3);\n"
4591 "}",
4592 Style);
4593
4594 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4595 verifyFormat("void test() {\n"
4596 " someFunction(\n"
4597 " arg1,\n"
4598 " this + argument + has +\n"
4599 " anotherFunc(nested,\n"
4600 " calls + whose +\n"
4601 " arguments +\n"
4602 " are + also +\n"
4603 " wrapped,\n"
4604 " in + addition) +\n"
4605 " to + being + bin - packed,\n"
4606 " arg3);\n"
4607 "}",
4608 Style);
4609 }
4610
TEST_F(FormatTest,ConstructorInitializers)4611 TEST_F(FormatTest, ConstructorInitializers) {
4612 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
4613 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
4614 getLLVMStyleWithColumns(45));
4615 verifyFormat("Constructor()\n"
4616 " : Inttializer(FitsOnTheLine) {}",
4617 getLLVMStyleWithColumns(44));
4618 verifyFormat("Constructor()\n"
4619 " : Inttializer(FitsOnTheLine) {}",
4620 getLLVMStyleWithColumns(43));
4621
4622 verifyFormat("template <typename T>\n"
4623 "Constructor() : Initializer(FitsOnTheLine) {}",
4624 getLLVMStyleWithColumns(45));
4625
4626 verifyFormat(
4627 "SomeClass::Constructor()\n"
4628 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
4629
4630 verifyFormat(
4631 "SomeClass::Constructor()\n"
4632 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4633 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
4634 verifyFormat(
4635 "SomeClass::Constructor()\n"
4636 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4637 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
4638 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4639 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4640 " : aaaaaaaaaa(aaaaaa) {}");
4641
4642 verifyFormat("Constructor()\n"
4643 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4644 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4645 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4646 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
4647
4648 verifyFormat("Constructor()\n"
4649 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4651
4652 verifyFormat("Constructor(int Parameter = 0)\n"
4653 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
4654 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
4655 verifyFormat("Constructor()\n"
4656 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
4657 "}",
4658 getLLVMStyleWithColumns(60));
4659 verifyFormat("Constructor()\n"
4660 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4661 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
4662
4663 // Here a line could be saved by splitting the second initializer onto two
4664 // lines, but that is not desirable.
4665 verifyFormat("Constructor()\n"
4666 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
4667 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
4668 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4669
4670 FormatStyle OnePerLine = getLLVMStyle();
4671 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4672 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
4673 verifyFormat("SomeClass::Constructor()\n"
4674 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4675 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4676 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4677 OnePerLine);
4678 verifyFormat("SomeClass::Constructor()\n"
4679 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
4680 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4681 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4682 OnePerLine);
4683 verifyFormat("MyClass::MyClass(int var)\n"
4684 " : some_var_(var), // 4 space indent\n"
4685 " some_other_var_(var + 1) { // lined up\n"
4686 "}",
4687 OnePerLine);
4688 verifyFormat("Constructor()\n"
4689 " : aaaaa(aaaaaa),\n"
4690 " aaaaa(aaaaaa),\n"
4691 " aaaaa(aaaaaa),\n"
4692 " aaaaa(aaaaaa),\n"
4693 " aaaaa(aaaaaa) {}",
4694 OnePerLine);
4695 verifyFormat("Constructor()\n"
4696 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4697 " aaaaaaaaaaaaaaaaaaaaaa) {}",
4698 OnePerLine);
4699 OnePerLine.BinPackParameters = false;
4700 verifyFormat(
4701 "Constructor()\n"
4702 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4703 " aaaaaaaaaaa().aaa(),\n"
4704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4705 OnePerLine);
4706 OnePerLine.ColumnLimit = 60;
4707 verifyFormat("Constructor()\n"
4708 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
4709 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
4710 OnePerLine);
4711
4712 EXPECT_EQ("Constructor()\n"
4713 " : // Comment forcing unwanted break.\n"
4714 " aaaa(aaaa) {}",
4715 format("Constructor() :\n"
4716 " // Comment forcing unwanted break.\n"
4717 " aaaa(aaaa) {}"));
4718 }
4719
TEST_F(FormatTest,AllowAllConstructorInitializersOnNextLine)4720 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
4721 FormatStyle Style = getLLVMStyle();
4722 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4723 Style.ColumnLimit = 60;
4724 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4725 Style.AllowAllConstructorInitializersOnNextLine = true;
4726 Style.BinPackParameters = false;
4727
4728 for (int i = 0; i < 4; ++i) {
4729 // Test all combinations of parameters that should not have an effect.
4730 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
4731 Style.AllowAllArgumentsOnNextLine = i & 2;
4732
4733 Style.AllowAllConstructorInitializersOnNextLine = true;
4734 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4735 verifyFormat("Constructor()\n"
4736 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4737 Style);
4738 verifyFormat("Constructor() : a(a), b(b) {}", Style);
4739
4740 Style.AllowAllConstructorInitializersOnNextLine = false;
4741 verifyFormat("Constructor()\n"
4742 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
4743 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4744 Style);
4745 verifyFormat("Constructor() : a(a), b(b) {}", Style);
4746
4747 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
4748 Style.AllowAllConstructorInitializersOnNextLine = true;
4749 verifyFormat("Constructor()\n"
4750 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4751 Style);
4752
4753 Style.AllowAllConstructorInitializersOnNextLine = false;
4754 verifyFormat("Constructor()\n"
4755 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
4756 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
4757 Style);
4758
4759 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4760 Style.AllowAllConstructorInitializersOnNextLine = true;
4761 verifyFormat("Constructor() :\n"
4762 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4763 Style);
4764
4765 Style.AllowAllConstructorInitializersOnNextLine = false;
4766 verifyFormat("Constructor() :\n"
4767 " aaaaaaaaaaaaaaaaaa(a),\n"
4768 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
4769 Style);
4770 }
4771
4772 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
4773 // AllowAllConstructorInitializersOnNextLine in all
4774 // BreakConstructorInitializers modes
4775 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
4776 Style.AllowAllParametersOfDeclarationOnNextLine = true;
4777 Style.AllowAllConstructorInitializersOnNextLine = false;
4778 verifyFormat("SomeClassWithALongName::Constructor(\n"
4779 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
4780 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
4781 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4782 Style);
4783
4784 Style.AllowAllConstructorInitializersOnNextLine = true;
4785 verifyFormat("SomeClassWithALongName::Constructor(\n"
4786 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4787 " int bbbbbbbbbbbbb,\n"
4788 " int cccccccccccccccc)\n"
4789 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4790 Style);
4791
4792 Style.AllowAllParametersOfDeclarationOnNextLine = false;
4793 Style.AllowAllConstructorInitializersOnNextLine = false;
4794 verifyFormat("SomeClassWithALongName::Constructor(\n"
4795 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4796 " int bbbbbbbbbbbbb)\n"
4797 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
4798 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
4799 Style);
4800
4801 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
4802
4803 Style.AllowAllParametersOfDeclarationOnNextLine = true;
4804 verifyFormat("SomeClassWithALongName::Constructor(\n"
4805 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
4806 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
4807 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
4808 Style);
4809
4810 Style.AllowAllConstructorInitializersOnNextLine = true;
4811 verifyFormat("SomeClassWithALongName::Constructor(\n"
4812 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4813 " int bbbbbbbbbbbbb,\n"
4814 " int cccccccccccccccc)\n"
4815 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4816 Style);
4817
4818 Style.AllowAllParametersOfDeclarationOnNextLine = false;
4819 Style.AllowAllConstructorInitializersOnNextLine = false;
4820 verifyFormat("SomeClassWithALongName::Constructor(\n"
4821 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4822 " int bbbbbbbbbbbbb)\n"
4823 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
4824 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
4825 Style);
4826
4827 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4828 Style.AllowAllParametersOfDeclarationOnNextLine = true;
4829 verifyFormat("SomeClassWithALongName::Constructor(\n"
4830 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
4831 " aaaaaaaaaaaaaaaaaaaa(a),\n"
4832 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
4833 Style);
4834
4835 Style.AllowAllConstructorInitializersOnNextLine = true;
4836 verifyFormat("SomeClassWithALongName::Constructor(\n"
4837 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4838 " int bbbbbbbbbbbbb,\n"
4839 " int cccccccccccccccc) :\n"
4840 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
4841 Style);
4842
4843 Style.AllowAllParametersOfDeclarationOnNextLine = false;
4844 Style.AllowAllConstructorInitializersOnNextLine = false;
4845 verifyFormat("SomeClassWithALongName::Constructor(\n"
4846 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
4847 " int bbbbbbbbbbbbb) :\n"
4848 " aaaaaaaaaaaaaaaaaaaa(a),\n"
4849 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
4850 Style);
4851 }
4852
TEST_F(FormatTest,AllowAllArgumentsOnNextLine)4853 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
4854 FormatStyle Style = getLLVMStyle();
4855 Style.ColumnLimit = 60;
4856 Style.BinPackArguments = false;
4857 for (int i = 0; i < 4; ++i) {
4858 // Test all combinations of parameters that should not have an effect.
4859 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
4860 Style.AllowAllConstructorInitializersOnNextLine = i & 2;
4861
4862 Style.AllowAllArgumentsOnNextLine = true;
4863 verifyFormat("void foo() {\n"
4864 " FunctionCallWithReallyLongName(\n"
4865 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
4866 "}",
4867 Style);
4868 Style.AllowAllArgumentsOnNextLine = false;
4869 verifyFormat("void foo() {\n"
4870 " FunctionCallWithReallyLongName(\n"
4871 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4872 " bbbbbbbbbbbb);\n"
4873 "}",
4874 Style);
4875
4876 Style.AllowAllArgumentsOnNextLine = true;
4877 verifyFormat("void foo() {\n"
4878 " auto VariableWithReallyLongName = {\n"
4879 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
4880 "}",
4881 Style);
4882 Style.AllowAllArgumentsOnNextLine = false;
4883 verifyFormat("void foo() {\n"
4884 " auto VariableWithReallyLongName = {\n"
4885 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4886 " bbbbbbbbbbbb};\n"
4887 "}",
4888 Style);
4889 }
4890
4891 // This parameter should not affect declarations.
4892 Style.BinPackParameters = false;
4893 Style.AllowAllArgumentsOnNextLine = false;
4894 Style.AllowAllParametersOfDeclarationOnNextLine = true;
4895 verifyFormat("void FunctionCallWithReallyLongName(\n"
4896 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
4897 Style);
4898 Style.AllowAllParametersOfDeclarationOnNextLine = false;
4899 verifyFormat("void FunctionCallWithReallyLongName(\n"
4900 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
4901 " int bbbbbbbbbbbb);",
4902 Style);
4903 }
4904
TEST_F(FormatTest,BreakConstructorInitializersAfterColon)4905 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
4906 FormatStyle Style = getLLVMStyle();
4907 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
4908
4909 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
4910 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
4911 getStyleWithColumns(Style, 45));
4912 verifyFormat("Constructor() :\n"
4913 " Initializer(FitsOnTheLine) {}",
4914 getStyleWithColumns(Style, 44));
4915 verifyFormat("Constructor() :\n"
4916 " Initializer(FitsOnTheLine) {}",
4917 getStyleWithColumns(Style, 43));
4918
4919 verifyFormat("template <typename T>\n"
4920 "Constructor() : Initializer(FitsOnTheLine) {}",
4921 getStyleWithColumns(Style, 50));
4922 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4923 verifyFormat(
4924 "SomeClass::Constructor() :\n"
4925 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4926 Style);
4927
4928 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
4929 verifyFormat(
4930 "SomeClass::Constructor() :\n"
4931 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4932 Style);
4933
4934 verifyFormat(
4935 "SomeClass::Constructor() :\n"
4936 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4937 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4938 Style);
4939 verifyFormat(
4940 "SomeClass::Constructor() :\n"
4941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4942 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
4943 Style);
4944 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4945 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4946 " aaaaaaaaaa(aaaaaa) {}",
4947 Style);
4948
4949 verifyFormat("Constructor() :\n"
4950 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4951 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4952 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4953 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
4954 Style);
4955
4956 verifyFormat("Constructor() :\n"
4957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4959 Style);
4960
4961 verifyFormat("Constructor(int Parameter = 0) :\n"
4962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
4963 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
4964 Style);
4965 verifyFormat("Constructor() :\n"
4966 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
4967 "}",
4968 getStyleWithColumns(Style, 60));
4969 verifyFormat("Constructor() :\n"
4970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4971 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
4972 Style);
4973
4974 // Here a line could be saved by splitting the second initializer onto two
4975 // lines, but that is not desirable.
4976 verifyFormat("Constructor() :\n"
4977 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
4978 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
4979 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4980 Style);
4981
4982 FormatStyle OnePerLine = Style;
4983 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
4984 OnePerLine.AllowAllConstructorInitializersOnNextLine = false;
4985 verifyFormat("SomeClass::Constructor() :\n"
4986 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4987 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4988 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4989 OnePerLine);
4990 verifyFormat("SomeClass::Constructor() :\n"
4991 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
4992 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
4993 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
4994 OnePerLine);
4995 verifyFormat("MyClass::MyClass(int var) :\n"
4996 " some_var_(var), // 4 space indent\n"
4997 " some_other_var_(var + 1) { // lined up\n"
4998 "}",
4999 OnePerLine);
5000 verifyFormat("Constructor() :\n"
5001 " aaaaa(aaaaaa),\n"
5002 " aaaaa(aaaaaa),\n"
5003 " aaaaa(aaaaaa),\n"
5004 " aaaaa(aaaaaa),\n"
5005 " aaaaa(aaaaaa) {}",
5006 OnePerLine);
5007 verifyFormat("Constructor() :\n"
5008 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5009 " aaaaaaaaaaaaaaaaaaaaaa) {}",
5010 OnePerLine);
5011 OnePerLine.BinPackParameters = false;
5012 verifyFormat("Constructor() :\n"
5013 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5014 " aaaaaaaaaaa().aaa(),\n"
5015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5016 OnePerLine);
5017 OnePerLine.ColumnLimit = 60;
5018 verifyFormat("Constructor() :\n"
5019 " aaaaaaaaaaaaaaaaaaaa(a),\n"
5020 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
5021 OnePerLine);
5022
5023 EXPECT_EQ("Constructor() :\n"
5024 " // Comment forcing unwanted break.\n"
5025 " aaaa(aaaa) {}",
5026 format("Constructor() :\n"
5027 " // Comment forcing unwanted break.\n"
5028 " aaaa(aaaa) {}",
5029 Style));
5030
5031 Style.ColumnLimit = 0;
5032 verifyFormat("SomeClass::Constructor() :\n"
5033 " a(a) {}",
5034 Style);
5035 verifyFormat("SomeClass::Constructor() noexcept :\n"
5036 " a(a) {}",
5037 Style);
5038 verifyFormat("SomeClass::Constructor() :\n"
5039 " a(a), b(b), c(c) {}",
5040 Style);
5041 verifyFormat("SomeClass::Constructor() :\n"
5042 " a(a) {\n"
5043 " foo();\n"
5044 " bar();\n"
5045 "}",
5046 Style);
5047
5048 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5049 verifyFormat("SomeClass::Constructor() :\n"
5050 " a(a), b(b), c(c) {\n"
5051 "}",
5052 Style);
5053 verifyFormat("SomeClass::Constructor() :\n"
5054 " a(a) {\n"
5055 "}",
5056 Style);
5057
5058 Style.ColumnLimit = 80;
5059 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
5060 Style.ConstructorInitializerIndentWidth = 2;
5061 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
5062 verifyFormat("SomeClass::Constructor() :\n"
5063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5064 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
5065 Style);
5066
5067 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
5068 // well
5069 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
5070 verifyFormat(
5071 "class SomeClass\n"
5072 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5073 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5074 Style);
5075 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
5076 verifyFormat(
5077 "class SomeClass\n"
5078 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5079 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5080 Style);
5081 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
5082 verifyFormat(
5083 "class SomeClass :\n"
5084 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5085 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
5086 Style);
5087 }
5088
5089 #ifndef EXPENSIVE_CHECKS
5090 // Expensive checks enables libstdc++ checking which includes validating the
5091 // state of ranges used in std::priority_queue - this blows out the
5092 // runtime/scalability of the function and makes this test unacceptably slow.
TEST_F(FormatTest,MemoizationTests)5093 TEST_F(FormatTest, MemoizationTests) {
5094 // This breaks if the memoization lookup does not take \c Indent and
5095 // \c LastSpace into account.
5096 verifyFormat(
5097 "extern CFRunLoopTimerRef\n"
5098 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
5099 " CFTimeInterval interval, CFOptionFlags flags,\n"
5100 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
5101 " CFRunLoopTimerContext *context) {}");
5102
5103 // Deep nesting somewhat works around our memoization.
5104 verifyFormat(
5105 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5106 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5107 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5108 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
5109 " aaaaa())))))))))))))))))))))))))))))))))))))));",
5110 getLLVMStyleWithColumns(65));
5111 verifyFormat(
5112 "aaaaa(\n"
5113 " aaaaa,\n"
5114 " aaaaa(\n"
5115 " aaaaa,\n"
5116 " aaaaa(\n"
5117 " aaaaa,\n"
5118 " aaaaa(\n"
5119 " aaaaa,\n"
5120 " aaaaa(\n"
5121 " aaaaa,\n"
5122 " aaaaa(\n"
5123 " aaaaa,\n"
5124 " aaaaa(\n"
5125 " aaaaa,\n"
5126 " aaaaa(\n"
5127 " aaaaa,\n"
5128 " aaaaa(\n"
5129 " aaaaa,\n"
5130 " aaaaa(\n"
5131 " aaaaa,\n"
5132 " aaaaa(\n"
5133 " aaaaa,\n"
5134 " aaaaa(\n"
5135 " aaaaa,\n"
5136 " aaaaa))))))))))));",
5137 getLLVMStyleWithColumns(65));
5138 verifyFormat(
5139 "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"
5140 " a),\n"
5141 " a),\n"
5142 " a),\n"
5143 " a),\n"
5144 " a),\n"
5145 " a),\n"
5146 " a),\n"
5147 " a),\n"
5148 " a),\n"
5149 " a),\n"
5150 " a),\n"
5151 " a),\n"
5152 " a),\n"
5153 " a),\n"
5154 " a),\n"
5155 " a),\n"
5156 " a)",
5157 getLLVMStyleWithColumns(65));
5158
5159 // This test takes VERY long when memoization is broken.
5160 FormatStyle OnePerLine = getLLVMStyle();
5161 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
5162 OnePerLine.BinPackParameters = false;
5163 std::string input = "Constructor()\n"
5164 " : aaaa(a,\n";
5165 for (unsigned i = 0, e = 80; i != e; ++i) {
5166 input += " a,\n";
5167 }
5168 input += " a) {}";
5169 verifyFormat(input, OnePerLine);
5170 }
5171 #endif
5172
TEST_F(FormatTest,BreaksAsHighAsPossible)5173 TEST_F(FormatTest, BreaksAsHighAsPossible) {
5174 verifyFormat(
5175 "void f() {\n"
5176 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
5177 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
5178 " f();\n"
5179 "}");
5180 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
5181 " Intervals[i - 1].getRange().getLast()) {\n}");
5182 }
5183
TEST_F(FormatTest,BreaksFunctionDeclarations)5184 TEST_F(FormatTest, BreaksFunctionDeclarations) {
5185 // Principially, we break function declarations in a certain order:
5186 // 1) break amongst arguments.
5187 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
5188 " Cccccccccccccc cccccccccccccc);");
5189 verifyFormat("template <class TemplateIt>\n"
5190 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
5191 " TemplateIt *stop) {}");
5192
5193 // 2) break after return type.
5194 verifyFormat(
5195 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5196 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
5197 getGoogleStyle());
5198
5199 // 3) break after (.
5200 verifyFormat(
5201 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
5202 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
5203 getGoogleStyle());
5204
5205 // 4) break before after nested name specifiers.
5206 verifyFormat(
5207 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5208 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
5209 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
5210 getGoogleStyle());
5211
5212 // However, there are exceptions, if a sufficient amount of lines can be
5213 // saved.
5214 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
5215 // more adjusting.
5216 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
5217 " Cccccccccccccc cccccccccc,\n"
5218 " Cccccccccccccc cccccccccc,\n"
5219 " Cccccccccccccc cccccccccc,\n"
5220 " Cccccccccccccc cccccccccc);");
5221 verifyFormat(
5222 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5223 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5224 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5225 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
5226 getGoogleStyle());
5227 verifyFormat(
5228 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
5229 " Cccccccccccccc cccccccccc,\n"
5230 " Cccccccccccccc cccccccccc,\n"
5231 " Cccccccccccccc cccccccccc,\n"
5232 " Cccccccccccccc cccccccccc,\n"
5233 " Cccccccccccccc cccccccccc,\n"
5234 " Cccccccccccccc cccccccccc);");
5235 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
5236 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5237 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5238 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
5239 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
5240
5241 // Break after multi-line parameters.
5242 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5244 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5245 " bbbb bbbb);");
5246 verifyFormat("void SomeLoooooooooooongFunction(\n"
5247 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5248 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5249 " int bbbbbbbbbbbbb);");
5250
5251 // Treat overloaded operators like other functions.
5252 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5253 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
5254 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5255 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
5256 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
5257 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
5258 verifyGoogleFormat(
5259 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
5260 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
5261 verifyGoogleFormat(
5262 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
5263 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
5264 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5265 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
5266 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
5267 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
5268 verifyGoogleFormat(
5269 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
5270 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5271 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
5272 verifyGoogleFormat("template <typename T>\n"
5273 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5274 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
5275 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
5276
5277 FormatStyle Style = getLLVMStyle();
5278 Style.PointerAlignment = FormatStyle::PAS_Left;
5279 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5280 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
5281 Style);
5282 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
5283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5284 Style);
5285 }
5286
TEST_F(FormatTest,DontBreakBeforeQualifiedOperator)5287 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
5288 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
5289 // Prefer keeping `::` followed by `operator` together.
5290 EXPECT_EQ("const aaaa::bbbbbbb &\n"
5291 "ccccccccc::operator++() {\n"
5292 " stuff();\n"
5293 "}",
5294 format("const aaaa::bbbbbbb\n"
5295 "&ccccccccc::operator++() { stuff(); }",
5296 getLLVMStyleWithColumns(40)));
5297 }
5298
TEST_F(FormatTest,TrailingReturnType)5299 TEST_F(FormatTest, TrailingReturnType) {
5300 verifyFormat("auto foo() -> int;\n");
5301 // correct trailing return type spacing
5302 verifyFormat("auto operator->() -> int;\n");
5303 verifyFormat("auto operator++(int) -> int;\n");
5304
5305 verifyFormat("struct S {\n"
5306 " auto bar() const -> int;\n"
5307 "};");
5308 verifyFormat("template <size_t Order, typename T>\n"
5309 "auto load_img(const std::string &filename)\n"
5310 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
5311 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
5312 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
5313 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
5314 verifyFormat("template <typename T>\n"
5315 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
5316 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
5317
5318 // Not trailing return types.
5319 verifyFormat("void f() { auto a = b->c(); }");
5320 }
5321
TEST_F(FormatTest,DeductionGuides)5322 TEST_F(FormatTest, DeductionGuides) {
5323 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
5324 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
5325 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
5326 verifyFormat(
5327 "template <class... T>\n"
5328 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
5329 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
5330 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
5331 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
5332 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
5333 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
5334 verifyFormat("template <class T> x() -> x<1>;");
5335 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
5336
5337 // Ensure not deduction guides.
5338 verifyFormat("c()->f<int>();");
5339 verifyFormat("x()->foo<1>;");
5340 verifyFormat("x = p->foo<3>();");
5341 verifyFormat("x()->x<1>();");
5342 verifyFormat("x()->x<1>;");
5343 }
5344
TEST_F(FormatTest,BreaksFunctionDeclarationsWithTrailingTokens)5345 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
5346 // Avoid breaking before trailing 'const' or other trailing annotations, if
5347 // they are not function-like.
5348 FormatStyle Style = getGoogleStyle();
5349 Style.ColumnLimit = 47;
5350 verifyFormat("void someLongFunction(\n"
5351 " int someLoooooooooooooongParameter) const {\n}",
5352 getLLVMStyleWithColumns(47));
5353 verifyFormat("LoooooongReturnType\n"
5354 "someLoooooooongFunction() const {}",
5355 getLLVMStyleWithColumns(47));
5356 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
5357 " const {}",
5358 Style);
5359 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5360 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
5361 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5362 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
5363 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
5364 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
5365 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
5366 " aaaaaaaaaaa aaaaa) const override;");
5367 verifyGoogleFormat(
5368 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5369 " const override;");
5370
5371 // Even if the first parameter has to be wrapped.
5372 verifyFormat("void someLongFunction(\n"
5373 " int someLongParameter) const {}",
5374 getLLVMStyleWithColumns(46));
5375 verifyFormat("void someLongFunction(\n"
5376 " int someLongParameter) const {}",
5377 Style);
5378 verifyFormat("void someLongFunction(\n"
5379 " int someLongParameter) override {}",
5380 Style);
5381 verifyFormat("void someLongFunction(\n"
5382 " int someLongParameter) OVERRIDE {}",
5383 Style);
5384 verifyFormat("void someLongFunction(\n"
5385 " int someLongParameter) final {}",
5386 Style);
5387 verifyFormat("void someLongFunction(\n"
5388 " int someLongParameter) FINAL {}",
5389 Style);
5390 verifyFormat("void someLongFunction(\n"
5391 " int parameter) const override {}",
5392 Style);
5393
5394 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
5395 verifyFormat("void someLongFunction(\n"
5396 " int someLongParameter) const\n"
5397 "{\n"
5398 "}",
5399 Style);
5400
5401 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
5402 verifyFormat("void someLongFunction(\n"
5403 " int someLongParameter) const\n"
5404 " {\n"
5405 " }",
5406 Style);
5407
5408 // Unless these are unknown annotations.
5409 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
5410 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5411 " LONG_AND_UGLY_ANNOTATION;");
5412
5413 // Breaking before function-like trailing annotations is fine to keep them
5414 // close to their arguments.
5415 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5416 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
5417 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
5418 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
5419 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
5420 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
5421 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
5422 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
5423 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
5424
5425 verifyFormat(
5426 "void aaaaaaaaaaaaaaaaaa()\n"
5427 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
5428 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
5429 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5430 " __attribute__((unused));");
5431 verifyGoogleFormat(
5432 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5433 " GUARDED_BY(aaaaaaaaaaaa);");
5434 verifyGoogleFormat(
5435 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5436 " GUARDED_BY(aaaaaaaaaaaa);");
5437 verifyGoogleFormat(
5438 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
5439 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5440 verifyGoogleFormat(
5441 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
5442 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
5443 }
5444
TEST_F(FormatTest,FunctionAnnotations)5445 TEST_F(FormatTest, FunctionAnnotations) {
5446 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
5447 "int OldFunction(const string ¶meter) {}");
5448 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
5449 "string OldFunction(const string ¶meter) {}");
5450 verifyFormat("template <typename T>\n"
5451 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
5452 "string OldFunction(const string ¶meter) {}");
5453
5454 // Not function annotations.
5455 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5456 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
5457 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
5458 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
5459 verifyFormat("MACRO(abc).function() // wrap\n"
5460 " << abc;");
5461 verifyFormat("MACRO(abc)->function() // wrap\n"
5462 " << abc;");
5463 verifyFormat("MACRO(abc)::function() // wrap\n"
5464 " << abc;");
5465 }
5466
TEST_F(FormatTest,BreaksDesireably)5467 TEST_F(FormatTest, BreaksDesireably) {
5468 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
5469 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
5470 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
5471 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
5473 "}");
5474
5475 verifyFormat(
5476 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
5478
5479 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5482
5483 verifyFormat(
5484 "aaaaaaaa(aaaaaaaaaaaaa,\n"
5485 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
5487 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
5489
5490 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5491 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5492
5493 verifyFormat(
5494 "void f() {\n"
5495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
5496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5497 "}");
5498 verifyFormat(
5499 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5501 verifyFormat(
5502 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5504 verifyFormat(
5505 "aaaaaa(aaa,\n"
5506 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
5508 " aaaa);");
5509 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5511 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5512
5513 // Indent consistently independent of call expression and unary operator.
5514 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
5515 " dddddddddddddddddddddddddddddd));");
5516 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
5517 " dddddddddddddddddddddddddddddd));");
5518 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
5519 " dddddddddddddddddddddddddddddd));");
5520
5521 // This test case breaks on an incorrect memoization, i.e. an optimization not
5522 // taking into account the StopAt value.
5523 verifyFormat(
5524 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
5525 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
5526 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
5527 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5528
5529 verifyFormat("{\n {\n {\n"
5530 " Annotation.SpaceRequiredBefore =\n"
5531 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
5532 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
5533 " }\n }\n}");
5534
5535 // Break on an outer level if there was a break on an inner level.
5536 EXPECT_EQ("f(g(h(a, // comment\n"
5537 " b, c),\n"
5538 " d, e),\n"
5539 " x, y);",
5540 format("f(g(h(a, // comment\n"
5541 " b, c), d, e), x, y);"));
5542
5543 // Prefer breaking similar line breaks.
5544 verifyFormat(
5545 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
5546 " NSTrackingMouseEnteredAndExited |\n"
5547 " NSTrackingActiveAlways;");
5548 }
5549
TEST_F(FormatTest,FormatsDeclarationsOnePerLine)5550 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
5551 FormatStyle NoBinPacking = getGoogleStyle();
5552 NoBinPacking.BinPackParameters = false;
5553 NoBinPacking.BinPackArguments = true;
5554 verifyFormat("void f() {\n"
5555 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
5556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5557 "}",
5558 NoBinPacking);
5559 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
5560 " int aaaaaaaaaaaaaaaaaaaa,\n"
5561 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5562 NoBinPacking);
5563
5564 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
5565 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5566 " vector<int> bbbbbbbbbbbbbbb);",
5567 NoBinPacking);
5568 // FIXME: This behavior difference is probably not wanted. However, currently
5569 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
5570 // template arguments from BreakBeforeParameter being set because of the
5571 // one-per-line formatting.
5572 verifyFormat(
5573 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
5574 " aaaaaaaaaa> aaaaaaaaaa);",
5575 NoBinPacking);
5576 verifyFormat(
5577 "void fffffffffff(\n"
5578 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
5579 " aaaaaaaaaa);");
5580 }
5581
TEST_F(FormatTest,FormatsOneParameterPerLineIfNecessary)5582 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
5583 FormatStyle NoBinPacking = getGoogleStyle();
5584 NoBinPacking.BinPackParameters = false;
5585 NoBinPacking.BinPackArguments = false;
5586 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
5587 " aaaaaaaaaaaaaaaaaaaa,\n"
5588 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
5589 NoBinPacking);
5590 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
5591 " aaaaaaaaaaaaa,\n"
5592 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
5593 NoBinPacking);
5594 verifyFormat(
5595 "aaaaaaaa(aaaaaaaaaaaaa,\n"
5596 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
5598 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5599 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
5600 NoBinPacking);
5601 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
5602 " .aaaaaaaaaaaaaaaaaa();",
5603 NoBinPacking);
5604 verifyFormat("void f() {\n"
5605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5606 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
5607 "}",
5608 NoBinPacking);
5609
5610 verifyFormat(
5611 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5612 " aaaaaaaaaaaa,\n"
5613 " aaaaaaaaaaaa);",
5614 NoBinPacking);
5615 verifyFormat(
5616 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
5617 " ddddddddddddddddddddddddddddd),\n"
5618 " test);",
5619 NoBinPacking);
5620
5621 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
5622 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
5623 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
5624 " aaaaaaaaaaaaaaaaaa;",
5625 NoBinPacking);
5626 verifyFormat("a(\"a\"\n"
5627 " \"a\",\n"
5628 " a);");
5629
5630 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
5631 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
5632 " aaaaaaaaa,\n"
5633 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5634 NoBinPacking);
5635 verifyFormat(
5636 "void f() {\n"
5637 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
5638 " .aaaaaaa();\n"
5639 "}",
5640 NoBinPacking);
5641 verifyFormat(
5642 "template <class SomeType, class SomeOtherType>\n"
5643 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
5644 NoBinPacking);
5645 }
5646
TEST_F(FormatTest,AdaptiveOnePerLineFormatting)5647 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
5648 FormatStyle Style = getLLVMStyleWithColumns(15);
5649 Style.ExperimentalAutoDetectBinPacking = true;
5650 EXPECT_EQ("aaa(aaaa,\n"
5651 " aaaa,\n"
5652 " aaaa);\n"
5653 "aaa(aaaa,\n"
5654 " aaaa,\n"
5655 " aaaa);",
5656 format("aaa(aaaa,\n" // one-per-line
5657 " aaaa,\n"
5658 " aaaa );\n"
5659 "aaa(aaaa, aaaa, aaaa);", // inconclusive
5660 Style));
5661 EXPECT_EQ("aaa(aaaa, aaaa,\n"
5662 " aaaa);\n"
5663 "aaa(aaaa, aaaa,\n"
5664 " aaaa);",
5665 format("aaa(aaaa, aaaa,\n" // bin-packed
5666 " aaaa );\n"
5667 "aaa(aaaa, aaaa, aaaa);", // inconclusive
5668 Style));
5669 }
5670
TEST_F(FormatTest,FormatsBuilderPattern)5671 TEST_F(FormatTest, FormatsBuilderPattern) {
5672 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
5673 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
5674 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
5675 " .StartsWith(\".init\", ORDER_INIT)\n"
5676 " .StartsWith(\".fini\", ORDER_FINI)\n"
5677 " .StartsWith(\".hash\", ORDER_HASH)\n"
5678 " .Default(ORDER_TEXT);\n");
5679
5680 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
5681 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
5682 verifyFormat("aaaaaaa->aaaaaaa\n"
5683 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5684 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5685 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
5686 verifyFormat(
5687 "aaaaaaa->aaaaaaa\n"
5688 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5689 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
5690 verifyFormat(
5691 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
5692 " aaaaaaaaaaaaaa);");
5693 verifyFormat(
5694 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
5695 " aaaaaa->aaaaaaaaaaaa()\n"
5696 " ->aaaaaaaaaaaaaaaa(\n"
5697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5698 " ->aaaaaaaaaaaaaaaaa();");
5699 verifyGoogleFormat(
5700 "void f() {\n"
5701 " someo->Add((new util::filetools::Handler(dir))\n"
5702 " ->OnEvent1(NewPermanentCallback(\n"
5703 " this, &HandlerHolderClass::EventHandlerCBA))\n"
5704 " ->OnEvent2(NewPermanentCallback(\n"
5705 " this, &HandlerHolderClass::EventHandlerCBB))\n"
5706 " ->OnEvent3(NewPermanentCallback(\n"
5707 " this, &HandlerHolderClass::EventHandlerCBC))\n"
5708 " ->OnEvent5(NewPermanentCallback(\n"
5709 " this, &HandlerHolderClass::EventHandlerCBD))\n"
5710 " ->OnEvent6(NewPermanentCallback(\n"
5711 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
5712 "}");
5713
5714 verifyFormat(
5715 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
5716 verifyFormat("aaaaaaaaaaaaaaa()\n"
5717 " .aaaaaaaaaaaaaaa()\n"
5718 " .aaaaaaaaaaaaaaa()\n"
5719 " .aaaaaaaaaaaaaaa()\n"
5720 " .aaaaaaaaaaaaaaa();");
5721 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
5722 " .aaaaaaaaaaaaaaa()\n"
5723 " .aaaaaaaaaaaaaaa()\n"
5724 " .aaaaaaaaaaaaaaa();");
5725 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
5726 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
5727 " .aaaaaaaaaaaaaaa();");
5728 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
5729 " ->aaaaaaaaaaaaaae(0)\n"
5730 " ->aaaaaaaaaaaaaaa();");
5731
5732 // Don't linewrap after very short segments.
5733 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5734 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5735 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5736 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5737 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5738 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5739 verifyFormat("aaa()\n"
5740 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5741 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5742 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5743
5744 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
5745 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5746 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
5747 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
5748 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
5750
5751 // Prefer not to break after empty parentheses.
5752 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
5753 " First->LastNewlineOffset);");
5754
5755 // Prefer not to create "hanging" indents.
5756 verifyFormat(
5757 "return !soooooooooooooome_map\n"
5758 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5759 " .second;");
5760 verifyFormat(
5761 "return aaaaaaaaaaaaaaaa\n"
5762 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
5763 " .aaaa(aaaaaaaaaaaaaa);");
5764 // No hanging indent here.
5765 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
5766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5767 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
5768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5769 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
5770 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5771 getLLVMStyleWithColumns(60));
5772 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
5773 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
5774 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5775 getLLVMStyleWithColumns(59));
5776 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5778 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5779
5780 // Dont break if only closing statements before member call
5781 verifyFormat("test() {\n"
5782 " ([]() -> {\n"
5783 " int b = 32;\n"
5784 " return 3;\n"
5785 " }).foo();\n"
5786 "}");
5787 verifyFormat("test() {\n"
5788 " (\n"
5789 " []() -> {\n"
5790 " int b = 32;\n"
5791 " return 3;\n"
5792 " },\n"
5793 " foo, bar)\n"
5794 " .foo();\n"
5795 "}");
5796 verifyFormat("test() {\n"
5797 " ([]() -> {\n"
5798 " int b = 32;\n"
5799 " return 3;\n"
5800 " })\n"
5801 " .foo()\n"
5802 " .bar();\n"
5803 "}");
5804 verifyFormat("test() {\n"
5805 " ([]() -> {\n"
5806 " int b = 32;\n"
5807 " return 3;\n"
5808 " })\n"
5809 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
5810 " \"bbbb\");\n"
5811 "}",
5812 getLLVMStyleWithColumns(30));
5813 }
5814
TEST_F(FormatTest,BreaksAccordingToOperatorPrecedence)5815 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
5816 verifyFormat(
5817 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5818 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
5819 verifyFormat(
5820 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
5821 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
5822
5823 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
5824 " ccccccccccccccccccccccccc) {\n}");
5825 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
5826 " ccccccccccccccccccccccccc) {\n}");
5827
5828 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
5829 " ccccccccccccccccccccccccc) {\n}");
5830 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
5831 " ccccccccccccccccccccccccc) {\n}");
5832
5833 verifyFormat(
5834 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
5835 " ccccccccccccccccccccccccc) {\n}");
5836 verifyFormat(
5837 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
5838 " ccccccccccccccccccccccccc) {\n}");
5839
5840 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
5841 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
5842 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
5843 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
5844 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
5845 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
5846 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
5847 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
5848
5849 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
5850 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
5851 " aaaaaaaaaaaaaaa != aa) {\n}");
5852 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
5853 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
5854 " aaaaaaaaaaaaaaa != aa) {\n}");
5855 }
5856
TEST_F(FormatTest,BreaksAfterAssignments)5857 TEST_F(FormatTest, BreaksAfterAssignments) {
5858 verifyFormat(
5859 "unsigned Cost =\n"
5860 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
5861 " SI->getPointerAddressSpaceee());\n");
5862 verifyFormat(
5863 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
5864 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
5865
5866 verifyFormat(
5867 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
5868 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
5869 verifyFormat("unsigned OriginalStartColumn =\n"
5870 " SourceMgr.getSpellingColumnNumber(\n"
5871 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
5872 " 1;");
5873 }
5874
TEST_F(FormatTest,ConfigurableBreakAssignmentPenalty)5875 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
5876 FormatStyle Style = getLLVMStyle();
5877 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5878 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
5879 Style);
5880
5881 Style.PenaltyBreakAssignment = 20;
5882 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
5883 " cccccccccccccccccccccccccc;",
5884 Style);
5885 }
5886
TEST_F(FormatTest,AlignsAfterAssignments)5887 TEST_F(FormatTest, AlignsAfterAssignments) {
5888 verifyFormat(
5889 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5890 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
5891 verifyFormat(
5892 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5893 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
5894 verifyFormat(
5895 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5896 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
5897 verifyFormat(
5898 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5899 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
5900 verifyFormat(
5901 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
5902 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
5903 " aaaaaaaaaaaaaaaaaaaaaaaa;");
5904 }
5905
TEST_F(FormatTest,AlignsAfterReturn)5906 TEST_F(FormatTest, AlignsAfterReturn) {
5907 verifyFormat(
5908 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5909 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
5910 verifyFormat(
5911 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
5912 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
5913 verifyFormat(
5914 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
5915 " aaaaaaaaaaaaaaaaaaaaaa();");
5916 verifyFormat(
5917 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
5918 " aaaaaaaaaaaaaaaaaaaaaa());");
5919 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5921 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
5923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5924 verifyFormat("return\n"
5925 " // true if code is one of a or b.\n"
5926 " code == a || code == b;");
5927 }
5928
TEST_F(FormatTest,AlignsAfterOpenBracket)5929 TEST_F(FormatTest, AlignsAfterOpenBracket) {
5930 verifyFormat(
5931 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
5932 " aaaaaaaaa aaaaaaa) {}");
5933 verifyFormat(
5934 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
5935 " aaaaaaaaaaa aaaaaaaaa);");
5936 verifyFormat(
5937 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
5938 " aaaaaaaaaaaaaaaaaaaaa));");
5939 FormatStyle Style = getLLVMStyle();
5940 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5941 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5942 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
5943 Style);
5944 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
5945 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
5946 Style);
5947 verifyFormat("SomeLongVariableName->someFunction(\n"
5948 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
5949 Style);
5950 verifyFormat(
5951 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
5952 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5953 Style);
5954 verifyFormat(
5955 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
5956 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5957 Style);
5958 verifyFormat(
5959 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
5960 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
5961 Style);
5962
5963 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
5964 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
5965 " b));",
5966 Style);
5967
5968 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5969 Style.BinPackArguments = false;
5970 Style.BinPackParameters = false;
5971 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5972 " aaaaaaaaaaa aaaaaaaa,\n"
5973 " aaaaaaaaa aaaaaaa,\n"
5974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
5975 Style);
5976 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
5977 " aaaaaaaaaaa aaaaaaaaa,\n"
5978 " aaaaaaaaaaa aaaaaaaaa,\n"
5979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5980 Style);
5981 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
5982 " aaaaaaaaaaaaaaa,\n"
5983 " aaaaaaaaaaaaaaaaaaaaa,\n"
5984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
5985 Style);
5986 verifyFormat(
5987 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
5988 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
5989 Style);
5990 verifyFormat(
5991 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
5992 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
5993 Style);
5994 verifyFormat(
5995 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
5996 " aaaaaaaaaaaaaaaaaaaaa(\n"
5997 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
5998 " aaaaaaaaaaaaaaaa);",
5999 Style);
6000 verifyFormat(
6001 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6002 " aaaaaaaaaaaaaaaaaaaaa(\n"
6003 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
6004 " aaaaaaaaaaaaaaaa);",
6005 Style);
6006 }
6007
TEST_F(FormatTest,ParenthesesAndOperandAlignment)6008 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
6009 FormatStyle Style = getLLVMStyleWithColumns(40);
6010 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6011 " bbbbbbbbbbbbbbbbbbbbbb);",
6012 Style);
6013 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
6014 Style.AlignOperands = FormatStyle::OAS_DontAlign;
6015 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6016 " bbbbbbbbbbbbbbbbbbbbbb);",
6017 Style);
6018 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6019 Style.AlignOperands = FormatStyle::OAS_Align;
6020 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6021 " bbbbbbbbbbbbbbbbbbbbbb);",
6022 Style);
6023 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
6024 Style.AlignOperands = FormatStyle::OAS_DontAlign;
6025 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
6026 " bbbbbbbbbbbbbbbbbbbbbb);",
6027 Style);
6028 }
6029
TEST_F(FormatTest,BreaksConditionalExpressions)6030 TEST_F(FormatTest, BreaksConditionalExpressions) {
6031 verifyFormat(
6032 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6033 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6034 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6035 verifyFormat(
6036 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
6037 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6038 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6039 verifyFormat(
6040 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6041 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6042 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
6043 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6044 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6045 verifyFormat(
6046 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
6047 " : aaaaaaaaaaaaa);");
6048 verifyFormat(
6049 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6050 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6051 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6052 " aaaaaaaaaaaaa);");
6053 verifyFormat(
6054 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6055 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6056 " aaaaaaaaaaaaa);");
6057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6058 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6060 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6062 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6064 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6066 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6068 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6069 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6071 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6073 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6074 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6075 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6076 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6077 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
6078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6079 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6080 " : aaaaaaaaaaaaaaaa;");
6081 verifyFormat(
6082 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6083 " ? aaaaaaaaaaaaaaa\n"
6084 " : aaaaaaaaaaaaaaa;");
6085 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
6086 " aaaaaaaaa\n"
6087 " ? b\n"
6088 " : c);");
6089 verifyFormat("return aaaa == bbbb\n"
6090 " // comment\n"
6091 " ? aaaa\n"
6092 " : bbbb;");
6093 verifyFormat("unsigned Indent =\n"
6094 " format(TheLine.First,\n"
6095 " IndentForLevel[TheLine.Level] >= 0\n"
6096 " ? IndentForLevel[TheLine.Level]\n"
6097 " : TheLine * 2,\n"
6098 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
6099 getLLVMStyleWithColumns(60));
6100 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
6101 " ? aaaaaaaaaaaaaaa\n"
6102 " : bbbbbbbbbbbbbbb //\n"
6103 " ? ccccccccccccccc\n"
6104 " : ddddddddddddddd;");
6105 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
6106 " ? aaaaaaaaaaaaaaa\n"
6107 " : (bbbbbbbbbbbbbbb //\n"
6108 " ? ccccccccccccccc\n"
6109 " : ddddddddddddddd);");
6110 verifyFormat(
6111 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6112 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6113 " aaaaaaaaaaaaaaaaaaaaa +\n"
6114 " aaaaaaaaaaaaaaaaaaaaa\n"
6115 " : aaaaaaaaaa;");
6116 verifyFormat(
6117 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6118 " : aaaaaaaaaaaaaaaaaaaaaa\n"
6119 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6120
6121 FormatStyle NoBinPacking = getLLVMStyle();
6122 NoBinPacking.BinPackArguments = false;
6123 verifyFormat(
6124 "void f() {\n"
6125 " g(aaa,\n"
6126 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
6127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6128 " ? aaaaaaaaaaaaaaa\n"
6129 " : aaaaaaaaaaaaaaa);\n"
6130 "}",
6131 NoBinPacking);
6132 verifyFormat(
6133 "void f() {\n"
6134 " g(aaa,\n"
6135 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
6136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6137 " ?: aaaaaaaaaaaaaaa);\n"
6138 "}",
6139 NoBinPacking);
6140
6141 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
6142 " // comment.\n"
6143 " ccccccccccccccccccccccccccccccccccccccc\n"
6144 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6145 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
6146
6147 // Assignments in conditional expressions. Apparently not uncommon :-(.
6148 verifyFormat("return a != b\n"
6149 " // comment\n"
6150 " ? a = b\n"
6151 " : a = b;");
6152 verifyFormat("return a != b\n"
6153 " // comment\n"
6154 " ? a = a != b\n"
6155 " // comment\n"
6156 " ? a = b\n"
6157 " : a\n"
6158 " : a;\n");
6159 verifyFormat("return a != b\n"
6160 " // comment\n"
6161 " ? a\n"
6162 " : a = a != b\n"
6163 " // comment\n"
6164 " ? a = b\n"
6165 " : a;");
6166
6167 // Chained conditionals
6168 FormatStyle Style = getLLVMStyle();
6169 Style.ColumnLimit = 70;
6170 Style.AlignOperands = FormatStyle::OAS_Align;
6171 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6172 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6173 " : 3333333333333333;",
6174 Style);
6175 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6176 " : bbbbbbbbbb ? 2222222222222222\n"
6177 " : 3333333333333333;",
6178 Style);
6179 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
6180 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
6181 " : 3333333333333333;",
6182 Style);
6183 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6184 " : bbbbbbbbbbbbbb ? 222222\n"
6185 " : 333333;",
6186 Style);
6187 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6188 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6189 " : cccccccccccccc ? 3333333333333333\n"
6190 " : 4444444444444444;",
6191 Style);
6192 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
6193 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6194 " : 3333333333333333;",
6195 Style);
6196 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6197 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6198 " : (aaa ? bbb : ccc);",
6199 Style);
6200 verifyFormat(
6201 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6202 " : cccccccccccccccccc)\n"
6203 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6204 " : 3333333333333333;",
6205 Style);
6206 verifyFormat(
6207 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6208 " : cccccccccccccccccc)\n"
6209 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6210 " : 3333333333333333;",
6211 Style);
6212 verifyFormat(
6213 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6214 " : dddddddddddddddddd)\n"
6215 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6216 " : 3333333333333333;",
6217 Style);
6218 verifyFormat(
6219 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6220 " : dddddddddddddddddd)\n"
6221 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6222 " : 3333333333333333;",
6223 Style);
6224 verifyFormat(
6225 "return aaaaaaaaa ? 1111111111111111\n"
6226 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6227 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6228 " : dddddddddddddddddd)\n",
6229 Style);
6230 verifyFormat(
6231 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
6232 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6233 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6234 " : cccccccccccccccccc);",
6235 Style);
6236 verifyFormat(
6237 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6238 " : ccccccccccccccc ? dddddddddddddddddd\n"
6239 " : eeeeeeeeeeeeeeeeee)\n"
6240 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6241 " : 3333333333333333;",
6242 Style);
6243 verifyFormat(
6244 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6245 " : ccccccccccccccc ? dddddddddddddddddd\n"
6246 " : eeeeeeeeeeeeeeeeee)\n"
6247 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6248 " : 3333333333333333;",
6249 Style);
6250 verifyFormat(
6251 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6252 " : cccccccccccc ? dddddddddddddddddd\n"
6253 " : eeeeeeeeeeeeeeeeee)\n"
6254 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6255 " : 3333333333333333;",
6256 Style);
6257 verifyFormat(
6258 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6259 " : cccccccccccccccccc\n"
6260 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6261 " : 3333333333333333;",
6262 Style);
6263 verifyFormat(
6264 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6265 " : cccccccccccccccc ? dddddddddddddddddd\n"
6266 " : eeeeeeeeeeeeeeeeee\n"
6267 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
6268 " : 3333333333333333;",
6269 Style);
6270 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
6271 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6272 " : cccccccccccccccccc ? dddddddddddddddddd\n"
6273 " : eeeeeeeeeeeeeeeeee)\n"
6274 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
6275 " : 3333333333333333;",
6276 Style);
6277 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
6278 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
6279 " : cccccccccccccccc ? dddddddddddddddddd\n"
6280 " : eeeeeeeeeeeeeeeeee\n"
6281 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
6282 " : 3333333333333333;",
6283 Style);
6284
6285 Style.AlignOperands = FormatStyle::OAS_DontAlign;
6286 Style.BreakBeforeTernaryOperators = false;
6287 // FIXME: Aligning the question marks is weird given DontAlign.
6288 // Consider disabling this alignment in this case. Also check whether this
6289 // will render the adjustment from https://reviews.llvm.org/D82199
6290 // unnecessary.
6291 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
6292 " bbbb ? cccccccccccccccccc :\n"
6293 " ddddd;\n",
6294 Style);
6295 }
6296
TEST_F(FormatTest,BreaksConditionalExpressionsAfterOperator)6297 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
6298 FormatStyle Style = getLLVMStyle();
6299 Style.BreakBeforeTernaryOperators = false;
6300 Style.ColumnLimit = 70;
6301 verifyFormat(
6302 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6305 Style);
6306 verifyFormat(
6307 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
6308 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6309 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6310 Style);
6311 verifyFormat(
6312 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6314 Style);
6315 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
6316 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6318 Style);
6319 verifyFormat(
6320 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
6321 " aaaaaaaaaaaaa);",
6322 Style);
6323 verifyFormat(
6324 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6325 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6327 " aaaaaaaaaaaaa);",
6328 Style);
6329 verifyFormat(
6330 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6331 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6332 " aaaaaaaaaaaaa);",
6333 Style);
6334 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6339 Style);
6340 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
6344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6346 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6347 Style);
6348 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
6350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6352 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
6353 Style);
6354 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6355 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6356 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6357 Style);
6358 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
6359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
6361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
6362 Style);
6363 verifyFormat(
6364 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6365 " aaaaaaaaaaaaaaa :\n"
6366 " aaaaaaaaaaaaaaa;",
6367 Style);
6368 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
6369 " aaaaaaaaa ?\n"
6370 " b :\n"
6371 " c);",
6372 Style);
6373 verifyFormat("unsigned Indent =\n"
6374 " format(TheLine.First,\n"
6375 " IndentForLevel[TheLine.Level] >= 0 ?\n"
6376 " IndentForLevel[TheLine.Level] :\n"
6377 " TheLine * 2,\n"
6378 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
6379 Style);
6380 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
6381 " aaaaaaaaaaaaaaa :\n"
6382 " bbbbbbbbbbbbbbb ? //\n"
6383 " ccccccccccccccc :\n"
6384 " ddddddddddddddd;",
6385 Style);
6386 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
6387 " aaaaaaaaaaaaaaa :\n"
6388 " (bbbbbbbbbbbbbbb ? //\n"
6389 " ccccccccccccccc :\n"
6390 " ddddddddddddddd);",
6391 Style);
6392 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6393 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
6394 " ccccccccccccccccccccccccccc;",
6395 Style);
6396 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
6397 " aaaaa :\n"
6398 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
6399 Style);
6400
6401 // Chained conditionals
6402 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6403 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6404 " 3333333333333333;",
6405 Style);
6406 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6407 " bbbbbbbbbb ? 2222222222222222 :\n"
6408 " 3333333333333333;",
6409 Style);
6410 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
6411 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6412 " 3333333333333333;",
6413 Style);
6414 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6415 " bbbbbbbbbbbbbbbb ? 222222 :\n"
6416 " 333333;",
6417 Style);
6418 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6419 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6420 " cccccccccccccccc ? 3333333333333333 :\n"
6421 " 4444444444444444;",
6422 Style);
6423 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
6424 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6425 " 3333333333333333;",
6426 Style);
6427 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6428 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6429 " (aaa ? bbb : ccc);",
6430 Style);
6431 verifyFormat(
6432 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6433 " cccccccccccccccccc) :\n"
6434 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6435 " 3333333333333333;",
6436 Style);
6437 verifyFormat(
6438 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6439 " cccccccccccccccccc) :\n"
6440 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6441 " 3333333333333333;",
6442 Style);
6443 verifyFormat(
6444 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6445 " dddddddddddddddddd) :\n"
6446 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6447 " 3333333333333333;",
6448 Style);
6449 verifyFormat(
6450 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6451 " dddddddddddddddddd) :\n"
6452 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6453 " 3333333333333333;",
6454 Style);
6455 verifyFormat(
6456 "return aaaaaaaaa ? 1111111111111111 :\n"
6457 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6458 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6459 " dddddddddddddddddd)\n",
6460 Style);
6461 verifyFormat(
6462 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
6463 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6464 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6465 " cccccccccccccccccc);",
6466 Style);
6467 verifyFormat(
6468 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6469 " ccccccccccccccccc ? dddddddddddddddddd :\n"
6470 " eeeeeeeeeeeeeeeeee) :\n"
6471 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6472 " 3333333333333333;",
6473 Style);
6474 verifyFormat(
6475 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6476 " ccccccccccccc ? dddddddddddddddddd :\n"
6477 " eeeeeeeeeeeeeeeeee) :\n"
6478 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6479 " 3333333333333333;",
6480 Style);
6481 verifyFormat(
6482 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6483 " ccccccccccccccccc ? dddddddddddddddddd :\n"
6484 " eeeeeeeeeeeeeeeeee) :\n"
6485 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6486 " 3333333333333333;",
6487 Style);
6488 verifyFormat(
6489 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6490 " cccccccccccccccccc :\n"
6491 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6492 " 3333333333333333;",
6493 Style);
6494 verifyFormat(
6495 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6496 " cccccccccccccccccc ? dddddddddddddddddd :\n"
6497 " eeeeeeeeeeeeeeeeee :\n"
6498 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6499 " 3333333333333333;",
6500 Style);
6501 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
6502 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6503 " cccccccccccccccccc ? dddddddddddddddddd :\n"
6504 " eeeeeeeeeeeeeeeeee) :\n"
6505 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6506 " 3333333333333333;",
6507 Style);
6508 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
6509 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
6510 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
6511 " eeeeeeeeeeeeeeeeee :\n"
6512 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
6513 " 3333333333333333;",
6514 Style);
6515 }
6516
TEST_F(FormatTest,DeclarationsOfMultipleVariables)6517 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
6518 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
6519 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
6520 verifyFormat("bool a = true, b = false;");
6521
6522 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
6524 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
6525 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
6526 verifyFormat(
6527 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
6528 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
6529 " d = e && f;");
6530 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
6531 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
6532 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
6533 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
6534 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
6535 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
6536
6537 FormatStyle Style = getGoogleStyle();
6538 Style.PointerAlignment = FormatStyle::PAS_Left;
6539 Style.DerivePointerAlignment = false;
6540 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6541 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
6542 " *b = bbbbbbbbbbbbbbbbbbb;",
6543 Style);
6544 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
6545 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
6546 Style);
6547 verifyFormat("vector<int*> a, b;", Style);
6548 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
6549 }
6550
TEST_F(FormatTest,ConditionalExpressionsInBrackets)6551 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
6552 verifyFormat("arr[foo ? bar : baz];");
6553 verifyFormat("f()[foo ? bar : baz];");
6554 verifyFormat("(a + b)[foo ? bar : baz];");
6555 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
6556 }
6557
TEST_F(FormatTest,AlignsStringLiterals)6558 TEST_F(FormatTest, AlignsStringLiterals) {
6559 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
6560 " \"short literal\");");
6561 verifyFormat(
6562 "looooooooooooooooooooooooongFunction(\n"
6563 " \"short literal\"\n"
6564 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
6565 verifyFormat("someFunction(\"Always break between multi-line\"\n"
6566 " \" string literals\",\n"
6567 " and, other, parameters);");
6568 EXPECT_EQ("fun + \"1243\" /* comment */\n"
6569 " \"5678\";",
6570 format("fun + \"1243\" /* comment */\n"
6571 " \"5678\";",
6572 getLLVMStyleWithColumns(28)));
6573 EXPECT_EQ(
6574 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6575 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
6576 " \"aaaaaaaaaaaaaaaa\";",
6577 format("aaaaaa ="
6578 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
6579 "aaaaaaaaaaaaaaaaaaaaa\" "
6580 "\"aaaaaaaaaaaaaaaa\";"));
6581 verifyFormat("a = a + \"a\"\n"
6582 " \"a\"\n"
6583 " \"a\";");
6584 verifyFormat("f(\"a\", \"b\"\n"
6585 " \"c\");");
6586
6587 verifyFormat(
6588 "#define LL_FORMAT \"ll\"\n"
6589 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
6590 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
6591
6592 verifyFormat("#define A(X) \\\n"
6593 " \"aaaaa\" #X \"bbbbbb\" \\\n"
6594 " \"ccccc\"",
6595 getLLVMStyleWithColumns(23));
6596 verifyFormat("#define A \"def\"\n"
6597 "f(\"abc\" A \"ghi\"\n"
6598 " \"jkl\");");
6599
6600 verifyFormat("f(L\"a\"\n"
6601 " L\"b\");");
6602 verifyFormat("#define A(X) \\\n"
6603 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
6604 " L\"ccccc\"",
6605 getLLVMStyleWithColumns(25));
6606
6607 verifyFormat("f(@\"a\"\n"
6608 " @\"b\");");
6609 verifyFormat("NSString s = @\"a\"\n"
6610 " @\"b\"\n"
6611 " @\"c\";");
6612 verifyFormat("NSString s = @\"a\"\n"
6613 " \"b\"\n"
6614 " \"c\";");
6615 }
6616
TEST_F(FormatTest,ReturnTypeBreakingStyle)6617 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
6618 FormatStyle Style = getLLVMStyle();
6619 // No declarations or definitions should be moved to own line.
6620 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
6621 verifyFormat("class A {\n"
6622 " int f() { return 1; }\n"
6623 " int g();\n"
6624 "};\n"
6625 "int f() { return 1; }\n"
6626 "int g();\n",
6627 Style);
6628
6629 // All declarations and definitions should have the return type moved to its
6630 // own
6631 // line.
6632 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
6633 verifyFormat("class E {\n"
6634 " int\n"
6635 " f() {\n"
6636 " return 1;\n"
6637 " }\n"
6638 " int\n"
6639 " g();\n"
6640 "};\n"
6641 "int\n"
6642 "f() {\n"
6643 " return 1;\n"
6644 "}\n"
6645 "int\n"
6646 "g();\n",
6647 Style);
6648
6649 // Top-level definitions, and no kinds of declarations should have the
6650 // return type moved to its own line.
6651 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
6652 verifyFormat("class B {\n"
6653 " int f() { return 1; }\n"
6654 " int g();\n"
6655 "};\n"
6656 "int\n"
6657 "f() {\n"
6658 " return 1;\n"
6659 "}\n"
6660 "int g();\n",
6661 Style);
6662
6663 // Top-level definitions and declarations should have the return type moved
6664 // to its own line.
6665 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
6666 verifyFormat("class C {\n"
6667 " int f() { return 1; }\n"
6668 " int g();\n"
6669 "};\n"
6670 "int\n"
6671 "f() {\n"
6672 " return 1;\n"
6673 "}\n"
6674 "int\n"
6675 "g();\n",
6676 Style);
6677
6678 // All definitions should have the return type moved to its own line, but no
6679 // kinds of declarations.
6680 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
6681 verifyFormat("class D {\n"
6682 " int\n"
6683 " f() {\n"
6684 " return 1;\n"
6685 " }\n"
6686 " int g();\n"
6687 "};\n"
6688 "int\n"
6689 "f() {\n"
6690 " return 1;\n"
6691 "}\n"
6692 "int g();\n",
6693 Style);
6694 verifyFormat("const char *\n"
6695 "f(void) {\n" // Break here.
6696 " return \"\";\n"
6697 "}\n"
6698 "const char *bar(void);\n", // No break here.
6699 Style);
6700 verifyFormat("template <class T>\n"
6701 "T *\n"
6702 "f(T &c) {\n" // Break here.
6703 " return NULL;\n"
6704 "}\n"
6705 "template <class T> T *f(T &c);\n", // No break here.
6706 Style);
6707 verifyFormat("class C {\n"
6708 " int\n"
6709 " operator+() {\n"
6710 " return 1;\n"
6711 " }\n"
6712 " int\n"
6713 " operator()() {\n"
6714 " return 1;\n"
6715 " }\n"
6716 "};\n",
6717 Style);
6718 verifyFormat("void\n"
6719 "A::operator()() {}\n"
6720 "void\n"
6721 "A::operator>>() {}\n"
6722 "void\n"
6723 "A::operator+() {}\n"
6724 "void\n"
6725 "A::operator*() {}\n"
6726 "void\n"
6727 "A::operator->() {}\n"
6728 "void\n"
6729 "A::operator void *() {}\n"
6730 "void\n"
6731 "A::operator void &() {}\n"
6732 "void\n"
6733 "A::operator void &&() {}\n"
6734 "void\n"
6735 "A::operator char *() {}\n"
6736 "void\n"
6737 "A::operator[]() {}\n"
6738 "void\n"
6739 "A::operator!() {}\n"
6740 "void\n"
6741 "A::operator**() {}\n"
6742 "void\n"
6743 "A::operator<Foo> *() {}\n"
6744 "void\n"
6745 "A::operator<Foo> **() {}\n"
6746 "void\n"
6747 "A::operator<Foo> &() {}\n"
6748 "void\n"
6749 "A::operator void **() {}\n",
6750 Style);
6751 verifyFormat("constexpr auto\n"
6752 "operator()() const -> reference {}\n"
6753 "constexpr auto\n"
6754 "operator>>() const -> reference {}\n"
6755 "constexpr auto\n"
6756 "operator+() const -> reference {}\n"
6757 "constexpr auto\n"
6758 "operator*() const -> reference {}\n"
6759 "constexpr auto\n"
6760 "operator->() const -> reference {}\n"
6761 "constexpr auto\n"
6762 "operator++() const -> reference {}\n"
6763 "constexpr auto\n"
6764 "operator void *() const -> reference {}\n"
6765 "constexpr auto\n"
6766 "operator void **() const -> reference {}\n"
6767 "constexpr auto\n"
6768 "operator void *() const -> reference {}\n"
6769 "constexpr auto\n"
6770 "operator void &() const -> reference {}\n"
6771 "constexpr auto\n"
6772 "operator void &&() const -> reference {}\n"
6773 "constexpr auto\n"
6774 "operator char *() const -> reference {}\n"
6775 "constexpr auto\n"
6776 "operator!() const -> reference {}\n"
6777 "constexpr auto\n"
6778 "operator[]() const -> reference {}\n",
6779 Style);
6780 verifyFormat("void *operator new(std::size_t s);", // No break here.
6781 Style);
6782 verifyFormat("void *\n"
6783 "operator new(std::size_t s) {}",
6784 Style);
6785 verifyFormat("void *\n"
6786 "operator delete[](void *ptr) {}",
6787 Style);
6788 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
6789 verifyFormat("const char *\n"
6790 "f(void)\n" // Break here.
6791 "{\n"
6792 " return \"\";\n"
6793 "}\n"
6794 "const char *bar(void);\n", // No break here.
6795 Style);
6796 verifyFormat("template <class T>\n"
6797 "T *\n" // Problem here: no line break
6798 "f(T &c)\n" // Break here.
6799 "{\n"
6800 " return NULL;\n"
6801 "}\n"
6802 "template <class T> T *f(T &c);\n", // No break here.
6803 Style);
6804 verifyFormat("int\n"
6805 "foo(A<bool> a)\n"
6806 "{\n"
6807 " return a;\n"
6808 "}\n",
6809 Style);
6810 verifyFormat("int\n"
6811 "foo(A<8> a)\n"
6812 "{\n"
6813 " return a;\n"
6814 "}\n",
6815 Style);
6816 verifyFormat("int\n"
6817 "foo(A<B<bool>, 8> a)\n"
6818 "{\n"
6819 " return a;\n"
6820 "}\n",
6821 Style);
6822 verifyFormat("int\n"
6823 "foo(A<B<8>, bool> a)\n"
6824 "{\n"
6825 " return a;\n"
6826 "}\n",
6827 Style);
6828 verifyFormat("int\n"
6829 "foo(A<B<bool>, bool> a)\n"
6830 "{\n"
6831 " return a;\n"
6832 "}\n",
6833 Style);
6834 verifyFormat("int\n"
6835 "foo(A<B<8>, 8> a)\n"
6836 "{\n"
6837 " return a;\n"
6838 "}\n",
6839 Style);
6840
6841 Style = getGNUStyle();
6842
6843 // Test for comments at the end of function declarations.
6844 verifyFormat("void\n"
6845 "foo (int a, /*abc*/ int b) // def\n"
6846 "{\n"
6847 "}\n",
6848 Style);
6849
6850 verifyFormat("void\n"
6851 "foo (int a, /* abc */ int b) /* def */\n"
6852 "{\n"
6853 "}\n",
6854 Style);
6855
6856 // Definitions that should not break after return type
6857 verifyFormat("void foo (int a, int b); // def\n", Style);
6858 verifyFormat("void foo (int a, int b); /* def */\n", Style);
6859 verifyFormat("void foo (int a, int b);\n", Style);
6860 }
6861
TEST_F(FormatTest,AlwaysBreakBeforeMultilineStrings)6862 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
6863 FormatStyle NoBreak = getLLVMStyle();
6864 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
6865 FormatStyle Break = getLLVMStyle();
6866 Break.AlwaysBreakBeforeMultilineStrings = true;
6867 verifyFormat("aaaa = \"bbbb\"\n"
6868 " \"cccc\";",
6869 NoBreak);
6870 verifyFormat("aaaa =\n"
6871 " \"bbbb\"\n"
6872 " \"cccc\";",
6873 Break);
6874 verifyFormat("aaaa(\"bbbb\"\n"
6875 " \"cccc\");",
6876 NoBreak);
6877 verifyFormat("aaaa(\n"
6878 " \"bbbb\"\n"
6879 " \"cccc\");",
6880 Break);
6881 verifyFormat("aaaa(qqq, \"bbbb\"\n"
6882 " \"cccc\");",
6883 NoBreak);
6884 verifyFormat("aaaa(qqq,\n"
6885 " \"bbbb\"\n"
6886 " \"cccc\");",
6887 Break);
6888 verifyFormat("aaaa(qqq,\n"
6889 " L\"bbbb\"\n"
6890 " L\"cccc\");",
6891 Break);
6892 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
6893 " \"bbbb\"));",
6894 Break);
6895 verifyFormat("string s = someFunction(\n"
6896 " \"abc\"\n"
6897 " \"abc\");",
6898 Break);
6899
6900 // As we break before unary operators, breaking right after them is bad.
6901 verifyFormat("string foo = abc ? \"x\"\n"
6902 " \"blah blah blah blah blah blah\"\n"
6903 " : \"y\";",
6904 Break);
6905
6906 // Don't break if there is no column gain.
6907 verifyFormat("f(\"aaaa\"\n"
6908 " \"bbbb\");",
6909 Break);
6910
6911 // Treat literals with escaped newlines like multi-line string literals.
6912 EXPECT_EQ("x = \"a\\\n"
6913 "b\\\n"
6914 "c\";",
6915 format("x = \"a\\\n"
6916 "b\\\n"
6917 "c\";",
6918 NoBreak));
6919 EXPECT_EQ("xxxx =\n"
6920 " \"a\\\n"
6921 "b\\\n"
6922 "c\";",
6923 format("xxxx = \"a\\\n"
6924 "b\\\n"
6925 "c\";",
6926 Break));
6927
6928 EXPECT_EQ("NSString *const kString =\n"
6929 " @\"aaaa\"\n"
6930 " @\"bbbb\";",
6931 format("NSString *const kString = @\"aaaa\"\n"
6932 "@\"bbbb\";",
6933 Break));
6934
6935 Break.ColumnLimit = 0;
6936 verifyFormat("const char *hello = \"hello llvm\";", Break);
6937 }
6938
TEST_F(FormatTest,AlignsPipes)6939 TEST_F(FormatTest, AlignsPipes) {
6940 verifyFormat(
6941 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6942 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6943 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6944 verifyFormat(
6945 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
6946 " << aaaaaaaaaaaaaaaaaaaa;");
6947 verifyFormat(
6948 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6949 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6950 verifyFormat(
6951 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
6952 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6953 verifyFormat(
6954 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
6955 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
6956 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
6957 verifyFormat(
6958 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6959 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6960 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6961 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6964 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6965 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
6966 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
6967 verifyFormat(
6968 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6969 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6970 verifyFormat(
6971 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
6972 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
6973
6974 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
6975 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
6976 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6978 " aaaaaaaaaaaaaaaaaaaaa)\n"
6979 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
6980 verifyFormat("LOG_IF(aaa == //\n"
6981 " bbb)\n"
6982 " << a << b;");
6983
6984 // But sometimes, breaking before the first "<<" is desirable.
6985 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
6986 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
6987 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
6988 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6989 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6990 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
6991 " << BEF << IsTemplate << Description << E->getType();");
6992 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
6993 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6995 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
6996 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6997 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
6998 " << aaa;");
6999
7000 verifyFormat(
7001 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7002 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7003
7004 // Incomplete string literal.
7005 EXPECT_EQ("llvm::errs() << \"\n"
7006 " << a;",
7007 format("llvm::errs() << \"\n<<a;"));
7008
7009 verifyFormat("void f() {\n"
7010 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
7011 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
7012 "}");
7013
7014 // Handle 'endl'.
7015 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
7016 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
7017 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
7018
7019 // Handle '\n'.
7020 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
7021 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
7022 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
7023 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
7024 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
7025 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
7026 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
7027 }
7028
TEST_F(FormatTest,KeepStringLabelValuePairsOnALine)7029 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
7030 verifyFormat("return out << \"somepacket = {\\n\"\n"
7031 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
7032 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
7033 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
7034 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
7035 " << \"}\";");
7036
7037 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
7038 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
7039 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
7040 verifyFormat(
7041 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
7042 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
7043 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
7044 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
7045 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
7046 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
7047 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
7048 verifyFormat(
7049 "void f() {\n"
7050 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
7051 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
7052 "}");
7053
7054 // Breaking before the first "<<" is generally not desirable.
7055 verifyFormat(
7056 "llvm::errs()\n"
7057 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7058 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7059 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7060 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7061 getLLVMStyleWithColumns(70));
7062 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7063 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7064 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7065 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7066 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
7067 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7068 getLLVMStyleWithColumns(70));
7069
7070 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
7071 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
7072 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
7073 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
7074 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
7075 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
7076 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
7077 " (aaaa + aaaa);",
7078 getLLVMStyleWithColumns(40));
7079 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
7080 " (aaaaaaa + aaaaa));",
7081 getLLVMStyleWithColumns(40));
7082 verifyFormat(
7083 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
7084 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
7085 " bbbbbbbbbbbbbbbbbbbbbbb);");
7086 }
7087
TEST_F(FormatTest,UnderstandsEquals)7088 TEST_F(FormatTest, UnderstandsEquals) {
7089 verifyFormat(
7090 "aaaaaaaaaaaaaaaaa =\n"
7091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7092 verifyFormat(
7093 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
7095 verifyFormat(
7096 "if (a) {\n"
7097 " f();\n"
7098 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7100 "}");
7101
7102 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7103 " 100000000 + 10000000) {\n}");
7104 }
7105
TEST_F(FormatTest,WrapsAtFunctionCallsIfNecessary)7106 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
7107 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
7108 " .looooooooooooooooooooooooooooooooooooooongFunction();");
7109
7110 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
7111 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
7112
7113 verifyFormat(
7114 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
7115 " Parameter2);");
7116
7117 verifyFormat(
7118 "ShortObject->shortFunction(\n"
7119 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
7120 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
7121
7122 verifyFormat("loooooooooooooongFunction(\n"
7123 " LoooooooooooooongObject->looooooooooooooooongFunction());");
7124
7125 verifyFormat(
7126 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
7127 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
7128
7129 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
7130 " .WillRepeatedly(Return(SomeValue));");
7131 verifyFormat("void f() {\n"
7132 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
7133 " .Times(2)\n"
7134 " .WillRepeatedly(Return(SomeValue));\n"
7135 "}");
7136 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
7137 " ccccccccccccccccccccccc);");
7138 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7140 " .aaaaa(aaaaa),\n"
7141 " aaaaaaaaaaaaaaaaaaaaa);");
7142 verifyFormat("void f() {\n"
7143 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7144 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
7145 "}");
7146 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7148 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7149 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7150 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7151 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7152 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7153 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7154 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
7155 "}");
7156
7157 // Here, it is not necessary to wrap at "." or "->".
7158 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
7159 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
7160 verifyFormat(
7161 "aaaaaaaaaaa->aaaaaaaaa(\n"
7162 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7163 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
7164
7165 verifyFormat(
7166 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
7168 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
7169 " aaaaaaaaa()->aaaaaa()->aaaaa());");
7170 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
7171 " aaaaaaaaa()->aaaaaa()->aaaaa());");
7172
7173 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7175 " .a();");
7176
7177 FormatStyle NoBinPacking = getLLVMStyle();
7178 NoBinPacking.BinPackParameters = false;
7179 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7180 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7181 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
7182 " aaaaaaaaaaaaaaaaaaa,\n"
7183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7184 NoBinPacking);
7185
7186 // If there is a subsequent call, change to hanging indentation.
7187 verifyFormat(
7188 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7189 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
7190 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7191 verifyFormat(
7192 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7193 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
7194 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7196 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7197 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7199 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
7200 }
7201
TEST_F(FormatTest,WrapsTemplateDeclarations)7202 TEST_F(FormatTest, WrapsTemplateDeclarations) {
7203 verifyFormat("template <typename T>\n"
7204 "virtual void loooooooooooongFunction(int Param1, int Param2);");
7205 verifyFormat("template <typename T>\n"
7206 "// T should be one of {A, B}.\n"
7207 "virtual void loooooooooooongFunction(int Param1, int Param2);");
7208 verifyFormat(
7209 "template <typename T>\n"
7210 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
7211 verifyFormat("template <typename T>\n"
7212 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
7213 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
7214 verifyFormat(
7215 "template <typename T>\n"
7216 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
7217 " int Paaaaaaaaaaaaaaaaaaaaram2);");
7218 verifyFormat(
7219 "template <typename T>\n"
7220 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
7221 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
7222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7223 verifyFormat("template <typename T>\n"
7224 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7225 " int aaaaaaaaaaaaaaaaaaaaaa);");
7226 verifyFormat(
7227 "template <typename T1, typename T2 = char, typename T3 = char,\n"
7228 " typename T4 = char>\n"
7229 "void f();");
7230 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
7231 " template <typename> class cccccccccccccccccccccc,\n"
7232 " typename ddddddddddddd>\n"
7233 "class C {};");
7234 verifyFormat(
7235 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
7236 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7237
7238 verifyFormat("void f() {\n"
7239 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
7240 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
7241 "}");
7242
7243 verifyFormat("template <typename T> class C {};");
7244 verifyFormat("template <typename T> void f();");
7245 verifyFormat("template <typename T> void f() {}");
7246 verifyFormat(
7247 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
7248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
7250 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
7251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
7253 " bbbbbbbbbbbbbbbbbbbbbbbb);",
7254 getLLVMStyleWithColumns(72));
7255 EXPECT_EQ("static_cast<A< //\n"
7256 " B> *>(\n"
7257 "\n"
7258 ");",
7259 format("static_cast<A<//\n"
7260 " B>*>(\n"
7261 "\n"
7262 " );"));
7263 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7264 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
7265
7266 FormatStyle AlwaysBreak = getLLVMStyle();
7267 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
7268 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
7269 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
7270 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
7271 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7272 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
7273 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
7274 verifyFormat("template <template <typename> class Fooooooo,\n"
7275 " template <typename> class Baaaaaaar>\n"
7276 "struct C {};",
7277 AlwaysBreak);
7278 verifyFormat("template <typename T> // T can be A, B or C.\n"
7279 "struct C {};",
7280 AlwaysBreak);
7281 verifyFormat("template <enum E> class A {\n"
7282 "public:\n"
7283 " E *f();\n"
7284 "};");
7285
7286 FormatStyle NeverBreak = getLLVMStyle();
7287 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
7288 verifyFormat("template <typename T> class C {};", NeverBreak);
7289 verifyFormat("template <typename T> void f();", NeverBreak);
7290 verifyFormat("template <typename T> void f() {}", NeverBreak);
7291 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
7292 "bbbbbbbbbbbbbbbbbbbb) {}",
7293 NeverBreak);
7294 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7295 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
7296 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
7297 NeverBreak);
7298 verifyFormat("template <template <typename> class Fooooooo,\n"
7299 " template <typename> class Baaaaaaar>\n"
7300 "struct C {};",
7301 NeverBreak);
7302 verifyFormat("template <typename T> // T can be A, B or C.\n"
7303 "struct C {};",
7304 NeverBreak);
7305 verifyFormat("template <enum E> class A {\n"
7306 "public:\n"
7307 " E *f();\n"
7308 "};",
7309 NeverBreak);
7310 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
7311 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
7312 "bbbbbbbbbbbbbbbbbbbb) {}",
7313 NeverBreak);
7314 }
7315
TEST_F(FormatTest,WrapsTemplateDeclarationsWithComments)7316 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
7317 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
7318 Style.ColumnLimit = 60;
7319 EXPECT_EQ("// Baseline - no comments.\n"
7320 "template <\n"
7321 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
7322 "void f() {}",
7323 format("// Baseline - no comments.\n"
7324 "template <\n"
7325 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
7326 "void f() {}",
7327 Style));
7328
7329 EXPECT_EQ("template <\n"
7330 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7331 "void f() {}",
7332 format("template <\n"
7333 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7334 "void f() {}",
7335 Style));
7336
7337 EXPECT_EQ(
7338 "template <\n"
7339 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
7340 "void f() {}",
7341 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
7342 "void f() {}",
7343 Style));
7344
7345 EXPECT_EQ(
7346 "template <\n"
7347 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7348 " // multiline\n"
7349 "void f() {}",
7350 format("template <\n"
7351 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
7352 " // multiline\n"
7353 "void f() {}",
7354 Style));
7355
7356 EXPECT_EQ(
7357 "template <typename aaaaaaaaaa<\n"
7358 " bbbbbbbbbbbb>::value> // trailing loooong\n"
7359 "void f() {}",
7360 format(
7361 "template <\n"
7362 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
7363 "void f() {}",
7364 Style));
7365 }
7366
TEST_F(FormatTest,WrapsTemplateParameters)7367 TEST_F(FormatTest, WrapsTemplateParameters) {
7368 FormatStyle Style = getLLVMStyle();
7369 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7370 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7371 verifyFormat(
7372 "template <typename... a> struct q {};\n"
7373 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
7374 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
7375 " y;",
7376 Style);
7377 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7378 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7379 verifyFormat(
7380 "template <typename... a> struct r {};\n"
7381 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
7382 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
7383 " y;",
7384 Style);
7385 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7386 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7387 verifyFormat("template <typename... a> struct s {};\n"
7388 "extern s<\n"
7389 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7390 "aaaaaaaaaaaaaaaaaaaaaa,\n"
7391 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7392 "aaaaaaaaaaaaaaaaaaaaaa>\n"
7393 " y;",
7394 Style);
7395 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7396 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7397 verifyFormat("template <typename... a> struct t {};\n"
7398 "extern t<\n"
7399 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7400 "aaaaaaaaaaaaaaaaaaaaaa,\n"
7401 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
7402 "aaaaaaaaaaaaaaaaaaaaaa>\n"
7403 " y;",
7404 Style);
7405 }
7406
TEST_F(FormatTest,WrapsAtNestedNameSpecifiers)7407 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
7408 verifyFormat(
7409 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7411 verifyFormat(
7412 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
7415
7416 // FIXME: Should we have the extra indent after the second break?
7417 verifyFormat(
7418 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7421
7422 verifyFormat(
7423 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
7424 " cccccccccccccccccccccccccccccccccccccccccccccc());");
7425
7426 // Breaking at nested name specifiers is generally not desirable.
7427 verifyFormat(
7428 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7429 " aaaaaaaaaaaaaaaaaaaaaaa);");
7430
7431 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
7432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7434 " aaaaaaaaaaaaaaaaaaaaa);",
7435 getLLVMStyleWithColumns(74));
7436
7437 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
7438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7439 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
7440 }
7441
TEST_F(FormatTest,UnderstandsTemplateParameters)7442 TEST_F(FormatTest, UnderstandsTemplateParameters) {
7443 verifyFormat("A<int> a;");
7444 verifyFormat("A<A<A<int>>> a;");
7445 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
7446 verifyFormat("bool x = a < 1 || 2 > a;");
7447 verifyFormat("bool x = 5 < f<int>();");
7448 verifyFormat("bool x = f<int>() > 5;");
7449 verifyFormat("bool x = 5 < a<int>::x;");
7450 verifyFormat("bool x = a < 4 ? a > 2 : false;");
7451 verifyFormat("bool x = f() ? a < 2 : a > 2;");
7452
7453 verifyGoogleFormat("A<A<int>> a;");
7454 verifyGoogleFormat("A<A<A<int>>> a;");
7455 verifyGoogleFormat("A<A<A<A<int>>>> a;");
7456 verifyGoogleFormat("A<A<int> > a;");
7457 verifyGoogleFormat("A<A<A<int> > > a;");
7458 verifyGoogleFormat("A<A<A<A<int> > > > a;");
7459 verifyGoogleFormat("A<::A<int>> a;");
7460 verifyGoogleFormat("A<::A> a;");
7461 verifyGoogleFormat("A< ::A> a;");
7462 verifyGoogleFormat("A< ::A<int> > a;");
7463 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
7464 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
7465 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
7466 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
7467 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
7468 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
7469
7470 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
7471
7472 // template closer followed by a token that starts with > or =
7473 verifyFormat("bool b = a<1> > 1;");
7474 verifyFormat("bool b = a<1> >= 1;");
7475 verifyFormat("int i = a<1> >> 1;");
7476 FormatStyle Style = getLLVMStyle();
7477 Style.SpaceBeforeAssignmentOperators = false;
7478 verifyFormat("bool b= a<1> == 1;", Style);
7479 verifyFormat("a<int> = 1;", Style);
7480 verifyFormat("a<int> >>= 1;", Style);
7481
7482 verifyFormat("test >> a >> b;");
7483 verifyFormat("test << a >> b;");
7484
7485 verifyFormat("f<int>();");
7486 verifyFormat("template <typename T> void f() {}");
7487 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
7488 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
7489 "sizeof(char)>::type>;");
7490 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
7491 verifyFormat("f(a.operator()<A>());");
7492 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7493 " .template operator()<A>());",
7494 getLLVMStyleWithColumns(35));
7495
7496 // Not template parameters.
7497 verifyFormat("return a < b && c > d;");
7498 verifyFormat("void f() {\n"
7499 " while (a < b && c > d) {\n"
7500 " }\n"
7501 "}");
7502 verifyFormat("template <typename... Types>\n"
7503 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
7504
7505 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
7507 getLLVMStyleWithColumns(60));
7508 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
7509 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
7510 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
7511 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
7512 }
7513
TEST_F(FormatTest,UnderstandsShiftOperators)7514 TEST_F(FormatTest, UnderstandsShiftOperators) {
7515 verifyFormat("if (i < x >> 1)");
7516 verifyFormat("while (i < x >> 1)");
7517 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
7518 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
7519 verifyFormat(
7520 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
7521 verifyFormat("Foo.call<Bar<Function>>()");
7522 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
7523 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
7524 "++i, v = v >> 1)");
7525 verifyFormat("if (w<u<v<x>>, 1>::t)");
7526 }
7527
TEST_F(FormatTest,BitshiftOperatorWidth)7528 TEST_F(FormatTest, BitshiftOperatorWidth) {
7529 EXPECT_EQ("int a = 1 << 2; /* foo\n"
7530 " bar */",
7531 format("int a=1<<2; /* foo\n"
7532 " bar */"));
7533
7534 EXPECT_EQ("int b = 256 >> 1; /* foo\n"
7535 " bar */",
7536 format("int b =256>>1 ; /* foo\n"
7537 " bar */"));
7538 }
7539
TEST_F(FormatTest,UnderstandsBinaryOperators)7540 TEST_F(FormatTest, UnderstandsBinaryOperators) {
7541 verifyFormat("COMPARE(a, ==, b);");
7542 verifyFormat("auto s = sizeof...(Ts) - 1;");
7543 }
7544
TEST_F(FormatTest,UnderstandsPointersToMembers)7545 TEST_F(FormatTest, UnderstandsPointersToMembers) {
7546 verifyFormat("int A::*x;");
7547 verifyFormat("int (S::*func)(void *);");
7548 verifyFormat("void f() { int (S::*func)(void *); }");
7549 verifyFormat("typedef bool *(Class::*Member)() const;");
7550 verifyFormat("void f() {\n"
7551 " (a->*f)();\n"
7552 " a->*x;\n"
7553 " (a.*f)();\n"
7554 " ((*a).*f)();\n"
7555 " a.*x;\n"
7556 "}");
7557 verifyFormat("void f() {\n"
7558 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
7559 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
7560 "}");
7561 verifyFormat(
7562 "(aaaaaaaaaa->*bbbbbbb)(\n"
7563 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
7564 FormatStyle Style = getLLVMStyle();
7565 Style.PointerAlignment = FormatStyle::PAS_Left;
7566 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
7567 }
7568
TEST_F(FormatTest,UnderstandsUnaryOperators)7569 TEST_F(FormatTest, UnderstandsUnaryOperators) {
7570 verifyFormat("int a = -2;");
7571 verifyFormat("f(-1, -2, -3);");
7572 verifyFormat("a[-1] = 5;");
7573 verifyFormat("int a = 5 + -2;");
7574 verifyFormat("if (i == -1) {\n}");
7575 verifyFormat("if (i != -1) {\n}");
7576 verifyFormat("if (i > -1) {\n}");
7577 verifyFormat("if (i < -1) {\n}");
7578 verifyFormat("++(a->f());");
7579 verifyFormat("--(a->f());");
7580 verifyFormat("(a->f())++;");
7581 verifyFormat("a[42]++;");
7582 verifyFormat("if (!(a->f())) {\n}");
7583 verifyFormat("if (!+i) {\n}");
7584 verifyFormat("~&a;");
7585
7586 verifyFormat("a-- > b;");
7587 verifyFormat("b ? -a : c;");
7588 verifyFormat("n * sizeof char16;");
7589 verifyFormat("n * alignof char16;", getGoogleStyle());
7590 verifyFormat("sizeof(char);");
7591 verifyFormat("alignof(char);", getGoogleStyle());
7592
7593 verifyFormat("return -1;");
7594 verifyFormat("throw -1;");
7595 verifyFormat("switch (a) {\n"
7596 "case -1:\n"
7597 " break;\n"
7598 "}");
7599 verifyFormat("#define X -1");
7600 verifyFormat("#define X -kConstant");
7601
7602 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
7603 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
7604
7605 verifyFormat("int a = /* confusing comment */ -1;");
7606 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
7607 verifyFormat("int a = i /* confusing comment */++;");
7608
7609 verifyFormat("co_yield -1;");
7610 verifyFormat("co_return -1;");
7611 }
7612
TEST_F(FormatTest,DoesNotIndentRelativeToUnaryOperators)7613 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
7614 verifyFormat("if (!aaaaaaaaaa( // break\n"
7615 " aaaaa)) {\n"
7616 "}");
7617 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
7618 " aaaaa));");
7619 verifyFormat("*aaa = aaaaaaa( // break\n"
7620 " bbbbbb);");
7621 }
7622
TEST_F(FormatTest,UnderstandsOverloadedOperators)7623 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
7624 verifyFormat("bool operator<();");
7625 verifyFormat("bool operator>();");
7626 verifyFormat("bool operator=();");
7627 verifyFormat("bool operator==();");
7628 verifyFormat("bool operator!=();");
7629 verifyFormat("int operator+();");
7630 verifyFormat("int operator++();");
7631 verifyFormat("int operator++(int) volatile noexcept;");
7632 verifyFormat("bool operator,();");
7633 verifyFormat("bool operator();");
7634 verifyFormat("bool operator()();");
7635 verifyFormat("bool operator[]();");
7636 verifyFormat("operator bool();");
7637 verifyFormat("operator int();");
7638 verifyFormat("operator void *();");
7639 verifyFormat("operator SomeType<int>();");
7640 verifyFormat("operator SomeType<int, int>();");
7641 verifyFormat("operator SomeType<SomeType<int>>();");
7642 verifyFormat("void *operator new(std::size_t size);");
7643 verifyFormat("void *operator new[](std::size_t size);");
7644 verifyFormat("void operator delete(void *ptr);");
7645 verifyFormat("void operator delete[](void *ptr);");
7646 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
7647 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
7648 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
7649 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
7650
7651 verifyFormat(
7652 "ostream &operator<<(ostream &OutputStream,\n"
7653 " SomeReallyLongType WithSomeReallyLongValue);");
7654 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
7655 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
7656 " return left.group < right.group;\n"
7657 "}");
7658 verifyFormat("SomeType &operator=(const SomeType &S);");
7659 verifyFormat("f.template operator()<int>();");
7660
7661 verifyGoogleFormat("operator void*();");
7662 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
7663 verifyGoogleFormat("operator ::A();");
7664
7665 verifyFormat("using A::operator+;");
7666 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
7667 "int i;");
7668 }
7669
TEST_F(FormatTest,UnderstandsFunctionRefQualification)7670 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
7671 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
7672 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
7673 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
7674 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
7675 verifyFormat("Deleted &operator=(const Deleted &) &;");
7676 verifyFormat("Deleted &operator=(const Deleted &) &&;");
7677 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
7678 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
7679 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
7680 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
7681 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
7682 verifyFormat("void Fn(T const &) const &;");
7683 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
7684 verifyFormat("template <typename T>\n"
7685 "void F(T) && = delete;",
7686 getGoogleStyle());
7687
7688 FormatStyle AlignLeft = getLLVMStyle();
7689 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
7690 verifyFormat("void A::b() && {}", AlignLeft);
7691 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
7692 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
7693 AlignLeft);
7694 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
7695 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
7696 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
7697 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
7698 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
7699 verifyFormat("auto Function(T) & -> void;", AlignLeft);
7700 verifyFormat("void Fn(T const&) const&;", AlignLeft);
7701 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
7702
7703 FormatStyle Spaces = getLLVMStyle();
7704 Spaces.SpacesInCStyleCastParentheses = true;
7705 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
7706 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
7707 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
7708 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
7709
7710 Spaces.SpacesInCStyleCastParentheses = false;
7711 Spaces.SpacesInParentheses = true;
7712 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
7713 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
7714 Spaces);
7715 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
7716 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
7717
7718 FormatStyle BreakTemplate = getLLVMStyle();
7719 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
7720
7721 verifyFormat("struct f {\n"
7722 " template <class T>\n"
7723 " int &foo(const std::string &str) &noexcept {}\n"
7724 "};",
7725 BreakTemplate);
7726
7727 verifyFormat("struct f {\n"
7728 " template <class T>\n"
7729 " int &foo(const std::string &str) &&noexcept {}\n"
7730 "};",
7731 BreakTemplate);
7732
7733 verifyFormat("struct f {\n"
7734 " template <class T>\n"
7735 " int &foo(const std::string &str) const &noexcept {}\n"
7736 "};",
7737 BreakTemplate);
7738
7739 verifyFormat("struct f {\n"
7740 " template <class T>\n"
7741 " int &foo(const std::string &str) const &noexcept {}\n"
7742 "};",
7743 BreakTemplate);
7744
7745 verifyFormat("struct f {\n"
7746 " template <class T>\n"
7747 " auto foo(const std::string &str) &&noexcept -> int & {}\n"
7748 "};",
7749 BreakTemplate);
7750
7751 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
7752 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
7753 FormatStyle::BTDS_Yes;
7754 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
7755
7756 verifyFormat("struct f {\n"
7757 " template <class T>\n"
7758 " int& foo(const std::string& str) & noexcept {}\n"
7759 "};",
7760 AlignLeftBreakTemplate);
7761
7762 verifyFormat("struct f {\n"
7763 " template <class T>\n"
7764 " int& foo(const std::string& str) && noexcept {}\n"
7765 "};",
7766 AlignLeftBreakTemplate);
7767
7768 verifyFormat("struct f {\n"
7769 " template <class T>\n"
7770 " int& foo(const std::string& str) const& noexcept {}\n"
7771 "};",
7772 AlignLeftBreakTemplate);
7773
7774 verifyFormat("struct f {\n"
7775 " template <class T>\n"
7776 " int& foo(const std::string& str) const&& noexcept {}\n"
7777 "};",
7778 AlignLeftBreakTemplate);
7779
7780 verifyFormat("struct f {\n"
7781 " template <class T>\n"
7782 " auto foo(const std::string& str) && noexcept -> int& {}\n"
7783 "};",
7784 AlignLeftBreakTemplate);
7785
7786 // The `&` in `Type&` should not be confused with a trailing `&` of
7787 // DEPRECATED(reason) member function.
7788 verifyFormat("struct f {\n"
7789 " template <class T>\n"
7790 " DEPRECATED(reason)\n"
7791 " Type &foo(arguments) {}\n"
7792 "};",
7793 BreakTemplate);
7794
7795 verifyFormat("struct f {\n"
7796 " template <class T>\n"
7797 " DEPRECATED(reason)\n"
7798 " Type& foo(arguments) {}\n"
7799 "};",
7800 AlignLeftBreakTemplate);
7801
7802 verifyFormat("void (*foopt)(int) = &func;");
7803 }
7804
TEST_F(FormatTest,UnderstandsNewAndDelete)7805 TEST_F(FormatTest, UnderstandsNewAndDelete) {
7806 verifyFormat("void f() {\n"
7807 " A *a = new A;\n"
7808 " A *a = new (placement) A;\n"
7809 " delete a;\n"
7810 " delete (A *)a;\n"
7811 "}");
7812 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
7813 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
7814 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7815 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
7816 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
7817 verifyFormat("delete[] h->p;");
7818 }
7819
TEST_F(FormatTest,UnderstandsUsesOfStarAndAmp)7820 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
7821 verifyFormat("int *f(int *a) {}");
7822 verifyFormat("int main(int argc, char **argv) {}");
7823 verifyFormat("Test::Test(int b) : a(b * b) {}");
7824 verifyIndependentOfContext("f(a, *a);");
7825 verifyFormat("void g() { f(*a); }");
7826 verifyIndependentOfContext("int a = b * 10;");
7827 verifyIndependentOfContext("int a = 10 * b;");
7828 verifyIndependentOfContext("int a = b * c;");
7829 verifyIndependentOfContext("int a += b * c;");
7830 verifyIndependentOfContext("int a -= b * c;");
7831 verifyIndependentOfContext("int a *= b * c;");
7832 verifyIndependentOfContext("int a /= b * c;");
7833 verifyIndependentOfContext("int a = *b;");
7834 verifyIndependentOfContext("int a = *b * c;");
7835 verifyIndependentOfContext("int a = b * *c;");
7836 verifyIndependentOfContext("int a = b * (10);");
7837 verifyIndependentOfContext("S << b * (10);");
7838 verifyIndependentOfContext("return 10 * b;");
7839 verifyIndependentOfContext("return *b * *c;");
7840 verifyIndependentOfContext("return a & ~b;");
7841 verifyIndependentOfContext("f(b ? *c : *d);");
7842 verifyIndependentOfContext("int a = b ? *c : *d;");
7843 verifyIndependentOfContext("*b = a;");
7844 verifyIndependentOfContext("a * ~b;");
7845 verifyIndependentOfContext("a * !b;");
7846 verifyIndependentOfContext("a * +b;");
7847 verifyIndependentOfContext("a * -b;");
7848 verifyIndependentOfContext("a * ++b;");
7849 verifyIndependentOfContext("a * --b;");
7850 verifyIndependentOfContext("a[4] * b;");
7851 verifyIndependentOfContext("a[a * a] = 1;");
7852 verifyIndependentOfContext("f() * b;");
7853 verifyIndependentOfContext("a * [self dostuff];");
7854 verifyIndependentOfContext("int x = a * (a + b);");
7855 verifyIndependentOfContext("(a *)(a + b);");
7856 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
7857 verifyIndependentOfContext("int *pa = (int *)&a;");
7858 verifyIndependentOfContext("return sizeof(int **);");
7859 verifyIndependentOfContext("return sizeof(int ******);");
7860 verifyIndependentOfContext("return (int **&)a;");
7861 verifyIndependentOfContext("f((*PointerToArray)[10]);");
7862 verifyFormat("void f(Type (*parameter)[10]) {}");
7863 verifyFormat("void f(Type (¶meter)[10]) {}");
7864 verifyGoogleFormat("return sizeof(int**);");
7865 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
7866 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
7867 verifyFormat("auto a = [](int **&, int ***) {};");
7868 verifyFormat("auto PointerBinding = [](const char *S) {};");
7869 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
7870 verifyFormat("[](const decltype(*a) &value) {}");
7871 verifyFormat("decltype(a * b) F();");
7872 verifyFormat("#define MACRO() [](A *a) { return 1; }");
7873 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
7874 verifyIndependentOfContext("typedef void (*f)(int *a);");
7875 verifyIndependentOfContext("int i{a * b};");
7876 verifyIndependentOfContext("aaa && aaa->f();");
7877 verifyIndependentOfContext("int x = ~*p;");
7878 verifyFormat("Constructor() : a(a), area(width * height) {}");
7879 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
7880 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
7881 verifyFormat("void f() { f(a, c * d); }");
7882 verifyFormat("void f() { f(new a(), c * d); }");
7883 verifyFormat("void f(const MyOverride &override);");
7884 verifyFormat("void f(const MyFinal &final);");
7885 verifyIndependentOfContext("bool a = f() && override.f();");
7886 verifyIndependentOfContext("bool a = f() && final.f();");
7887
7888 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
7889
7890 verifyIndependentOfContext("A<int *> a;");
7891 verifyIndependentOfContext("A<int **> a;");
7892 verifyIndependentOfContext("A<int *, int *> a;");
7893 verifyIndependentOfContext("A<int *[]> a;");
7894 verifyIndependentOfContext(
7895 "const char *const p = reinterpret_cast<const char *const>(q);");
7896 verifyIndependentOfContext("A<int **, int **> a;");
7897 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
7898 verifyFormat("for (char **a = b; *a; ++a) {\n}");
7899 verifyFormat("for (; a && b;) {\n}");
7900 verifyFormat("bool foo = true && [] { return false; }();");
7901
7902 verifyFormat(
7903 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
7905
7906 verifyGoogleFormat("int const* a = &b;");
7907 verifyGoogleFormat("**outparam = 1;");
7908 verifyGoogleFormat("*outparam = a * b;");
7909 verifyGoogleFormat("int main(int argc, char** argv) {}");
7910 verifyGoogleFormat("A<int*> a;");
7911 verifyGoogleFormat("A<int**> a;");
7912 verifyGoogleFormat("A<int*, int*> a;");
7913 verifyGoogleFormat("A<int**, int**> a;");
7914 verifyGoogleFormat("f(b ? *c : *d);");
7915 verifyGoogleFormat("int a = b ? *c : *d;");
7916 verifyGoogleFormat("Type* t = **x;");
7917 verifyGoogleFormat("Type* t = *++*x;");
7918 verifyGoogleFormat("*++*x;");
7919 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
7920 verifyGoogleFormat("Type* t = x++ * y;");
7921 verifyGoogleFormat(
7922 "const char* const p = reinterpret_cast<const char* const>(q);");
7923 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
7924 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
7925 verifyGoogleFormat("template <typename T>\n"
7926 "void f(int i = 0, SomeType** temps = NULL);");
7927
7928 FormatStyle Left = getLLVMStyle();
7929 Left.PointerAlignment = FormatStyle::PAS_Left;
7930 verifyFormat("x = *a(x) = *a(y);", Left);
7931 verifyFormat("for (;; *a = b) {\n}", Left);
7932 verifyFormat("return *this += 1;", Left);
7933 verifyFormat("throw *x;", Left);
7934 verifyFormat("delete *x;", Left);
7935 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
7936 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
7937 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
7938 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
7939 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
7940 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
7941
7942 verifyIndependentOfContext("a = *(x + y);");
7943 verifyIndependentOfContext("a = &(x + y);");
7944 verifyIndependentOfContext("*(x + y).call();");
7945 verifyIndependentOfContext("&(x + y)->call();");
7946 verifyFormat("void f() { &(*I).first; }");
7947
7948 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
7949 verifyFormat(
7950 "int *MyValues = {\n"
7951 " *A, // Operator detection might be confused by the '{'\n"
7952 " *BB // Operator detection might be confused by previous comment\n"
7953 "};");
7954
7955 verifyIndependentOfContext("if (int *a = &b)");
7956 verifyIndependentOfContext("if (int &a = *b)");
7957 verifyIndependentOfContext("if (a & b[i])");
7958 verifyIndependentOfContext("if constexpr (a & b[i])");
7959 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
7960 verifyIndependentOfContext("if (a * (b * c))");
7961 verifyIndependentOfContext("if constexpr (a * (b * c))");
7962 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
7963 verifyIndependentOfContext("if (a::b::c::d & b[i])");
7964 verifyIndependentOfContext("if (*b[i])");
7965 verifyIndependentOfContext("if (int *a = (&b))");
7966 verifyIndependentOfContext("while (int *a = &b)");
7967 verifyIndependentOfContext("while (a * (b * c))");
7968 verifyIndependentOfContext("size = sizeof *a;");
7969 verifyIndependentOfContext("if (a && (b = c))");
7970 verifyFormat("void f() {\n"
7971 " for (const int &v : Values) {\n"
7972 " }\n"
7973 "}");
7974 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
7975 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
7976 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
7977
7978 verifyFormat("#define A (!a * b)");
7979 verifyFormat("#define MACRO \\\n"
7980 " int *i = a * b; \\\n"
7981 " void f(a *b);",
7982 getLLVMStyleWithColumns(19));
7983
7984 verifyIndependentOfContext("A = new SomeType *[Length];");
7985 verifyIndependentOfContext("A = new SomeType *[Length]();");
7986 verifyIndependentOfContext("T **t = new T *;");
7987 verifyIndependentOfContext("T **t = new T *();");
7988 verifyGoogleFormat("A = new SomeType*[Length]();");
7989 verifyGoogleFormat("A = new SomeType*[Length];");
7990 verifyGoogleFormat("T** t = new T*;");
7991 verifyGoogleFormat("T** t = new T*();");
7992
7993 verifyFormat("STATIC_ASSERT((a & b) == 0);");
7994 verifyFormat("STATIC_ASSERT(0 == (a & b));");
7995 verifyFormat("template <bool a, bool b> "
7996 "typename t::if<x && y>::type f() {}");
7997 verifyFormat("template <int *y> f() {}");
7998 verifyFormat("vector<int *> v;");
7999 verifyFormat("vector<int *const> v;");
8000 verifyFormat("vector<int *const **const *> v;");
8001 verifyFormat("vector<int *volatile> v;");
8002 verifyFormat("vector<a * b> v;");
8003 verifyFormat("foo<b && false>();");
8004 verifyFormat("foo<b & 1>();");
8005 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
8006 verifyFormat(
8007 "template <class T, class = typename std::enable_if<\n"
8008 " std::is_integral<T>::value &&\n"
8009 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
8010 "void F();",
8011 getLLVMStyleWithColumns(70));
8012 verifyFormat("template <class T,\n"
8013 " class = typename std::enable_if<\n"
8014 " std::is_integral<T>::value &&\n"
8015 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
8016 " class U>\n"
8017 "void F();",
8018 getLLVMStyleWithColumns(70));
8019 verifyFormat(
8020 "template <class T,\n"
8021 " class = typename ::std::enable_if<\n"
8022 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
8023 "void F();",
8024 getGoogleStyleWithColumns(68));
8025
8026 verifyIndependentOfContext("MACRO(int *i);");
8027 verifyIndependentOfContext("MACRO(auto *a);");
8028 verifyIndependentOfContext("MACRO(const A *a);");
8029 verifyIndependentOfContext("MACRO(A *const a);");
8030 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
8031 verifyFormat("void f() { f(float{1}, a * a); }");
8032 // FIXME: Is there a way to make this work?
8033 // verifyIndependentOfContext("MACRO(A *a);");
8034
8035 verifyFormat("DatumHandle const *operator->() const { return input_; }");
8036 verifyFormat("return options != nullptr && operator==(*options);");
8037
8038 EXPECT_EQ("#define OP(x) \\\n"
8039 " ostream &operator<<(ostream &s, const A &a) { \\\n"
8040 " return s << a.DebugString(); \\\n"
8041 " }",
8042 format("#define OP(x) \\\n"
8043 " ostream &operator<<(ostream &s, const A &a) { \\\n"
8044 " return s << a.DebugString(); \\\n"
8045 " }",
8046 getLLVMStyleWithColumns(50)));
8047
8048 // FIXME: We cannot handle this case yet; we might be able to figure out that
8049 // foo<x> d > v; doesn't make sense.
8050 verifyFormat("foo<a<b && c> d> v;");
8051
8052 FormatStyle PointerMiddle = getLLVMStyle();
8053 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
8054 verifyFormat("delete *x;", PointerMiddle);
8055 verifyFormat("int * x;", PointerMiddle);
8056 verifyFormat("int *[] x;", PointerMiddle);
8057 verifyFormat("template <int * y> f() {}", PointerMiddle);
8058 verifyFormat("int * f(int * a) {}", PointerMiddle);
8059 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
8060 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
8061 verifyFormat("A<int *> a;", PointerMiddle);
8062 verifyFormat("A<int **> a;", PointerMiddle);
8063 verifyFormat("A<int *, int *> a;", PointerMiddle);
8064 verifyFormat("A<int *[]> a;", PointerMiddle);
8065 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
8066 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
8067 verifyFormat("T ** t = new T *;", PointerMiddle);
8068
8069 // Member function reference qualifiers aren't binary operators.
8070 verifyFormat("string // break\n"
8071 "operator()() & {}");
8072 verifyFormat("string // break\n"
8073 "operator()() && {}");
8074 verifyGoogleFormat("template <typename T>\n"
8075 "auto x() & -> int {}");
8076 }
8077
TEST_F(FormatTest,UnderstandsAttributes)8078 TEST_F(FormatTest, UnderstandsAttributes) {
8079 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
8080 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
8081 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
8082 FormatStyle AfterType = getLLVMStyle();
8083 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
8084 verifyFormat("__attribute__((nodebug)) void\n"
8085 "foo() {}\n",
8086 AfterType);
8087 }
8088
TEST_F(FormatTest,UnderstandsSquareAttributes)8089 TEST_F(FormatTest, UnderstandsSquareAttributes) {
8090 verifyFormat("SomeType s [[unused]] (InitValue);");
8091 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
8092 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
8093 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
8094 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
8095 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8096 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
8097 verifyFormat("[[nodiscard]] bool f() { return false; }");
8098 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
8099 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
8100 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
8101
8102 // Make sure we do not mistake attributes for array subscripts.
8103 verifyFormat("int a() {}\n"
8104 "[[unused]] int b() {}\n");
8105 verifyFormat("NSArray *arr;\n"
8106 "arr[[Foo() bar]];");
8107
8108 // On the other hand, we still need to correctly find array subscripts.
8109 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
8110
8111 // Make sure that we do not mistake Objective-C method inside array literals
8112 // as attributes, even if those method names are also keywords.
8113 verifyFormat("@[ [foo bar] ];");
8114 verifyFormat("@[ [NSArray class] ];");
8115 verifyFormat("@[ [foo enum] ];");
8116
8117 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
8118
8119 // Make sure we do not parse attributes as lambda introducers.
8120 FormatStyle MultiLineFunctions = getLLVMStyle();
8121 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8122 verifyFormat("[[unused]] int b() {\n"
8123 " return 42;\n"
8124 "}\n",
8125 MultiLineFunctions);
8126 }
8127
TEST_F(FormatTest,AttributeClass)8128 TEST_F(FormatTest, AttributeClass) {
8129 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
8130 verifyFormat("class S {\n"
8131 " S(S&&) = default;\n"
8132 "};",
8133 Style);
8134 verifyFormat("class [[nodiscard]] S {\n"
8135 " S(S&&) = default;\n"
8136 "};",
8137 Style);
8138 verifyFormat("class __attribute((maybeunused)) S {\n"
8139 " S(S&&) = default;\n"
8140 "};",
8141 Style);
8142 verifyFormat("struct S {\n"
8143 " S(S&&) = default;\n"
8144 "};",
8145 Style);
8146 verifyFormat("struct [[nodiscard]] S {\n"
8147 " S(S&&) = default;\n"
8148 "};",
8149 Style);
8150 }
8151
TEST_F(FormatTest,AttributesAfterMacro)8152 TEST_F(FormatTest, AttributesAfterMacro) {
8153 FormatStyle Style = getLLVMStyle();
8154 verifyFormat("MACRO;\n"
8155 "__attribute__((maybe_unused)) int foo() {\n"
8156 " //...\n"
8157 "}");
8158
8159 verifyFormat("MACRO;\n"
8160 "[[nodiscard]] int foo() {\n"
8161 " //...\n"
8162 "}");
8163
8164 EXPECT_EQ("MACRO\n\n"
8165 "__attribute__((maybe_unused)) int foo() {\n"
8166 " //...\n"
8167 "}",
8168 format("MACRO\n\n"
8169 "__attribute__((maybe_unused)) int foo() {\n"
8170 " //...\n"
8171 "}"));
8172
8173 EXPECT_EQ("MACRO\n\n"
8174 "[[nodiscard]] int foo() {\n"
8175 " //...\n"
8176 "}",
8177 format("MACRO\n\n"
8178 "[[nodiscard]] int foo() {\n"
8179 " //...\n"
8180 "}"));
8181 }
8182
TEST_F(FormatTest,AttributePenaltyBreaking)8183 TEST_F(FormatTest, AttributePenaltyBreaking) {
8184 FormatStyle Style = getLLVMStyle();
8185 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
8186 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
8187 Style);
8188 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
8189 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
8190 Style);
8191 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
8192 "shared_ptr<ALongTypeName> &C d) {\n}",
8193 Style);
8194 }
8195
TEST_F(FormatTest,UnderstandsEllipsis)8196 TEST_F(FormatTest, UnderstandsEllipsis) {
8197 FormatStyle Style = getLLVMStyle();
8198 verifyFormat("int printf(const char *fmt, ...);");
8199 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
8200 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
8201
8202 verifyFormat("template <int *...PP> a;", Style);
8203
8204 Style.PointerAlignment = FormatStyle::PAS_Left;
8205 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
8206
8207 verifyFormat("template <int*... PP> a;", Style);
8208
8209 Style.PointerAlignment = FormatStyle::PAS_Middle;
8210 verifyFormat("template <int *... PP> a;", Style);
8211 }
8212
TEST_F(FormatTest,AdaptivelyFormatsPointersAndReferences)8213 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
8214 EXPECT_EQ("int *a;\n"
8215 "int *a;\n"
8216 "int *a;",
8217 format("int *a;\n"
8218 "int* a;\n"
8219 "int *a;",
8220 getGoogleStyle()));
8221 EXPECT_EQ("int* a;\n"
8222 "int* a;\n"
8223 "int* a;",
8224 format("int* a;\n"
8225 "int* a;\n"
8226 "int *a;",
8227 getGoogleStyle()));
8228 EXPECT_EQ("int *a;\n"
8229 "int *a;\n"
8230 "int *a;",
8231 format("int *a;\n"
8232 "int * a;\n"
8233 "int * a;",
8234 getGoogleStyle()));
8235 EXPECT_EQ("auto x = [] {\n"
8236 " int *a;\n"
8237 " int *a;\n"
8238 " int *a;\n"
8239 "};",
8240 format("auto x=[]{int *a;\n"
8241 "int * a;\n"
8242 "int * a;};",
8243 getGoogleStyle()));
8244 }
8245
TEST_F(FormatTest,UnderstandsRvalueReferences)8246 TEST_F(FormatTest, UnderstandsRvalueReferences) {
8247 verifyFormat("int f(int &&a) {}");
8248 verifyFormat("int f(int a, char &&b) {}");
8249 verifyFormat("void f() { int &&a = b; }");
8250 verifyGoogleFormat("int f(int a, char&& b) {}");
8251 verifyGoogleFormat("void f() { int&& a = b; }");
8252
8253 verifyIndependentOfContext("A<int &&> a;");
8254 verifyIndependentOfContext("A<int &&, int &&> a;");
8255 verifyGoogleFormat("A<int&&> a;");
8256 verifyGoogleFormat("A<int&&, int&&> a;");
8257
8258 // Not rvalue references:
8259 verifyFormat("template <bool B, bool C> class A {\n"
8260 " static_assert(B && C, \"Something is wrong\");\n"
8261 "};");
8262 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
8263 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
8264 verifyFormat("#define A(a, b) (a && b)");
8265 }
8266
TEST_F(FormatTest,FormatsBinaryOperatorsPrecedingEquals)8267 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
8268 verifyFormat("void f() {\n"
8269 " x[aaaaaaaaa -\n"
8270 " b] = 23;\n"
8271 "}",
8272 getLLVMStyleWithColumns(15));
8273 }
8274
TEST_F(FormatTest,FormatsCasts)8275 TEST_F(FormatTest, FormatsCasts) {
8276 verifyFormat("Type *A = static_cast<Type *>(P);");
8277 verifyFormat("Type *A = (Type *)P;");
8278 verifyFormat("Type *A = (vector<Type *, int *>)P;");
8279 verifyFormat("int a = (int)(2.0f);");
8280 verifyFormat("int a = (int)2.0f;");
8281 verifyFormat("x[(int32)y];");
8282 verifyFormat("x = (int32)y;");
8283 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
8284 verifyFormat("int a = (int)*b;");
8285 verifyFormat("int a = (int)2.0f;");
8286 verifyFormat("int a = (int)~0;");
8287 verifyFormat("int a = (int)++a;");
8288 verifyFormat("int a = (int)sizeof(int);");
8289 verifyFormat("int a = (int)+2;");
8290 verifyFormat("my_int a = (my_int)2.0f;");
8291 verifyFormat("my_int a = (my_int)sizeof(int);");
8292 verifyFormat("return (my_int)aaa;");
8293 verifyFormat("#define x ((int)-1)");
8294 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
8295 verifyFormat("#define p(q) ((int *)&q)");
8296 verifyFormat("fn(a)(b) + 1;");
8297
8298 verifyFormat("void f() { my_int a = (my_int)*b; }");
8299 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
8300 verifyFormat("my_int a = (my_int)~0;");
8301 verifyFormat("my_int a = (my_int)++a;");
8302 verifyFormat("my_int a = (my_int)-2;");
8303 verifyFormat("my_int a = (my_int)1;");
8304 verifyFormat("my_int a = (my_int *)1;");
8305 verifyFormat("my_int a = (const my_int)-1;");
8306 verifyFormat("my_int a = (const my_int *)-1;");
8307 verifyFormat("my_int a = (my_int)(my_int)-1;");
8308 verifyFormat("my_int a = (ns::my_int)-2;");
8309 verifyFormat("case (my_int)ONE:");
8310 verifyFormat("auto x = (X)this;");
8311 // Casts in Obj-C style calls used to not be recognized as such.
8312 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle());
8313
8314 // FIXME: single value wrapped with paren will be treated as cast.
8315 verifyFormat("void f(int i = (kValue)*kMask) {}");
8316
8317 verifyFormat("{ (void)F; }");
8318
8319 // Don't break after a cast's
8320 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8321 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
8322 " bbbbbbbbbbbbbbbbbbbbbb);");
8323
8324 // These are not casts.
8325 verifyFormat("void f(int *) {}");
8326 verifyFormat("f(foo)->b;");
8327 verifyFormat("f(foo).b;");
8328 verifyFormat("f(foo)(b);");
8329 verifyFormat("f(foo)[b];");
8330 verifyFormat("[](foo) { return 4; }(bar);");
8331 verifyFormat("(*funptr)(foo)[4];");
8332 verifyFormat("funptrs[4](foo)[4];");
8333 verifyFormat("void f(int *);");
8334 verifyFormat("void f(int *) = 0;");
8335 verifyFormat("void f(SmallVector<int>) {}");
8336 verifyFormat("void f(SmallVector<int>);");
8337 verifyFormat("void f(SmallVector<int>) = 0;");
8338 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
8339 verifyFormat("int a = sizeof(int) * b;");
8340 verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
8341 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
8342 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
8343 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
8344
8345 // These are not casts, but at some point were confused with casts.
8346 verifyFormat("virtual void foo(int *) override;");
8347 verifyFormat("virtual void foo(char &) const;");
8348 verifyFormat("virtual void foo(int *a, char *) const;");
8349 verifyFormat("int a = sizeof(int *) + b;");
8350 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
8351 verifyFormat("bool b = f(g<int>) && c;");
8352 verifyFormat("typedef void (*f)(int i) func;");
8353 verifyFormat("void operator++(int) noexcept;");
8354 verifyFormat("void operator++(int &) noexcept;");
8355 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
8356 "&) noexcept;");
8357 verifyFormat(
8358 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
8359 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
8360 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
8361 verifyFormat("void operator delete(nothrow_t &) noexcept;");
8362 verifyFormat("void operator delete(foo &) noexcept;");
8363 verifyFormat("void operator delete(foo) noexcept;");
8364 verifyFormat("void operator delete(int) noexcept;");
8365 verifyFormat("void operator delete(int &) noexcept;");
8366 verifyFormat("void operator delete(int &) volatile noexcept;");
8367 verifyFormat("void operator delete(int &) const");
8368 verifyFormat("void operator delete(int &) = default");
8369 verifyFormat("void operator delete(int &) = delete");
8370 verifyFormat("void operator delete(int &) [[noreturn]]");
8371 verifyFormat("void operator delete(int &) throw();");
8372 verifyFormat("void operator delete(int &) throw(int);");
8373 verifyFormat("auto operator delete(int &) -> int;");
8374 verifyFormat("auto operator delete(int &) override");
8375 verifyFormat("auto operator delete(int &) final");
8376
8377 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
8378 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
8379 // FIXME: The indentation here is not ideal.
8380 verifyFormat(
8381 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8382 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
8383 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
8384 }
8385
TEST_F(FormatTest,FormatsFunctionTypes)8386 TEST_F(FormatTest, FormatsFunctionTypes) {
8387 verifyFormat("A<bool()> a;");
8388 verifyFormat("A<SomeType()> a;");
8389 verifyFormat("A<void (*)(int, std::string)> a;");
8390 verifyFormat("A<void *(int)>;");
8391 verifyFormat("void *(*a)(int *, SomeType *);");
8392 verifyFormat("int (*func)(void *);");
8393 verifyFormat("void f() { int (*func)(void *); }");
8394 verifyFormat("template <class CallbackClass>\n"
8395 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
8396
8397 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
8398 verifyGoogleFormat("void* (*a)(int);");
8399 verifyGoogleFormat(
8400 "template <class CallbackClass>\n"
8401 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
8402
8403 // Other constructs can look somewhat like function types:
8404 verifyFormat("A<sizeof(*x)> a;");
8405 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
8406 verifyFormat("some_var = function(*some_pointer_var)[0];");
8407 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
8408 verifyFormat("int x = f(&h)();");
8409 verifyFormat("returnsFunction(¶m1, ¶m2)(param);");
8410 verifyFormat("std::function<\n"
8411 " LooooooooooongTemplatedType<\n"
8412 " SomeType>*(\n"
8413 " LooooooooooooooooongType type)>\n"
8414 " function;",
8415 getGoogleStyleWithColumns(40));
8416 }
8417
TEST_F(FormatTest,FormatsPointersToArrayTypes)8418 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
8419 verifyFormat("A (*foo_)[6];");
8420 verifyFormat("vector<int> (*foo_)[6];");
8421 }
8422
TEST_F(FormatTest,BreaksLongVariableDeclarations)8423 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
8424 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
8425 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
8426 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
8427 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
8428 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
8429 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
8430
8431 // Different ways of ()-initializiation.
8432 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
8433 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
8434 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
8435 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
8436 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
8437 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
8438 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
8439 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
8440
8441 // Lambdas should not confuse the variable declaration heuristic.
8442 verifyFormat("LooooooooooooooooongType\n"
8443 " variable(nullptr, [](A *a) {});",
8444 getLLVMStyleWithColumns(40));
8445 }
8446
TEST_F(FormatTest,BreaksLongDeclarations)8447 TEST_F(FormatTest, BreaksLongDeclarations) {
8448 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
8449 " AnotherNameForTheLongType;");
8450 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
8451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8452 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
8453 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
8454 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
8455 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
8456 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
8457 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
8458 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
8459 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
8460 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
8461 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
8462 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
8463 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
8464 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
8465 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
8466 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
8467 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
8468 FormatStyle Indented = getLLVMStyle();
8469 Indented.IndentWrappedFunctionNames = true;
8470 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
8471 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
8472 Indented);
8473 verifyFormat(
8474 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
8475 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
8476 Indented);
8477 verifyFormat(
8478 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
8479 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
8480 Indented);
8481 verifyFormat(
8482 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
8483 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
8484 Indented);
8485
8486 // FIXME: Without the comment, this breaks after "(".
8487 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
8488 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
8489 getGoogleStyle());
8490
8491 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
8492 " int LoooooooooooooooooooongParam2) {}");
8493 verifyFormat(
8494 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
8495 " SourceLocation L, IdentifierIn *II,\n"
8496 " Type *T) {}");
8497 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
8498 "ReallyReaaallyLongFunctionName(\n"
8499 " const std::string &SomeParameter,\n"
8500 " const SomeType<string, SomeOtherTemplateParameter>\n"
8501 " &ReallyReallyLongParameterName,\n"
8502 " const SomeType<string, SomeOtherTemplateParameter>\n"
8503 " &AnotherLongParameterName) {}");
8504 verifyFormat("template <typename A>\n"
8505 "SomeLoooooooooooooooooooooongType<\n"
8506 " typename some_namespace::SomeOtherType<A>::Type>\n"
8507 "Function() {}");
8508
8509 verifyGoogleFormat(
8510 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
8511 " aaaaaaaaaaaaaaaaaaaaaaa;");
8512 verifyGoogleFormat(
8513 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
8514 " SourceLocation L) {}");
8515 verifyGoogleFormat(
8516 "some_namespace::LongReturnType\n"
8517 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
8518 " int first_long_parameter, int second_parameter) {}");
8519
8520 verifyGoogleFormat("template <typename T>\n"
8521 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8522 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
8523 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8524 " int aaaaaaaaaaaaaaaaaaaaaaa);");
8525
8526 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
8527 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8528 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8529 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8530 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8531 " aaaaaaaaaaaaaaaaaaaaaaaa);");
8532 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8533 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
8534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
8535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8536
8537 verifyFormat("template <typename T> // Templates on own line.\n"
8538 "static int // Some comment.\n"
8539 "MyFunction(int a);",
8540 getLLVMStyle());
8541 }
8542
TEST_F(FormatTest,FormatsArrays)8543 TEST_F(FormatTest, FormatsArrays) {
8544 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
8545 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
8546 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
8547 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
8548 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8549 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
8550 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8551 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
8552 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8553 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
8554 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8555 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
8556 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
8557 verifyFormat(
8558 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
8559 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
8560 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
8561 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
8562 " .aaaaaaaaaaaaaaaaaaaaaa();");
8563
8564 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
8565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
8566 verifyFormat(
8567 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
8568 " .aaaaaaa[0]\n"
8569 " .aaaaaaaaaaaaaaaaaaaaaa();");
8570 verifyFormat("a[::b::c];");
8571
8572 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
8573
8574 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
8575 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
8576 }
8577
TEST_F(FormatTest,LineStartsWithSpecialCharacter)8578 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
8579 verifyFormat("(a)->b();");
8580 verifyFormat("--a;");
8581 }
8582
TEST_F(FormatTest,HandlesIncludeDirectives)8583 TEST_F(FormatTest, HandlesIncludeDirectives) {
8584 verifyFormat("#include <string>\n"
8585 "#include <a/b/c.h>\n"
8586 "#include \"a/b/string\"\n"
8587 "#include \"string.h\"\n"
8588 "#include \"string.h\"\n"
8589 "#include <a-a>\n"
8590 "#include < path with space >\n"
8591 "#include_next <test.h>"
8592 "#include \"abc.h\" // this is included for ABC\n"
8593 "#include \"some long include\" // with a comment\n"
8594 "#include \"some very long include path\"\n"
8595 "#include <some/very/long/include/path>\n",
8596 getLLVMStyleWithColumns(35));
8597 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
8598 EXPECT_EQ("#include <a>", format("#include<a>"));
8599
8600 verifyFormat("#import <string>");
8601 verifyFormat("#import <a/b/c.h>");
8602 verifyFormat("#import \"a/b/string\"");
8603 verifyFormat("#import \"string.h\"");
8604 verifyFormat("#import \"string.h\"");
8605 verifyFormat("#if __has_include(<strstream>)\n"
8606 "#include <strstream>\n"
8607 "#endif");
8608
8609 verifyFormat("#define MY_IMPORT <a/b>");
8610
8611 verifyFormat("#if __has_include(<a/b>)");
8612 verifyFormat("#if __has_include_next(<a/b>)");
8613 verifyFormat("#define F __has_include(<a/b>)");
8614 verifyFormat("#define F __has_include_next(<a/b>)");
8615
8616 // Protocol buffer definition or missing "#".
8617 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
8618 getLLVMStyleWithColumns(30));
8619
8620 FormatStyle Style = getLLVMStyle();
8621 Style.AlwaysBreakBeforeMultilineStrings = true;
8622 Style.ColumnLimit = 0;
8623 verifyFormat("#import \"abc.h\"", Style);
8624
8625 // But 'import' might also be a regular C++ namespace.
8626 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8628 }
8629
8630 //===----------------------------------------------------------------------===//
8631 // Error recovery tests.
8632 //===----------------------------------------------------------------------===//
8633
TEST_F(FormatTest,IncompleteParameterLists)8634 TEST_F(FormatTest, IncompleteParameterLists) {
8635 FormatStyle NoBinPacking = getLLVMStyle();
8636 NoBinPacking.BinPackParameters = false;
8637 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
8638 " double *min_x,\n"
8639 " double *max_x,\n"
8640 " double *min_y,\n"
8641 " double *max_y,\n"
8642 " double *min_z,\n"
8643 " double *max_z, ) {}",
8644 NoBinPacking);
8645 }
8646
TEST_F(FormatTest,IncorrectCodeTrailingStuff)8647 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
8648 verifyFormat("void f() { return; }\n42");
8649 verifyFormat("void f() {\n"
8650 " if (0)\n"
8651 " return;\n"
8652 "}\n"
8653 "42");
8654 verifyFormat("void f() { return }\n42");
8655 verifyFormat("void f() {\n"
8656 " if (0)\n"
8657 " return\n"
8658 "}\n"
8659 "42");
8660 }
8661
TEST_F(FormatTest,IncorrectCodeMissingSemicolon)8662 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
8663 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
8664 EXPECT_EQ("void f() {\n"
8665 " if (a)\n"
8666 " return\n"
8667 "}",
8668 format("void f ( ) { if ( a ) return }"));
8669 EXPECT_EQ("namespace N {\n"
8670 "void f()\n"
8671 "}",
8672 format("namespace N { void f() }"));
8673 EXPECT_EQ("namespace N {\n"
8674 "void f() {}\n"
8675 "void g()\n"
8676 "} // namespace N",
8677 format("namespace N { void f( ) { } void g( ) }"));
8678 }
8679
TEST_F(FormatTest,IndentationWithinColumnLimitNotPossible)8680 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
8681 verifyFormat("int aaaaaaaa =\n"
8682 " // Overlylongcomment\n"
8683 " b;",
8684 getLLVMStyleWithColumns(20));
8685 verifyFormat("function(\n"
8686 " ShortArgument,\n"
8687 " LoooooooooooongArgument);\n",
8688 getLLVMStyleWithColumns(20));
8689 }
8690
TEST_F(FormatTest,IncorrectAccessSpecifier)8691 TEST_F(FormatTest, IncorrectAccessSpecifier) {
8692 verifyFormat("public:");
8693 verifyFormat("class A {\n"
8694 "public\n"
8695 " void f() {}\n"
8696 "};");
8697 verifyFormat("public\n"
8698 "int qwerty;");
8699 verifyFormat("public\n"
8700 "B {}");
8701 verifyFormat("public\n"
8702 "{}");
8703 verifyFormat("public\n"
8704 "B { int x; }");
8705 }
8706
TEST_F(FormatTest,IncorrectCodeUnbalancedBraces)8707 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
8708 verifyFormat("{");
8709 verifyFormat("#})");
8710 verifyNoCrash("(/**/[:!] ?[).");
8711 }
8712
TEST_F(FormatTest,IncorrectUnbalancedBracesInMacrosWithUnicode)8713 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
8714 // Found by oss-fuzz:
8715 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
8716 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
8717 Style.ColumnLimit = 60;
8718 verifyNoCrash(
8719 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
8720 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
8721 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
8722 Style);
8723 }
8724
TEST_F(FormatTest,IncorrectCodeDoNoWhile)8725 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
8726 verifyFormat("do {\n}");
8727 verifyFormat("do {\n}\n"
8728 "f();");
8729 verifyFormat("do {\n}\n"
8730 "wheeee(fun);");
8731 verifyFormat("do {\n"
8732 " f();\n"
8733 "}");
8734 }
8735
TEST_F(FormatTest,IncorrectCodeMissingParens)8736 TEST_F(FormatTest, IncorrectCodeMissingParens) {
8737 verifyFormat("if {\n foo;\n foo();\n}");
8738 verifyFormat("switch {\n foo;\n foo();\n}");
8739 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
8740 verifyFormat("while {\n foo;\n foo();\n}");
8741 verifyFormat("do {\n foo;\n foo();\n} while;");
8742 }
8743
TEST_F(FormatTest,DoesNotTouchUnwrappedLinesWithErrors)8744 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
8745 verifyIncompleteFormat("namespace {\n"
8746 "class Foo { Foo (\n"
8747 "};\n"
8748 "} // namespace");
8749 }
8750
TEST_F(FormatTest,IncorrectCodeErrorDetection)8751 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
8752 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
8753 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
8754 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
8755 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
8756
8757 EXPECT_EQ("{\n"
8758 " {\n"
8759 " breakme(\n"
8760 " qwe);\n"
8761 " }\n",
8762 format("{\n"
8763 " {\n"
8764 " breakme(qwe);\n"
8765 "}\n",
8766 getLLVMStyleWithColumns(10)));
8767 }
8768
TEST_F(FormatTest,LayoutCallsInsideBraceInitializers)8769 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
8770 verifyFormat("int x = {\n"
8771 " avariable,\n"
8772 " b(alongervariable)};",
8773 getLLVMStyleWithColumns(25));
8774 }
8775
TEST_F(FormatTest,LayoutBraceInitializersInReturnStatement)8776 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
8777 verifyFormat("return (a)(b){1, 2, 3};");
8778 }
8779
TEST_F(FormatTest,LayoutCxx11BraceInitializers)8780 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
8781 verifyFormat("vector<int> x{1, 2, 3, 4};");
8782 verifyFormat("vector<int> x{\n"
8783 " 1,\n"
8784 " 2,\n"
8785 " 3,\n"
8786 " 4,\n"
8787 "};");
8788 verifyFormat("vector<T> x{{}, {}, {}, {}};");
8789 verifyFormat("f({1, 2});");
8790 verifyFormat("auto v = Foo{-1};");
8791 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
8792 verifyFormat("Class::Class : member{1, 2, 3} {}");
8793 verifyFormat("new vector<int>{1, 2, 3};");
8794 verifyFormat("new int[3]{1, 2, 3};");
8795 verifyFormat("new int{1};");
8796 verifyFormat("return {arg1, arg2};");
8797 verifyFormat("return {arg1, SomeType{parameter}};");
8798 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
8799 verifyFormat("new T{arg1, arg2};");
8800 verifyFormat("f(MyMap[{composite, key}]);");
8801 verifyFormat("class Class {\n"
8802 " T member = {arg1, arg2};\n"
8803 "};");
8804 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
8805 verifyFormat("const struct A a = {.a = 1, .b = 2};");
8806 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
8807 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
8808 verifyFormat("int a = std::is_integral<int>{} + 0;");
8809
8810 verifyFormat("int foo(int i) { return fo1{}(i); }");
8811 verifyFormat("int foo(int i) { return fo1{}(i); }");
8812 verifyFormat("auto i = decltype(x){};");
8813 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
8814 verifyFormat("Node n{1, Node{1000}, //\n"
8815 " 2};");
8816 verifyFormat("Aaaa aaaaaaa{\n"
8817 " {\n"
8818 " aaaa,\n"
8819 " },\n"
8820 "};");
8821 verifyFormat("class C : public D {\n"
8822 " SomeClass SC{2};\n"
8823 "};");
8824 verifyFormat("class C : public A {\n"
8825 " class D : public B {\n"
8826 " void f() { int i{2}; }\n"
8827 " };\n"
8828 "};");
8829 verifyFormat("#define A {a, a},");
8830
8831 // Avoid breaking between equal sign and opening brace
8832 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
8833 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
8834 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
8835 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
8836 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
8837 " {\"ccccccccccccccccccccc\", 2}};",
8838 AvoidBreakingFirstArgument);
8839
8840 // Binpacking only if there is no trailing comma
8841 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
8842 " cccccccccc, dddddddddd};",
8843 getLLVMStyleWithColumns(50));
8844 verifyFormat("const Aaaaaa aaaaa = {\n"
8845 " aaaaaaaaaaa,\n"
8846 " bbbbbbbbbbb,\n"
8847 " ccccccccccc,\n"
8848 " ddddddddddd,\n"
8849 "};",
8850 getLLVMStyleWithColumns(50));
8851
8852 // Cases where distinguising braced lists and blocks is hard.
8853 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
8854 verifyFormat("void f() {\n"
8855 " return; // comment\n"
8856 "}\n"
8857 "SomeType t;");
8858 verifyFormat("void f() {\n"
8859 " if (a) {\n"
8860 " f();\n"
8861 " }\n"
8862 "}\n"
8863 "SomeType t;");
8864
8865 // In combination with BinPackArguments = false.
8866 FormatStyle NoBinPacking = getLLVMStyle();
8867 NoBinPacking.BinPackArguments = false;
8868 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
8869 " bbbbb,\n"
8870 " ccccc,\n"
8871 " ddddd,\n"
8872 " eeeee,\n"
8873 " ffffff,\n"
8874 " ggggg,\n"
8875 " hhhhhh,\n"
8876 " iiiiii,\n"
8877 " jjjjjj,\n"
8878 " kkkkkk};",
8879 NoBinPacking);
8880 verifyFormat("const Aaaaaa aaaaa = {\n"
8881 " aaaaa,\n"
8882 " bbbbb,\n"
8883 " ccccc,\n"
8884 " ddddd,\n"
8885 " eeeee,\n"
8886 " ffffff,\n"
8887 " ggggg,\n"
8888 " hhhhhh,\n"
8889 " iiiiii,\n"
8890 " jjjjjj,\n"
8891 " kkkkkk,\n"
8892 "};",
8893 NoBinPacking);
8894 verifyFormat(
8895 "const Aaaaaa aaaaa = {\n"
8896 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
8897 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
8898 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
8899 "};",
8900 NoBinPacking);
8901
8902 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8903 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n"
8904 " CDDDP83848_BMCR_REGISTER,\n"
8905 " CDDDP83848_BMSR_REGISTER,\n"
8906 " CDDDP83848_RBR_REGISTER};",
8907 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
8908 " CDDDP83848_BMSR_REGISTER,\n"
8909 " CDDDP83848_RBR_REGISTER};",
8910 NoBinPacking));
8911
8912 // FIXME: The alignment of these trailing comments might be bad. Then again,
8913 // this might be utterly useless in real code.
8914 verifyFormat("Constructor::Constructor()\n"
8915 " : some_value{ //\n"
8916 " aaaaaaa, //\n"
8917 " bbbbbbb} {}");
8918
8919 // In braced lists, the first comment is always assumed to belong to the
8920 // first element. Thus, it can be moved to the next or previous line as
8921 // appropriate.
8922 EXPECT_EQ("function({// First element:\n"
8923 " 1,\n"
8924 " // Second element:\n"
8925 " 2});",
8926 format("function({\n"
8927 " // First element:\n"
8928 " 1,\n"
8929 " // Second element:\n"
8930 " 2});"));
8931 EXPECT_EQ("std::vector<int> MyNumbers{\n"
8932 " // First element:\n"
8933 " 1,\n"
8934 " // Second element:\n"
8935 " 2};",
8936 format("std::vector<int> MyNumbers{// First element:\n"
8937 " 1,\n"
8938 " // Second element:\n"
8939 " 2};",
8940 getLLVMStyleWithColumns(30)));
8941 // A trailing comma should still lead to an enforced line break and no
8942 // binpacking.
8943 EXPECT_EQ("vector<int> SomeVector = {\n"
8944 " // aaa\n"
8945 " 1,\n"
8946 " 2,\n"
8947 "};",
8948 format("vector<int> SomeVector = { // aaa\n"
8949 " 1, 2, };"));
8950
8951 // C++11 brace initializer list l-braces should not be treated any differently
8952 // when breaking before lambda bodies is enabled
8953 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
8954 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
8955 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
8956 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
8957 verifyFormat(
8958 "std::runtime_error{\n"
8959 " \"Long string which will force a break onto the next line...\"};",
8960 BreakBeforeLambdaBody);
8961
8962 FormatStyle ExtraSpaces = getLLVMStyle();
8963 ExtraSpaces.Cpp11BracedListStyle = false;
8964 ExtraSpaces.ColumnLimit = 75;
8965 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
8966 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
8967 verifyFormat("f({ 1, 2 });", ExtraSpaces);
8968 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
8969 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
8970 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
8971 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
8972 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
8973 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
8974 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
8975 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
8976 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
8977 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
8978 verifyFormat("class Class {\n"
8979 " T member = { arg1, arg2 };\n"
8980 "};",
8981 ExtraSpaces);
8982 verifyFormat(
8983 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8984 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
8985 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8986 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
8987 ExtraSpaces);
8988 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
8989 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
8990 ExtraSpaces);
8991 verifyFormat(
8992 "someFunction(OtherParam,\n"
8993 " BracedList{ // comment 1 (Forcing interesting break)\n"
8994 " param1, param2,\n"
8995 " // comment 2\n"
8996 " param3, param4 });",
8997 ExtraSpaces);
8998 verifyFormat(
8999 "std::this_thread::sleep_for(\n"
9000 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
9001 ExtraSpaces);
9002 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
9003 " aaaaaaa,\n"
9004 " aaaaaaaaaa,\n"
9005 " aaaaa,\n"
9006 " aaaaaaaaaaaaaaa,\n"
9007 " aaa,\n"
9008 " aaaaaaaaaa,\n"
9009 " a,\n"
9010 " aaaaaaaaaaaaaaaaaaaaa,\n"
9011 " aaaaaaaaaaaa,\n"
9012 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
9013 " aaaaaaa,\n"
9014 " a};");
9015 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
9016 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
9017 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
9018
9019 // Avoid breaking between initializer/equal sign and opening brace
9020 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
9021 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
9022 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
9023 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
9024 " { \"ccccccccccccccccccccc\", 2 }\n"
9025 "};",
9026 ExtraSpaces);
9027 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
9028 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
9029 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
9030 " { \"ccccccccccccccccccccc\", 2 }\n"
9031 "};",
9032 ExtraSpaces);
9033
9034 FormatStyle SpaceBeforeBrace = getLLVMStyle();
9035 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
9036 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
9037 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
9038
9039 FormatStyle SpaceBetweenBraces = getLLVMStyle();
9040 SpaceBetweenBraces.SpacesInAngles = true;
9041 SpaceBetweenBraces.SpacesInParentheses = true;
9042 SpaceBetweenBraces.SpacesInSquareBrackets = true;
9043 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
9044 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
9045 verifyFormat("vector< int > x{ // comment 1\n"
9046 " 1, 2, 3, 4 };",
9047 SpaceBetweenBraces);
9048 SpaceBetweenBraces.ColumnLimit = 20;
9049 EXPECT_EQ("vector< int > x{\n"
9050 " 1, 2, 3, 4 };",
9051 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
9052 SpaceBetweenBraces.ColumnLimit = 24;
9053 EXPECT_EQ("vector< int > x{ 1, 2,\n"
9054 " 3, 4 };",
9055 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces));
9056 EXPECT_EQ("vector< int > x{\n"
9057 " 1,\n"
9058 " 2,\n"
9059 " 3,\n"
9060 " 4,\n"
9061 "};",
9062 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
9063 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
9064 SpaceBetweenBraces.SpaceInEmptyParentheses = true;
9065 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
9066 }
9067
TEST_F(FormatTest,FormatSpacesInAngles)9068 TEST_F(FormatTest, FormatSpacesInAngles) {
9069 FormatStyle SpaceInAngles = getLLVMStyle();
9070 SpaceInAngles.SpacesInAngles = true;
9071 verifyFormat("vector< ::std::string > x1;", SpaceInAngles);
9072 verifyFormat("Foo< int, Bar > x2;", SpaceInAngles);
9073 verifyFormat("Foo< ::int, ::Bar > x3;", SpaceInAngles);
9074
9075 SpaceInAngles.SpacesInAngles = false;
9076 verifyFormat("vector<::std::string> x4;", SpaceInAngles);
9077 verifyFormat("vector<int> x5;", SpaceInAngles);
9078 verifyFormat("Foo<int, Bar> x6;", SpaceInAngles);
9079 verifyFormat("Foo<::int, ::Bar> x7;", SpaceInAngles);
9080 }
9081
TEST_F(FormatTest,FormatsBracedListsInColumnLayout)9082 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
9083 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9084 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9085 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9086 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9087 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9088 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
9089 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
9090 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9091 " 1, 22, 333, 4444, 55555, //\n"
9092 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9093 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
9094 verifyFormat(
9095 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9096 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9097 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
9098 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
9099 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
9100 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
9101 " 7777777};");
9102 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
9103 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
9104 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
9105 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
9106 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
9107 " // Separating comment.\n"
9108 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
9109 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
9110 " // Leading comment\n"
9111 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
9112 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
9113 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
9114 " 1, 1, 1, 1};",
9115 getLLVMStyleWithColumns(39));
9116 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
9117 " 1, 1, 1, 1};",
9118 getLLVMStyleWithColumns(38));
9119 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
9120 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
9121 getLLVMStyleWithColumns(43));
9122 verifyFormat(
9123 "static unsigned SomeValues[10][3] = {\n"
9124 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
9125 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
9126 verifyFormat("static auto fields = new vector<string>{\n"
9127 " \"aaaaaaaaaaaaa\",\n"
9128 " \"aaaaaaaaaaaaa\",\n"
9129 " \"aaaaaaaaaaaa\",\n"
9130 " \"aaaaaaaaaaaaaa\",\n"
9131 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
9132 " \"aaaaaaaaaaaa\",\n"
9133 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
9134 "};");
9135 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
9136 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
9137 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
9138 " 3, cccccccccccccccccccccc};",
9139 getLLVMStyleWithColumns(60));
9140
9141 // Trailing commas.
9142 verifyFormat("vector<int> x = {\n"
9143 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
9144 "};",
9145 getLLVMStyleWithColumns(39));
9146 verifyFormat("vector<int> x = {\n"
9147 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
9148 "};",
9149 getLLVMStyleWithColumns(39));
9150 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
9151 " 1, 1, 1, 1,\n"
9152 " /**/ /**/};",
9153 getLLVMStyleWithColumns(39));
9154
9155 // Trailing comment in the first line.
9156 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
9157 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
9158 " 111111111, 222222222, 3333333333, 444444444, //\n"
9159 " 11111111, 22222222, 333333333, 44444444};");
9160 // Trailing comment in the last line.
9161 verifyFormat("int aaaaa[] = {\n"
9162 " 1, 2, 3, // comment\n"
9163 " 4, 5, 6 // comment\n"
9164 "};");
9165
9166 // With nested lists, we should either format one item per line or all nested
9167 // lists one on line.
9168 // FIXME: For some nested lists, we can do better.
9169 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
9170 " {aaaaaaaaaaaaaaaaaaa},\n"
9171 " {aaaaaaaaaaaaaaaaaaaaa},\n"
9172 " {aaaaaaaaaaaaaaaaa}};",
9173 getLLVMStyleWithColumns(60));
9174 verifyFormat(
9175 "SomeStruct my_struct_array = {\n"
9176 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
9177 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
9178 " {aaa, aaa},\n"
9179 " {aaa, aaa},\n"
9180 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
9181 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
9182 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
9183
9184 // No column layout should be used here.
9185 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
9186 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
9187
9188 verifyNoCrash("a<,");
9189
9190 // No braced initializer here.
9191 verifyFormat("void f() {\n"
9192 " struct Dummy {};\n"
9193 " f(v);\n"
9194 "}");
9195
9196 // Long lists should be formatted in columns even if they are nested.
9197 verifyFormat(
9198 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9199 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9200 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9201 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9202 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
9203 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
9204
9205 // Allow "single-column" layout even if that violates the column limit. There
9206 // isn't going to be a better way.
9207 verifyFormat("std::vector<int> a = {\n"
9208 " aaaaaaaa,\n"
9209 " aaaaaaaa,\n"
9210 " aaaaaaaa,\n"
9211 " aaaaaaaa,\n"
9212 " aaaaaaaaaa,\n"
9213 " aaaaaaaa,\n"
9214 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
9215 getLLVMStyleWithColumns(30));
9216 verifyFormat("vector<int> aaaa = {\n"
9217 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9218 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9219 " aaaaaa.aaaaaaa,\n"
9220 " aaaaaa.aaaaaaa,\n"
9221 " aaaaaa.aaaaaaa,\n"
9222 " aaaaaa.aaaaaaa,\n"
9223 "};");
9224
9225 // Don't create hanging lists.
9226 verifyFormat("someFunction(Param, {List1, List2,\n"
9227 " List3});",
9228 getLLVMStyleWithColumns(35));
9229 verifyFormat("someFunction(Param, Param,\n"
9230 " {List1, List2,\n"
9231 " List3});",
9232 getLLVMStyleWithColumns(35));
9233 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
9234 " aaaaaaaaaaaaaaaaaaaaaaa);");
9235 }
9236
TEST_F(FormatTest,PullTrivialFunctionDefinitionsIntoSingleLine)9237 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
9238 FormatStyle DoNotMerge = getLLVMStyle();
9239 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9240
9241 verifyFormat("void f() { return 42; }");
9242 verifyFormat("void f() {\n"
9243 " return 42;\n"
9244 "}",
9245 DoNotMerge);
9246 verifyFormat("void f() {\n"
9247 " // Comment\n"
9248 "}");
9249 verifyFormat("{\n"
9250 "#error {\n"
9251 " int a;\n"
9252 "}");
9253 verifyFormat("{\n"
9254 " int a;\n"
9255 "#error {\n"
9256 "}");
9257 verifyFormat("void f() {} // comment");
9258 verifyFormat("void f() { int a; } // comment");
9259 verifyFormat("void f() {\n"
9260 "} // comment",
9261 DoNotMerge);
9262 verifyFormat("void f() {\n"
9263 " int a;\n"
9264 "} // comment",
9265 DoNotMerge);
9266 verifyFormat("void f() {\n"
9267 "} // comment",
9268 getLLVMStyleWithColumns(15));
9269
9270 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
9271 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
9272
9273 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
9274 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
9275 verifyFormat("class C {\n"
9276 " C()\n"
9277 " : iiiiiiii(nullptr),\n"
9278 " kkkkkkk(nullptr),\n"
9279 " mmmmmmm(nullptr),\n"
9280 " nnnnnnn(nullptr) {}\n"
9281 "};",
9282 getGoogleStyle());
9283
9284 FormatStyle NoColumnLimit = getLLVMStyle();
9285 NoColumnLimit.ColumnLimit = 0;
9286 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
9287 EXPECT_EQ("class C {\n"
9288 " A() : b(0) {}\n"
9289 "};",
9290 format("class C{A():b(0){}};", NoColumnLimit));
9291 EXPECT_EQ("A()\n"
9292 " : b(0) {\n"
9293 "}",
9294 format("A()\n:b(0)\n{\n}", NoColumnLimit));
9295
9296 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
9297 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
9298 FormatStyle::SFS_None;
9299 EXPECT_EQ("A()\n"
9300 " : b(0) {\n"
9301 "}",
9302 format("A():b(0){}", DoNotMergeNoColumnLimit));
9303 EXPECT_EQ("A()\n"
9304 " : b(0) {\n"
9305 "}",
9306 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
9307
9308 verifyFormat("#define A \\\n"
9309 " void f() { \\\n"
9310 " int i; \\\n"
9311 " }",
9312 getLLVMStyleWithColumns(20));
9313 verifyFormat("#define A \\\n"
9314 " void f() { int i; }",
9315 getLLVMStyleWithColumns(21));
9316 verifyFormat("#define A \\\n"
9317 " void f() { \\\n"
9318 " int i; \\\n"
9319 " } \\\n"
9320 " int j;",
9321 getLLVMStyleWithColumns(22));
9322 verifyFormat("#define A \\\n"
9323 " void f() { int i; } \\\n"
9324 " int j;",
9325 getLLVMStyleWithColumns(23));
9326 }
9327
TEST_F(FormatTest,PullEmptyFunctionDefinitionsIntoSingleLine)9328 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
9329 FormatStyle MergeEmptyOnly = getLLVMStyle();
9330 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
9331 verifyFormat("class C {\n"
9332 " int f() {}\n"
9333 "};",
9334 MergeEmptyOnly);
9335 verifyFormat("class C {\n"
9336 " int f() {\n"
9337 " return 42;\n"
9338 " }\n"
9339 "};",
9340 MergeEmptyOnly);
9341 verifyFormat("int f() {}", MergeEmptyOnly);
9342 verifyFormat("int f() {\n"
9343 " return 42;\n"
9344 "}",
9345 MergeEmptyOnly);
9346
9347 // Also verify behavior when BraceWrapping.AfterFunction = true
9348 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
9349 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
9350 verifyFormat("int f() {}", MergeEmptyOnly);
9351 verifyFormat("class C {\n"
9352 " int f() {}\n"
9353 "};",
9354 MergeEmptyOnly);
9355 }
9356
TEST_F(FormatTest,PullInlineFunctionDefinitionsIntoSingleLine)9357 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
9358 FormatStyle MergeInlineOnly = getLLVMStyle();
9359 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9360 verifyFormat("class C {\n"
9361 " int f() { return 42; }\n"
9362 "};",
9363 MergeInlineOnly);
9364 verifyFormat("int f() {\n"
9365 " return 42;\n"
9366 "}",
9367 MergeInlineOnly);
9368
9369 // SFS_Inline implies SFS_Empty
9370 verifyFormat("class C {\n"
9371 " int f() {}\n"
9372 "};",
9373 MergeInlineOnly);
9374 verifyFormat("int f() {}", MergeInlineOnly);
9375
9376 // Also verify behavior when BraceWrapping.AfterFunction = true
9377 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
9378 MergeInlineOnly.BraceWrapping.AfterFunction = true;
9379 verifyFormat("class C {\n"
9380 " int f() { return 42; }\n"
9381 "};",
9382 MergeInlineOnly);
9383 verifyFormat("int f()\n"
9384 "{\n"
9385 " return 42;\n"
9386 "}",
9387 MergeInlineOnly);
9388
9389 // SFS_Inline implies SFS_Empty
9390 verifyFormat("int f() {}", MergeInlineOnly);
9391 verifyFormat("class C {\n"
9392 " int f() {}\n"
9393 "};",
9394 MergeInlineOnly);
9395 }
9396
TEST_F(FormatTest,PullInlineOnlyFunctionDefinitionsIntoSingleLine)9397 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
9398 FormatStyle MergeInlineOnly = getLLVMStyle();
9399 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
9400 FormatStyle::SFS_InlineOnly;
9401 verifyFormat("class C {\n"
9402 " int f() { return 42; }\n"
9403 "};",
9404 MergeInlineOnly);
9405 verifyFormat("int f() {\n"
9406 " return 42;\n"
9407 "}",
9408 MergeInlineOnly);
9409
9410 // SFS_InlineOnly does not imply SFS_Empty
9411 verifyFormat("class C {\n"
9412 " int f() {}\n"
9413 "};",
9414 MergeInlineOnly);
9415 verifyFormat("int f() {\n"
9416 "}",
9417 MergeInlineOnly);
9418
9419 // Also verify behavior when BraceWrapping.AfterFunction = true
9420 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
9421 MergeInlineOnly.BraceWrapping.AfterFunction = true;
9422 verifyFormat("class C {\n"
9423 " int f() { return 42; }\n"
9424 "};",
9425 MergeInlineOnly);
9426 verifyFormat("int f()\n"
9427 "{\n"
9428 " return 42;\n"
9429 "}",
9430 MergeInlineOnly);
9431
9432 // SFS_InlineOnly does not imply SFS_Empty
9433 verifyFormat("int f()\n"
9434 "{\n"
9435 "}",
9436 MergeInlineOnly);
9437 verifyFormat("class C {\n"
9438 " int f() {}\n"
9439 "};",
9440 MergeInlineOnly);
9441 }
9442
TEST_F(FormatTest,SplitEmptyFunction)9443 TEST_F(FormatTest, SplitEmptyFunction) {
9444 FormatStyle Style = getLLVMStyle();
9445 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9446 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9447 Style.BraceWrapping.AfterFunction = true;
9448 Style.BraceWrapping.SplitEmptyFunction = false;
9449 Style.ColumnLimit = 40;
9450
9451 verifyFormat("int f()\n"
9452 "{}",
9453 Style);
9454 verifyFormat("int f()\n"
9455 "{\n"
9456 " return 42;\n"
9457 "}",
9458 Style);
9459 verifyFormat("int f()\n"
9460 "{\n"
9461 " // some comment\n"
9462 "}",
9463 Style);
9464
9465 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
9466 verifyFormat("int f() {}", Style);
9467 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
9468 "{}",
9469 Style);
9470 verifyFormat("int f()\n"
9471 "{\n"
9472 " return 0;\n"
9473 "}",
9474 Style);
9475
9476 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9477 verifyFormat("class Foo {\n"
9478 " int f() {}\n"
9479 "};\n",
9480 Style);
9481 verifyFormat("class Foo {\n"
9482 " int f() { return 0; }\n"
9483 "};\n",
9484 Style);
9485 verifyFormat("class Foo {\n"
9486 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
9487 " {}\n"
9488 "};\n",
9489 Style);
9490 verifyFormat("class Foo {\n"
9491 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
9492 " {\n"
9493 " return 0;\n"
9494 " }\n"
9495 "};\n",
9496 Style);
9497
9498 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9499 verifyFormat("int f() {}", Style);
9500 verifyFormat("int f() { return 0; }", Style);
9501 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
9502 "{}",
9503 Style);
9504 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
9505 "{\n"
9506 " return 0;\n"
9507 "}",
9508 Style);
9509 }
TEST_F(FormatTest,KeepShortFunctionAfterPPElse)9510 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
9511 FormatStyle Style = getLLVMStyle();
9512 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9513 verifyFormat("#ifdef A\n"
9514 "int f() {}\n"
9515 "#else\n"
9516 "int g() {}\n"
9517 "#endif",
9518 Style);
9519 }
9520
TEST_F(FormatTest,SplitEmptyClass)9521 TEST_F(FormatTest, SplitEmptyClass) {
9522 FormatStyle Style = getLLVMStyle();
9523 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9524 Style.BraceWrapping.AfterClass = true;
9525 Style.BraceWrapping.SplitEmptyRecord = false;
9526
9527 verifyFormat("class Foo\n"
9528 "{};",
9529 Style);
9530 verifyFormat("/* something */ class Foo\n"
9531 "{};",
9532 Style);
9533 verifyFormat("template <typename X> class Foo\n"
9534 "{};",
9535 Style);
9536 verifyFormat("class Foo\n"
9537 "{\n"
9538 " Foo();\n"
9539 "};",
9540 Style);
9541 verifyFormat("typedef class Foo\n"
9542 "{\n"
9543 "} Foo_t;",
9544 Style);
9545 }
9546
TEST_F(FormatTest,SplitEmptyStruct)9547 TEST_F(FormatTest, SplitEmptyStruct) {
9548 FormatStyle Style = getLLVMStyle();
9549 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9550 Style.BraceWrapping.AfterStruct = true;
9551 Style.BraceWrapping.SplitEmptyRecord = false;
9552
9553 verifyFormat("struct Foo\n"
9554 "{};",
9555 Style);
9556 verifyFormat("/* something */ struct Foo\n"
9557 "{};",
9558 Style);
9559 verifyFormat("template <typename X> struct Foo\n"
9560 "{};",
9561 Style);
9562 verifyFormat("struct Foo\n"
9563 "{\n"
9564 " Foo();\n"
9565 "};",
9566 Style);
9567 verifyFormat("typedef struct Foo\n"
9568 "{\n"
9569 "} Foo_t;",
9570 Style);
9571 // typedef struct Bar {} Bar_t;
9572 }
9573
TEST_F(FormatTest,SplitEmptyUnion)9574 TEST_F(FormatTest, SplitEmptyUnion) {
9575 FormatStyle Style = getLLVMStyle();
9576 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9577 Style.BraceWrapping.AfterUnion = true;
9578 Style.BraceWrapping.SplitEmptyRecord = false;
9579
9580 verifyFormat("union Foo\n"
9581 "{};",
9582 Style);
9583 verifyFormat("/* something */ union Foo\n"
9584 "{};",
9585 Style);
9586 verifyFormat("union Foo\n"
9587 "{\n"
9588 " A,\n"
9589 "};",
9590 Style);
9591 verifyFormat("typedef union Foo\n"
9592 "{\n"
9593 "} Foo_t;",
9594 Style);
9595 }
9596
TEST_F(FormatTest,SplitEmptyNamespace)9597 TEST_F(FormatTest, SplitEmptyNamespace) {
9598 FormatStyle Style = getLLVMStyle();
9599 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9600 Style.BraceWrapping.AfterNamespace = true;
9601 Style.BraceWrapping.SplitEmptyNamespace = false;
9602
9603 verifyFormat("namespace Foo\n"
9604 "{};",
9605 Style);
9606 verifyFormat("/* something */ namespace Foo\n"
9607 "{};",
9608 Style);
9609 verifyFormat("inline namespace Foo\n"
9610 "{};",
9611 Style);
9612 verifyFormat("/* something */ inline namespace Foo\n"
9613 "{};",
9614 Style);
9615 verifyFormat("export namespace Foo\n"
9616 "{};",
9617 Style);
9618 verifyFormat("namespace Foo\n"
9619 "{\n"
9620 "void Bar();\n"
9621 "};",
9622 Style);
9623 }
9624
TEST_F(FormatTest,NeverMergeShortRecords)9625 TEST_F(FormatTest, NeverMergeShortRecords) {
9626 FormatStyle Style = getLLVMStyle();
9627
9628 verifyFormat("class Foo {\n"
9629 " Foo();\n"
9630 "};",
9631 Style);
9632 verifyFormat("typedef class Foo {\n"
9633 " Foo();\n"
9634 "} Foo_t;",
9635 Style);
9636 verifyFormat("struct Foo {\n"
9637 " Foo();\n"
9638 "};",
9639 Style);
9640 verifyFormat("typedef struct Foo {\n"
9641 " Foo();\n"
9642 "} Foo_t;",
9643 Style);
9644 verifyFormat("union Foo {\n"
9645 " A,\n"
9646 "};",
9647 Style);
9648 verifyFormat("typedef union Foo {\n"
9649 " A,\n"
9650 "} Foo_t;",
9651 Style);
9652 verifyFormat("namespace Foo {\n"
9653 "void Bar();\n"
9654 "};",
9655 Style);
9656
9657 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9658 Style.BraceWrapping.AfterClass = true;
9659 Style.BraceWrapping.AfterStruct = true;
9660 Style.BraceWrapping.AfterUnion = true;
9661 Style.BraceWrapping.AfterNamespace = true;
9662 verifyFormat("class Foo\n"
9663 "{\n"
9664 " Foo();\n"
9665 "};",
9666 Style);
9667 verifyFormat("typedef class Foo\n"
9668 "{\n"
9669 " Foo();\n"
9670 "} Foo_t;",
9671 Style);
9672 verifyFormat("struct Foo\n"
9673 "{\n"
9674 " Foo();\n"
9675 "};",
9676 Style);
9677 verifyFormat("typedef struct Foo\n"
9678 "{\n"
9679 " Foo();\n"
9680 "} Foo_t;",
9681 Style);
9682 verifyFormat("union Foo\n"
9683 "{\n"
9684 " A,\n"
9685 "};",
9686 Style);
9687 verifyFormat("typedef union Foo\n"
9688 "{\n"
9689 " A,\n"
9690 "} Foo_t;",
9691 Style);
9692 verifyFormat("namespace Foo\n"
9693 "{\n"
9694 "void Bar();\n"
9695 "};",
9696 Style);
9697 }
9698
TEST_F(FormatTest,UnderstandContextOfRecordTypeKeywords)9699 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
9700 // Elaborate type variable declarations.
9701 verifyFormat("struct foo a = {bar};\nint n;");
9702 verifyFormat("class foo a = {bar};\nint n;");
9703 verifyFormat("union foo a = {bar};\nint n;");
9704
9705 // Elaborate types inside function definitions.
9706 verifyFormat("struct foo f() {}\nint n;");
9707 verifyFormat("class foo f() {}\nint n;");
9708 verifyFormat("union foo f() {}\nint n;");
9709
9710 // Templates.
9711 verifyFormat("template <class X> void f() {}\nint n;");
9712 verifyFormat("template <struct X> void f() {}\nint n;");
9713 verifyFormat("template <union X> void f() {}\nint n;");
9714
9715 // Actual definitions...
9716 verifyFormat("struct {\n} n;");
9717 verifyFormat(
9718 "template <template <class T, class Y>, class Z> class X {\n} n;");
9719 verifyFormat("union Z {\n int n;\n} x;");
9720 verifyFormat("class MACRO Z {\n} n;");
9721 verifyFormat("class MACRO(X) Z {\n} n;");
9722 verifyFormat("class __attribute__(X) Z {\n} n;");
9723 verifyFormat("class __declspec(X) Z {\n} n;");
9724 verifyFormat("class A##B##C {\n} n;");
9725 verifyFormat("class alignas(16) Z {\n} n;");
9726 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
9727 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
9728
9729 // Redefinition from nested context:
9730 verifyFormat("class A::B::C {\n} n;");
9731
9732 // Template definitions.
9733 verifyFormat(
9734 "template <typename F>\n"
9735 "Matcher(const Matcher<F> &Other,\n"
9736 " typename enable_if_c<is_base_of<F, T>::value &&\n"
9737 " !is_same<F, T>::value>::type * = 0)\n"
9738 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
9739
9740 // FIXME: This is still incorrectly handled at the formatter side.
9741 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
9742 verifyFormat("int i = SomeFunction(a<b, a> b);");
9743
9744 // FIXME:
9745 // This now gets parsed incorrectly as class definition.
9746 // verifyFormat("class A<int> f() {\n}\nint n;");
9747
9748 // Elaborate types where incorrectly parsing the structural element would
9749 // break the indent.
9750 verifyFormat("if (true)\n"
9751 " class X x;\n"
9752 "else\n"
9753 " f();\n");
9754
9755 // This is simply incomplete. Formatting is not important, but must not crash.
9756 verifyFormat("class A:");
9757 }
9758
TEST_F(FormatTest,DoNotInterfereWithErrorAndWarning)9759 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
9760 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
9761 format("#error Leave all white!!!!! space* alone!\n"));
9762 EXPECT_EQ(
9763 "#warning Leave all white!!!!! space* alone!\n",
9764 format("#warning Leave all white!!!!! space* alone!\n"));
9765 EXPECT_EQ("#error 1", format(" # error 1"));
9766 EXPECT_EQ("#warning 1", format(" # warning 1"));
9767 }
9768
TEST_F(FormatTest,FormatHashIfExpressions)9769 TEST_F(FormatTest, FormatHashIfExpressions) {
9770 verifyFormat("#if AAAA && BBBB");
9771 verifyFormat("#if (AAAA && BBBB)");
9772 verifyFormat("#elif (AAAA && BBBB)");
9773 // FIXME: Come up with a better indentation for #elif.
9774 verifyFormat(
9775 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
9776 " defined(BBBBBBBB)\n"
9777 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
9778 " defined(BBBBBBBB)\n"
9779 "#endif",
9780 getLLVMStyleWithColumns(65));
9781 }
9782
TEST_F(FormatTest,MergeHandlingInTheFaceOfPreprocessorDirectives)9783 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
9784 FormatStyle AllowsMergedIf = getGoogleStyle();
9785 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
9786 FormatStyle::SIS_WithoutElse;
9787 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
9788 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
9789 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
9790 EXPECT_EQ("if (true) return 42;",
9791 format("if (true)\nreturn 42;", AllowsMergedIf));
9792 FormatStyle ShortMergedIf = AllowsMergedIf;
9793 ShortMergedIf.ColumnLimit = 25;
9794 verifyFormat("#define A \\\n"
9795 " if (true) return 42;",
9796 ShortMergedIf);
9797 verifyFormat("#define A \\\n"
9798 " f(); \\\n"
9799 " if (true)\n"
9800 "#define B",
9801 ShortMergedIf);
9802 verifyFormat("#define A \\\n"
9803 " f(); \\\n"
9804 " if (true)\n"
9805 "g();",
9806 ShortMergedIf);
9807 verifyFormat("{\n"
9808 "#ifdef A\n"
9809 " // Comment\n"
9810 " if (true) continue;\n"
9811 "#endif\n"
9812 " // Comment\n"
9813 " if (true) continue;\n"
9814 "}",
9815 ShortMergedIf);
9816 ShortMergedIf.ColumnLimit = 33;
9817 verifyFormat("#define A \\\n"
9818 " if constexpr (true) return 42;",
9819 ShortMergedIf);
9820 verifyFormat("#define A \\\n"
9821 " if CONSTEXPR (true) return 42;",
9822 ShortMergedIf);
9823 ShortMergedIf.ColumnLimit = 29;
9824 verifyFormat("#define A \\\n"
9825 " if (aaaaaaaaaa) return 1; \\\n"
9826 " return 2;",
9827 ShortMergedIf);
9828 ShortMergedIf.ColumnLimit = 28;
9829 verifyFormat("#define A \\\n"
9830 " if (aaaaaaaaaa) \\\n"
9831 " return 1; \\\n"
9832 " return 2;",
9833 ShortMergedIf);
9834 verifyFormat("#define A \\\n"
9835 " if constexpr (aaaaaaa) \\\n"
9836 " return 1; \\\n"
9837 " return 2;",
9838 ShortMergedIf);
9839 verifyFormat("#define A \\\n"
9840 " if CONSTEXPR (aaaaaaa) \\\n"
9841 " return 1; \\\n"
9842 " return 2;",
9843 ShortMergedIf);
9844 }
9845
TEST_F(FormatTest,FormatStarDependingOnContext)9846 TEST_F(FormatTest, FormatStarDependingOnContext) {
9847 verifyFormat("void f(int *a);");
9848 verifyFormat("void f() { f(fint * b); }");
9849 verifyFormat("class A {\n void f(int *a);\n};");
9850 verifyFormat("class A {\n int *a;\n};");
9851 verifyFormat("namespace a {\n"
9852 "namespace b {\n"
9853 "class A {\n"
9854 " void f() {}\n"
9855 " int *a;\n"
9856 "};\n"
9857 "} // namespace b\n"
9858 "} // namespace a");
9859 }
9860
TEST_F(FormatTest,SpecialTokensAtEndOfLine)9861 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
9862 verifyFormat("while");
9863 verifyFormat("operator");
9864 }
9865
TEST_F(FormatTest,SkipsDeeplyNestedLines)9866 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
9867 // This code would be painfully slow to format if we didn't skip it.
9868 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
9869 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
9870 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
9871 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
9872 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
9873 "A(1, 1)\n"
9874 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
9875 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9876 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9877 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9878 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9879 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9880 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9881 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9882 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
9883 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
9884 // Deeply nested part is untouched, rest is formatted.
9885 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
9886 format(std::string("int i;\n") + Code + "int j;\n",
9887 getLLVMStyle(), SC_ExpectIncomplete));
9888 }
9889
9890 //===----------------------------------------------------------------------===//
9891 // Objective-C tests.
9892 //===----------------------------------------------------------------------===//
9893
TEST_F(FormatTest,FormatForObjectiveCMethodDecls)9894 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
9895 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
9896 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
9897 format("-(NSUInteger)indexOfObject:(id)anObject;"));
9898 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
9899 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
9900 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
9901 format("-(NSInteger)Method3:(id)anObject;"));
9902 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
9903 format("-(NSInteger)Method4:(id)anObject;"));
9904 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
9905 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
9906 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
9907 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
9908 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
9909 "forAllCells:(BOOL)flag;",
9910 format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
9911 "forAllCells:(BOOL)flag;"));
9912
9913 // Very long objectiveC method declaration.
9914 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
9915 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
9916 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
9917 " inRange:(NSRange)range\n"
9918 " outRange:(NSRange)out_range\n"
9919 " outRange1:(NSRange)out_range1\n"
9920 " outRange2:(NSRange)out_range2\n"
9921 " outRange3:(NSRange)out_range3\n"
9922 " outRange4:(NSRange)out_range4\n"
9923 " outRange5:(NSRange)out_range5\n"
9924 " outRange6:(NSRange)out_range6\n"
9925 " outRange7:(NSRange)out_range7\n"
9926 " outRange8:(NSRange)out_range8\n"
9927 " outRange9:(NSRange)out_range9;");
9928
9929 // When the function name has to be wrapped.
9930 FormatStyle Style = getLLVMStyle();
9931 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
9932 // and always indents instead.
9933 Style.IndentWrappedFunctionNames = false;
9934 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
9935 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
9936 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
9937 "}",
9938 Style);
9939 Style.IndentWrappedFunctionNames = true;
9940 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
9941 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
9942 " anotherName:(NSString)dddddddddddddd {\n"
9943 "}",
9944 Style);
9945
9946 verifyFormat("- (int)sum:(vector<int>)numbers;");
9947 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
9948 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
9949 // protocol lists (but not for template classes):
9950 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
9951
9952 verifyFormat("- (int (*)())foo:(int (*)())f;");
9953 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
9954
9955 // If there's no return type (very rare in practice!), LLVM and Google style
9956 // agree.
9957 verifyFormat("- foo;");
9958 verifyFormat("- foo:(int)f;");
9959 verifyGoogleFormat("- foo:(int)foo;");
9960 }
9961
TEST_F(FormatTest,BreaksStringLiterals)9962 TEST_F(FormatTest, BreaksStringLiterals) {
9963 EXPECT_EQ("\"some text \"\n"
9964 "\"other\";",
9965 format("\"some text other\";", getLLVMStyleWithColumns(12)));
9966 EXPECT_EQ("\"some text \"\n"
9967 "\"other\";",
9968 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
9969 EXPECT_EQ(
9970 "#define A \\\n"
9971 " \"some \" \\\n"
9972 " \"text \" \\\n"
9973 " \"other\";",
9974 format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
9975 EXPECT_EQ(
9976 "#define A \\\n"
9977 " \"so \" \\\n"
9978 " \"text \" \\\n"
9979 " \"other\";",
9980 format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
9981
9982 EXPECT_EQ("\"some text\"",
9983 format("\"some text\"", getLLVMStyleWithColumns(1)));
9984 EXPECT_EQ("\"some text\"",
9985 format("\"some text\"", getLLVMStyleWithColumns(11)));
9986 EXPECT_EQ("\"some \"\n"
9987 "\"text\"",
9988 format("\"some text\"", getLLVMStyleWithColumns(10)));
9989 EXPECT_EQ("\"some \"\n"
9990 "\"text\"",
9991 format("\"some text\"", getLLVMStyleWithColumns(7)));
9992 EXPECT_EQ("\"some\"\n"
9993 "\" tex\"\n"
9994 "\"t\"",
9995 format("\"some text\"", getLLVMStyleWithColumns(6)));
9996 EXPECT_EQ("\"some\"\n"
9997 "\" tex\"\n"
9998 "\" and\"",
9999 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
10000 EXPECT_EQ("\"some\"\n"
10001 "\"/tex\"\n"
10002 "\"/and\"",
10003 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
10004
10005 EXPECT_EQ("variable =\n"
10006 " \"long string \"\n"
10007 " \"literal\";",
10008 format("variable = \"long string literal\";",
10009 getLLVMStyleWithColumns(20)));
10010
10011 EXPECT_EQ("variable = f(\n"
10012 " \"long string \"\n"
10013 " \"literal\",\n"
10014 " short,\n"
10015 " loooooooooooooooooooong);",
10016 format("variable = f(\"long string literal\", short, "
10017 "loooooooooooooooooooong);",
10018 getLLVMStyleWithColumns(20)));
10019
10020 EXPECT_EQ(
10021 "f(g(\"long string \"\n"
10022 " \"literal\"),\n"
10023 " b);",
10024 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
10025 EXPECT_EQ("f(g(\"long string \"\n"
10026 " \"literal\",\n"
10027 " a),\n"
10028 " b);",
10029 format("f(g(\"long string literal\", a), b);",
10030 getLLVMStyleWithColumns(20)));
10031 EXPECT_EQ(
10032 "f(\"one two\".split(\n"
10033 " variable));",
10034 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
10035 EXPECT_EQ("f(\"one two three four five six \"\n"
10036 " \"seven\".split(\n"
10037 " really_looooong_variable));",
10038 format("f(\"one two three four five six seven\"."
10039 "split(really_looooong_variable));",
10040 getLLVMStyleWithColumns(33)));
10041
10042 EXPECT_EQ("f(\"some \"\n"
10043 " \"text\",\n"
10044 " other);",
10045 format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
10046
10047 // Only break as a last resort.
10048 verifyFormat(
10049 "aaaaaaaaaaaaaaaaaaaa(\n"
10050 " aaaaaaaaaaaaaaaaaaaa,\n"
10051 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
10052
10053 EXPECT_EQ("\"splitmea\"\n"
10054 "\"trandomp\"\n"
10055 "\"oint\"",
10056 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
10057
10058 EXPECT_EQ("\"split/\"\n"
10059 "\"pathat/\"\n"
10060 "\"slashes\"",
10061 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
10062
10063 EXPECT_EQ("\"split/\"\n"
10064 "\"pathat/\"\n"
10065 "\"slashes\"",
10066 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
10067 EXPECT_EQ("\"split at \"\n"
10068 "\"spaces/at/\"\n"
10069 "\"slashes.at.any$\"\n"
10070 "\"non-alphanumeric%\"\n"
10071 "\"1111111111characte\"\n"
10072 "\"rs\"",
10073 format("\"split at "
10074 "spaces/at/"
10075 "slashes.at."
10076 "any$non-"
10077 "alphanumeric%"
10078 "1111111111characte"
10079 "rs\"",
10080 getLLVMStyleWithColumns(20)));
10081
10082 // Verify that splitting the strings understands
10083 // Style::AlwaysBreakBeforeMultilineStrings.
10084 EXPECT_EQ("aaaaaaaaaaaa(\n"
10085 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
10086 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
10087 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
10088 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
10089 "aaaaaaaaaaaaaaaaaaaaaa\");",
10090 getGoogleStyle()));
10091 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10092 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
10093 format("return \"aaaaaaaaaaaaaaaaaaaaaa "
10094 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
10095 "aaaaaaaaaaaaaaaaaaaaaa\";",
10096 getGoogleStyle()));
10097 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10098 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
10099 format("llvm::outs() << "
10100 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
10101 "aaaaaaaaaaaaaaaaaaa\";"));
10102 EXPECT_EQ("ffff(\n"
10103 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10104 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
10105 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10106 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
10107 getGoogleStyle()));
10108
10109 FormatStyle Style = getLLVMStyleWithColumns(12);
10110 Style.BreakStringLiterals = false;
10111 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
10112
10113 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
10114 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10115 EXPECT_EQ("#define A \\\n"
10116 " \"some \" \\\n"
10117 " \"text \" \\\n"
10118 " \"other\";",
10119 format("#define A \"some text other\";", AlignLeft));
10120 }
10121
TEST_F(FormatTest,BreaksStringLiteralsAtColumnLimit)10122 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
10123 EXPECT_EQ("C a = \"some more \"\n"
10124 " \"text\";",
10125 format("C a = \"some more text\";", getLLVMStyleWithColumns(18)));
10126 }
10127
TEST_F(FormatTest,FullyRemoveEmptyLines)10128 TEST_F(FormatTest, FullyRemoveEmptyLines) {
10129 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
10130 NoEmptyLines.MaxEmptyLinesToKeep = 0;
10131 EXPECT_EQ("int i = a(b());",
10132 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
10133 }
10134
TEST_F(FormatTest,BreaksStringLiteralsWithTabs)10135 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
10136 EXPECT_EQ(
10137 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10138 "(\n"
10139 " \"x\t\");",
10140 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10141 "aaaaaaa("
10142 "\"x\t\");"));
10143 }
10144
TEST_F(FormatTest,BreaksWideAndNSStringLiterals)10145 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
10146 EXPECT_EQ(
10147 "u8\"utf8 string \"\n"
10148 "u8\"literal\";",
10149 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
10150 EXPECT_EQ(
10151 "u\"utf16 string \"\n"
10152 "u\"literal\";",
10153 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
10154 EXPECT_EQ(
10155 "U\"utf32 string \"\n"
10156 "U\"literal\";",
10157 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
10158 EXPECT_EQ("L\"wide string \"\n"
10159 "L\"literal\";",
10160 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
10161 EXPECT_EQ("@\"NSString \"\n"
10162 "@\"literal\";",
10163 format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
10164 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
10165
10166 // This input makes clang-format try to split the incomplete unicode escape
10167 // sequence, which used to lead to a crasher.
10168 verifyNoCrash(
10169 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
10170 getLLVMStyleWithColumns(60));
10171 }
10172
TEST_F(FormatTest,DoesNotBreakRawStringLiterals)10173 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
10174 FormatStyle Style = getGoogleStyleWithColumns(15);
10175 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
10176 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
10177 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
10178 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
10179 EXPECT_EQ("u8R\"x(raw literal)x\";",
10180 format("u8R\"x(raw literal)x\";", Style));
10181 }
10182
TEST_F(FormatTest,BreaksStringLiteralsWithin_TMacro)10183 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
10184 FormatStyle Style = getLLVMStyleWithColumns(20);
10185 EXPECT_EQ(
10186 "_T(\"aaaaaaaaaaaaaa\")\n"
10187 "_T(\"aaaaaaaaaaaaaa\")\n"
10188 "_T(\"aaaaaaaaaaaa\")",
10189 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
10190 EXPECT_EQ("f(x,\n"
10191 " _T(\"aaaaaaaaaaaa\")\n"
10192 " _T(\"aaa\"),\n"
10193 " z);",
10194 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
10195
10196 // FIXME: Handle embedded spaces in one iteration.
10197 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
10198 // "_T(\"aaaaaaaaaaaaa\")\n"
10199 // "_T(\"aaaaaaaaaaaaa\")\n"
10200 // "_T(\"a\")",
10201 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
10202 // getLLVMStyleWithColumns(20)));
10203 EXPECT_EQ(
10204 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
10205 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
10206 EXPECT_EQ("f(\n"
10207 "#if !TEST\n"
10208 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
10209 "#endif\n"
10210 ");",
10211 format("f(\n"
10212 "#if !TEST\n"
10213 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
10214 "#endif\n"
10215 ");"));
10216 EXPECT_EQ("f(\n"
10217 "\n"
10218 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
10219 format("f(\n"
10220 "\n"
10221 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
10222 }
10223
TEST_F(FormatTest,BreaksStringLiteralOperands)10224 TEST_F(FormatTest, BreaksStringLiteralOperands) {
10225 // In a function call with two operands, the second can be broken with no line
10226 // break before it.
10227 EXPECT_EQ(
10228 "func(a, \"long long \"\n"
10229 " \"long long\");",
10230 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24)));
10231 // In a function call with three operands, the second must be broken with a
10232 // line break before it.
10233 EXPECT_EQ("func(a,\n"
10234 " \"long long long \"\n"
10235 " \"long\",\n"
10236 " c);",
10237 format("func(a, \"long long long long\", c);",
10238 getLLVMStyleWithColumns(24)));
10239 // In a function call with three operands, the third must be broken with a
10240 // line break before it.
10241 EXPECT_EQ("func(a, b,\n"
10242 " \"long long long \"\n"
10243 " \"long\");",
10244 format("func(a, b, \"long long long long\");",
10245 getLLVMStyleWithColumns(24)));
10246 // In a function call with three operands, both the second and the third must
10247 // be broken with a line break before them.
10248 EXPECT_EQ("func(a,\n"
10249 " \"long long long \"\n"
10250 " \"long\",\n"
10251 " \"long long long \"\n"
10252 " \"long\");",
10253 format("func(a, \"long long long long\", \"long long long long\");",
10254 getLLVMStyleWithColumns(24)));
10255 // In a chain of << with two operands, the second can be broken with no line
10256 // break before it.
10257 EXPECT_EQ("a << \"line line \"\n"
10258 " \"line\";",
10259 format("a << \"line line line\";", getLLVMStyleWithColumns(20)));
10260 // In a chain of << with three operands, the second can be broken with no line
10261 // break before it.
10262 EXPECT_EQ(
10263 "abcde << \"line \"\n"
10264 " \"line line\"\n"
10265 " << c;",
10266 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20)));
10267 // In a chain of << with three operands, the third must be broken with a line
10268 // break before it.
10269 EXPECT_EQ(
10270 "a << b\n"
10271 " << \"line line \"\n"
10272 " \"line\";",
10273 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20)));
10274 // In a chain of << with three operands, the second can be broken with no line
10275 // break before it and the third must be broken with a line break before it.
10276 EXPECT_EQ("abcd << \"line line \"\n"
10277 " \"line\"\n"
10278 " << \"line line \"\n"
10279 " \"line\";",
10280 format("abcd << \"line line line\" << \"line line line\";",
10281 getLLVMStyleWithColumns(20)));
10282 // In a chain of binary operators with two operands, the second can be broken
10283 // with no line break before it.
10284 EXPECT_EQ(
10285 "abcd + \"line line \"\n"
10286 " \"line line\";",
10287 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20)));
10288 // In a chain of binary operators with three operands, the second must be
10289 // broken with a line break before it.
10290 EXPECT_EQ("abcd +\n"
10291 " \"line line \"\n"
10292 " \"line line\" +\n"
10293 " e;",
10294 format("abcd + \"line line line line\" + e;",
10295 getLLVMStyleWithColumns(20)));
10296 // In a function call with two operands, with AlignAfterOpenBracket enabled,
10297 // the first must be broken with a line break before it.
10298 FormatStyle Style = getLLVMStyleWithColumns(25);
10299 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10300 EXPECT_EQ("someFunction(\n"
10301 " \"long long long \"\n"
10302 " \"long\",\n"
10303 " a);",
10304 format("someFunction(\"long long long long\", a);", Style));
10305 }
10306
TEST_F(FormatTest,DontSplitStringLiteralsWithEscapedNewlines)10307 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
10308 EXPECT_EQ(
10309 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
10310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
10311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
10312 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
10313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
10314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
10315 }
10316
TEST_F(FormatTest,CountsCharactersInMultilineRawStringLiterals)10317 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
10318 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
10319 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
10320 EXPECT_EQ("fffffffffff(g(R\"x(\n"
10321 "multiline raw string literal xxxxxxxxxxxxxx\n"
10322 ")x\",\n"
10323 " a),\n"
10324 " b);",
10325 format("fffffffffff(g(R\"x(\n"
10326 "multiline raw string literal xxxxxxxxxxxxxx\n"
10327 ")x\", a), b);",
10328 getGoogleStyleWithColumns(20)));
10329 EXPECT_EQ("fffffffffff(\n"
10330 " g(R\"x(qqq\n"
10331 "multiline raw string literal xxxxxxxxxxxxxx\n"
10332 ")x\",\n"
10333 " a),\n"
10334 " b);",
10335 format("fffffffffff(g(R\"x(qqq\n"
10336 "multiline raw string literal xxxxxxxxxxxxxx\n"
10337 ")x\", a), b);",
10338 getGoogleStyleWithColumns(20)));
10339
10340 EXPECT_EQ("fffffffffff(R\"x(\n"
10341 "multiline raw string literal xxxxxxxxxxxxxx\n"
10342 ")x\");",
10343 format("fffffffffff(R\"x(\n"
10344 "multiline raw string literal xxxxxxxxxxxxxx\n"
10345 ")x\");",
10346 getGoogleStyleWithColumns(20)));
10347 EXPECT_EQ("fffffffffff(R\"x(\n"
10348 "multiline raw string literal xxxxxxxxxxxxxx\n"
10349 ")x\" + bbbbbb);",
10350 format("fffffffffff(R\"x(\n"
10351 "multiline raw string literal xxxxxxxxxxxxxx\n"
10352 ")x\" + bbbbbb);",
10353 getGoogleStyleWithColumns(20)));
10354 EXPECT_EQ("fffffffffff(\n"
10355 " R\"x(\n"
10356 "multiline raw string literal xxxxxxxxxxxxxx\n"
10357 ")x\" +\n"
10358 " bbbbbb);",
10359 format("fffffffffff(\n"
10360 " R\"x(\n"
10361 "multiline raw string literal xxxxxxxxxxxxxx\n"
10362 ")x\" + bbbbbb);",
10363 getGoogleStyleWithColumns(20)));
10364 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
10365 format("fffffffffff(\n"
10366 " R\"(single line raw string)\" + bbbbbb);"));
10367 }
10368
TEST_F(FormatTest,SkipsUnknownStringLiterals)10369 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
10370 verifyFormat("string a = \"unterminated;");
10371 EXPECT_EQ("function(\"unterminated,\n"
10372 " OtherParameter);",
10373 format("function( \"unterminated,\n"
10374 " OtherParameter);"));
10375 }
10376
TEST_F(FormatTest,DoesNotTryToParseUDLiteralsInPreCpp11Code)10377 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
10378 FormatStyle Style = getLLVMStyle();
10379 Style.Standard = FormatStyle::LS_Cpp03;
10380 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
10381 format("#define x(_a) printf(\"foo\"_a);", Style));
10382 }
10383
TEST_F(FormatTest,CppLexVersion)10384 TEST_F(FormatTest, CppLexVersion) {
10385 FormatStyle Style = getLLVMStyle();
10386 // Formatting of x * y differs if x is a type.
10387 verifyFormat("void foo() { MACRO(a * b); }", Style);
10388 verifyFormat("void foo() { MACRO(int *b); }", Style);
10389
10390 // LLVM style uses latest lexer.
10391 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
10392 Style.Standard = FormatStyle::LS_Cpp17;
10393 // But in c++17, char8_t isn't a keyword.
10394 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
10395 }
10396
TEST_F(FormatTest,UnderstandsCpp1y)10397 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
10398
TEST_F(FormatTest,BreakStringLiteralsBeforeUnbreakableTokenSequence)10399 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
10400 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
10401 " \"ddeeefff\");",
10402 format("someFunction(\"aaabbbcccdddeeefff\");",
10403 getLLVMStyleWithColumns(25)));
10404 EXPECT_EQ("someFunction1234567890(\n"
10405 " \"aaabbbcccdddeeefff\");",
10406 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
10407 getLLVMStyleWithColumns(26)));
10408 EXPECT_EQ("someFunction1234567890(\n"
10409 " \"aaabbbcccdddeeeff\"\n"
10410 " \"f\");",
10411 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
10412 getLLVMStyleWithColumns(25)));
10413 EXPECT_EQ("someFunction1234567890(\n"
10414 " \"aaabbbcccdddeeeff\"\n"
10415 " \"f\");",
10416 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
10417 getLLVMStyleWithColumns(24)));
10418 EXPECT_EQ("someFunction(\n"
10419 " \"aaabbbcc ddde \"\n"
10420 " \"efff\");",
10421 format("someFunction(\"aaabbbcc ddde efff\");",
10422 getLLVMStyleWithColumns(25)));
10423 EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
10424 " \"ddeeefff\");",
10425 format("someFunction(\"aaabbbccc ddeeefff\");",
10426 getLLVMStyleWithColumns(25)));
10427 EXPECT_EQ("someFunction1234567890(\n"
10428 " \"aaabb \"\n"
10429 " \"cccdddeeefff\");",
10430 format("someFunction1234567890(\"aaabb cccdddeeefff\");",
10431 getLLVMStyleWithColumns(25)));
10432 EXPECT_EQ("#define A \\\n"
10433 " string s = \\\n"
10434 " \"123456789\" \\\n"
10435 " \"0\"; \\\n"
10436 " int i;",
10437 format("#define A string s = \"1234567890\"; int i;",
10438 getLLVMStyleWithColumns(20)));
10439 EXPECT_EQ("someFunction(\n"
10440 " \"aaabbbcc \"\n"
10441 " \"dddeeefff\");",
10442 format("someFunction(\"aaabbbcc dddeeefff\");",
10443 getLLVMStyleWithColumns(25)));
10444 }
10445
TEST_F(FormatTest,DoNotBreakStringLiteralsInEscapeSequence)10446 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
10447 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
10448 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
10449 EXPECT_EQ("\"test\"\n"
10450 "\"\\n\"",
10451 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
10452 EXPECT_EQ("\"tes\\\\\"\n"
10453 "\"n\"",
10454 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
10455 EXPECT_EQ("\"\\\\\\\\\"\n"
10456 "\"\\n\"",
10457 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
10458 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
10459 EXPECT_EQ("\"\\uff01\"\n"
10460 "\"test\"",
10461 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
10462 EXPECT_EQ("\"\\Uff01ff02\"",
10463 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
10464 EXPECT_EQ("\"\\x000000000001\"\n"
10465 "\"next\"",
10466 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
10467 EXPECT_EQ("\"\\x000000000001next\"",
10468 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
10469 EXPECT_EQ("\"\\x000000000001\"",
10470 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
10471 EXPECT_EQ("\"test\"\n"
10472 "\"\\000000\"\n"
10473 "\"000001\"",
10474 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
10475 EXPECT_EQ("\"test\\000\"\n"
10476 "\"00000000\"\n"
10477 "\"1\"",
10478 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
10479 }
10480
TEST_F(FormatTest,DoNotCreateUnreasonableUnwrappedLines)10481 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
10482 verifyFormat("void f() {\n"
10483 " return g() {}\n"
10484 " void h() {}");
10485 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
10486 "g();\n"
10487 "}");
10488 }
10489
TEST_F(FormatTest,DoNotPrematurelyEndUnwrappedLineForReturnStatements)10490 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
10491 verifyFormat(
10492 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
10493 }
10494
TEST_F(FormatTest,FormatsClosingBracesInEmptyNestedBlocks)10495 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
10496 verifyFormat("class X {\n"
10497 " void f() {\n"
10498 " }\n"
10499 "};",
10500 getLLVMStyleWithColumns(12));
10501 }
10502
TEST_F(FormatTest,ConfigurableIndentWidth)10503 TEST_F(FormatTest, ConfigurableIndentWidth) {
10504 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
10505 EightIndent.IndentWidth = 8;
10506 EightIndent.ContinuationIndentWidth = 8;
10507 verifyFormat("void f() {\n"
10508 " someFunction();\n"
10509 " if (true) {\n"
10510 " f();\n"
10511 " }\n"
10512 "}",
10513 EightIndent);
10514 verifyFormat("class X {\n"
10515 " void f() {\n"
10516 " }\n"
10517 "};",
10518 EightIndent);
10519 verifyFormat("int x[] = {\n"
10520 " call(),\n"
10521 " call()};",
10522 EightIndent);
10523 }
10524
TEST_F(FormatTest,ConfigurableFunctionDeclarationIndentAfterType)10525 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
10526 verifyFormat("double\n"
10527 "f();",
10528 getLLVMStyleWithColumns(8));
10529 }
10530
TEST_F(FormatTest,ConfigurableUseOfTab)10531 TEST_F(FormatTest, ConfigurableUseOfTab) {
10532 FormatStyle Tab = getLLVMStyleWithColumns(42);
10533 Tab.IndentWidth = 8;
10534 Tab.UseTab = FormatStyle::UT_Always;
10535 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10536
10537 EXPECT_EQ("if (aaaaaaaa && // q\n"
10538 " bb)\t\t// w\n"
10539 "\t;",
10540 format("if (aaaaaaaa &&// q\n"
10541 "bb)// w\n"
10542 ";",
10543 Tab));
10544 EXPECT_EQ("if (aaa && bbb) // w\n"
10545 "\t;",
10546 format("if(aaa&&bbb)// w\n"
10547 ";",
10548 Tab));
10549
10550 verifyFormat("class X {\n"
10551 "\tvoid f() {\n"
10552 "\t\tsomeFunction(parameter1,\n"
10553 "\t\t\t parameter2);\n"
10554 "\t}\n"
10555 "};",
10556 Tab);
10557 verifyFormat("#define A \\\n"
10558 "\tvoid f() { \\\n"
10559 "\t\tsomeFunction( \\\n"
10560 "\t\t parameter1, \\\n"
10561 "\t\t parameter2); \\\n"
10562 "\t}",
10563 Tab);
10564 verifyFormat("int a;\t // x\n"
10565 "int bbbbbbbb; // x\n",
10566 Tab);
10567
10568 Tab.TabWidth = 4;
10569 Tab.IndentWidth = 8;
10570 verifyFormat("class TabWidth4Indent8 {\n"
10571 "\t\tvoid f() {\n"
10572 "\t\t\t\tsomeFunction(parameter1,\n"
10573 "\t\t\t\t\t\t\t parameter2);\n"
10574 "\t\t}\n"
10575 "};",
10576 Tab);
10577
10578 Tab.TabWidth = 4;
10579 Tab.IndentWidth = 4;
10580 verifyFormat("class TabWidth4Indent4 {\n"
10581 "\tvoid f() {\n"
10582 "\t\tsomeFunction(parameter1,\n"
10583 "\t\t\t\t\t parameter2);\n"
10584 "\t}\n"
10585 "};",
10586 Tab);
10587
10588 Tab.TabWidth = 8;
10589 Tab.IndentWidth = 4;
10590 verifyFormat("class TabWidth8Indent4 {\n"
10591 " void f() {\n"
10592 "\tsomeFunction(parameter1,\n"
10593 "\t\t parameter2);\n"
10594 " }\n"
10595 "};",
10596 Tab);
10597
10598 Tab.TabWidth = 8;
10599 Tab.IndentWidth = 8;
10600 EXPECT_EQ("/*\n"
10601 "\t a\t\tcomment\n"
10602 "\t in multiple lines\n"
10603 " */",
10604 format(" /*\t \t \n"
10605 " \t \t a\t\tcomment\t \t\n"
10606 " \t \t in multiple lines\t\n"
10607 " \t */",
10608 Tab));
10609
10610 Tab.UseTab = FormatStyle::UT_ForIndentation;
10611 verifyFormat("{\n"
10612 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10613 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10614 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10615 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10616 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10617 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10618 "};",
10619 Tab);
10620 verifyFormat("enum AA {\n"
10621 "\ta1, // Force multiple lines\n"
10622 "\ta2,\n"
10623 "\ta3\n"
10624 "};",
10625 Tab);
10626 EXPECT_EQ("if (aaaaaaaa && // q\n"
10627 " bb) // w\n"
10628 "\t;",
10629 format("if (aaaaaaaa &&// q\n"
10630 "bb)// w\n"
10631 ";",
10632 Tab));
10633 verifyFormat("class X {\n"
10634 "\tvoid f() {\n"
10635 "\t\tsomeFunction(parameter1,\n"
10636 "\t\t parameter2);\n"
10637 "\t}\n"
10638 "};",
10639 Tab);
10640 verifyFormat("{\n"
10641 "\tQ(\n"
10642 "\t {\n"
10643 "\t\t int a;\n"
10644 "\t\t someFunction(aaaaaaaa,\n"
10645 "\t\t bbbbbbb);\n"
10646 "\t },\n"
10647 "\t p);\n"
10648 "}",
10649 Tab);
10650 EXPECT_EQ("{\n"
10651 "\t/* aaaa\n"
10652 "\t bbbb */\n"
10653 "}",
10654 format("{\n"
10655 "/* aaaa\n"
10656 " bbbb */\n"
10657 "}",
10658 Tab));
10659 EXPECT_EQ("{\n"
10660 "\t/*\n"
10661 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10662 "\t bbbbbbbbbbbbb\n"
10663 "\t*/\n"
10664 "}",
10665 format("{\n"
10666 "/*\n"
10667 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10668 "*/\n"
10669 "}",
10670 Tab));
10671 EXPECT_EQ("{\n"
10672 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10673 "\t// bbbbbbbbbbbbb\n"
10674 "}",
10675 format("{\n"
10676 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10677 "}",
10678 Tab));
10679 EXPECT_EQ("{\n"
10680 "\t/*\n"
10681 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10682 "\t bbbbbbbbbbbbb\n"
10683 "\t*/\n"
10684 "}",
10685 format("{\n"
10686 "\t/*\n"
10687 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10688 "\t*/\n"
10689 "}",
10690 Tab));
10691 EXPECT_EQ("{\n"
10692 "\t/*\n"
10693 "\n"
10694 "\t*/\n"
10695 "}",
10696 format("{\n"
10697 "\t/*\n"
10698 "\n"
10699 "\t*/\n"
10700 "}",
10701 Tab));
10702 EXPECT_EQ("{\n"
10703 "\t/*\n"
10704 " asdf\n"
10705 "\t*/\n"
10706 "}",
10707 format("{\n"
10708 "\t/*\n"
10709 " asdf\n"
10710 "\t*/\n"
10711 "}",
10712 Tab));
10713
10714 Tab.UseTab = FormatStyle::UT_Never;
10715 EXPECT_EQ("/*\n"
10716 " a\t\tcomment\n"
10717 " in multiple lines\n"
10718 " */",
10719 format(" /*\t \t \n"
10720 " \t \t a\t\tcomment\t \t\n"
10721 " \t \t in multiple lines\t\n"
10722 " \t */",
10723 Tab));
10724 EXPECT_EQ("/* some\n"
10725 " comment */",
10726 format(" \t \t /* some\n"
10727 " \t \t comment */",
10728 Tab));
10729 EXPECT_EQ("int a; /* some\n"
10730 " comment */",
10731 format(" \t \t int a; /* some\n"
10732 " \t \t comment */",
10733 Tab));
10734
10735 EXPECT_EQ("int a; /* some\n"
10736 "comment */",
10737 format(" \t \t int\ta; /* some\n"
10738 " \t \t comment */",
10739 Tab));
10740 EXPECT_EQ("f(\"\t\t\"); /* some\n"
10741 " comment */",
10742 format(" \t \t f(\"\t\t\"); /* some\n"
10743 " \t \t comment */",
10744 Tab));
10745 EXPECT_EQ("{\n"
10746 " /*\n"
10747 " * Comment\n"
10748 " */\n"
10749 " int i;\n"
10750 "}",
10751 format("{\n"
10752 "\t/*\n"
10753 "\t * Comment\n"
10754 "\t */\n"
10755 "\t int i;\n"
10756 "}",
10757 Tab));
10758
10759 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
10760 Tab.TabWidth = 8;
10761 Tab.IndentWidth = 8;
10762 EXPECT_EQ("if (aaaaaaaa && // q\n"
10763 " bb) // w\n"
10764 "\t;",
10765 format("if (aaaaaaaa &&// q\n"
10766 "bb)// w\n"
10767 ";",
10768 Tab));
10769 EXPECT_EQ("if (aaa && bbb) // w\n"
10770 "\t;",
10771 format("if(aaa&&bbb)// w\n"
10772 ";",
10773 Tab));
10774 verifyFormat("class X {\n"
10775 "\tvoid f() {\n"
10776 "\t\tsomeFunction(parameter1,\n"
10777 "\t\t\t parameter2);\n"
10778 "\t}\n"
10779 "};",
10780 Tab);
10781 verifyFormat("#define A \\\n"
10782 "\tvoid f() { \\\n"
10783 "\t\tsomeFunction( \\\n"
10784 "\t\t parameter1, \\\n"
10785 "\t\t parameter2); \\\n"
10786 "\t}",
10787 Tab);
10788 Tab.TabWidth = 4;
10789 Tab.IndentWidth = 8;
10790 verifyFormat("class TabWidth4Indent8 {\n"
10791 "\t\tvoid f() {\n"
10792 "\t\t\t\tsomeFunction(parameter1,\n"
10793 "\t\t\t\t\t\t\t parameter2);\n"
10794 "\t\t}\n"
10795 "};",
10796 Tab);
10797 Tab.TabWidth = 4;
10798 Tab.IndentWidth = 4;
10799 verifyFormat("class TabWidth4Indent4 {\n"
10800 "\tvoid f() {\n"
10801 "\t\tsomeFunction(parameter1,\n"
10802 "\t\t\t\t\t parameter2);\n"
10803 "\t}\n"
10804 "};",
10805 Tab);
10806 Tab.TabWidth = 8;
10807 Tab.IndentWidth = 4;
10808 verifyFormat("class TabWidth8Indent4 {\n"
10809 " void f() {\n"
10810 "\tsomeFunction(parameter1,\n"
10811 "\t\t parameter2);\n"
10812 " }\n"
10813 "};",
10814 Tab);
10815 Tab.TabWidth = 8;
10816 Tab.IndentWidth = 8;
10817 EXPECT_EQ("/*\n"
10818 "\t a\t\tcomment\n"
10819 "\t in multiple lines\n"
10820 " */",
10821 format(" /*\t \t \n"
10822 " \t \t a\t\tcomment\t \t\n"
10823 " \t \t in multiple lines\t\n"
10824 " \t */",
10825 Tab));
10826 verifyFormat("{\n"
10827 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10828 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10829 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10830 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10831 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10832 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
10833 "};",
10834 Tab);
10835 verifyFormat("enum AA {\n"
10836 "\ta1, // Force multiple lines\n"
10837 "\ta2,\n"
10838 "\ta3\n"
10839 "};",
10840 Tab);
10841 EXPECT_EQ("if (aaaaaaaa && // q\n"
10842 " bb) // w\n"
10843 "\t;",
10844 format("if (aaaaaaaa &&// q\n"
10845 "bb)// w\n"
10846 ";",
10847 Tab));
10848 verifyFormat("class X {\n"
10849 "\tvoid f() {\n"
10850 "\t\tsomeFunction(parameter1,\n"
10851 "\t\t\t parameter2);\n"
10852 "\t}\n"
10853 "};",
10854 Tab);
10855 verifyFormat("{\n"
10856 "\tQ(\n"
10857 "\t {\n"
10858 "\t\t int a;\n"
10859 "\t\t someFunction(aaaaaaaa,\n"
10860 "\t\t\t\t bbbbbbb);\n"
10861 "\t },\n"
10862 "\t p);\n"
10863 "}",
10864 Tab);
10865 EXPECT_EQ("{\n"
10866 "\t/* aaaa\n"
10867 "\t bbbb */\n"
10868 "}",
10869 format("{\n"
10870 "/* aaaa\n"
10871 " bbbb */\n"
10872 "}",
10873 Tab));
10874 EXPECT_EQ("{\n"
10875 "\t/*\n"
10876 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10877 "\t bbbbbbbbbbbbb\n"
10878 "\t*/\n"
10879 "}",
10880 format("{\n"
10881 "/*\n"
10882 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10883 "*/\n"
10884 "}",
10885 Tab));
10886 EXPECT_EQ("{\n"
10887 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10888 "\t// bbbbbbbbbbbbb\n"
10889 "}",
10890 format("{\n"
10891 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10892 "}",
10893 Tab));
10894 EXPECT_EQ("{\n"
10895 "\t/*\n"
10896 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10897 "\t bbbbbbbbbbbbb\n"
10898 "\t*/\n"
10899 "}",
10900 format("{\n"
10901 "\t/*\n"
10902 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10903 "\t*/\n"
10904 "}",
10905 Tab));
10906 EXPECT_EQ("{\n"
10907 "\t/*\n"
10908 "\n"
10909 "\t*/\n"
10910 "}",
10911 format("{\n"
10912 "\t/*\n"
10913 "\n"
10914 "\t*/\n"
10915 "}",
10916 Tab));
10917 EXPECT_EQ("{\n"
10918 "\t/*\n"
10919 " asdf\n"
10920 "\t*/\n"
10921 "}",
10922 format("{\n"
10923 "\t/*\n"
10924 " asdf\n"
10925 "\t*/\n"
10926 "}",
10927 Tab));
10928 EXPECT_EQ("/* some\n"
10929 " comment */",
10930 format(" \t \t /* some\n"
10931 " \t \t comment */",
10932 Tab));
10933 EXPECT_EQ("int a; /* some\n"
10934 " comment */",
10935 format(" \t \t int a; /* some\n"
10936 " \t \t comment */",
10937 Tab));
10938 EXPECT_EQ("int a; /* some\n"
10939 "comment */",
10940 format(" \t \t int\ta; /* some\n"
10941 " \t \t comment */",
10942 Tab));
10943 EXPECT_EQ("f(\"\t\t\"); /* some\n"
10944 " comment */",
10945 format(" \t \t f(\"\t\t\"); /* some\n"
10946 " \t \t comment */",
10947 Tab));
10948 EXPECT_EQ("{\n"
10949 "\t/*\n"
10950 "\t * Comment\n"
10951 "\t */\n"
10952 "\tint i;\n"
10953 "}",
10954 format("{\n"
10955 "\t/*\n"
10956 "\t * Comment\n"
10957 "\t */\n"
10958 "\t int i;\n"
10959 "}",
10960 Tab));
10961 Tab.TabWidth = 2;
10962 Tab.IndentWidth = 2;
10963 EXPECT_EQ("{\n"
10964 "\t/* aaaa\n"
10965 "\t\t bbbb */\n"
10966 "}",
10967 format("{\n"
10968 "/* aaaa\n"
10969 "\t bbbb */\n"
10970 "}",
10971 Tab));
10972 EXPECT_EQ("{\n"
10973 "\t/*\n"
10974 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10975 "\t\tbbbbbbbbbbbbb\n"
10976 "\t*/\n"
10977 "}",
10978 format("{\n"
10979 "/*\n"
10980 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
10981 "*/\n"
10982 "}",
10983 Tab));
10984 Tab.AlignConsecutiveAssignments = true;
10985 Tab.AlignConsecutiveDeclarations = true;
10986 Tab.TabWidth = 4;
10987 Tab.IndentWidth = 4;
10988 verifyFormat("class Assign {\n"
10989 "\tvoid f() {\n"
10990 "\t\tint x = 123;\n"
10991 "\t\tint random = 4;\n"
10992 "\t\tstd::string alphabet =\n"
10993 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
10994 "\t}\n"
10995 "};",
10996 Tab);
10997
10998 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
10999 Tab.TabWidth = 8;
11000 Tab.IndentWidth = 8;
11001 EXPECT_EQ("if (aaaaaaaa && // q\n"
11002 " bb) // w\n"
11003 "\t;",
11004 format("if (aaaaaaaa &&// q\n"
11005 "bb)// w\n"
11006 ";",
11007 Tab));
11008 EXPECT_EQ("if (aaa && bbb) // w\n"
11009 "\t;",
11010 format("if(aaa&&bbb)// w\n"
11011 ";",
11012 Tab));
11013 verifyFormat("class X {\n"
11014 "\tvoid f() {\n"
11015 "\t\tsomeFunction(parameter1,\n"
11016 "\t\t parameter2);\n"
11017 "\t}\n"
11018 "};",
11019 Tab);
11020 verifyFormat("#define A \\\n"
11021 "\tvoid f() { \\\n"
11022 "\t\tsomeFunction( \\\n"
11023 "\t\t parameter1, \\\n"
11024 "\t\t parameter2); \\\n"
11025 "\t}",
11026 Tab);
11027 Tab.TabWidth = 4;
11028 Tab.IndentWidth = 8;
11029 verifyFormat("class TabWidth4Indent8 {\n"
11030 "\t\tvoid f() {\n"
11031 "\t\t\t\tsomeFunction(parameter1,\n"
11032 "\t\t\t\t parameter2);\n"
11033 "\t\t}\n"
11034 "};",
11035 Tab);
11036 Tab.TabWidth = 4;
11037 Tab.IndentWidth = 4;
11038 verifyFormat("class TabWidth4Indent4 {\n"
11039 "\tvoid f() {\n"
11040 "\t\tsomeFunction(parameter1,\n"
11041 "\t\t parameter2);\n"
11042 "\t}\n"
11043 "};",
11044 Tab);
11045 Tab.TabWidth = 8;
11046 Tab.IndentWidth = 4;
11047 verifyFormat("class TabWidth8Indent4 {\n"
11048 " void f() {\n"
11049 "\tsomeFunction(parameter1,\n"
11050 "\t parameter2);\n"
11051 " }\n"
11052 "};",
11053 Tab);
11054 Tab.TabWidth = 8;
11055 Tab.IndentWidth = 8;
11056 EXPECT_EQ("/*\n"
11057 " a\t\tcomment\n"
11058 " in multiple lines\n"
11059 " */",
11060 format(" /*\t \t \n"
11061 " \t \t a\t\tcomment\t \t\n"
11062 " \t \t in multiple lines\t\n"
11063 " \t */",
11064 Tab));
11065 verifyFormat("{\n"
11066 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
11067 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
11068 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
11069 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
11070 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
11071 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
11072 "};",
11073 Tab);
11074 verifyFormat("enum AA {\n"
11075 "\ta1, // Force multiple lines\n"
11076 "\ta2,\n"
11077 "\ta3\n"
11078 "};",
11079 Tab);
11080 EXPECT_EQ("if (aaaaaaaa && // q\n"
11081 " bb) // w\n"
11082 "\t;",
11083 format("if (aaaaaaaa &&// q\n"
11084 "bb)// w\n"
11085 ";",
11086 Tab));
11087 verifyFormat("class X {\n"
11088 "\tvoid f() {\n"
11089 "\t\tsomeFunction(parameter1,\n"
11090 "\t\t parameter2);\n"
11091 "\t}\n"
11092 "};",
11093 Tab);
11094 verifyFormat("{\n"
11095 "\tQ(\n"
11096 "\t {\n"
11097 "\t\t int a;\n"
11098 "\t\t someFunction(aaaaaaaa,\n"
11099 "\t\t bbbbbbb);\n"
11100 "\t },\n"
11101 "\t p);\n"
11102 "}",
11103 Tab);
11104 EXPECT_EQ("{\n"
11105 "\t/* aaaa\n"
11106 "\t bbbb */\n"
11107 "}",
11108 format("{\n"
11109 "/* aaaa\n"
11110 " bbbb */\n"
11111 "}",
11112 Tab));
11113 EXPECT_EQ("{\n"
11114 "\t/*\n"
11115 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11116 "\t bbbbbbbbbbbbb\n"
11117 "\t*/\n"
11118 "}",
11119 format("{\n"
11120 "/*\n"
11121 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
11122 "*/\n"
11123 "}",
11124 Tab));
11125 EXPECT_EQ("{\n"
11126 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11127 "\t// bbbbbbbbbbbbb\n"
11128 "}",
11129 format("{\n"
11130 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
11131 "}",
11132 Tab));
11133 EXPECT_EQ("{\n"
11134 "\t/*\n"
11135 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11136 "\t bbbbbbbbbbbbb\n"
11137 "\t*/\n"
11138 "}",
11139 format("{\n"
11140 "\t/*\n"
11141 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
11142 "\t*/\n"
11143 "}",
11144 Tab));
11145 EXPECT_EQ("{\n"
11146 "\t/*\n"
11147 "\n"
11148 "\t*/\n"
11149 "}",
11150 format("{\n"
11151 "\t/*\n"
11152 "\n"
11153 "\t*/\n"
11154 "}",
11155 Tab));
11156 EXPECT_EQ("{\n"
11157 "\t/*\n"
11158 " asdf\n"
11159 "\t*/\n"
11160 "}",
11161 format("{\n"
11162 "\t/*\n"
11163 " asdf\n"
11164 "\t*/\n"
11165 "}",
11166 Tab));
11167 EXPECT_EQ("/* some\n"
11168 " comment */",
11169 format(" \t \t /* some\n"
11170 " \t \t comment */",
11171 Tab));
11172 EXPECT_EQ("int a; /* some\n"
11173 " comment */",
11174 format(" \t \t int a; /* some\n"
11175 " \t \t comment */",
11176 Tab));
11177 EXPECT_EQ("int a; /* some\n"
11178 "comment */",
11179 format(" \t \t int\ta; /* some\n"
11180 " \t \t comment */",
11181 Tab));
11182 EXPECT_EQ("f(\"\t\t\"); /* some\n"
11183 " comment */",
11184 format(" \t \t f(\"\t\t\"); /* some\n"
11185 " \t \t comment */",
11186 Tab));
11187 EXPECT_EQ("{\n"
11188 "\t/*\n"
11189 "\t * Comment\n"
11190 "\t */\n"
11191 "\tint i;\n"
11192 "}",
11193 format("{\n"
11194 "\t/*\n"
11195 "\t * Comment\n"
11196 "\t */\n"
11197 "\t int i;\n"
11198 "}",
11199 Tab));
11200 Tab.TabWidth = 2;
11201 Tab.IndentWidth = 2;
11202 EXPECT_EQ("{\n"
11203 "\t/* aaaa\n"
11204 "\t bbbb */\n"
11205 "}",
11206 format("{\n"
11207 "/* aaaa\n"
11208 " bbbb */\n"
11209 "}",
11210 Tab));
11211 EXPECT_EQ("{\n"
11212 "\t/*\n"
11213 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11214 "\t bbbbbbbbbbbbb\n"
11215 "\t*/\n"
11216 "}",
11217 format("{\n"
11218 "/*\n"
11219 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
11220 "*/\n"
11221 "}",
11222 Tab));
11223 Tab.AlignConsecutiveAssignments = true;
11224 Tab.AlignConsecutiveDeclarations = true;
11225 Tab.TabWidth = 4;
11226 Tab.IndentWidth = 4;
11227 verifyFormat("class Assign {\n"
11228 "\tvoid f() {\n"
11229 "\t\tint x = 123;\n"
11230 "\t\tint random = 4;\n"
11231 "\t\tstd::string alphabet =\n"
11232 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
11233 "\t}\n"
11234 "};",
11235 Tab);
11236 }
11237
TEST_F(FormatTest,ZeroTabWidth)11238 TEST_F(FormatTest, ZeroTabWidth) {
11239 FormatStyle Tab = getLLVMStyleWithColumns(42);
11240 Tab.IndentWidth = 8;
11241 Tab.UseTab = FormatStyle::UT_Never;
11242 Tab.TabWidth = 0;
11243 EXPECT_EQ("void a(){\n"
11244 " // line starts with '\t'\n"
11245 "};",
11246 format("void a(){\n"
11247 "\t// line starts with '\t'\n"
11248 "};",
11249 Tab));
11250
11251 EXPECT_EQ("void a(){\n"
11252 " // line starts with '\t'\n"
11253 "};",
11254 format("void a(){\n"
11255 "\t\t// line starts with '\t'\n"
11256 "};",
11257 Tab));
11258
11259 Tab.UseTab = FormatStyle::UT_ForIndentation;
11260 EXPECT_EQ("void a(){\n"
11261 " // line starts with '\t'\n"
11262 "};",
11263 format("void a(){\n"
11264 "\t// line starts with '\t'\n"
11265 "};",
11266 Tab));
11267
11268 EXPECT_EQ("void a(){\n"
11269 " // line starts with '\t'\n"
11270 "};",
11271 format("void a(){\n"
11272 "\t\t// line starts with '\t'\n"
11273 "};",
11274 Tab));
11275
11276 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
11277 EXPECT_EQ("void a(){\n"
11278 " // line starts with '\t'\n"
11279 "};",
11280 format("void a(){\n"
11281 "\t// line starts with '\t'\n"
11282 "};",
11283 Tab));
11284
11285 EXPECT_EQ("void a(){\n"
11286 " // line starts with '\t'\n"
11287 "};",
11288 format("void a(){\n"
11289 "\t\t// line starts with '\t'\n"
11290 "};",
11291 Tab));
11292
11293 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
11294 EXPECT_EQ("void a(){\n"
11295 " // line starts with '\t'\n"
11296 "};",
11297 format("void a(){\n"
11298 "\t// line starts with '\t'\n"
11299 "};",
11300 Tab));
11301
11302 EXPECT_EQ("void a(){\n"
11303 " // line starts with '\t'\n"
11304 "};",
11305 format("void a(){\n"
11306 "\t\t// line starts with '\t'\n"
11307 "};",
11308 Tab));
11309
11310 Tab.UseTab = FormatStyle::UT_Always;
11311 EXPECT_EQ("void a(){\n"
11312 "// line starts with '\t'\n"
11313 "};",
11314 format("void a(){\n"
11315 "\t// line starts with '\t'\n"
11316 "};",
11317 Tab));
11318
11319 EXPECT_EQ("void a(){\n"
11320 "// line starts with '\t'\n"
11321 "};",
11322 format("void a(){\n"
11323 "\t\t// line starts with '\t'\n"
11324 "};",
11325 Tab));
11326 }
11327
TEST_F(FormatTest,CalculatesOriginalColumn)11328 TEST_F(FormatTest, CalculatesOriginalColumn) {
11329 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
11330 "q\"; /* some\n"
11331 " comment */",
11332 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
11333 "q\"; /* some\n"
11334 " comment */",
11335 getLLVMStyle()));
11336 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
11337 "/* some\n"
11338 " comment */",
11339 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
11340 " /* some\n"
11341 " comment */",
11342 getLLVMStyle()));
11343 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
11344 "qqq\n"
11345 "/* some\n"
11346 " comment */",
11347 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
11348 "qqq\n"
11349 " /* some\n"
11350 " comment */",
11351 getLLVMStyle()));
11352 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
11353 "wwww; /* some\n"
11354 " comment */",
11355 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
11356 "wwww; /* some\n"
11357 " comment */",
11358 getLLVMStyle()));
11359 }
11360
TEST_F(FormatTest,ConfigurableSpaceBeforeParens)11361 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
11362 FormatStyle NoSpace = getLLVMStyle();
11363 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
11364
11365 verifyFormat("while(true)\n"
11366 " continue;",
11367 NoSpace);
11368 verifyFormat("for(;;)\n"
11369 " continue;",
11370 NoSpace);
11371 verifyFormat("if(true)\n"
11372 " f();\n"
11373 "else if(true)\n"
11374 " f();",
11375 NoSpace);
11376 verifyFormat("do {\n"
11377 " do_something();\n"
11378 "} while(something());",
11379 NoSpace);
11380 verifyFormat("switch(x) {\n"
11381 "default:\n"
11382 " break;\n"
11383 "}",
11384 NoSpace);
11385 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
11386 verifyFormat("size_t x = sizeof(x);", NoSpace);
11387 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
11388 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
11389 verifyFormat("alignas(128) char a[128];", NoSpace);
11390 verifyFormat("size_t x = alignof(MyType);", NoSpace);
11391 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
11392 verifyFormat("int f() throw(Deprecated);", NoSpace);
11393 verifyFormat("typedef void (*cb)(int);", NoSpace);
11394 verifyFormat("T A::operator()();", NoSpace);
11395 verifyFormat("X A::operator++(T);", NoSpace);
11396 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
11397
11398 FormatStyle Space = getLLVMStyle();
11399 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
11400
11401 verifyFormat("int f ();", Space);
11402 verifyFormat("void f (int a, T b) {\n"
11403 " while (true)\n"
11404 " continue;\n"
11405 "}",
11406 Space);
11407 verifyFormat("if (true)\n"
11408 " f ();\n"
11409 "else if (true)\n"
11410 " f ();",
11411 Space);
11412 verifyFormat("do {\n"
11413 " do_something ();\n"
11414 "} while (something ());",
11415 Space);
11416 verifyFormat("switch (x) {\n"
11417 "default:\n"
11418 " break;\n"
11419 "}",
11420 Space);
11421 verifyFormat("A::A () : a (1) {}", Space);
11422 verifyFormat("void f () __attribute__ ((asdf));", Space);
11423 verifyFormat("*(&a + 1);\n"
11424 "&((&a)[1]);\n"
11425 "a[(b + c) * d];\n"
11426 "(((a + 1) * 2) + 3) * 4;",
11427 Space);
11428 verifyFormat("#define A(x) x", Space);
11429 verifyFormat("#define A (x) x", Space);
11430 verifyFormat("#if defined(x)\n"
11431 "#endif",
11432 Space);
11433 verifyFormat("auto i = std::make_unique<int> (5);", Space);
11434 verifyFormat("size_t x = sizeof (x);", Space);
11435 verifyFormat("auto f (int x) -> decltype (x);", Space);
11436 verifyFormat("int f (T x) noexcept (x.create ());", Space);
11437 verifyFormat("alignas (128) char a[128];", Space);
11438 verifyFormat("size_t x = alignof (MyType);", Space);
11439 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
11440 verifyFormat("int f () throw (Deprecated);", Space);
11441 verifyFormat("typedef void (*cb) (int);", Space);
11442 verifyFormat("T A::operator() ();", Space);
11443 verifyFormat("X A::operator++ (T);", Space);
11444 verifyFormat("auto lambda = [] () { return 0; };", Space);
11445 verifyFormat("int x = int (y);", Space);
11446
11447 FormatStyle SomeSpace = getLLVMStyle();
11448 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
11449
11450 verifyFormat("[]() -> float {}", SomeSpace);
11451 verifyFormat("[] (auto foo) {}", SomeSpace);
11452 verifyFormat("[foo]() -> int {}", SomeSpace);
11453 verifyFormat("int f();", SomeSpace);
11454 verifyFormat("void f (int a, T b) {\n"
11455 " while (true)\n"
11456 " continue;\n"
11457 "}",
11458 SomeSpace);
11459 verifyFormat("if (true)\n"
11460 " f();\n"
11461 "else if (true)\n"
11462 " f();",
11463 SomeSpace);
11464 verifyFormat("do {\n"
11465 " do_something();\n"
11466 "} while (something());",
11467 SomeSpace);
11468 verifyFormat("switch (x) {\n"
11469 "default:\n"
11470 " break;\n"
11471 "}",
11472 SomeSpace);
11473 verifyFormat("A::A() : a (1) {}", SomeSpace);
11474 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
11475 verifyFormat("*(&a + 1);\n"
11476 "&((&a)[1]);\n"
11477 "a[(b + c) * d];\n"
11478 "(((a + 1) * 2) + 3) * 4;",
11479 SomeSpace);
11480 verifyFormat("#define A(x) x", SomeSpace);
11481 verifyFormat("#define A (x) x", SomeSpace);
11482 verifyFormat("#if defined(x)\n"
11483 "#endif",
11484 SomeSpace);
11485 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
11486 verifyFormat("size_t x = sizeof (x);", SomeSpace);
11487 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
11488 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
11489 verifyFormat("alignas (128) char a[128];", SomeSpace);
11490 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
11491 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
11492 SomeSpace);
11493 verifyFormat("int f() throw (Deprecated);", SomeSpace);
11494 verifyFormat("typedef void (*cb) (int);", SomeSpace);
11495 verifyFormat("T A::operator()();", SomeSpace);
11496 verifyFormat("X A::operator++ (T);", SomeSpace);
11497 verifyFormat("int x = int (y);", SomeSpace);
11498 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
11499 }
11500
TEST_F(FormatTest,SpaceAfterLogicalNot)11501 TEST_F(FormatTest, SpaceAfterLogicalNot) {
11502 FormatStyle Spaces = getLLVMStyle();
11503 Spaces.SpaceAfterLogicalNot = true;
11504
11505 verifyFormat("bool x = ! y", Spaces);
11506 verifyFormat("if (! isFailure())", Spaces);
11507 verifyFormat("if (! (a && b))", Spaces);
11508 verifyFormat("\"Error!\"", Spaces);
11509 verifyFormat("! ! x", Spaces);
11510 }
11511
TEST_F(FormatTest,ConfigurableSpacesInParentheses)11512 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
11513 FormatStyle Spaces = getLLVMStyle();
11514
11515 Spaces.SpacesInParentheses = true;
11516 verifyFormat("do_something( ::globalVar );", Spaces);
11517 verifyFormat("call( x, y, z );", Spaces);
11518 verifyFormat("call();", Spaces);
11519 verifyFormat("std::function<void( int, int )> callback;", Spaces);
11520 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
11521 Spaces);
11522 verifyFormat("while ( (bool)1 )\n"
11523 " continue;",
11524 Spaces);
11525 verifyFormat("for ( ;; )\n"
11526 " continue;",
11527 Spaces);
11528 verifyFormat("if ( true )\n"
11529 " f();\n"
11530 "else if ( true )\n"
11531 " f();",
11532 Spaces);
11533 verifyFormat("do {\n"
11534 " do_something( (int)i );\n"
11535 "} while ( something() );",
11536 Spaces);
11537 verifyFormat("switch ( x ) {\n"
11538 "default:\n"
11539 " break;\n"
11540 "}",
11541 Spaces);
11542
11543 Spaces.SpacesInParentheses = false;
11544 Spaces.SpacesInCStyleCastParentheses = true;
11545 verifyFormat("Type *A = ( Type * )P;", Spaces);
11546 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
11547 verifyFormat("x = ( int32 )y;", Spaces);
11548 verifyFormat("int a = ( int )(2.0f);", Spaces);
11549 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
11550 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
11551 verifyFormat("#define x (( int )-1)", Spaces);
11552
11553 // Run the first set of tests again with:
11554 Spaces.SpacesInParentheses = false;
11555 Spaces.SpaceInEmptyParentheses = true;
11556 Spaces.SpacesInCStyleCastParentheses = true;
11557 verifyFormat("call(x, y, z);", Spaces);
11558 verifyFormat("call( );", Spaces);
11559 verifyFormat("std::function<void(int, int)> callback;", Spaces);
11560 verifyFormat("while (( bool )1)\n"
11561 " continue;",
11562 Spaces);
11563 verifyFormat("for (;;)\n"
11564 " continue;",
11565 Spaces);
11566 verifyFormat("if (true)\n"
11567 " f( );\n"
11568 "else if (true)\n"
11569 " f( );",
11570 Spaces);
11571 verifyFormat("do {\n"
11572 " do_something(( int )i);\n"
11573 "} while (something( ));",
11574 Spaces);
11575 verifyFormat("switch (x) {\n"
11576 "default:\n"
11577 " break;\n"
11578 "}",
11579 Spaces);
11580
11581 // Run the first set of tests again with:
11582 Spaces.SpaceAfterCStyleCast = true;
11583 verifyFormat("call(x, y, z);", Spaces);
11584 verifyFormat("call( );", Spaces);
11585 verifyFormat("std::function<void(int, int)> callback;", Spaces);
11586 verifyFormat("while (( bool ) 1)\n"
11587 " continue;",
11588 Spaces);
11589 verifyFormat("for (;;)\n"
11590 " continue;",
11591 Spaces);
11592 verifyFormat("if (true)\n"
11593 " f( );\n"
11594 "else if (true)\n"
11595 " f( );",
11596 Spaces);
11597 verifyFormat("do {\n"
11598 " do_something(( int ) i);\n"
11599 "} while (something( ));",
11600 Spaces);
11601 verifyFormat("switch (x) {\n"
11602 "default:\n"
11603 " break;\n"
11604 "}",
11605 Spaces);
11606
11607 // Run subset of tests again with:
11608 Spaces.SpacesInCStyleCastParentheses = false;
11609 Spaces.SpaceAfterCStyleCast = true;
11610 verifyFormat("while ((bool) 1)\n"
11611 " continue;",
11612 Spaces);
11613 verifyFormat("do {\n"
11614 " do_something((int) i);\n"
11615 "} while (something( ));",
11616 Spaces);
11617 }
11618
TEST_F(FormatTest,ConfigurableSpacesInSquareBrackets)11619 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
11620 verifyFormat("int a[5];");
11621 verifyFormat("a[3] += 42;");
11622
11623 FormatStyle Spaces = getLLVMStyle();
11624 Spaces.SpacesInSquareBrackets = true;
11625 // Not lambdas.
11626 verifyFormat("int a[ 5 ];", Spaces);
11627 verifyFormat("a[ 3 ] += 42;", Spaces);
11628 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
11629 verifyFormat("double &operator[](int i) { return 0; }\n"
11630 "int i;",
11631 Spaces);
11632 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
11633 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
11634 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
11635 // Lambdas.
11636 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
11637 verifyFormat("return [ i, args... ] {};", Spaces);
11638 verifyFormat("int foo = [ &bar ]() {};", Spaces);
11639 verifyFormat("int foo = [ = ]() {};", Spaces);
11640 verifyFormat("int foo = [ & ]() {};", Spaces);
11641 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
11642 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
11643 }
11644
TEST_F(FormatTest,ConfigurableSpaceBeforeBrackets)11645 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
11646 FormatStyle NoSpaceStyle = getLLVMStyle();
11647 verifyFormat("int a[5];", NoSpaceStyle);
11648 verifyFormat("a[3] += 42;", NoSpaceStyle);
11649
11650 verifyFormat("int a[1];", NoSpaceStyle);
11651 verifyFormat("int 1 [a];", NoSpaceStyle);
11652 verifyFormat("int a[1][2];", NoSpaceStyle);
11653 verifyFormat("a[7] = 5;", NoSpaceStyle);
11654 verifyFormat("int a = (f())[23];", NoSpaceStyle);
11655 verifyFormat("f([] {})", NoSpaceStyle);
11656
11657 FormatStyle Space = getLLVMStyle();
11658 Space.SpaceBeforeSquareBrackets = true;
11659 verifyFormat("int c = []() -> int { return 2; }();\n", Space);
11660 verifyFormat("return [i, args...] {};", Space);
11661
11662 verifyFormat("int a [5];", Space);
11663 verifyFormat("a [3] += 42;", Space);
11664 verifyFormat("constexpr char hello []{\"hello\"};", Space);
11665 verifyFormat("double &operator[](int i) { return 0; }\n"
11666 "int i;",
11667 Space);
11668 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
11669 verifyFormat("int i = a [a][a]->f();", Space);
11670 verifyFormat("int i = (*b) [a]->f();", Space);
11671
11672 verifyFormat("int a [1];", Space);
11673 verifyFormat("int 1 [a];", Space);
11674 verifyFormat("int a [1][2];", Space);
11675 verifyFormat("a [7] = 5;", Space);
11676 verifyFormat("int a = (f()) [23];", Space);
11677 verifyFormat("f([] {})", Space);
11678 }
11679
TEST_F(FormatTest,ConfigurableSpaceBeforeAssignmentOperators)11680 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
11681 verifyFormat("int a = 5;");
11682 verifyFormat("a += 42;");
11683 verifyFormat("a or_eq 8;");
11684
11685 FormatStyle Spaces = getLLVMStyle();
11686 Spaces.SpaceBeforeAssignmentOperators = false;
11687 verifyFormat("int a= 5;", Spaces);
11688 verifyFormat("a+= 42;", Spaces);
11689 verifyFormat("a or_eq 8;", Spaces);
11690 }
11691
TEST_F(FormatTest,ConfigurableSpaceBeforeColon)11692 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
11693 verifyFormat("class Foo : public Bar {};");
11694 verifyFormat("Foo::Foo() : foo(1) {}");
11695 verifyFormat("for (auto a : b) {\n}");
11696 verifyFormat("int x = a ? b : c;");
11697 verifyFormat("{\n"
11698 "label0:\n"
11699 " int x = 0;\n"
11700 "}");
11701 verifyFormat("switch (x) {\n"
11702 "case 1:\n"
11703 "default:\n"
11704 "}");
11705
11706 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
11707 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
11708 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
11709 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
11710 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
11711 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
11712 verifyFormat("{\n"
11713 "label1:\n"
11714 " int x = 0;\n"
11715 "}",
11716 CtorInitializerStyle);
11717 verifyFormat("switch (x) {\n"
11718 "case 1:\n"
11719 "default:\n"
11720 "}",
11721 CtorInitializerStyle);
11722 CtorInitializerStyle.BreakConstructorInitializers =
11723 FormatStyle::BCIS_AfterColon;
11724 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
11725 " aaaaaaaaaaaaaaaa(1),\n"
11726 " bbbbbbbbbbbbbbbb(2) {}",
11727 CtorInitializerStyle);
11728 CtorInitializerStyle.BreakConstructorInitializers =
11729 FormatStyle::BCIS_BeforeComma;
11730 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
11731 " : aaaaaaaaaaaaaaaa(1)\n"
11732 " , bbbbbbbbbbbbbbbb(2) {}",
11733 CtorInitializerStyle);
11734 CtorInitializerStyle.BreakConstructorInitializers =
11735 FormatStyle::BCIS_BeforeColon;
11736 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
11737 " : aaaaaaaaaaaaaaaa(1),\n"
11738 " bbbbbbbbbbbbbbbb(2) {}",
11739 CtorInitializerStyle);
11740 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
11741 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
11742 ": aaaaaaaaaaaaaaaa(1),\n"
11743 " bbbbbbbbbbbbbbbb(2) {}",
11744 CtorInitializerStyle);
11745
11746 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
11747 InheritanceStyle.SpaceBeforeInheritanceColon = false;
11748 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
11749 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
11750 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
11751 verifyFormat("int x = a ? b : c;", InheritanceStyle);
11752 verifyFormat("{\n"
11753 "label2:\n"
11754 " int x = 0;\n"
11755 "}",
11756 InheritanceStyle);
11757 verifyFormat("switch (x) {\n"
11758 "case 1:\n"
11759 "default:\n"
11760 "}",
11761 InheritanceStyle);
11762 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
11763 verifyFormat("class Foooooooooooooooooooooo:\n"
11764 " public aaaaaaaaaaaaaaaaaa,\n"
11765 " public bbbbbbbbbbbbbbbbbb {\n"
11766 "}",
11767 InheritanceStyle);
11768 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
11769 verifyFormat("class Foooooooooooooooooooooo\n"
11770 " : public aaaaaaaaaaaaaaaaaa\n"
11771 " , public bbbbbbbbbbbbbbbbbb {\n"
11772 "}",
11773 InheritanceStyle);
11774 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
11775 verifyFormat("class Foooooooooooooooooooooo\n"
11776 " : public aaaaaaaaaaaaaaaaaa,\n"
11777 " public bbbbbbbbbbbbbbbbbb {\n"
11778 "}",
11779 InheritanceStyle);
11780 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
11781 verifyFormat("class Foooooooooooooooooooooo\n"
11782 ": public aaaaaaaaaaaaaaaaaa,\n"
11783 " public bbbbbbbbbbbbbbbbbb {}",
11784 InheritanceStyle);
11785
11786 FormatStyle ForLoopStyle = getLLVMStyle();
11787 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
11788 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
11789 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
11790 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
11791 verifyFormat("int x = a ? b : c;", ForLoopStyle);
11792 verifyFormat("{\n"
11793 "label2:\n"
11794 " int x = 0;\n"
11795 "}",
11796 ForLoopStyle);
11797 verifyFormat("switch (x) {\n"
11798 "case 1:\n"
11799 "default:\n"
11800 "}",
11801 ForLoopStyle);
11802
11803 FormatStyle NoSpaceStyle = getLLVMStyle();
11804 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
11805 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
11806 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
11807 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
11808 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
11809 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
11810 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
11811 verifyFormat("{\n"
11812 "label3:\n"
11813 " int x = 0;\n"
11814 "}",
11815 NoSpaceStyle);
11816 verifyFormat("switch (x) {\n"
11817 "case 1:\n"
11818 "default:\n"
11819 "}",
11820 NoSpaceStyle);
11821 }
11822
TEST_F(FormatTest,AlignConsecutiveMacros)11823 TEST_F(FormatTest, AlignConsecutiveMacros) {
11824 FormatStyle Style = getLLVMStyle();
11825 Style.AlignConsecutiveAssignments = true;
11826 Style.AlignConsecutiveDeclarations = true;
11827 Style.AlignConsecutiveMacros = false;
11828
11829 verifyFormat("#define a 3\n"
11830 "#define bbbb 4\n"
11831 "#define ccc (5)",
11832 Style);
11833
11834 verifyFormat("#define f(x) (x * x)\n"
11835 "#define fff(x, y, z) (x * y + z)\n"
11836 "#define ffff(x, y) (x - y)",
11837 Style);
11838
11839 verifyFormat("#define foo(x, y) (x + y)\n"
11840 "#define bar (5, 6)(2 + 2)",
11841 Style);
11842
11843 verifyFormat("#define a 3\n"
11844 "#define bbbb 4\n"
11845 "#define ccc (5)\n"
11846 "#define f(x) (x * x)\n"
11847 "#define fff(x, y, z) (x * y + z)\n"
11848 "#define ffff(x, y) (x - y)",
11849 Style);
11850
11851 Style.AlignConsecutiveMacros = true;
11852 verifyFormat("#define a 3\n"
11853 "#define bbbb 4\n"
11854 "#define ccc (5)",
11855 Style);
11856
11857 verifyFormat("#define f(x) (x * x)\n"
11858 "#define fff(x, y, z) (x * y + z)\n"
11859 "#define ffff(x, y) (x - y)",
11860 Style);
11861
11862 verifyFormat("#define foo(x, y) (x + y)\n"
11863 "#define bar (5, 6)(2 + 2)",
11864 Style);
11865
11866 verifyFormat("#define a 3\n"
11867 "#define bbbb 4\n"
11868 "#define ccc (5)\n"
11869 "#define f(x) (x * x)\n"
11870 "#define fff(x, y, z) (x * y + z)\n"
11871 "#define ffff(x, y) (x - y)",
11872 Style);
11873
11874 verifyFormat("#define a 5\n"
11875 "#define foo(x, y) (x + y)\n"
11876 "#define CCC (6)\n"
11877 "auto lambda = []() {\n"
11878 " auto ii = 0;\n"
11879 " float j = 0;\n"
11880 " return 0;\n"
11881 "};\n"
11882 "int i = 0;\n"
11883 "float i2 = 0;\n"
11884 "auto v = type{\n"
11885 " i = 1, //\n"
11886 " (i = 2), //\n"
11887 " i = 3 //\n"
11888 "};",
11889 Style);
11890
11891 Style.AlignConsecutiveMacros = false;
11892 Style.ColumnLimit = 20;
11893
11894 verifyFormat("#define a \\\n"
11895 " \"aabbbbbbbbbbbb\"\n"
11896 "#define D \\\n"
11897 " \"aabbbbbbbbbbbb\" \\\n"
11898 " \"ccddeeeeeeeee\"\n"
11899 "#define B \\\n"
11900 " \"QQQQQQQQQQQQQ\" \\\n"
11901 " \"FFFFFFFFFFFFF\" \\\n"
11902 " \"LLLLLLLL\"\n",
11903 Style);
11904
11905 Style.AlignConsecutiveMacros = true;
11906 verifyFormat("#define a \\\n"
11907 " \"aabbbbbbbbbbbb\"\n"
11908 "#define D \\\n"
11909 " \"aabbbbbbbbbbbb\" \\\n"
11910 " \"ccddeeeeeeeee\"\n"
11911 "#define B \\\n"
11912 " \"QQQQQQQQQQQQQ\" \\\n"
11913 " \"FFFFFFFFFFFFF\" \\\n"
11914 " \"LLLLLLLL\"\n",
11915 Style);
11916 }
11917
TEST_F(FormatTest,AlignConsecutiveAssignments)11918 TEST_F(FormatTest, AlignConsecutiveAssignments) {
11919 FormatStyle Alignment = getLLVMStyle();
11920 Alignment.AlignConsecutiveMacros = true;
11921 Alignment.AlignConsecutiveAssignments = false;
11922 verifyFormat("int a = 5;\n"
11923 "int oneTwoThree = 123;",
11924 Alignment);
11925 verifyFormat("int a = 5;\n"
11926 "int oneTwoThree = 123;",
11927 Alignment);
11928
11929 Alignment.AlignConsecutiveAssignments = true;
11930 verifyFormat("int a = 5;\n"
11931 "int oneTwoThree = 123;",
11932 Alignment);
11933 verifyFormat("int a = method();\n"
11934 "int oneTwoThree = 133;",
11935 Alignment);
11936 verifyFormat("a &= 5;\n"
11937 "bcd *= 5;\n"
11938 "ghtyf += 5;\n"
11939 "dvfvdb -= 5;\n"
11940 "a /= 5;\n"
11941 "vdsvsv %= 5;\n"
11942 "sfdbddfbdfbb ^= 5;\n"
11943 "dvsdsv |= 5;\n"
11944 "int dsvvdvsdvvv = 123;",
11945 Alignment);
11946 verifyFormat("int i = 1, j = 10;\n"
11947 "something = 2000;",
11948 Alignment);
11949 verifyFormat("something = 2000;\n"
11950 "int i = 1, j = 10;\n",
11951 Alignment);
11952 verifyFormat("something = 2000;\n"
11953 "another = 911;\n"
11954 "int i = 1, j = 10;\n"
11955 "oneMore = 1;\n"
11956 "i = 2;",
11957 Alignment);
11958 verifyFormat("int a = 5;\n"
11959 "int one = 1;\n"
11960 "method();\n"
11961 "int oneTwoThree = 123;\n"
11962 "int oneTwo = 12;",
11963 Alignment);
11964 verifyFormat("int oneTwoThree = 123;\n"
11965 "int oneTwo = 12;\n"
11966 "method();\n",
11967 Alignment);
11968 verifyFormat("int oneTwoThree = 123; // comment\n"
11969 "int oneTwo = 12; // comment",
11970 Alignment);
11971 EXPECT_EQ("int a = 5;\n"
11972 "\n"
11973 "int oneTwoThree = 123;",
11974 format("int a = 5;\n"
11975 "\n"
11976 "int oneTwoThree= 123;",
11977 Alignment));
11978 EXPECT_EQ("int a = 5;\n"
11979 "int one = 1;\n"
11980 "\n"
11981 "int oneTwoThree = 123;",
11982 format("int a = 5;\n"
11983 "int one = 1;\n"
11984 "\n"
11985 "int oneTwoThree = 123;",
11986 Alignment));
11987 EXPECT_EQ("int a = 5;\n"
11988 "int one = 1;\n"
11989 "\n"
11990 "int oneTwoThree = 123;\n"
11991 "int oneTwo = 12;",
11992 format("int a = 5;\n"
11993 "int one = 1;\n"
11994 "\n"
11995 "int oneTwoThree = 123;\n"
11996 "int oneTwo = 12;",
11997 Alignment));
11998 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
11999 verifyFormat("#define A \\\n"
12000 " int aaaa = 12; \\\n"
12001 " int b = 23; \\\n"
12002 " int ccc = 234; \\\n"
12003 " int dddddddddd = 2345;",
12004 Alignment);
12005 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12006 verifyFormat("#define A \\\n"
12007 " int aaaa = 12; \\\n"
12008 " int b = 23; \\\n"
12009 " int ccc = 234; \\\n"
12010 " int dddddddddd = 2345;",
12011 Alignment);
12012 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
12013 verifyFormat("#define A "
12014 " \\\n"
12015 " int aaaa = 12; "
12016 " \\\n"
12017 " int b = 23; "
12018 " \\\n"
12019 " int ccc = 234; "
12020 " \\\n"
12021 " int dddddddddd = 2345;",
12022 Alignment);
12023 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
12024 "k = 4, int l = 5,\n"
12025 " int m = 6) {\n"
12026 " int j = 10;\n"
12027 " otherThing = 1;\n"
12028 "}",
12029 Alignment);
12030 verifyFormat("void SomeFunction(int parameter = 0) {\n"
12031 " int i = 1;\n"
12032 " int j = 2;\n"
12033 " int big = 10000;\n"
12034 "}",
12035 Alignment);
12036 verifyFormat("class C {\n"
12037 "public:\n"
12038 " int i = 1;\n"
12039 " virtual void f() = 0;\n"
12040 "};",
12041 Alignment);
12042 verifyFormat("int i = 1;\n"
12043 "if (SomeType t = getSomething()) {\n"
12044 "}\n"
12045 "int j = 2;\n"
12046 "int big = 10000;",
12047 Alignment);
12048 verifyFormat("int j = 7;\n"
12049 "for (int k = 0; k < N; ++k) {\n"
12050 "}\n"
12051 "int j = 2;\n"
12052 "int big = 10000;\n"
12053 "}",
12054 Alignment);
12055 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
12056 verifyFormat("int i = 1;\n"
12057 "LooooooooooongType loooooooooooooooooooooongVariable\n"
12058 " = someLooooooooooooooooongFunction();\n"
12059 "int j = 2;",
12060 Alignment);
12061 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
12062 verifyFormat("int i = 1;\n"
12063 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
12064 " someLooooooooooooooooongFunction();\n"
12065 "int j = 2;",
12066 Alignment);
12067
12068 verifyFormat("auto lambda = []() {\n"
12069 " auto i = 0;\n"
12070 " return 0;\n"
12071 "};\n"
12072 "int i = 0;\n"
12073 "auto v = type{\n"
12074 " i = 1, //\n"
12075 " (i = 2), //\n"
12076 " i = 3 //\n"
12077 "};",
12078 Alignment);
12079
12080 verifyFormat(
12081 "int i = 1;\n"
12082 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
12083 " loooooooooooooooooooooongParameterB);\n"
12084 "int j = 2;",
12085 Alignment);
12086
12087 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
12088 " typename B = very_long_type_name_1,\n"
12089 " typename T_2 = very_long_type_name_2>\n"
12090 "auto foo() {}\n",
12091 Alignment);
12092 verifyFormat("int a, b = 1;\n"
12093 "int c = 2;\n"
12094 "int dd = 3;\n",
12095 Alignment);
12096 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
12097 "float b[1][] = {{3.f}};\n",
12098 Alignment);
12099 verifyFormat("for (int i = 0; i < 1; i++)\n"
12100 " int x = 1;\n",
12101 Alignment);
12102 verifyFormat("for (i = 0; i < 1; i++)\n"
12103 " x = 1;\n"
12104 "y = 1;\n",
12105 Alignment);
12106
12107 Alignment.ReflowComments = true;
12108 Alignment.ColumnLimit = 50;
12109 EXPECT_EQ("int x = 0;\n"
12110 "int yy = 1; /// specificlennospace\n"
12111 "int zzz = 2;\n",
12112 format("int x = 0;\n"
12113 "int yy = 1; ///specificlennospace\n"
12114 "int zzz = 2;\n",
12115 Alignment));
12116 }
12117
TEST_F(FormatTest,AlignConsecutiveBitFields)12118 TEST_F(FormatTest, AlignConsecutiveBitFields) {
12119 FormatStyle Alignment = getLLVMStyle();
12120 Alignment.AlignConsecutiveBitFields = true;
12121 verifyFormat("int const a : 5;\n"
12122 "int oneTwoThree : 23;",
12123 Alignment);
12124
12125 // Initializers are allowed starting with c++2a
12126 verifyFormat("int const a : 5 = 1;\n"
12127 "int oneTwoThree : 23 = 0;",
12128 Alignment);
12129
12130 Alignment.AlignConsecutiveDeclarations = true;
12131 verifyFormat("int const a : 5;\n"
12132 "int oneTwoThree : 23;",
12133 Alignment);
12134
12135 verifyFormat("int const a : 5; // comment\n"
12136 "int oneTwoThree : 23; // comment",
12137 Alignment);
12138
12139 verifyFormat("int const a : 5 = 1;\n"
12140 "int oneTwoThree : 23 = 0;",
12141 Alignment);
12142
12143 Alignment.AlignConsecutiveAssignments = true;
12144 verifyFormat("int const a : 5 = 1;\n"
12145 "int oneTwoThree : 23 = 0;",
12146 Alignment);
12147 verifyFormat("int const a : 5 = {1};\n"
12148 "int oneTwoThree : 23 = 0;",
12149 Alignment);
12150
12151 // Known limitations: ':' is only recognized as a bitfield colon when
12152 // followed by a number.
12153 /*
12154 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
12155 "int a : 5;",
12156 Alignment);
12157 */
12158 }
12159
TEST_F(FormatTest,AlignConsecutiveDeclarations)12160 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
12161 FormatStyle Alignment = getLLVMStyle();
12162 Alignment.AlignConsecutiveMacros = true;
12163 Alignment.AlignConsecutiveDeclarations = false;
12164 verifyFormat("float const a = 5;\n"
12165 "int oneTwoThree = 123;",
12166 Alignment);
12167 verifyFormat("int a = 5;\n"
12168 "float const oneTwoThree = 123;",
12169 Alignment);
12170
12171 Alignment.AlignConsecutiveDeclarations = true;
12172 verifyFormat("float const a = 5;\n"
12173 "int oneTwoThree = 123;",
12174 Alignment);
12175 verifyFormat("int a = method();\n"
12176 "float const oneTwoThree = 133;",
12177 Alignment);
12178 verifyFormat("int i = 1, j = 10;\n"
12179 "something = 2000;",
12180 Alignment);
12181 verifyFormat("something = 2000;\n"
12182 "int i = 1, j = 10;\n",
12183 Alignment);
12184 verifyFormat("float something = 2000;\n"
12185 "double another = 911;\n"
12186 "int i = 1, j = 10;\n"
12187 "const int *oneMore = 1;\n"
12188 "unsigned i = 2;",
12189 Alignment);
12190 verifyFormat("float a = 5;\n"
12191 "int one = 1;\n"
12192 "method();\n"
12193 "const double oneTwoThree = 123;\n"
12194 "const unsigned int oneTwo = 12;",
12195 Alignment);
12196 verifyFormat("int oneTwoThree{0}; // comment\n"
12197 "unsigned oneTwo; // comment",
12198 Alignment);
12199 EXPECT_EQ("float const a = 5;\n"
12200 "\n"
12201 "int oneTwoThree = 123;",
12202 format("float const a = 5;\n"
12203 "\n"
12204 "int oneTwoThree= 123;",
12205 Alignment));
12206 EXPECT_EQ("float a = 5;\n"
12207 "int one = 1;\n"
12208 "\n"
12209 "unsigned oneTwoThree = 123;",
12210 format("float a = 5;\n"
12211 "int one = 1;\n"
12212 "\n"
12213 "unsigned oneTwoThree = 123;",
12214 Alignment));
12215 EXPECT_EQ("float a = 5;\n"
12216 "int one = 1;\n"
12217 "\n"
12218 "unsigned oneTwoThree = 123;\n"
12219 "int oneTwo = 12;",
12220 format("float a = 5;\n"
12221 "int one = 1;\n"
12222 "\n"
12223 "unsigned oneTwoThree = 123;\n"
12224 "int oneTwo = 12;",
12225 Alignment));
12226 // Function prototype alignment
12227 verifyFormat("int a();\n"
12228 "double b();",
12229 Alignment);
12230 verifyFormat("int a(int x);\n"
12231 "double b();",
12232 Alignment);
12233 unsigned OldColumnLimit = Alignment.ColumnLimit;
12234 // We need to set ColumnLimit to zero, in order to stress nested alignments,
12235 // otherwise the function parameters will be re-flowed onto a single line.
12236 Alignment.ColumnLimit = 0;
12237 EXPECT_EQ("int a(int x,\n"
12238 " float y);\n"
12239 "double b(int x,\n"
12240 " double y);",
12241 format("int a(int x,\n"
12242 " float y);\n"
12243 "double b(int x,\n"
12244 " double y);",
12245 Alignment));
12246 // This ensures that function parameters of function declarations are
12247 // correctly indented when their owning functions are indented.
12248 // The failure case here is for 'double y' to not be indented enough.
12249 EXPECT_EQ("double a(int x);\n"
12250 "int b(int y,\n"
12251 " double z);",
12252 format("double a(int x);\n"
12253 "int b(int y,\n"
12254 " double z);",
12255 Alignment));
12256 // Set ColumnLimit low so that we induce wrapping immediately after
12257 // the function name and opening paren.
12258 Alignment.ColumnLimit = 13;
12259 verifyFormat("int function(\n"
12260 " int x,\n"
12261 " bool y);",
12262 Alignment);
12263 Alignment.ColumnLimit = OldColumnLimit;
12264 // Ensure function pointers don't screw up recursive alignment
12265 verifyFormat("int a(int x, void (*fp)(int y));\n"
12266 "double b();",
12267 Alignment);
12268 Alignment.AlignConsecutiveAssignments = true;
12269 // Ensure recursive alignment is broken by function braces, so that the
12270 // "a = 1" does not align with subsequent assignments inside the function
12271 // body.
12272 verifyFormat("int func(int a = 1) {\n"
12273 " int b = 2;\n"
12274 " int cc = 3;\n"
12275 "}",
12276 Alignment);
12277 verifyFormat("float something = 2000;\n"
12278 "double another = 911;\n"
12279 "int i = 1, j = 10;\n"
12280 "const int *oneMore = 1;\n"
12281 "unsigned i = 2;",
12282 Alignment);
12283 verifyFormat("int oneTwoThree = {0}; // comment\n"
12284 "unsigned oneTwo = 0; // comment",
12285 Alignment);
12286 // Make sure that scope is correctly tracked, in the absence of braces
12287 verifyFormat("for (int i = 0; i < n; i++)\n"
12288 " j = i;\n"
12289 "double x = 1;\n",
12290 Alignment);
12291 verifyFormat("if (int i = 0)\n"
12292 " j = i;\n"
12293 "double x = 1;\n",
12294 Alignment);
12295 // Ensure operator[] and operator() are comprehended
12296 verifyFormat("struct test {\n"
12297 " long long int foo();\n"
12298 " int operator[](int a);\n"
12299 " double bar();\n"
12300 "};\n",
12301 Alignment);
12302 verifyFormat("struct test {\n"
12303 " long long int foo();\n"
12304 " int operator()(int a);\n"
12305 " double bar();\n"
12306 "};\n",
12307 Alignment);
12308 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
12309 " int const i = 1;\n"
12310 " int * j = 2;\n"
12311 " int big = 10000;\n"
12312 "\n"
12313 " unsigned oneTwoThree = 123;\n"
12314 " int oneTwo = 12;\n"
12315 " method();\n"
12316 " float k = 2;\n"
12317 " int ll = 10000;\n"
12318 "}",
12319 format("void SomeFunction(int parameter= 0) {\n"
12320 " int const i= 1;\n"
12321 " int *j=2;\n"
12322 " int big = 10000;\n"
12323 "\n"
12324 "unsigned oneTwoThree =123;\n"
12325 "int oneTwo = 12;\n"
12326 " method();\n"
12327 "float k= 2;\n"
12328 "int ll=10000;\n"
12329 "}",
12330 Alignment));
12331 Alignment.AlignConsecutiveAssignments = false;
12332 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
12333 verifyFormat("#define A \\\n"
12334 " int aaaa = 12; \\\n"
12335 " float b = 23; \\\n"
12336 " const int ccc = 234; \\\n"
12337 " unsigned dddddddddd = 2345;",
12338 Alignment);
12339 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
12340 verifyFormat("#define A \\\n"
12341 " int aaaa = 12; \\\n"
12342 " float b = 23; \\\n"
12343 " const int ccc = 234; \\\n"
12344 " unsigned dddddddddd = 2345;",
12345 Alignment);
12346 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
12347 Alignment.ColumnLimit = 30;
12348 verifyFormat("#define A \\\n"
12349 " int aaaa = 12; \\\n"
12350 " float b = 23; \\\n"
12351 " const int ccc = 234; \\\n"
12352 " int dddddddddd = 2345;",
12353 Alignment);
12354 Alignment.ColumnLimit = 80;
12355 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
12356 "k = 4, int l = 5,\n"
12357 " int m = 6) {\n"
12358 " const int j = 10;\n"
12359 " otherThing = 1;\n"
12360 "}",
12361 Alignment);
12362 verifyFormat("void SomeFunction(int parameter = 0) {\n"
12363 " int const i = 1;\n"
12364 " int * j = 2;\n"
12365 " int big = 10000;\n"
12366 "}",
12367 Alignment);
12368 verifyFormat("class C {\n"
12369 "public:\n"
12370 " int i = 1;\n"
12371 " virtual void f() = 0;\n"
12372 "};",
12373 Alignment);
12374 verifyFormat("float i = 1;\n"
12375 "if (SomeType t = getSomething()) {\n"
12376 "}\n"
12377 "const unsigned j = 2;\n"
12378 "int big = 10000;",
12379 Alignment);
12380 verifyFormat("float j = 7;\n"
12381 "for (int k = 0; k < N; ++k) {\n"
12382 "}\n"
12383 "unsigned j = 2;\n"
12384 "int big = 10000;\n"
12385 "}",
12386 Alignment);
12387 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
12388 verifyFormat("float i = 1;\n"
12389 "LooooooooooongType loooooooooooooooooooooongVariable\n"
12390 " = someLooooooooooooooooongFunction();\n"
12391 "int j = 2;",
12392 Alignment);
12393 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
12394 verifyFormat("int i = 1;\n"
12395 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
12396 " someLooooooooooooooooongFunction();\n"
12397 "int j = 2;",
12398 Alignment);
12399
12400 Alignment.AlignConsecutiveAssignments = true;
12401 verifyFormat("auto lambda = []() {\n"
12402 " auto ii = 0;\n"
12403 " float j = 0;\n"
12404 " return 0;\n"
12405 "};\n"
12406 "int i = 0;\n"
12407 "float i2 = 0;\n"
12408 "auto v = type{\n"
12409 " i = 1, //\n"
12410 " (i = 2), //\n"
12411 " i = 3 //\n"
12412 "};",
12413 Alignment);
12414 Alignment.AlignConsecutiveAssignments = false;
12415
12416 verifyFormat(
12417 "int i = 1;\n"
12418 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
12419 " loooooooooooooooooooooongParameterB);\n"
12420 "int j = 2;",
12421 Alignment);
12422
12423 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
12424 // We expect declarations and assignments to align, as long as it doesn't
12425 // exceed the column limit, starting a new alignment sequence whenever it
12426 // happens.
12427 Alignment.AlignConsecutiveAssignments = true;
12428 Alignment.ColumnLimit = 30;
12429 verifyFormat("float ii = 1;\n"
12430 "unsigned j = 2;\n"
12431 "int someVerylongVariable = 1;\n"
12432 "AnotherLongType ll = 123456;\n"
12433 "VeryVeryLongType k = 2;\n"
12434 "int myvar = 1;",
12435 Alignment);
12436 Alignment.ColumnLimit = 80;
12437 Alignment.AlignConsecutiveAssignments = false;
12438
12439 verifyFormat(
12440 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
12441 " typename LongType, typename B>\n"
12442 "auto foo() {}\n",
12443 Alignment);
12444 verifyFormat("float a, b = 1;\n"
12445 "int c = 2;\n"
12446 "int dd = 3;\n",
12447 Alignment);
12448 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
12449 "float b[1][] = {{3.f}};\n",
12450 Alignment);
12451 Alignment.AlignConsecutiveAssignments = true;
12452 verifyFormat("float a, b = 1;\n"
12453 "int c = 2;\n"
12454 "int dd = 3;\n",
12455 Alignment);
12456 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
12457 "float b[1][] = {{3.f}};\n",
12458 Alignment);
12459 Alignment.AlignConsecutiveAssignments = false;
12460
12461 Alignment.ColumnLimit = 30;
12462 Alignment.BinPackParameters = false;
12463 verifyFormat("void foo(float a,\n"
12464 " float b,\n"
12465 " int c,\n"
12466 " uint32_t *d) {\n"
12467 " int * e = 0;\n"
12468 " float f = 0;\n"
12469 " double g = 0;\n"
12470 "}\n"
12471 "void bar(ino_t a,\n"
12472 " int b,\n"
12473 " uint32_t *c,\n"
12474 " bool d) {}\n",
12475 Alignment);
12476 Alignment.BinPackParameters = true;
12477 Alignment.ColumnLimit = 80;
12478
12479 // Bug 33507
12480 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
12481 verifyFormat(
12482 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
12483 " static const Version verVs2017;\n"
12484 " return true;\n"
12485 "});\n",
12486 Alignment);
12487 Alignment.PointerAlignment = FormatStyle::PAS_Right;
12488
12489 // See llvm.org/PR35641
12490 Alignment.AlignConsecutiveDeclarations = true;
12491 verifyFormat("int func() { //\n"
12492 " int b;\n"
12493 " unsigned c;\n"
12494 "}",
12495 Alignment);
12496
12497 // See PR37175
12498 FormatStyle Style = getMozillaStyle();
12499 Style.AlignConsecutiveDeclarations = true;
12500 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
12501 "foo(int a);",
12502 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style));
12503 }
12504
TEST_F(FormatTest,LinuxBraceBreaking)12505 TEST_F(FormatTest, LinuxBraceBreaking) {
12506 FormatStyle LinuxBraceStyle = getLLVMStyle();
12507 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
12508 verifyFormat("namespace a\n"
12509 "{\n"
12510 "class A\n"
12511 "{\n"
12512 " void f()\n"
12513 " {\n"
12514 " if (true) {\n"
12515 " a();\n"
12516 " b();\n"
12517 " } else {\n"
12518 " a();\n"
12519 " }\n"
12520 " }\n"
12521 " void g() { return; }\n"
12522 "};\n"
12523 "struct B {\n"
12524 " int x;\n"
12525 "};\n"
12526 "} // namespace a\n",
12527 LinuxBraceStyle);
12528 verifyFormat("enum X {\n"
12529 " Y = 0,\n"
12530 "}\n",
12531 LinuxBraceStyle);
12532 verifyFormat("struct S {\n"
12533 " int Type;\n"
12534 " union {\n"
12535 " int x;\n"
12536 " double y;\n"
12537 " } Value;\n"
12538 " class C\n"
12539 " {\n"
12540 " MyFavoriteType Value;\n"
12541 " } Class;\n"
12542 "}\n",
12543 LinuxBraceStyle);
12544 }
12545
TEST_F(FormatTest,MozillaBraceBreaking)12546 TEST_F(FormatTest, MozillaBraceBreaking) {
12547 FormatStyle MozillaBraceStyle = getLLVMStyle();
12548 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
12549 MozillaBraceStyle.FixNamespaceComments = false;
12550 verifyFormat("namespace a {\n"
12551 "class A\n"
12552 "{\n"
12553 " void f()\n"
12554 " {\n"
12555 " if (true) {\n"
12556 " a();\n"
12557 " b();\n"
12558 " }\n"
12559 " }\n"
12560 " void g() { return; }\n"
12561 "};\n"
12562 "enum E\n"
12563 "{\n"
12564 " A,\n"
12565 " // foo\n"
12566 " B,\n"
12567 " C\n"
12568 "};\n"
12569 "struct B\n"
12570 "{\n"
12571 " int x;\n"
12572 "};\n"
12573 "}\n",
12574 MozillaBraceStyle);
12575 verifyFormat("struct S\n"
12576 "{\n"
12577 " int Type;\n"
12578 " union\n"
12579 " {\n"
12580 " int x;\n"
12581 " double y;\n"
12582 " } Value;\n"
12583 " class C\n"
12584 " {\n"
12585 " MyFavoriteType Value;\n"
12586 " } Class;\n"
12587 "}\n",
12588 MozillaBraceStyle);
12589 }
12590
TEST_F(FormatTest,StroustrupBraceBreaking)12591 TEST_F(FormatTest, StroustrupBraceBreaking) {
12592 FormatStyle StroustrupBraceStyle = getLLVMStyle();
12593 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
12594 verifyFormat("namespace a {\n"
12595 "class A {\n"
12596 " void f()\n"
12597 " {\n"
12598 " if (true) {\n"
12599 " a();\n"
12600 " b();\n"
12601 " }\n"
12602 " }\n"
12603 " void g() { return; }\n"
12604 "};\n"
12605 "struct B {\n"
12606 " int x;\n"
12607 "};\n"
12608 "} // namespace a\n",
12609 StroustrupBraceStyle);
12610
12611 verifyFormat("void foo()\n"
12612 "{\n"
12613 " if (a) {\n"
12614 " a();\n"
12615 " }\n"
12616 " else {\n"
12617 " b();\n"
12618 " }\n"
12619 "}\n",
12620 StroustrupBraceStyle);
12621
12622 verifyFormat("#ifdef _DEBUG\n"
12623 "int foo(int i = 0)\n"
12624 "#else\n"
12625 "int foo(int i = 5)\n"
12626 "#endif\n"
12627 "{\n"
12628 " return i;\n"
12629 "}",
12630 StroustrupBraceStyle);
12631
12632 verifyFormat("void foo() {}\n"
12633 "void bar()\n"
12634 "#ifdef _DEBUG\n"
12635 "{\n"
12636 " foo();\n"
12637 "}\n"
12638 "#else\n"
12639 "{\n"
12640 "}\n"
12641 "#endif",
12642 StroustrupBraceStyle);
12643
12644 verifyFormat("void foobar() { int i = 5; }\n"
12645 "#ifdef _DEBUG\n"
12646 "void bar() {}\n"
12647 "#else\n"
12648 "void bar() { foobar(); }\n"
12649 "#endif",
12650 StroustrupBraceStyle);
12651 }
12652
TEST_F(FormatTest,AllmanBraceBreaking)12653 TEST_F(FormatTest, AllmanBraceBreaking) {
12654 FormatStyle AllmanBraceStyle = getLLVMStyle();
12655 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
12656
12657 EXPECT_EQ("namespace a\n"
12658 "{\n"
12659 "void f();\n"
12660 "void g();\n"
12661 "} // namespace a\n",
12662 format("namespace a\n"
12663 "{\n"
12664 "void f();\n"
12665 "void g();\n"
12666 "}\n",
12667 AllmanBraceStyle));
12668
12669 verifyFormat("namespace a\n"
12670 "{\n"
12671 "class A\n"
12672 "{\n"
12673 " void f()\n"
12674 " {\n"
12675 " if (true)\n"
12676 " {\n"
12677 " a();\n"
12678 " b();\n"
12679 " }\n"
12680 " }\n"
12681 " void g() { return; }\n"
12682 "};\n"
12683 "struct B\n"
12684 "{\n"
12685 " int x;\n"
12686 "};\n"
12687 "union C\n"
12688 "{\n"
12689 "};\n"
12690 "} // namespace a",
12691 AllmanBraceStyle);
12692
12693 verifyFormat("void f()\n"
12694 "{\n"
12695 " if (true)\n"
12696 " {\n"
12697 " a();\n"
12698 " }\n"
12699 " else if (false)\n"
12700 " {\n"
12701 " b();\n"
12702 " }\n"
12703 " else\n"
12704 " {\n"
12705 " c();\n"
12706 " }\n"
12707 "}\n",
12708 AllmanBraceStyle);
12709
12710 verifyFormat("void f()\n"
12711 "{\n"
12712 " for (int i = 0; i < 10; ++i)\n"
12713 " {\n"
12714 " a();\n"
12715 " }\n"
12716 " while (false)\n"
12717 " {\n"
12718 " b();\n"
12719 " }\n"
12720 " do\n"
12721 " {\n"
12722 " c();\n"
12723 " } while (false)\n"
12724 "}\n",
12725 AllmanBraceStyle);
12726
12727 verifyFormat("void f(int a)\n"
12728 "{\n"
12729 " switch (a)\n"
12730 " {\n"
12731 " case 0:\n"
12732 " break;\n"
12733 " case 1:\n"
12734 " {\n"
12735 " break;\n"
12736 " }\n"
12737 " case 2:\n"
12738 " {\n"
12739 " }\n"
12740 " break;\n"
12741 " default:\n"
12742 " break;\n"
12743 " }\n"
12744 "}\n",
12745 AllmanBraceStyle);
12746
12747 verifyFormat("enum X\n"
12748 "{\n"
12749 " Y = 0,\n"
12750 "}\n",
12751 AllmanBraceStyle);
12752 verifyFormat("enum X\n"
12753 "{\n"
12754 " Y = 0\n"
12755 "}\n",
12756 AllmanBraceStyle);
12757
12758 verifyFormat("@interface BSApplicationController ()\n"
12759 "{\n"
12760 "@private\n"
12761 " id _extraIvar;\n"
12762 "}\n"
12763 "@end\n",
12764 AllmanBraceStyle);
12765
12766 verifyFormat("#ifdef _DEBUG\n"
12767 "int foo(int i = 0)\n"
12768 "#else\n"
12769 "int foo(int i = 5)\n"
12770 "#endif\n"
12771 "{\n"
12772 " return i;\n"
12773 "}",
12774 AllmanBraceStyle);
12775
12776 verifyFormat("void foo() {}\n"
12777 "void bar()\n"
12778 "#ifdef _DEBUG\n"
12779 "{\n"
12780 " foo();\n"
12781 "}\n"
12782 "#else\n"
12783 "{\n"
12784 "}\n"
12785 "#endif",
12786 AllmanBraceStyle);
12787
12788 verifyFormat("void foobar() { int i = 5; }\n"
12789 "#ifdef _DEBUG\n"
12790 "void bar() {}\n"
12791 "#else\n"
12792 "void bar() { foobar(); }\n"
12793 "#endif",
12794 AllmanBraceStyle);
12795
12796 // This shouldn't affect ObjC blocks..
12797 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
12798 " // ...\n"
12799 " int i;\n"
12800 "}];",
12801 AllmanBraceStyle);
12802 verifyFormat("void (^block)(void) = ^{\n"
12803 " // ...\n"
12804 " int i;\n"
12805 "};",
12806 AllmanBraceStyle);
12807 // .. or dict literals.
12808 verifyFormat("void f()\n"
12809 "{\n"
12810 " // ...\n"
12811 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
12812 "}",
12813 AllmanBraceStyle);
12814 verifyFormat("void f()\n"
12815 "{\n"
12816 " // ...\n"
12817 " [object someMethod:@{a : @\"b\"}];\n"
12818 "}",
12819 AllmanBraceStyle);
12820 verifyFormat("int f()\n"
12821 "{ // comment\n"
12822 " return 42;\n"
12823 "}",
12824 AllmanBraceStyle);
12825
12826 AllmanBraceStyle.ColumnLimit = 19;
12827 verifyFormat("void f() { int i; }", AllmanBraceStyle);
12828 AllmanBraceStyle.ColumnLimit = 18;
12829 verifyFormat("void f()\n"
12830 "{\n"
12831 " int i;\n"
12832 "}",
12833 AllmanBraceStyle);
12834 AllmanBraceStyle.ColumnLimit = 80;
12835
12836 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
12837 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
12838 FormatStyle::SIS_WithoutElse;
12839 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
12840 verifyFormat("void f(bool b)\n"
12841 "{\n"
12842 " if (b)\n"
12843 " {\n"
12844 " return;\n"
12845 " }\n"
12846 "}\n",
12847 BreakBeforeBraceShortIfs);
12848 verifyFormat("void f(bool b)\n"
12849 "{\n"
12850 " if constexpr (b)\n"
12851 " {\n"
12852 " return;\n"
12853 " }\n"
12854 "}\n",
12855 BreakBeforeBraceShortIfs);
12856 verifyFormat("void f(bool b)\n"
12857 "{\n"
12858 " if CONSTEXPR (b)\n"
12859 " {\n"
12860 " return;\n"
12861 " }\n"
12862 "}\n",
12863 BreakBeforeBraceShortIfs);
12864 verifyFormat("void f(bool b)\n"
12865 "{\n"
12866 " if (b) return;\n"
12867 "}\n",
12868 BreakBeforeBraceShortIfs);
12869 verifyFormat("void f(bool b)\n"
12870 "{\n"
12871 " if constexpr (b) return;\n"
12872 "}\n",
12873 BreakBeforeBraceShortIfs);
12874 verifyFormat("void f(bool b)\n"
12875 "{\n"
12876 " if CONSTEXPR (b) return;\n"
12877 "}\n",
12878 BreakBeforeBraceShortIfs);
12879 verifyFormat("void f(bool b)\n"
12880 "{\n"
12881 " while (b)\n"
12882 " {\n"
12883 " return;\n"
12884 " }\n"
12885 "}\n",
12886 BreakBeforeBraceShortIfs);
12887 }
12888
TEST_F(FormatTest,WhitesmithsBraceBreaking)12889 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
12890 FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
12891 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
12892
12893 // Make a few changes to the style for testing purposes
12894 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
12895 FormatStyle::SFS_Empty;
12896 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
12897 WhitesmithsBraceStyle.ColumnLimit = 0;
12898
12899 // FIXME: this test case can't decide whether there should be a blank line
12900 // after the ~D() line or not. It adds one if one doesn't exist in the test
12901 // and it removes the line if one exists.
12902 /*
12903 verifyFormat("class A;\n"
12904 "namespace B\n"
12905 " {\n"
12906 "class C;\n"
12907 "// Comment\n"
12908 "class D\n"
12909 " {\n"
12910 "public:\n"
12911 " D();\n"
12912 " ~D() {}\n"
12913 "private:\n"
12914 " enum E\n"
12915 " {\n"
12916 " F\n"
12917 " }\n"
12918 " };\n"
12919 " } // namespace B\n",
12920 WhitesmithsBraceStyle);
12921 */
12922
12923 verifyFormat("namespace a\n"
12924 " {\n"
12925 "class A\n"
12926 " {\n"
12927 " void f()\n"
12928 " {\n"
12929 " if (true)\n"
12930 " {\n"
12931 " a();\n"
12932 " b();\n"
12933 " }\n"
12934 " }\n"
12935 " void g()\n"
12936 " {\n"
12937 " return;\n"
12938 " }\n"
12939 " };\n"
12940 "struct B\n"
12941 " {\n"
12942 " int x;\n"
12943 " };\n"
12944 " } // namespace a",
12945 WhitesmithsBraceStyle);
12946
12947 verifyFormat("void f()\n"
12948 " {\n"
12949 " if (true)\n"
12950 " {\n"
12951 " a();\n"
12952 " }\n"
12953 " else if (false)\n"
12954 " {\n"
12955 " b();\n"
12956 " }\n"
12957 " else\n"
12958 " {\n"
12959 " c();\n"
12960 " }\n"
12961 " }\n",
12962 WhitesmithsBraceStyle);
12963
12964 verifyFormat("void f()\n"
12965 " {\n"
12966 " for (int i = 0; i < 10; ++i)\n"
12967 " {\n"
12968 " a();\n"
12969 " }\n"
12970 " while (false)\n"
12971 " {\n"
12972 " b();\n"
12973 " }\n"
12974 " do\n"
12975 " {\n"
12976 " c();\n"
12977 " } while (false)\n"
12978 " }\n",
12979 WhitesmithsBraceStyle);
12980
12981 WhitesmithsBraceStyle.IndentCaseBlocks = true;
12982 verifyFormat("void switchTest1(int a)\n"
12983 " {\n"
12984 " switch (a)\n"
12985 " {\n"
12986 " case 2:\n"
12987 " {\n"
12988 " }\n"
12989 " break;\n"
12990 " }\n"
12991 " }\n",
12992 WhitesmithsBraceStyle);
12993
12994 verifyFormat("void switchTest2(int a)\n"
12995 " {\n"
12996 " switch (a)\n"
12997 " {\n"
12998 " case 0:\n"
12999 " break;\n"
13000 " case 1:\n"
13001 " {\n"
13002 " break;\n"
13003 " }\n"
13004 " case 2:\n"
13005 " {\n"
13006 " }\n"
13007 " break;\n"
13008 " default:\n"
13009 " break;\n"
13010 " }\n"
13011 " }\n",
13012 WhitesmithsBraceStyle);
13013
13014 verifyFormat("void switchTest3(int a)\n"
13015 " {\n"
13016 " switch (a)\n"
13017 " {\n"
13018 " case 0:\n"
13019 " {\n"
13020 " foo(x);\n"
13021 " }\n"
13022 " break;\n"
13023 " default:\n"
13024 " {\n"
13025 " foo(1);\n"
13026 " }\n"
13027 " break;\n"
13028 " }\n"
13029 " }\n",
13030 WhitesmithsBraceStyle);
13031
13032 WhitesmithsBraceStyle.IndentCaseBlocks = false;
13033
13034 verifyFormat("void switchTest4(int a)\n"
13035 " {\n"
13036 " switch (a)\n"
13037 " {\n"
13038 " case 2:\n"
13039 " {\n"
13040 " }\n"
13041 " break;\n"
13042 " }\n"
13043 " }\n",
13044 WhitesmithsBraceStyle);
13045
13046 verifyFormat("void switchTest5(int a)\n"
13047 " {\n"
13048 " switch (a)\n"
13049 " {\n"
13050 " case 0:\n"
13051 " break;\n"
13052 " case 1:\n"
13053 " {\n"
13054 " foo();\n"
13055 " break;\n"
13056 " }\n"
13057 " case 2:\n"
13058 " {\n"
13059 " }\n"
13060 " break;\n"
13061 " default:\n"
13062 " break;\n"
13063 " }\n"
13064 " }\n",
13065 WhitesmithsBraceStyle);
13066
13067 verifyFormat("void switchTest6(int a)\n"
13068 " {\n"
13069 " switch (a)\n"
13070 " {\n"
13071 " case 0:\n"
13072 " {\n"
13073 " foo(x);\n"
13074 " }\n"
13075 " break;\n"
13076 " default:\n"
13077 " {\n"
13078 " foo(1);\n"
13079 " }\n"
13080 " break;\n"
13081 " }\n"
13082 " }\n",
13083 WhitesmithsBraceStyle);
13084
13085 verifyFormat("enum X\n"
13086 " {\n"
13087 " Y = 0, // testing\n"
13088 " }\n",
13089 WhitesmithsBraceStyle);
13090
13091 verifyFormat("enum X\n"
13092 " {\n"
13093 " Y = 0\n"
13094 " }\n",
13095 WhitesmithsBraceStyle);
13096 verifyFormat("enum X\n"
13097 " {\n"
13098 " Y = 0,\n"
13099 " Z = 1\n"
13100 " };\n",
13101 WhitesmithsBraceStyle);
13102
13103 verifyFormat("@interface BSApplicationController ()\n"
13104 " {\n"
13105 "@private\n"
13106 " id _extraIvar;\n"
13107 " }\n"
13108 "@end\n",
13109 WhitesmithsBraceStyle);
13110
13111 verifyFormat("#ifdef _DEBUG\n"
13112 "int foo(int i = 0)\n"
13113 "#else\n"
13114 "int foo(int i = 5)\n"
13115 "#endif\n"
13116 " {\n"
13117 " return i;\n"
13118 " }",
13119 WhitesmithsBraceStyle);
13120
13121 verifyFormat("void foo() {}\n"
13122 "void bar()\n"
13123 "#ifdef _DEBUG\n"
13124 " {\n"
13125 " foo();\n"
13126 " }\n"
13127 "#else\n"
13128 " {\n"
13129 " }\n"
13130 "#endif",
13131 WhitesmithsBraceStyle);
13132
13133 verifyFormat("void foobar()\n"
13134 " {\n"
13135 " int i = 5;\n"
13136 " }\n"
13137 "#ifdef _DEBUG\n"
13138 "void bar()\n"
13139 " {\n"
13140 " }\n"
13141 "#else\n"
13142 "void bar()\n"
13143 " {\n"
13144 " foobar();\n"
13145 " }\n"
13146 "#endif",
13147 WhitesmithsBraceStyle);
13148
13149 // This shouldn't affect ObjC blocks..
13150 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
13151 " // ...\n"
13152 " int i;\n"
13153 "}];",
13154 WhitesmithsBraceStyle);
13155 verifyFormat("void (^block)(void) = ^{\n"
13156 " // ...\n"
13157 " int i;\n"
13158 "};",
13159 WhitesmithsBraceStyle);
13160 // .. or dict literals.
13161 verifyFormat("void f()\n"
13162 " {\n"
13163 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
13164 " }",
13165 WhitesmithsBraceStyle);
13166
13167 verifyFormat("int f()\n"
13168 " { // comment\n"
13169 " return 42;\n"
13170 " }",
13171 WhitesmithsBraceStyle);
13172
13173 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
13174 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
13175 FormatStyle::SIS_Always;
13176 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
13177 verifyFormat("void f(bool b)\n"
13178 " {\n"
13179 " if (b)\n"
13180 " {\n"
13181 " return;\n"
13182 " }\n"
13183 " }\n",
13184 BreakBeforeBraceShortIfs);
13185 verifyFormat("void f(bool b)\n"
13186 " {\n"
13187 " if (b) return;\n"
13188 " }\n",
13189 BreakBeforeBraceShortIfs);
13190 verifyFormat("void f(bool b)\n"
13191 " {\n"
13192 " while (b)\n"
13193 " {\n"
13194 " return;\n"
13195 " }\n"
13196 " }\n",
13197 BreakBeforeBraceShortIfs);
13198 }
13199
TEST_F(FormatTest,GNUBraceBreaking)13200 TEST_F(FormatTest, GNUBraceBreaking) {
13201 FormatStyle GNUBraceStyle = getLLVMStyle();
13202 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
13203 verifyFormat("namespace a\n"
13204 "{\n"
13205 "class A\n"
13206 "{\n"
13207 " void f()\n"
13208 " {\n"
13209 " int a;\n"
13210 " {\n"
13211 " int b;\n"
13212 " }\n"
13213 " if (true)\n"
13214 " {\n"
13215 " a();\n"
13216 " b();\n"
13217 " }\n"
13218 " }\n"
13219 " void g() { return; }\n"
13220 "}\n"
13221 "} // namespace a",
13222 GNUBraceStyle);
13223
13224 verifyFormat("void f()\n"
13225 "{\n"
13226 " if (true)\n"
13227 " {\n"
13228 " a();\n"
13229 " }\n"
13230 " else if (false)\n"
13231 " {\n"
13232 " b();\n"
13233 " }\n"
13234 " else\n"
13235 " {\n"
13236 " c();\n"
13237 " }\n"
13238 "}\n",
13239 GNUBraceStyle);
13240
13241 verifyFormat("void f()\n"
13242 "{\n"
13243 " for (int i = 0; i < 10; ++i)\n"
13244 " {\n"
13245 " a();\n"
13246 " }\n"
13247 " while (false)\n"
13248 " {\n"
13249 " b();\n"
13250 " }\n"
13251 " do\n"
13252 " {\n"
13253 " c();\n"
13254 " }\n"
13255 " while (false);\n"
13256 "}\n",
13257 GNUBraceStyle);
13258
13259 verifyFormat("void f(int a)\n"
13260 "{\n"
13261 " switch (a)\n"
13262 " {\n"
13263 " case 0:\n"
13264 " break;\n"
13265 " case 1:\n"
13266 " {\n"
13267 " break;\n"
13268 " }\n"
13269 " case 2:\n"
13270 " {\n"
13271 " }\n"
13272 " break;\n"
13273 " default:\n"
13274 " break;\n"
13275 " }\n"
13276 "}\n",
13277 GNUBraceStyle);
13278
13279 verifyFormat("enum X\n"
13280 "{\n"
13281 " Y = 0,\n"
13282 "}\n",
13283 GNUBraceStyle);
13284
13285 verifyFormat("@interface BSApplicationController ()\n"
13286 "{\n"
13287 "@private\n"
13288 " id _extraIvar;\n"
13289 "}\n"
13290 "@end\n",
13291 GNUBraceStyle);
13292
13293 verifyFormat("#ifdef _DEBUG\n"
13294 "int foo(int i = 0)\n"
13295 "#else\n"
13296 "int foo(int i = 5)\n"
13297 "#endif\n"
13298 "{\n"
13299 " return i;\n"
13300 "}",
13301 GNUBraceStyle);
13302
13303 verifyFormat("void foo() {}\n"
13304 "void bar()\n"
13305 "#ifdef _DEBUG\n"
13306 "{\n"
13307 " foo();\n"
13308 "}\n"
13309 "#else\n"
13310 "{\n"
13311 "}\n"
13312 "#endif",
13313 GNUBraceStyle);
13314
13315 verifyFormat("void foobar() { int i = 5; }\n"
13316 "#ifdef _DEBUG\n"
13317 "void bar() {}\n"
13318 "#else\n"
13319 "void bar() { foobar(); }\n"
13320 "#endif",
13321 GNUBraceStyle);
13322 }
13323
TEST_F(FormatTest,WebKitBraceBreaking)13324 TEST_F(FormatTest, WebKitBraceBreaking) {
13325 FormatStyle WebKitBraceStyle = getLLVMStyle();
13326 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
13327 WebKitBraceStyle.FixNamespaceComments = false;
13328 verifyFormat("namespace a {\n"
13329 "class A {\n"
13330 " void f()\n"
13331 " {\n"
13332 " if (true) {\n"
13333 " a();\n"
13334 " b();\n"
13335 " }\n"
13336 " }\n"
13337 " void g() { return; }\n"
13338 "};\n"
13339 "enum E {\n"
13340 " A,\n"
13341 " // foo\n"
13342 " B,\n"
13343 " C\n"
13344 "};\n"
13345 "struct B {\n"
13346 " int x;\n"
13347 "};\n"
13348 "}\n",
13349 WebKitBraceStyle);
13350 verifyFormat("struct S {\n"
13351 " int Type;\n"
13352 " union {\n"
13353 " int x;\n"
13354 " double y;\n"
13355 " } Value;\n"
13356 " class C {\n"
13357 " MyFavoriteType Value;\n"
13358 " } Class;\n"
13359 "};\n",
13360 WebKitBraceStyle);
13361 }
13362
TEST_F(FormatTest,CatchExceptionReferenceBinding)13363 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
13364 verifyFormat("void f() {\n"
13365 " try {\n"
13366 " } catch (const Exception &e) {\n"
13367 " }\n"
13368 "}\n",
13369 getLLVMStyle());
13370 }
13371
TEST_F(FormatTest,UnderstandsPragmas)13372 TEST_F(FormatTest, UnderstandsPragmas) {
13373 verifyFormat("#pragma omp reduction(| : var)");
13374 verifyFormat("#pragma omp reduction(+ : var)");
13375
13376 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
13377 "(including parentheses).",
13378 format("#pragma mark Any non-hyphenated or hyphenated string "
13379 "(including parentheses)."));
13380 }
13381
TEST_F(FormatTest,UnderstandPragmaOption)13382 TEST_F(FormatTest, UnderstandPragmaOption) {
13383 verifyFormat("#pragma option -C -A");
13384
13385 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
13386 }
13387
TEST_F(FormatTest,OptimizeBreakPenaltyVsExcess)13388 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
13389 FormatStyle Style = getLLVMStyle();
13390 Style.ColumnLimit = 20;
13391
13392 // See PR41213
13393 EXPECT_EQ("/*\n"
13394 " *\t9012345\n"
13395 " * /8901\n"
13396 " */",
13397 format("/*\n"
13398 " *\t9012345 /8901\n"
13399 " */",
13400 Style));
13401 EXPECT_EQ("/*\n"
13402 " *345678\n"
13403 " *\t/8901\n"
13404 " */",
13405 format("/*\n"
13406 " *345678\t/8901\n"
13407 " */",
13408 Style));
13409
13410 verifyFormat("int a; // the\n"
13411 " // comment",
13412 Style);
13413 EXPECT_EQ("int a; /* first line\n"
13414 " * second\n"
13415 " * line third\n"
13416 " * line\n"
13417 " */",
13418 format("int a; /* first line\n"
13419 " * second\n"
13420 " * line third\n"
13421 " * line\n"
13422 " */",
13423 Style));
13424 EXPECT_EQ("int a; // first line\n"
13425 " // second\n"
13426 " // line third\n"
13427 " // line",
13428 format("int a; // first line\n"
13429 " // second line\n"
13430 " // third line",
13431 Style));
13432
13433 Style.PenaltyExcessCharacter = 90;
13434 verifyFormat("int a; // the comment", Style);
13435 EXPECT_EQ("int a; // the comment\n"
13436 " // aaa",
13437 format("int a; // the comment aaa", Style));
13438 EXPECT_EQ("int a; /* first line\n"
13439 " * second line\n"
13440 " * third line\n"
13441 " */",
13442 format("int a; /* first line\n"
13443 " * second line\n"
13444 " * third line\n"
13445 " */",
13446 Style));
13447 EXPECT_EQ("int a; // first line\n"
13448 " // second line\n"
13449 " // third line",
13450 format("int a; // first line\n"
13451 " // second line\n"
13452 " // third line",
13453 Style));
13454 // FIXME: Investigate why this is not getting the same layout as the test
13455 // above.
13456 EXPECT_EQ("int a; /* first line\n"
13457 " * second line\n"
13458 " * third line\n"
13459 " */",
13460 format("int a; /* first line second line third line"
13461 "\n*/",
13462 Style));
13463
13464 EXPECT_EQ("// foo bar baz bazfoo\n"
13465 "// foo bar foo bar\n",
13466 format("// foo bar baz bazfoo\n"
13467 "// foo bar foo bar\n",
13468 Style));
13469 EXPECT_EQ("// foo bar baz bazfoo\n"
13470 "// foo bar foo bar\n",
13471 format("// foo bar baz bazfoo\n"
13472 "// foo bar foo bar\n",
13473 Style));
13474
13475 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
13476 // next one.
13477 EXPECT_EQ("// foo bar baz bazfoo\n"
13478 "// bar foo bar\n",
13479 format("// foo bar baz bazfoo bar\n"
13480 "// foo bar\n",
13481 Style));
13482
13483 EXPECT_EQ("// foo bar baz bazfoo\n"
13484 "// foo bar baz bazfoo\n"
13485 "// bar foo bar\n",
13486 format("// foo bar baz bazfoo\n"
13487 "// foo bar baz bazfoo bar\n"
13488 "// foo bar\n",
13489 Style));
13490
13491 EXPECT_EQ("// foo bar baz bazfoo\n"
13492 "// foo bar baz bazfoo\n"
13493 "// bar foo bar\n",
13494 format("// foo bar baz bazfoo\n"
13495 "// foo bar baz bazfoo bar\n"
13496 "// foo bar\n",
13497 Style));
13498
13499 // Make sure we do not keep protruding characters if strict mode reflow is
13500 // cheaper than keeping protruding characters.
13501 Style.ColumnLimit = 21;
13502 EXPECT_EQ(
13503 "// foo foo foo foo\n"
13504 "// foo foo foo foo\n"
13505 "// foo foo foo foo\n",
13506 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style));
13507
13508 EXPECT_EQ("int a = /* long block\n"
13509 " comment */\n"
13510 " 42;",
13511 format("int a = /* long block comment */ 42;", Style));
13512 }
13513
13514 #define EXPECT_ALL_STYLES_EQUAL(Styles) \
13515 for (size_t i = 1; i < Styles.size(); ++i) \
13516 EXPECT_EQ(Styles[0], Styles[i]) \
13517 << "Style #" << i << " of " << Styles.size() << " differs from Style #0"
13518
TEST_F(FormatTest,GetsPredefinedStyleByName)13519 TEST_F(FormatTest, GetsPredefinedStyleByName) {
13520 SmallVector<FormatStyle, 3> Styles;
13521 Styles.resize(3);
13522
13523 Styles[0] = getLLVMStyle();
13524 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
13525 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
13526 EXPECT_ALL_STYLES_EQUAL(Styles);
13527
13528 Styles[0] = getGoogleStyle();
13529 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
13530 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
13531 EXPECT_ALL_STYLES_EQUAL(Styles);
13532
13533 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
13534 EXPECT_TRUE(
13535 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
13536 EXPECT_TRUE(
13537 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
13538 EXPECT_ALL_STYLES_EQUAL(Styles);
13539
13540 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
13541 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
13542 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
13543 EXPECT_ALL_STYLES_EQUAL(Styles);
13544
13545 Styles[0] = getMozillaStyle();
13546 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
13547 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
13548 EXPECT_ALL_STYLES_EQUAL(Styles);
13549
13550 Styles[0] = getWebKitStyle();
13551 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
13552 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
13553 EXPECT_ALL_STYLES_EQUAL(Styles);
13554
13555 Styles[0] = getGNUStyle();
13556 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
13557 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
13558 EXPECT_ALL_STYLES_EQUAL(Styles);
13559
13560 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
13561 }
13562
TEST_F(FormatTest,GetsCorrectBasedOnStyle)13563 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
13564 SmallVector<FormatStyle, 8> Styles;
13565 Styles.resize(2);
13566
13567 Styles[0] = getGoogleStyle();
13568 Styles[1] = getLLVMStyle();
13569 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
13570 EXPECT_ALL_STYLES_EQUAL(Styles);
13571
13572 Styles.resize(5);
13573 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
13574 Styles[1] = getLLVMStyle();
13575 Styles[1].Language = FormatStyle::LK_JavaScript;
13576 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
13577
13578 Styles[2] = getLLVMStyle();
13579 Styles[2].Language = FormatStyle::LK_JavaScript;
13580 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
13581 "BasedOnStyle: Google",
13582 &Styles[2])
13583 .value());
13584
13585 Styles[3] = getLLVMStyle();
13586 Styles[3].Language = FormatStyle::LK_JavaScript;
13587 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
13588 "Language: JavaScript",
13589 &Styles[3])
13590 .value());
13591
13592 Styles[4] = getLLVMStyle();
13593 Styles[4].Language = FormatStyle::LK_JavaScript;
13594 EXPECT_EQ(0, parseConfiguration("---\n"
13595 "BasedOnStyle: LLVM\n"
13596 "IndentWidth: 123\n"
13597 "---\n"
13598 "BasedOnStyle: Google\n"
13599 "Language: JavaScript",
13600 &Styles[4])
13601 .value());
13602 EXPECT_ALL_STYLES_EQUAL(Styles);
13603 }
13604
13605 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
13606 Style.FIELD = false; \
13607 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
13608 EXPECT_TRUE(Style.FIELD); \
13609 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
13610 EXPECT_FALSE(Style.FIELD);
13611
13612 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
13613
13614 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
13615 Style.STRUCT.FIELD = false; \
13616 EXPECT_EQ(0, \
13617 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
13618 .value()); \
13619 EXPECT_TRUE(Style.STRUCT.FIELD); \
13620 EXPECT_EQ(0, \
13621 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
13622 .value()); \
13623 EXPECT_FALSE(Style.STRUCT.FIELD);
13624
13625 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
13626 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
13627
13628 #define CHECK_PARSE(TEXT, FIELD, VALUE) \
13629 EXPECT_NE(VALUE, Style.FIELD); \
13630 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
13631 EXPECT_EQ(VALUE, Style.FIELD)
13632
TEST_F(FormatTest,ParsesConfigurationBools)13633 TEST_F(FormatTest, ParsesConfigurationBools) {
13634 FormatStyle Style = {};
13635 Style.Language = FormatStyle::LK_Cpp;
13636 CHECK_PARSE_BOOL(AlignTrailingComments);
13637 CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
13638 CHECK_PARSE_BOOL(AlignConsecutiveBitFields);
13639 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
13640 CHECK_PARSE_BOOL(AlignConsecutiveMacros);
13641 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
13642 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
13643 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
13644 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
13645 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
13646 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
13647 CHECK_PARSE_BOOL(BinPackArguments);
13648 CHECK_PARSE_BOOL(BinPackParameters);
13649 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
13650 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
13651 CHECK_PARSE_BOOL(BreakStringLiterals);
13652 CHECK_PARSE_BOOL(CompactNamespaces);
13653 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
13654 CHECK_PARSE_BOOL(DeriveLineEnding);
13655 CHECK_PARSE_BOOL(DerivePointerAlignment);
13656 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
13657 CHECK_PARSE_BOOL(DisableFormat);
13658 CHECK_PARSE_BOOL(IndentCaseLabels);
13659 CHECK_PARSE_BOOL(IndentCaseBlocks);
13660 CHECK_PARSE_BOOL(IndentGotoLabels);
13661 CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
13662 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
13663 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
13664 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
13665 CHECK_PARSE_BOOL(Cpp11BracedListStyle);
13666 CHECK_PARSE_BOOL(ReflowComments);
13667 CHECK_PARSE_BOOL(SortIncludes);
13668 CHECK_PARSE_BOOL(SortUsingDeclarations);
13669 CHECK_PARSE_BOOL(SpacesInParentheses);
13670 CHECK_PARSE_BOOL(SpacesInSquareBrackets);
13671 CHECK_PARSE_BOOL(SpacesInAngles);
13672 CHECK_PARSE_BOOL(SpacesInConditionalStatement);
13673 CHECK_PARSE_BOOL(SpaceInEmptyBlock);
13674 CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
13675 CHECK_PARSE_BOOL(SpacesInContainerLiterals);
13676 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
13677 CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
13678 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
13679 CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
13680 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
13681 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList);
13682 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
13683 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
13684 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
13685 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
13686 CHECK_PARSE_BOOL(UseCRLF);
13687
13688 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
13689 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
13690 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
13691 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
13692 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
13693 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
13694 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
13695 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
13696 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
13697 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
13698 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
13699 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
13700 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
13701 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
13702 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
13703 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
13704 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
13705 }
13706
13707 #undef CHECK_PARSE_BOOL
13708
TEST_F(FormatTest,ParsesConfiguration)13709 TEST_F(FormatTest, ParsesConfiguration) {
13710 FormatStyle Style = {};
13711 Style.Language = FormatStyle::LK_Cpp;
13712 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
13713 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
13714 ConstructorInitializerIndentWidth, 1234u);
13715 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
13716 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
13717 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
13718 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u);
13719 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
13720 PenaltyBreakBeforeFirstCallParameter, 1234u);
13721 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234",
13722 PenaltyBreakTemplateDeclaration, 1234u);
13723 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
13724 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
13725 PenaltyReturnTypeOnItsOwnLine, 1234u);
13726 CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
13727 SpacesBeforeTrailingComments, 1234u);
13728 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
13729 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
13730 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
13731
13732 Style.PointerAlignment = FormatStyle::PAS_Middle;
13733 CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
13734 FormatStyle::PAS_Left);
13735 CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
13736 FormatStyle::PAS_Right);
13737 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
13738 FormatStyle::PAS_Middle);
13739 // For backward compatibility:
13740 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
13741 FormatStyle::PAS_Left);
13742 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
13743 FormatStyle::PAS_Right);
13744 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
13745 FormatStyle::PAS_Middle);
13746
13747 Style.Standard = FormatStyle::LS_Auto;
13748 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03);
13749 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11);
13750 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14);
13751 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17);
13752 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20);
13753 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
13754 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest);
13755 // Legacy aliases:
13756 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
13757 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest);
13758 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
13759 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
13760
13761 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
13762 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
13763 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
13764 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
13765 FormatStyle::BOS_None);
13766 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
13767 FormatStyle::BOS_All);
13768 // For backward compatibility:
13769 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
13770 FormatStyle::BOS_None);
13771 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
13772 FormatStyle::BOS_All);
13773
13774 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
13775 CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
13776 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
13777 CHECK_PARSE("BreakConstructorInitializers: AfterColon",
13778 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
13779 CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
13780 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
13781 // For backward compatibility:
13782 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
13783 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
13784
13785 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
13786 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList,
13787 FormatStyle::BILS_BeforeComma);
13788 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList,
13789 FormatStyle::BILS_AfterColon);
13790 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList,
13791 FormatStyle::BILS_BeforeColon);
13792 // For backward compatibility:
13793 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
13794 FormatStyle::BILS_BeforeComma);
13795
13796 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13797 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
13798 FormatStyle::BAS_Align);
13799 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
13800 FormatStyle::BAS_DontAlign);
13801 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
13802 FormatStyle::BAS_AlwaysBreak);
13803 // For backward compatibility:
13804 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
13805 FormatStyle::BAS_DontAlign);
13806 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
13807 FormatStyle::BAS_Align);
13808
13809 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
13810 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
13811 FormatStyle::ENAS_DontAlign);
13812 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
13813 FormatStyle::ENAS_Left);
13814 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
13815 FormatStyle::ENAS_Right);
13816 // For backward compatibility:
13817 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
13818 FormatStyle::ENAS_Left);
13819 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
13820 FormatStyle::ENAS_Right);
13821
13822 Style.AlignOperands = FormatStyle::OAS_Align;
13823 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands,
13824 FormatStyle::OAS_DontAlign);
13825 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align);
13826 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands,
13827 FormatStyle::OAS_AlignAfterOperator);
13828 // For backward compatibility:
13829 CHECK_PARSE("AlignOperands: false", AlignOperands,
13830 FormatStyle::OAS_DontAlign);
13831 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align);
13832
13833 Style.UseTab = FormatStyle::UT_ForIndentation;
13834 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
13835 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
13836 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
13837 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
13838 FormatStyle::UT_ForContinuationAndIndentation);
13839 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab,
13840 FormatStyle::UT_AlignWithSpaces);
13841 // For backward compatibility:
13842 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
13843 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
13844
13845 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
13846 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
13847 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
13848 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
13849 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
13850 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
13851 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
13852 // For backward compatibility:
13853 CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
13854 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
13855 CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
13856 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
13857
13858 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
13859 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
13860 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
13861 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
13862 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
13863 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
13864 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
13865 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
13866 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
13867 // For backward compatibility:
13868 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
13869 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
13870 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
13871 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
13872
13873 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
13874 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
13875 FormatStyle::SBPO_Never);
13876 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
13877 FormatStyle::SBPO_Always);
13878 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
13879 FormatStyle::SBPO_ControlStatements);
13880 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens,
13881 FormatStyle::SBPO_NonEmptyParentheses);
13882 // For backward compatibility:
13883 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
13884 FormatStyle::SBPO_Never);
13885 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
13886 FormatStyle::SBPO_ControlStatements);
13887
13888 Style.ColumnLimit = 123;
13889 FormatStyle BaseStyle = getLLVMStyle();
13890 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
13891 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
13892
13893 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
13894 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
13895 FormatStyle::BS_Attach);
13896 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
13897 FormatStyle::BS_Linux);
13898 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
13899 FormatStyle::BS_Mozilla);
13900 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
13901 FormatStyle::BS_Stroustrup);
13902 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
13903 FormatStyle::BS_Allman);
13904 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
13905 FormatStyle::BS_Whitesmiths);
13906 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
13907 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
13908 FormatStyle::BS_WebKit);
13909 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
13910 FormatStyle::BS_Custom);
13911
13912 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
13913 CHECK_PARSE("BraceWrapping:\n"
13914 " AfterControlStatement: MultiLine",
13915 BraceWrapping.AfterControlStatement,
13916 FormatStyle::BWACS_MultiLine);
13917 CHECK_PARSE("BraceWrapping:\n"
13918 " AfterControlStatement: Always",
13919 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
13920 CHECK_PARSE("BraceWrapping:\n"
13921 " AfterControlStatement: Never",
13922 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
13923 // For backward compatibility:
13924 CHECK_PARSE("BraceWrapping:\n"
13925 " AfterControlStatement: true",
13926 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always);
13927 CHECK_PARSE("BraceWrapping:\n"
13928 " AfterControlStatement: false",
13929 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
13930
13931 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
13932 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
13933 FormatStyle::RTBS_None);
13934 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
13935 FormatStyle::RTBS_All);
13936 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
13937 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
13938 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
13939 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
13940 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
13941 AlwaysBreakAfterReturnType,
13942 FormatStyle::RTBS_TopLevelDefinitions);
13943
13944 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
13945 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No",
13946 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No);
13947 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine",
13948 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
13949 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes",
13950 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
13951 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false",
13952 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine);
13953 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true",
13954 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes);
13955
13956 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
13957 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
13958 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
13959 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
13960 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
13961 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
13962 AlwaysBreakAfterDefinitionReturnType,
13963 FormatStyle::DRTBS_TopLevel);
13964
13965 Style.NamespaceIndentation = FormatStyle::NI_All;
13966 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
13967 FormatStyle::NI_None);
13968 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
13969 FormatStyle::NI_Inner);
13970 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
13971 FormatStyle::NI_All);
13972
13973 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always;
13974 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never",
13975 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
13976 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse",
13977 AllowShortIfStatementsOnASingleLine,
13978 FormatStyle::SIS_WithoutElse);
13979 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always",
13980 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always);
13981 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false",
13982 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never);
13983 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true",
13984 AllowShortIfStatementsOnASingleLine,
13985 FormatStyle::SIS_WithoutElse);
13986
13987 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
13988 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock,
13989 FormatStyle::IEBS_AfterExternBlock);
13990 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock,
13991 FormatStyle::IEBS_Indent);
13992 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock,
13993 FormatStyle::IEBS_NoIndent);
13994 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock,
13995 FormatStyle::IEBS_Indent);
13996 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock,
13997 FormatStyle::IEBS_NoIndent);
13998
13999 // FIXME: This is required because parsing a configuration simply overwrites
14000 // the first N elements of the list instead of resetting it.
14001 Style.ForEachMacros.clear();
14002 std::vector<std::string> BoostForeach;
14003 BoostForeach.push_back("BOOST_FOREACH");
14004 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
14005 std::vector<std::string> BoostAndQForeach;
14006 BoostAndQForeach.push_back("BOOST_FOREACH");
14007 BoostAndQForeach.push_back("Q_FOREACH");
14008 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
14009 BoostAndQForeach);
14010
14011 Style.StatementMacros.clear();
14012 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros,
14013 std::vector<std::string>{"QUNUSED"});
14014 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros,
14015 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"}));
14016
14017 Style.NamespaceMacros.clear();
14018 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros,
14019 std::vector<std::string>{"TESTSUITE"});
14020 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros,
14021 std::vector<std::string>({"TESTSUITE", "SUITE"}));
14022
14023 Style.WhitespaceSensitiveMacros.clear();
14024 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]",
14025 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
14026 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]",
14027 WhitespaceSensitiveMacros,
14028 std::vector<std::string>({"STRINGIZE", "ASSERT"}));
14029 Style.WhitespaceSensitiveMacros.clear();
14030 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']",
14031 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"});
14032 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']",
14033 WhitespaceSensitiveMacros,
14034 std::vector<std::string>({"STRINGIZE", "ASSERT"}));
14035
14036 Style.IncludeStyle.IncludeCategories.clear();
14037 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = {
14038 {"abc/.*", 2, 0}, {".*", 1, 0}};
14039 CHECK_PARSE("IncludeCategories:\n"
14040 " - Regex: abc/.*\n"
14041 " Priority: 2\n"
14042 " - Regex: .*\n"
14043 " Priority: 1",
14044 IncludeStyle.IncludeCategories, ExpectedCategories);
14045 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex,
14046 "abc$");
14047 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
14048 IncludeStyle.IncludeIsMainSourceRegex, "abc$");
14049
14050 Style.RawStringFormats.clear();
14051 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
14052 {
14053 FormatStyle::LK_TextProto,
14054 {"pb", "proto"},
14055 {"PARSE_TEXT_PROTO"},
14056 /*CanonicalDelimiter=*/"",
14057 "llvm",
14058 },
14059 {
14060 FormatStyle::LK_Cpp,
14061 {"cc", "cpp"},
14062 {"C_CODEBLOCK", "CPPEVAL"},
14063 /*CanonicalDelimiter=*/"cc",
14064 /*BasedOnStyle=*/"",
14065 },
14066 };
14067
14068 CHECK_PARSE("RawStringFormats:\n"
14069 " - Language: TextProto\n"
14070 " Delimiters:\n"
14071 " - 'pb'\n"
14072 " - 'proto'\n"
14073 " EnclosingFunctions:\n"
14074 " - 'PARSE_TEXT_PROTO'\n"
14075 " BasedOnStyle: llvm\n"
14076 " - Language: Cpp\n"
14077 " Delimiters:\n"
14078 " - 'cc'\n"
14079 " - 'cpp'\n"
14080 " EnclosingFunctions:\n"
14081 " - 'C_CODEBLOCK'\n"
14082 " - 'CPPEVAL'\n"
14083 " CanonicalDelimiter: 'cc'",
14084 RawStringFormats, ExpectedRawStringFormats);
14085 }
14086
TEST_F(FormatTest,ParsesConfigurationWithLanguages)14087 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
14088 FormatStyle Style = {};
14089 Style.Language = FormatStyle::LK_Cpp;
14090 CHECK_PARSE("Language: Cpp\n"
14091 "IndentWidth: 12",
14092 IndentWidth, 12u);
14093 EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
14094 "IndentWidth: 34",
14095 &Style),
14096 ParseError::Unsuitable);
14097 FormatStyle BinPackedTCS = {};
14098 BinPackedTCS.Language = FormatStyle::LK_JavaScript;
14099 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n"
14100 "InsertTrailingCommas: Wrapped",
14101 &BinPackedTCS),
14102 ParseError::BinPackTrailingCommaConflict);
14103 EXPECT_EQ(12u, Style.IndentWidth);
14104 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
14105 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
14106
14107 Style.Language = FormatStyle::LK_JavaScript;
14108 CHECK_PARSE("Language: JavaScript\n"
14109 "IndentWidth: 12",
14110 IndentWidth, 12u);
14111 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
14112 EXPECT_EQ(parseConfiguration("Language: Cpp\n"
14113 "IndentWidth: 34",
14114 &Style),
14115 ParseError::Unsuitable);
14116 EXPECT_EQ(23u, Style.IndentWidth);
14117 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
14118 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
14119
14120 CHECK_PARSE("BasedOnStyle: LLVM\n"
14121 "IndentWidth: 67",
14122 IndentWidth, 67u);
14123
14124 CHECK_PARSE("---\n"
14125 "Language: JavaScript\n"
14126 "IndentWidth: 12\n"
14127 "---\n"
14128 "Language: Cpp\n"
14129 "IndentWidth: 34\n"
14130 "...\n",
14131 IndentWidth, 12u);
14132
14133 Style.Language = FormatStyle::LK_Cpp;
14134 CHECK_PARSE("---\n"
14135 "Language: JavaScript\n"
14136 "IndentWidth: 12\n"
14137 "---\n"
14138 "Language: Cpp\n"
14139 "IndentWidth: 34\n"
14140 "...\n",
14141 IndentWidth, 34u);
14142 CHECK_PARSE("---\n"
14143 "IndentWidth: 78\n"
14144 "---\n"
14145 "Language: JavaScript\n"
14146 "IndentWidth: 56\n"
14147 "...\n",
14148 IndentWidth, 78u);
14149
14150 Style.ColumnLimit = 123;
14151 Style.IndentWidth = 234;
14152 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
14153 Style.TabWidth = 345;
14154 EXPECT_FALSE(parseConfiguration("---\n"
14155 "IndentWidth: 456\n"
14156 "BreakBeforeBraces: Allman\n"
14157 "---\n"
14158 "Language: JavaScript\n"
14159 "IndentWidth: 111\n"
14160 "TabWidth: 111\n"
14161 "---\n"
14162 "Language: Cpp\n"
14163 "BreakBeforeBraces: Stroustrup\n"
14164 "TabWidth: 789\n"
14165 "...\n",
14166 &Style));
14167 EXPECT_EQ(123u, Style.ColumnLimit);
14168 EXPECT_EQ(456u, Style.IndentWidth);
14169 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
14170 EXPECT_EQ(789u, Style.TabWidth);
14171
14172 EXPECT_EQ(parseConfiguration("---\n"
14173 "Language: JavaScript\n"
14174 "IndentWidth: 56\n"
14175 "---\n"
14176 "IndentWidth: 78\n"
14177 "...\n",
14178 &Style),
14179 ParseError::Error);
14180 EXPECT_EQ(parseConfiguration("---\n"
14181 "Language: JavaScript\n"
14182 "IndentWidth: 56\n"
14183 "---\n"
14184 "Language: JavaScript\n"
14185 "IndentWidth: 78\n"
14186 "...\n",
14187 &Style),
14188 ParseError::Error);
14189
14190 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
14191 }
14192
14193 #undef CHECK_PARSE
14194
TEST_F(FormatTest,UsesLanguageForBasedOnStyle)14195 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
14196 FormatStyle Style = {};
14197 Style.Language = FormatStyle::LK_JavaScript;
14198 Style.BreakBeforeTernaryOperators = true;
14199 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
14200 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
14201
14202 Style.BreakBeforeTernaryOperators = true;
14203 EXPECT_EQ(0, parseConfiguration("---\n"
14204 "BasedOnStyle: Google\n"
14205 "---\n"
14206 "Language: JavaScript\n"
14207 "IndentWidth: 76\n"
14208 "...\n",
14209 &Style)
14210 .value());
14211 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
14212 EXPECT_EQ(76u, Style.IndentWidth);
14213 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
14214 }
14215
TEST_F(FormatTest,ConfigurationRoundTripTest)14216 TEST_F(FormatTest, ConfigurationRoundTripTest) {
14217 FormatStyle Style = getLLVMStyle();
14218 std::string YAML = configurationAsText(Style);
14219 FormatStyle ParsedStyle = {};
14220 ParsedStyle.Language = FormatStyle::LK_Cpp;
14221 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
14222 EXPECT_EQ(Style, ParsedStyle);
14223 }
14224
TEST_F(FormatTest,WorksFor8bitEncodings)14225 TEST_F(FormatTest, WorksFor8bitEncodings) {
14226 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
14227 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
14228 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
14229 "\"\xef\xee\xf0\xf3...\"",
14230 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
14231 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
14232 "\xef\xee\xf0\xf3...\"",
14233 getLLVMStyleWithColumns(12)));
14234 }
14235
TEST_F(FormatTest,HandlesUTF8BOM)14236 TEST_F(FormatTest, HandlesUTF8BOM) {
14237 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
14238 EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
14239 format("\xef\xbb\xbf#include <iostream>"));
14240 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
14241 format("\xef\xbb\xbf\n#include <iostream>"));
14242 }
14243
14244 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
14245 #if !defined(_MSC_VER)
14246
TEST_F(FormatTest,CountsUTF8CharactersProperly)14247 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
14248 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
14249 getLLVMStyleWithColumns(35));
14250 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
14251 getLLVMStyleWithColumns(31));
14252 verifyFormat("// Однажды в студёную зимнюю пору...",
14253 getLLVMStyleWithColumns(36));
14254 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
14255 verifyFormat("/* Однажды в студёную зимнюю пору... */",
14256 getLLVMStyleWithColumns(39));
14257 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
14258 getLLVMStyleWithColumns(35));
14259 }
14260
TEST_F(FormatTest,SplitsUTF8Strings)14261 TEST_F(FormatTest, SplitsUTF8Strings) {
14262 // Non-printable characters' width is currently considered to be the length in
14263 // bytes in UTF8. The characters can be displayed in very different manner
14264 // (zero-width, single width with a substitution glyph, expanded to their code
14265 // (e.g. "<8d>"), so there's no single correct way to handle them.
14266 EXPECT_EQ("\"aaaaÄ\"\n"
14267 "\"\xc2\x8d\";",
14268 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
14269 EXPECT_EQ("\"aaaaaaaÄ\"\n"
14270 "\"\xc2\x8d\";",
14271 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
14272 EXPECT_EQ("\"Однажды, в \"\n"
14273 "\"студёную \"\n"
14274 "\"зимнюю \"\n"
14275 "\"пору,\"",
14276 format("\"Однажды, в студёную зимнюю пору,\"",
14277 getLLVMStyleWithColumns(13)));
14278 EXPECT_EQ(
14279 "\"一 二 三 \"\n"
14280 "\"四 五六 \"\n"
14281 "\"七 八 九 \"\n"
14282 "\"十\"",
14283 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
14284 EXPECT_EQ("\"一\t\"\n"
14285 "\"二 \t\"\n"
14286 "\"三 四 \"\n"
14287 "\"五\t\"\n"
14288 "\"六 \t\"\n"
14289 "\"七 \"\n"
14290 "\"八九十\tqq\"",
14291 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
14292 getLLVMStyleWithColumns(11)));
14293
14294 // UTF8 character in an escape sequence.
14295 EXPECT_EQ("\"aaaaaa\"\n"
14296 "\"\\\xC2\x8D\"",
14297 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
14298 }
14299
TEST_F(FormatTest,HandlesDoubleWidthCharsInMultiLineStrings)14300 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
14301 EXPECT_EQ("const char *sssss =\n"
14302 " \"一二三四五六七八\\\n"
14303 " 九 十\";",
14304 format("const char *sssss = \"一二三四五六七八\\\n"
14305 " 九 十\";",
14306 getLLVMStyleWithColumns(30)));
14307 }
14308
TEST_F(FormatTest,SplitsUTF8LineComments)14309 TEST_F(FormatTest, SplitsUTF8LineComments) {
14310 EXPECT_EQ("// aaaaÄ\xc2\x8d",
14311 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
14312 EXPECT_EQ("// Я из лесу\n"
14313 "// вышел; был\n"
14314 "// сильный\n"
14315 "// мороз.",
14316 format("// Я из лесу вышел; был сильный мороз.",
14317 getLLVMStyleWithColumns(13)));
14318 EXPECT_EQ("// 一二三\n"
14319 "// 四五六七\n"
14320 "// 八 九\n"
14321 "// 十",
14322 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
14323 }
14324
TEST_F(FormatTest,SplitsUTF8BlockComments)14325 TEST_F(FormatTest, SplitsUTF8BlockComments) {
14326 EXPECT_EQ("/* Гляжу,\n"
14327 " * поднимается\n"
14328 " * медленно в\n"
14329 " * гору\n"
14330 " * Лошадка,\n"
14331 " * везущая\n"
14332 " * хворосту\n"
14333 " * воз. */",
14334 format("/* Гляжу, поднимается медленно в гору\n"
14335 " * Лошадка, везущая хворосту воз. */",
14336 getLLVMStyleWithColumns(13)));
14337 EXPECT_EQ(
14338 "/* 一二三\n"
14339 " * 四五六七\n"
14340 " * 八 九\n"
14341 " * 十 */",
14342 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
14343 EXPECT_EQ("/* \n"
14344 " * \n"
14345 " * - */",
14346 format("/* - */", getLLVMStyleWithColumns(12)));
14347 }
14348
14349 #endif // _MSC_VER
14350
TEST_F(FormatTest,ConstructorInitializerIndentWidth)14351 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
14352 FormatStyle Style = getLLVMStyle();
14353
14354 Style.ConstructorInitializerIndentWidth = 4;
14355 verifyFormat(
14356 "SomeClass::Constructor()\n"
14357 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
14358 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
14359 Style);
14360
14361 Style.ConstructorInitializerIndentWidth = 2;
14362 verifyFormat(
14363 "SomeClass::Constructor()\n"
14364 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
14365 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
14366 Style);
14367
14368 Style.ConstructorInitializerIndentWidth = 0;
14369 verifyFormat(
14370 "SomeClass::Constructor()\n"
14371 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
14372 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
14373 Style);
14374 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14375 verifyFormat(
14376 "SomeLongTemplateVariableName<\n"
14377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
14378 Style);
14379 verifyFormat("bool smaller = 1 < "
14380 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
14381 " "
14382 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
14383 Style);
14384
14385 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
14386 verifyFormat("SomeClass::Constructor() :\n"
14387 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
14388 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
14389 Style);
14390 }
14391
TEST_F(FormatTest,BreakConstructorInitializersBeforeComma)14392 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
14393 FormatStyle Style = getLLVMStyle();
14394 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
14395 Style.ConstructorInitializerIndentWidth = 4;
14396 verifyFormat("SomeClass::Constructor()\n"
14397 " : a(a)\n"
14398 " , b(b)\n"
14399 " , c(c) {}",
14400 Style);
14401 verifyFormat("SomeClass::Constructor()\n"
14402 " : a(a) {}",
14403 Style);
14404
14405 Style.ColumnLimit = 0;
14406 verifyFormat("SomeClass::Constructor()\n"
14407 " : a(a) {}",
14408 Style);
14409 verifyFormat("SomeClass::Constructor() noexcept\n"
14410 " : a(a) {}",
14411 Style);
14412 verifyFormat("SomeClass::Constructor()\n"
14413 " : a(a)\n"
14414 " , b(b)\n"
14415 " , c(c) {}",
14416 Style);
14417 verifyFormat("SomeClass::Constructor()\n"
14418 " : a(a) {\n"
14419 " foo();\n"
14420 " bar();\n"
14421 "}",
14422 Style);
14423
14424 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14425 verifyFormat("SomeClass::Constructor()\n"
14426 " : a(a)\n"
14427 " , b(b)\n"
14428 " , c(c) {\n}",
14429 Style);
14430 verifyFormat("SomeClass::Constructor()\n"
14431 " : a(a) {\n}",
14432 Style);
14433
14434 Style.ColumnLimit = 80;
14435 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14436 Style.ConstructorInitializerIndentWidth = 2;
14437 verifyFormat("SomeClass::Constructor()\n"
14438 " : a(a)\n"
14439 " , b(b)\n"
14440 " , c(c) {}",
14441 Style);
14442
14443 Style.ConstructorInitializerIndentWidth = 0;
14444 verifyFormat("SomeClass::Constructor()\n"
14445 ": a(a)\n"
14446 ", b(b)\n"
14447 ", c(c) {}",
14448 Style);
14449
14450 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
14451 Style.ConstructorInitializerIndentWidth = 4;
14452 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
14453 verifyFormat(
14454 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
14455 Style);
14456 verifyFormat(
14457 "SomeClass::Constructor()\n"
14458 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
14459 Style);
14460 Style.ConstructorInitializerIndentWidth = 4;
14461 Style.ColumnLimit = 60;
14462 verifyFormat("SomeClass::Constructor()\n"
14463 " : aaaaaaaa(aaaaaaaa)\n"
14464 " , aaaaaaaa(aaaaaaaa)\n"
14465 " , aaaaaaaa(aaaaaaaa) {}",
14466 Style);
14467 }
14468
TEST_F(FormatTest,Destructors)14469 TEST_F(FormatTest, Destructors) {
14470 verifyFormat("void F(int &i) { i.~int(); }");
14471 verifyFormat("void F(int &i) { i->~int(); }");
14472 }
14473
TEST_F(FormatTest,FormatsWithWebKitStyle)14474 TEST_F(FormatTest, FormatsWithWebKitStyle) {
14475 FormatStyle Style = getWebKitStyle();
14476
14477 // Don't indent in outer namespaces.
14478 verifyFormat("namespace outer {\n"
14479 "int i;\n"
14480 "namespace inner {\n"
14481 " int i;\n"
14482 "} // namespace inner\n"
14483 "} // namespace outer\n"
14484 "namespace other_outer {\n"
14485 "int i;\n"
14486 "}",
14487 Style);
14488
14489 // Don't indent case labels.
14490 verifyFormat("switch (variable) {\n"
14491 "case 1:\n"
14492 "case 2:\n"
14493 " doSomething();\n"
14494 " break;\n"
14495 "default:\n"
14496 " ++variable;\n"
14497 "}",
14498 Style);
14499
14500 // Wrap before binary operators.
14501 EXPECT_EQ("void f()\n"
14502 "{\n"
14503 " if (aaaaaaaaaaaaaaaa\n"
14504 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
14505 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
14506 " return;\n"
14507 "}",
14508 format("void f() {\n"
14509 "if (aaaaaaaaaaaaaaaa\n"
14510 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
14511 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
14512 "return;\n"
14513 "}",
14514 Style));
14515
14516 // Allow functions on a single line.
14517 verifyFormat("void f() { return; }", Style);
14518
14519 // Allow empty blocks on a single line and insert a space in empty blocks.
14520 EXPECT_EQ("void f() { }", format("void f() {}", Style));
14521 EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
14522 // However, don't merge non-empty short loops.
14523 EXPECT_EQ("while (true) {\n"
14524 " continue;\n"
14525 "}",
14526 format("while (true) { continue; }", Style));
14527
14528 // Constructor initializers are formatted one per line with the "," on the
14529 // new line.
14530 verifyFormat("Constructor()\n"
14531 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
14532 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
14533 " aaaaaaaaaaaaaa)\n"
14534 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
14535 "{\n"
14536 "}",
14537 Style);
14538 verifyFormat("SomeClass::Constructor()\n"
14539 " : a(a)\n"
14540 "{\n"
14541 "}",
14542 Style);
14543 EXPECT_EQ("SomeClass::Constructor()\n"
14544 " : a(a)\n"
14545 "{\n"
14546 "}",
14547 format("SomeClass::Constructor():a(a){}", Style));
14548 verifyFormat("SomeClass::Constructor()\n"
14549 " : a(a)\n"
14550 " , b(b)\n"
14551 " , c(c)\n"
14552 "{\n"
14553 "}",
14554 Style);
14555 verifyFormat("SomeClass::Constructor()\n"
14556 " : a(a)\n"
14557 "{\n"
14558 " foo();\n"
14559 " bar();\n"
14560 "}",
14561 Style);
14562
14563 // Access specifiers should be aligned left.
14564 verifyFormat("class C {\n"
14565 "public:\n"
14566 " int i;\n"
14567 "};",
14568 Style);
14569
14570 // Do not align comments.
14571 verifyFormat("int a; // Do not\n"
14572 "double b; // align comments.",
14573 Style);
14574
14575 // Do not align operands.
14576 EXPECT_EQ("ASSERT(aaaa\n"
14577 " || bbbb);",
14578 format("ASSERT ( aaaa\n||bbbb);", Style));
14579
14580 // Accept input's line breaks.
14581 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
14582 " || bbbbbbbbbbbbbbb) {\n"
14583 " i++;\n"
14584 "}",
14585 format("if (aaaaaaaaaaaaaaa\n"
14586 "|| bbbbbbbbbbbbbbb) { i++; }",
14587 Style));
14588 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
14589 " i++;\n"
14590 "}",
14591 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
14592
14593 // Don't automatically break all macro definitions (llvm.org/PR17842).
14594 verifyFormat("#define aNumber 10", Style);
14595 // However, generally keep the line breaks that the user authored.
14596 EXPECT_EQ("#define aNumber \\\n"
14597 " 10",
14598 format("#define aNumber \\\n"
14599 " 10",
14600 Style));
14601
14602 // Keep empty and one-element array literals on a single line.
14603 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
14604 " copyItems:YES];",
14605 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
14606 "copyItems:YES];",
14607 Style));
14608 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
14609 " copyItems:YES];",
14610 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
14611 " copyItems:YES];",
14612 Style));
14613 // FIXME: This does not seem right, there should be more indentation before
14614 // the array literal's entries. Nested blocks have the same problem.
14615 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
14616 " @\"a\",\n"
14617 " @\"a\"\n"
14618 "]\n"
14619 " copyItems:YES];",
14620 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
14621 " @\"a\",\n"
14622 " @\"a\"\n"
14623 " ]\n"
14624 " copyItems:YES];",
14625 Style));
14626 EXPECT_EQ(
14627 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
14628 " copyItems:YES];",
14629 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
14630 " copyItems:YES];",
14631 Style));
14632
14633 verifyFormat("[self.a b:c c:d];", Style);
14634 EXPECT_EQ("[self.a b:c\n"
14635 " c:d];",
14636 format("[self.a b:c\n"
14637 "c:d];",
14638 Style));
14639 }
14640
TEST_F(FormatTest,FormatsLambdas)14641 TEST_F(FormatTest, FormatsLambdas) {
14642 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
14643 verifyFormat(
14644 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n");
14645 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
14646 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
14647 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
14648 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
14649 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
14650 verifyFormat("auto c = [a = [b = 42] {}] {};\n");
14651 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
14652 verifyFormat("int x = f(*+[] {});");
14653 verifyFormat("void f() {\n"
14654 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
14655 "}\n");
14656 verifyFormat("void f() {\n"
14657 " other(x.begin(), //\n"
14658 " x.end(), //\n"
14659 " [&](int, int) { return 1; });\n"
14660 "}\n");
14661 verifyFormat("void f() {\n"
14662 " other.other.other.other.other(\n"
14663 " x.begin(), x.end(),\n"
14664 " [something, rather](int, int, int, int, int, int, int) { "
14665 "return 1; });\n"
14666 "}\n");
14667 verifyFormat(
14668 "void f() {\n"
14669 " other.other.other.other.other(\n"
14670 " x.begin(), x.end(),\n"
14671 " [something, rather](int, int, int, int, int, int, int) {\n"
14672 " //\n"
14673 " });\n"
14674 "}\n");
14675 verifyFormat("SomeFunction([]() { // A cool function...\n"
14676 " return 43;\n"
14677 "});");
14678 EXPECT_EQ("SomeFunction([]() {\n"
14679 "#define A a\n"
14680 " return 43;\n"
14681 "});",
14682 format("SomeFunction([](){\n"
14683 "#define A a\n"
14684 "return 43;\n"
14685 "});"));
14686 verifyFormat("void f() {\n"
14687 " SomeFunction([](decltype(x), A *a) {});\n"
14688 "}");
14689 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14690 " [](const aaaaaaaaaa &a) { return a; });");
14691 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
14692 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
14693 "});");
14694 verifyFormat("Constructor()\n"
14695 " : Field([] { // comment\n"
14696 " int i;\n"
14697 " }) {}");
14698 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
14699 " return some_parameter.size();\n"
14700 "};");
14701 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
14702 " [](const string &s) { return s; };");
14703 verifyFormat("int i = aaaaaa ? 1 //\n"
14704 " : [] {\n"
14705 " return 2; //\n"
14706 " }();");
14707 verifyFormat("llvm::errs() << \"number of twos is \"\n"
14708 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
14709 " return x == 2; // force break\n"
14710 " });");
14711 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14712 " [=](int iiiiiiiiiiii) {\n"
14713 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
14714 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
14715 " });",
14716 getLLVMStyleWithColumns(60));
14717 verifyFormat("SomeFunction({[&] {\n"
14718 " // comment\n"
14719 " },\n"
14720 " [&] {\n"
14721 " // comment\n"
14722 " }});");
14723 verifyFormat("SomeFunction({[&] {\n"
14724 " // comment\n"
14725 "}});");
14726 verifyFormat(
14727 "virtual aaaaaaaaaaaaaaaa(\n"
14728 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
14729 " aaaaa aaaaaaaaa);");
14730
14731 // Lambdas with return types.
14732 verifyFormat("int c = []() -> int { return 2; }();\n");
14733 verifyFormat("int c = []() -> int * { return 2; }();\n");
14734 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
14735 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
14736 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
14737 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
14738 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
14739 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
14740 verifyFormat("[a, a]() -> a<1> {};");
14741 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
14742 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
14743 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
14744 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
14745 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
14746 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
14747 verifyFormat("[]() -> foo<!5> { return {}; };");
14748 verifyFormat("[]() -> foo<~5> { return {}; };");
14749 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
14750 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
14751 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
14752 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
14753 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
14754 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
14755 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
14756 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
14757 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
14758 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
14759 verifyFormat("namespace bar {\n"
14760 "// broken:\n"
14761 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
14762 "} // namespace bar");
14763 verifyFormat("namespace bar {\n"
14764 "// broken:\n"
14765 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
14766 "} // namespace bar");
14767 verifyFormat("namespace bar {\n"
14768 "// broken:\n"
14769 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
14770 "} // namespace bar");
14771 verifyFormat("namespace bar {\n"
14772 "// broken:\n"
14773 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
14774 "} // namespace bar");
14775 verifyFormat("namespace bar {\n"
14776 "// broken:\n"
14777 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
14778 "} // namespace bar");
14779 verifyFormat("namespace bar {\n"
14780 "// broken:\n"
14781 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
14782 "} // namespace bar");
14783 verifyFormat("namespace bar {\n"
14784 "// broken:\n"
14785 "auto foo{[]() -> foo<!5> { return {}; }};\n"
14786 "} // namespace bar");
14787 verifyFormat("namespace bar {\n"
14788 "// broken:\n"
14789 "auto foo{[]() -> foo<~5> { return {}; }};\n"
14790 "} // namespace bar");
14791 verifyFormat("namespace bar {\n"
14792 "// broken:\n"
14793 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
14794 "} // namespace bar");
14795 verifyFormat("namespace bar {\n"
14796 "// broken:\n"
14797 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
14798 "} // namespace bar");
14799 verifyFormat("namespace bar {\n"
14800 "// broken:\n"
14801 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
14802 "} // namespace bar");
14803 verifyFormat("namespace bar {\n"
14804 "// broken:\n"
14805 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
14806 "} // namespace bar");
14807 verifyFormat("namespace bar {\n"
14808 "// broken:\n"
14809 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
14810 "} // namespace bar");
14811 verifyFormat("namespace bar {\n"
14812 "// broken:\n"
14813 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
14814 "} // namespace bar");
14815 verifyFormat("namespace bar {\n"
14816 "// broken:\n"
14817 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
14818 "} // namespace bar");
14819 verifyFormat("namespace bar {\n"
14820 "// broken:\n"
14821 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
14822 "} // namespace bar");
14823 verifyFormat("namespace bar {\n"
14824 "// broken:\n"
14825 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
14826 "} // namespace bar");
14827 verifyFormat("namespace bar {\n"
14828 "// broken:\n"
14829 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
14830 "} // namespace bar");
14831 verifyFormat("[]() -> a<1> {};");
14832 verifyFormat("[]() -> a<1> { ; };");
14833 verifyFormat("[]() -> a<1> { ; }();");
14834 verifyFormat("[a, a]() -> a<true> {};");
14835 verifyFormat("[]() -> a<true> {};");
14836 verifyFormat("[]() -> a<true> { ; };");
14837 verifyFormat("[]() -> a<true> { ; }();");
14838 verifyFormat("[a, a]() -> a<false> {};");
14839 verifyFormat("[]() -> a<false> {};");
14840 verifyFormat("[]() -> a<false> { ; };");
14841 verifyFormat("[]() -> a<false> { ; }();");
14842 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
14843 verifyFormat("namespace bar {\n"
14844 "auto foo{[]() -> foo<false> { ; }};\n"
14845 "} // namespace bar");
14846 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
14847 " int j) -> int {\n"
14848 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
14849 "};");
14850 verifyFormat(
14851 "aaaaaaaaaaaaaaaaaaaaaa(\n"
14852 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
14853 " return aaaaaaaaaaaaaaaaa;\n"
14854 " });",
14855 getLLVMStyleWithColumns(70));
14856 verifyFormat("[]() //\n"
14857 " -> int {\n"
14858 " return 1; //\n"
14859 "};");
14860 verifyFormat("[]() -> Void<T...> {};");
14861 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
14862
14863 // Lambdas with explicit template argument lists.
14864 verifyFormat(
14865 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n");
14866
14867 // Multiple lambdas in the same parentheses change indentation rules. These
14868 // lambdas are forced to start on new lines.
14869 verifyFormat("SomeFunction(\n"
14870 " []() {\n"
14871 " //\n"
14872 " },\n"
14873 " []() {\n"
14874 " //\n"
14875 " });");
14876
14877 // A lambda passed as arg0 is always pushed to the next line.
14878 verifyFormat("SomeFunction(\n"
14879 " [this] {\n"
14880 " //\n"
14881 " },\n"
14882 " 1);\n");
14883
14884 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
14885 // the arg0 case above.
14886 auto Style = getGoogleStyle();
14887 Style.BinPackArguments = false;
14888 verifyFormat("SomeFunction(\n"
14889 " a,\n"
14890 " [this] {\n"
14891 " //\n"
14892 " },\n"
14893 " b);\n",
14894 Style);
14895 verifyFormat("SomeFunction(\n"
14896 " a,\n"
14897 " [this] {\n"
14898 " //\n"
14899 " },\n"
14900 " b);\n");
14901
14902 // A lambda with a very long line forces arg0 to be pushed out irrespective of
14903 // the BinPackArguments value (as long as the code is wide enough).
14904 verifyFormat(
14905 "something->SomeFunction(\n"
14906 " a,\n"
14907 " [this] {\n"
14908 " "
14909 "D0000000000000000000000000000000000000000000000000000000000001();\n"
14910 " },\n"
14911 " b);\n");
14912
14913 // A multi-line lambda is pulled up as long as the introducer fits on the
14914 // previous line and there are no further args.
14915 verifyFormat("function(1, [this, that] {\n"
14916 " //\n"
14917 "});\n");
14918 verifyFormat("function([this, that] {\n"
14919 " //\n"
14920 "});\n");
14921 // FIXME: this format is not ideal and we should consider forcing the first
14922 // arg onto its own line.
14923 verifyFormat("function(a, b, c, //\n"
14924 " d, [this, that] {\n"
14925 " //\n"
14926 " });\n");
14927
14928 // Multiple lambdas are treated correctly even when there is a short arg0.
14929 verifyFormat("SomeFunction(\n"
14930 " 1,\n"
14931 " [this] {\n"
14932 " //\n"
14933 " },\n"
14934 " [this] {\n"
14935 " //\n"
14936 " },\n"
14937 " 1);\n");
14938
14939 // More complex introducers.
14940 verifyFormat("return [i, args...] {};");
14941
14942 // Not lambdas.
14943 verifyFormat("constexpr char hello[]{\"hello\"};");
14944 verifyFormat("double &operator[](int i) { return 0; }\n"
14945 "int i;");
14946 verifyFormat("std::unique_ptr<int[]> foo() {}");
14947 verifyFormat("int i = a[a][a]->f();");
14948 verifyFormat("int i = (*b)[a]->f();");
14949
14950 // Other corner cases.
14951 verifyFormat("void f() {\n"
14952 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
14953 " );\n"
14954 "}");
14955
14956 // Lambdas created through weird macros.
14957 verifyFormat("void f() {\n"
14958 " MACRO((const AA &a) { return 1; });\n"
14959 " MACRO((AA &a) { return 1; });\n"
14960 "}");
14961
14962 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
14963 " doo_dah();\n"
14964 " doo_dah();\n"
14965 " })) {\n"
14966 "}");
14967 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
14968 " doo_dah();\n"
14969 " doo_dah();\n"
14970 " })) {\n"
14971 "}");
14972 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
14973 " doo_dah();\n"
14974 " doo_dah();\n"
14975 " })) {\n"
14976 "}");
14977 verifyFormat("auto lambda = []() {\n"
14978 " int a = 2\n"
14979 "#if A\n"
14980 " + 2\n"
14981 "#endif\n"
14982 " ;\n"
14983 "};");
14984
14985 // Lambdas with complex multiline introducers.
14986 verifyFormat(
14987 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14988 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
14989 " -> ::std::unordered_set<\n"
14990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
14991 " //\n"
14992 " });");
14993
14994 FormatStyle DoNotMerge = getLLVMStyle();
14995 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
14996 verifyFormat("auto c = []() {\n"
14997 " return b;\n"
14998 "};",
14999 "auto c = []() { return b; };", DoNotMerge);
15000 verifyFormat("auto c = []() {\n"
15001 "};",
15002 " auto c = []() {};", DoNotMerge);
15003
15004 FormatStyle MergeEmptyOnly = getLLVMStyle();
15005 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
15006 verifyFormat("auto c = []() {\n"
15007 " return b;\n"
15008 "};",
15009 "auto c = []() {\n"
15010 " return b;\n"
15011 " };",
15012 MergeEmptyOnly);
15013 verifyFormat("auto c = []() {};",
15014 "auto c = []() {\n"
15015 "};",
15016 MergeEmptyOnly);
15017
15018 FormatStyle MergeInline = getLLVMStyle();
15019 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
15020 verifyFormat("auto c = []() {\n"
15021 " return b;\n"
15022 "};",
15023 "auto c = []() { return b; };", MergeInline);
15024 verifyFormat("function([]() { return b; })", "function([]() { return b; })",
15025 MergeInline);
15026 verifyFormat("function([]() { return b; }, a)",
15027 "function([]() { return b; }, a)", MergeInline);
15028 verifyFormat("function(a, []() { return b; })",
15029 "function(a, []() { return b; })", MergeInline);
15030
15031 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
15032 // AllowShortLambdasOnASingleLine
15033 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
15034 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
15035 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
15036 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
15037 FormatStyle::ShortLambdaStyle::SLS_None;
15038 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
15039 " []()\n"
15040 " {\n"
15041 " return 17;\n"
15042 " });",
15043 LLVMWithBeforeLambdaBody);
15044 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
15045 " []()\n"
15046 " {\n"
15047 " });",
15048 LLVMWithBeforeLambdaBody);
15049 verifyFormat("auto fct_SLS_None = []()\n"
15050 "{\n"
15051 " return 17;\n"
15052 "};",
15053 LLVMWithBeforeLambdaBody);
15054 verifyFormat("TwoNestedLambdas_SLS_None(\n"
15055 " []()\n"
15056 " {\n"
15057 " return Call(\n"
15058 " []()\n"
15059 " {\n"
15060 " return 17;\n"
15061 " });\n"
15062 " });",
15063 LLVMWithBeforeLambdaBody);
15064 verifyFormat("void Fct()\n"
15065 "{\n"
15066 " return {[]()\n"
15067 " {\n"
15068 " return 17;\n"
15069 " }};\n"
15070 "}",
15071 LLVMWithBeforeLambdaBody);
15072
15073 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
15074 FormatStyle::ShortLambdaStyle::SLS_Empty;
15075 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
15076 " []()\n"
15077 " {\n"
15078 " return 17;\n"
15079 " });",
15080 LLVMWithBeforeLambdaBody);
15081 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
15082 LLVMWithBeforeLambdaBody);
15083 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
15084 "ongFunctionName_SLS_Empty(\n"
15085 " []() {});",
15086 LLVMWithBeforeLambdaBody);
15087 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
15088 " []()\n"
15089 " {\n"
15090 " return 17;\n"
15091 " });",
15092 LLVMWithBeforeLambdaBody);
15093 verifyFormat("auto fct_SLS_Empty = []()\n"
15094 "{\n"
15095 " return 17;\n"
15096 "};",
15097 LLVMWithBeforeLambdaBody);
15098 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
15099 " []()\n"
15100 " {\n"
15101 " return Call([]() {});\n"
15102 " });",
15103 LLVMWithBeforeLambdaBody);
15104 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
15105 " []()\n"
15106 " {\n"
15107 " return Call([]() {});\n"
15108 " });",
15109 LLVMWithBeforeLambdaBody);
15110 verifyFormat(
15111 "FctWithLongLineInLambda_SLS_Empty(\n"
15112 " []()\n"
15113 " {\n"
15114 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
15115 " AndShouldNotBeConsiderAsInline,\n"
15116 " LambdaBodyMustBeBreak);\n"
15117 " });",
15118 LLVMWithBeforeLambdaBody);
15119
15120 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
15121 FormatStyle::ShortLambdaStyle::SLS_Inline;
15122 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
15123 LLVMWithBeforeLambdaBody);
15124 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
15125 LLVMWithBeforeLambdaBody);
15126 verifyFormat("auto fct_SLS_Inline = []()\n"
15127 "{\n"
15128 " return 17;\n"
15129 "};",
15130 LLVMWithBeforeLambdaBody);
15131 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
15132 "17; }); });",
15133 LLVMWithBeforeLambdaBody);
15134 verifyFormat(
15135 "FctWithLongLineInLambda_SLS_Inline(\n"
15136 " []()\n"
15137 " {\n"
15138 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
15139 " AndShouldNotBeConsiderAsInline,\n"
15140 " LambdaBodyMustBeBreak);\n"
15141 " });",
15142 LLVMWithBeforeLambdaBody);
15143 verifyFormat("FctWithMultipleParams_SLS_Inline("
15144 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
15145 " []() { return 17; });",
15146 LLVMWithBeforeLambdaBody);
15147 verifyFormat(
15148 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
15149 LLVMWithBeforeLambdaBody);
15150
15151 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
15152 FormatStyle::ShortLambdaStyle::SLS_All;
15153 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
15154 LLVMWithBeforeLambdaBody);
15155 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
15156 LLVMWithBeforeLambdaBody);
15157 verifyFormat("auto fct_SLS_All = []() { return 17; };",
15158 LLVMWithBeforeLambdaBody);
15159 verifyFormat("FctWithOneParam_SLS_All(\n"
15160 " []()\n"
15161 " {\n"
15162 " // A cool function...\n"
15163 " return 43;\n"
15164 " });",
15165 LLVMWithBeforeLambdaBody);
15166 verifyFormat("FctWithMultipleParams_SLS_All("
15167 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
15168 " []() { return 17; });",
15169 LLVMWithBeforeLambdaBody);
15170 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
15171 LLVMWithBeforeLambdaBody);
15172 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
15173 LLVMWithBeforeLambdaBody);
15174 verifyFormat(
15175 "FctWithLongLineInLambda_SLS_All(\n"
15176 " []()\n"
15177 " {\n"
15178 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
15179 " AndShouldNotBeConsiderAsInline,\n"
15180 " LambdaBodyMustBeBreak);\n"
15181 " });",
15182 LLVMWithBeforeLambdaBody);
15183 verifyFormat(
15184 "auto fct_SLS_All = []()\n"
15185 "{\n"
15186 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
15187 " AndShouldNotBeConsiderAsInline,\n"
15188 " LambdaBodyMustBeBreak);\n"
15189 "};",
15190 LLVMWithBeforeLambdaBody);
15191 LLVMWithBeforeLambdaBody.BinPackParameters = false;
15192 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
15193 LLVMWithBeforeLambdaBody);
15194 verifyFormat(
15195 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
15196 " FirstParam,\n"
15197 " SecondParam,\n"
15198 " ThirdParam,\n"
15199 " FourthParam);",
15200 LLVMWithBeforeLambdaBody);
15201 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
15202 " []() { return "
15203 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
15204 " FirstParam,\n"
15205 " SecondParam,\n"
15206 " ThirdParam,\n"
15207 " FourthParam);",
15208 LLVMWithBeforeLambdaBody);
15209 verifyFormat(
15210 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
15211 " SecondParam,\n"
15212 " ThirdParam,\n"
15213 " FourthParam,\n"
15214 " []() { return SomeValueNotSoLong; });",
15215 LLVMWithBeforeLambdaBody);
15216 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
15217 " []()\n"
15218 " {\n"
15219 " return "
15220 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
15221 "eConsiderAsInline;\n"
15222 " });",
15223 LLVMWithBeforeLambdaBody);
15224 verifyFormat(
15225 "FctWithLongLineInLambda_SLS_All(\n"
15226 " []()\n"
15227 " {\n"
15228 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
15229 " AndShouldNotBeConsiderAsInline,\n"
15230 " LambdaBodyMustBeBreak);\n"
15231 " });",
15232 LLVMWithBeforeLambdaBody);
15233 verifyFormat("FctWithTwoParams_SLS_All(\n"
15234 " []()\n"
15235 " {\n"
15236 " // A cool function...\n"
15237 " return 43;\n"
15238 " },\n"
15239 " 87);",
15240 LLVMWithBeforeLambdaBody);
15241 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
15242 LLVMWithBeforeLambdaBody);
15243 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
15244 LLVMWithBeforeLambdaBody);
15245 verifyFormat(
15246 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
15247 LLVMWithBeforeLambdaBody);
15248 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
15249 "}); }, x);",
15250 LLVMWithBeforeLambdaBody);
15251 verifyFormat("TwoNestedLambdas_SLS_All(\n"
15252 " []()\n"
15253 " {\n"
15254 " // A cool function...\n"
15255 " return Call([]() { return 17; });\n"
15256 " });",
15257 LLVMWithBeforeLambdaBody);
15258 verifyFormat("TwoNestedLambdas_SLS_All(\n"
15259 " []()\n"
15260 " {\n"
15261 " return Call(\n"
15262 " []()\n"
15263 " {\n"
15264 " // A cool function...\n"
15265 " return 17;\n"
15266 " });\n"
15267 " });",
15268 LLVMWithBeforeLambdaBody);
15269 }
15270
TEST_F(FormatTest,LambdaWithLineComments)15271 TEST_F(FormatTest, LambdaWithLineComments) {
15272 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
15273 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
15274 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
15275 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
15276 FormatStyle::ShortLambdaStyle::SLS_All;
15277
15278 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
15279 verifyFormat("auto k = []() // comment\n"
15280 "{ return; }",
15281 LLVMWithBeforeLambdaBody);
15282 verifyFormat("auto k = []() /* comment */ { return; }",
15283 LLVMWithBeforeLambdaBody);
15284 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
15285 LLVMWithBeforeLambdaBody);
15286 verifyFormat("auto k = []() // X\n"
15287 "{ return; }",
15288 LLVMWithBeforeLambdaBody);
15289 verifyFormat(
15290 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
15291 "{ return; }",
15292 LLVMWithBeforeLambdaBody);
15293 }
15294
TEST_F(FormatTest,EmptyLinesInLambdas)15295 TEST_F(FormatTest, EmptyLinesInLambdas) {
15296 verifyFormat("auto lambda = []() {\n"
15297 " x(); //\n"
15298 "};",
15299 "auto lambda = []() {\n"
15300 "\n"
15301 " x(); //\n"
15302 "\n"
15303 "};");
15304 }
15305
TEST_F(FormatTest,FormatsBlocks)15306 TEST_F(FormatTest, FormatsBlocks) {
15307 FormatStyle ShortBlocks = getLLVMStyle();
15308 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
15309 verifyFormat("int (^Block)(int, int);", ShortBlocks);
15310 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
15311 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
15312 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
15313 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
15314 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
15315
15316 verifyFormat("foo(^{ bar(); });", ShortBlocks);
15317 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
15318 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
15319
15320 verifyFormat("[operation setCompletionBlock:^{\n"
15321 " [self onOperationDone];\n"
15322 "}];");
15323 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
15324 " [self onOperationDone];\n"
15325 "}]};");
15326 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
15327 " f();\n"
15328 "}];");
15329 verifyFormat("int a = [operation block:^int(int *i) {\n"
15330 " return 1;\n"
15331 "}];");
15332 verifyFormat("[myObject doSomethingWith:arg1\n"
15333 " aaa:^int(int *a) {\n"
15334 " return 1;\n"
15335 " }\n"
15336 " bbb:f(a * bbbbbbbb)];");
15337
15338 verifyFormat("[operation setCompletionBlock:^{\n"
15339 " [self.delegate newDataAvailable];\n"
15340 "}];",
15341 getLLVMStyleWithColumns(60));
15342 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
15343 " NSString *path = [self sessionFilePath];\n"
15344 " if (path) {\n"
15345 " // ...\n"
15346 " }\n"
15347 "});");
15348 verifyFormat("[[SessionService sharedService]\n"
15349 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
15350 " if (window) {\n"
15351 " [self windowDidLoad:window];\n"
15352 " } else {\n"
15353 " [self errorLoadingWindow];\n"
15354 " }\n"
15355 " }];");
15356 verifyFormat("void (^largeBlock)(void) = ^{\n"
15357 " // ...\n"
15358 "};\n",
15359 getLLVMStyleWithColumns(40));
15360 verifyFormat("[[SessionService sharedService]\n"
15361 " loadWindowWithCompletionBlock: //\n"
15362 " ^(SessionWindow *window) {\n"
15363 " if (window) {\n"
15364 " [self windowDidLoad:window];\n"
15365 " } else {\n"
15366 " [self errorLoadingWindow];\n"
15367 " }\n"
15368 " }];",
15369 getLLVMStyleWithColumns(60));
15370 verifyFormat("[myObject doSomethingWith:arg1\n"
15371 " firstBlock:^(Foo *a) {\n"
15372 " // ...\n"
15373 " int i;\n"
15374 " }\n"
15375 " secondBlock:^(Bar *b) {\n"
15376 " // ...\n"
15377 " int i;\n"
15378 " }\n"
15379 " thirdBlock:^Foo(Bar *b) {\n"
15380 " // ...\n"
15381 " int i;\n"
15382 " }];");
15383 verifyFormat("[myObject doSomethingWith:arg1\n"
15384 " firstBlock:-1\n"
15385 " secondBlock:^(Bar *b) {\n"
15386 " // ...\n"
15387 " int i;\n"
15388 " }];");
15389
15390 verifyFormat("f(^{\n"
15391 " @autoreleasepool {\n"
15392 " if (a) {\n"
15393 " g();\n"
15394 " }\n"
15395 " }\n"
15396 "});");
15397 verifyFormat("Block b = ^int *(A *a, B *b) {}");
15398 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
15399 "};");
15400
15401 FormatStyle FourIndent = getLLVMStyle();
15402 FourIndent.ObjCBlockIndentWidth = 4;
15403 verifyFormat("[operation setCompletionBlock:^{\n"
15404 " [self onOperationDone];\n"
15405 "}];",
15406 FourIndent);
15407 }
15408
TEST_F(FormatTest,FormatsBlocksWithZeroColumnWidth)15409 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
15410 FormatStyle ZeroColumn = getLLVMStyle();
15411 ZeroColumn.ColumnLimit = 0;
15412
15413 verifyFormat("[[SessionService sharedService] "
15414 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
15415 " if (window) {\n"
15416 " [self windowDidLoad:window];\n"
15417 " } else {\n"
15418 " [self errorLoadingWindow];\n"
15419 " }\n"
15420 "}];",
15421 ZeroColumn);
15422 EXPECT_EQ("[[SessionService sharedService]\n"
15423 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
15424 " if (window) {\n"
15425 " [self windowDidLoad:window];\n"
15426 " } else {\n"
15427 " [self errorLoadingWindow];\n"
15428 " }\n"
15429 " }];",
15430 format("[[SessionService sharedService]\n"
15431 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
15432 " if (window) {\n"
15433 " [self windowDidLoad:window];\n"
15434 " } else {\n"
15435 " [self errorLoadingWindow];\n"
15436 " }\n"
15437 "}];",
15438 ZeroColumn));
15439 verifyFormat("[myObject doSomethingWith:arg1\n"
15440 " firstBlock:^(Foo *a) {\n"
15441 " // ...\n"
15442 " int i;\n"
15443 " }\n"
15444 " secondBlock:^(Bar *b) {\n"
15445 " // ...\n"
15446 " int i;\n"
15447 " }\n"
15448 " thirdBlock:^Foo(Bar *b) {\n"
15449 " // ...\n"
15450 " int i;\n"
15451 " }];",
15452 ZeroColumn);
15453 verifyFormat("f(^{\n"
15454 " @autoreleasepool {\n"
15455 " if (a) {\n"
15456 " g();\n"
15457 " }\n"
15458 " }\n"
15459 "});",
15460 ZeroColumn);
15461 verifyFormat("void (^largeBlock)(void) = ^{\n"
15462 " // ...\n"
15463 "};",
15464 ZeroColumn);
15465
15466 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
15467 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
15468 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
15469 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
15470 EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
15471 " int i;\n"
15472 "};",
15473 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
15474 }
15475
TEST_F(FormatTest,SupportsCRLF)15476 TEST_F(FormatTest, SupportsCRLF) {
15477 EXPECT_EQ("int a;\r\n"
15478 "int b;\r\n"
15479 "int c;\r\n",
15480 format("int a;\r\n"
15481 " int b;\r\n"
15482 " int c;\r\n",
15483 getLLVMStyle()));
15484 EXPECT_EQ("int a;\r\n"
15485 "int b;\r\n"
15486 "int c;\r\n",
15487 format("int a;\r\n"
15488 " int b;\n"
15489 " int c;\r\n",
15490 getLLVMStyle()));
15491 EXPECT_EQ("int a;\n"
15492 "int b;\n"
15493 "int c;\n",
15494 format("int a;\r\n"
15495 " int b;\n"
15496 " int c;\n",
15497 getLLVMStyle()));
15498 EXPECT_EQ("\"aaaaaaa \"\r\n"
15499 "\"bbbbbbb\";\r\n",
15500 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
15501 EXPECT_EQ("#define A \\\r\n"
15502 " b; \\\r\n"
15503 " c; \\\r\n"
15504 " d;\r\n",
15505 format("#define A \\\r\n"
15506 " b; \\\r\n"
15507 " c; d; \r\n",
15508 getGoogleStyle()));
15509
15510 EXPECT_EQ("/*\r\n"
15511 "multi line block comments\r\n"
15512 "should not introduce\r\n"
15513 "an extra carriage return\r\n"
15514 "*/\r\n",
15515 format("/*\r\n"
15516 "multi line block comments\r\n"
15517 "should not introduce\r\n"
15518 "an extra carriage return\r\n"
15519 "*/\r\n"));
15520 EXPECT_EQ("/*\r\n"
15521 "\r\n"
15522 "*/",
15523 format("/*\r\n"
15524 " \r\r\r\n"
15525 "*/"));
15526
15527 FormatStyle style = getLLVMStyle();
15528
15529 style.DeriveLineEnding = true;
15530 style.UseCRLF = false;
15531 EXPECT_EQ("union FooBarBazQux {\n"
15532 " int foo;\n"
15533 " int bar;\n"
15534 " int baz;\n"
15535 "};",
15536 format("union FooBarBazQux {\r\n"
15537 " int foo;\n"
15538 " int bar;\r\n"
15539 " int baz;\n"
15540 "};",
15541 style));
15542 style.UseCRLF = true;
15543 EXPECT_EQ("union FooBarBazQux {\r\n"
15544 " int foo;\r\n"
15545 " int bar;\r\n"
15546 " int baz;\r\n"
15547 "};",
15548 format("union FooBarBazQux {\r\n"
15549 " int foo;\n"
15550 " int bar;\r\n"
15551 " int baz;\n"
15552 "};",
15553 style));
15554
15555 style.DeriveLineEnding = false;
15556 style.UseCRLF = false;
15557 EXPECT_EQ("union FooBarBazQux {\n"
15558 " int foo;\n"
15559 " int bar;\n"
15560 " int baz;\n"
15561 " int qux;\n"
15562 "};",
15563 format("union FooBarBazQux {\r\n"
15564 " int foo;\n"
15565 " int bar;\r\n"
15566 " int baz;\n"
15567 " int qux;\r\n"
15568 "};",
15569 style));
15570 style.UseCRLF = true;
15571 EXPECT_EQ("union FooBarBazQux {\r\n"
15572 " int foo;\r\n"
15573 " int bar;\r\n"
15574 " int baz;\r\n"
15575 " int qux;\r\n"
15576 "};",
15577 format("union FooBarBazQux {\r\n"
15578 " int foo;\n"
15579 " int bar;\r\n"
15580 " int baz;\n"
15581 " int qux;\n"
15582 "};",
15583 style));
15584
15585 style.DeriveLineEnding = true;
15586 style.UseCRLF = false;
15587 EXPECT_EQ("union FooBarBazQux {\r\n"
15588 " int foo;\r\n"
15589 " int bar;\r\n"
15590 " int baz;\r\n"
15591 " int qux;\r\n"
15592 "};",
15593 format("union FooBarBazQux {\r\n"
15594 " int foo;\n"
15595 " int bar;\r\n"
15596 " int baz;\n"
15597 " int qux;\r\n"
15598 "};",
15599 style));
15600 style.UseCRLF = true;
15601 EXPECT_EQ("union FooBarBazQux {\n"
15602 " int foo;\n"
15603 " int bar;\n"
15604 " int baz;\n"
15605 " int qux;\n"
15606 "};",
15607 format("union FooBarBazQux {\r\n"
15608 " int foo;\n"
15609 " int bar;\r\n"
15610 " int baz;\n"
15611 " int qux;\n"
15612 "};",
15613 style));
15614 }
15615
TEST_F(FormatTest,MunchSemicolonAfterBlocks)15616 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
15617 verifyFormat("MY_CLASS(C) {\n"
15618 " int i;\n"
15619 " int j;\n"
15620 "};");
15621 }
15622
TEST_F(FormatTest,ConfigurableContinuationIndentWidth)15623 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
15624 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
15625 TwoIndent.ContinuationIndentWidth = 2;
15626
15627 EXPECT_EQ("int i =\n"
15628 " longFunction(\n"
15629 " arg);",
15630 format("int i = longFunction(arg);", TwoIndent));
15631
15632 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
15633 SixIndent.ContinuationIndentWidth = 6;
15634
15635 EXPECT_EQ("int i =\n"
15636 " longFunction(\n"
15637 " arg);",
15638 format("int i = longFunction(arg);", SixIndent));
15639 }
15640
TEST_F(FormatTest,WrappedClosingParenthesisIndent)15641 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
15642 FormatStyle Style = getLLVMStyle();
15643 verifyFormat("int Foo::getter(\n"
15644 " //\n"
15645 ") const {\n"
15646 " return foo;\n"
15647 "}",
15648 Style);
15649 verifyFormat("void Foo::setter(\n"
15650 " //\n"
15651 ") {\n"
15652 " foo = 1;\n"
15653 "}",
15654 Style);
15655 }
15656
TEST_F(FormatTest,SpacesInAngles)15657 TEST_F(FormatTest, SpacesInAngles) {
15658 FormatStyle Spaces = getLLVMStyle();
15659 Spaces.SpacesInAngles = true;
15660
15661 verifyFormat("static_cast< int >(arg);", Spaces);
15662 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
15663 verifyFormat("f< int, float >();", Spaces);
15664 verifyFormat("template <> g() {}", Spaces);
15665 verifyFormat("template < std::vector< int > > f() {}", Spaces);
15666 verifyFormat("std::function< void(int, int) > fct;", Spaces);
15667 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
15668 Spaces);
15669
15670 Spaces.Standard = FormatStyle::LS_Cpp03;
15671 Spaces.SpacesInAngles = true;
15672 verifyFormat("A< A< int > >();", Spaces);
15673
15674 Spaces.SpacesInAngles = false;
15675 verifyFormat("A<A<int> >();", Spaces);
15676
15677 Spaces.Standard = FormatStyle::LS_Cpp11;
15678 Spaces.SpacesInAngles = true;
15679 verifyFormat("A< A< int > >();", Spaces);
15680
15681 Spaces.SpacesInAngles = false;
15682 verifyFormat("A<A<int>>();", Spaces);
15683 }
15684
TEST_F(FormatTest,SpaceAfterTemplateKeyword)15685 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
15686 FormatStyle Style = getLLVMStyle();
15687 Style.SpaceAfterTemplateKeyword = false;
15688 verifyFormat("template<int> void foo();", Style);
15689 }
15690
TEST_F(FormatTest,TripleAngleBrackets)15691 TEST_F(FormatTest, TripleAngleBrackets) {
15692 verifyFormat("f<<<1, 1>>>();");
15693 verifyFormat("f<<<1, 1, 1, s>>>();");
15694 verifyFormat("f<<<a, b, c, d>>>();");
15695 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
15696 verifyFormat("f<param><<<1, 1>>>();");
15697 verifyFormat("f<1><<<1, 1>>>();");
15698 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
15699 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15700 "aaaaaaaaaaa<<<\n 1, 1>>>();");
15701 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
15702 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
15703 }
15704
TEST_F(FormatTest,MergeLessLessAtEnd)15705 TEST_F(FormatTest, MergeLessLessAtEnd) {
15706 verifyFormat("<<");
15707 EXPECT_EQ("< < <", format("\\\n<<<"));
15708 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15709 "aaallvm::outs() <<");
15710 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15711 "aaaallvm::outs()\n <<");
15712 }
15713
TEST_F(FormatTest,HandleUnbalancedImplicitBracesAcrossPPBranches)15714 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
15715 std::string code = "#if A\n"
15716 "#if B\n"
15717 "a.\n"
15718 "#endif\n"
15719 " a = 1;\n"
15720 "#else\n"
15721 "#endif\n"
15722 "#if C\n"
15723 "#else\n"
15724 "#endif\n";
15725 EXPECT_EQ(code, format(code));
15726 }
15727
TEST_F(FormatTest,HandleConflictMarkers)15728 TEST_F(FormatTest, HandleConflictMarkers) {
15729 // Git/SVN conflict markers.
15730 EXPECT_EQ("int a;\n"
15731 "void f() {\n"
15732 " callme(some(parameter1,\n"
15733 "<<<<<<< text by the vcs\n"
15734 " parameter2),\n"
15735 "||||||| text by the vcs\n"
15736 " parameter2),\n"
15737 " parameter3,\n"
15738 "======= text by the vcs\n"
15739 " parameter2, parameter3),\n"
15740 ">>>>>>> text by the vcs\n"
15741 " otherparameter);\n",
15742 format("int a;\n"
15743 "void f() {\n"
15744 " callme(some(parameter1,\n"
15745 "<<<<<<< text by the vcs\n"
15746 " parameter2),\n"
15747 "||||||| text by the vcs\n"
15748 " parameter2),\n"
15749 " parameter3,\n"
15750 "======= text by the vcs\n"
15751 " parameter2,\n"
15752 " parameter3),\n"
15753 ">>>>>>> text by the vcs\n"
15754 " otherparameter);\n"));
15755
15756 // Perforce markers.
15757 EXPECT_EQ("void f() {\n"
15758 " function(\n"
15759 ">>>> text by the vcs\n"
15760 " parameter,\n"
15761 "==== text by the vcs\n"
15762 " parameter,\n"
15763 "==== text by the vcs\n"
15764 " parameter,\n"
15765 "<<<< text by the vcs\n"
15766 " parameter);\n",
15767 format("void f() {\n"
15768 " function(\n"
15769 ">>>> text by the vcs\n"
15770 " parameter,\n"
15771 "==== text by the vcs\n"
15772 " parameter,\n"
15773 "==== text by the vcs\n"
15774 " parameter,\n"
15775 "<<<< text by the vcs\n"
15776 " parameter);\n"));
15777
15778 EXPECT_EQ("<<<<<<<\n"
15779 "|||||||\n"
15780 "=======\n"
15781 ">>>>>>>",
15782 format("<<<<<<<\n"
15783 "|||||||\n"
15784 "=======\n"
15785 ">>>>>>>"));
15786
15787 EXPECT_EQ("<<<<<<<\n"
15788 "|||||||\n"
15789 "int i;\n"
15790 "=======\n"
15791 ">>>>>>>",
15792 format("<<<<<<<\n"
15793 "|||||||\n"
15794 "int i;\n"
15795 "=======\n"
15796 ">>>>>>>"));
15797
15798 // FIXME: Handle parsing of macros around conflict markers correctly:
15799 EXPECT_EQ("#define Macro \\\n"
15800 "<<<<<<<\n"
15801 "Something \\\n"
15802 "|||||||\n"
15803 "Else \\\n"
15804 "=======\n"
15805 "Other \\\n"
15806 ">>>>>>>\n"
15807 " End int i;\n",
15808 format("#define Macro \\\n"
15809 "<<<<<<<\n"
15810 " Something \\\n"
15811 "|||||||\n"
15812 " Else \\\n"
15813 "=======\n"
15814 " Other \\\n"
15815 ">>>>>>>\n"
15816 " End\n"
15817 "int i;\n"));
15818 }
15819
TEST_F(FormatTest,DisableRegions)15820 TEST_F(FormatTest, DisableRegions) {
15821 EXPECT_EQ("int i;\n"
15822 "// clang-format off\n"
15823 " int j;\n"
15824 "// clang-format on\n"
15825 "int k;",
15826 format(" int i;\n"
15827 " // clang-format off\n"
15828 " int j;\n"
15829 " // clang-format on\n"
15830 " int k;"));
15831 EXPECT_EQ("int i;\n"
15832 "/* clang-format off */\n"
15833 " int j;\n"
15834 "/* clang-format on */\n"
15835 "int k;",
15836 format(" int i;\n"
15837 " /* clang-format off */\n"
15838 " int j;\n"
15839 " /* clang-format on */\n"
15840 " int k;"));
15841
15842 // Don't reflow comments within disabled regions.
15843 EXPECT_EQ("// clang-format off\n"
15844 "// long long long long long long line\n"
15845 "/* clang-format on */\n"
15846 "/* long long long\n"
15847 " * long long long\n"
15848 " * line */\n"
15849 "int i;\n"
15850 "/* clang-format off */\n"
15851 "/* long long long long long long line */\n",
15852 format("// clang-format off\n"
15853 "// long long long long long long line\n"
15854 "/* clang-format on */\n"
15855 "/* long long long long long long line */\n"
15856 "int i;\n"
15857 "/* clang-format off */\n"
15858 "/* long long long long long long line */\n",
15859 getLLVMStyleWithColumns(20)));
15860 }
15861
TEST_F(FormatTest,DoNotCrashOnInvalidInput)15862 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
15863 format("? ) =");
15864 verifyNoCrash("#define a\\\n /**/}");
15865 }
15866
TEST_F(FormatTest,FormatsTableGenCode)15867 TEST_F(FormatTest, FormatsTableGenCode) {
15868 FormatStyle Style = getLLVMStyle();
15869 Style.Language = FormatStyle::LK_TableGen;
15870 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
15871 }
15872
TEST_F(FormatTest,ArrayOfTemplates)15873 TEST_F(FormatTest, ArrayOfTemplates) {
15874 EXPECT_EQ("auto a = new unique_ptr<int>[10];",
15875 format("auto a = new unique_ptr<int > [ 10];"));
15876
15877 FormatStyle Spaces = getLLVMStyle();
15878 Spaces.SpacesInSquareBrackets = true;
15879 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
15880 format("auto a = new unique_ptr<int > [10];", Spaces));
15881 }
15882
TEST_F(FormatTest,ArrayAsTemplateType)15883 TEST_F(FormatTest, ArrayAsTemplateType) {
15884 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
15885 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
15886
15887 FormatStyle Spaces = getLLVMStyle();
15888 Spaces.SpacesInSquareBrackets = true;
15889 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
15890 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
15891 }
15892
TEST_F(FormatTest,NoSpaceAfterSuper)15893 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
15894
TEST(FormatStyle,GetStyleWithEmptyFileName)15895 TEST(FormatStyle, GetStyleWithEmptyFileName) {
15896 llvm::vfs::InMemoryFileSystem FS;
15897 auto Style1 = getStyle("file", "", "Google", "", &FS);
15898 ASSERT_TRUE((bool)Style1);
15899 ASSERT_EQ(*Style1, getGoogleStyle());
15900 }
15901
TEST(FormatStyle,GetStyleOfFile)15902 TEST(FormatStyle, GetStyleOfFile) {
15903 llvm::vfs::InMemoryFileSystem FS;
15904 // Test 1: format file in the same directory.
15905 ASSERT_TRUE(
15906 FS.addFile("/a/.clang-format", 0,
15907 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
15908 ASSERT_TRUE(
15909 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
15910 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
15911 ASSERT_TRUE((bool)Style1);
15912 ASSERT_EQ(*Style1, getLLVMStyle());
15913
15914 // Test 2.1: fallback to default.
15915 ASSERT_TRUE(
15916 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
15917 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
15918 ASSERT_TRUE((bool)Style2);
15919 ASSERT_EQ(*Style2, getMozillaStyle());
15920
15921 // Test 2.2: no format on 'none' fallback style.
15922 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
15923 ASSERT_TRUE((bool)Style2);
15924 ASSERT_EQ(*Style2, getNoStyle());
15925
15926 // Test 2.3: format if config is found with no based style while fallback is
15927 // 'none'.
15928 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
15929 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
15930 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
15931 ASSERT_TRUE((bool)Style2);
15932 ASSERT_EQ(*Style2, getLLVMStyle());
15933
15934 // Test 2.4: format if yaml with no based style, while fallback is 'none'.
15935 Style2 = getStyle("{}", "a.h", "none", "", &FS);
15936 ASSERT_TRUE((bool)Style2);
15937 ASSERT_EQ(*Style2, getLLVMStyle());
15938
15939 // Test 3: format file in parent directory.
15940 ASSERT_TRUE(
15941 FS.addFile("/c/.clang-format", 0,
15942 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
15943 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
15944 llvm::MemoryBuffer::getMemBuffer("int i;")));
15945 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
15946 ASSERT_TRUE((bool)Style3);
15947 ASSERT_EQ(*Style3, getGoogleStyle());
15948
15949 // Test 4: error on invalid fallback style
15950 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
15951 ASSERT_FALSE((bool)Style4);
15952 llvm::consumeError(Style4.takeError());
15953
15954 // Test 5: error on invalid yaml on command line
15955 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
15956 ASSERT_FALSE((bool)Style5);
15957 llvm::consumeError(Style5.takeError());
15958
15959 // Test 6: error on invalid style
15960 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
15961 ASSERT_FALSE((bool)Style6);
15962 llvm::consumeError(Style6.takeError());
15963
15964 // Test 7: found config file, error on parsing it
15965 ASSERT_TRUE(
15966 FS.addFile("/d/.clang-format", 0,
15967 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
15968 "InvalidKey: InvalidValue")));
15969 ASSERT_TRUE(
15970 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
15971 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
15972 ASSERT_FALSE((bool)Style7);
15973 llvm::consumeError(Style7.takeError());
15974
15975 // Test 8: inferred per-language defaults apply.
15976 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS);
15977 ASSERT_TRUE((bool)StyleTd);
15978 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen));
15979 }
15980
TEST_F(ReplacementTest,FormatCodeAfterReplacements)15981 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
15982 // Column limit is 20.
15983 std::string Code = "Type *a =\n"
15984 " new Type();\n"
15985 "g(iiiii, 0, jjjjj,\n"
15986 " 0, kkkkk, 0, mm);\n"
15987 "int bad = format ;";
15988 std::string Expected = "auto a = new Type();\n"
15989 "g(iiiii, nullptr,\n"
15990 " jjjjj, nullptr,\n"
15991 " kkkkk, nullptr,\n"
15992 " mm);\n"
15993 "int bad = format ;";
15994 FileID ID = Context.createInMemoryFile("format.cpp", Code);
15995 tooling::Replacements Replaces = toReplacements(
15996 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
15997 "auto "),
15998 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
15999 "nullptr"),
16000 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
16001 "nullptr"),
16002 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
16003 "nullptr")});
16004
16005 format::FormatStyle Style = format::getLLVMStyle();
16006 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
16007 auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
16008 EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
16009 << llvm::toString(FormattedReplaces.takeError()) << "\n";
16010 auto Result = applyAllReplacements(Code, *FormattedReplaces);
16011 EXPECT_TRUE(static_cast<bool>(Result));
16012 EXPECT_EQ(Expected, *Result);
16013 }
16014
TEST_F(ReplacementTest,SortIncludesAfterReplacement)16015 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
16016 std::string Code = "#include \"a.h\"\n"
16017 "#include \"c.h\"\n"
16018 "\n"
16019 "int main() {\n"
16020 " return 0;\n"
16021 "}";
16022 std::string Expected = "#include \"a.h\"\n"
16023 "#include \"b.h\"\n"
16024 "#include \"c.h\"\n"
16025 "\n"
16026 "int main() {\n"
16027 " return 0;\n"
16028 "}";
16029 FileID ID = Context.createInMemoryFile("fix.cpp", Code);
16030 tooling::Replacements Replaces = toReplacements(
16031 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
16032 "#include \"b.h\"\n")});
16033
16034 format::FormatStyle Style = format::getLLVMStyle();
16035 Style.SortIncludes = true;
16036 auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
16037 EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
16038 << llvm::toString(FormattedReplaces.takeError()) << "\n";
16039 auto Result = applyAllReplacements(Code, *FormattedReplaces);
16040 EXPECT_TRUE(static_cast<bool>(Result));
16041 EXPECT_EQ(Expected, *Result);
16042 }
16043
TEST_F(FormatTest,FormatSortsUsingDeclarations)16044 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
16045 EXPECT_EQ("using std::cin;\n"
16046 "using std::cout;",
16047 format("using std::cout;\n"
16048 "using std::cin;",
16049 getGoogleStyle()));
16050 }
16051
TEST_F(FormatTest,UTF8CharacterLiteralCpp03)16052 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
16053 format::FormatStyle Style = format::getLLVMStyle();
16054 Style.Standard = FormatStyle::LS_Cpp03;
16055 // cpp03 recognize this string as identifier u8 and literal character 'a'
16056 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
16057 }
16058
TEST_F(FormatTest,UTF8CharacterLiteralCpp11)16059 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
16060 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
16061 // all modes, including C++11, C++14 and C++17
16062 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
16063 }
16064
TEST_F(FormatTest,DoNotFormatLikelyXml)16065 TEST_F(FormatTest, DoNotFormatLikelyXml) {
16066 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
16067 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
16068 }
16069
TEST_F(FormatTest,StructuredBindings)16070 TEST_F(FormatTest, StructuredBindings) {
16071 // Structured bindings is a C++17 feature.
16072 // all modes, including C++11, C++14 and C++17
16073 verifyFormat("auto [a, b] = f();");
16074 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
16075 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();"));
16076 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();"));
16077 EXPECT_EQ("auto const volatile [a, b] = f();",
16078 format("auto const volatile[a, b] = f();"));
16079 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();"));
16080 EXPECT_EQ("auto &[a, b, c] = f();",
16081 format("auto &[ a , b,c ] = f();"));
16082 EXPECT_EQ("auto &&[a, b, c] = f();",
16083 format("auto &&[ a , b,c ] = f();"));
16084 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();"));
16085 EXPECT_EQ("auto const volatile &&[a, b] = f();",
16086 format("auto const volatile &&[a, b] = f();"));
16087 EXPECT_EQ("auto const &&[a, b] = f();",
16088 format("auto const && [a, b] = f();"));
16089 EXPECT_EQ("const auto &[a, b] = f();",
16090 format("const auto & [a, b] = f();"));
16091 EXPECT_EQ("const auto volatile &&[a, b] = f();",
16092 format("const auto volatile &&[a, b] = f();"));
16093 EXPECT_EQ("volatile const auto &&[a, b] = f();",
16094 format("volatile const auto &&[a, b] = f();"));
16095 EXPECT_EQ("const auto &&[a, b] = f();",
16096 format("const auto && [a, b] = f();"));
16097
16098 // Make sure we don't mistake structured bindings for lambdas.
16099 FormatStyle PointerMiddle = getLLVMStyle();
16100 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
16101 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
16102 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
16103 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
16104 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
16105 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
16106 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
16107 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
16108 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
16109 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
16110 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
16111 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
16112 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
16113
16114 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
16115 format("for (const auto && [a, b] : some_range) {\n}"));
16116 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
16117 format("for (const auto & [a, b] : some_range) {\n}"));
16118 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
16119 format("for (const auto[a, b] : some_range) {\n}"));
16120 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);"));
16121 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);"));
16122 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);"));
16123 EXPECT_EQ("auto const &[x, y](expr);",
16124 format("auto const & [x,y] (expr);"));
16125 EXPECT_EQ("auto const &&[x, y](expr);",
16126 format("auto const && [x,y] (expr);"));
16127 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};"));
16128 EXPECT_EQ("auto const &[x, y]{expr};",
16129 format("auto const & [x,y] {expr};"));
16130 EXPECT_EQ("auto const &&[x, y]{expr};",
16131 format("auto const && [x,y] {expr};"));
16132
16133 format::FormatStyle Spaces = format::getLLVMStyle();
16134 Spaces.SpacesInSquareBrackets = true;
16135 verifyFormat("auto [ a, b ] = f();", Spaces);
16136 verifyFormat("auto &&[ a, b ] = f();", Spaces);
16137 verifyFormat("auto &[ a, b ] = f();", Spaces);
16138 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
16139 verifyFormat("auto const &[ a, b ] = f();", Spaces);
16140 }
16141
TEST_F(FormatTest,FileAndCode)16142 TEST_F(FormatTest, FileAndCode) {
16143 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
16144 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
16145 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
16146 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
16147 EXPECT_EQ(FormatStyle::LK_ObjC,
16148 guessLanguage("foo.h", "@interface Foo\n@end\n"));
16149 EXPECT_EQ(
16150 FormatStyle::LK_ObjC,
16151 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
16152 EXPECT_EQ(FormatStyle::LK_ObjC,
16153 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
16154 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
16155 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
16156 EXPECT_EQ(FormatStyle::LK_ObjC,
16157 guessLanguage("foo", "@interface Foo\n@end\n"));
16158 EXPECT_EQ(FormatStyle::LK_ObjC,
16159 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
16160 EXPECT_EQ(
16161 FormatStyle::LK_ObjC,
16162 guessLanguage("foo.h",
16163 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n"));
16164 EXPECT_EQ(
16165 FormatStyle::LK_Cpp,
16166 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
16167 }
16168
TEST_F(FormatTest,GuessLanguageWithCpp11AttributeSpecifiers)16169 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
16170 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
16171 EXPECT_EQ(FormatStyle::LK_ObjC,
16172 guessLanguage("foo.h", "array[[calculator getIndex]];"));
16173 EXPECT_EQ(FormatStyle::LK_Cpp,
16174 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
16175 EXPECT_EQ(
16176 FormatStyle::LK_Cpp,
16177 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
16178 EXPECT_EQ(FormatStyle::LK_ObjC,
16179 guessLanguage("foo.h", "[[noreturn foo] bar];"));
16180 EXPECT_EQ(FormatStyle::LK_Cpp,
16181 guessLanguage("foo.h", "[[clang::fallthrough]];"));
16182 EXPECT_EQ(FormatStyle::LK_ObjC,
16183 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
16184 EXPECT_EQ(FormatStyle::LK_Cpp,
16185 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
16186 EXPECT_EQ(FormatStyle::LK_Cpp,
16187 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
16188 EXPECT_EQ(FormatStyle::LK_ObjC,
16189 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
16190 EXPECT_EQ(FormatStyle::LK_Cpp,
16191 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
16192 EXPECT_EQ(
16193 FormatStyle::LK_Cpp,
16194 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
16195 EXPECT_EQ(
16196 FormatStyle::LK_Cpp,
16197 guessLanguage("foo.h",
16198 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
16199 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
16200 }
16201
TEST_F(FormatTest,GuessLanguageWithCaret)16202 TEST_F(FormatTest, GuessLanguageWithCaret) {
16203 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
16204 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
16205 EXPECT_EQ(FormatStyle::LK_ObjC,
16206 guessLanguage("foo.h", "int(^)(char, float);"));
16207 EXPECT_EQ(FormatStyle::LK_ObjC,
16208 guessLanguage("foo.h", "int(^foo)(char, float);"));
16209 EXPECT_EQ(FormatStyle::LK_ObjC,
16210 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
16211 EXPECT_EQ(FormatStyle::LK_ObjC,
16212 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
16213 EXPECT_EQ(
16214 FormatStyle::LK_ObjC,
16215 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
16216 }
16217
TEST_F(FormatTest,FormatsInlineAsmSymbolicNames)16218 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
16219 // ASM symbolic names are identifiers that must be surrounded by [] without
16220 // space in between:
16221 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
16222
16223 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
16224 verifyFormat(R"(//
16225 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
16226 )");
16227
16228 // A list of several ASM symbolic names.
16229 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
16230
16231 // ASM symbolic names in inline ASM with inputs and outputs.
16232 verifyFormat(R"(//
16233 asm("cmoveq %1, %2, %[result]"
16234 : [result] "=r"(result)
16235 : "r"(test), "r"(new), "[result]"(old));
16236 )");
16237
16238 // ASM symbolic names in inline ASM with no outputs.
16239 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
16240 }
16241
TEST_F(FormatTest,GuessedLanguageWithInlineAsmClobbers)16242 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
16243 EXPECT_EQ(FormatStyle::LK_Cpp,
16244 guessLanguage("foo.h", "void f() {\n"
16245 " asm (\"mov %[e], %[d]\"\n"
16246 " : [d] \"=rm\" (d)\n"
16247 " [e] \"rm\" (*e));\n"
16248 "}"));
16249 EXPECT_EQ(FormatStyle::LK_Cpp,
16250 guessLanguage("foo.h", "void f() {\n"
16251 " _asm (\"mov %[e], %[d]\"\n"
16252 " : [d] \"=rm\" (d)\n"
16253 " [e] \"rm\" (*e));\n"
16254 "}"));
16255 EXPECT_EQ(FormatStyle::LK_Cpp,
16256 guessLanguage("foo.h", "void f() {\n"
16257 " __asm (\"mov %[e], %[d]\"\n"
16258 " : [d] \"=rm\" (d)\n"
16259 " [e] \"rm\" (*e));\n"
16260 "}"));
16261 EXPECT_EQ(FormatStyle::LK_Cpp,
16262 guessLanguage("foo.h", "void f() {\n"
16263 " __asm__ (\"mov %[e], %[d]\"\n"
16264 " : [d] \"=rm\" (d)\n"
16265 " [e] \"rm\" (*e));\n"
16266 "}"));
16267 EXPECT_EQ(FormatStyle::LK_Cpp,
16268 guessLanguage("foo.h", "void f() {\n"
16269 " asm (\"mov %[e], %[d]\"\n"
16270 " : [d] \"=rm\" (d),\n"
16271 " [e] \"rm\" (*e));\n"
16272 "}"));
16273 EXPECT_EQ(FormatStyle::LK_Cpp,
16274 guessLanguage("foo.h", "void f() {\n"
16275 " asm volatile (\"mov %[e], %[d]\"\n"
16276 " : [d] \"=rm\" (d)\n"
16277 " [e] \"rm\" (*e));\n"
16278 "}"));
16279 }
16280
TEST_F(FormatTest,GuessLanguageWithChildLines)16281 TEST_F(FormatTest, GuessLanguageWithChildLines) {
16282 EXPECT_EQ(FormatStyle::LK_Cpp,
16283 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
16284 EXPECT_EQ(FormatStyle::LK_ObjC,
16285 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
16286 EXPECT_EQ(
16287 FormatStyle::LK_Cpp,
16288 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
16289 EXPECT_EQ(
16290 FormatStyle::LK_ObjC,
16291 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
16292 }
16293
TEST_F(FormatTest,TypenameMacros)16294 TEST_F(FormatTest, TypenameMacros) {
16295 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
16296
16297 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
16298 FormatStyle Google = getGoogleStyleWithColumns(0);
16299 Google.TypenameMacros = TypenameMacros;
16300 verifyFormat("struct foo {\n"
16301 " int bar;\n"
16302 " TAILQ_ENTRY(a) bleh;\n"
16303 "};",
16304 Google);
16305
16306 FormatStyle Macros = getLLVMStyle();
16307 Macros.TypenameMacros = TypenameMacros;
16308
16309 verifyFormat("STACK_OF(int) a;", Macros);
16310 verifyFormat("STACK_OF(int) *a;", Macros);
16311 verifyFormat("STACK_OF(int const *) *a;", Macros);
16312 verifyFormat("STACK_OF(int *const) *a;", Macros);
16313 verifyFormat("STACK_OF(int, string) a;", Macros);
16314 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
16315 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
16316 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
16317 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
16318
16319 Macros.PointerAlignment = FormatStyle::PAS_Left;
16320 verifyFormat("STACK_OF(int)* a;", Macros);
16321 verifyFormat("STACK_OF(int*)* a;", Macros);
16322 }
16323
TEST_F(FormatTest,AmbersandInLamda)16324 TEST_F(FormatTest, AmbersandInLamda) {
16325 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
16326 FormatStyle AlignStyle = getLLVMStyle();
16327 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
16328 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
16329 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
16330 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
16331 }
16332
TEST_F(FormatTest,SpacesInConditionalStatement)16333 TEST_F(FormatTest, SpacesInConditionalStatement) {
16334 FormatStyle Spaces = getLLVMStyle();
16335 Spaces.SpacesInConditionalStatement = true;
16336 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
16337 verifyFormat("if ( !a )\n return;", Spaces);
16338 verifyFormat("if ( a )\n return;", Spaces);
16339 verifyFormat("if constexpr ( a )\n return;", Spaces);
16340 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
16341 verifyFormat("while ( a )\n return;", Spaces);
16342 verifyFormat("while ( (a && b) )\n return;", Spaces);
16343 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
16344 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
16345 // Check that space on the left of "::" is inserted as expected at beginning
16346 // of condition.
16347 verifyFormat("while ( ::func() )\n return;", Spaces);
16348 }
16349
TEST_F(FormatTest,AlternativeOperators)16350 TEST_F(FormatTest, AlternativeOperators) {
16351 // Test case for ensuring alternate operators are not
16352 // combined with their right most neighbour.
16353 verifyFormat("int a and b;");
16354 verifyFormat("int a and_eq b;");
16355 verifyFormat("int a bitand b;");
16356 verifyFormat("int a bitor b;");
16357 verifyFormat("int a compl b;");
16358 verifyFormat("int a not b;");
16359 verifyFormat("int a not_eq b;");
16360 verifyFormat("int a or b;");
16361 verifyFormat("int a xor b;");
16362 verifyFormat("int a xor_eq b;");
16363 verifyFormat("return this not_eq bitand other;");
16364 verifyFormat("bool operator not_eq(const X bitand other)");
16365
16366 verifyFormat("int a and 5;");
16367 verifyFormat("int a and_eq 5;");
16368 verifyFormat("int a bitand 5;");
16369 verifyFormat("int a bitor 5;");
16370 verifyFormat("int a compl 5;");
16371 verifyFormat("int a not 5;");
16372 verifyFormat("int a not_eq 5;");
16373 verifyFormat("int a or 5;");
16374 verifyFormat("int a xor 5;");
16375 verifyFormat("int a xor_eq 5;");
16376
16377 verifyFormat("int a compl(5);");
16378 verifyFormat("int a not(5);");
16379
16380 /* FIXME handle alternate tokens
16381 * https://en.cppreference.com/w/cpp/language/operator_alternative
16382 // alternative tokens
16383 verifyFormat("compl foo();"); // ~foo();
16384 verifyFormat("foo() <%%>;"); // foo();
16385 verifyFormat("void foo() <%%>;"); // void foo(){}
16386 verifyFormat("int a <:1:>;"); // int a[1];[
16387 verifyFormat("%:define ABC abc"); // #define ABC abc
16388 verifyFormat("%:%:"); // ##
16389 */
16390 }
16391
TEST_F(FormatTest,STLWhileNotDefineChed)16392 TEST_F(FormatTest, STLWhileNotDefineChed) {
16393 verifyFormat("#if defined(while)\n"
16394 "#define while EMIT WARNING C4005\n"
16395 "#endif // while");
16396 }
16397
TEST_F(FormatTest,OperatorSpacing)16398 TEST_F(FormatTest, OperatorSpacing) {
16399 FormatStyle Style = getLLVMStyle();
16400 Style.PointerAlignment = FormatStyle::PAS_Right;
16401 verifyFormat("Foo::operator*();", Style);
16402 verifyFormat("Foo::operator void *();", Style);
16403 verifyFormat("Foo::operator void **();", Style);
16404 verifyFormat("Foo::operator void *&();", Style);
16405 verifyFormat("Foo::operator void *&&();", Style);
16406 verifyFormat("Foo::operator()(void *);", Style);
16407 verifyFormat("Foo::operator*(void *);", Style);
16408 verifyFormat("Foo::operator*();", Style);
16409 verifyFormat("Foo::operator**();", Style);
16410 verifyFormat("Foo::operator&();", Style);
16411 verifyFormat("Foo::operator<int> *();", Style);
16412 verifyFormat("Foo::operator<Foo> *();", Style);
16413 verifyFormat("Foo::operator<int> **();", Style);
16414 verifyFormat("Foo::operator<Foo> **();", Style);
16415 verifyFormat("Foo::operator<int> &();", Style);
16416 verifyFormat("Foo::operator<Foo> &();", Style);
16417 verifyFormat("Foo::operator<int> &&();", Style);
16418 verifyFormat("Foo::operator<Foo> &&();", Style);
16419 verifyFormat("Foo::operator<int> *&();", Style);
16420 verifyFormat("Foo::operator<Foo> *&();", Style);
16421 verifyFormat("Foo::operator<int> *&&();", Style);
16422 verifyFormat("Foo::operator<Foo> *&&();", Style);
16423 verifyFormat("operator*(int (*)(), class Foo);", Style);
16424
16425 verifyFormat("Foo::operator&();", Style);
16426 verifyFormat("Foo::operator void &();", Style);
16427 verifyFormat("Foo::operator()(void &);", Style);
16428 verifyFormat("Foo::operator&(void &);", Style);
16429 verifyFormat("Foo::operator&();", Style);
16430 verifyFormat("operator&(int (&)(), class Foo);", Style);
16431
16432 verifyFormat("Foo::operator&&();", Style);
16433 verifyFormat("Foo::operator**();", Style);
16434 verifyFormat("Foo::operator void &&();", Style);
16435 verifyFormat("Foo::operator()(void &&);", Style);
16436 verifyFormat("Foo::operator&&(void &&);", Style);
16437 verifyFormat("Foo::operator&&();", Style);
16438 verifyFormat("operator&&(int(&&)(), class Foo);", Style);
16439 verifyFormat("operator const nsTArrayRight<E> &()", Style);
16440 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
16441 Style);
16442 verifyFormat("operator void **()", Style);
16443 verifyFormat("operator const FooRight<Object> &()", Style);
16444 verifyFormat("operator const FooRight<Object> *()", Style);
16445 verifyFormat("operator const FooRight<Object> **()", Style);
16446 verifyFormat("operator const FooRight<Object> *&()", Style);
16447 verifyFormat("operator const FooRight<Object> *&&()", Style);
16448
16449 Style.PointerAlignment = FormatStyle::PAS_Left;
16450 verifyFormat("Foo::operator*();", Style);
16451 verifyFormat("Foo::operator**();", Style);
16452 verifyFormat("Foo::operator void*();", Style);
16453 verifyFormat("Foo::operator void**();", Style);
16454 verifyFormat("Foo::operator void*&();", Style);
16455 verifyFormat("Foo::operator/*comment*/ void*();", Style);
16456 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
16457 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
16458 verifyFormat("Foo::operator()(void*);", Style);
16459 verifyFormat("Foo::operator*(void*);", Style);
16460 verifyFormat("Foo::operator*();", Style);
16461 verifyFormat("Foo::operator<int>*();", Style);
16462 verifyFormat("Foo::operator<Foo>*();", Style);
16463 verifyFormat("Foo::operator<int>**();", Style);
16464 verifyFormat("Foo::operator<Foo>**();", Style);
16465 verifyFormat("Foo::operator<Foo>*&();", Style);
16466 verifyFormat("Foo::operator<int>&();", Style);
16467 verifyFormat("Foo::operator<Foo>&();", Style);
16468 verifyFormat("Foo::operator<int>&&();", Style);
16469 verifyFormat("Foo::operator<Foo>&&();", Style);
16470 verifyFormat("Foo::operator<int>*&();", Style);
16471 verifyFormat("Foo::operator<Foo>*&();", Style);
16472 verifyFormat("operator*(int (*)(), class Foo);", Style);
16473
16474 verifyFormat("Foo::operator&();", Style);
16475 verifyFormat("Foo::operator void&();", Style);
16476 verifyFormat("Foo::operator/*comment*/ void&();", Style);
16477 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
16478 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
16479 verifyFormat("Foo::operator()(void&);", Style);
16480 verifyFormat("Foo::operator&(void&);", Style);
16481 verifyFormat("Foo::operator&();", Style);
16482 verifyFormat("operator&(int (&)(), class Foo);", Style);
16483
16484 verifyFormat("Foo::operator&&();", Style);
16485 verifyFormat("Foo::operator void&&();", Style);
16486 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
16487 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
16488 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
16489 verifyFormat("Foo::operator()(void&&);", Style);
16490 verifyFormat("Foo::operator&&(void&&);", Style);
16491 verifyFormat("Foo::operator&&();", Style);
16492 verifyFormat("operator&&(int(&&)(), class Foo);", Style);
16493 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
16494 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
16495 Style);
16496 verifyFormat("operator void**()", Style);
16497 verifyFormat("operator const FooLeft<Object>&()", Style);
16498 verifyFormat("operator const FooLeft<Object>*()", Style);
16499 verifyFormat("operator const FooLeft<Object>**()", Style);
16500 verifyFormat("operator const FooLeft<Object>*&()", Style);
16501 verifyFormat("operator const FooLeft<Object>*&&()", Style);
16502
16503 // PR45107
16504 verifyFormat("operator Vector<String>&();", Style);
16505 verifyFormat("operator const Vector<String>&();", Style);
16506 verifyFormat("operator foo::Bar*();", Style);
16507 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
16508 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
16509 Style);
16510
16511 Style.PointerAlignment = FormatStyle::PAS_Middle;
16512 verifyFormat("Foo::operator*();", Style);
16513 verifyFormat("Foo::operator void *();", Style);
16514 verifyFormat("Foo::operator()(void *);", Style);
16515 verifyFormat("Foo::operator*(void *);", Style);
16516 verifyFormat("Foo::operator*();", Style);
16517 verifyFormat("operator*(int (*)(), class Foo);", Style);
16518
16519 verifyFormat("Foo::operator&();", Style);
16520 verifyFormat("Foo::operator void &();", Style);
16521 verifyFormat("Foo::operator()(void &);", Style);
16522 verifyFormat("Foo::operator&(void &);", Style);
16523 verifyFormat("Foo::operator&();", Style);
16524 verifyFormat("operator&(int (&)(), class Foo);", Style);
16525
16526 verifyFormat("Foo::operator&&();", Style);
16527 verifyFormat("Foo::operator void &&();", Style);
16528 verifyFormat("Foo::operator()(void &&);", Style);
16529 verifyFormat("Foo::operator&&(void &&);", Style);
16530 verifyFormat("Foo::operator&&();", Style);
16531 verifyFormat("operator&&(int(&&)(), class Foo);", Style);
16532 }
16533
TEST_F(FormatTest,OperatorPassedAsAFunctionPtr)16534 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
16535 FormatStyle Style = getLLVMStyle();
16536 // PR46157
16537 verifyFormat("foo(operator+, -42);", Style);
16538 verifyFormat("foo(operator++, -42);", Style);
16539 verifyFormat("foo(operator--, -42);", Style);
16540 verifyFormat("foo(-42, operator--);", Style);
16541 verifyFormat("foo(-42, operator, );", Style);
16542 verifyFormat("foo(operator, , -42);", Style);
16543 }
16544
TEST_F(FormatTest,WhitespaceSensitiveMacros)16545 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
16546 FormatStyle Style = getLLVMStyle();
16547 Style.WhitespaceSensitiveMacros.push_back("FOO");
16548
16549 // Don't use the helpers here, since 'mess up' will change the whitespace
16550 // and these are all whitespace sensitive by definition
16551 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
16552 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
16553 EXPECT_EQ(
16554 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
16555 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
16556 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
16557 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
16558 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
16559 " Still=Intentional);",
16560 format("FOO(String-ized&Messy+But,: :\n"
16561 " Still=Intentional);",
16562 Style));
16563 Style.AlignConsecutiveAssignments = true;
16564 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n"
16565 " Still=Intentional);",
16566 format("FOO(String-ized=&Messy+But,: :\n"
16567 " Still=Intentional);",
16568 Style));
16569
16570 Style.ColumnLimit = 21;
16571 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
16572 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
16573 }
16574
TEST_F(FormatTest,VeryLongNamespaceCommentSplit)16575 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
16576 // These tests are not in NamespaceFixer because that doesn't
16577 // test its interaction with line wrapping
16578 FormatStyle Style = getLLVMStyle();
16579 Style.ColumnLimit = 80;
16580 verifyFormat("namespace {\n"
16581 "int i;\n"
16582 "int j;\n"
16583 "} // namespace",
16584 Style);
16585
16586 verifyFormat("namespace AAA {\n"
16587 "int i;\n"
16588 "int j;\n"
16589 "} // namespace AAA",
16590 Style);
16591
16592 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n"
16593 "int i;\n"
16594 "int j;\n"
16595 "} // namespace Averyveryveryverylongnamespace",
16596 format("namespace Averyveryveryverylongnamespace {\n"
16597 "int i;\n"
16598 "int j;\n"
16599 "}",
16600 Style));
16601
16602 EXPECT_EQ(
16603 "namespace "
16604 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
16605 " went::mad::now {\n"
16606 "int i;\n"
16607 "int j;\n"
16608 "} // namespace\n"
16609 " // "
16610 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
16611 "went::mad::now",
16612 format("namespace "
16613 "would::it::save::you::a::lot::of::time::if_::i::"
16614 "just::gave::up::and_::went::mad::now {\n"
16615 "int i;\n"
16616 "int j;\n"
16617 "}",
16618 Style));
16619
16620 // This used to duplicate the comment again and again on subsequent runs
16621 EXPECT_EQ(
16622 "namespace "
16623 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
16624 " went::mad::now {\n"
16625 "int i;\n"
16626 "int j;\n"
16627 "} // namespace\n"
16628 " // "
16629 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
16630 "went::mad::now",
16631 format("namespace "
16632 "would::it::save::you::a::lot::of::time::if_::i::"
16633 "just::gave::up::and_::went::mad::now {\n"
16634 "int i;\n"
16635 "int j;\n"
16636 "} // namespace\n"
16637 " // "
16638 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
16639 "and_::went::mad::now",
16640 Style));
16641 }
16642
TEST_F(FormatTest,LikelyUnlikely)16643 TEST_F(FormatTest, LikelyUnlikely) {
16644 FormatStyle Style = getLLVMStyle();
16645
16646 verifyFormat("if (argc > 5) [[unlikely]] {\n"
16647 " return 29;\n"
16648 "}",
16649 Style);
16650
16651 verifyFormat("if (argc > 5) [[likely]] {\n"
16652 " return 29;\n"
16653 "}",
16654 Style);
16655
16656 verifyFormat("if (argc > 5) [[unlikely]] {\n"
16657 " return 29;\n"
16658 "} else [[likely]] {\n"
16659 " return 42;\n"
16660 "}\n",
16661 Style);
16662
16663 verifyFormat("if (argc > 5) [[unlikely]] {\n"
16664 " return 29;\n"
16665 "} else if (argc > 10) [[likely]] {\n"
16666 " return 99;\n"
16667 "} else {\n"
16668 " return 42;\n"
16669 "}\n",
16670 Style);
16671
16672 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
16673 " return 29;\n"
16674 "}",
16675 Style);
16676 }
16677
TEST_F(FormatTest,LLVMDefaultStyle)16678 TEST_F(FormatTest, LLVMDefaultStyle) {
16679 FormatStyle Style = getLLVMStyle();
16680 verifyFormat("extern \"C\" {\n"
16681 "int foo();\n"
16682 "}",
16683 Style);
16684 }
TEST_F(FormatTest,GNUDefaultStyle)16685 TEST_F(FormatTest, GNUDefaultStyle) {
16686 FormatStyle Style = getGNUStyle();
16687 verifyFormat("extern \"C\"\n"
16688 "{\n"
16689 " int foo ();\n"
16690 "}",
16691 Style);
16692 }
TEST_F(FormatTest,MozillaDefaultStyle)16693 TEST_F(FormatTest, MozillaDefaultStyle) {
16694 FormatStyle Style = getMozillaStyle();
16695 verifyFormat("extern \"C\"\n"
16696 "{\n"
16697 " int foo();\n"
16698 "}",
16699 Style);
16700 }
TEST_F(FormatTest,GoogleDefaultStyle)16701 TEST_F(FormatTest, GoogleDefaultStyle) {
16702 FormatStyle Style = getGoogleStyle();
16703 verifyFormat("extern \"C\" {\n"
16704 "int foo();\n"
16705 "}",
16706 Style);
16707 }
TEST_F(FormatTest,ChromiumDefaultStyle)16708 TEST_F(FormatTest, ChromiumDefaultStyle) {
16709 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
16710 verifyFormat("extern \"C\" {\n"
16711 "int foo();\n"
16712 "}",
16713 Style);
16714 }
TEST_F(FormatTest,MicrosoftDefaultStyle)16715 TEST_F(FormatTest, MicrosoftDefaultStyle) {
16716 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
16717 verifyFormat("extern \"C\"\n"
16718 "{\n"
16719 " int foo();\n"
16720 "}",
16721 Style);
16722 }
TEST_F(FormatTest,WebKitDefaultStyle)16723 TEST_F(FormatTest, WebKitDefaultStyle) {
16724 FormatStyle Style = getWebKitStyle();
16725 verifyFormat("extern \"C\" {\n"
16726 "int foo();\n"
16727 "}",
16728 Style);
16729 }
16730 } // namespace
16731 } // namespace format
16732 } // namespace clang
16733