Tokenizer: Simplifying redundant parantheses

http://apps.sourceforge.net/trac/cppcheck/ticket/330
This commit is contained in:
Daniel Marjamäki 2009-05-28 19:37:39 +02:00
parent 31c68ffb5a
commit a3990648a9
2 changed files with 27 additions and 3 deletions

View File

@ -2390,7 +2390,7 @@ bool Tokenizer::simplifyRedundantParanthesis()
bool ret = false; bool ret = false;
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (Token::simpleMatch(tok, "( (")) while (Token::simpleMatch(tok, "( ("))
{ {
int parlevel = 0; int parlevel = 0;
for (Token *tok2 = tok; tok2; tok2 = tok2->next()) for (Token *tok2 = tok; tok2; tok2 = tok2->next())
@ -2407,6 +2407,7 @@ bool Tokenizer::simplifyRedundantParanthesis()
{ {
tok->deleteNext(); tok->deleteNext();
tok2->deleteNext(); tok2->deleteNext();
ret = true;
} }
break; break;
} }

View File

@ -127,7 +127,8 @@ private:
TEST_CASE(simplify_function_parameters); TEST_CASE(simplify_function_parameters);
TEST_CASE(reduce_redundant_paranthesis); // Ticket #61 TEST_CASE(removeParantheses1); // Ticket #61
TEST_CASE(removeParantheses2);
TEST_CASE(simplify_numeric_condition); TEST_CASE(simplify_numeric_condition);
TEST_CASE(tokenize_double); TEST_CASE(tokenize_double);
@ -1571,7 +1572,7 @@ private:
// Simplify "((..))" into "(..)" // Simplify "((..))" into "(..)"
void reduce_redundant_paranthesis() void removeParantheses1()
{ {
const char code[] = "void foo()\n" const char code[] = "void foo()\n"
"{\n" "{\n"
@ -1591,6 +1592,28 @@ private:
ASSERT_EQUALS(std::string(" void foo ( ) { free ( p ) ; }"), ostr.str()); ASSERT_EQUALS(std::string(" void foo ( ) { free ( p ) ; }"), ostr.str());
} }
void removeParantheses2()
{
const char code[] = "void foo()\n"
"{\n"
" if (__builtin_expect((s == NULL), 0))\n"
" return;\n"
"}";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void foo ( ) { if ( ! s ) { return ; } }"), ostr.str());
}
void simplify_numeric_condition() void simplify_numeric_condition()