Format numbers more nicely.

This commit is contained in:
chriseth 2017-07-13 22:07:01 +02:00
parent 1e05ebe50e
commit 5bfd5d98c1
2 changed files with 36 additions and 11 deletions

View File

@ -145,6 +145,17 @@ inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
return (prefix == HexPrefix::Add) ? "0x" + str : str; return (prefix == HexPrefix::Add) ? "0x" + str : str;
} }
/// Returns decimal representation for small numbers and hex for large numbers.
inline std::string formatNumber(bigint const& _value)
{
if (_value < 0)
return "-" + formatNumber(-_value);
if (_value > 0x1000000)
return toHex(toCompactBigEndian(_value), 2, HexPrefix::Add);
else
return _value.str();
}
inline std::string toCompactHexWithPrefix(u256 val) inline std::string toCompactHexWithPrefix(u256 val)
{ {
std::ostringstream ret; std::ostringstream ret;

View File

@ -262,14 +262,14 @@ void SMTChecker::arithmeticOperation(BinaryOperation const& _op)
checkCondition( checkCondition(
value < minValue(intType), value < minValue(intType),
_op.location(), _op.location(),
"Underflow (resulting value less than " + intType.minValue().str() + ")", "Underflow (resulting value less than " + formatNumber(intType.minValue()) + ")",
"value", "value",
&value &value
); );
checkCondition( checkCondition(
value > maxValue(intType), value > maxValue(intType),
_op.location(), _op.location(),
"Overflow (resulting value larger than " + intType.maxValue().str() + ")", "Overflow (resulting value larger than " + formatNumber(intType.maxValue()) + ")",
"value", "value",
&value &value
); );
@ -341,16 +341,26 @@ void SMTChecker::checkCondition(
m_interface->addAssertion(_condition); m_interface->addAssertion(_condition);
vector<smt::Expression> expressionsToEvaluate; vector<smt::Expression> expressionsToEvaluate;
vector<string> expressionNames;
if (m_currentFunction) if (m_currentFunction)
{ {
if (_additionalValue) if (_additionalValue)
{
expressionsToEvaluate.emplace_back(*_additionalValue); expressionsToEvaluate.emplace_back(*_additionalValue);
expressionNames.push_back(_additionalValueName);
}
for (auto const& param: m_currentFunction->parameters()) for (auto const& param: m_currentFunction->parameters())
if (knownVariable(*param)) if (knownVariable(*param))
{
expressionsToEvaluate.emplace_back(currentValue(*param)); expressionsToEvaluate.emplace_back(currentValue(*param));
expressionNames.push_back(param->name());
}
for (auto const& var: m_currentFunction->localVariables()) for (auto const& var: m_currentFunction->localVariables())
if (knownVariable(*var)) if (knownVariable(*var))
{
expressionsToEvaluate.emplace_back(currentValue(*var)); expressionsToEvaluate.emplace_back(currentValue(*var));
expressionNames.push_back(var->name());
}
} }
smt::CheckResult result; smt::CheckResult result;
vector<string> values; vector<string> values;
@ -373,18 +383,22 @@ void SMTChecker::checkCondition(
{ {
std::ostringstream message; std::ostringstream message;
message << _description << " happens here"; message << _description << " happens here";
size_t i = 0;
if (m_currentFunction) if (m_currentFunction)
{ {
message << " for:\n"; message << " for:\n";
if (_additionalValue) solAssert(values.size() == expressionNames.size(), "");
message << " " << _additionalValueName << " = " << values.at(i++) << "\n"; for (size_t i = 0; i < values.size(); ++i)
for (auto const& param: m_currentFunction->parameters()) {
if (knownVariable(*param)) string formattedValue = values.at(i);
message << " " << param->name() << " = " << values.at(i++) << "\n"; try
for (auto const& var: m_currentFunction->localVariables()) {
if (knownVariable(*var)) // Parse and re-format nicely
message << " " << var->name() << " = " << values.at(i++) << "\n"; formattedValue = formatNumber(bigint(formattedValue));
}
catch (...) { }
message << " " << expressionNames.at(i) << " = " << formattedValue << "\n";
}
} }
else else
message << "."; message << ".";