emit diagnostic when casting a ptr to a small int when doing static initialization (addresses Eli's comments I believe)

llvm-svn: 63562
This commit is contained in:
Nuno Lopes 2009-02-02 22:57:15 +00:00
parent e862b3dd96
commit 7cffb63a9d
2 changed files with 14 additions and 4 deletions

View File

@ -2055,14 +2055,23 @@ bool Sema::CheckArithmeticConstantExpression(const Expr* Init) {
}
case Expr::ImplicitCastExprClass:
case Expr::CStyleCastExprClass: {
const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr();
const CastExpr *CE = cast<CastExpr>(Init);
const Expr *SubExpr = CE->getSubExpr();
if (SubExpr->getType()->isArithmeticType())
return CheckArithmeticConstantExpression(SubExpr);
if (SubExpr->getType()->isPointerType()) {
const Expr* Base = FindExpressionBaseAddress(SubExpr);
// If the pointer has a null base, this is an offsetof-like construct
return Base ? false : CheckAddressConstantExpression(SubExpr);
if (Base) {
// the cast is only valid if done to a wide enough type
if (Context.getTypeSize(CE->getType()) >=
Context.getTypeSize(SubExpr->getType()))
return false;
} else {
// If the pointer has a null base, this is an offsetof-like construct
return CheckAddressConstantExpression(SubExpr);
}
}
InitializerElementNotConstant(Init);

View File

@ -16,4 +16,5 @@ struct foo {
};
union bar u[1];
struct foo x = {(int) u}; // no-error
struct foo x = {(long) u}; // no-error
struct foo y = {(char) u}; // expected-error {{initializer element is not a compile-time constant}}