Fix issue 9304: boolean type of ternary (#2131)

* Add test cases for 9304

* Fix 9304
This commit is contained in:
Ken-Patrick 2019-09-01 09:51:53 +02:00 committed by Daniel Marjamäki
parent 121093658d
commit d1c6cb9aa5
3 changed files with 14 additions and 1 deletions

View File

@ -5258,7 +5258,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
vt.sign = vt2->sign; vt.sign = vt2->sign;
vt.originalTypeName = vt2->originalTypeName; vt.originalTypeName = vt2->originalTypeName;
} }
if (vt.type < ValueType::Type::INT) { if (vt.type < ValueType::Type::INT && !(ternary && vt.type==ValueType::Type::BOOL)) {
vt.type = ValueType::Type::INT; vt.type = ValueType::Type::INT;
vt.sign = ValueType::Sign::SIGNED; vt.sign = ValueType::Sign::SIGNED;
vt.originalTypeName.clear(); vt.originalTypeName.clear();

View File

@ -54,6 +54,7 @@ private:
TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool
TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true) TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true)
TEST_CASE(comparisonOfBoolWithInt8); // #9165 TEST_CASE(comparisonOfBoolWithInt8); // #9165
TEST_CASE(comparisonOfBoolWithInt9); // #9304
TEST_CASE(checkComparisonOfFuncReturningBool1); TEST_CASE(checkComparisonOfFuncReturningBool1);
TEST_CASE(checkComparisonOfFuncReturningBool2); TEST_CASE(checkComparisonOfFuncReturningBool2);
@ -1038,6 +1039,17 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
} }
void comparisonOfBoolWithInt9() { // #9304
check("bool f(int a, bool b)\n"
"{\n"
" if ((a == 0 ? false : true) != b) {\n"
" b = !b;\n"
" }\n"
" return b;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void pointerArithBool1() { // #5126 void pointerArithBool1() { // #5126
check("void f(char *p) {\n" check("void f(char *p) {\n"

View File

@ -6149,6 +6149,7 @@ private:
ASSERT_EQUALS("double", typeOf("int x; double y; a = (b ? x : y);", "?")); ASSERT_EQUALS("double", typeOf("int x; double y; a = (b ? x : y);", "?"));
ASSERT_EQUALS("const char *", typeOf("int x; double y; a = (b ? \"a\" : \"b\");", "?")); ASSERT_EQUALS("const char *", typeOf("int x; double y; a = (b ? \"a\" : \"b\");", "?"));
ASSERT_EQUALS("", typeOf("int x; double y; a = (b ? \"a\" : std::string(\"b\"));", "?")); ASSERT_EQUALS("", typeOf("int x; double y; a = (b ? \"a\" : std::string(\"b\"));", "?"));
ASSERT_EQUALS("bool", typeOf("int x; a = (b ? false : true);", "?"));
// Boolean operators/literals // Boolean operators/literals
ASSERT_EQUALS("bool", typeOf("a > b;", ">")); ASSERT_EQUALS("bool", typeOf("a > b;", ">"));