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.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
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.
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-reference-coroutine-parameters.rst | ||
---|---|---|
10 | Reference link is usually placed at the end of documentation. |
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 | Add full link to this particular rule: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rcoro-reference-parameters |
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>
Nit: this is called "LangOpts" in 157/159 checks, please rename for consistency.