simplifyCalculations: Improved handling of casts (#4899)

This commit is contained in:
Daniel Marjamäki 2013-09-03 06:27:11 +02:00
parent 0dddd424a4
commit 5d7518aa57
2 changed files with 23 additions and 1 deletions

View File

@ -840,7 +840,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
// keep parentheses here: Functor()(a ... )
// keep parentheses here: ) ( var ) ;
if ((Token::Match(tok->next(), "( %var% ) ;|)|,|]") ||
(Token::Match(tok->next(), "( %var% ) %cop%") && (tok->tokAt(2)->varId()>0 || !Token::Match(tok->tokAt(4), "[*&]")))) &&
(Token::Match(tok->next(), "( %var% ) %cop%") && (tok->tokAt(2)->varId()>0 || !Token::Match(tok->tokAt(4), "[*&+-]")))) &&
!tok->isName() &&
tok->str() != ">" &&
tok->str() != "]" &&

View File

@ -94,6 +94,7 @@ private:
TEST_CASE(removeCast10);
TEST_CASE(removeCast11);
TEST_CASE(removeCast12);
TEST_CASE(removeCast13);
TEST_CASE(inlineasm);
@ -1051,6 +1052,27 @@ private:
ASSERT_EQUALS("; ( ( short * ) data ) [ 5 ] = 0 ;", tokenizeAndStringify("; ((short*)data)[5] = 0;", true));
}
void removeCast13() {
// casting deref / address of
ASSERT_EQUALS("; int x ; x = * y ;", tokenizeAndStringify(";int x=(int)*y;",true));
ASSERT_EQUALS("; int x ; x = & y ;", tokenizeAndStringify(";int x=(int)&y;",true));
TODO_ASSERT_EQUALS("; int x ; x = ( INT ) * y ;",
"; int x ; x = * y ;",
tokenizeAndStringify(";int x=(INT)*y;",true)); // INT might be a variable
TODO_ASSERT_EQUALS("; int x ; x = ( INT ) & y ;",
"; int x ; x = & y ;",
tokenizeAndStringify(";int x=(INT)&y;",true)); // INT might be a variable
// #4899 - False positive on unused variable
ASSERT_EQUALS("; float angle ; angle = tilt ;", tokenizeAndStringify("; float angle = (float) tilt;", true)); // status quo
TODO_ASSERT_EQUALS("; float angle ; angle = - tilt ;",
"; float angle ; angle = ( float ) - tilt ;",
tokenizeAndStringify("; float angle = (float) -tilt;", true));
TODO_ASSERT_EQUALS("; float angle ; angle = tilt ;",
"; float angle ; angle = ( float ) + tilt ;",
tokenizeAndStringify("; float angle = (float) +tilt;", true));
}
void inlineasm() {
ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("asm { mov ax,bx };"));
ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("_asm { mov ax,bx };"));