Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 14e25c0

Browse files
committed
clang-format: Keep empty lines and format 1-line nested blocks.
Let clang-format consistently keep up to one empty line (configured via FormatStyle::MaxEmptyLinesToKeep) in nested blocks, e.g. lambdas. Also, actually format single statements in nested blocks. Before: DEBUG({ int i; }); DEBUG({ int i; // an empty line here would just be removed. int j; }); After: DEBUG({ int i; }); DEBUG({ int i; int j; }); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190278 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c23b1db commit 14e25c0

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/Format/Format.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,14 @@ class UnwrappedLineFormatter {
530530
I != E; ++I) {
531531
unsigned Indent =
532532
ParentIndent + ((*I)->Level - Line.Level) * Style.IndentWidth;
533-
if (!DryRun)
533+
if (!DryRun) {
534+
unsigned Newlines = std::min((*I)->First->NewlinesBefore,
535+
Style.MaxEmptyLinesToKeep + 1);
536+
Newlines = std::max(1u, Newlines);
534537
Whitespaces->replaceWhitespace(
535-
*(*I)->First, /*Newlines=*/1, /*Spaces=*/Indent,
538+
*(*I)->First, Newlines, /*Spaces=*/Indent,
536539
/*StartOfTokenColumn=*/Indent, Line.InPPDirective);
540+
}
537541
UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I);
538542
Penalty += Formatter.format(Indent, DryRun);
539543
}
@@ -552,6 +556,9 @@ class UnwrappedLineFormatter {
552556
/*Newlines=*/0, /*Spaces=*/1,
553557
/*StartOfTokenColumn=*/State.Column,
554558
State.Line->InPPDirective);
559+
UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
560+
*LBrace.Children[0]);
561+
Penalty += Formatter.format(State.Column + 1, DryRun);
555562
}
556563

557564
State.Column += 1 + LBrace.Children[0]->Last->TotalLength;

unittests/Format/FormatTest.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,19 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
22762276
" somethingElse();\n"
22772277
"});",
22782278
getLLVMStyleWithColumns(29)));
2279+
EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
2280+
EXPECT_EQ("DEBUG({\n"
2281+
" int i;\n"
2282+
"\n"
2283+
" // comment\n"
2284+
" int j;\n"
2285+
"});",
2286+
format("DEBUG({\n"
2287+
" int i;\n"
2288+
"\n"
2289+
" // comment\n"
2290+
" int j;\n"
2291+
"});"));
22792292
}
22802293

22812294
TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
@@ -6395,7 +6408,7 @@ TEST_F(FormatTest, FormatsLambdas) {
63956408
verifyFormat("auto c = { [&a, &a, a] {\n"
63966409
" [=, a, b, &c] { return b++; }();\n"
63976410
"} }\n");
6398-
verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] { }(); } }\n");
6411+
verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] {}(); } }\n");
63996412
verifyFormat("void f() {\n"
64006413
" other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
64016414
"}\n");

0 commit comments

Comments
 (0)