From 9136d8cf800960adae528eaee7533ba852ee168e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 24 Jul 2009 21:55:35 +0200 Subject: [PATCH] Tokenizer: simplify return statements - remove redundant parantheses --- src/checkmemoryleak.cpp | 26 ++++++++++---------------- src/tokenize.cpp | 32 ++++++++++++++++++++++++++++++++ test/testsimplifytokens.cpp | 2 ++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index a6bfc374e..3095a93f5 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -861,19 +861,18 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext(), "(")) + else if (Token::Match(tok, ("return strcpy|strncpy|memcpy ( " + varnameStr).c_str())) { - int parlevel = 1; - for (const Token *tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) + addtoken("use"); + tok = tok->tokAt(2); + } + + else + { + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { - if (tok2->str() == "(") - ++parlevel; - else if (tok2->str() == ")") - { - if (parlevel <= 1) - break; - --parlevel; - } + if (tok2->str() == ";") + break; if (tok2->str() == varname) { @@ -882,11 +881,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listtokAt(2); - } } // throw.. diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 8a6ceb7fb..79805ab1a 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1505,6 +1505,38 @@ void Tokenizer::simplifyTokenList() modified |= simplifyQuestionMark(); } + // Remove redundant parantheses in return.. + for (Token *tok = _tokens; tok; tok = tok->next()) + { + while (Token::simpleMatch(tok, "return (")) + { + unsigned int parlevel = 0; + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) + { + if (tok2->str() == "(") + ++parlevel; + + else if (tok2->str() == ")") + { + if (parlevel <= 1) + { + if (Token::simpleMatch(tok2, ") ;")) + { + tok->deleteNext(); + tok2->deleteThis(); + } + else + { + tok = tok->next(); + } + break; + } + --parlevel; + } + } + } + } + simplifyComma(); createLinks(); if (_settings && _settings->_debug) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d0a499f75..e4f823c67 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -390,10 +390,12 @@ private: 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)){}}")); + ASSERT_EQUALS("return p ;", tok("return (p);")); // keep parantheses.. ASSERT_EQUALS("= a ;", tok("= (char)a;")); ASSERT_EQUALS("cast < char * > ( p )", tok("cast(p)")); + ASSERT_EQUALS("return ( a + b ) * c ;", tok("return (a+b)*c;")); } void declareVar()