From 3b7948fb4eb67ad7809d2ac2aeaaf28885844e75 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Thu, 4 Feb 2016 21:20:05 +0100 Subject: [PATCH] ValueType: Support ternary operator --- lib/symboldatabase.cpp | 8 ++++++-- test/testsymboldatabase.cpp | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 05bb54f90..49df6273c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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(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) { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 97d6f7d48..210074ecb 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -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;", ">>"));