Simplify if conditions more.. simplifyIfAssign + simplifyIfNot
This commit is contained in:
parent
44c952873e
commit
65070cc067
|
@ -1135,6 +1135,7 @@ void Tokenizer::simplifyTokenList()
|
||||||
simplifyFunctionParameters();
|
simplifyFunctionParameters();
|
||||||
|
|
||||||
elseif();
|
elseif();
|
||||||
|
simplifyIfNot();
|
||||||
simplifyIfAssign();
|
simplifyIfAssign();
|
||||||
|
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||||
|
@ -1812,10 +1813,21 @@ bool Tokenizer::simplifyIfAssign()
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
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();
|
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;
|
int numpar = 0;
|
||||||
while (tok->next()->str() == "(")
|
while (tok->next()->str() == "(")
|
||||||
{
|
{
|
||||||
|
@ -1823,6 +1835,7 @@ bool Tokenizer::simplifyIfAssign()
|
||||||
tok->deleteNext();
|
tok->deleteNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip the "%var% = ..."
|
||||||
Token *tok2 = tok;
|
Token *tok2 = tok;
|
||||||
int indentlevel = 0;
|
int indentlevel = 0;
|
||||||
for (tok2 = tok; tok2; tok2 = tok2->next())
|
for (tok2 = tok; tok2; tok2 = tok2->next())
|
||||||
|
@ -1836,12 +1849,16 @@ bool Tokenizer::simplifyIfAssign()
|
||||||
--indentlevel;
|
--indentlevel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert "; if ( .."
|
||||||
if (tok2)
|
if (tok2)
|
||||||
{
|
{
|
||||||
tok2 = tok2->previous();
|
tok2 = tok2->previous();
|
||||||
tok2->insertToken(tok->strAt(1));
|
tok2->insertToken(tok->strAt(1));
|
||||||
for (int p = 0; p < numpar; ++p)
|
for (int p = 0; p < numpar; ++p)
|
||||||
tok2->insertToken("(");
|
tok2->insertToken("(");
|
||||||
|
if (isNot)
|
||||||
|
tok2->next()->insertToken("!");
|
||||||
tok2->insertToken("if");
|
tok2->insertToken("if");
|
||||||
tok2->insertToken(";");
|
tok2->insertToken(";");
|
||||||
ret = true;
|
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 Tokenizer::simplifyKnownVariables()
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
|
@ -101,6 +101,12 @@ public:
|
||||||
*/
|
*/
|
||||||
bool simplifyIfAssign();
|
bool simplifyIfAssign();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* simplify if-not..
|
||||||
|
* Example: "if(0==x);" => "if(!x);"
|
||||||
|
*/
|
||||||
|
bool simplifyIfNot();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
/** Add braces to an if-block
|
/** Add braces to an if-block
|
||||||
|
|
|
@ -87,6 +87,9 @@ private:
|
||||||
|
|
||||||
// Assignment in condition..
|
// Assignment in condition..
|
||||||
TEST_CASE(ifassign1);
|
TEST_CASE(ifassign1);
|
||||||
|
|
||||||
|
// "if(0==x)" => "if(!x)"
|
||||||
|
TEST_CASE(ifnot);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string tok(const char code[])
|
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()));"));
|
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))"));
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue