[cfe-commits] r150292 - in /cfe/trunk: include/clang/Sema/ScopeInfo.h lib/Sema/SemaExprCXX.cpp test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp
Eli Friedman
eli.friedman at gmail.com
Fri Feb 10 18:51:17 PST 2012
Author: efriedma
Date: Fri Feb 10 20:51:16 2012
New Revision: 150292
URL: http://llvm.org/viewvc/llvm-project?rev=150292&view=rev
Log:
Make sure Sema creates a field for 'this' captures. (Doug, please double-check that this is correct.)
Modified:
cfe/trunk/include/clang/Sema/ScopeInfo.h
cfe/trunk/lib/Sema/SemaExprCXX.cpp
cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp
Modified: cfe/trunk/include/clang/Sema/ScopeInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/ScopeInfo.h?rev=150292&r1=150291&r2=150292&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/ScopeInfo.h (original)
+++ cfe/trunk/include/clang/Sema/ScopeInfo.h Fri Feb 10 20:51:16 2012
@@ -156,8 +156,8 @@
CopyExprAndNested(Cpy, isNested) {}
enum IsThisCapture { ThisCapture };
- Capture(IsThisCapture, bool isNested, SourceLocation Loc)
- : VarAndKind(0, Cap_This), CopyExprAndNested(0, isNested), Loc(Loc) {
+ Capture(IsThisCapture, bool isNested, SourceLocation Loc, Expr *Cpy)
+ : VarAndKind(0, Cap_This), CopyExprAndNested(Cpy, isNested), Loc(Loc) {
}
bool isThisCapture() const { return VarAndKind.getInt() == Cap_This; }
@@ -208,8 +208,8 @@
CaptureMap[Var] = Captures.size();
}
- void AddThisCapture(bool isNested, SourceLocation Loc) {
- Captures.push_back(Capture(Capture::ThisCapture, isNested, Loc));
+ void AddThisCapture(bool isNested, SourceLocation Loc, Expr *Cpy) {
+ Captures.push_back(Capture(Capture::ThisCapture, isNested, Loc, Cpy));
CXXThisCaptureIndex = Captures.size();
}
Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=150292&r1=150291&r2=150292&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Fri Feb 10 20:51:16 2012
@@ -707,8 +707,22 @@
for (unsigned idx = FunctionScopes.size() - 1;
NumClosures; --idx, --NumClosures) {
CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[idx]);
+ Expr *ThisExpr = 0;
+ if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(CSI)) {
+ // For lambda expressions, build a field and an initializing expression.
+ QualType ThisTy = getCurrentThisType();
+ CXXRecordDecl *Lambda = LSI->Lambda;
+ FieldDecl *Field
+ = FieldDecl::Create(Context, Lambda, Loc, Loc, 0, ThisTy,
+ Context.getTrivialTypeSourceInfo(ThisTy, Loc),
+ 0, false, false);
+ Field->setImplicit(true);
+ Field->setAccess(AS_private);
+ Lambda->addDecl(Field);
+ ThisExpr = new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/true);
+ }
bool isNested = NumClosures > 1;
- CSI->AddThisCapture(isNested, Loc);
+ CSI->AddThisCapture(isNested, Loc, ThisExpr);
}
}
Modified: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp?rev=150292&r1=150291&r2=150292&view=diff
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp (original)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp Fri Feb 10 20:51:16 2012
@@ -55,3 +55,11 @@
};
static_assert(sizeof(x) == sizeof(ExpectedLayout), "Layout mismatch!");
}
+
+struct ExpectedThisLayout {
+ ExpectedThisLayout* a;
+ void f() {
+ auto x = [this]() -> void {};
+ static_assert(sizeof(x) == sizeof(ExpectedThisLayout), "Layout mismatch!");
+ }
+};
More information about the cfe-commits
mailing list