Fixed #528 (Tokenizer: Simplify: 'if(!(a->x=b()))')
This commit is contained in:
parent
35f9bc3009
commit
1c59e4a51b
|
@ -2464,10 +2464,9 @@ bool Tokenizer::simplifyIfAssign()
|
|||
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
{
|
||||
if (Token::Match(tok->next(), "if|while ( (| %var% =") ||
|
||||
Token::Match(tok->next(), "if|while ( ! ( %var% ="))
|
||||
{
|
||||
ret = true;
|
||||
if (!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");
|
||||
|
@ -2475,16 +2474,16 @@ bool Tokenizer::simplifyIfAssign()
|
|||
// delete the "if"
|
||||
tok->deleteNext();
|
||||
|
||||
// The tokenlist has changed
|
||||
ret = true;
|
||||
|
||||
// Remember if there is a "!" or not. And delete it if there are.
|
||||
bool isNot = false;
|
||||
if (tok->tokAt(2)->str() == "!")
|
||||
{
|
||||
isNot = true;
|
||||
const bool isNot(tok->tokAt(2)->str() == "!");
|
||||
if (isNot)
|
||||
tok->next()->deleteNext();
|
||||
}
|
||||
|
||||
// Delete paranthesis.. and remember how many there are.
|
||||
int numpar = 0;
|
||||
unsigned int numpar = 0;
|
||||
while (tok->next()->str() == "(")
|
||||
{
|
||||
++numpar;
|
||||
|
@ -2492,8 +2491,8 @@ bool Tokenizer::simplifyIfAssign()
|
|||
}
|
||||
|
||||
// Skip the "%var% = ..."
|
||||
Token *tok2 = tok;
|
||||
int indentlevel = 0;
|
||||
Token *tok2;
|
||||
unsigned int indentlevel = 0;
|
||||
for (tok2 = tok; tok2; tok2 = tok2->next())
|
||||
{
|
||||
if (tok2->str() == "(")
|
||||
|
@ -2507,11 +2506,14 @@ bool Tokenizer::simplifyIfAssign()
|
|||
}
|
||||
|
||||
// Insert "; if|while ( .."
|
||||
if (tok2)
|
||||
{
|
||||
tok2 = tok2->previous();
|
||||
if (Token::simpleMatch(tok->tokAt(2), "."))
|
||||
{
|
||||
tok2->insertToken(tok->strAt(3));
|
||||
tok2->insertToken(tok->strAt(2));
|
||||
}
|
||||
tok2->insertToken(tok->strAt(1));
|
||||
for (int p = 0; p < numpar; ++p)
|
||||
for (unsigned int p = 0; p < numpar; ++p)
|
||||
tok2->insertToken("(");
|
||||
if (isNot)
|
||||
tok2->next()->insertToken("!");
|
||||
|
@ -2547,8 +2549,7 @@ bool Tokenizer::simplifyIfAssign()
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()));"));
|
||||
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()
|
||||
{
|
||||
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 ( ) ; 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; }"));
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue