Skip to content

Commit 91b5d50

Browse files
committed
Revert "[clang-format] Link the braces of a block in UnwrappedLineParser"
This reverts commit e33243c but keeps the added test case and also adds another test case. Fixes #59417. Differential Revision: https://reviews.llvm.org/D139760
1 parent d9bef74 commit 91b5d50

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,6 @@ FormatToken *UnwrappedLineParser::parseBlock(
934934
return IfLBrace;
935935
}
936936

937-
Tok->MatchingParen = FormatTok;
938-
FormatTok->MatchingParen = Tok;
939-
940937
const bool IsFunctionRBrace =
941938
FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
942939

@@ -970,7 +967,10 @@ FormatToken *UnwrappedLineParser::parseBlock(
970967
}
971968
return mightFitOnOneLine((*CurrentLines)[Index], Tok);
972969
};
973-
Tok->Optional = RemoveBraces();
970+
if (RemoveBraces()) {
971+
Tok->MatchingParen = FormatTok;
972+
FormatTok->MatchingParen = Tok;
973+
}
974974

975975
size_t PPEndHash = computePPHash();
976976

@@ -2707,20 +2707,10 @@ static void markOptionalBraces(FormatToken *LeftBrace) {
27072707

27082708
assert(RightBrace->is(tok::r_brace));
27092709
assert(RightBrace->MatchingParen == LeftBrace);
2710+
assert(LeftBrace->Optional == RightBrace->Optional);
27102711

2711-
RightBrace->Optional = LeftBrace->Optional;
2712-
}
2713-
2714-
static void resetOptional(FormatToken *LeftBrace) {
2715-
if (!LeftBrace)
2716-
return;
2717-
2718-
const auto *RightBrace = LeftBrace->MatchingParen;
2719-
const bool IsOptionalRightBrace = RightBrace && RightBrace->Optional;
2720-
assert(LeftBrace->Optional || !IsOptionalRightBrace);
2721-
2722-
if (!IsOptionalRightBrace)
2723-
LeftBrace->Optional = false;
2712+
LeftBrace->Optional = true;
2713+
RightBrace->Optional = true;
27242714
}
27252715

27262716
void UnwrappedLineParser::handleAttributes() {
@@ -2788,7 +2778,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
27882778

27892779
if (Style.RemoveBracesLLVM) {
27902780
assert(!NestedTooDeep.empty());
2791-
KeepIfBraces = KeepIfBraces || (IfLeftBrace && !IfLeftBrace->Optional) ||
2781+
KeepIfBraces = KeepIfBraces ||
2782+
(IfLeftBrace && !IfLeftBrace->MatchingParen) ||
27922783
NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
27932784
IfBlockKind == IfStmtKind::IfElseIf;
27942785
}
@@ -2819,9 +2810,8 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
28192810
ElseBlockKind == IfStmtKind::IfElseIf;
28202811
} else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
28212812
KeepElseBraces = true;
2822-
assert(ElseLeftBrace->Optional);
28232813
assert(ElseLeftBrace->MatchingParen);
2824-
ElseLeftBrace->MatchingParen->Optional = true;
2814+
markOptionalBraces(ElseLeftBrace);
28252815
}
28262816
addUnwrappedLine();
28272817
} else if (FormatTok->is(tok::kw_if)) {
@@ -2856,19 +2846,25 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
28562846

28572847
assert(!NestedTooDeep.empty());
28582848
KeepElseBraces = KeepElseBraces ||
2859-
(ElseLeftBrace && !ElseLeftBrace->Optional) ||
2849+
(ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
28602850
NestedTooDeep.back();
28612851

28622852
NestedTooDeep.pop_back();
28632853

28642854
if (!KeepIfBraces && !KeepElseBraces) {
28652855
markOptionalBraces(IfLeftBrace);
28662856
markOptionalBraces(ElseLeftBrace);
2857+
} else if (IfLeftBrace) {
2858+
FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
2859+
if (IfRightBrace) {
2860+
assert(IfRightBrace->MatchingParen == IfLeftBrace);
2861+
assert(!IfLeftBrace->Optional);
2862+
assert(!IfRightBrace->Optional);
2863+
IfLeftBrace->MatchingParen = nullptr;
2864+
IfRightBrace->MatchingParen = nullptr;
2865+
}
28672866
}
28682867

2869-
resetOptional(IfLeftBrace);
2870-
resetOptional(ElseLeftBrace);
2871-
28722868
if (IfKind)
28732869
*IfKind = Kind;
28742870

@@ -3083,7 +3079,6 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
30833079
if (!NestedTooDeep.back())
30843080
markOptionalBraces(LeftBrace);
30853081
}
3086-
resetOptional(LeftBrace);
30873082
if (WrapRightBrace)
30883083
addUnwrappedLine();
30893084
} else {

clang/unittests/Format/BracesRemoverTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,41 @@ TEST_F(BracesRemoverTest, RemoveBraces) {
683683
"return a;",
684684
Style);
685685

686+
verifyFormat("if (a)\n"
687+
"#ifdef FOO\n"
688+
" if (b)\n"
689+
" bar = c;\n"
690+
" else\n"
691+
"#endif\n"
692+
" {\n"
693+
" foo = d;\n"
694+
"#ifdef FOO\n"
695+
" bar = e;\n"
696+
"#else\n"
697+
" bar = f;\n" // FIXME: should be indented 1 more level.
698+
"#endif\n"
699+
" }\n"
700+
"else\n"
701+
" bar = g;",
702+
"if (a)\n"
703+
"#ifdef FOO\n"
704+
" if (b)\n"
705+
" bar = c;\n"
706+
" else\n"
707+
"#endif\n"
708+
" {\n"
709+
" foo = d;\n"
710+
"#ifdef FOO\n"
711+
" bar = e;\n"
712+
"#else\n"
713+
" bar = f;\n"
714+
"#endif\n"
715+
" }\n"
716+
"else {\n"
717+
" bar = g;\n"
718+
"}",
719+
Style);
720+
686721
Style.ColumnLimit = 65;
687722
verifyFormat("if (condition) {\n"
688723
" ff(Indices,\n"

0 commit comments

Comments
 (0)