This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] clang-format eats space in front of attributes for operator delete
ClosedPublic

Authored by MyDeveloperDay on Jan 10 2022, 1:05 AM.

Details

Summary

https://github.com/llvm/llvm-project/issues/27037

Sorry its taken so long to get to this issue! (got it before it hit its 6th birthday!)

void operator delete(void *foo)ATTRIB;

(void *foo) is incorrectly determined to be a C-Style Cast resulting in the space being removed after the ) and before the attrib, due to the detection of

delete (A* )a;

The following was previously unaffected

void operator new(void *foo) ATTRIB;

Fixes #27037

Diff Detail

Event Timeline

MyDeveloperDay requested review of this revision.Jan 10 2022, 1:05 AM
MyDeveloperDay created this revision.
curdeius added inline comments.Jan 10 2022, 2:21 AM
clang/lib/Format/TokenAnnotator.cpp
1729

The current solution looks a bit like a hack to me.
Unless I'm mistaken, casts can appear only in expressions, but not in declarations (except for inside decltype stuff in templates or concepts, but these are still expressions at this level).
Given that void operator delete... is not an expression but a declaration, we shouldn't check rParenEndsCast in this case at all, no?
So, would it be possible to check here for e.g. Line.MightBeFunctionDecl or something like this to avoid it?

Also, how is it that you don't need to special-case new operator?

MyDeveloperDay added inline comments.Jan 10 2022, 2:50 AM
clang/lib/Format/TokenAnnotator.cpp
1729

I will check to see if we can use that..

To answer your second question, the existing code was this...

!isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,tok::kw_delete)

its the kw_delete here that breaks the previous code.

to cover this being a cast

delete (A *)a;

my change is to use more context by avoid operator delete (A* a) a; from being seen as a cast

curdeius added inline comments.Jan 10 2022, 3:16 PM
clang/unittests/Format/FormatTest.cpp
9463

Please add a test for operator delete[].

Line.MightBeFunctionDecl is not true in this case

Added new tests

MyDeveloperDay marked an inline comment as done.

remove additional space

curdeius accepted this revision.Jan 11 2022, 3:40 AM

LGTM. Don't forget to reformat.

It's a pity though that we cannot use MightBeFunctionDecl.

This revision is now accepted and ready to land.Jan 11 2022, 3:40 AM
This revision was landed with ongoing or failed builds.Jan 12 2022, 11:58 PM
This revision was automatically updated to reflect the committed changes.