[cfe-commits] r63562 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/static-init.c
Nuno Lopes
nunoplopes at sapo.pt
Mon Feb 2 14:57:16 PST 2009
Author: nlopes
Date: Mon Feb 2 16:57:15 2009
New Revision: 63562
URL: http://llvm.org/viewvc/llvm-project?rev=63562&view=rev
Log:
emit diagnostic when casting a ptr to a small int when doing static initialization (addresses Eli's comments I believe)
Modified:
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/test/Sema/static-init.c
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=63562&r1=63561&r2=63562&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Feb 2 16:57:15 2009
@@ -2055,14 +2055,23 @@
}
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);
Modified: cfe/trunk/test/Sema/static-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/static-init.c?rev=63562&r1=63561&r2=63562&view=diff
==============================================================================
--- cfe/trunk/test/Sema/static-init.c (original)
+++ cfe/trunk/test/Sema/static-init.c Mon Feb 2 16:57:15 2009
@@ -16,4 +16,5 @@
};
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}}
More information about the cfe-commits
mailing list