Fixed #2714 (False positive: ternary operator and += style warning.)

This commit is contained in:
Daniel Marjamäki 2011-04-14 17:30:50 +02:00
parent 75e5353b6d
commit 7021e3224b
2 changed files with 30 additions and 0 deletions

View File

@ -5015,9 +5015,35 @@ void Tokenizer::simplifyCompoundAssignment()
}
else
{
// Enclose the rhs in parantheses..
if (!Token::Match(tok->next(), "%any% [;)]"))
{
// Only enclose rhs in parantheses if there is some operator
bool someOperator = false;
for (Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == "(")
tok2 = tok2->link();
if (Token::Match(tok2->next(), "[;)]"))
{
if (someOperator)
{
tok->insertToken("(");
tok2->insertToken(")");
Token::createMutualLinks(tok->next(), tok2->next());
}
break;
}
someOperator |= bool(tok2->isArithmeticalOp() || (tok2->str() == "?"));
}
}
// simplify the compound assignment..
tok->str("=");
tok->insertToken(op);
std::stack<Token *> tokend;
for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous())
{

View File

@ -5542,6 +5542,10 @@ private:
// #2368
ASSERT_EQUALS("if ( false ) { } else { j = j - i ; }", tokenizeAndStringify("if (false) {} else { j -= i; }"));
// #2714 - wrong simplification of "a += b?c:d;"
ASSERT_EQUALS("; a = a + ( b ? c : d ) ;", tokenizeAndStringify("; a+=b?c:d;"));
ASSERT_EQUALS("; a = a * ( b + 1 ) ;", tokenizeAndStringify("; a*=b+1;"));
}
void simplifyAssignmentInFunctionCall()