Fixed #844 (Tokenizer: Simplify 'sizeof *ptr' correctly)
This commit is contained in:
parent
eeb281daf6
commit
b80a954f55
|
@ -1585,16 +1585,24 @@ void Tokenizer::simplifySizeof()
|
|||
else if (Token::Match(tok, "sizeof ( * %var% )") || Token::Match(tok, "sizeof ( %var% [ %num% ] )"))
|
||||
{
|
||||
// Some default value..
|
||||
unsigned int sz = 100;
|
||||
unsigned int sz = 0;
|
||||
|
||||
unsigned int varid = tok->tokAt((tok->tokAt(2)->str() == "*") ? 3 : 2)->varId();
|
||||
if (varid != 0)
|
||||
{
|
||||
// Try to locate variable declaration..
|
||||
const Token *decltok = Token::findmatch(_tokens, "%type% %varid% [", varid);
|
||||
if (decltok)
|
||||
const Token *decltok = Token::findmatch(_tokens, "%varid%", varid);
|
||||
if (Token::findmatch(decltok->previous(), "%type% %var% ["))
|
||||
{
|
||||
sz = sizeOfType(decltok);
|
||||
sz = sizeOfType(decltok->previous());
|
||||
}
|
||||
else if (Token::findmatch(decltok->previous(), "* %var% ["))
|
||||
{
|
||||
sz = sizeOfType(decltok->previous());
|
||||
}
|
||||
else if (Token::findmatch(decltok->tokAt(-2), "%type% * %var%"))
|
||||
{
|
||||
sz = sizeOfType(decltok->tokAt(-2));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,6 +67,7 @@ private:
|
|||
TEST_CASE(sizeof9);
|
||||
TEST_CASE(sizeof10);
|
||||
TEST_CASE(sizeof11);
|
||||
TEST_CASE(sizeof12);
|
||||
TEST_CASE(casting);
|
||||
|
||||
TEST_CASE(template1);
|
||||
|
@ -549,11 +550,8 @@ private:
|
|||
|
||||
void sizeof1()
|
||||
{
|
||||
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));
|
||||
ASSERT_EQUALS("struct ABC * abc ; abc = malloc ( 100 ) ;", tok("struct ABC *abc = malloc(sizeof(*abc));"));
|
||||
ASSERT_EQUALS("struct ABC * abc ; abc = malloc ( 100 ) ;", tok("struct ABC *abc = malloc(sizeof *abc );"));
|
||||
}
|
||||
|
||||
|
||||
|
@ -597,9 +595,10 @@ private:
|
|||
void sizeof5()
|
||||
{
|
||||
const char code[] =
|
||||
"for (int i = 0; i < sizeof(g_ReservedNames[0]); i++)"
|
||||
"const char * names[2];"
|
||||
"for (int i = 0; i < sizeof(names[0]); i++)"
|
||||
"{}";
|
||||
ASSERT_EQUALS("for ( int i = 0 ; i < 100 ; i ++ ) { }", sizeof_(code));
|
||||
ASSERT_EQUALS("const char * names [ 2 ] ; for ( int i = 0 ; i < 4 ; i ++ ) { }", sizeof_(code));
|
||||
}
|
||||
|
||||
void sizeof6()
|
||||
|
@ -821,6 +820,24 @@ private:
|
|||
ASSERT_EQUALS(expected, tok(code));
|
||||
}
|
||||
|
||||
void sizeof12()
|
||||
{
|
||||
// ticket #827
|
||||
const char code[] = "void f()\n"
|
||||
"{\n"
|
||||
" int *p;\n"
|
||||
" (sizeof *p);\n"
|
||||
"}";
|
||||
|
||||
const char expected[] = "void f ( ) "
|
||||
"{"
|
||||
" int * p ;"
|
||||
" 4 ; "
|
||||
"}";
|
||||
|
||||
ASSERT_EQUALS(expected, tok(code));
|
||||
}
|
||||
|
||||
void casting()
|
||||
{
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue