This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Don't allow variable decls to have trailing return arrows
ClosedPublic

Authored by rymiel on Apr 1 2023, 6:43 AM.

Details

Summary

The heuristic for determining if an arrow is a trailing return arrow
looks for the auto keyword, along with parentheses. This isn't
sufficient, since it also triggers on variable declarations with an auto
type, and with an arrow operator.

This patch makes sure a function declaration is being parsed, instead of
any other declaration.

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

Diff Detail

Event Timeline

rymiel created this revision.Apr 1 2023, 6:43 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 1 2023, 6:43 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
rymiel requested review of this revision.Apr 1 2023, 6:43 AM
MyDeveloperDay accepted this revision.Apr 1 2023, 8:01 AM
This revision is now accepted and ready to land.Apr 1 2023, 8:01 AM
owenpan added inline comments.Apr 2 2023, 1:03 AM
clang/lib/Format/TokenAnnotator.cpp
1922

It seems we can simply check if the line might be a function declaration here instead of resetting AutoFound below.

clang/unittests/Format/TokenAnnotatorTest.cpp
1493

Can this be valid C++?

rymiel updated this revision to Diff 510315.Apr 2 2023, 1:13 AM
rymiel marked an inline comment as done.

Use MightBeFunctionDecl instead

clang/lib/Format/TokenAnnotator.cpp
1922

I could swear I specifically tried using that flag and it didn't work in all cases, but I guess I did something wrong back then...

rymiel marked an inline comment as done.Apr 2 2023, 1:16 AM
rymiel added inline comments.
clang/unittests/Format/TokenAnnotatorTest.cpp
1493

Yes! (https://godbolt.org/z/bnYahK4cz) (also I copied it from FormatTest)

rymiel updated this revision to Diff 510317.Apr 2 2023, 1:17 AM
rymiel marked an inline comment as done.
rymiel edited the summary of this revision. (Show Details)

update commit message

owenpan accepted this revision.Apr 2 2023, 1:19 AM
This revision was landed with ongoing or failed builds.Apr 3 2023, 5:49 AM
This revision was automatically updated to reflect the committed changes.