This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Allow trailing return types in macros
ClosedPublic

Authored by rymiel on Jan 15 2023, 4:55 PM.

Details

Summary

The trailing return type arrow checker verifies that a declaration is
being parsed, however, this isn't true when inside of macros.

It turns out the existence of the auto keyword is enough to make
sure that we're dealing with a trailing return type, and whether we're
in a declaration doesn't matter.

Fixes https://github.com/llvm/llvm-project/issues/47664

Diff Detail

Event Timeline

rymiel created this revision.Jan 15 2023, 4:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 15 2023, 4:55 PM
rymiel requested review of this revision.Jan 15 2023, 4:55 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 15 2023, 4:55 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

What about decltype(auto)?

This revision is now accepted and ready to land.Jan 15 2023, 11:35 PM
rymiel planned changes to this revision.Jan 18 2023, 11:27 PM

What about decltype(auto)?

Turns out this is a problem even without this patch:

decltype(auto) a = (b) -> c;

I'm sure this could somehow be valid c++ syntax, but it does have to be at the top scope. I suppose I could fix it in this patch.

rymiel added a comment.EditedJan 18 2023, 11:38 PM

I suppose this means auto a = (b) -> c; is also technically broken, actually, and there's no easy fix for that.

I did have an idea for adding an additional pass taking place somewhere in TokenAnnotator::calculateFormattingInformation, which would detect trailing return types using a bit more context, as a potential fix for https://github.com/llvm/llvm-project/issues/41495. I suppose I could make that theoretically replace all of this AutoFound logic. I should actually try to see if my idea would work at all though.

edit: nevermind, not as easy as i thought, as operator precedence logic wants to know sooner if an arrow is an operator or a trailing return arrow

What about decltype(auto)?

Turns out this is a problem even without this patch:

decltype(auto) a = (b) -> c;

I'm sure this could somehow be valid c++ syntax, but it does have to be at the top scope. I suppose I could fix it in this patch.

I thought about auto NAME() -> int { return 42; }.

decltype(auto) a = (b) -> c; is something else...

I thought about auto NAME() -> int { return 42; }.

decltype(auto) a = (b) -> c; is something else...

I'm not sure how that affects this? Using decltype(auto) as in decltype(auto) NAME() -> int { return 42; } will recognize the arrow as a trailing return type, but it doesn't really matter as its invalid syntax. I tried to find valid syntax where the arrow would be seen as a trailing return type, and I did with that second example

I thought about auto NAME() -> int { return 42; }.

decltype(auto) a = (b) -> c; is something else...

I'm not sure how that affects this? Using decltype(auto) as in decltype(auto) NAME() -> int { return 42; } will recognize the arrow as a trailing return type, but it doesn't really matter as its invalid syntax. I tried to find valid syntax where the arrow would be seen as a trailing return type, and I did with that second example

I'd just add a test to confirm that it works as expected.

rymiel requested review of this revision.Mar 17 2023, 2:46 AM

Okay, I planned changes because I had more ambitious plans for fixing this, but those didn't work out, so instead I made a separate issue (https://github.com/llvm/llvm-project/issues/61469).
This patch in its current state is a simple fix, so it's probably fine as is

This revision is now accepted and ready to land.Mar 17 2023, 2:46 AM
owenpan accepted this revision.Mar 19 2023, 3:09 AM
owenpan added inline comments.
clang/unittests/Format/FormatTest.cpp
8013–8014

I'd reduce ColumnLimit to 60 for example to get rid of the string concatenation.

rymiel updated this revision to Diff 507451.Mar 22 2023, 11:40 AM

Reduce column limit for macro test

rymiel marked an inline comment as done.Mar 22 2023, 11:40 AM
owenpan accepted this revision.Mar 22 2023, 5:02 PM
This revision was landed with ongoing or failed builds.Mar 23 2023, 10:38 AM
This revision was automatically updated to reflect the committed changes.