Skip to content

Commit f1ad736

Browse files
committed
ValueFlow: bool values
1 parent aaf19c1 commit f1ad736

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,16 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
19451945
std::list<ValueFlow::Value> values = tok->astOperand2()->values;
19461946
const bool constValue = tok->astOperand2()->isNumber();
19471947

1948+
if (tokenlist->isCPP() && Token::simpleMatch(var->typeStartToken(), "bool")) {
1949+
std::list<ValueFlow::Value>::iterator it;
1950+
for (it = values.begin(); it != values.end(); ++it) {
1951+
if (it->isIntValue())
1952+
it->intvalue = (it->intvalue != 0);
1953+
if (it->isTokValue())
1954+
it ->intvalue = (it->tokvalue != 0);
1955+
}
1956+
}
1957+
19481958
// Static variable initialisation?
19491959
if (var->isStatic() && var->nameToken() == tok->astOperand1()) {
19501960
for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) {

test/testvalueflow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,12 @@ class TestValueFlow : public TestFixture {
899899
"}";
900900
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
901901

902+
code = "void f() {\n"
903+
" bool x = 32;\n"
904+
" a = x;\n"
905+
"}";
906+
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1));
907+
902908
code = "void f() {\n"
903909
" int x = 123;\n"
904910
" a = sizeof(x);\n"

0 commit comments

Comments
 (0)