From 26358debcfe80e166340930814d267103827b194 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 3 Jul 2009 21:57:44 +0300 Subject: [PATCH] Fix ticket #448 (false positive:: resource leak) http://sourceforge.net/apps/trac/cppcheck/ticket/448 Tokenizer improved to simplify "if( (p)==-1 )" --- src/tokenize.cpp | 11 +++++++++++ test/testsimplifytokens.cpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 17e8fc170..ca9b72229 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2656,6 +2656,17 @@ bool Tokenizer::simplifyRedundantParanthesis() ret = true; } + if (Token::Match(tok->previous(), "( ( %var% )") && tok->next()->varId() != 0) + { + // We have "( var )", remove the paranthesis + tok = tok->previous(); + tok->deleteNext(); + tok = tok->next(); + tok->deleteNext(); + ret = true; + continue; + } + if (Token::Match(tok, "( ( %bool% )") || Token::Match(tok, "( ( %num% )")) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 458a83936..8e1771e4f 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -379,6 +379,9 @@ private: // remove parantheses.. ASSERT_EQUALS("= p ; ", tok("= (p);")); ASSERT_EQUALS("if ( a < p ) { } ", tok("if(a<(p)){}")); + ASSERT_EQUALS("void f ( ) { int p ; if ( p == -1 ) { } } ", tok("void f(){int p; if((p)==-1){}}")); + ASSERT_EQUALS("void f ( ) { int p ; if ( -1 == p ) { } } ", tok("void f(){int p; if(-1==(p)){}}")); + ASSERT_EQUALS("void f ( ) { int p ; if ( p ) { } } ", tok("void f(){int p; if((p)){}}")); // keep parantheses.. ASSERT_EQUALS("= a ; ", tok("= (char)a;"));