ValueFlow: Improved handling of a[i]

This commit is contained in:
Daniel Marjamäki 2015-11-12 16:14:27 +01:00
parent 4ec333dbb6
commit 34dbc3c00c
2 changed files with 18 additions and 1 deletions

View File

@ -558,10 +558,15 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
result.inconclusive = value1->inconclusive | value2->inconclusive;
result.varId = (value1->varId != 0U) ? value1->varId : value2->varId;
result.varvalue = (result.varId == value1->varId) ? value1->intvalue : value2->intvalue;
if (value1->valueKind == value2->valueKind)
result.valueKind = value1->valueKind;
if (value1->tokvalue->tokType() == Token::eString) {
const std::string s = value1->tokvalue->strValue();
const MathLib::bigint index = value2->intvalue;
if (index >= 0 && index < s.size()) {
if (index == s.size()) {
result.intvalue = 0;
setTokenValue(parent, result);
} else if (index >= 0 && index < s.size()) {
result.intvalue = s[index];
setTokenValue(parent, result);
}

View File

@ -236,6 +236,18 @@ private:
" *x = 0;\n" // .. => x value is a
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4, "a"));
code = "char f() {\n"
" const char *x = \"abcd\";\n"
" return x[0];\n"
"}";
ASSERT_EQUALS((int)('a'), valueOfTok(code, "[").intvalue);
code = "char f() {\n"
" const char *x = \"\";\n"
" return x[0];\n"
"}";
ASSERT_EQUALS(0, valueOfTok(code, "[").intvalue);
}
void valueFlowCalculations() {