Skip to content

Commit c41b3b0

Browse files
cpplearnerowenca
authored andcommitted
[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
1 parent bdd7c53 commit c41b3b0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,17 +578,14 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
578578
// BlockKind later if we parse a braced list (where all blocks
579579
// inside are by default braced lists), or when we explicitly detect
580580
// blocks (for example while parsing lambdas).
581-
// FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
582-
// braced list in JS.
583581
ProbablyBracedList =
584582
(Style.Language == FormatStyle::LK_JavaScript &&
585583
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
586584
Keywords.kw_as)) ||
587585
(Style.isCpp() && NextTok->is(tok::l_paren)) ||
588586
NextTok->isOneOf(tok::comma, tok::period, tok::colon,
589-
tok::r_paren, tok::r_square, tok::l_brace,
590-
tok::ellipsis) ||
591-
(NextTok->is(tok::identifier) &&
587+
tok::r_paren, tok::r_square, tok::ellipsis) ||
588+
(NextTok->isOneOf(tok::l_brace, tok::identifier) &&
592589
!PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
593590
(NextTok->is(tok::semi) &&
594591
(!ExpectClassBody || LBraceStack.size() != 1)) ||
@@ -2856,7 +2853,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
28562853
// class Foo implements {bar: number} { }
28572854
nextToken();
28582855
if (FormatTok->is(tok::l_brace)) {
2859-
tryToParseBracedList();
2856+
parseBracedList();
28602857
continue;
28612858
}
28622859
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11773,6 +11773,27 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
1177311773
" f(v);\n"
1177411774
"}");
1177511775

11776+
verifyFormat("void foo() {\n"
11777+
" { // asdf\n"
11778+
" { int a; }\n"
11779+
" }\n"
11780+
" {\n"
11781+
" { int b; }\n"
11782+
" }\n"
11783+
"}");
11784+
verifyFormat("namespace n {\n"
11785+
"void foo() {\n"
11786+
" {\n"
11787+
" {\n"
11788+
" statement();\n"
11789+
" if (false) {\n"
11790+
" }\n"
11791+
" }\n"
11792+
" }\n"
11793+
" {}\n"
11794+
"}\n"
11795+
"} // namespace n");
11796+
1177611797
// Long lists should be formatted in columns even if they are nested.
1177711798
verifyFormat(
1177811799
"vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"

0 commit comments

Comments
 (0)