Skip to content

Commit

Permalink
Enable the -Wconversion warning
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Dec 8, 2020
1 parent 71a4a4e commit 7e88ba8
Show file tree
Hide file tree
Showing 56 changed files with 171 additions and 176 deletions.
1 change: 1 addition & 0 deletions cmake/EthCompilerSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MA
add_compile_options(-Wno-unknown-pragmas)
add_compile_options(-Wimplicit-fallthrough)
add_compile_options(-Wsign-conversion)
add_compile_options(-Wconversion)

eth_add_cxx_compiler_flag_if_supported(
$<$<COMPILE_LANGUAGE:CXX>:-Wextra-semi>
Expand Down
54 changes: 27 additions & 27 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ unsigned Assembly::bytesRequired(unsigned subTagSize) const
{
for (unsigned tagSize = subTagSize; true; ++tagSize)
{
unsigned ret = 1;
size_t ret = 1;
for (auto const& i: m_data)
ret += i.second.size();

for (AssemblyItem const& i: m_items)
ret += i.bytesRequired(tagSize);
if (util::bytesRequired(ret) <= tagSize)
return ret;
return static_cast<unsigned>(ret);
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
break;
case PushString:
collection.append(
createJsonValue("PUSH tag", sourceIndex, i.location().start, i.location().end, m_strings.at((h256)i.data())));
createJsonValue("PUSH tag", sourceIndex, i.location().start, i.location().end, m_strings.at(h256(i.data()))));
break;
case PushTag:
if (i.data() == 0)
Expand Down Expand Up @@ -573,21 +573,21 @@ LinkerObject const& Assembly::assemble() const
"Cannot push and assign immutables in the same assembly subroutine."
);

size_t bytesRequiredForCode = bytesRequired(subTagSize);
unsigned bytesRequiredForCode = bytesRequired(static_cast<unsigned>(subTagSize));
m_tagPositionsInBytecode = vector<size_t>(m_usedTags, numeric_limits<size_t>::max());
map<size_t, pair<size_t, size_t>> tagRef;
multimap<h256, unsigned> dataRef;
multimap<size_t, size_t> subRef;
vector<unsigned> sizeRef; ///< Pointers to code locations where the size of the program is inserted
unsigned bytesPerTag = util::bytesRequired(bytesRequiredForCode);
uint8_t tagPush = (uint8_t)pushInstruction(bytesPerTag);
uint8_t tagPush = static_cast<uint8_t>(pushInstruction(bytesPerTag));

unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + m_auxiliaryData.size();
unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast<unsigned>(m_auxiliaryData.size());
for (auto const& sub: m_subs)
bytesRequiredIncludingData += sub->assemble().bytecode.size();
bytesRequiredIncludingData += static_cast<unsigned>(sub->assemble().bytecode.size());

unsigned bytesPerDataRef = util::bytesRequired(bytesRequiredIncludingData);
uint8_t dataRefPush = (uint8_t)pushInstruction(bytesPerDataRef);
uint8_t dataRefPush = static_cast<uint8_t>(pushInstruction(bytesPerDataRef));
ret.bytecode.reserve(bytesRequiredIncludingData);

for (AssemblyItem const& i: m_items)
Expand All @@ -599,25 +599,25 @@ LinkerObject const& Assembly::assemble() const
switch (i.type())
{
case Operation:
ret.bytecode.push_back((uint8_t)i.instruction());
ret.bytecode.push_back(static_cast<uint8_t>(i.instruction()));
break;
case PushString:
{
ret.bytecode.push_back((uint8_t)Instruction::PUSH32);
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH32));
unsigned ii = 0;
for (auto j: m_strings.at((h256)i.data()))
for (auto j: m_strings.at(h256(i.data())))
if (++ii > 32)
break;
else
ret.bytecode.push_back((uint8_t)j);
ret.bytecode.push_back(uint8_t(j));
while (ii++ < 32)
ret.bytecode.push_back(0);
break;
}
case Push:
{
uint8_t b = max<unsigned>(1, util::bytesRequired(i.data()));
ret.bytecode.push_back((uint8_t)pushInstruction(b));
unsigned b = max<unsigned>(1, util::bytesRequired(i.data()));
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b)));
ret.bytecode.resize(ret.bytecode.size() + b);
bytesRef byr(&ret.bytecode.back() + 1 - b, b);
toBigEndian(i.data(), byr);
Expand All @@ -632,7 +632,7 @@ LinkerObject const& Assembly::assemble() const
}
case PushData:
ret.bytecode.push_back(dataRefPush);
dataRef.insert(make_pair((h256)i.data(), ret.bytecode.size()));
dataRef.insert(make_pair(h256(i.data()), ret.bytecode.size()));
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
break;
case PushSub:
Expand All @@ -646,8 +646,8 @@ LinkerObject const& Assembly::assemble() const
assertThrow(i.data() <= numeric_limits<size_t>::max(), AssemblyException, "");
auto s = subAssemblyById(static_cast<size_t>(i.data()))->assemble().bytecode.size();
i.setPushedValue(u256(s));
uint8_t b = max<unsigned>(1, util::bytesRequired(s));
ret.bytecode.push_back((uint8_t)pushInstruction(b));
unsigned b = max<unsigned>(1, util::bytesRequired(s));
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b)));
ret.bytecode.resize(ret.bytecode.size() + b);
bytesRef byr(&ret.bytecode.back() + 1 - b, b);
toBigEndian(s, byr);
Expand All @@ -656,36 +656,36 @@ LinkerObject const& Assembly::assemble() const
case PushProgramSize:
{
ret.bytecode.push_back(dataRefPush);
sizeRef.push_back(ret.bytecode.size());
sizeRef.push_back(static_cast<unsigned>(ret.bytecode.size()));
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
break;
}
case PushLibraryAddress:
ret.bytecode.push_back(uint8_t(Instruction::PUSH20));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH20));
ret.linkReferences[ret.bytecode.size()] = m_libraries.at(i.data());
ret.bytecode.resize(ret.bytecode.size() + 20);
break;
case PushImmutable:
ret.bytecode.push_back(uint8_t(Instruction::PUSH32));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH32));
ret.immutableReferences[i.data()].first = m_immutables.at(i.data());
ret.immutableReferences[i.data()].second.emplace_back(ret.bytecode.size());
ret.bytecode.resize(ret.bytecode.size() + 32);
break;
case AssignImmutable:
for (auto const& offset: immutableReferencesBySub[i.data()].second)
{
ret.bytecode.push_back(uint8_t(Instruction::DUP1));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::DUP1));
// TODO: should we make use of the constant optimizer methods for pushing the offsets?
bytes offsetBytes = toCompactBigEndian(u256(offset));
ret.bytecode.push_back(uint8_t(pushInstruction(offsetBytes.size())));
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(static_cast<unsigned>(offsetBytes.size()))));
ret.bytecode += offsetBytes;
ret.bytecode.push_back(uint8_t(Instruction::MSTORE));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::MSTORE));
}
immutableReferencesBySub.erase(i.data());
ret.bytecode.push_back(uint8_t(Instruction::POP));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::POP));
break;
case PushDeployTimeAddress:
ret.bytecode.push_back(uint8_t(Instruction::PUSH20));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH20));
ret.bytecode.resize(ret.bytecode.size() + 20);
break;
case Tag:
Expand All @@ -694,7 +694,7 @@ LinkerObject const& Assembly::assemble() const
assertThrow(ret.bytecode.size() < 0xffffffffL, AssemblyException, "Tag too large.");
assertThrow(m_tagPositionsInBytecode[static_cast<size_t>(i.data())] == numeric_limits<size_t>::max(), AssemblyException, "Duplicate tag position.");
m_tagPositionsInBytecode[static_cast<size_t>(i.data())] = ret.bytecode.size();
ret.bytecode.push_back((uint8_t)Instruction::JUMPDEST);
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::JUMPDEST));
break;
default:
assertThrow(false, InvalidOpcode, "Unexpected opcode while assembling.");
Expand All @@ -708,7 +708,7 @@ LinkerObject const& Assembly::assemble() const

