Browse Source

[coroutines] Evaluate the operand of void `co_return` expressions.

Summary:
Previously Clang incorrectly ignored the expression of a void `co_return`. This patch addresses that bug.

I'm not quite sure if I got the code-gen right, but this patch is at least a start.

Reviewers: rsmith, GorNishanov

Reviewed By: rsmith, GorNishanov

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D36070

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309545 91177308-0d34-0410-b5e6-96231b3b80d8
Eric Fiselier 8 years ago
parent
commit
7e10b22a6c
2 changed files with 21 additions and 0 deletions
  1. 7 0
      lib/CodeGen/CGCoroutine.cpp
  2. 14 0
      test/CodeGenCoroutines/coro-ret-void.cpp

+ 7 - 0
lib/CodeGen/CGCoroutine.cpp

@@ -234,6 +234,13 @@ RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E,
 
 
 void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) {
 void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) {
   ++CurCoro.Data->CoreturnCount;
   ++CurCoro.Data->CoreturnCount;
+  const Expr *RV = S.getOperand();
+  if (RV && RV->getType()->isVoidType()) {
+    // Make sure to evaluate the expression of a co_return with a void
+    // expression for side effects.
+    RunCleanupsScope cleanupScope(*this);
+    EmitIgnoredExpr(RV);
+  }
   EmitStmt(S.getPromiseCall());
   EmitStmt(S.getPromiseCall());
   EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
   EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
 }
 }

+ 14 - 0
test/CodeGenCoroutines/coro-ret-void.cpp

@@ -21,6 +21,20 @@ coro1 f() {
 // CHECK: call void @_ZNSt12experimental13coroutines_v113suspend_never12await_resumeEv(%"struct.std::experimental::coroutines_v1::suspend_never"*
 // CHECK: call void @_ZNSt12experimental13coroutines_v113suspend_never12await_resumeEv(%"struct.std::experimental::coroutines_v1::suspend_never"*
 // CHECK: call void @_ZN5coro112promise_type11return_voidEv(%"struct.coro1::promise_type"* %__promise)
 // CHECK: call void @_ZN5coro112promise_type11return_voidEv(%"struct.coro1::promise_type"* %__promise)
 
 
+struct A {
+  A();
+  ~A();
+};
+
+coro1 f2() {
+  co_return (void) A{};
+}
+
+// CHECK-LABEL: define void @_Z2f2v(
+// CHECK: call void @_ZN1AC1Ev(%struct.A* %[[AVar:.*]])
+// CHECK-NEXT: call void @_ZN1AD1Ev(%struct.A* %[[AVar]])
+// CHECK-NEXT: call void @_ZN5coro112promise_type11return_voidEv(%"struct.coro1::promise_type"*
+
 struct coro2 {
 struct coro2 {
   struct promise_type {
   struct promise_type {
     coro2 get_return_object();
     coro2 get_return_object();