Partial support for sizeof x, by converting it into sizeof(x). Does not handle complex structures. Closing ticket #65
This commit is contained in:
parent
64e3250f00
commit
2f7dec2fc3
|
@ -601,6 +601,49 @@ void Tokenizer::simplifyTokenList()
|
|||
if (tok->str() != "sizeof")
|
||||
continue;
|
||||
|
||||
if (tok->strAt(1) != std::string("("))
|
||||
{
|
||||
// Add parenthesis around the sizeof
|
||||
for (Token *tempToken = tok->next(); tempToken; tempToken = tempToken->next())
|
||||
{
|
||||
if (Token::Match(tempToken, "%var%"))
|
||||
{
|
||||
if (Token::Match(tempToken->next(), "."))
|
||||
{
|
||||
// We are checking a class or struct, search next varname
|
||||
tempToken = tempToken->tokAt(1);
|
||||
continue;
|
||||
}
|
||||
else if (Token::Match(tempToken->next(), "- >"))
|
||||
{
|
||||
// We are checking a class or struct, search next varname
|
||||
tempToken = tempToken->tokAt(2);
|
||||
continue;
|
||||
}
|
||||
else if (Token::Match(tempToken->next(), "++") ||
|
||||
Token::Match(tempToken->next(), "--"))
|
||||
{
|
||||
// We have variable++ or variable--, there should be
|
||||
// nothing after this
|
||||
tempToken = tempToken->tokAt(2);
|
||||
}
|
||||
else if (Token::Match(tempToken->next(), "["))
|
||||
{
|
||||
// TODO: We need to find closing ], then check for
|
||||
// dots and arrows "var[some[0]]->other"
|
||||
|
||||
// But for now, just bail out
|
||||
break;
|
||||
}
|
||||
|
||||
// Ok, we should be clean. Add ) after tempToken
|
||||
tok->insertToken("(");
|
||||
tempToken->insertToken(")");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Token::Match(tok, "sizeof ( %type% * )"))
|
||||
{
|
||||
std::ostringstream str;
|
||||
|
|
|
@ -68,7 +68,9 @@ private:
|
|||
{
|
||||
const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); ";
|
||||
const char code2[] = " struct ABC *abc = malloc(100); ";
|
||||
const char code3[] = " struct ABC *abc = malloc(sizeof *abc ); ";
|
||||
ASSERT_EQUALS(tok(code1), tok(code2));
|
||||
ASSERT_EQUALS(tok(code2), tok(code3));
|
||||
}
|
||||
|
||||
void iftruefalse()
|
||||
|
|
Loading…
Reference in New Issue