This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Implement CppCoreGuideline CP.53
ClosedPublic

Authored by ccotter on Dec 30 2022, 6:58 PM.

Details

Summary

Implement CppCoreGuideline CP.53 to warn when a coroutine accepts
references parameters. Although the guideline mentions that it is safe
to access a reference parameter before suspension points, the guideline
recommends flagging all coroutine parameter references.

Diff Detail

Event Timeline

ccotter created this revision.Dec 30 2022, 6:58 PM
Herald added a project: Restricted Project. · View Herald Transcript
ccotter requested review of this revision.Dec 30 2022, 6:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 30 2022, 6:58 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Passing and accessing a reference is safe if the access is done before any suspend point (taking into account whether the initial coroutine suspend actually suspends or not). Although CP.53 recommends warning on any reference parameter regardless of whether the code only accesses the reference before any suspend points, I did implement (not in this changeset - in a separate branch) a version of this check that uses the CFG to determine whether reference parameters are never accessed after a suspend point. If there's interest, I could reimplement this check with this (perhaps as a tool option). That said, I do agree with CP.53 that relying on such access being safe is brittle e.g. if the code is refactored in the future to rearrange suspend points, so I went with this approach for the review.

Eugene.Zelenko added inline comments.
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.rst
10

Reference link is usually placed at the end of documentation.

ccotter updated this revision to Diff 485745.Dec 30 2022, 9:24 PM

move reference link

ccotter updated this revision to Diff 485782.Dec 31 2022, 10:34 AM
  • run clang-format
ccotter updated this revision to Diff 485783.Dec 31 2022, 10:53 AM
  • Does not offer fixes
carlosgalvezp added inline comments.Jan 1 2023, 5:55 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.cpp
32

Check for nullptr before dereferencing. Typically it's done like:

if (const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"))
  diag(Param->getBeginLoc(), ...);
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidReferenceCoroutineParametersCheck.h
32

Nit: this is called "LangOpts" in 157/159 checks, please rename for consistency.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.rst
21
ccotter updated this revision to Diff 485824.Jan 1 2023, 3:42 PM
  • Cleanups
ccotter updated this revision to Diff 485827.Jan 1 2023, 4:00 PM
  • oops - bad arc diff / bad change
carlosgalvezp accepted this revision.Jan 2 2023, 2:25 AM

LGTM, thanks for the patch! Maybe wait a couple days before landing to give some extra time for other reviewers. You might also want to mark the comments as "Done", so reviewers can get a quick overview of unresolved issues (if any).

This revision is now accepted and ready to land.Jan 2 2023, 2:25 AM
ccotter marked 4 inline comments as done.Jan 2 2023, 5:12 AM

LGTM, thanks for the patch! Maybe wait a couple days before landing to give some extra time for other reviewers. You might also want to mark the comments as "Done", so reviewers can get a quick overview of unresolved issues (if any).

Done, and thanks! I don't have permissions to land, so could you help land this for me after a few days have passed (assuming now further feedback)? You can use Chris Cotter <ccotter14@bloomberg.net>

This revision was automatically updated to reflect the committed changes.