Fixed #851 (Tokenizer: sizeof(*a) returns incorrect result)

This commit is contained in:
Daniel Marjamäki 2009-10-22 23:00:27 +02:00
parent 701d622ff0
commit 88070b044b
2 changed files with 29 additions and 3 deletions

View File

@ -1592,15 +1592,15 @@ void Tokenizer::simplifySizeof()
{ {
// Try to locate variable declaration.. // Try to locate variable declaration..
const Token *decltok = Token::findmatch(_tokens, "%varid%", varid); const Token *decltok = Token::findmatch(_tokens, "%varid%", varid);
if (Token::findmatch(decltok->previous(), "%type% %var% [")) if (Token::Match(decltok->previous(), "%type% %var% ["))
{ {
sz = sizeOfType(decltok->previous()); sz = sizeOfType(decltok->previous());
} }
else if (Token::findmatch(decltok->previous(), "* %var% [")) else if (Token::Match(decltok->previous(), "* %var% ["))
{ {
sz = sizeOfType(decltok->previous()); sz = sizeOfType(decltok->previous());
} }
else if (Token::findmatch(decltok->tokAt(-2), "%type% * %var%")) else if (Token::Match(decltok->tokAt(-2), "%type% * %var%"))
{ {
sz = sizeOfType(decltok->tokAt(-2)); sz = sizeOfType(decltok->tokAt(-2));
} }

View File

@ -68,6 +68,7 @@ private:
TEST_CASE(sizeof10); TEST_CASE(sizeof10);
TEST_CASE(sizeof11); TEST_CASE(sizeof11);
TEST_CASE(sizeof12); TEST_CASE(sizeof12);
TEST_CASE(sizeof13);
TEST_CASE(casting); TEST_CASE(casting);
TEST_CASE(template1); TEST_CASE(template1);
@ -838,6 +839,31 @@ private:
ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS(expected, tok(code));
} }
void sizeof13()
{
// ticket #851
const char code[] = "int main()\n"
"{\n"
" char *a;\n"
" a = malloc(sizeof(*a));\n"
"}\n"
"\n"
"struct B\n"
"{\n"
" char * b[2];\n"
"};";
const char expected[] = "int main ( ) "
"{"
" char * a ;"
" a = malloc ( 1 ) ; "
"} "
"struct B "
"{"
" char * b [ 2 ] ; "
"} ;";
ASSERT_EQUALS(expected, tok(code));
}
void casting() void casting()
{ {
{ {