Skip to content

[clang-format] Handle parenthesized list in RemoveParentheses #100852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,7 @@ bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
assert(FormatTok->is(tok::l_paren) && "'(' expected.");
auto *LeftParen = FormatTok;
bool SeenComma = false;
bool SeenEqual = false;
bool MightBeFoldExpr = false;
const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
Expand All @@ -2553,10 +2554,14 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
const auto *Next = Tokens->peekNextToken();
const bool DoubleParens =
Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
const bool CommaSeparated =
!DoubleParens && Prev && Prev->isOneOf(tok::l_paren, tok::comma) &&
Next && Next->isOneOf(tok::comma, tok::r_paren);
const auto *PrevPrev = Prev ? Prev->getPreviousNonComment() : nullptr;
const bool Disallowed =
const bool Excluded =
PrevPrev &&
(PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
SeenComma ||
(SeenEqual &&
(PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
Expand All @@ -2566,7 +2571,8 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
(!NestedLambdas.empty() && !NestedLambdas.back())) &&
Prev && Prev->isOneOf(tok::kw_return, tok::kw_co_return) && Next &&
Next->is(tok::semi);
if ((DoubleParens && !Disallowed) || ReturnParens) {
if ((DoubleParens && !Excluded) || (CommaSeparated && !SeenComma) ||
ReturnParens) {
LeftParen->Optional = true;
FormatTok->Optional = true;
}
Expand Down Expand Up @@ -2595,6 +2601,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
parseBracedList();
}
break;
case tok::comma:
SeenComma = true;
nextToken();
break;
case tok::ellipsis:
MightBeFoldExpr = true;
nextToken();
Expand Down
2 changes: 1 addition & 1 deletion clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
: SourceMgr::DiagKind::DK_Warning,
"code should be clang-formatted [-Wclang-format-violations]");

Diag.print(nullptr, llvm::errs(), (ShowColors && !NoShowColors));
Diag.print(nullptr, llvm::errs(), ShowColors && !NoShowColors);
if (ErrorLimit && ++Errors >= ErrorLimit)
break;
}
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27445,6 +27445,10 @@ TEST_F(FormatTest, RemoveParentheses) {
verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
"static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
Style);
verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
verifyFormat("foo((a, b));", "foo(((a), b));", Style);
verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
verifyFormat("return (0);", "return (((0)));", Style);
verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
Expand Down
2 changes: 1 addition & 1 deletion clang/unittests/Format/MatchFilePathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TEST_F(MatchFilePathTest, Newline) {

TEST_F(MatchFilePathTest, Star) {
EXPECT_TRUE(match(std::string(50, 'a'), "*a*a*a*a*a*a*a*a*a*a"));
EXPECT_FALSE(match((std::string(50, 'a') + 'b'), "*a*a*a*a*a*a*a*a*a*a"));
EXPECT_FALSE(match(std::string(50, 'a') + 'b', "*a*a*a*a*a*a*a*a*a*a"));
}

TEST_F(MatchFilePathTest, CaseSensitive) {
Expand Down
Loading