1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-05 16:56:04 +02:00

Bug 544509 - Evaluation of negative integer cast to unsigned

Change-Id: I683045870eca5f1b013afddbc0938df2aef779c2
This commit is contained in:
Nathan Ridge 2019-02-16 19:32:21 -05:00
parent 783af3dee6
commit 4a35647d1f
2 changed files with 24 additions and 1 deletions

View file

@ -12719,6 +12719,12 @@ public class AST2CPPTests extends AST2CPPTestBase {
helper.assertVariableValue("waldo", 1);
}
// constexpr bool waldo = unsigned(-1) < unsigned(0);
public void testNegativeCastToUnsigned_544509() throws Exception {
BindingAssertionHelper helper = getAssertionHelper();
helper.assertVariableValue("waldo", 0);
}
// namespace x {
// void foo();
// }

View file

@ -41,6 +41,7 @@ import org.eclipse.cdt.internal.core.dom.parser.CompositeValue;
import org.eclipse.cdt.internal.core.dom.parser.DependentValue;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.SizeofCalculator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPPointerType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
@ -189,7 +190,23 @@ public class EvalTypeId extends CPPDependentEvaluation {
}
}
if (fArguments.length == 1) {
return fArguments[0].getValue();
IValue argVal = fArguments[0].getValue();
if (argVal instanceof IntegralValue && argVal.numberValue() != null) {
// Cast signed integer to unsigned.
Long val = argVal.numberValue().longValue();
if (val < 0 && inputType instanceof ICPPBasicType && ((ICPPBasicType) inputType).isUnsigned()) {
long sizeof = SizeofCalculator.getSizeAndAlignment(inputType).size;
if (sizeof > 4) {
// Java's "long" can't represent the full range of an 64-bit unsigned integer
// in C++.
sizeof = 4;
}
long range = (1L << (sizeof * 8 - 1));
val += range;
return IntegralValue.create(val);
}
}
return argVal;
}
return IntegralValue.UNKNOWN;
}