Always explicitly initialise Json objects

This commit is contained in:
Alex Beregszaszi 2021-09-15 19:09:03 +01:00
parent 69e9531181
commit 55c64e3ca1
5 changed files with 36 additions and 33 deletions

View File

@ -200,7 +200,7 @@ string Assembly::assemblyString(StringMap const& _sourceCodes) const
Json::Value Assembly::createJsonValue(string _name, int _source, int _begin, int _end, string _value, string _jumpType) Json::Value Assembly::createJsonValue(string _name, int _source, int _begin, int _end, string _value, string _jumpType)
{ {
Json::Value value; Json::Value value{Json::objectValue};
value["name"] = _name; value["name"] = _name;
value["source"] = _source; value["source"] = _source;
value["begin"] = _begin; value["begin"] = _begin;
@ -222,8 +222,9 @@ string Assembly::toStringInHex(u256 _value)
Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices) const Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices) const
{ {
Json::Value root; Json::Value root;
root[".code"] = Json::arrayValue;
Json::Value& collection = root[".code"] = Json::arrayValue; Json::Value& collection = root[".code"];
for (AssemblyItem const& i: m_items) for (AssemblyItem const& i: m_items)
{ {
int sourceIndex = -1; int sourceIndex = -1;
@ -317,7 +318,8 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
if (!m_data.empty() || !m_subs.empty()) if (!m_data.empty() || !m_subs.empty())
{ {
Json::Value& data = root[".data"] = Json::objectValue; root[".data"] = Json::objectValue;
Json::Value& data = root[".data"];
for (auto const& i: m_data) for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size()) if (u256(i.first) >= m_subs.size())
data[toStringInHex((u256)i.first)] = toHex(i.second); data[toStringInHex((u256)i.first)] = toHex(i.second);

View File

@ -56,7 +56,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
FunctionType const* externalFunctionType = it.second->interfaceFunctionType(); FunctionType const* externalFunctionType = it.second->interfaceFunctionType();
solAssert(!!externalFunctionType, ""); solAssert(!!externalFunctionType, "");
Json::Value method; Json::Value method{Json::objectValue};
method["type"] = "function"; method["type"] = "function";
method["name"] = it.second->declaration().name(); method["name"] = it.second->declaration().name();
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability()); method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
@ -80,7 +80,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
FunctionType constrType(*constructor); FunctionType constrType(*constructor);
FunctionType const* externalFunctionType = constrType.interfaceFunctionType(); FunctionType const* externalFunctionType = constrType.interfaceFunctionType();
solAssert(!!externalFunctionType, ""); solAssert(!!externalFunctionType, "");
Json::Value method; Json::Value method{Json::objectValue};
method["type"] = "constructor"; method["type"] = "constructor";
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability()); method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
method["inputs"] = formatTypeList( method["inputs"] = formatTypeList(
@ -96,23 +96,22 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
{ {
auto const* externalFunctionType = FunctionType(*fallbackOrReceive).interfaceFunctionType(); auto const* externalFunctionType = FunctionType(*fallbackOrReceive).interfaceFunctionType();
solAssert(!!externalFunctionType, ""); solAssert(!!externalFunctionType, "");
Json::Value method; Json::Value method{Json::objectValue};
method["type"] = TokenTraits::toString(fallbackOrReceive->kind()); method["type"] = TokenTraits::toString(fallbackOrReceive->kind());
method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability()); method["stateMutability"] = stateMutabilityToString(externalFunctionType->stateMutability());
abi.emplace(std::move(method)); abi.emplace(std::move(method));
} }
for (auto const& it: _contractDef.interfaceEvents()) for (auto const& it: _contractDef.interfaceEvents())
{ {
Json::Value event; Json::Value event{Json::objectValue};
event["type"] = "event"; event["type"] = "event";
event["name"] = it->name(); event["name"] = it->name();
event["anonymous"] = it->isAnonymous(); event["anonymous"] = it->isAnonymous();
Json::Value params(Json::arrayValue); Json::Value params{Json::arrayValue};
for (auto const& p: it->parameters()) for (auto const& p: it->parameters())
{ {
Type const* type = p->annotation().type->interfaceType(false); Type const* type = p->annotation().type->interfaceType(false);
solAssert(type, ""); solAssert(type, "");
Json::Value input;
auto param = formatType(p->name(), *type, *p->annotation().type, false); auto param = formatType(p->name(), *type, *p->annotation().type, false);
param["indexed"] = p->isIndexed(); param["indexed"] = p->isIndexed();
params.append(std::move(param)); params.append(std::move(param));
@ -123,7 +122,7 @@ Json::Value ABI::generate(ContractDefinition const& _contractDef)
for (ErrorDefinition const* error: _contractDef.interfaceErrors()) for (ErrorDefinition const* error: _contractDef.interfaceErrors())
{ {
Json::Value errorJson; Json::Value errorJson{Json::objectValue};
errorJson["type"] = "error"; errorJson["type"] = "error";
errorJson["name"] = error->name(); errorJson["name"] = error->name();
errorJson["inputs"] = Json::arrayValue; errorJson["inputs"] = Json::arrayValue;
@ -151,7 +150,7 @@ Json::Value ABI::formatTypeList(
bool _forLibrary bool _forLibrary
) )
{ {
Json::Value params(Json::arrayValue); Json::Value params{Json::arrayValue};
solAssert(_names.size() == _encodingTypes.size(), "Names and types vector size does not match"); solAssert(_names.size() == _encodingTypes.size(), "Names and types vector size does not match");
solAssert(_names.size() == _solidityTypes.size(), ""); solAssert(_names.size() == _solidityTypes.size(), "");
for (unsigned i = 0; i < _names.size(); ++i) for (unsigned i = 0; i < _names.size(); ++i)
@ -169,7 +168,7 @@ Json::Value ABI::formatType(
bool _forLibrary bool _forLibrary
) )
{ {
Json::Value ret; Json::Value ret{Json::objectValue};
ret["name"] = _name; ret["name"] = _name;
ret["internalType"] = _solidityType.toString(true); ret["internalType"] = _solidityType.toString(true);
string suffix = (_forLibrary && _encodingType.dataStoredIn(DataLocation::Storage)) ? " storage" : ""; string suffix = (_forLibrary && _encodingType.dataStoredIn(DataLocation::Storage)) ? " storage" : "";

View File

@ -1429,7 +1429,7 @@ CompilerStack::Source const& CompilerStack::source(string const& _sourceName) co
string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) const string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) const
{ {
Json::Value meta; Json::Value meta{Json::objectValue};
meta["version"] = 1; meta["version"] = 1;
meta["language"] = m_importedSources ? "SolidityAST" : "Solidity"; meta["language"] = m_importedSources ? "SolidityAST" : "Solidity";
meta["compiler"]["version"] = VersionStringStrict; meta["compiler"]["version"] = VersionStringStrict;

View File

@ -37,7 +37,7 @@ using namespace solidity::frontend;
Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef) Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef)
{ {
Json::Value doc; Json::Value doc{Json::objectValue};
doc["version"] = Json::Value(c_natspecVersion); doc["version"] = Json::Value(c_natspecVersion);
doc["kind"] = Json::Value("user"); doc["kind"] = Json::Value("user");
@ -50,7 +50,7 @@ Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef)
if (!value.empty()) if (!value.empty())
{ {
// add the constructor, only if we have any documentation to add // add the constructor, only if we have any documentation to add
Json::Value user; Json::Value user{Json::objectValue};
user["notice"] = Json::Value(value); user["notice"] = Json::Value(value);
doc["methods"]["constructor"] = user; doc["methods"]["constructor"] = user;
} }
@ -90,7 +90,7 @@ Json::Value Natspec::userDocumentation(ContractDefinition const& _contractDef)
string value = extractDoc(error->annotation().docTags, "notice"); string value = extractDoc(error->annotation().docTags, "notice");
if (!value.empty()) if (!value.empty())
{ {
Json::Value errorDoc; Json::Value errorDoc{Json::objectValue};
errorDoc["notice"] = value; errorDoc["notice"] = value;
doc["errors"][error->functionType(true)->externalSignature()].append(move(errorDoc)); doc["errors"][error->functionType(true)->externalSignature()].append(move(errorDoc));
} }
@ -225,7 +225,10 @@ Json::Value Natspec::extractCustomDoc(multimap<string, DocTag> const& _tags)
for (auto const& [tag, value]: _tags) for (auto const& [tag, value]: _tags)
if (boost::starts_with(tag, "custom")) if (boost::starts_with(tag, "custom"))
concatenated[tag] += value.content; concatenated[tag] += value.content;
Json::Value result; // We do not want to create an object if there are no custom tags found.
if (concatenated.empty())
return Json::nullValue;
Json::Value result{Json::objectValue};
for (auto& [tag, value]: concatenated) for (auto& [tag, value]: concatenated)
result[tag] = move(value); result[tag] = move(value);
return result; return result;

View File

@ -59,7 +59,7 @@ Json::Value formatError(
Json::Value const& _secondarySourceLocation = Json::Value() Json::Value const& _secondarySourceLocation = Json::Value()
) )
{ {
Json::Value error = Json::objectValue; Json::Value error{Json::objectValue};
error["type"] = _type; error["type"] = _type;
error["component"] = _component; error["component"] = _component;
error["severity"] = Error::formatErrorSeverityLowercase(_severity); error["severity"] = Error::formatErrorSeverityLowercase(_severity);
@ -74,7 +74,7 @@ Json::Value formatError(
Json::Value formatFatalError(string const& _type, string const& _message) Json::Value formatFatalError(string const& _type, string const& _message)
{ {
Json::Value output = Json::objectValue; Json::Value output{Json::objectValue};
output["errors"] = Json::arrayValue; output["errors"] = Json::arrayValue;
output["errors"].append(formatError(Error::Severity::Error, _type, "general", _message)); output["errors"].append(formatError(Error::Severity::Error, _type, "general", _message));
return output; return output;
@ -82,23 +82,22 @@ Json::Value formatFatalError(string const& _type, string const& _message)
Json::Value formatSourceLocation(SourceLocation const* location) Json::Value formatSourceLocation(SourceLocation const* location)
{ {
Json::Value sourceLocation; if (!location || !location->sourceName)
if (location && location->sourceName) return Json::nullValue;
{
sourceLocation["file"] = *location->sourceName;
sourceLocation["start"] = location->start;
sourceLocation["end"] = location->end;
}
Json::Value sourceLocation{Json::objectValue};
sourceLocation["file"] = *location->sourceName;
sourceLocation["start"] = location->start;
sourceLocation["end"] = location->end;
return sourceLocation; return sourceLocation;
} }
Json::Value formatSecondarySourceLocation(SecondarySourceLocation const* _secondaryLocation) Json::Value formatSecondarySourceLocation(SecondarySourceLocation const* _secondaryLocation)
{ {
if (!_secondaryLocation) if (!_secondaryLocation)
return {}; return Json::nullValue;
Json::Value secondarySourceLocation = Json::arrayValue; Json::Value secondarySourceLocation{Json::arrayValue};
for (auto const& location: _secondaryLocation->infos) for (auto const& location: _secondaryLocation->infos)
{ {
Json::Value msg = formatSourceLocation(&location.second); Json::Value msg = formatSourceLocation(&location.second);
@ -330,7 +329,7 @@ bool isIRRequested(Json::Value const& _outputSelection)
Json::Value formatLinkReferences(std::map<size_t, std::string> const& linkReferences) Json::Value formatLinkReferences(std::map<size_t, std::string> const& linkReferences)
{ {
Json::Value ret(Json::objectValue); Json::Value ret{Json::objectValue};
for (auto const& ref: linkReferences) for (auto const& ref: linkReferences)
{ {
@ -345,7 +344,7 @@ Json::Value formatLinkReferences(std::map<size_t, std::string> const& linkRefere
Json::Value fileObject = ret.get(file, Json::objectValue); Json::Value fileObject = ret.get(file, Json::objectValue);
Json::Value libraryArray = fileObject.get(name, Json::arrayValue); Json::Value libraryArray = fileObject.get(name, Json::arrayValue);
Json::Value entry = Json::objectValue; Json::Value entry{Json::objectValue};
entry["start"] = Json::UInt(ref.first); entry["start"] = Json::UInt(ref.first);
entry["length"] = 20; entry["length"] = 20;
@ -359,7 +358,7 @@ Json::Value formatLinkReferences(std::map<size_t, std::string> const& linkRefere
Json::Value formatImmutableReferences(map<u256, pair<string, vector<size_t>>> const& _immutableReferences) Json::Value formatImmutableReferences(map<u256, pair<string, vector<size_t>>> const& _immutableReferences)
{ {
Json::Value ret(Json::objectValue); Json::Value ret{Json::objectValue};
for (auto const& immutableReference: _immutableReferences) for (auto const& immutableReference: _immutableReferences)
{ {
@ -367,7 +366,7 @@ Json::Value formatImmutableReferences(map<u256, pair<string, vector<size_t>>> co
Json::Value array(Json::arrayValue); Json::Value array(Json::arrayValue);
for (size_t byteOffset: byteOffsets) for (size_t byteOffset: byteOffsets)
{ {
Json::Value byteRange(Json::objectValue); Json::Value byteRange{Json::objectValue};
byteRange["start"] = Json::UInt(byteOffset); byteRange["start"] = Json::UInt(byteOffset);
byteRange["length"] = Json::UInt(32); // immutable references are currently always 32 bytes wide byteRange["length"] = Json::UInt(32); // immutable references are currently always 32 bytes wide
array.append(byteRange); array.append(byteRange);
@ -386,7 +385,7 @@ Json::Value collectEVMObject(
function<bool(string)> const& _artifactRequested function<bool(string)> const& _artifactRequested
) )
{ {
Json::Value output = Json::objectValue; Json::Value output{Json::objectValue};
if (_artifactRequested("object")) if (_artifactRequested("object"))
output["object"] = _object.toHex(); output["object"] = _object.toHex();
if (_artifactRequested("opcodes")) if (_artifactRequested("opcodes"))