PR4013 and PR4105: pointer-like types can only be cast to/from integers

and other pointer-like types.

llvm-svn: 70531
This commit is contained in:
Eli Friedman
2009-05-01 02:23:58 +00:00
parent 43a675bf8a
commit f4e3ad6500
4 changed files with 22 additions and 1 deletions

View 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<

View 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;
}

View 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}}
}

View 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;