From 8fc21dda27eabd3b000787f4ae1a8400c852566e Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 7 Oct 2009 12:05:44 +0300 Subject: [PATCH] Fix #801 (Tokenizer: Simplify sizeof for function parameters) http://sourceforge.net/apps/trac/cppcheck/ticket/801 --- src/tokenize.cpp | 16 ++++++--- test/testsimplifytokens.cpp | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index fa07762c9..bb1929d25 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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(sizeOfType(tok->tokAt(-1))); } @@ -1448,6 +1448,14 @@ void Tokenizer::simplifySizeof() sizeOfVar[varId] = MathLib::toString(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(sizeOfType(&tempTok)); + } + else if (Token::Match(tok->tokAt(-1), "%type% %var% [ ] = %str% ;")) { int size = sizeOfType(tok->tokAt(4)); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 5aa96d700..8f822960a 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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\"");