[llvm-branch-commits] [clang] release/20.x: [clang-format] RemoveParentheses shouldn't remove empty… (PR #138380)

Owen Pan via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat May 3 01:37:07 PDT 2025


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/138380

>From 4b85fd08252bfe37382d4f9a7ecde898422830c7 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 2 May 2025 19:19:39 -0700
Subject: [PATCH 1/2] release/20.x: [clang-format] RemoveParentheses shouldn't
 remove empty parentheses (#138229)

Backport d3506ee573a2aa1403817642ef45f8c0305bb572
---
 clang/lib/Format/UnwrappedLineParser.cpp | 3 ++-
 clang/unittests/Format/FormatTest.cpp    | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 2b348c926294e..c3ffabce15ec8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2581,7 +2581,8 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
       if (Prev) {
         auto OptionalParens = [&] {
           if (MightBeStmtExpr || MightBeFoldExpr || Line->InMacroBody ||
-              SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave) {
+              SeenComma || Style.RemoveParentheses == FormatStyle::RPS_Leave ||
+              RParen->getPreviousNonComment() == LParen) {
             return false;
           }
           const bool DoubleParens =
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index bf3eff129efd5..49e1fde1d9ccf 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27895,6 +27895,8 @@ TEST_F(FormatTest, RemoveParentheses) {
   verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
                "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
                Style);
+  verifyFormat("MOCK_METHOD(void, Function, (), override);",
+               "MOCK_METHOD(void, Function, (), (override));", Style);
 
   Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
   verifyFormat("#define Return0 return (0);", Style);

>From 0e88d0b2e6c88c3996ec72f31e211c91976cb7b0 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 2 May 2025 19:20:42 -0700
Subject: [PATCH 2/2] release/20.x: [clang-format] Fix a crash on formatting
 missing r_paren/r_brace (#138230)

Backport 79210feb2993ff9a79ef11f8a7016a527d4fcf22
---
 clang/lib/Format/UnwrappedLineParser.cpp | 4 ++--
 clang/unittests/Format/FormatTest.cpp    | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index c3ffabce15ec8..673b3e6c4b8c2 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1837,8 +1837,8 @@ void UnwrappedLineParser::parseStructuralElement(
       nextToken();
       if (FormatTok->is(tok::l_paren)) {
         parseParens();
-        assert(FormatTok->Previous);
-        if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
+        if (FormatTok->Previous &&
+            FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
                                               tok::l_paren)) {
           Line->SeenDecltypeAuto = true;
         }
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 49e1fde1d9ccf..90a79230e9f4c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13962,6 +13962,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyNoCrash("struct Foo {\n"
                 "  operator foo(bar\n"
                 "};");
+  verifyNoCrash("decltype( {\n"
+                "  {");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {



More information about the llvm-branch-commits mailing list