From 34285849257d91d27ce9f87491472ea863311467 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 3 Jun 2009 22:02:16 +0300 Subject: [PATCH] Fix ticket #351 (false positive::resource leak) http://apps.sourceforge.net/trac/cppcheck/ticket/351 --- src/tokenize.cpp | 13 +++- test/testtokenize.cpp | 163 +++++++++++++++++++++++++++++++++--------- 2 files changed, 142 insertions(+), 34 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index c37f9ad9d..ade46ed9b 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1723,6 +1723,17 @@ bool Tokenizer::simplifyConditions() for (Token *tok = _tokens; tok; tok = tok->next()) { + if (Token::Match(tok, "! %num%") || Token::Match(tok, "! %bool%")) + { + if (tok->next()->str() == "0" || tok->next()->str() == "false") + tok->str("true"); + else + tok->str("false"); + + tok->deleteNext(); + ret = true; + } + if (Token::simpleMatch(tok, "( true &&") || Token::simpleMatch(tok, "&& true &&") || Token::simpleMatch(tok->next(), "&& true )")) { tok->deleteNext(); @@ -2354,7 +2365,7 @@ bool Tokenizer::simplifyKnownVariables() break; // Using the variable in condition.. - if (Token::Match(tok3, "(|==|!=|<|<=|>|>= %varid% )|==|!=|<|<=|>|>=", varid)) + if (Token::Match(tok3, "(|!|==|!=|<|<=|>|>= %varid% )|==|!=|<|<=|>|>=", varid)) { tok3 = tok3->next(); tok3->str(value.c_str()); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d63d8722b..c87a8456d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -480,24 +480,47 @@ private: void simplifyKnownVariables1() { - const char code[] = "void f()\n" - "{\n" - " int a = 10;\n" - " if (a);\n" - "}\n"; + { + const char code[] = "void f()\n" + "{\n" + " int a = 10;\n" + " if (a);\n" + "}\n"; - // tokenize.. - OurTokenizer tokenizer; - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); - tokenizer.setVarId(); - tokenizer.simplifyKnownVariables(); + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); - std::ostringstream ostr; - for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) - ostr << " " << tok->str(); - ASSERT_EQUALS(" void f ( ) { int a ; a = 10 ; if ( 10 ) ; }", ostr.str()); + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void f ( ) { int a ; a = 10 ; if ( 10 ) ; }", ostr.str()); + } + + { + const char code[] = "void f()\n" + "{\n" + " int a = 10;\n" + " if (!a);\n" + "}\n"; + + // tokenize.. + OurTokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyKnownVariables(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void f ( ) { int a ; a = 10 ; if ( ! 10 ) ; }", ostr.str()); + } } void simplifyKnownVariables2() @@ -1702,27 +1725,101 @@ private: void simplify_numeric_condition() { - const char code[] = - "void f()\n" - "{\n" - "int x = 0;\n" - "if( !x || 0 )\n" - "{\n" - "}\n" - "}"; + { + const char code[] = + "void f()\n" + "{\n" + "int x = 0;\n" + "if( !x || 0 )\n" + "{ g();\n" + "}\n" + "}"; - // tokenize.. - Tokenizer tokenizer; - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); - tokenizer.setVarId(); - tokenizer.simplifyTokenList(); + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); - std::ostringstream ostr; - for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) - ostr << " " << tok->str(); - ASSERT_EQUALS(" void f ( ) { int x ; x = 0 ; if ( ! x ) { } }", ostr.str()); + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void f ( ) { int x ; x = 0 ; { g ( ) ; } }", ostr.str()); + } + + { + const char code[] = + "void f()\n" + "{\n" + "int x = 1;\n" + "if( !x )\n" + "{ g();\n" + "}\n" + "}"; + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void f ( ) { int x ; x = 1 ; }", ostr.str()); + } + + { + const char code[] = + "void f()\n" + "{\n" + "bool x = true;\n" + "if( !x )\n" + "{ g();\n" + "}\n" + "}"; + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void f ( ) { bool x ; x = true ; }", ostr.str()); + } + + { + const char code[] = + "void f()\n" + "{\n" + "bool x = false;\n" + "if( !x )\n" + "{ g();\n" + "}\n" + "}"; + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void f ( ) { bool x ; x = false ; { g ( ) ; } }", ostr.str()); + } } void tokenize_double()