diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a91888929..3b793ec67 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2820,10 +2820,9 @@ void Tokenizer::simplifySizeof() } // sizeof(type *) => sizeof(*) - if (Token::Match(tok->next(), "( %type% *)")) + if (Token::Match(tok->next(), "( %type% * )")) { tok->next()->deleteNext(); - continue; } if (Token::Match(tok->next(), "( * )")) @@ -3190,16 +3189,6 @@ bool Tokenizer::simplifyTokenList() } } - // Replace pointer casts of 0.. "(char *)0" => "0" - for (Token *tok = _tokens; tok; tok = tok->next()) - { - if (Token::Match(tok->next(), "( %type% * ) 0") || - Token::Match(tok->next(), "( %type% %type% * ) 0")) - { - Token::eraseTokens(tok, tok->next()->link()->next()); - } - } - // Change initialisation of variable to assignment simplifyInitVar(); @@ -3947,6 +3936,19 @@ void Tokenizer::simplifyCasts() } } + // Replace pointer casts of 0.. "(char *)0" => "0" + while (Token::Match(tok->next(), "( %type% * ) 0") || + Token::Match(tok->next(), "( %type% %type% * ) 0")) + { + Token::eraseTokens(tok, tok->next()->link()->next()); + if (tok->str() == ")" && tok->link()->previous()) + { + // If there was another cast before this, go back + // there to check it also. e.g. "(char*)(char*)0" + tok = tok->link()->previous(); + } + } + if (Token::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <")) { Token *tok2 = tok->next(); diff --git a/readme.txt b/readme.txt index 133965a00..42ac7c127 100644 --- a/readme.txt +++ b/readme.txt @@ -55,12 +55,6 @@ Usage Run the cppcheck program without parameters and a help text will be shown. - -Recommendations - - When the "--all" flag is given you may get a lot of error messages. - - Webpage http://www.sf.net/projects/cppcheck diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 451f3a633..1a353e940 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -105,6 +105,7 @@ private: // Assignment in condition.. TEST_CASE(ifassign1); + TEST_CASE(ifAssignWithCast); TEST_CASE(whileAssign); // "if(0==x)" => "if(!x)" @@ -294,6 +295,7 @@ private: ASSERT_EQUALS("if ( * a )", tok("if ((unsigned int)(unsigned char)*a)")); ASSERT_EQUALS("class A { A operator * ( int ) ; } ;", tok("class A { A operator *(int); };")); ASSERT_EQUALS("class A { A operator * ( int ) const ; } ;", tok("class A { A operator *(int) const; };")); + ASSERT_EQUALS("if ( ! p )", tok("if (p == (char *)(char *)0)")); } @@ -1093,14 +1095,16 @@ private: void sizeof18() { - if (sizeof(short int) == 2) { + std::ostringstream expected; + expected << sizeof(short int); + { const char code[] = "void f()\n" "{\n" " sizeof(short int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1109,7 +1113,7 @@ private: "{\n" " sizeof(unsigned short int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1118,7 +1122,7 @@ private: "{\n" " sizeof(short unsigned int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1127,19 +1131,21 @@ private: "{\n" " sizeof(signed short int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 2 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } } - if (sizeof(long long) == 8) { + std::ostringstream expected; + expected << sizeof(long long); + { const char code[] = "void f()\n" "{\n" " sizeof(long long);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1148,7 +1154,7 @@ private: "{\n" " sizeof(signed long long);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1157,7 +1163,7 @@ private: "{\n" " sizeof(unsigned long long);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1166,7 +1172,7 @@ private: "{\n" " sizeof(long unsigned long);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1175,7 +1181,7 @@ private: "{\n" " sizeof(long long int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1184,7 +1190,7 @@ private: "{\n" " sizeof(signed long long int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1193,7 +1199,7 @@ private: "{\n" " sizeof(unsigned long long int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } @@ -1202,10 +1208,32 @@ private: "{\n" " sizeof(long unsigned long int);\n" "}\n"; - ASSERT_EQUALS("void f ( ) { 8 ; }", tok(code)); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); ASSERT_EQUALS("", errout.str()); } } + + { + const char code[] = "void f()\n" + "{\n" + " sizeof(char*);\n" + "}\n"; + std::ostringstream expected; + expected << sizeof(int*); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "void f()\n" + "{\n" + " sizeof(unsigned int*);\n" + "}\n"; + std::ostringstream expected; + expected << sizeof(int*); + ASSERT_EQUALS("void f ( ) { " + expected.str() + " ; }", tok(code)); + ASSERT_EQUALS("", errout.str()); + } } void casting() @@ -1861,6 +1889,27 @@ private: TODO_ASSERT_EQUALS("void foo ( A a ) { a . c = b ( ) ; if ( 0 <= a . c ) { ; } }", tok("void foo(A a) {if((a.c=b())>=0);}")); } + void ifAssignWithCast() + { + const char *code = "void foo()\n" + "{\n" + "FILE *f;\n" + "if( (f = fopen(\"foo\", \"r\")) == ((FILE*)NULL) )\n" + "return(-1);\n" + "fclose(f);\n" + "}\n"; + const char *exptected = "void foo ( ) " + "{ " + "FILE * f ; " + "f = fopen ( \"foo\" , \"r\" ) ; " + "if ( ! f ) " + "{ " + "return -1 ; " + "} " + "fclose ( f ) ; " + "}"; + ASSERT_EQUALS(exptected, tok(code)); + } void whileAssign() {