From ed7b27830d75b611c324307f21f1fe52f3f871de Mon Sep 17 00:00:00 2001 From: John McCall Date: Fri, 6 Apr 2012 18:20:53 +0000 Subject: [PATCH] Fix a Sema invariant bug that I recently introduced involving the template instantiation of statement-expressions. I think it was jyasskin who had a crashing testcase in this area; hopefully this fixes it and he can find his testcase and check it in. llvm-svn: 154189 --- clang/lib/Sema/SemaExpr.cpp | 3 +++ clang/lib/Sema/TreeTransform.h | 10 ++++++++-- clang/test/CodeGenObjCXX/arc.mm | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 709b944447e0..82d23783f917 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -8567,6 +8567,9 @@ void Sema::ActOnStartStmtExpr() { } void Sema::ActOnStmtExprError() { + // Note that function is also called by TreeTransform when leaving a + // StmtExpr scope without rebuilding anything. + DiscardCleanupsInEvaluationContext(); PopExpressionEvaluationContext(); } diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index f16b667a658d..fdb861eea51b 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6736,14 +6736,20 @@ TreeTransform::TransformAddrLabelExpr(AddrLabelExpr *E) { template ExprResult TreeTransform::TransformStmtExpr(StmtExpr *E) { + SemaRef.ActOnStartStmtExpr(); StmtResult SubStmt = getDerived().TransformCompoundStmt(E->getSubStmt(), true); - if (SubStmt.isInvalid()) + if (SubStmt.isInvalid()) { + SemaRef.ActOnStmtExprError(); return ExprError(); + } if (!getDerived().AlwaysRebuild() && - SubStmt.get() == E->getSubStmt()) + SubStmt.get() == E->getSubStmt()) { + // Calling this an 'error' is unintuitive, but it does the right thing. + SemaRef.ActOnStmtExprError(); return SemaRef.MaybeBindToTemporary(E); + } return getDerived().RebuildStmtExpr(E->getLParenLoc(), SubStmt.get(), diff --git a/clang/test/CodeGenObjCXX/arc.mm b/clang/test/CodeGenObjCXX/arc.mm index 7b7429085fbf..45211a2c19ab 100644 --- a/clang/test/CodeGenObjCXX/arc.mm +++ b/clang/test/CodeGenObjCXX/arc.mm @@ -241,3 +241,14 @@ Test37 *instantiate_init() { // CHECK: call i8* @objc_autoreleaseReturnValue template Test37* instantiate_init(); +// Just make sure that the AST invariants hold properly here, +// i.e. that we don't crash. +// The block should get bound in the full-expression outside +// the statement-expression. +template class Test38 { + void test(T x) { + ^{ (void) x; }, ({ x; }); + } +}; +// CHECK: define weak_odr void @_ZN6Test38IiE4testEi( +template class Test38;