aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/SemaDecl.cpp
diff options
context:
space:
mode:
authorNuno Lopes <nunoplopes@sapo.pt>2009-02-02 22:57:15 +0000
committerNuno Lopes <nunoplopes@sapo.pt>2009-02-02 22:57:15 +0000
commitff77645058c5d56b4f3273e27f7492c78288825e (patch)
tree600d153099678f8cb32dba87989b353dcfb15766 /lib/Sema/SemaDecl.cpp
parent1dfa6e15cba39f27a438d63837435596e58af1c1 (diff)
emit diagnostic when casting a ptr to a small int when doing static initialization (addresses Eli's comments I believe)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63562 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDecl.cpp')
-rw-r--r--lib/Sema/SemaDecl.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 21fe6e1ce4..d2140b2267 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -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);