Skip to content

Commit 8e5de66

Browse files
authored
[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.
1 parent 07b18c5 commit 8e5de66

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4613,7 +4613,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
46134613
return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
46144614
spaceRequiredBeforeParens(Right);
46154615
}
4616-
if (!Left.Previous || Left.Previous->isNot(tok::period)) {
4616+
if (!Left.Previous || !Left.Previous->isOneOf(tok::period, tok::arrow)) {
46174617
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
46184618
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
46194619
spaceRequiredBeforeParens(Right);

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11475,6 +11475,11 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
1147511475
"void new (link p);\n"
1147611476
"void delete (link p);");
1147711477

11478+
verifyFormat("{ p->delete(); }\n"
11479+
"{ p->new(); }",
11480+
"{ p->delete (); }\n"
11481+
"{ p->new (); }");
11482+
1147811483
FormatStyle AfterPlacementOperator = getLLVMStyle();
1147911484
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
1148011485
EXPECT_TRUE(

0 commit comments

Comments
 (0)