clang-format: Be more conservative about specially indenting blocks in C++.
This is a bit of a step back of what we did in r222531, as there are
some corner cases in C++, where this kind of formatting is really bad.
Example:
Before:
virtual aaaaaaaaaaaaaaaa(std::function<bool()> IsKindWeWant = [&]() {
return true;
}, aaaaa aaaaaaaaa);
After:
virtual aaaaaaaaaaaaaaaa(std::function<bool()> IsKindWeWant =
[&]() { return true; },
aaaaa aaaaaaaaa);
The block formatting logic in JavaScript will probably go some other changes,
too, and we'll potentially be able to make the rules more consistent again. For
now, this seems to be the best approach for C++.
llvm-svn: 245694
This commit is contained in:
@@ -501,6 +501,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
|
|||||||
// Any break on this level means that the parent level has been broken
|
// Any break on this level means that the parent level has been broken
|
||||||
// and we need to avoid bin packing there.
|
// and we need to avoid bin packing there.
|
||||||
bool NestedBlockSpecialCase =
|
bool NestedBlockSpecialCase =
|
||||||
|
Style.Language != FormatStyle::LK_Cpp &&
|
||||||
Current.is(tok::r_brace) && State.Stack.size() > 1 &&
|
Current.is(tok::r_brace) && State.Stack.size() > 1 &&
|
||||||
State.Stack[State.Stack.size() - 2].NestedBlockInlined;
|
State.Stack[State.Stack.size() - 2].NestedBlockInlined;
|
||||||
if (!NestedBlockSpecialCase)
|
if (!NestedBlockSpecialCase)
|
||||||
|
|||||||
@@ -2245,7 +2245,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
|
|||||||
Left.is(tok::kw_operator))
|
Left.is(tok::kw_operator))
|
||||||
return false;
|
return false;
|
||||||
if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
|
if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
|
||||||
Line.Type == LT_VirtualFunctionDecl)
|
Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
|
||||||
return false;
|
return false;
|
||||||
if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
|
if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -3076,10 +3076,12 @@ TEST_F(FormatTest, LayoutBlockInsideParens) {
|
|||||||
" int i;\n"
|
" int i;\n"
|
||||||
" int j;\n"
|
" int j;\n"
|
||||||
"});");
|
"});");
|
||||||
verifyFormat("functionCall({\n"
|
verifyFormat("functionCall(\n"
|
||||||
" int i;\n"
|
" {\n"
|
||||||
" int j;\n"
|
" int i;\n"
|
||||||
"}, aaaa, bbbb, cccc);");
|
" int j;\n"
|
||||||
|
" },\n"
|
||||||
|
" aaaa, bbbb, cccc);");
|
||||||
verifyFormat("functionA(functionB({\n"
|
verifyFormat("functionA(functionB({\n"
|
||||||
" int i;\n"
|
" int i;\n"
|
||||||
" int j;\n"
|
" int j;\n"
|
||||||
@@ -3186,9 +3188,11 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
|
|||||||
"});");
|
"});");
|
||||||
FormatStyle Style = getGoogleStyle();
|
FormatStyle Style = getGoogleStyle();
|
||||||
Style.ColumnLimit = 45;
|
Style.ColumnLimit = 45;
|
||||||
verifyFormat("Debug(aaaaa, {\n"
|
verifyFormat("Debug(aaaaa,\n"
|
||||||
" if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
|
" {\n"
|
||||||
"}, a);",
|
" if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
|
||||||
|
" },\n"
|
||||||
|
" a);",
|
||||||
Style);
|
Style);
|
||||||
|
|
||||||
verifyFormat("SomeFunction({MACRO({ return output; }), b});");
|
verifyFormat("SomeFunction({MACRO({ return output; }), b});");
|
||||||
@@ -8100,11 +8104,13 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
|
|||||||
"};",
|
"};",
|
||||||
Tab);
|
Tab);
|
||||||
verifyFormat("{\n"
|
verifyFormat("{\n"
|
||||||
"\tQ({\n"
|
"\tQ(\n"
|
||||||
"\t\tint a;\n"
|
"\t {\n"
|
||||||
"\t\tsomeFunction(aaaaaaaa,\n"
|
"\t\t int a;\n"
|
||||||
"\t\t bbbbbbb);\n"
|
"\t\t someFunction(aaaaaaaa,\n"
|
||||||
"\t}, p);\n"
|
"\t\t bbbbbbb);\n"
|
||||||
|
"\t },\n"
|
||||||
|
"\t p);\n"
|
||||||
"}",
|
"}",
|
||||||
Tab);
|
Tab);
|
||||||
EXPECT_EQ("{\n"
|
EXPECT_EQ("{\n"
|
||||||
@@ -9996,6 +10002,9 @@ TEST_F(FormatTest, FormatsLambdas) {
|
|||||||
verifyFormat("SomeFunction({[&] {\n"
|
verifyFormat("SomeFunction({[&] {\n"
|
||||||
" // comment\n"
|
" // comment\n"
|
||||||
"}});");
|
"}});");
|
||||||
|
verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
|
||||||
|
" [&]() { return true; },\n"
|
||||||
|
" aaaaa aaaaaaaaa);");
|
||||||
|
|
||||||
// Lambdas with return types.
|
// Lambdas with return types.
|
||||||
verifyFormat("int c = []() -> int { return 2; }();\n");
|
verifyFormat("int c = []() -> int { return 2; }();\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user