Fixed #528 (Tokenizer: Simplify: 'if(!(a->x=b()))')
This commit is contained in:
parent
35f9bc3009
commit
1c59e4a51b
129
src/tokenize.cpp
129
src/tokenize.cpp
|
@ -2464,91 +2464,92 @@ bool Tokenizer::simplifyIfAssign()
|
||||||
|
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||||
{
|
{
|
||||||
if (Token::Match(tok->next(), "if|while ( (| %var% =") ||
|
if (!Token::Match(tok->next(), "if|while ( !| (| %var% =") &&
|
||||||
Token::Match(tok->next(), "if|while ( ! ( %var% ="))
|
!Token::Match(tok->next(), "if|while ( !| (| %var% . %var% ="))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// simplifying a "while" condition ?
|
||||||
|
const bool iswhile(tok->next()->str() == "while");
|
||||||
|
|
||||||
|
// delete the "if"
|
||||||
|
tok->deleteNext();
|
||||||
|
|
||||||
|
// The tokenlist has changed
|
||||||
|
ret = true;
|
||||||
|
|
||||||
|
// Remember if there is a "!" or not. And delete it if there are.
|
||||||
|
const bool isNot(tok->tokAt(2)->str() == "!");
|
||||||
|
if (isNot)
|
||||||
|
tok->next()->deleteNext();
|
||||||
|
|
||||||
|
// Delete paranthesis.. and remember how many there are.
|
||||||
|
unsigned int numpar = 0;
|
||||||
|
while (tok->next()->str() == "(")
|
||||||
{
|
{
|
||||||
ret = true;
|
++numpar;
|
||||||
|
|
||||||
// simplifying a "while" condition ?
|
|
||||||
const bool iswhile(tok->next()->str() == "while");
|
|
||||||
|
|
||||||
// delete the "if"
|
|
||||||
tok->deleteNext();
|
tok->deleteNext();
|
||||||
|
}
|
||||||
|
|
||||||
// Remember if there is a "!" or not. And delete it if there are.
|
// Skip the "%var% = ..."
|
||||||
bool isNot = false;
|
Token *tok2;
|
||||||
if (tok->tokAt(2)->str() == "!")
|
unsigned int indentlevel = 0;
|
||||||
|
for (tok2 = tok; tok2; tok2 = tok2->next())
|
||||||
|
{
|
||||||
|
if (tok2->str() == "(")
|
||||||
|
++indentlevel;
|
||||||
|
else if (tok2->str() == ")")
|
||||||
{
|
{
|
||||||
isNot = true;
|
if (indentlevel <= 0)
|
||||||
tok->next()->deleteNext();
|
break;
|
||||||
|
--indentlevel;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Delete paranthesis.. and remember how many there are.
|
// Insert "; if|while ( .."
|
||||||
int numpar = 0;
|
tok2 = tok2->previous();
|
||||||
while (tok->next()->str() == "(")
|
if (Token::simpleMatch(tok->tokAt(2), "."))
|
||||||
{
|
{
|
||||||
++numpar;
|
tok2->insertToken(tok->strAt(3));
|
||||||
tok->deleteNext();
|
tok2->insertToken(tok->strAt(2));
|
||||||
}
|
}
|
||||||
|
tok2->insertToken(tok->strAt(1));
|
||||||
|
for (unsigned int p = 0; p < numpar; ++p)
|
||||||
|
tok2->insertToken("(");
|
||||||
|
if (isNot)
|
||||||
|
tok2->next()->insertToken("!");
|
||||||
|
tok2->insertToken(iswhile ? "while" : "if");
|
||||||
|
tok2->insertToken(";");
|
||||||
|
|
||||||
// Skip the "%var% = ..."
|
// If it's a while loop.. insert the assignment in the loop
|
||||||
Token *tok2 = tok;
|
if (iswhile)
|
||||||
int indentlevel = 0;
|
{
|
||||||
for (tok2 = tok; tok2; tok2 = tok2->next())
|
indentlevel = 0;
|
||||||
|
Token *tok3 = tok2;
|
||||||
|
for (tok3 = tok2; tok3; tok3 = tok3->next())
|
||||||
{
|
{
|
||||||
if (tok2->str() == "(")
|
if (tok3->str() == "{")
|
||||||
++indentlevel;
|
++indentlevel;
|
||||||
else if (tok2->str() == ")")
|
else if (tok3->str() == "}")
|
||||||
{
|
{
|
||||||
if (indentlevel <= 0)
|
if (indentlevel <= 1)
|
||||||
break;
|
break;
|
||||||
--indentlevel;
|
--indentlevel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert "; if|while ( .."
|
if (tok3 && indentlevel == 1)
|
||||||
if (tok2)
|
|
||||||
{
|
{
|
||||||
tok2 = tok2->previous();
|
tok3 = tok3->previous();
|
||||||
tok2->insertToken(tok->strAt(1));
|
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous())
|
||||||
for (int p = 0; p < numpar; ++p)
|
|
||||||
tok2->insertToken("(");
|
|
||||||
if (isNot)
|
|
||||||
tok2->next()->insertToken("!");
|
|
||||||
tok2->insertToken(iswhile ? "while" : "if");
|
|
||||||
tok2->insertToken(";");
|
|
||||||
|
|
||||||
// If it's a while loop.. insert the assignment in the loop
|
|
||||||
if (iswhile)
|
|
||||||
{
|
{
|
||||||
indentlevel = 0;
|
tok3->insertToken(tok2->str().c_str());
|
||||||
Token *tok3 = tok2;
|
tok3->next()->fileIndex(tok2->fileIndex());
|
||||||
for (tok3 = tok2; tok3; tok3 = tok3->next())
|
tok3->next()->linenr(tok2->linenr());
|
||||||
{
|
|
||||||
if (tok3->str() == "{")
|
|
||||||
++indentlevel;
|
|
||||||
else if (tok3->str() == "}")
|
|
||||||
{
|
|
||||||
if (indentlevel <= 1)
|
|
||||||
break;
|
|
||||||
--indentlevel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tok3 && indentlevel == 1)
|
|
||||||
{
|
|
||||||
tok3 = tok3->previous();
|
|
||||||
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous())
|
|
||||||
{
|
|
||||||
tok3->insertToken(tok2->str().c_str());
|
|
||||||
tok3->next()->fileIndex(tok2->fileIndex());
|
|
||||||
tok3->next()->linenr(tok2->linenr());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -954,15 +954,14 @@ 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()));"));
|
ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) ;", simplifyIfAssign(";if(!(a=b()));"));
|
||||||
TODO_ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) ;", simplifyIfAssign(";if(!(a->x=b()));"));
|
ASSERT_EQUALS("; a . x = b ( ) ; if ( ! ( a . x ) ) ;", simplifyIfAssign(";if(!(a->x=b()));"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void whileAssign()
|
void whileAssign()
|
||||||
{
|
{
|
||||||
ASSERT_EQUALS("; a = b ; while ( a ) { b = 0 ; a = b ; }", simplifyIfAssign(";while(a=b) { b = 0; }"));
|
ASSERT_EQUALS("; a = b ; while ( a ) { b = 0 ; a = b ; }", simplifyIfAssign(";while(a=b) { b = 0; }"));
|
||||||
//ASSERT_EQUALS("; a = b ( ) ; if ( ( a ) ) ;", simplifyIfAssign(";if((a=b()));"));
|
ASSERT_EQUALS("; a . b = c ; while ( a . b ) { c = 0 ; a . b = c ; }", simplifyIfAssign(";while(a.b=c) { c=0; }"));
|
||||||
//ASSERT_EQUALS("; a = b ( ) ; if ( ! ( a ) ) ;", simplifyIfAssign(";if(!(a=b()));"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue