Fix ticket #364 (false positive:: division by zero)
http://apps.sourceforge.net/trac/cppcheck/ticket/364
This commit is contained in:
parent
51349d126b
commit
9cd5558f5e
|
@ -1465,6 +1465,7 @@ void Tokenizer::simplifyTokenList()
|
||||||
modified |= removeReduntantConditions();
|
modified |= removeReduntantConditions();
|
||||||
modified |= simplifyRedundantParanthesis();
|
modified |= simplifyRedundantParanthesis();
|
||||||
modified |= simplifyCalculations();
|
modified |= simplifyCalculations();
|
||||||
|
modified |= simplifyQuestionMark();
|
||||||
}
|
}
|
||||||
|
|
||||||
createLinks();
|
createLinks();
|
||||||
|
@ -1840,6 +1841,51 @@ bool Tokenizer::simplifyConditions()
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::simplifyQuestionMark()
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||||
|
{
|
||||||
|
if (tok->str() != "?")
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!tok->previous() || !tok->previous()->previous())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!Token::Match(tok->previous()->previous(), "[=,(]"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!Token::Match(tok->previous(), "%bool%") &&
|
||||||
|
!Token::Match(tok->previous(), "%num%"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (tok->previous()->str() == "false" ||
|
||||||
|
tok->previous()->str() == "0")
|
||||||
|
{
|
||||||
|
// Use code after semicolon, remove code before it.
|
||||||
|
const Token *end = Token::findmatch(tok, ":");
|
||||||
|
if (!end || !end->next())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
end = end->next();
|
||||||
|
tok = tok->previous();
|
||||||
|
while (tok->next() != end)
|
||||||
|
{
|
||||||
|
tok->deleteNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
Token *temp = tok;
|
||||||
|
tok = tok->next();
|
||||||
|
temp->deleteThis();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Use code before semicolon
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
bool Tokenizer::simplifyCasts()
|
bool Tokenizer::simplifyCasts()
|
||||||
{
|
{
|
||||||
|
|
|
@ -100,6 +100,11 @@ public:
|
||||||
*/
|
*/
|
||||||
bool simplifyVarDecl();
|
bool simplifyVarDecl();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simplify question mark - colon operator
|
||||||
|
* Example: 0 ? (2/0) : 0 => 0
|
||||||
|
*/
|
||||||
|
bool simplifyQuestionMark();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* simplify if-assignments..
|
* simplify if-assignments..
|
||||||
|
|
|
@ -111,10 +111,14 @@ private:
|
||||||
" cout<<b/sum;\n"
|
" cout<<b/sum;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" int a = 0 ? (2/0) : 0;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void delete1()
|
void delete1()
|
||||||
{
|
{
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
|
|
Loading…
Reference in New Issue