From d1c6cb9aa5316148b792dfe6e0a6697e077edb59 Mon Sep 17 00:00:00 2001 From: Ken-Patrick Date: Sun, 1 Sep 2019 09:51:53 +0200 Subject: [PATCH] Fix issue 9304: boolean type of ternary (#2131) * Add test cases for 9304 * Fix 9304 --- lib/symboldatabase.cpp | 2 +- test/testbool.cpp | 12 ++++++++++++ test/testsymboldatabase.cpp | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 953c8256f..214c94b88 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5258,7 +5258,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) vt.sign = vt2->sign; 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.sign = ValueType::Sign::SIGNED; vt.originalTypeName.clear(); diff --git a/test/testbool.cpp b/test/testbool.cpp index 42005e69e..58e64a741 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -54,6 +54,7 @@ private: TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true) TEST_CASE(comparisonOfBoolWithInt8); // #9165 + TEST_CASE(comparisonOfBoolWithInt9); // #9304 TEST_CASE(checkComparisonOfFuncReturningBool1); 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()); } + 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 check("void f(char *p) {\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 7f8b13843..2be3fe821 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -6149,6 +6149,7 @@ private: 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("", typeOf("int x; double y; a = (b ? \"a\" : std::string(\"b\"));", "?")); + ASSERT_EQUALS("bool", typeOf("int x; a = (b ? false : true);", "?")); // Boolean operators/literals ASSERT_EQUALS("bool", typeOf("a > b;", ">"));