Fixed #3723 (Preprocessor evaluation order)

This commit is contained in:
Daniel Marjamäki 2012-06-14 21:47:03 +02:00
parent 8b294a2d4f
commit 5174f7ff5e
2 changed files with 26 additions and 3 deletions

View File

@ -748,6 +748,26 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
} }
if (tok->isNumber()) { if (tok->isNumber()) {
// Remove redundant conditions (0&&x) (1||x)
if (Token::Match(tok->previous(), "[(=,] 0 &&") ||
Token::Match(tok->previous(), "[(=,] 1 ||")) {
unsigned int par = 0;
const Token *tok2 = tok;
for (tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "(")
++par;
else if (tok2->str() == ")") {
if (par == 0)
break;
--par;
} else if (par == 0 && (Token::Match(tok2, "[,;]")))
break;
}
if (Token::Match(tok2, "[);,]"))
Token::eraseTokens(tok, tok2);
continue;
}
if (tok->str() == "0") { if (tok->str() == "0") {
if (Token::Match(tok->previous(), "[+-|] 0")) { if (Token::Match(tok->previous(), "[+-|] 0")) {
tok = tok->previous(); tok = tok->previous();
@ -766,7 +786,6 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
tok->deleteThis(); tok->deleteThis();
ret = true; ret = true;
} else if (Token::Match(tok->previous(), "[=[(,] 0 * %any% ,|]|)|;|=|%op%") || } else if (Token::Match(tok->previous(), "[=[(,] 0 * %any% ,|]|)|;|=|%op%") ||
Token::Match(tok->previous(), "[=[(,] 0 && %any% ,|]|)|;|=|%op%") ||
Token::Match(tok->previous(), "return|case 0 * %any% ,|:|;|=|%op%") || Token::Match(tok->previous(), "return|case 0 * %any% ,|:|;|=|%op%") ||
Token::Match(tok->previous(), "return|case 0 && %any% ,|:|;|=|%op%")) { Token::Match(tok->previous(), "return|case 0 && %any% ,|:|;|=|%op%")) {
tok->deleteNext(); tok->deleteNext();

View File

@ -393,7 +393,7 @@ private:
TEST_CASE(simplifyLogicalOperators); TEST_CASE(simplifyLogicalOperators);
TEST_CASE(simplifyCalculations); // ticket #2870 TEST_CASE(simplifyCalculations);
// foo(p = new char[10]); => p = new char[10]; foo(p); // foo(p = new char[10]); => p = new char[10]; foo(p);
TEST_CASE(simplifyAssignmentInFunctionCall); TEST_CASE(simplifyAssignmentInFunctionCall);
@ -6348,7 +6348,11 @@ private:
tokenizeAndStringify("int foo ( ) { int i; int j; i = 1 || j; return i; }", true)); tokenizeAndStringify("int foo ( ) { int i; int j; i = 1 || j; return i; }", true));
ASSERT_EQUALS("int foo ( ) { return 0 ; }", ASSERT_EQUALS("int foo ( ) { return 0 ; }",
tokenizeAndStringify("int foo ( ) { int i; int j; i = 0 && j; return i; }", true)); tokenizeAndStringify("int foo ( ) { int i; int j; i = 0 && j; return i; }", true)); // ticket #3576 - False positives in boolean expressions
// ticket #3723 - Simplify condition (0 && a < 123)
ASSERT_EQUALS("( 0 )",
tokenizeAndStringify("( 0 && a < 123 )", true));
} }
void simplifyCompoundAssignment() { void simplifyCompoundAssignment() {