Fix #801 (Tokenizer: Simplify sizeof for function parameters)
http://sourceforge.net/apps/trac/cppcheck/ticket/801
This commit is contained in:
parent
06dd66bff4
commit
8fc21dda27
|
@ -1430,10 +1430,10 @@ void Tokenizer::simplifySizeof()
|
||||||
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end())
|
if (tok->varId() != 0 && sizeOfVar.find(tok->varId()) == sizeOfVar.end())
|
||||||
{
|
{
|
||||||
const unsigned int varId = tok->varId();
|
const unsigned int varId = tok->varId();
|
||||||
if (Token::Match(tok->tokAt(-3), "[;{}] %type% * %var% ;") ||
|
if (Token::Match(tok->tokAt(-3), "[;{}(,] %type% * %var% [;,)]") ||
|
||||||
Token::Match(tok->tokAt(-4), "[;{}] const %type% * %var% ;") ||
|
Token::Match(tok->tokAt(-4), "[;{}(,] const %type% * %var% [;),]") ||
|
||||||
Token::Match(tok->tokAt(-2), "[;{}] %type% %var% ;") ||
|
Token::Match(tok->tokAt(-2), "[;{}(,] %type% %var% [;),]") ||
|
||||||
Token::Match(tok->tokAt(-3), "[;{}] const %type% %var% ;"))
|
Token::Match(tok->tokAt(-3), "[;{}(,] const %type% %var% [;),]"))
|
||||||
{
|
{
|
||||||
sizeOfVar[varId] = MathLib::toString<long>(sizeOfType(tok->tokAt(-1)));
|
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)));
|
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% ;"))
|
else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;"))
|
||||||
{
|
{
|
||||||
int size = sizeOfType(tok->tokAt(4));
|
int size = sizeOfType(tok->tokAt(4));
|
||||||
|
|
|
@ -695,6 +695,73 @@ private:
|
||||||
ASSERT_EQUALS(expected.str(), sizeof_("; sizeof(\"123\")"));
|
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;
|
std::ostringstream expected;
|
||||||
expected << "; " << sizeof("\"quote\"");
|
expected << "; " << sizeof("\"quote\"");
|
||||||
|
|
Loading…
Reference in New Issue