ValueFlow: sizeof string and char literals (#2285)
This commit is contained in:
parent
78c02f0505
commit
f83eb127ae
|
@ -1232,6 +1232,20 @@ static Token * valueFlowSetConstantValue(Token *tok, const Settings *settings, b
|
|||
setTokenValue(tok->next(), value, settings);
|
||||
}
|
||||
}
|
||||
} else if (tok2->tokType() == Token::eString) {
|
||||
size_t sz = Token::getStrSize(tok2, settings);
|
||||
if (sz > 0) {
|
||||
ValueFlow::Value value(sz);
|
||||
value.setKnown();
|
||||
setTokenValue(const_cast<Token *>(tok->next()), value, settings);
|
||||
}
|
||||
} else if (tok2->tokType() == Token::eChar) {
|
||||
size_t sz = ValueFlow::getSizeOf(*tok2->valueType(), settings);
|
||||
if (sz > 0) {
|
||||
ValueFlow::Value value(sz);
|
||||
value.setKnown();
|
||||
setTokenValue(tok->next(), value, settings);
|
||||
}
|
||||
} else if (!tok2->type()) {
|
||||
const ValueType &vt = ValueType::parseDecl(tok2,settings);
|
||||
const size_t sz = ValueFlow::getSizeOf(vt, settings);
|
||||
|
|
|
@ -813,6 +813,19 @@ private:
|
|||
CHECK("int", settings.sizeof_int);
|
||||
CHECK("long", settings.sizeof_long);
|
||||
CHECK("wchar_t", settings.sizeof_wchar_t);
|
||||
|
||||
// string/char literals
|
||||
CHECK("\"asdf\"", 5);
|
||||
CHECK("L\"asdf\"", 5 * settings.sizeof_wchar_t);
|
||||
CHECK("u8\"asdf\"", 5); // char8_t
|
||||
CHECK("u\"asdf\"", 5 * 2); // char16_t
|
||||
CHECK("U\"asdf\"", 5 * 4); // char32_t
|
||||
CHECK("'a'", 1U);
|
||||
CHECK("'ab'", settings.sizeof_int);
|
||||
CHECK("L'a'", settings.sizeof_wchar_t);
|
||||
CHECK("u8'a'", 1U); // char8_t
|
||||
CHECK("u'a'", 2U); // char16_t
|
||||
CHECK("U'a'", 4U); // char32_t
|
||||
#undef CHECK
|
||||
|
||||
// array size
|
||||
|
|
Loading…
Reference in New Issue