From: Douglas Gregor Date: Fri, 15 May 2009 21:56:04 +0000 (+0000) Subject: Template instantiation for do-while statements. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9f3ca2a7747bd47f14d7693f333103fac29a24d2;p=clang Template instantiation for do-while statements. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71899 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 14afcfcd62..8f1771016a 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -687,12 +687,15 @@ class DoStmt : public Stmt { enum { COND, BODY, END_EXPR }; Stmt* SubExprs[END_EXPR]; SourceLocation DoLoc; + SourceLocation WhileLoc; + public: - DoStmt(Stmt *body, Expr *cond, SourceLocation DL) - : Stmt(DoStmtClass), DoLoc(DL) { + DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL) + : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL) { SubExprs[COND] = reinterpret_cast(cond); SubExprs[BODY] = body; DoLoc = DL; + WhileLoc = WL; } /// \brief Build an empty do-while statement. @@ -707,6 +710,8 @@ public: SourceLocation getDoLoc() const { return DoLoc; } void setDoLoc(SourceLocation L) { DoLoc = L; } + SourceLocation getWhileLoc() const { return WhileLoc; } + void setWhileLoc(SourceLocation L) { WhileLoc = L; } virtual SourceRange getSourceRange() const { return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); diff --git a/lib/Frontend/PCHReaderStmt.cpp b/lib/Frontend/PCHReaderStmt.cpp index eb8dab968b..e526f5bd94 100644 --- a/lib/Frontend/PCHReaderStmt.cpp +++ b/lib/Frontend/PCHReaderStmt.cpp @@ -206,6 +206,7 @@ unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) { S->setCond(cast_or_null(StmtStack[StmtStack.size() - 2])); S->setBody(StmtStack.back()); S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); return 2; } diff --git a/lib/Frontend/PCHWriterStmt.cpp b/lib/Frontend/PCHWriterStmt.cpp index af5d563587..827676ad30 100644 --- a/lib/Frontend/PCHWriterStmt.cpp +++ b/lib/Frontend/PCHWriterStmt.cpp @@ -193,6 +193,7 @@ void PCHStmtWriter::VisitDoStmt(DoStmt *S) { Writer.WriteSubStmt(S->getCond()); Writer.WriteSubStmt(S->getBody()); Writer.AddSourceLocation(S->getDoLoc(), Record); + Writer.AddSourceLocation(S->getWhileLoc(), Record); Code = pch::STMT_DO; } diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 652e2a6e0f..8226dd23f9 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -545,19 +545,23 @@ Sema::ActOnDoStmt(SourceLocation DoLoc, StmtArg Body, Expr *condExpr = Cond.takeAs(); assert(condExpr && "ActOnDoStmt(): missing expression"); - DefaultFunctionArrayConversion(condExpr); - Cond = condExpr; - QualType condType = condExpr->getType(); - - if (getLangOptions().CPlusPlus) { - if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 - return StmtError(); - } else if (!condType->isScalarType()) // C99 6.8.5p2 - return StmtError(Diag(DoLoc, diag::err_typecheck_statement_requires_scalar) - << condType << condExpr->getSourceRange()); + if (!condExpr->isTypeDependent()) { + DefaultFunctionArrayConversion(condExpr); + Cond = condExpr; + QualType condType = condExpr->getType(); + + if (getLangOptions().CPlusPlus) { + if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4 + return StmtError(); + } else if (!condType->isScalarType()) // C99 6.8.5p2 + return StmtError(Diag(DoLoc, + diag::err_typecheck_statement_requires_scalar) + << condType << condExpr->getSourceRange()); + } Cond.release(); - return Owned(new (Context) DoStmt(Body.takeAs(), condExpr, DoLoc)); + return Owned(new (Context) DoStmt(Body.takeAs(), condExpr, DoLoc, + WhileLoc)); } Action::OwningStmtResult diff --git a/lib/Sema/SemaTemplateInstantiateStmt.cpp b/lib/Sema/SemaTemplateInstantiateStmt.cpp index 9a4d75e117..c18966a878 100644 --- a/lib/Sema/SemaTemplateInstantiateStmt.cpp +++ b/lib/Sema/SemaTemplateInstantiateStmt.cpp @@ -41,6 +41,7 @@ namespace { OwningStmtResult VisitCompoundStmt(CompoundStmt *S); OwningStmtResult VisitIfStmt(IfStmt *S); OwningStmtResult VisitWhileStmt(WhileStmt *S); + OwningStmtResult VisitDoStmt(DoStmt *S); OwningStmtResult VisitExpr(Expr *E); OwningStmtResult VisitLabelStmt(LabelStmt *S); OwningStmtResult VisitGotoStmt(GotoStmt *S); @@ -171,6 +172,21 @@ Sema::OwningStmtResult TemplateStmtInstantiator::VisitWhileStmt(WhileStmt *S) { return SemaRef.ActOnWhileStmt(S->getWhileLoc(), move(Cond), move(Body)); } +Sema::OwningStmtResult TemplateStmtInstantiator::VisitDoStmt(DoStmt *S) { + // Instantiate the condition + OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); + if (Cond.isInvalid()) + return SemaRef.StmtError(); + + // Instantiate the body + OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs); + if (Body.isInvalid()) + return SemaRef.StmtError(); + + return SemaRef.ActOnDoStmt(S->getDoLoc(), move(Body), S->getWhileLoc(), + move(Cond)); +} + Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) { Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs); if (Result.isInvalid()) diff --git a/test/SemaTemplate/instantiate-function-1.cpp b/test/SemaTemplate/instantiate-function-1.cpp index 3b88700c54..6da25e5984 100644 --- a/test/SemaTemplate/instantiate-function-1.cpp +++ b/test/SemaTemplate/instantiate-function-1.cpp @@ -98,3 +98,17 @@ template struct While0 { }; template struct While0; + +template struct Do0 { + void f(T t) { + do { + } while (t); // expected-error{{not contextually}} + + do { + } while (T t2 = T()); + } +}; + +struct NotConvertibleToBool { }; +template struct Do0; +template struct Do0; // expected-note{{instantiation}}