From 4f0f980272462acdaaafe8712b06179f906266d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 13 Apr 2010 21:55:16 +0200 Subject: [PATCH 1/3] readme: minor cleanup. we don't need to mention the '--all' flag anymore. --- readme.txt | 6 ------ 1 file changed, 6 deletions(-) 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 From 4a33b226e6eff76a19b078d9983e0beb5ac32397 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Tue, 13 Apr 2010 22:57:52 +0300 Subject: [PATCH 2/3] Fix #1599 (false positive on if( (f = fopen("foo", "r")) == ((FILE*)NULL) )) http://sourceforge.net/apps/trac/cppcheck/ticket/1599 --- lib/tokenize.cpp | 23 +++++++++++++---------- test/testsimplifytokens.cpp | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a91888929..65af6a8fb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3190,16 +3190,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 +3937,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/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 451f3a633..0899a9df2 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)")); } @@ -1861,6 +1863,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() { From 0912836c25f443a8dbeb5267fe6e168e697af7e8 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Tue, 13 Apr 2010 23:17:27 +0300 Subject: [PATCH 3/3] Fix: sizeof(char*) was not simplified correctly --- lib/tokenize.cpp | 3 +-- test/testsimplifytokens.cpp | 54 +++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 65af6a8fb..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(), "( * )")) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 0899a9df2..1a353e940 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1095,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()); } @@ -1111,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()); } @@ -1120,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()); } @@ -1129,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()); } @@ -1150,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()); } @@ -1159,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()); } @@ -1168,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()); } @@ -1177,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()); } @@ -1186,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()); } @@ -1195,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()); } @@ -1204,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()