Author: Owen Pan Date: 2024-05-05T21:33:41-07:00 New Revision: db0ed5533368414b1c4e1c884eef651c66359da2
URL: https://github.com/llvm/llvm-project/commit/db0ed5533368414b1c4e1c884eef651c66359da2 DIFF: https://github.com/llvm/llvm-project/commit/db0ed5533368414b1c4e1c884eef651c66359da2.diff LOG: [clang-format] Don't remove parentheses of fold expressions (#91045) Fixes #90966. Added: Modified: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index b5415fa9ecab55..f71661d837ec3d 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2511,6 +2511,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { assert(FormatTok->is(tok::l_paren) && "'(' expected."); auto *LeftParen = FormatTok; bool SeenEqual = false; + bool MightBeFoldExpr = false; const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace); nextToken(); do { @@ -2522,7 +2523,7 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { parseChildBlock(); break; case tok::r_paren: - if (!MightBeStmtExpr && !Line->InMacroBody && + if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody && Style.RemoveParentheses > FormatStyle::RPS_Leave) { const auto *Prev = LeftParen->Previous; const auto *Next = Tokens->peekNextToken(); @@ -2565,6 +2566,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { parseBracedList(); } break; + case tok::ellipsis: + MightBeFoldExpr = true; + nextToken(); + break; case tok::equal: SeenEqual = true; if (Style.isCSharp() && FormatTok->is(TT_FatArrow)) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 32ba6b6853c799..e6f8e4a06515ea 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -27204,8 +27204,14 @@ TEST_F(FormatTest, RemoveParentheses) { "if ((({ a; })))\n" " b;", Style); + verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));", + "static_assert(((std::is_constructible_v<T, Args &&> && ...)));", + Style); verifyFormat("return (0);", "return (((0)));", Style); verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style); + verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));", + "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", + Style); Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement; verifyFormat("#define Return0 return (0);", Style); @@ -27213,6 +27219,9 @@ TEST_F(FormatTest, RemoveParentheses) { verifyFormat("co_return 0;", "co_return ((0));", Style); verifyFormat("return 0;", "return (((0)));", Style); verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style); + verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);", + "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", + Style); verifyFormat("inline decltype(auto) f() {\n" " if (a) {\n" " return (a);\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits