value flow: improved error message output for conditional division by zero

This commit is contained in:
Daniel Marjamäki 2014-01-06 10:51:17 +01:00
parent a5b79bc968
commit 39b47cc1e7
1 changed files with 24 additions and 2 deletions

View File

@ -2304,6 +2304,24 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
}
}
// TODO: this utility function should probably be moved to some common file
static std::string astStringify(const Token *top)
{
const Token *start = top;
while (start->astOperand1() && start->astOperand2())
start = start->astOperand1();
const Token *end = top;
while (end->astOperand1() && end->astOperand2())
end = end->astOperand2();
std::string str;
for (const Token *tok = start; tok && tok != end; tok = tok->next()) {
str += tok->str();
if (Token::Match(tok, "%var%|%num% %var%|%num%"))
str += " ";
}
return str + end->str();
}
void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv)
{
std::list<const Token *> callstack;
@ -2314,9 +2332,13 @@ void CheckOther::zerodivcondError(const Token *tokcond, const Token *tokdiv)
callstack.push_back(tokdiv);
}
std::string condition;
if (Token::Match(tokcond, "%num% <|<=")) {
if (!tokcond) {
// getErrorMessages
} else if (Token::Match(tokcond, "%num% <|<=")) {
condition = tokcond->strAt(2) + ((tokcond->strAt(1) == "<") ? ">" : ">=") + tokcond->str();
} else if (tokcond) {
} else if (tokcond->isComparisonOp()) {
condition = astStringify(tokcond);
} else {
if (tokcond->str() == "!")
condition = tokcond->next()->str() + "==0";
else