Fixed #10347 (ValueFlow: No known value set for sizeof(a[0]))

This commit is contained in:
Daniel Marjamäki 2021-07-08 18:18:32 +02:00
parent 4c23c2caa4
commit 56924643be
2 changed files with 20 additions and 0 deletions

View File

@ -999,6 +999,18 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
value.setKnown(); value.setKnown();
setTokenValue(tok, value, settings); setTokenValue(tok, value, settings);
} else if (Token::simpleMatch(tok, "sizeof (")) { } else if (Token::simpleMatch(tok, "sizeof (")) {
if (tok->next()->astOperand2() && !tok->next()->astOperand2()->isLiteral() && tok->next()->astOperand2()->valueType() &&
tok->next()->astOperand2()->valueType()->pointer == 0 && // <- TODO this is a bailout, abort when there are array->pointer conversions
!tok->next()->astOperand2()->valueType()->isEnum()) { // <- TODO this is a bailout, handle enum with non-int types
const size_t sz = ValueFlow::getSizeOf(*tok->next()->astOperand2()->valueType(), settings);
if (sz) {
ValueFlow::Value value(sz);
value.setKnown();
setTokenValue(tok->next(), value, settings);
return tok->linkAt(1);
}
}
const Token *tok2 = tok->tokAt(2); const Token *tok2 = tok->tokAt(2);
// skip over tokens to find variable or type // skip over tokens to find variable or type
while (Token::Match(tok2, "%name% ::|.|[")) { while (Token::Match(tok2, "%name% ::|.|[")) {

View File

@ -907,6 +907,14 @@ private:
ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(1, values.back().intvalue); ASSERT_EQUALS(1, values.back().intvalue);
code = "void f() {\n"
" char a[10];"
" x = sizeof(a[0]);\n"
"}";
values = tokenValues(code,"( a");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(1, values.back().intvalue);
code = "enum testEnum : uint32_t { a };\n" code = "enum testEnum : uint32_t { a };\n"
"sizeof(testEnum);"; "sizeof(testEnum);";
values = tokenValues(code,"( testEnum"); values = tokenValues(code,"( testEnum");