Skip to content

Commit 5cbcc79

Browse files
owencagedare
authored andcommitted
Improve the logic.
1 parent 4a74892 commit 5cbcc79

File tree

1 file changed

+26
-53
lines changed

1 file changed

+26
-53
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,6 +4346,28 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
43464346
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
43474347
return Style.SpacesInParensOptions.InEmptyParentheses;
43484348
}
4349+
if (Style.SpacesInParens == FormatStyle::SIPO_Custom &&
4350+
Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4351+
Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
4352+
auto *InnerLParen = Left.MatchingParen;
4353+
if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
4354+
InnerLParen->SpacesRequiredBefore = 0;
4355+
return false;
4356+
}
4357+
}
4358+
if (Style.SpacesInParensOptions.InConditionalStatements) {
4359+
const FormatToken *LeftParen = nullptr;
4360+
if (Left.is(tok::l_paren))
4361+
LeftParen = &Left;
4362+
else if (Right.is(tok::r_paren) && Right.MatchingParen)
4363+
LeftParen = Right.MatchingParen;
4364+
if (LeftParen) {
4365+
if (LeftParen->is(TT_ConditionLParen))
4366+
return true;
4367+
if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4368+
return true;
4369+
}
4370+
}
43494371

43504372
// trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
43514373
if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
@@ -4372,60 +4394,11 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
43724394
}
43734395

43744396
if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4375-
const FormatToken *LeftParen =
4376-
Left.is(tok::l_paren) ? &Left : Right.MatchingParen;
4377-
const FormatToken *RightParen =
4378-
LeftParen ? LeftParen->MatchingParen : nullptr;
4379-
const auto IsAttributeParen = [](const FormatToken *Paren) {
4380-
return Paren && Paren->isOneOf(TT_AttributeLParen, TT_AttributeRParen);
4381-
};
4382-
auto AddSpaceExceptInDoubleParens = [&]() {
4383-
const auto *RPrev = RightParen ? RightParen->Previous : nullptr;
4384-
const auto *LNext = LeftParen->Next;
4385-
const auto *LPrev = LeftParen->Previous;
4386-
if (!(RPrev && RPrev->is(tok::r_paren) && LNext &&
4387-
LNext->is(tok::l_paren))) {
4388-
return true;
4389-
}
4390-
auto HasEqualBeforeNextParen = [&]() {
4391-
auto *Tok = LNext;
4392-
if (!Tok || !Tok->is(tok::l_paren))
4393-
return false;
4394-
while ((Tok = Tok->Next) && !Tok->isOneOf(tok::l_paren, tok::r_paren))
4395-
if (Tok->is(tok::equal))
4396-
return true;
4397-
return false;
4398-
};
4399-
const bool SuppressSpace =
4400-
IsAttributeParen(LeftParen) ||
4401-
(LPrev && (LPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
4402-
(HasEqualBeforeNextParen() &&
4403-
(LPrev->isOneOf(tok::kw_if, tok::kw_while) ||
4404-
LPrev->endsSequence(tok::kw_constexpr, tok::kw_if)))));
4405-
return !SuppressSpace;
4406-
};
4407-
const auto AddSpace = [&](bool Option) {
4408-
if (Style.SpacesInParensOptions.ExceptDoubleParentheses && Option)
4409-
return AddSpaceExceptInDoubleParens();
4410-
return Option;
4411-
};
4412-
4413-
if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
4414-
(LeftParen->Previous &&
4415-
isKeywordWithCondition(*LeftParen->Previous)))) {
4416-
return AddSpace(Style.SpacesInParensOptions.InConditionalStatements);
4417-
}
4418-
if (RightParen && RightParen->is(TT_CastRParen))
4419-
return AddSpace(Style.SpacesInParensOptions.InCStyleCasts);
4420-
if (IsAttributeParen(LeftParen) || IsAttributeParen(RightParen))
4421-
return AddSpace(Style.SpacesInParensOptions.Other);
4422-
if ((LeftParen && IsAttributeParen(LeftParen->Previous)) ||
4423-
(RightParen && IsAttributeParen(RightParen->Next))) {
4424-
return AddSpace(Style.SpacesInParensOptions.Other);
4425-
}
4426-
return AddSpace(Style.SpacesInParensOptions.Other);
4397+
return (Right.is(TT_CastRParen) ||
4398+
(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4399+
? Style.SpacesInParensOptions.InCStyleCasts
4400+
: Style.SpacesInParensOptions.Other;
44274401
}
4428-
44294402
if (Right.isOneOf(tok::semi, tok::comma))
44304403
return false;
44314404
if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {

0 commit comments

Comments
 (0)