[PATCH] Tune capturing of init-captures in non-generic lambdas

Faisal Vali faisalv at yahoo.com
Sat Nov 2 10:33:49 PDT 2013


Hi rsmith, doug.gregor,



Currently clang does not seem to get capturing of init-captures correctly.

Consider the following code:
    void test() {
       const int x = 10;
       const int y = 12;
       auto L = [&k = x, j = x](int a) {
           return [&l = x, m = y] (int b) { };
       };  
    }

The outer lambda needs to capture 'x' but not 'y'.  And the inner lambda should not capture either x or y.  

This patch fixes this by:
  - moving up the checking of the init capture before its lambda call operator's declcontext, scope, scopeinfo are pushed on (so that lvalue-to-rvalue conversions can be checked for const variables in the init-expr)
  - using the declcontext of the enclosing scope temporarily when the init-variable is created, and then fixing it once its call-operator has been created.  (this does require fixing the linkage computation of init-vars to return none, so that it isn't perceived as ever being declared at namespace scope).
  
Tests added - and regressions tests all seem to pass.

Certain aspects of this patch might emanate a bad smell - lets see if anyone else feels the same ;)

Thanks!

http://llvm-reviews.chandlerc.com/D2092

Files:
  include/clang/AST/Decl.h
  include/clang/Sema/DeclSpec.h
  include/clang/Sema/Sema.h
  lib/AST/Decl.cpp
  lib/Parse/ParseExprCXX.cpp
  lib/Sema/Sema.cpp
  lib/Sema/SemaDecl.cpp
  lib/Sema/SemaLambda.cpp
  lib/Sema/SemaTemplateInstantiate.cpp
  lib/Sema/SemaTemplateVariadic.cpp
  lib/Sema/TreeTransform.h
  test/SemaCXX/cxx1y-init-captures.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2092.1.patch
Type: text/x-patch
Size: 25825 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131102/23ca0913/attachment.bin>


More information about the cfe-commits mailing list