This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] fix handling of consecutive unary operators
ClosedPublic

Authored by kevinl on Feb 14 2018, 12:14 PM.

Details

Summary

Code that used to be formatted as if (! + object) { is now formatted as if (!+object) {
(we have a particular object in our codebase where unary operator+ is overloaded to return the underlying value, which in this case is a bool)

We still preserve the TypeScript behavior where ! is a trailing non-null operator. (This is already tested by an existing unit test in FormatTestJS.cpp)

It doesn't appear like handling of consecutive unary operators are tested in general, so I added another test for completeness

Diff Detail

Repository
rL LLVM

Event Timeline

kevinl created this revision.Feb 14 2018, 12:14 PM
djasper edited reviewers, added: krasimir; removed: djasper.Feb 15 2018, 4:52 AM
krasimir added inline comments.Feb 15 2018, 5:14 AM
lib/Format/TokenAnnotator.cpp
1497 ↗(On Diff #134288)

I think that TypeScript has both if (!cond) and x!. I'd expect that if (!+i) {\n} is also handled in the TypeScript case. Could you add a test for this for TypeScript please.

kevinl updated this revision to Diff 134511.Feb 15 2018, 2:29 PM
kevinl edited the summary of this revision. (Show Details)

Simplified the heuristic logic, which extends the fix to TypeScript as well

lib/Format/TokenAnnotator.cpp
1497 ↗(On Diff #134288)

Turns out the comment below was misleading. The x! operator is classified as TT_JsNonNullAssertion and not TT_UnaryOperator, so removing the check for tok::exclaim entirely fixes if (!+i) {\n} in TypeScript as well as C++. Tests are now added for both languages

krasimir accepted this revision.Feb 16 2018, 6:24 AM

Thank you! Do you have commit access or should I commit this for you?

This revision is now accepted and ready to land.Feb 16 2018, 6:24 AM

I do not have commit access.

Would appreciate you committing it for me. Thanks!

@krasimir ping?

would appreciate you committing this for me. thanks!

This revision was automatically updated to reflect the committed changes.