diff options
| author | Douglas Gregor <dgregor@apple.com> | 2009-11-23 23:44:04 +0000 |
|---|---|---|
| committer | Douglas Gregor <dgregor@apple.com> | 2009-11-23 23:44:04 +0000 |
| commit | 633caca35396864f9de09b3dae6e165d917ce698 (patch) | |
| tree | 2a5540826621e653e8bc0e50db0bccc8b9d7ec9c /clang/lib/Sema | |
| parent | 8a9fa3af6911f61fe71df752aaba6d6f292563ae (diff) | |
| download | bcm5719-llvm-633caca35396864f9de09b3dae6e165d917ce698.tar.gz bcm5719-llvm-633caca35396864f9de09b3dae6e165d917ce698.zip | |
Explicitly track the condition variable within an "if" statement,
rather than burying it in a CXXConditionDeclExpr (that occassionally
hides behind implicit conversions). Similar changes for
switch, while, and do-while will follow, then the removal of
CXXConditionDeclExpr. This commit is the canary.
llvm-svn: 89717
Diffstat (limited to 'clang/lib/Sema')
| -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 |
4 files changed, 48 insertions, 5 deletions
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. |

