[cfe-commits] r39943 - in /cfe/trunk: Sema/SemaExpr.cpp include/clang/Basic/DiagnosticKinds.def test/Parser/check_cast.c

Steve Naroff snaroff at apple.com
Mon Jul 16 16:25:19 PDT 2007


Author: snaroff
Date: Mon Jul 16 18:25:18 2007
New Revision: 39943

URL: http://llvm.org/viewvc/llvm-project?rev=39943&view=rev
Log:

Implement semantic analysis for the cast operator.


Added:
    cfe/trunk/test/Parser/check_cast.c
Modified:
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=39943&r1=39942&r2=39943&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Mon Jul 16 18:25:18 2007
@@ -473,10 +473,22 @@
 Action::ExprResult Sema::
 ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
               SourceLocation RParenLoc, ExprTy *Op) {
-  // If error parsing type, ignore.
-  assert((Ty != 0) && "ParseCastExpr(): missing type");
-  // FIXME: Sema for cast is completely missing.
-  return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
+  assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");
+
+  Expr *castExpr = static_cast<Expr*>(Op);
+  QualType castType = QualType::getFromOpaquePtr(Ty);
+
+  // C99 6.5.4p2: both the cast type and expression type need to be scalars.
+  if (!castType->isScalarType()) { 
+    return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar, 
+                castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
+  }
+  if (!castExpr->getType()->isScalarType()) {
+    return Diag(castExpr->getLocStart(), 
+                diag::err_typecheck_expect_scalar_operand, 
+                castExpr->getType().getAsString(), castExpr->getSourceRange());
+  }
+  return new CastExpr(castType, castExpr, LParenLoc);
 }
 
 inline QualType Sema::CheckConditionalOperands( // C99 6.5.15

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Jul 16 18:25:18 2007
@@ -634,6 +634,8 @@
      "passing '%0' to '%1' discards qualifiers")
 DIAG(err_typecheck_cond_expect_scalar, ERROR,
      "used type '%0' where arithmetic or pointer type is required")
+DIAG(err_typecheck_expect_scalar_operand, ERROR,
+     "operand of type '%0' where arithmetic or pointer type is required")
 DIAG(err_typecheck_cond_incompatible_operands, ERROR,
      "incompatible operand types ('%0' and '%1')")
 DIAG(ext_typecheck_cond_incompatible_pointers, EXTENSION,

Added: cfe/trunk/test/Parser/check_cast.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/check_cast.c?rev=39943&view=auto

==============================================================================
--- cfe/trunk/test/Parser/check_cast.c (added)
+++ cfe/trunk/test/Parser/check_cast.c Mon Jul 16 18:25:18 2007
@@ -0,0 +1,12 @@
+// RUN: clang -parse-ast-check %s
+struct foo {
+  int a;
+};
+
+int main() {
+  struct foo xxx;
+  int i;
+
+  xxx = (struct foo)1;  // expected-error {{used type 'struct foo' where arithmetic or pointer type is required}}
+  i = (int)xxx; // expected-error {{operand of type 'struct foo' where arithmetic or pointer type is required}}
+}





More information about the cfe-commits mailing list