This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Change heuristic for locating lambda template arguments
ClosedPublic

Authored by rymiel on Aug 20 2022, 6:49 AM.

Details

Summary

Previously, the heuristic was simply to look for template argument-
specific keywords, such as typename, class, template and auto
that are preceded by a left angle bracket <.

This changes the heuristic to instead look for a left angle bracket <
preceded by a right square bracket ], since according to the C++
grammar, the template arguments must *directly* follow the introducer.
(This sort of check might just end up being *too* aggressive)

This patch also adds a bunch more token annotator tests for lambdas,
specifically for some of the stranger forms of lambdas now allowed as
of C++20 or soon-to-be-allowed as part of C++23.

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

This does NOT resolve the FIXME regarding explicit template lists, but
perhaps it gets closer

Diff Detail

Event Timeline

rymiel created this revision.Aug 20 2022, 6:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 20 2022, 6:49 AM
rymiel requested review of this revision.Aug 20 2022, 6:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 20 2022, 6:49 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
rymiel added a project: Restricted Project.Aug 20 2022, 6:49 AM

can you add some format tests to show examples of how you think it should change

rymiel updated this revision to Diff 454304.Aug 21 2022, 6:19 AM

Add a few format tests in addition to the annotator tests

From some incidental testing, I am pretty sure the formatting of lambdas that were mis-annotated as "array subscript" tended to end up being formatted correct-ish, however, I added a test case for the bug which this patch solves, which I should have done earlier.

If none of the previous tests fail then this looks like it might be a good fix.

MyDeveloperDay accepted this revision.Aug 21 2022, 6:53 AM

LGTM but maybe wait for the others

This revision is now accepted and ready to land.Aug 21 2022, 6:53 AM
owenpan accepted this revision.Aug 21 2022, 11:02 PM
owenpan added inline comments.
clang/unittests/Format/FormatTest.cpp
21738

There should be no terminating newline here.

21744

Ditto.

rymiel updated this revision to Diff 454513.Aug 22 2022, 8:39 AM

Remove trailing newlines in added format tests

rymiel marked 2 inline comments as done.Aug 22 2022, 8:39 AM
curdeius accepted this revision.Sep 5 2022, 12:38 AM

@rymiel, please provide your name and email address for the commit message, so that we can land it for you.

Can you please rebase the patch, it doesn't apply anymore.
Sorry for that.

rymiel updated this revision to Diff 458012.Sep 5 2022, 8:23 AM

Rebase on main