Skip to content

Commit a8dd8f6

Browse files
authored
[clang-format] Fix a bug in SpacesInParens InConditionalStatements (#108797)
Fixes #64416.
1 parent 7153a4b commit a8dd8f6

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4413,31 +4413,29 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
44134413
Right.MatchingParen == &Left && Line.Children.empty()) {
44144414
return Style.SpaceInEmptyBlock;
44154415
}
4416-
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4417-
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
4418-
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
4419-
return Style.SpacesInParensOptions.InEmptyParentheses;
4420-
}
4421-
if (Style.SpacesInParens == FormatStyle::SIPO_Custom &&
4422-
Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4423-
Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
4424-
auto *InnerLParen = Left.MatchingParen;
4425-
if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
4426-
InnerLParen->SpacesRequiredBefore = 0;
4427-
return false;
4416+
if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
4417+
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4418+
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
4419+
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
4420+
return Style.SpacesInParensOptions.InEmptyParentheses;
4421+
}
4422+
if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4423+
Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
4424+
auto *InnerLParen = Left.MatchingParen;
4425+
if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
4426+
InnerLParen->SpacesRequiredBefore = 0;
4427+
return false;
4428+
}
44284429
}
4429-
}
4430-
if (Style.SpacesInParensOptions.InConditionalStatements) {
44314430
const FormatToken *LeftParen = nullptr;
44324431
if (Left.is(tok::l_paren))
44334432
LeftParen = &Left;
44344433
else if (Right.is(tok::r_paren) && Right.MatchingParen)
44354434
LeftParen = Right.MatchingParen;
4436-
if (LeftParen) {
4437-
if (LeftParen->is(TT_ConditionLParen))
4438-
return true;
4439-
if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4440-
return true;
4435+
if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4436+
(LeftParen->Previous &&
4437+
isKeywordWithCondition(*LeftParen->Previous)))) {
4438+
return Style.SpacesInParensOptions.InConditionalStatements;
44414439
}
44424440
}
44434441

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17282,6 +17282,12 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
1728217282
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
1728317283
Spaces.SpacesInParensOptions = {};
1728417284
Spaces.SpacesInParensOptions.Other = true;
17285+
17286+
EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
17287+
verifyFormat("if (a)\n"
17288+
" return;",
17289+
Spaces);
17290+
1728517291
Spaces.SpacesInParensOptions.InConditionalStatements = true;
1728617292
verifyFormat("do_something( ::globalVar );", Spaces);
1728717293
verifyFormat("call( x, y, z );", Spaces);

0 commit comments

Comments
 (0)