Simplify if conditions more.. simplifyIfAssign + simplifyIfNot

This commit is contained in:
Daniel Marjamäki 2009-03-24 18:23:21 +01:00
parent 44c952873e
commit 65070cc067
3 changed files with 72 additions and 1 deletions

View File

@ -1135,6 +1135,7 @@ void Tokenizer::simplifyTokenList()
simplifyFunctionParameters();
elseif();
simplifyIfNot();
simplifyIfAssign();
for (Token *tok = _tokens; tok; tok = tok->next())
@ -1812,10 +1813,21 @@ bool Tokenizer::simplifyIfAssign()
bool ret = false;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok->next(), "if ( (| %var% ="))
if (Token::Match(tok->next(), "if ( (| %var% =") ||
Token::Match(tok->next(), "if ( ! ( %var% ="))
{
// delete the "if"
tok->deleteNext();
// Remember if there is a "!" or not. And delete it if there are.
bool isNot = false;
if (Token::simpleMatch(tok->tokAt(2), "!"))
{
isNot = true;
tok->next()->deleteNext();
}
// Delete paranthesis.. and remember how many there are.
int numpar = 0;
while (tok->next()->str() == "(")
{
@ -1823,6 +1835,7 @@ bool Tokenizer::simplifyIfAssign()
tok->deleteNext();
}
// Skip the "%var% = ..."
Token *tok2 = tok;
int indentlevel = 0;
for (tok2 = tok; tok2; tok2 = tok2->next())
@ -1836,12 +1849,16 @@ bool Tokenizer::simplifyIfAssign()
--indentlevel;
}
}
// Insert "; if ( .."
if (tok2)
{
tok2 = tok2->previous();
tok2->insertToken(tok->strAt(1));
for (int p = 0; p < numpar; ++p)
tok2->insertToken("(");
if (isNot)
tok2->next()->insertToken("!");
tok2->insertToken("if");
tok2->insertToken(";");
ret = true;
@ -1852,6 +1869,27 @@ bool Tokenizer::simplifyIfAssign()
}
bool Tokenizer::simplifyIfNot()
{
bool ret = false;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "if ( 0 == %var% )") || Token::simpleMatch(tok, "if ( 0 == ("))
{
tok = tok->next();
tok->deleteNext();
tok->next()->str("!");
tok = tok->tokAt(3);
ret = true;
}
}
return ret;
}
bool Tokenizer::simplifyKnownVariables()
{
bool ret = false;

View File

@ -101,6 +101,12 @@ public:
*/
bool simplifyIfAssign();
/**
* simplify if-not..
* Example: "if(0==x);" => "if(!x);"
*/
bool simplifyIfNot();
protected:
/** Add braces to an if-block

View File

@ -87,6 +87,9 @@ private:
// Assignment in condition..
TEST_CASE(ifassign1);
// "if(0==x)" => "if(!x)"
TEST_CASE(ifnot);
}
std::string tok(const char code[])
@ -569,6 +572,30 @@ private:
{
ASSERT_EQUALS("; a = b ; if ( a ) ;", simplifyIfAssign(";if(a=b);"));
ASSERT_EQUALS("; a = b ( ) ; if ( ( a ) ) ;", simplifyIfAssign(";if((a=b()));"));
ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) ;", simplifyIfAssign(";if(!(a=b()));"));
}
std::string simplifyIfNot(const char code[])
{
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyIfNot();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << (tok->previous() ? " " : "") << tok->str();
return ostr.str();
}
void ifnot()
{
ASSERT_EQUALS("if ( ! x )", simplifyIfNot("if(0==x)"));
ASSERT_EQUALS("if ( ! ( a = b ) )", simplifyIfNot("if(0==(a=b))"));
}
};