[clang-format] Fix clang-format issue with 'new' and 'delete' keywords in C files (#85470)

This resolves an issue in clang-format where `new` and `delete` were
incorrectly formatted as keywords in C files. The fix modifies
`TokenAnnotator::spaceRequiredBetween` to handle `new` and `delete` when
used as identifiers for function pointers in structs in C code.
This commit is contained in:
scythris
2024-03-17 21:06:32 +01:00
committed by GitHub
parent 07b18c5e1b
commit 8e5de66af3
2 changed files with 6 additions and 1 deletions

View File

@@ -4613,7 +4613,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
spaceRequiredBeforeParens(Right);
}
if (!Left.Previous || Left.Previous->isNot(tok::period)) {
if (!Left.Previous || !Left.Previous->isOneOf(tok::period, tok::arrow)) {
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
spaceRequiredBeforeParens(Right);

View File

@@ -11475,6 +11475,11 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
"void new (link p);\n"
"void delete (link p);");
verifyFormat("{ p->delete(); }\n"
"{ p->new(); }",
"{ p->delete (); }\n"
"{ p->new (); }");
FormatStyle AfterPlacementOperator = getLLVMStyle();
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
EXPECT_TRUE(