[clang] [clang-format] Insert a space between new/delete and a C-style cast (PR #106175)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 26 21:05:38 PDT 2024


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/106175

It doesn't make sense to remove the space between new/delete and a C-style cast when SpaceBeforeParensOptions.AfterPlacementOperator is set to false.

Fixes #105628.

>From 8266cfb1d52dd55c710c16d9c57ca974bd94bb9d Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 26 Aug 2024 20:50:42 -0700
Subject: [PATCH] [clang-format] Insert a space between new/delete and a
 C-style cast

It doesn't make sense to remove the space between new/delete and a C-style
cast when SpaceBeforeParensOptions.AfterPlacementOperator is set to false.

Fixes #105628.
---
 clang/lib/Format/TokenAnnotator.cpp   | 4 +++-
 clang/unittests/Format/FormatTest.cpp | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f15330098a2395..d92874266cc36f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4732,7 +4732,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
         Left.isOneOf(tok::kw_new, tok::kw_delete) &&
         Right.isNot(TT_OverloadedOperatorLParen) &&
         !(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
-      return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
+      const auto *RParen = Right.MatchingParen;
+      return Style.SpaceBeforeParensOptions.AfterPlacementOperator ||
+             (RParen && RParen->is(TT_CastRParen));
     }
     if (Line.Type == LT_ObjCDecl)
       return true;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index a383a624434b1f..8887a958d458a3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -11749,6 +11749,7 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
                "};",
                AfterPlacementOperator);
   verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
+  verifyFormat("delete (int *)p;", AfterPlacementOperator);
 
   AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
       false;
@@ -11764,6 +11765,7 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
                "};",
                AfterPlacementOperator);
   verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
+  verifyFormat("delete (int *)p;", AfterPlacementOperator);
 }
 
 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {



More information about the cfe-commits mailing list