Skip to content

Commit dbc4d28

Browse files
penagosmkurdej
authored andcommitted
[clang-format] Do not insert space after new/delete keywords in C function declarations
Fixes #46915. Reviewed By: curdeius, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D120374
1 parent de462a4 commit dbc4d28

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,11 +3299,15 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
32993299
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch))
33003300
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
33013301
spaceRequiredBeforeParens(Right);
3302-
if (Left.isOneOf(tok::kw_new, tok::kw_delete) ||
3303-
(Left.is(tok::r_square) && Left.MatchingParen &&
3304-
Left.MatchingParen->Previous &&
3305-
Left.MatchingParen->Previous->is(tok::kw_delete)))
3306-
return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
3302+
if (Left.isOneOf(tok::kw_new, tok::kw_delete))
3303+
return ((!Line.MightBeFunctionDecl || !Left.Previous) &&
3304+
Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
3305+
spaceRequiredBeforeParens(Right);
3306+
3307+
if (Left.is(tok::r_square) && Left.MatchingParen &&
3308+
Left.MatchingParen->Previous &&
3309+
Left.MatchingParen->Previous->is(tok::kw_delete))
3310+
return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
33073311
spaceRequiredBeforeParens(Right);
33083312
}
33093313
if (Line.Type != LT_PreprocessorDirective &&

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9919,6 +9919,11 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
99199919
verifyFormat("void operator new(void *foo) ATTRIB;");
99209920
verifyFormat("void operator delete[](void *foo) ATTRIB;");
99219921
verifyFormat("void operator delete(void *ptr) noexcept;");
9922+
9923+
EXPECT_EQ("void new(link p);\n"
9924+
"void delete(link p);\n",
9925+
format("void new (link p);\n"
9926+
"void delete (link p);\n"));
99229927
}
99239928

99249929
TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {

0 commit comments

Comments
 (0)