[clang-format] Adjust braced list detection
This avoids mishandling nested compound statements that are followed by another compound statement. Fixes https://llvm.org/PR38314 and https://llvm.org/PR48305. Differential Revision: https://reviews.llvm.org/D114583
This commit is contained in:
parent
bdd7c53dc5
commit
c41b3b0fa0
|
|
@ -578,17 +578,14 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
|
||||||
// BlockKind later if we parse a braced list (where all blocks
|
// BlockKind later if we parse a braced list (where all blocks
|
||||||
// inside are by default braced lists), or when we explicitly detect
|
// inside are by default braced lists), or when we explicitly detect
|
||||||
// blocks (for example while parsing lambdas).
|
// blocks (for example while parsing lambdas).
|
||||||
// FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
|
|
||||||
// braced list in JS.
|
|
||||||
ProbablyBracedList =
|
ProbablyBracedList =
|
||||||
(Style.Language == FormatStyle::LK_JavaScript &&
|
(Style.Language == FormatStyle::LK_JavaScript &&
|
||||||
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
|
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
|
||||||
Keywords.kw_as)) ||
|
Keywords.kw_as)) ||
|
||||||
(Style.isCpp() && NextTok->is(tok::l_paren)) ||
|
(Style.isCpp() && NextTok->is(tok::l_paren)) ||
|
||||||
NextTok->isOneOf(tok::comma, tok::period, tok::colon,
|
NextTok->isOneOf(tok::comma, tok::period, tok::colon,
|
||||||
tok::r_paren, tok::r_square, tok::l_brace,
|
tok::r_paren, tok::r_square, tok::ellipsis) ||
|
||||||
tok::ellipsis) ||
|
(NextTok->isOneOf(tok::l_brace, tok::identifier) &&
|
||||||
(NextTok->is(tok::identifier) &&
|
|
||||||
!PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
|
!PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
|
||||||
(NextTok->is(tok::semi) &&
|
(NextTok->is(tok::semi) &&
|
||||||
(!ExpectClassBody || LBraceStack.size() != 1)) ||
|
(!ExpectClassBody || LBraceStack.size() != 1)) ||
|
||||||
|
|
@ -2856,7 +2853,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
|
||||||
// class Foo implements {bar: number} { }
|
// class Foo implements {bar: number} { }
|
||||||
nextToken();
|
nextToken();
|
||||||
if (FormatTok->is(tok::l_brace)) {
|
if (FormatTok->is(tok::l_brace)) {
|
||||||
tryToParseBracedList();
|
parseBracedList();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11773,6 +11773,27 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
|
||||||
" f(v);\n"
|
" f(v);\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
||||||
|
verifyFormat("void foo() {\n"
|
||||||
|
" { // asdf\n"
|
||||||
|
" { int a; }\n"
|
||||||
|
" }\n"
|
||||||
|
" {\n"
|
||||||
|
" { int b; }\n"
|
||||||
|
" }\n"
|
||||||
|
"}");
|
||||||
|
verifyFormat("namespace n {\n"
|
||||||
|
"void foo() {\n"
|
||||||
|
" {\n"
|
||||||
|
" {\n"
|
||||||
|
" statement();\n"
|
||||||
|
" if (false) {\n"
|
||||||
|
" }\n"
|
||||||
|
" }\n"
|
||||||
|
" }\n"
|
||||||
|
" {}\n"
|
||||||
|
"}\n"
|
||||||
|
"} // namespace n");
|
||||||
|
|
||||||
// Long lists should be formatted in columns even if they are nested.
|
// Long lists should be formatted in columns even if they are nested.
|
||||||
verifyFormat(
|
verifyFormat(
|
||||||
"vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
|
"vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue