[PATCH] D24639: [Sema] Warn when returning a lambda that captures a local variable by reference

Erik Pilkington via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 12 05:31:25 PST 2016


erik.pilkington added inline comments.


================
Comment at: test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp:185
           f(y, d);
-          f(z, d);
+          f(z, d); // expected-warning {{address of stack memory associated with local variable 'z' returned}}
           decltype(a) A = a;
----------------
rsmith wrote:
> This diagnostic looks like it'll be confusing. Can you add a note pointing at the `return` statement in question (we're warning about the one on line 179, I assume)?
OK, in the new patch we emit the warning at the 'return', and a note at the capture location.


================
Comment at: test/SemaCXX/cxx1y-init-captures.cpp:123-124
       };
-      return M;
+      // FIXME: We shouldn't emit this warning here.
+      return M; // expected-warning{{stack memory}}
     };
----------------
rsmith wrote:
> Do you know why this happens? It looks like `EvalVal` should bail out on the `DeclRefExpr` in the init-capture, because it `refersToEnclosingVariableOrCapture`.
For some reason `refersToEnclosingVariableOrCapture` isn't set here, which seems like a bug to me. The new patch works around this by warning only if the variable that we're capturing is declared in the same context as the return statement.


https://reviews.llvm.org/D24639





More information about the cfe-commits mailing list