[cfe-commits] r146842 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/SemaCXX/constant-expression.cpp
Richard Smith
richard-llvm at metafoo.co.uk
Sat Dec 17 18:33:09 PST 2011
Author: rsmith
Date: Sat Dec 17 20:33:09 2011
New Revision: 146842
URL: http://llvm.org/viewvc/llvm-project?rev=146842&view=rev
Log:
PR11604: don't allow floating-literal-to-integer casts in ICEs if the (truncated)
floating literal value does not fit into the destination type. Such casts have
undefined behavior at translation time; treating them as non-ICE matches the
behavior of modern gcc versions.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/test/SemaCXX/constant-expression.cpp
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=146842&r1=146841&r2=146842&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sat Dec 17 20:33:09 2011
@@ -5252,9 +5252,23 @@
case Expr::CXXConstCastExprClass:
case Expr::ObjCBridgedCastExprClass: {
const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
- if (isa<ExplicitCastExpr>(E) &&
- isa<FloatingLiteral>(SubExpr->IgnoreParenImpCasts()))
- return NoDiag();
+ if (isa<ExplicitCastExpr>(E)) {
+ if (const FloatingLiteral *FL
+ = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {
+ unsigned DestWidth = Ctx.getIntWidth(E->getType());
+ bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
+ APSInt IgnoredVal(DestWidth, !DestSigned);
+ bool Ignored;
+ // If the value does not fit in the destination type, the behavior is
+ // undefined, so we are not required to treat it as a constant
+ // expression.
+ if (FL->getValue().convertToInteger(IgnoredVal,
+ llvm::APFloat::rmTowardZero,
+ &Ignored) & APFloat::opInvalidOp)
+ return ICEDiag(2, E->getLocStart());
+ return NoDiag();
+ }
+ }
switch (cast<CastExpr>(E)->getCastKind()) {
case CK_LValueToRValue:
case CK_NoOp:
Modified: cfe/trunk/test/SemaCXX/constant-expression.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression.cpp?rev=146842&r1=146841&r2=146842&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/constant-expression.cpp (original)
+++ cfe/trunk/test/SemaCXX/constant-expression.cpp Sat Dec 17 20:33:09 2011
@@ -102,3 +102,9 @@
template<int n> struct S {};
S<p> s; // expected-error {{not an integral constant expression}}
}
+
+namespace FloatConvert {
+ typedef int a[(int)42.3];
+ typedef int a[(int)42.997];
+ typedef int b[(int)4e10]; // expected-error {{variable length}}
+}
More information about the cfe-commits
mailing list