r190278 - clang-format: Keep empty lines and format 1-line nested blocks.
Daniel Jasper
djasper at google.com
Sun Sep 8 07:07:57 PDT 2013
Author: djasper
Date: Sun Sep 8 09:07:57 2013
New Revision: 190278
URL: http://llvm.org/viewvc/llvm-project?rev=190278&view=rev
Log:
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;
});
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=190278&r1=190277&r2=190278&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sun Sep 8 09:07:57 2013
@@ -530,10 +530,14 @@ private:
I != E; ++I) {
unsigned Indent =
ParentIndent + ((*I)->Level - Line.Level) * Style.IndentWidth;
- if (!DryRun)
+ if (!DryRun) {
+ unsigned Newlines = std::min((*I)->First->NewlinesBefore,
+ Style.MaxEmptyLinesToKeep + 1);
+ Newlines = std::max(1u, Newlines);
Whitespaces->replaceWhitespace(
- *(*I)->First, /*Newlines=*/1, /*Spaces=*/Indent,
+ *(*I)->First, Newlines, /*Spaces=*/Indent,
/*StartOfTokenColumn=*/Indent, Line.InPPDirective);
+ }
UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I);
Penalty += Formatter.format(Indent, DryRun);
}
@@ -552,6 +556,9 @@ private:
/*Newlines=*/0, /*Spaces=*/1,
/*StartOfTokenColumn=*/State.Column,
State.Line->InPPDirective);
+ UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
+ *LBrace.Children[0]);
+ Penalty += Formatter.format(State.Column + 1, DryRun);
}
State.Column += 1 + LBrace.Children[0]->Last->TotalLength;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=190278&r1=190277&r2=190278&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Sep 8 09:07:57 2013
@@ -2276,6 +2276,19 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
" somethingElse();\n"
"});",
getLLVMStyleWithColumns(29)));
+ EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
+ EXPECT_EQ("DEBUG({\n"
+ " int i;\n"
+ "\n"
+ " // comment\n"
+ " int j;\n"
+ "});",
+ format("DEBUG({\n"
+ " int i;\n"
+ "\n"
+ " // comment\n"
+ " int j;\n"
+ "});"));
}
TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
@@ -6395,7 +6408,7 @@ TEST_F(FormatTest, FormatsLambdas) {
verifyFormat("auto c = { [&a, &a, a] {\n"
" [=, a, b, &c] { return b++; }();\n"
"} }\n");
- verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] { }(); } }\n");
+ verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] {}(); } }\n");
verifyFormat("void f() {\n"
" other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
"}\n");
More information about the cfe-commits
mailing list