Fixed #2353 (Tokenizer::simplifyKnownVariables: wrong simplification after return)

This commit is contained in:
Daniel Marjamäki 2010-12-25 10:07:57 +01:00
parent 81c5576eb4
commit 6f8f0e1aa1
3 changed files with 20 additions and 17 deletions

View File

@ -6079,17 +6079,10 @@ bool Tokenizer::simplifyKnownVariables()
// Stop if return or break is found .. // Stop if return or break is found ..
if (tok3->str() == "break") if (tok3->str() == "break")
break; break;
if (indentlevel3 == 1) if ((indentlevel3 > 1 || !Token::simpleMatch(Token::findmatch(tok3,";"), "; }")) && tok3->str() == "return")
{ ret3 = true;
if (tok3->str() == "return") if (ret3 && tok3->str() == ";")
ret3 = true; break;
else if (tok3->str() == ";")
{
if (ret3 && !Token::simpleMatch(tok3->next(), "}"))
break;
ret3 = false;
}
}
if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str())) if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str()))
break; break;

View File

@ -5413,12 +5413,7 @@ private:
" int *p = &i;\n" " int *p = &i;\n"
" return *p;\n" " return *p;\n"
"}\n"; "}\n";
const char expected[] = "void f ( ) " ASSERT_EQUALS("void f ( ) { int i ; return i ; }", tok(code));
"{ "
"int i ; "
"return i ; "
"}";
ASSERT_EQUALS(expected, tok(code));
} }
void pointeralias3() void pointeralias3()

View File

@ -121,6 +121,7 @@ private:
TEST_CASE(simplifyKnownVariables32); // const TEST_CASE(simplifyKnownVariables32); // const
TEST_CASE(simplifyKnownVariables33); // struct variable TEST_CASE(simplifyKnownVariables33); // struct variable
TEST_CASE(simplifyKnownVariables34); TEST_CASE(simplifyKnownVariables34);
TEST_CASE(simplifyKnownVariables35); // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;'
TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutAssign);
TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor1);
TEST_CASE(simplifyKnownVariablesBailOutFor2); TEST_CASE(simplifyKnownVariablesBailOutFor2);
@ -1871,6 +1872,20 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
} }
void simplifyKnownVariables35()
{
// Ticket #2353
const char code[] = "int f() {"
" int x = 0;"
" if (x == 0) {"
" return 0;"
" }"
" return 10 / x;"
"}";
const char expected[] = "int f ( ) { int x ; x = 0 ; { return 0 ; } return 10 / x ; }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOutAssign() void simplifyKnownVariablesBailOutAssign()
{ {
const char code[] = "int foo() {\n" const char code[] = "int foo() {\n"