From 66b46be52f82addd4edab3a54928e111dfa09de7 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 20 May 2009 22:57:03 +0000 Subject: [PATCH] Fix template instantiation for compound statements so that it properly passes the "isStmtExpr" flag, to suppress warnings about unused expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72190 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/Sema.h | 3 ++ lib/Sema/SemaTemplateInstantiateExpr.cpp | 4 +-- lib/Sema/SemaTemplateInstantiateStmt.cpp | 42 +++++++++++++++--------- test/SemaTemplate/instantiate-expr-3.cpp | 2 +- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index e16f544abb..9fc54e4149 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -2188,6 +2188,9 @@ public: OwningStmtResult InstantiateStmt(Stmt *S, const TemplateArgumentList &TemplateArgs); + OwningStmtResult InstantiateCompoundStmt(CompoundStmt *S, + const TemplateArgumentList &TemplateArgs, + bool isStmtExpr); Decl *InstantiateDecl(Decl *D, DeclContext *Owner, const TemplateArgumentList &TemplateArgs); diff --git a/lib/Sema/SemaTemplateInstantiateExpr.cpp b/lib/Sema/SemaTemplateInstantiateExpr.cpp index a93ad1132a..8100ba421b 100644 --- a/lib/Sema/SemaTemplateInstantiateExpr.cpp +++ b/lib/Sema/SemaTemplateInstantiateExpr.cpp @@ -486,8 +486,8 @@ TemplateExprInstantiator::VisitConditionalOperator(ConditionalOperator *E) { } Sema::OwningExprResult TemplateExprInstantiator::VisitStmtExpr(StmtExpr *E) { - Sema::OwningStmtResult SubStmt = SemaRef.InstantiateStmt(E->getSubStmt(), - TemplateArgs); + Sema::OwningStmtResult SubStmt + = SemaRef.InstantiateCompoundStmt(E->getSubStmt(), TemplateArgs, true); if (SubStmt.isInvalid()) return SemaRef.ExprError(); diff --git a/lib/Sema/SemaTemplateInstantiateStmt.cpp b/lib/Sema/SemaTemplateInstantiateStmt.cpp index e83718e33d..d635dff94c 100644 --- a/lib/Sema/SemaTemplateInstantiateStmt.cpp +++ b/lib/Sema/SemaTemplateInstantiateStmt.cpp @@ -136,22 +136,7 @@ TemplateStmtInstantiator::VisitReturnStmt(ReturnStmt *S) { Sema::OwningStmtResult TemplateStmtInstantiator::VisitCompoundStmt(CompoundStmt *S) { - ASTOwningVector<&ActionBase::DeleteStmt> Statements(SemaRef); - - for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end(); - B != BEnd; ++B) { - OwningStmtResult Result = Visit(*B); - if (Result.isInvalid()) - return SemaRef.StmtError(); - - Statements.push_back(Result.takeAs()); - } - - return SemaRef.ActOnCompoundStmt(S->getLBracLoc(), S->getRBracLoc(), - Sema::MultiStmtArg(SemaRef, - Statements.take(), - Statements.size()), - /*isStmtExpr=*/false); + return SemaRef.InstantiateCompoundStmt(S, TemplateArgs, false); } Sema::OwningStmtResult @@ -437,3 +422,28 @@ Sema::InstantiateStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) { TemplateStmtInstantiator Instantiator(*this, TemplateArgs); return Instantiator.Visit(S); } + +Sema::OwningStmtResult +Sema::InstantiateCompoundStmt(CompoundStmt *S, + const TemplateArgumentList &TemplateArgs, + bool isStmtExpr) { + if (!S) + return Owned((Stmt *)0); + + TemplateStmtInstantiator Instantiator(*this, TemplateArgs); + ASTOwningVector<&ActionBase::DeleteStmt> Statements(*this); + for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end(); + B != BEnd; ++B) { + OwningStmtResult Result = Instantiator.Visit(*B); + if (Result.isInvalid()) + return StmtError(); + + Statements.push_back(Result.takeAs()); + } + + return ActOnCompoundStmt(S->getLBracLoc(), S->getRBracLoc(), + Sema::MultiStmtArg(*this, + Statements.take(), + Statements.size()), + isStmtExpr); +} diff --git a/test/SemaTemplate/instantiate-expr-3.cpp b/test/SemaTemplate/instantiate-expr-3.cpp index 69d2fe715d..03386e1873 100644 --- a/test/SemaTemplate/instantiate-expr-3.cpp +++ b/test/SemaTemplate/instantiate-expr-3.cpp @@ -63,7 +63,7 @@ template struct Conditional0; template struct StatementExpr0 { void f(T t) { - (void)({ if (t) t = t + 17; }); // expected-error{{invalid}} + (void)({ if (t) t = t + 17; t + 12;}); // expected-error{{invalid}} } }; -- 2.40.0