sizeof: fixed a problem with 'sizeof(varname)' but there are more to fix with it

This commit is contained in:
Daniel Marjamäki 2009-02-28 08:34:02 +00:00
parent 42176f52ca
commit 7d9bf491ec
2 changed files with 32 additions and 3 deletions

View File

@ -682,6 +682,35 @@ void Tokenizer::simplifyTokenList()
}
// Replace 'sizeof(var)'..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[;{}] %type% %var% ;") && tok->tokAt(2)->varId() > 0)
{
const unsigned int varid = tok->tokAt(2)->varId();
// Replace 'sizeof(var)' with 'sizeof(type)'
int indentlevel = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
--indentlevel;
if (indentlevel < 0)
break;
}
else if (Token::Match(tok2, "sizeof ( %varid% )", varid))
{
tok2 = tok2->tokAt(2);
tok2->str(tok->strAt(1));
}
}
}
}
// Replace 'sizeof(type)'..
for (Token *tok = _tokens; tok; tok = tok->next())
{

View File

@ -116,7 +116,7 @@ private:
TEST_CASE(sizeof2);
TEST_CASE(sizeof3);
TEST_CASE(sizeof4);
// TODO (ticket #108) TEST_CASE(sizeof5);
TEST_CASE(sizeof5);
TEST_CASE(simplify_numeric_condition);
TEST_CASE(tokenize_double);
TEST_CASE(tokenize_strings);
@ -1166,7 +1166,7 @@ private:
void sizeof5()
{
const char code[] = "int i;\n"
const char code[] = ";int i;\n"
"sizeof(i);\n";
// tokenize..
@ -1182,7 +1182,7 @@ private:
ostr << " " << tok->str();
std::ostringstream oss;
oss << " int i ; " << sizeof(int);
oss << " ; int i ; " << sizeof(int) << " ;";
ASSERT_EQUALS(oss.str(), ostr.str());
}