Tokenizer: simplify return statements - remove redundant parantheses

This commit is contained in:
Daniel Marjamäki 2009-07-24 21:55:35 +02:00
parent 6bb85703f6
commit 9136d8cf80
3 changed files with 44 additions and 16 deletions

View File

@ -861,20 +861,19 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
addtoken("use");
}
else if (Token::simpleMatch(tok->next(), "("))
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())
{
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")")
{
if (parlevel <= 1)
break;
--parlevel;
addtoken("use");
tok = tok->tokAt(2);
}
else
{
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == ";")
break;
if (tok2->str() == varname)
{
addtoken("use");
@ -882,11 +881,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
}
}
}
else if (Token::Match(tok, ("return strcpy|strncpy|memcpy ( " + varnameStr).c_str()))
{
addtoken("use");
tok = tok->tokAt(2);
}
}
// throw..

View File

@ -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)

View File

@ -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<char *>(p)"));
ASSERT_EQUALS("return ( a + b ) * c ;", tok("return (a+b)*c;"));
}
void declareVar()