ValueFlow: set value for char literals

This commit is contained in:
Daniel Marjamäki 2015-11-12 16:38:58 +01:00
parent 34dbc3c00c
commit 595f1e1a27
2 changed files with 23 additions and 2 deletions

View File

@ -598,6 +598,25 @@ static void valueFlowNumber(TokenList *tokenlist)
ValueFlow::Value value(MathLib::toLongNumber(tok->str()));
value.setKnown();
setTokenValue(tok, value);
} else if (tok->tokType() == Token::eChar) {
char c;
if (tok->str() == "\'\\n\'")
c = '\n';
else if (tok->str() == "\'\\r\'")
c = '\r';
else if (tok->str() == "\'\\t\'")
c = '\t';
else if (tok->str().size() == 3U &&
tok->str()[0] == '\'' &&
(tok->str()[1] & 0x80) == 0 &&
tok->str()[2] == '\'')
c = tok->str()[1];
else
continue;
ValueFlow::Value value(c);
value.setKnown();
setTokenValue(tok, value);
}
}

View File

@ -167,6 +167,8 @@ private:
ASSERT_EQUALS(123, valueOfTok("x=123;", "123").intvalue);
ASSERT_EQUALS(0, valueOfTok("x=false;", "false").intvalue);
ASSERT_EQUALS(1, valueOfTok("x=true;", "true").intvalue);
ASSERT_EQUALS((int)('a'), valueOfTok("x='a';", "'a'").intvalue);
ASSERT_EQUALS((int)('\n'), valueOfTok("x='\\n';", "'\\n'").intvalue);
}
void valueFlowString() {