Skip to content

Commit 451a664

Browse files
committed
Fix clang-format issue with 'new' and 'delete' keywords in C files
This patch resolves an issue in clang-format where 'new' and 'delete' were incorrectly recognized as keywords in C files. The fix modifies the TokenAnnotator::spaceRequiredBetween to correctly handle 'new' and 'delete' as an identifier in C code, preventing incorrect formatting.
1 parent 60fa2b0 commit 451a664

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
@@ -4607,7 +4607,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
46074607
return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
46084608
spaceRequiredBeforeParens(Right);
46094609
}
4610-
if (!Left.Previous || Left.Previous->isNot(tok::period)) {
4610+
if (!Left.Previous || !Left.Previous->isOneOf(tok::period, tok::arrow)) {
46114611
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
46124612
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
46134613
spaceRequiredBeforeParens(Right);

clang/unittests/Format/FormatTest.cpp

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

11453+
verifyFormat("{ p->delete(); }\n"
11454+
"{ p->new(); }",
11455+
"{ p->delete (); }\n"
11456+
"{ p->new (); }");
11457+
1145311458
FormatStyle AfterPlacementOperator = getLLVMStyle();
1145411459
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
1145511460
EXPECT_TRUE(

0 commit comments

Comments
 (0)