Skip to content

Commit

Permalink
[clang-format] Fix clang-format issue with 'new' and 'delete' keyword…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
scythris committed Mar 17, 2024
1 parent 07b18c5 commit 8e5de66
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8e5de66

Please sign in to comment.