[cfe-commits] r39515 - in /cfe/cfe/trunk: Sema/SemaStmt.cpp include/clang/Basic/DiagnosticKinds.def

Steve Naroff snaroff at apple.com
Wed Jul 11 09:45:11 PDT 2007


Author: snaroff
Date: Wed Jul 11 11:45:11 2007
New Revision: 39515

URL: http://llvm.org/viewvc/llvm-project?rev=39515&view=rev
Log:
Bug #:
Submitted by:
Reviewed by:
Implement type checking for ParseDoStmt, ParseWhileStmt, ParseIfStmt, and
ParseForStmt. ParseForStmt still under construction (contains a FIXME).

Modified:
    cfe/cfe/trunk/Sema/SemaStmt.cpp
    cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaStmt.cpp?rev=39515&r1=39514&r2=39515&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaStmt.cpp Wed Jul 11 11:45:11 2007
@@ -81,28 +81,73 @@
 Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
                   StmtTy *ThenVal, SourceLocation ElseLoc,
                   StmtTy *ElseVal) {
-  return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal);
+  Expr *condExpr = (Expr *)CondVal;
+  assert(condExpr && "ParseIfStmt(): missing expression");
+  
+  QualType condType = condExpr->getType();
+  assert(!condType.isNull() && "ParseIfStmt(): missing expression type");
+  
+  if (!condType->isScalarType()) // C99 6.8.4.1p1
+    return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
+             condType.getAsString(), condExpr->getSourceRange());
+
+  return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
 }
+
 Action::StmtResult
 Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
   return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
 }
 
 Action::StmtResult
-Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){
-  return new WhileStmt((Expr*)Cond, (Stmt*)Body);
+Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
+  Expr *condExpr = (Expr *)Cond;
+  assert(condExpr && "ParseWhileStmt(): missing expression");
+  
+  QualType condType = condExpr->getType();
+  assert(!condType.isNull() && "ParseWhileStmt(): missing expression type");
+  
+  if (!condType->isScalarType()) // C99 6.8.5p2
+    return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
+             condType.getAsString(), condExpr->getSourceRange());
+
+  return new WhileStmt(condExpr, (Stmt*)Body);
 }
 
 Action::StmtResult
 Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
                   SourceLocation WhileLoc, ExprTy *Cond) {
-  return new DoStmt((Stmt*)Body, (Expr*)Cond);
+  Expr *condExpr = (Expr *)Cond;
+  assert(condExpr && "ParseDoStmt(): missing expression");
+  
+  QualType condType = condExpr->getType();
+  assert(!condType.isNull() && "ParseDoStmt(): missing expression type");
+  
+  if (!condType->isScalarType()) // C99 6.8.5p2
+    return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
+             condType.getAsString(), condExpr->getSourceRange());
+
+  return new DoStmt((Stmt*)Body, condExpr);
 }
 
 Action::StmtResult 
 Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, 
                    StmtTy *First, ExprTy *Second, ExprTy *Third,
                    SourceLocation RParenLoc, StmtTy *Body) {
+  if (First) {
+    // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
+    // declaration support to create a DeclStmt node. Once this is done, 
+    // we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
+  }
+  if (Second) {
+    Expr *testExpr = (Expr *)Second;
+    QualType testType = testExpr->getType();
+    assert(!testType.isNull() && "ParseForStmt(): missing test expression type");
+    
+    if (!testType->isScalarType()) // C99 6.8.5p2
+      return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
+               testType.getAsString(), testExpr->getSourceRange());
+  }
   return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
 }
 

Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=39515&r1=39514&r2=39515&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:45:11 2007
@@ -551,7 +551,7 @@
 DIAG(err_typecheck_unary_expr, ERROR,
      "invalid argument type to unary expression '%0'")
 DIAG(err_typecheck_indirection_requires_pointer, ERROR,
-     "indirection requires a pointer operand ('%0' invalid)")
+     "indirection requires pointer operand ('%0' invalid)")
 DIAG(err_typecheck_deref_incomplete_type, ERROR,
      "dereferencing pointer to incomplete type '%0'")
 DIAG(ext_typecheck_deref_ptr_to_void, EXTENSION,
@@ -610,6 +610,8 @@
      "incompatible operand types ('%0' and '%1')")
 DIAG(ext_typecheck_cond_incompatible_pointers, EXTENSION,
      "pointer type mismatch ('%0' and '%1')")
+DIAG(err_typecheck_statement_requires_scalar, ERROR,
+     "statement requires expression of scalar type ('%0' invalid)")
 
 // Statements.
 DIAG(err_continue_not_in_loop, ERROR,





More information about the cfe-commits mailing list