return true;
}
-static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
- APSInt &Value, const ASTContext &Ctx) {
- unsigned DestWidth = Ctx.getIntWidth(DestType);
+static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
+ QualType DestType, QualType SrcType,
+ APSInt &Value) {
+ unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
APSInt Result = Value;
// Figure out if this is a truncate, extend or noop cast.
// If the input is signed, do a sign extend, noop, or truncate.
Result = Result.extOrTrunc(DestWidth);
+
+ // Check whether we overflowed. If so, fold the cast anyway.
+ if (DestType->isSignedIntegerOrEnumerationType() &&
+ ((Result.isNegative() && Value.isUnsigned()) ||
+ Result.extOrTrunc(Value.getBitWidth()) != Value))
+ (void)HandleOverflow(Info, E, Value, DestType);
+
Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
return Result;
}
return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
}
- return Success(HandleIntToIntCast(DestType, SrcType,
- Result.getInt(), Info.Ctx), E);
+ return Success(HandleIntToIntCast(Info, E, DestType, SrcType,
+ Result.getInt()), E);
}
case CK_PointerToIntegral: {
if (LV.getLValueBase()) {
// Only allow based lvalue casts if they are lossless.
+ // FIXME: Allow a larger integer size than the pointer size, and allow
+ // narrowing back down to pointer width in subsequent integral casts.
+ // FIXME: Check integer type's active bits, not its type size.
if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
return Error(E);
APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
SrcType);
- return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
+ return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);
}
case CK_IntegralComplexToReal: {
QualType From
= E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
- Result.IntReal = HandleIntToIntCast(To, From, Result.IntReal, Info.Ctx);
- Result.IntImag = HandleIntToIntCast(To, From, Result.IntImag, Info.Ctx);
+ Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);
+ Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);
return true;
}
// Check for a narrowing implicit conversion.
APValue PreNarrowingValue;
+ bool Diagnosed = false;
switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue)) {
case NK_Variable_Narrowing:
// Implicit conversion to a narrower type, and the value is not a constant
Diag(From->getSourceRange().getBegin(), diag::err_cce_narrowing)
<< CCE << /*Constant*/1
<< PreNarrowingValue.getAsString(Context, QualType()) << T;
+ Diagnosed = true;
break;
case NK_Type_Narrowing:
Diag(From->getSourceRange().getBegin(), diag::err_cce_narrowing)
<< CCE << /*Constant*/0 << From->getType() << T;
+ Diagnosed = true;
break;
}
// The expression can't be folded, so we can't keep it at this position in
// the AST.
Result = ExprError();
- } else if (Notes.empty()) {
- // It's a constant expression.
+ } else {
Value = Eval.Val.getInt();
- return Result;
+
+ if (Notes.empty()) {
+ // It's a constant expression.
+ return Result;
+ }
}
+ // Only issue one narrowing diagnostic.
+ if (Diagnosed)
+ return Result;
+
// It's not a constant expression. Produce an appropriate diagnostic.
if (Notes.size() == 1 &&
Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr)
for (unsigned I = 0; I < Notes.size(); ++I)
Diag(Notes[I].first, Notes[I].second);
}
- return ExprError();
+ return Result;
}
/// dropPointerConversions - If the given standard conversion sequence