if (!m_subs.empty() || !m_data.empty() || !m_auxiliaryData.empty())
// Append an INVALID here to help tests find miscompilation.
ret.bytecode.push_back(uint8_t(Instruction::INVALID));
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::INVALID));

for (auto const& [subIdPath, bytecodeOffset]: subRef)
{
Expand Down
1 change: 0 additions & 1 deletion libevmasm/ConstantOptimiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <libevmasm/ConstantOptimiser.h>
#include <libevmasm/Assembly.h>
#include <libevmasm/GasMeter.h>
#include <libsolutil/CommonData.h>

using namespace std;
using namespace solidity;
Expand Down
2 changes: 0 additions & 2 deletions libevmasm/ConstantOptimiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <liblangutil/EVMVersion.h>

#include <libsolutil/Assertions.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/CommonIO.h>

#include <vector>

Expand Down
8 changes: 4 additions & 4 deletions libevmasm/ControlFlowGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ void ControlFlowGraph::splitBlocks()
if (item.type() == Tag)
{
if (id)
m_blocks[id].end = index;
m_blocks[id].end = static_cast<unsigned>(index);
id = BlockId::invalid();
}
if (!id)
{
id = item.type() == Tag ? BlockId(item.data()) : generateNewId();
m_blocks[id].begin = index;
m_blocks[id].begin = static_cast<unsigned>(index);
}
if (item.type() == PushTag)
m_blocks[id].pushedTags.emplace_back(item.data());
if (SemanticInformation::altersControlFlow(item))
{
m_blocks[id].end = index + 1;
m_blocks[id].end = static_cast<unsigned>(index + 1);
if (item == Instruction::JUMP)
m_blocks[id].endType = BasicBlock::EndType::JUMP;
else if (item == Instruction::JUMPI)
Expand All @@ -103,7 +103,7 @@ void ControlFlowGraph::splitBlocks()
}
if (id)
{
m_blocks[id].end = m_items.size();
m_blocks[id].end = static_cast<unsigned>(m_items.size());
if (m_blocks[id].endType == BasicBlock::EndType::HANDOVER)
m_blocks[id].endType = BasicBlock::EndType::STOP;
}
Expand Down
4 changes: 2 additions & 2 deletions libevmasm/ExpressionClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ ExpressionClasses::Id ExpressionClasses::find(
exp.id = id;
else
{
exp.id = m_representatives.size();
exp.id = static_cast<Id>(m_representatives.size());
m_representatives.push_back(exp);
}
m_expressions.insert(exp);
Expand Down Expand Up @@ -117,7 +117,7 @@ void ExpressionClasses::forceEqual(
ExpressionClasses::Id ExpressionClasses::newClass(SourceLocation const& _location)
{
Expression exp;
exp.id = m_representatives.size();
exp.id = static_cast<Id>(m_representatives.size());
exp.item = storeItem(AssemblyItem(UndefinedItem, (u256(1) << 255) + exp.id, _location));
m_representatives.push_back(exp);
m_expressions.insert(exp);
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/ExpressionClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ExpressionClasses
/// @returns the canonical representative of an expression class.
Expression const& representative(Id _id) const { return m_representatives.at(_id); }
/// @returns the number of classes.
Id size() const { return m_representatives.size(); }
size_t size() const { return m_representatives.size(); }

/// Forces the given @a _item with @a _arguments to the class @a _id. This can be used to
/// add prior knowledge e.g. about CALLDATA, but has to be used with caution. Will not work as
Expand Down
2 changes: 1 addition & 1 deletion libsmtutil/CVC4Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ CVC4::Expr CVC4Interface::toCVC4Expr(Expression const& _expr)
else if (n == "int2bv")
{
size_t size = std::stoul(_expr.arguments[1].name);
auto i2bvOp = m_context.mkConst(CVC4::IntToBitVector(size));
auto i2bvOp = m_context.mkConst(CVC4::IntToBitVector(static_cast<unsigned>(size)));
// CVC4 treats all BVs as unsigned, so we need to manually apply 2's complement if needed.
return m_context.mkExpr(
CVC4::kind::ITE,
Expand Down
1 change: 0 additions & 1 deletion libsolidity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,3 @@ set(sources

add_library(solidity ${sources})
target_link_libraries(solidity PUBLIC yul evmasm langutil smtutil solutil Boost::boost)

3 changes: 1 addition & 2 deletions libsolidity/analysis/ControlFlowGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ class VariableOccurrence
using KindCompareType = std::underlying_type<VariableOccurrence::Kind>::type;
return
std::make_pair(m_declaration.id(), static_cast<KindCompareType>(m_occurrenceKind)) <
std::make_pair(_rhs.m_declaration.id(), static_cast<KindCompareType>(_rhs.m_occurrenceKind))
;
std::make_pair(_rhs.m_declaration.id(), static_cast<KindCompareType>(_rhs.m_occurrenceKind));
}

VariableDeclaration const& declaration() const { return m_declaration; }
Expand Down
6 changes: 3 additions & 3 deletions libsolidity/ast/ASTJsonConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ class ASTJsonConverter: public ASTConstVisitor
static std::string literalTokenKind(Token _token);
static std::string type(Expression const& _expression);
static std::string type(VariableDeclaration const& _varDecl);
static int nodeId(ASTNode const& _node)
static int64_t nodeId(ASTNode const& _node)
{
return _node.id();
}
template<class Container>
static Json::Value getContainerIds(Container const& _container, bool _order = false)
{
std::vector<int> tmp;
std::vector<int64_t> tmp;

for (auto const& element: _container)
{
Expand All @@ -174,7 +174,7 @@ class ASTJsonConverter: public ASTConstVisitor
std::sort(tmp.begin(), tmp.end());
Json::Value json(Json::arrayValue);

for (int val: tmp)
for (int64_t val: tmp)
json.append(val);

return json;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/ast/TypeProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ RationalNumberType const* TypeProvider::rationalNumber(Literal const& _literal)
{
size_t const digitCount = _literal.valueWithoutUnderscores().length() - 2;
if (digitCount % 2 == 0 && (digitCount / 2) <= 32)
compatibleBytesType = fixedBytes(digitCount / 2);
compatibleBytesType = fixedBytes(static_cast<unsigned>(digitCount / 2));
}

return rationalNumber(std::get<1>(validLiteral), compatibleBytesType);
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ tuple<bool, rational> RationalNumberType::parseRational(string const& _value)
denominator = bigint(string(fractionalBegin, _value.end()));
denominator /= boost::multiprecision::pow(
bigint(10),
static_cast<size_t>(distance(radixPoint + 1, _value.end()))
static_cast<unsigned>(distance(radixPoint + 1, _value.end()))
);
numerator = bigint(string(_value.begin(), radixPoint));
value = numerator + denominator;
Expand Down
3 changes: 2 additions & 1 deletion libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class Type
return *m_stackItems;
}
/// Total number of stack slots occupied by this type. This is the sum of ``sizeOnStack`` of all ``stackItems()``.
// TODO: consider changing the return type to be size_t
unsigned sizeOnStack() const
{
if (!m_stackSize)
Expand All @@ -306,7 +307,7 @@ class Type
++sizeOnStack;
m_stackSize = sizeOnStack;
}
return *m_stackSize;
return static_cast<unsigned>(*m_stackSize);
}
/// If it is possible to initialize such a value in memory by just writing zeros
/// of the size memoryHeadSize().
Expand Down
6 changes: 3 additions & 3 deletions libsolidity/codegen/CompilerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void CompilerContext::removeVariablesAboveStackHeight(unsigned _stackHeight)

unsigned CompilerContext::numberOfLocalVariables() const
{
return m_localVariables.size();
return static_cast<unsigned>(m_localVariables.size());
}

shared_ptr<evmasm::Assembly> CompilerContext::compiledContract(ContractDefinition const& _contract) const
Expand Down Expand Up @@ -421,10 +421,10 @@ void CompilerContext::appendInlineAssembly(
util::errinfo_comment("Stack too deep (" + to_string(stackDiff) + "), try removing local variables.")
);
if (_context == yul::IdentifierContext::RValue)
_assembly.appendInstruction(dupInstruction(stackDiff));
_assembly.appendInstruction(dupInstruction(static_cast<unsigned>(stackDiff)));
else
{
_assembly.appendInstruction(swapInstruction(stackDiff));
_assembly.appendInstruction(swapInstruction(static_cast<unsigned>(stackDiff)));
_assembly.appendInstruction(Instruction::POP);
}
};
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/CompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ void CompilerUtils::convertType(
{
auto const& arrayType = dynamic_cast<ArrayType const&>(_targetType);
solAssert(arrayType.isByteArray(), "");
unsigned storageSize = 32 + ((data.size() + 31) / 32) * 32;
size_t storageSize = 32 + ((data.size() + 31) / 32) * 32;
allocateMemory(storageSize);
// stack: mempos
m_context << Instruction::DUP1 << u256(data.size());
Expand Down
4 changes: 2 additions & 2 deletions libsolidity/codegen/ContractCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ size_t ContractCompiler::packIntoContractCreator(ContractDefinition const& _cont
auto const& immutables = contractType.immutableVariables();
// Push all immutable values on the stack.
for (auto const& immutable: immutables)
CompilerUtils(m_context).loadFromMemory(m_context.immutableMemoryOffset(*immutable), *immutable->annotation().type);
CompilerUtils(m_context).loadFromMemory(static_cast<unsigned>(m_context.immutableMemoryOffset(*immutable)), *immutable->annotation().type);
m_context.pushSubroutineSize(m_context.runtimeSub());
if (immutables.empty())
m_context << Instruction::DUP1;
Expand Down Expand Up @@ -662,7 +662,7 @@ bool ContractCompiler::visit(FunctionDefinition const& _function)
}
else
{
m_context << swapInstruction(stackLayout.size() - static_cast<unsigned>(stackLayout.back()) - 1u);
m_context << swapInstruction(static_cast<unsigned>(stackLayout.size()) - static_cast<unsigned>(stackLayout.back()) - 1u);
swap(stackLayout[static_cast<size_t>(stackLayout.back())], stackLayout.back());
}
for (size_t i = 0; i < stackLayout.size(); ++i)
Expand Down
Loading

0 comments on commit 7e88ba8

Please sign in to comment.