[clang] [coroutines][coro_lifetimebound] Detect lifetime issues with lambda captures (PR #77066)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 12 07:03:01 PST 2024


================
@@ -297,6 +297,26 @@ struct ReadySuspendResumeResult {
   bool IsInvalid;
 };
 
+// Adds [[clang::coro_wrapper]] and [[clang::coro_disable_lifetimebound]]
+// attributes to `get_return_object`.
+static void handleGetReturnObject(Sema &S, MemberExpr *ME) {
+  if (!ME || !ME->getMemberDecl() || !ME->getMemberDecl()->getAsFunction())
+    return;
+  auto *MD = ME->getMemberDecl()->getAsFunction();
+  auto *RetType = MD->getReturnType()->getAsRecordDecl();
+  if (!RetType || !RetType->hasAttr<CoroReturnTypeAttr>())
+    return;
+  // `get_return_object` should be allowed to return coro_return_type.
+  if (!MD->hasAttr<CoroWrapperAttr>())
+    MD->addAttr(
+        CoroWrapperAttr::CreateImplicit(S.getASTContext(), MD->getLocation()));
+  // Object arg of `__promise.get_return_object()` is not lifetimebound.
+  if (RetType->hasAttr<CoroLifetimeBoundAttr>() &&
+      !MD->hasAttr<CoroDisableLifetimeBoundAttr>())
+    MD->addAttr(CoroDisableLifetimeBoundAttr::CreateImplicit(
+        S.getASTContext(), MD->getLocation()));
+}
+
----------------
usx95 wrote:

Moved the check to `makeReturnObject`.

> Then we should be able to get rid of hard-coding the get_return_object name in Sema::CheckCoroutineWrapper too?

`CheckCoroutineWrapper` is called in `ActOnFinishFunctionBody`. At this point, we are in the `promise::get_return_object` definition. It is possible that we have not seen any coroutine associated with this promise until now. So the coroutine statements would not have been generated.

https://github.com/llvm/llvm-project/pull/77066


More information about the cfe-commits mailing list