This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Correctly annotate */&/&& in operator function calls
ClosedPublic

Authored by owenpan on Jun 29 2023, 11:28 PM.

Details

Summary

Reverts 4986f3f2f220 (but keeps its unit tests) and fixes #49973 differently.

Also fixes bugs that incorrectly annotate the operator keyword as TT_FunctionDeclarationName in function calls and as TT_Unknown in function declarations and definitions.

Diff Detail

Event Timeline

owenpan created this revision.Jun 29 2023, 11:28 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJun 29 2023, 11:29 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
owenpan requested review of this revision.Jun 29 2023, 11:29 PM
owenpan updated this revision to Diff 536139.Jun 30 2023, 1:21 AM

Early break if operator is TT_FunctionDeclarationName.

owenpan updated this revision to Diff 536145.Jun 30 2023, 2:05 AM

Handle operator preceded by return or co-return.

owenpan updated this revision to Diff 536151.Jun 30 2023, 2:21 AM

Handle multiple */&/&& in a single operator function argument.

owenpan updated this revision to Diff 536154.Jun 30 2023, 2:44 AM

Update a unit test to make it more rigorous.

rymiel accepted this revision.Jun 30 2023, 4:57 AM

Thanks, this probably makes more sense than what I did before

clang/lib/Format/TokenAnnotator.cpp
3323

I assume this extra fixup step means that the context of the call's parens won't be set to IsExpression = true? That won't cause any problems?

This revision is now accepted and ready to land.Jun 30 2023, 4:57 AM
MyDeveloperDay accepted this revision.Jun 30 2023, 9:22 AM
owenpan updated this revision to Diff 536501.Jun 30 2023, 8:32 PM

Don't fix the token type of */&/&& if the line is a function declaration.

owenpan added inline comments.Jun 30 2023, 10:56 PM
clang/lib/Format/TokenAnnotator.cpp
3323

This doesn't change IsExpression, which I believe has already been set by the annotator parser.

owenpan updated this revision to Diff 536566.Jul 1 2023, 8:28 PM

Re-annotate */&/&& only if isCpp() is true.

This revision was landed with ongoing or failed builds.Jul 3 2023, 5:49 PM
This revision was automatically updated to reflect the committed changes.
krasimir added a subscriber: krasimir.EditedJul 18 2023, 3:00 AM

It looks like this causes a regression in the following example:

% cat test.cc
struct A {
  std::unique_ptr<B> operator()(const B& b) const;
};
% clang-format -style=google test.cc  # should be the same as the input
struct A {
  std::unique_ptr<B> operator()(const B & b) const;
};

@owenpan, I was planning to temporarily revert this until you have a chance to look into this.
However it seems I'd also have to revert https://github.com/llvm/llvm-project/commit/cc2ff02eadede988ab890b2d0f428bd1531a3803 and https://github.com/llvm/llvm-project/commit/0556ab33532964dd8720b3b3ebd87dfe459d81d2 along as they seem to depend on this commit. Fo you feel that there's a quick fix for this regression that will allow us to fix-forward without reverting all these commits?

Shouldn't that regression already be handled by D155358?

Shouldn't that regression already be handled by D155358?

Ah, I missed that. That fixes it. Thank you!

Shouldn't that regression already be handled by D155358?

nice catch @rymiel!