Fix #801 (Tokenizer: Simplify sizeof for function parameters)

http://sourceforge.net/apps/trac/cppcheck/ticket/801
This commit is contained in:
Reijo Tomperi 2009-10-07 12:05:44 +03:00
parent 06dd66bff4
commit 8fc21dda27
2 changed files with 79 additions and 4 deletions

View File

@ -1430,10 +1430,10 @@ void Tokenizer::simplifySizeof()
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end())
{
const unsigned int varId = tok->varId();
if (Token::Match(tok->tokAt(-3), "[;{}] %type% * %var% ;") ||
Token::Match(tok->tokAt(-4), "[;{}] const %type% * %var% ;") ||
Token::Match(tok->tokAt(-2), "[;{}] %type% %var% ;") ||
Token::Match(tok->tokAt(-3), "[;{}] const %type% %var% ;"))
if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %var% [;,)]") ||
Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %var% [;),]") ||
Token::Match(tok->tokAt(-2), "[;{}(,] %type% %var% [;),]") ||
Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %var% [;),]"))
{
sizeOfVar[varId] = MathLib::toString<long>(sizeOfType(tok->tokAt(-1)));
}
@ -1448,6 +1448,14 @@ void Tokenizer::simplifySizeof()
sizeOfVar[varId] = MathLib::toString<long>(size * MathLib::toLongNumber(tok->strAt(2)));
}
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ %num% ] [,)]") ||
Token::Match(tok->tokAt(-2), "%type% * %var% [ %num% ] [,)]"))
{
Token tempTok;
tempTok.str("*");
sizeOfVar[varId] = MathLib::toString<long>(sizeOfType(&tempTok));
}
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;"))
{
int size = sizeOfType(tok->tokAt(4));

View File

@ -695,6 +695,73 @@ private:
ASSERT_EQUALS(expected.str(), sizeof_("; sizeof(\"123\")"));
}
{
const char code[] = "void f(char *a,char *b, char *c)"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( char * a , char * b , char * c ) { g ( " <<
(sizeof(char *)) << " , " << (sizeof(char *)) << " , " << (sizeof(char *)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
const char code[] = "void f(char a,char b, char c)"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( char a , char b , char c ) { g ( " <<
(sizeof(char)) << " , " << (sizeof(char)) << " , " << (sizeof(char)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
const char code[] = "void f(const char *a,const char *b, const char *c)"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( const char * a , const char * b , const char * c ) { g ( " <<
(sizeof(char *)) << " , " << (sizeof(char *)) << " , " << (sizeof(char *)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
const char code[] = "void f(char a[10],char b[10], char c[10])"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( char a [ 10 ] , char b [ 10 ] , char c [ 10 ] ) { g ( " <<
(sizeof(char *)) << " , " << (sizeof(char *)) << " , " << (sizeof(char *)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
const char code[] = "void f(const char a[10],const char b[10], const char c[10])"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( const char a [ 10 ] , "
"const char b [ 10 ] , "
"const char c [ 10 ] ) { g ( " <<
(sizeof(char *)) << " , " << (sizeof(char *)) << " , " << (sizeof(char *)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
const char code[] = "void f(const char *a[10],const char *b[10], const char *c[10])"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( const char * a [ 10 ] , "
"const char * b [ 10 ] , "
"const char * c [ 10 ] ) { g ( " <<
(sizeof(char *)) << " , " << (sizeof(char *)) << " , " << (sizeof(char *)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
const char code[] = "void f(char *a[10],char *b[10], char *c[10])"
"{g(sizeof(a),sizeof(b),sizeof(c));}";
std::ostringstream expected;
expected << "void f ( char * a [ 10 ] , char * b [ 10 ] , char * c [ 10 ] ) { g ( " <<
(sizeof(char *)) << " , " << (sizeof(char *)) << " , " << (sizeof(char *)) << " ) ; }";
ASSERT_EQUALS(expected.str(), sizeof_(code));
}
{
std::ostringstream expected;
expected << "; " << sizeof("\"quote\"");