Refactoring; Use range for

This commit is contained in:
Daniel Marjamäki 2018-04-24 18:02:36 +02:00
parent 40c42f2de7
commit dbcdb19b73
1 changed files with 60 additions and 60 deletions

View File

@ -387,157 +387,157 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti
return; return;
} }
for (std::list<ValueFlow::Value>::const_iterator value1 = parent->astOperand1()->values().begin(); value1 != parent->astOperand1()->values().end(); ++value1) { for (const ValueFlow::Value &value1 : parent->astOperand1()->values()) {
if (!value1->isIntValue() && !value1->isFloatValue() && !value1->isTokValue()) if (!value1.isIntValue() && !value1.isFloatValue() && !value1.isTokValue())
continue; continue;
if (value1->isTokValue() && (!parent->isComparisonOp() || value1->tokvalue->tokType() != Token::eString)) if (value1.isTokValue() && (!parent->isComparisonOp() || value1.tokvalue->tokType() != Token::eString))
continue; continue;
for (std::list<ValueFlow::Value>::const_iterator value2 = parent->astOperand2()->values().begin(); value2 != parent->astOperand2()->values().end(); ++value2) { for (const ValueFlow::Value &value2 : parent->astOperand2()->values()) {
if (!value2->isIntValue() && !value2->isFloatValue() && !value2->isTokValue()) if (!value2.isIntValue() && !value2.isFloatValue() && !value2.isTokValue())
continue; continue;
if (value2->isTokValue() && (!parent->isComparisonOp() || value2->tokvalue->tokType() != Token::eString || value1->isTokValue())) if (value2.isTokValue() && (!parent->isComparisonOp() || value2.tokvalue->tokType() != Token::eString || value1.isTokValue()))
continue; continue;
if (known || value1->varId == 0U || value2->varId == 0U || if (known || value1.varId == 0U || value2.varId == 0U ||
(value1->varId == value2->varId && value1->varvalue == value2->varvalue && value1->isIntValue() && value2->isIntValue())) { (value1.varId == value2.varId && value1.varvalue == value2.varvalue && value1.isIntValue() && value2.isIntValue())) {
ValueFlow::Value result(0); ValueFlow::Value result(0);
result.condition = value1->condition ? value1->condition : value2->condition; result.condition = value1.condition ? value1.condition : value2.condition;
result.setInconclusive(value1->isInconclusive() | value2->isInconclusive()); result.setInconclusive(value1.isInconclusive() | value2.isInconclusive());
result.varId = (value1->varId != 0U) ? value1->varId : value2->varId; result.varId = (value1.varId != 0U) ? value1.varId : value2.varId;
result.varvalue = (result.varId == value1->varId) ? value1->varvalue : value2->varvalue; result.varvalue = (result.varId == value1.varId) ? value1.varvalue : value2.varvalue;
result.errorPath = (value1->errorPath.empty() ? value2 : value1)->errorPath; result.errorPath = (value1.errorPath.empty() ? value2 : value1).errorPath;
if (value1->valueKind == value2->valueKind) if (value1.valueKind == value2.valueKind)
result.valueKind = value1->valueKind; result.valueKind = value1.valueKind;
const float floatValue1 = value1->isIntValue() ? value1->intvalue : value1->floatValue; const float floatValue1 = value1.isIntValue() ? value1.intvalue : value1.floatValue;
const float floatValue2 = value2->isIntValue() ? value2->intvalue : value2->floatValue; const float floatValue2 = value2.isIntValue() ? value2.intvalue : value2.floatValue;
switch (parent->str()[0]) { switch (parent->str()[0]) {
case '+': case '+':
if (value1->isTokValue() || value2->isTokValue()) if (value1.isTokValue() || value2.isTokValue())
break; break;
if (value1->isFloatValue() || value2->isFloatValue()) { if (value1.isFloatValue() || value2.isFloatValue()) {
result.valueType = ValueFlow::Value::FLOAT; result.valueType = ValueFlow::Value::FLOAT;
result.floatValue = floatValue1 + floatValue2; result.floatValue = floatValue1 + floatValue2;
} else { } else {
result.intvalue = value1->intvalue + value2->intvalue; result.intvalue = value1.intvalue + value2.intvalue;
} }
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '-': case '-':
if (value1->isTokValue() || value2->isTokValue()) if (value1.isTokValue() || value2.isTokValue())
break; break;
if (value1->isFloatValue() || value2->isFloatValue()) { if (value1.isFloatValue() || value2.isFloatValue()) {
result.valueType = ValueFlow::Value::FLOAT; result.valueType = ValueFlow::Value::FLOAT;
result.floatValue = floatValue1 - floatValue2; result.floatValue = floatValue1 - floatValue2;
} else { } else {
result.intvalue = value1->intvalue - value2->intvalue; result.intvalue = value1.intvalue - value2.intvalue;
} }
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '*': case '*':
if (value1->isTokValue() || value2->isTokValue()) if (value1.isTokValue() || value2.isTokValue())
break; break;
if (value1->isFloatValue() || value2->isFloatValue()) { if (value1.isFloatValue() || value2.isFloatValue()) {
result.valueType = ValueFlow::Value::FLOAT; result.valueType = ValueFlow::Value::FLOAT;
result.floatValue = floatValue1 * floatValue2; result.floatValue = floatValue1 * floatValue2;
} else { } else {
result.intvalue = value1->intvalue * value2->intvalue; result.intvalue = value1.intvalue * value2.intvalue;
} }
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '/': case '/':
if (value1->isTokValue() || value2->isTokValue() || value2->intvalue == 0) if (value1.isTokValue() || value2.isTokValue() || value2.intvalue == 0)
break; break;
if (value1->isFloatValue() || value2->isFloatValue()) { if (value1.isFloatValue() || value2.isFloatValue()) {
result.valueType = ValueFlow::Value::FLOAT; result.valueType = ValueFlow::Value::FLOAT;
result.floatValue = floatValue1 / floatValue2; result.floatValue = floatValue1 / floatValue2;
} else { } else {
result.intvalue = value1->intvalue / value2->intvalue; result.intvalue = value1.intvalue / value2.intvalue;
} }
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '%': case '%':
if (!value1->isIntValue() || !value2->isIntValue()) if (!value1.isIntValue() || !value2.isIntValue())
break; break;
if (value2->intvalue == 0) if (value2.intvalue == 0)
break; break;
result.intvalue = value1->intvalue % value2->intvalue; result.intvalue = value1.intvalue % value2.intvalue;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '=': case '=':
if (parent->str() == "==") { if (parent->str() == "==") {
if ((value1->isIntValue() && value2->isTokValue()) || if ((value1.isIntValue() && value2.isTokValue()) ||
(value1->isTokValue() && value2->isIntValue())) { (value1.isTokValue() && value2.isIntValue())) {
result.intvalue = 0; result.intvalue = 0;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
} else if (value1->isIntValue() && value2->isIntValue()) { } else if (value1.isIntValue() && value2.isIntValue()) {
result.intvalue = value1->intvalue == value2->intvalue; result.intvalue = value1.intvalue == value2.intvalue;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
} }
} }
break; break;
case '!': case '!':
if (parent->str() == "!=") { if (parent->str() == "!=") {
if ((value1->isIntValue() && value2->isTokValue()) || if ((value1.isIntValue() && value2.isTokValue()) ||
(value1->isTokValue() && value2->isIntValue())) { (value1.isTokValue() && value2.isIntValue())) {
result.intvalue = 1; result.intvalue = 1;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
} else if (value1->isIntValue() && value2->isIntValue()) { } else if (value1.isIntValue() && value2.isIntValue()) {
result.intvalue = value1->intvalue != value2->intvalue; result.intvalue = value1.intvalue != value2.intvalue;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
} }
} }
break; break;
case '>': { case '>': {
const bool f = value1->isFloatValue() || value2->isFloatValue(); const bool f = value1.isFloatValue() || value2.isFloatValue();
if (!f && !value1->isIntValue() && !value2->isIntValue()) if (!f && !value1.isIntValue() && !value2.isIntValue())
break; break;
if (parent->str() == ">") if (parent->str() == ">")
result.intvalue = f ? (floatValue1 > floatValue2) : (value1->intvalue > value2->intvalue); result.intvalue = f ? (floatValue1 > floatValue2) : (value1.intvalue > value2.intvalue);
else if (parent->str() == ">=") else if (parent->str() == ">=")
result.intvalue = f ? (floatValue1 >= floatValue2) : (value1->intvalue >= value2->intvalue); result.intvalue = f ? (floatValue1 >= floatValue2) : (value1.intvalue >= value2.intvalue);
else if (!f && parent->str() == ">>" && value1->intvalue >= 0 && value2->intvalue >= 0 && value2->intvalue < MathLib::bigint_bits) else if (!f && parent->str() == ">>" && value1.intvalue >= 0 && value2.intvalue >= 0 && value2.intvalue < MathLib::bigint_bits)
result.intvalue = value1->intvalue >> value2->intvalue; result.intvalue = value1.intvalue >> value2.intvalue;
else else
break; break;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
} }
case '<': { case '<': {
const bool f = value1->isFloatValue() || value2->isFloatValue(); const bool f = value1.isFloatValue() || value2.isFloatValue();
if (!f && !value1->isIntValue() && !value2->isIntValue()) if (!f && !value1.isIntValue() && !value2.isIntValue())
break; break;
if (parent->str() == "<") if (parent->str() == "<")
result.intvalue = f ? (floatValue1 < floatValue2) : (value1->intvalue < value2->intvalue); result.intvalue = f ? (floatValue1 < floatValue2) : (value1.intvalue < value2.intvalue);
else if (parent->str() == "<=") else if (parent->str() == "<=")
result.intvalue = f ? (floatValue1 <= floatValue2) : (value1->intvalue <= value2->intvalue); result.intvalue = f ? (floatValue1 <= floatValue2) : (value1.intvalue <= value2.intvalue);
else if (!f && parent->str() == "<<" && value1->intvalue >= 0 && value2->intvalue >= 0 && value2->intvalue < MathLib::bigint_bits) else if (!f && parent->str() == "<<" && value1.intvalue >= 0 && value2.intvalue >= 0 && value2.intvalue < MathLib::bigint_bits)
result.intvalue = value1->intvalue << value2->intvalue; result.intvalue = value1.intvalue << value2.intvalue;
else else
break; break;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
} }
case '&': case '&':
if (!value1->isIntValue() || !value2->isIntValue()) if (!value1.isIntValue() || !value2.isIntValue())
break; break;
if (parent->str() == "&") if (parent->str() == "&")
result.intvalue = value1->intvalue & value2->intvalue; result.intvalue = value1.intvalue & value2.intvalue;
else else
result.intvalue = value1->intvalue && value2->intvalue; result.intvalue = value1.intvalue && value2.intvalue;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '|': case '|':
if (!value1->isIntValue() || !value2->isIntValue()) if (!value1.isIntValue() || !value2.isIntValue())
break; break;
if (parent->str() == "|") if (parent->str() == "|")
result.intvalue = value1->intvalue | value2->intvalue; result.intvalue = value1.intvalue | value2.intvalue;
else else
result.intvalue = value1->intvalue || value2->intvalue; result.intvalue = value1.intvalue || value2.intvalue;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
case '^': case '^':
if (!value1->isIntValue() || !value2->isIntValue()) if (!value1.isIntValue() || !value2.isIntValue())
break; break;
result.intvalue = value1->intvalue ^ value2->intvalue; result.intvalue = value1.intvalue ^ value2.intvalue;
setTokenValue(parent, result, settings); setTokenValue(parent, result, settings);
break; break;
default: default: