From: Daniel Dunbar Date: Sat, 24 Jan 2009 19:08:01 +0000 (+0000) Subject: Fix invalid evaluation of _Complex float (real & imaginary parts had X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8f826f0e0c90b05ab04e84c29157d2e965713288;p=clang Fix invalid evaluation of _Complex float (real & imaginary parts had mismatched semantics). - Enforce this in APValue. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62924 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/APValue.h b/include/clang/AST/APValue.h index d88757de1a..5d5abfe011 100644 --- a/include/clang/AST/APValue.h +++ b/include/clang/AST/APValue.h @@ -188,11 +188,15 @@ public: ((Vec*)(void*)Data)->Elts[i] = E[i]; } void setComplexInt(const APSInt &R, const APSInt &I) { + assert(R.getBitWidth() == I.getBitWidth() && + "Invalid complex int (type mismatch)."); assert(isComplexInt() && "Invalid accessor"); ((ComplexAPSInt*)(void*)Data)->Real = R; ((ComplexAPSInt*)(void*)Data)->Imag = I; } void setComplexFloat(const APFloat &R, const APFloat &I) { + assert(&R.getSemantics() == &I.getSemantics() && + "Invalid complex float (type mismatch)."); assert(isComplexFloat() && "Invalid accessor"); ((ComplexAPFloat*)(void*)Data)->Real = R; ((ComplexAPFloat*)(void*)Data)->Imag = I; diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index f45bd787b5..16c6e89c32 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -1194,7 +1194,8 @@ public: if (!EvaluateFloat(E->getSubExpr(), Result, Info)) return APValue(); - return APValue(APFloat(0.0), Result); + return APValue(APFloat(Result.getSemantics(), APFloat::fcZero), + Result); } APValue VisitCastExpr(CastExpr *E) { @@ -1206,7 +1207,8 @@ public: if (!EvaluateFloat(SubExpr, Result, Info)) return APValue(); - return APValue(Result, APFloat(0.0)); + return APValue(Result, + APFloat(Result.getSemantics(), APFloat::fcZero)); } // FIXME: Handle more casts. @@ -1221,6 +1223,10 @@ public: static bool EvaluateComplexFloat(const Expr *E, APValue &Result, EvalInfo &Info) { Result = ComplexFloatExprEvaluator(Info).Visit(const_cast(E)); + if (Result.isComplexFloat()) + assert(&Result.getComplexFloatReal().getSemantics() == + &Result.getComplexFloatImag().getSemantics() && + "Invalid complex evaluation."); return Result.isComplexFloat(); } diff --git a/test/Sema/const-eval.c b/test/Sema/const-eval.c index e3d63ca94f..71f76228d1 100644 --- a/test/Sema/const-eval.c +++ b/test/Sema/const-eval.c @@ -26,3 +26,6 @@ void f() int a; EVAL_EXPR(15, (_Bool)&a); // expected-error {{fields must have a constant size}} } + +// FIXME: Turn into EVAL_EXPR test once we have more folding. +_Complex float g16 = (1.0f + 1.0fi);