ValueType: Support ternary operator

This commit is contained in:
PKEuS 2016-02-04 21:20:05 +01:00
parent a9f52aec04
commit 3b7948fb4e
2 changed files with 11 additions and 2 deletions

View File

@ -3734,7 +3734,11 @@ static void setValueType(Token *tok, const ValueType &valuetype, bool cpp, Value
if (parent->astOperand2() && !vt2)
return;
if (parent->isArithmeticalOp()) {
bool ternary = parent->str() == ":" && parent->astParent() && parent->astParent()->str() == "?";
if (ternary)
parent = const_cast<Token*>(parent->astParent());
if (ternary || parent->isArithmeticalOp()) {
if (vt1->pointer != 0U && vt2 && vt2->pointer == 0U) {
setValueType(parent, *vt1, cpp, defaultSignedness);
return;
@ -3766,7 +3770,7 @@ static void setValueType(Token *tok, const ValueType &valuetype, bool cpp, Value
if (vt1->isIntegral() && vt1->pointer == 0U &&
(!vt2 || (vt2->isIntegral() && vt2->pointer == 0U)) &&
(parent->isArithmeticalOp() || parent->tokType() == Token::eBitOp || parent->isAssignmentOp())) {
(ternary || parent->isArithmeticalOp() || parent->tokType() == Token::eBitOp || parent->isAssignmentOp())) {
ValueType vt;
if (!vt2 || vt1->type > vt2->type) {

View File

@ -3195,6 +3195,11 @@ private:
ASSERT_EQUALS("signed int", typeOf("int x; a = ~x;", "~"));
ASSERT_EQUALS("double", typeOf("double x; a = -x;", "-"));
// Ternary operator
ASSERT_EQUALS("signed int", typeOf("int x; a = (b ? x : x)", "?"));
ASSERT_EQUALS("", typeOf("int x; a = (b ? x : y)", "?"));
ASSERT_EQUALS("double", typeOf("int x; double y; a = (b ? x : y)", "?"));
// shift => result has same type as lhs
ASSERT_EQUALS("signed int", typeOf("int x; a = x << 1U;", "<<"));
ASSERT_EQUALS("signed int", typeOf("int x; a = x >> 1U;", ">>"));