diff options
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/CodeGen/CGStmt.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Frontend/PCHReaderStmt.cpp | 1 | ||||
-rw-r--r-- | clang/lib/Frontend/PCHWriterStmt.cpp | 1 | ||||
-rw-r--r-- | clang/lib/Sema/Sema.h | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 21 | ||||
-rw-r--r-- | clang/lib/Sema/SemaStmt.cpp | 13 | ||||
-rw-r--r-- | clang/lib/Sema/TreeTransform.h | 16 |
7 files changed, 52 insertions, 5 deletions
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index b6d7b399045..1c3f3577947 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -294,6 +294,8 @@ void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { void CodeGenFunction::EmitIfStmt(const IfStmt &S) { // C99 6.8.4.1: The first substatement is executed if the expression compares // unequal to 0. The condition must be a scalar type. + if (S.getConditionVariable()) + EmitDecl(*S.getConditionVariable()); // If the condition constant folds and can be elided, try to avoid emitting // the condition and the dead arm of the if/else. diff --git a/clang/lib/Frontend/PCHReaderStmt.cpp b/clang/lib/Frontend/PCHReaderStmt.cpp index 01af67dd50a..fb95584b2a8 100644 --- a/clang/lib/Frontend/PCHReaderStmt.cpp +++ b/clang/lib/Frontend/PCHReaderStmt.cpp @@ -177,6 +177,7 @@ unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) { unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) { VisitStmt(S); + S->setConditionVariable(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3])); S->setThen(StmtStack[StmtStack.size() - 2]); S->setElse(StmtStack[StmtStack.size() - 1]); diff --git a/clang/lib/Frontend/PCHWriterStmt.cpp b/clang/lib/Frontend/PCHWriterStmt.cpp index 78a56db7edc..8de094a2967 100644 --- a/clang/lib/Frontend/PCHWriterStmt.cpp +++ b/clang/lib/Frontend/PCHWriterStmt.cpp @@ -170,6 +170,7 @@ void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) { void PCHStmtWriter::VisitIfStmt(IfStmt *S) { VisitStmt(S); + Writer.AddDeclRef(S->getConditionVariable(), Record); Writer.WriteSubStmt(S->getCond()); Writer.WriteSubStmt(S->getThen()); Writer.WriteSubStmt(S->getElse()); diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 9e20771b826..1df1ec681e0 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -1879,7 +1879,8 @@ public: Declarator &D, SourceLocation EqualLoc, ExprArg AssignExprVal); - + OwningExprResult CheckConditionVariable(VarDecl *ConditionVar); + /// ActOnUnaryTypeTrait - Parsed one of the unary type trait support /// pseudo-functions. virtual OwningExprResult ActOnUnaryTypeTrait(UnaryTypeTrait OTT, diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 21ab093a1a5..f2b82aa98ba 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -1002,6 +1002,27 @@ Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc, VD)); } +/// \brief Check the use of the given variable as a C++ condition in an if, +/// while, do-while, or switch statement. +Action::OwningExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar) { + QualType T = ConditionVar->getType(); + + // C++ [stmt.select]p2: + // The declarator shall not specify a function or an array. + if (T->isFunctionType()) + return ExprError(Diag(ConditionVar->getLocation(), + diag::err_invalid_use_of_function_type) + << ConditionVar->getSourceRange()); + else if (T->isArrayType()) + return ExprError(Diag(ConditionVar->getLocation(), + diag::err_invalid_use_of_array_type) + << ConditionVar->getSourceRange()); + + return Owned(DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, + ConditionVar->getLocation(), + ConditionVar->getType().getNonReferenceType())); +} + /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { // C++ 6.4p4: diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index ad3376b9c82..cecc9b32c10 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -242,8 +242,17 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, OwningExprResult CondResult(CondVal.release()); Expr *condExpr = CondResult.takeAs<Expr>(); - assert(condExpr && "ActOnIfStmt(): missing expression"); + + VarDecl *ConditionVar = 0; + if (CXXConditionDeclExpr *Cond = dyn_cast<CXXConditionDeclExpr>(condExpr)) { + ConditionVar = Cond->getVarDecl(); + condExpr = DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, + ConditionVar->getLocation(), + ConditionVar->getType().getNonReferenceType()); + // FIXME: Leaks the old condExpr + } + if (CheckBooleanCondition(condExpr, IfLoc)) { CondResult = condExpr; return StmtError(); @@ -265,7 +274,7 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, FullExprArg CondVal, DiagnoseUnusedExprResult(elseStmt); CondResult.release(); - return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt, + return Owned(new (Context) IfStmt(IfLoc, ConditionVar, condExpr, thenStmt, ElseLoc, elseStmt)); } diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index ecac31a3e7a..d226c1eb5c2 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -3084,10 +3084,22 @@ template<typename Derived> Sema::OwningStmtResult TreeTransform<Derived>::TransformIfStmt(IfStmt *S) { // Transform the condition - OwningExprResult Cond = getDerived().TransformExpr(S->getCond()); + OwningExprResult Cond(SemaRef); + VarDecl *ConditionVar = 0; + if (S->getConditionVariable()) { + ConditionVar + = cast_or_null<VarDecl>( + getDerived().TransformDefinition(S->getConditionVariable())); + if (!ConditionVar) + return SemaRef.StmtError(); + + Cond = getSema().CheckConditionVariable(ConditionVar); + } else + Cond = getDerived().TransformExpr(S->getCond()); + if (Cond.isInvalid()) return SemaRef.StmtError(); - + Sema::FullExprArg FullCond(getSema().FullExpr(Cond)); // Transform the "then" branch. |