]> granicus.if.org Git - clang/commitdiff
PR4013 and PR4105: pointer-like types can only be cast to/from integers
authorEli Friedman <eli.friedman@gmail.com>
Fri, 1 May 2009 02:23:58 +0000 (02:23 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Fri, 1 May 2009 02:23:58 +0000 (02:23 +0000)
and other pointer-like types.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70531 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/Sema/cast.c
test/Sema/static-init.c

index a416e52fcb034ad9d0ee92cf6829830d6a3a0fd8..6bd5dec7dccf4a2bcc04b881af28d8ca999d4e06 100644 (file)
@@ -1319,6 +1319,10 @@ def ext_typecheck_cast_nonscalar : Extension<
 def ext_typecheck_cast_to_union : Extension<"C99 forbids casts to union type">;
 def err_typecheck_cast_to_union_no_type : Error<
   "cast to union type from type %0 not present in union">;
+def err_cast_pointer_from_non_pointer_int : Error<
+  "operand of type %0 cannot be cast to a pointer type">;
+def err_cast_pointer_to_non_pointer_int : Error<
+  "pointer cannot be cast to type %0">;
 def err_typecheck_expect_scalar_operand : Error<
   "operand of type %0 where arithmetic or pointer type is required">;
 def err_typecheck_cond_incompatible_operands : Error<
index 2de1ef300e9dee1254d003738bd08b9f465be069..66b4c9c6b2c6acbb34632061a589bd461f265a5f 100644 (file)
@@ -2604,6 +2604,17 @@ bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr) {
       return true;
   } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) {
     return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR;
+  } else if (!castType->isArithmeticType()) {
+    QualType castExprType = castExpr->getType();
+    if (!castExprType->isIntegralType() && castExprType->isArithmeticType())
+      return Diag(castExpr->getLocStart(),
+                  diag::err_cast_pointer_from_non_pointer_int)
+        << castExprType << castExpr->getSourceRange();
+  } else if (!castExpr->getType()->isArithmeticType()) {
+    if (!castType->isIntegralType() && castType->isArithmeticType())
+      return Diag(castExpr->getLocStart(),
+                  diag::err_cast_pointer_to_non_pointer_int)
+        << castType << castExpr->getSourceRange();
   }
   return false;
 }
index 6ceec6923c851804095cb7b4721f66fa898ef8a6..ec19626d28e01fb6a72a99d7b84b840ef5925a0e 100644 (file)
@@ -5,4 +5,10 @@ cpumask_t x;
 void foo() {
   (void)x;
 }
+void bar() {
+  char* a;
+  double b;
+  b = (double)a; // expected-error {{pointer cannot be cast to type}}
+  a = (char*)b; // expected-error {{cannot be cast to a pointer type}}
+}
 
index e6592f3bb65301849f2387ae7908b536b0e4563f..99905f05574aa4d97cd405a401cf4f68a7741ea4 100644 (file)
@@ -5,7 +5,7 @@
 static int f = 10;
 static int b = f; // expected-error {{initializer element is not a compile-time constant}}
 
-float r  = (float) &r; // expected-error {{initializer element is not a compile-time constant}}
+float r  = (float) (intptr_t) &r; // expected-error {{initializer element is not a compile-time constant}}
 intptr_t s = (intptr_t) &s;
 _Bool t = &t;