aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-11-23 23:44:04 +0000
committerDouglas Gregor <dgregor@apple.com>2009-11-23 23:44:04 +0000
commit8cfe5a784133d90bf329fd20801824a6f71bb8ca (patch)
tree074054ca6c3e71e178729248da6006d569804241
parent21affc089773edc30be43ecb0022a48acdac7093 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89717 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/AST/Stmt.h22
-rw-r--r--lib/CodeGen/CGStmt.cpp2
-rw-r--r--lib/Frontend/PCHReaderStmt.cpp1
-rw-r--r--lib/Frontend/PCHWriterStmt.cpp1
-rw-r--r--lib/Sema/Sema.h3
-rw-r--r--lib/Sema/SemaExprCXX.cpp21
-rw-r--r--lib/Sema/SemaStmt.cpp13
-rw-r--r--lib/Sema/TreeTransform.h16
8 files changed, 70 insertions, 9 deletions
diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h
index 411f215e91..609c7cdb1d 100644
--- a/include/clang/AST/Stmt.h
+++ b/include/clang/AST/Stmt.h
@@ -604,22 +604,36 @@ public:
class IfStmt : public Stmt {
enum { COND, THEN, ELSE, END_EXPR };
Stmt* SubExprs[END_EXPR];
+
+ /// \brief If non-NULL, the declaration in the "if" statement.
+ VarDecl *Var;
+
SourceLocation IfLoc;
SourceLocation ElseLoc;
+
public:
- IfStmt(SourceLocation IL, Expr *cond, Stmt *then,
+ IfStmt(SourceLocation IL, VarDecl *Var, Expr *cond, Stmt *then,
SourceLocation EL = SourceLocation(), Stmt *elsev = 0)
- : Stmt(IfStmtClass) {
+ : Stmt(IfStmtClass), Var(Var), IfLoc(IL), ElseLoc(EL) {
SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
SubExprs[THEN] = then;
SubExprs[ELSE] = elsev;
- IfLoc = IL;
- ElseLoc = EL;
}
/// \brief Build an empty if/then/else statement
explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
+ /// \brief Retrieve the variable declared in this "if" statement, if any.
+ ///
+ /// In the following example, "x" is the condition variable.
+ /// \code
+ /// if (int x = foo()) {
+ /// printf("x is %d", x);
+ /// }
+ /// \endcode
+ VarDecl *getConditionVariable() const { return Var; }
+ void setConditionVariable(VarDecl *V) { Var = V; }
+
const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
const Stmt *getThen() const { return SubExprs[THEN]; }
diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index b6d7b39904..1c3f357794 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/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/lib/Frontend/PCHReaderStmt.cpp b/lib/Frontend/PCHReaderStmt.cpp
index 01af67dd50..fb95584b2a 100644
--- a/lib/Frontend/PCHReaderStmt.cpp
+++ b/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/lib/Frontend/PCHWriterStmt.cpp b/lib/Frontend/PCHWriterStmt.cpp
index 78a56db7ed..8de094a296 100644
--- a/lib/Frontend/PCHWriterStmt.cpp
+++ b/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/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 9e20771b82..1df1ec681e 100644
--- a/lib/Sema/Sema.h
+++ b/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/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 21ab093a1a..f2b82aa98b 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/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/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index ad3376b9c8..cecc9b32c1 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/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/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index ecac31a3e7..d226c1eb5c 100644
--- a/lib/Sema/TreeTransform.h
+++ b/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.