[clang-format] Do not insert space after new/delete keywords in C function declarations

Fixes https://github.com/llvm/llvm-project/issues/46915.

Reviewed By: curdeius, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D120374
This commit is contained in:
Luis Penagos 2022-02-24 09:48:24 +01:00 committed by Marek Kurdej
parent de462a43d3
commit dbc4d281bd
2 changed files with 14 additions and 5 deletions

View File

@ -3299,11 +3299,15 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch))
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
spaceRequiredBeforeParens(Right);
if (Left.isOneOf(tok::kw_new, tok::kw_delete) ||
(Left.is(tok::r_square) && Left.MatchingParen &&
Left.MatchingParen->Previous &&
Left.MatchingParen->Previous->is(tok::kw_delete)))
return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
if (Left.isOneOf(tok::kw_new, tok::kw_delete))
return ((!Line.MightBeFunctionDecl || !Left.Previous) &&
Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
spaceRequiredBeforeParens(Right);
if (Left.is(tok::r_square) && Left.MatchingParen &&
Left.MatchingParen->Previous &&
Left.MatchingParen->Previous->is(tok::kw_delete))
return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
spaceRequiredBeforeParens(Right);
}
if (Line.Type != LT_PreprocessorDirective &&

View File

@ -9919,6 +9919,11 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
verifyFormat("void operator new(void *foo) ATTRIB;");
verifyFormat("void operator delete[](void *foo) ATTRIB;");
verifyFormat("void operator delete(void *ptr) noexcept;");
EXPECT_EQ("void new(link p);\n"
"void delete(link p);\n",
format("void new (link p);\n"
"void delete (link p);\n"));
}
TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {