[cfe-commits] r70531 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/cast.c test/Sema/static-init.c

Eli Friedman eli.friedman at gmail.com
Thu Apr 30 19:23:59 PDT 2009


Author: efriedma
Date: Thu Apr 30 21:23:58 2009
New Revision: 70531

URL: http://llvm.org/viewvc/llvm-project?rev=70531&view=rev
Log:
PR4013 and PR4105: pointer-like types can only be cast to/from integers 
and other pointer-like types.


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

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=70531&r1=70530&r2=70531&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Apr 30 21:23:58 2009
@@ -1319,6 +1319,10 @@
 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<

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Apr 30 21:23:58 2009
@@ -2604,6 +2604,17 @@
       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;
 }

Modified: cfe/trunk/test/Sema/cast.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/cast.c?rev=70531&r1=70530&r2=70531&view=diff

==============================================================================
--- cfe/trunk/test/Sema/cast.c (original)
+++ cfe/trunk/test/Sema/cast.c Thu Apr 30 21:23:58 2009
@@ -5,4 +5,10 @@
 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}}
+}
 

Modified: cfe/trunk/test/Sema/static-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/static-init.c?rev=70531&r1=70530&r2=70531&view=diff

==============================================================================
--- cfe/trunk/test/Sema/static-init.c (original)
+++ cfe/trunk/test/Sema/static-init.c Thu Apr 30 21:23:58 2009
@@ -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;
 





More information about the cfe-commits mailing list