This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Correctly annotate operator free function call
ClosedPublic

Authored by rymiel on Jun 26 2023, 11:54 AM.

Details

Summary

The annotator correctly annotates an overloaded operator call when
called as a member function, like x.operator+(y), however, when called
as a free function, like operator+(x, y), the annotator assumed it was
an overloaded operator function *declaration*, instead of a call.

This patch allows for a free function call to correctly be annotated as
a call, but only if the current like cannot be a declaration, usually
within the bodies of a function.

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

Diff Detail

Event Timeline

rymiel created this revision.Jun 26 2023, 11:54 AM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJun 26 2023, 11:54 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
rymiel requested review of this revision.Jun 26 2023, 11:54 AM
Nuullll accepted this revision.Jun 27 2023, 2:39 AM
Nuullll added a subscriber: Nuullll.

Thanks, LGTM!

clang/lib/Format/TokenAnnotator.cpp
312–313

minor. Probably better to rename it to something like IsOperatorCallSite.

This revision is now accepted and ready to land.Jun 27 2023, 2:39 AM
MyDeveloperDay accepted this revision.Jun 27 2023, 3:57 AM
HazardyKnusperkeks added inline comments.
clang/lib/Format/TokenAnnotator.cpp
312–313

The Name should really be changes, since it's not limited to member functions anymore.

owenpan added inline comments.Jun 28 2023, 12:48 AM
clang/lib/Format/TokenAnnotator.cpp
314

Why not Line.InMacroBody? Wouldn't it misformat the following snippet?

#define FOO           \
  void foo() {        \
    operator+(a * b); \
  }
rymiel added inline comments.Jun 28 2023, 7:53 AM
clang/lib/Format/TokenAnnotator.cpp
314

Yes, but it would break this test case: https://github.com/llvm/llvm-project/blob/e469d0d636f36140b08d0b5f603c043008307bcf/clang/unittests/Format/FormatTest.cpp#L11573

I understand it's a nasty workaround, though, but it's quick. The other option seems to be rewriting how overloaded operators are annotated to instead be more like regular function declarations, but I haven't gauged how hard that would be

owenpan accepted this revision.Jun 29 2023, 1:11 AM
owenpan added inline comments.
clang/lib/Format/TokenAnnotator.cpp
314

Yeah. Can you add the snippet as a FIXME test case (under #if 0)?

rymiel updated this revision to Diff 535859.Jun 29 2023, 9:45 AM

Address comments

rymiel marked 4 inline comments as done.Jun 29 2023, 9:45 AM
This revision was landed with ongoing or failed builds.Jun 29 2023, 9:53 AM
This revision was automatically updated to reflect the committed changes.