Skip to content

Commit d1c6cb9

Browse files
Ken-Patrickdanmar
authored andcommitted
Fix issue 9304: boolean type of ternary (#2131)
* Add test cases for 9304 * Fix 9304
1 parent 1210936 commit d1c6cb9

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5258,7 +5258,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype)
52585258
vt.sign = vt2->sign;
52595259
vt.originalTypeName = vt2->originalTypeName;
52605260
}
5261-
if (vt.type < ValueType::Type::INT) {
5261+
if (vt.type < ValueType::Type::INT && !(ternary && vt.type==ValueType::Type::BOOL)) {
52625262
vt.type = ValueType::Type::INT;
52635263
vt.sign = ValueType::Sign::SIGNED;
52645264
vt.originalTypeName.clear();

test/testbool.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class TestBool : public TestFixture {
5454
TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool
5555
TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true)
5656
TEST_CASE(comparisonOfBoolWithInt8); // #9165
57+
TEST_CASE(comparisonOfBoolWithInt9); // #9304
5758

5859
TEST_CASE(checkComparisonOfFuncReturningBool1);
5960
TEST_CASE(checkComparisonOfFuncReturningBool2);
@@ -1038,6 +1039,17 @@ class TestBool : public TestFixture {
10381039
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
10391040
}
10401041

1042+
void comparisonOfBoolWithInt9() { // #9304
1043+
check("bool f(int a, bool b)\n"
1044+
"{\n"
1045+
" if ((a == 0 ? false : true) != b) {\n"
1046+
" b = !b;\n"
1047+
" }\n"
1048+
" return b;\n"
1049+
"}");
1050+
ASSERT_EQUALS("", errout.str());
1051+
}
1052+
10411053

10421054
void pointerArithBool1() { // #5126
10431055
check("void f(char *p) {\n"

test/testsymboldatabase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6149,6 +6149,7 @@ class TestSymbolDatabase: public TestFixture {
61496149
ASSERT_EQUALS("double", typeOf("int x; double y; a = (b ? x : y);", "?"));
61506150
ASSERT_EQUALS("const char *", typeOf("int x; double y; a = (b ? \"a\" : \"b\");", "?"));
61516151
ASSERT_EQUALS("", typeOf("int x; double y; a = (b ? \"a\" : std::string(\"b\"));", "?"));
6152+
ASSERT_EQUALS("bool", typeOf("int x; a = (b ? false : true);", "?"));
61526153

61536154
// Boolean operators/literals
61546155
ASSERT_EQUALS("bool", typeOf("a > b;", ">"));

0 commit comments

Comments
 (0)