Fixed #2225 (false positive: variable is assigned a value that is never used '*(stack[0])+=arg')

This commit is contained in:
Daniel Marjamäki 2010-11-23 20:35:08 +01:00
parent 1842a122da
commit 22a4dd2cc3
2 changed files with 32 additions and 21 deletions

View File

@ -4467,7 +4467,7 @@ void Tokenizer::simplifyCompoundAssignment()
// "a+=b" => "a = a + b"
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "[;{}:] *| %var%"))
if (Token::Match(tok, "[;{}:] *| (| %var%"))
{
if (tok->str() == ":")
{
@ -4481,6 +4481,12 @@ void Tokenizer::simplifyCompoundAssignment()
if (tok->strAt(1) == "*")
tok = tok->next();
if (tok->strAt(1) == "(")
{
tok = tok->next()->link()->next();
}
else
{
// variable..
tok = tok->tokAt(2);
while (Token::Match(tok, ". %var%") || (tok && tok->str() == "["))
@ -4501,6 +4507,7 @@ void Tokenizer::simplifyCompoundAssignment()
break;
}
}
}
if (!tok)
break;
@ -4526,15 +4533,18 @@ void Tokenizer::simplifyCompoundAssignment()
// simplify the compound assignment..
tok->str("=");
tok->insertToken(op);
Token *tokend = 0;
std::stack<Token *> tokend;
for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous())
{
tok->insertToken(tok2->str());
tok->next()->varId(tok2->varId());
if (Token::simpleMatch(tok->next(), "]"))
tokend = tok->next();
else if (Token::simpleMatch(tok->next(), "["))
Token::createMutualLinks(tok->next(), tokend);
if (Token::Match(tok->next(), "]|)"))
tokend.push(tok->next());
else if (Token::Match(tok->next(), "(|["))
{
Token::createMutualLinks(tok->next(), tokend.top());
tokend.pop();
}
}
}
}

View File

@ -4694,6 +4694,7 @@ private:
ASSERT_EQUALS("{ x = x >> y ; }", tokenizeAndStringify("{ x >>= y;}"));
ASSERT_EQUALS("; * p = * p + y ;", tokenizeAndStringify("; *p += y;"));
ASSERT_EQUALS("; * ( p [ 0 ] ) = * ( p [ 0 ] ) + y ;", tokenizeAndStringify("; *(p[0]) += y;"));
ASSERT_EQUALS("case 0 : x = x + y ; break ;", tokenizeAndStringify("case 0: x += y; break;"));