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 <owenpi...@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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits