Skip to content

Commit 9fa17fe

Browse files
authored
[clang-format] Handle parenthesized list in RemoveParentheses (#100852)
Also, reformat clang-format source to remove redundant parentheses enclosing single list items. Fixes #100768.
1 parent 85c5265 commit 9fa17fe

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,7 @@ bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
25342534
bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25352535
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
25362536
auto *LeftParen = FormatTok;
2537+
bool SeenComma = false;
25372538
bool SeenEqual = false;
25382539
bool MightBeFoldExpr = false;
25392540
const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
@@ -2553,10 +2554,14 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25532554
const auto *Next = Tokens->peekNextToken();
25542555
const bool DoubleParens =
25552556
Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
2557+
const bool CommaSeparated =
2558+
!DoubleParens && Prev && Prev->isOneOf(tok::l_paren, tok::comma) &&
2559+
Next && Next->isOneOf(tok::comma, tok::r_paren);
25562560
const auto *PrevPrev = Prev ? Prev->getPreviousNonComment() : nullptr;
2557-
const bool Disallowed =
2561+
const bool Excluded =
25582562
PrevPrev &&
25592563
(PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2564+
SeenComma ||
25602565
(SeenEqual &&
25612566
(PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
25622567
PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
@@ -2566,7 +2571,8 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25662571
(!NestedLambdas.empty() && !NestedLambdas.back())) &&
25672572
Prev && Prev->isOneOf(tok::kw_return, tok::kw_co_return) && Next &&
25682573
Next->is(tok::semi);
2569-
if ((DoubleParens && !Disallowed) || ReturnParens) {
2574+
if ((DoubleParens && !Excluded) || (CommaSeparated && !SeenComma) ||
2575+
ReturnParens) {
25702576
LeftParen->Optional = true;
25712577
FormatTok->Optional = true;
25722578
}
@@ -2595,6 +2601,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25952601
parseBracedList();
25962602
}
25972603
break;
2604+
case tok::comma:
2605+
SeenComma = true;
2606+
nextToken();
2607+
break;
25982608
case tok::ellipsis:
25992609
MightBeFoldExpr = true;
26002610
nextToken();

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
364364
: SourceMgr::DiagKind::DK_Warning,
365365
"code should be clang-formatted [-Wclang-format-violations]");
366366

367-
Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
367+
Diag.print(nullptr, llvm::errs(), ShowColors && !NoShowColors);
368368
if (ErrorLimit && ++Errors >= ErrorLimit)
369369
break;
370370
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27445,6 +27445,10 @@ TEST_F(FormatTest, RemoveParentheses) {
2744527445
verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
2744627446
"static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
2744727447
Style);
27448+
verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
27449+
verifyFormat("foo((a, b));", "foo(((a), b));", Style);
27450+
verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
27451+
verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
2744827452
verifyFormat("return (0);", "return (((0)));", Style);
2744927453
verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
2745027454
verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",

clang/unittests/Format/MatchFilePathTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TEST_F(MatchFilePathTest, Newline) {
5353

5454
TEST_F(MatchFilePathTest, Star) {
5555
EXPECT_TRUE(match(std::string(50, 'a'), "*a*a*a*a*a*a*a*a*a*a"));
56-
EXPECT_FALSE(match((std::string(50, 'a') + 'b'), "*a*a*a*a*a*a*a*a*a*a"));
56+
EXPECT_FALSE(match(std::string(50, 'a') + 'b', "*a*a*a*a*a*a*a*a*a*a"));
5757
}
5858

5959
TEST_F(MatchFilePathTest, CaseSensitive) {

0 commit comments

Comments
 (0)