File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -2055,14 +2055,23 @@ bool Sema::CheckArithmeticConstantExpression(const Expr* Init) {
2055
2055
}
2056
2056
case Expr::ImplicitCastExprClass:
2057
2057
case Expr::CStyleCastExprClass: {
2058
- const Expr *SubExpr = cast<CastExpr>(Init)->getSubExpr ();
2058
+ const CastExpr *CE = cast<CastExpr>(Init);
2059
+ const Expr *SubExpr = CE->getSubExpr ();
2060
+
2059
2061
if (SubExpr->getType ()->isArithmeticType ())
2060
2062
return CheckArithmeticConstantExpression (SubExpr);
2061
2063
2062
2064
if (SubExpr->getType ()->isPointerType ()) {
2063
2065
const Expr* Base = FindExpressionBaseAddress (SubExpr);
2064
- // If the pointer has a null base, this is an offsetof-like construct
2065
- return Base ? false : CheckAddressConstantExpression (SubExpr);
2066
+ if (Base) {
2067
+ // the cast is only valid if done to a wide enough type
2068
+ if (Context.getTypeSize (CE->getType ()) >=
2069
+ Context.getTypeSize (SubExpr->getType ()))
2070
+ return false ;
2071
+ } else {
2072
+ // If the pointer has a null base, this is an offsetof-like construct
2073
+ return CheckAddressConstantExpression (SubExpr);
2074
+ }
2066
2075
}
2067
2076
2068
2077
InitializerElementNotConstant (Init);
Original file line number Diff line number Diff line change @@ -16,4 +16,5 @@ struct foo {
16
16
};
17
17
18
18
union bar u [1 ];
19
- struct foo x = {(int ) u }; // no-error
19
+ struct foo x = {(long ) u }; // no-error
20
+ struct foo y = {(char ) u }; // expected-error {{initializer element is not a compile-time constant}}
You can’t perform that action at this time.
0 commit comments