Set handlers use unified jsonPrint with prettifier if needed

This commit is contained in:
Aisultan Kali 2021-10-22 02:05:49 +00:00 committed by wechman
parent 1bc1f8bdbb
commit 96376264e8
36 changed files with 333 additions and 19 deletions

View File

@ -6,6 +6,7 @@ Language Features:
Compiler Features:
* Peephole Optimizer: Remove operations without side effects before simple terminations.
* Assembly-Json: Export: Include source list in `sourceList` field.
* Commandline Interface: option ``--pretty-json`` works also with the following options: ``--abi``, ``--asm-json``, ``--ast-compact-json``, ``--devdoc``, ``--storage-layout``, ``--userdoc``.
Bugfixes:

View File

@ -190,9 +190,9 @@ Json::Value ASTJsonConverter::inlineAssemblyIdentifierToJson(pair<yul::Identifie
return tuple;
}
void ASTJsonConverter::print(ostream& _stream, ASTNode const& _node)
void ASTJsonConverter::print(ostream& _stream, ASTNode const& _node, util::JsonFormat const& _format)
{
_stream << util::jsonPrettyPrint(toJson(_node));
_stream << util::jsonPrint(toJson(_node), _format);
}
Json::Value ASTJsonConverter::toJson(ASTNode const& _node)

View File

@ -29,6 +29,7 @@
#include <liblangutil/Exceptions.h>
#include <json/json.h>
#include <libsolutil/JSON.h>
#include <algorithm>
#include <optional>
@ -58,7 +59,7 @@ public:
std::map<std::string, unsigned> _sourceIndices = std::map<std::string, unsigned>()
);
/// Output the json representation of the AST to _stream.
void print(std::ostream& _stream, ASTNode const& _node);
void print(std::ostream& _stream, ASTNode const& _node, util::JsonFormat const& _format);
Json::Value toJson(ASTNode const& _node);
template <class T>
Json::Value toJson(std::vector<ASTPointer<T>> const& _nodes)

View File

@ -317,7 +317,7 @@ void CommandLineInterface::handleABI(string const& _contract)
if (!m_options.compiler.outputs.abi)
return;
string data = jsonCompactPrint(removeNullMembers(m_compiler->contractABI(_contract)));
string data = jsonPrint(removeNullMembers(m_compiler->contractABI(_contract)), m_options.formatting.json);
if (!m_options.output.dir.empty())
createFile(m_compiler->filesystemFriendlyName(_contract) + ".abi", data);
else
@ -331,7 +331,7 @@ void CommandLineInterface::handleStorageLayout(string const& _contract)
if (!m_options.compiler.outputs.storageLayout)
return;
string data = jsonCompactPrint(removeNullMembers(m_compiler->storageLayout(_contract)));
string data = jsonPrint(removeNullMembers(m_compiler->storageLayout(_contract)), m_options.formatting.json);
if (!m_options.output.dir.empty())
createFile(m_compiler->filesystemFriendlyName(_contract) + "_storage.json", data);
else
@ -361,12 +361,13 @@ void CommandLineInterface::handleNatspec(bool _natspecDev, string const& _contra
if (enabled)
{
std::string output = jsonPrettyPrint(
std::string output = jsonPrint(
removeNullMembers(
_natspecDev ?
m_compiler->natspecDev(_contract) :
m_compiler->natspecUser(_contract)
)
),
m_options.formatting.json
);
if (!m_options.output.dir.empty())
@ -892,7 +893,7 @@ void CommandLineInterface::handleAst()
{
stringstream data;
string postfix = "";
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(data, m_compiler->ast(sourceCode.first));
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(data, m_compiler->ast(sourceCode.first), m_options.formatting.json);
postfix += "_json";
boost::filesystem::path path(sourceCode.first);
createFile(path.filename().string() + postfix + ".ast", data.str());
@ -904,7 +905,7 @@ void CommandLineInterface::handleAst()
for (auto const& sourceCode: m_fileReader.sourceUnits())
{
sout() << endl << "======= " << sourceCode.first << " =======" << endl;
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(sout(), m_compiler->ast(sourceCode.first));
ASTJsonConverter(m_compiler->state(), m_compiler->sourceIndices()).print(sout(), m_compiler->ast(sourceCode.first), m_options.formatting.json);
}
}
}
@ -1146,7 +1147,7 @@ void CommandLineInterface::outputCompilationResults()
{
string ret;
if (m_options.compiler.outputs.asmJson)
ret = jsonPrettyPrint(removeNullMembers(m_compiler->assemblyJSON(contract)));
ret = util::jsonPrint(removeNullMembers(m_compiler->assemblyJSON(contract)), m_options.formatting.json);
else
ret = m_compiler->assemblyString(contract, m_fileReader.sourceUnits());

View File

@ -1 +1 @@
--asm-json
--asm-json --pretty-json

View File

@ -0,0 +1 @@
--asm-json

View File

@ -0,0 +1,9 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
contract C {
function f(uint x) public pure {
x += 42;
require(x > 100);
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
--ast-compact-json --allow-paths .

View File

@ -0,0 +1,4 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
contract C {}

View File

@ -0,0 +1,5 @@
JSON AST (compact format):
======= ast_compact_json_no_pretty_json/input.sol =======
{"absolutePath":"ast_compact_json_no_pretty_json/input.sol","exportedSymbols":{"C":[2]},"id":3,"license":"GPL-3.0","nodeType":"SourceUnit","nodes":[{"id":1,"literals":["solidity",">=","0.0"],"nodeType":"PragmaDirective","src":"36:22:0"},{"abstract":false,"baseContracts":[],"canonicalName":"C","contractDependencies":[],"contractKind":"contract","fullyImplemented":true,"id":2,"linearizedBaseContracts":[2],"name":"C","nameLocation":"69:1:0","nodeType":"ContractDefinition","nodes":[],"scope":3,"src":"60:13:0","usedErrors":[]}],"src":"36:38:0"}

View File

@ -1 +1 @@
--ast-compact-json --base-path . --allow-paths .
--ast-compact-json --pretty-json --base-path . --allow-paths .

View File

@ -1,7 +1,7 @@
{
"contracts":
{
"pretty_json_combined/input.sol:C":
"combined_json_abi/input.sol:C":
{
"abi": []
}

View File

@ -0,0 +1 @@
--combined-json abi

View File

@ -0,0 +1,3 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
contract C {}

View File

@ -0,0 +1 @@
{"contracts":{"combined_json_no_pretty_print/input.sol:C":{"abi":[]}},"version": "<VERSION REMOVED>"}

View File

@ -1 +1 @@
--combined-json ast --base-path . --allow-paths .
--combined-json ast --pretty-json --base-path . --allow-paths .

View File

@ -1 +1,111 @@
{"contracts":{"combined_json_with_base_path/c.sol:C":{}},"sourceList":["combined_json_with_base_path/c.sol","combined_json_with_base_path/input.sol"],"sources":{"combined_json_with_base_path/c.sol":{"AST":{"absolutePath":"combined_json_with_base_path/c.sol","exportedSymbols":{"C":[5]},"id":6,"license":"GPL-3.0","nodeType":"SourceUnit","nodes":[{"id":4,"literals":["solidity",">=","0.0"],"nodeType":"PragmaDirective","src":"36:22:0"},{"abstract":false,"baseContracts":[],"canonicalName":"C","contractDependencies":[],"contractKind":"contract","fullyImplemented":true,"id":5,"linearizedBaseContracts":[5],"name":"C","nameLocation":"69:1:0","nodeType":"ContractDefinition","nodes":[],"scope":6,"src":"60:13:0","usedErrors":[]}],"src":"36:38:0"}},"combined_json_with_base_path/input.sol":{"AST":{"absolutePath":"combined_json_with_base_path/input.sol","exportedSymbols":{"C":[5]},"id":3,"license":"GPL-3.0","nodeType":"SourceUnit","nodes":[{"id":1,"literals":["solidity",">=","0.0"],"nodeType":"PragmaDirective","src":"36:22:1"},{"absolutePath":"combined_json_with_base_path/c.sol","file":"./c.sol","id":2,"nameLocation":"-1:-1:-1","nodeType":"ImportDirective","scope":3,"sourceUnit":6,"src":"60:17:1","symbolAliases":[],"unitAlias":""}],"src":"36:42:1"}}},"version": "<VERSION REMOVED>"}
{
"contracts":
{
"combined_json_with_base_path/c.sol:C": {}
},
"sourceList":
[
"combined_json_with_base_path/c.sol",
"combined_json_with_base_path/input.sol"
],
"sources":
{
"combined_json_with_base_path/c.sol":
{
"AST":
{
"absolutePath": "combined_json_with_base_path/c.sol",
"exportedSymbols":
{
"C":
[
5
]
},
"id": 6,
"license": "GPL-3.0",
"nodeType": "SourceUnit",
"nodes":
[
{
"id": 4,
"literals":
[
"solidity",
">=",
"0.0"
],
"nodeType": "PragmaDirective",
"src": "36:22:0"
},
{
"abstract": false,
"baseContracts": [],
"canonicalName": "C",
"contractDependencies": [],
"contractKind": "contract",
"fullyImplemented": true,
"id": 5,
"linearizedBaseContracts":
[
5
],
"name": "C",
"nameLocation": "69:1:0",
"nodeType": "ContractDefinition",
"nodes": [],
"scope": 6,
"src": "60:13:0",
"usedErrors": []
}
],
"src": "36:38:0"
}
},
"combined_json_with_base_path/input.sol":
{
"AST":
{
"absolutePath": "combined_json_with_base_path/input.sol",
"exportedSymbols":
{
"C":
[
5
]
},
"id": 3,
"license": "GPL-3.0",
"nodeType": "SourceUnit",
"nodes":
[
{
"id": 1,
"literals":
[
"solidity",
">=",
"0.0"
],
"nodeType": "PragmaDirective",
"src": "36:22:1"
},
{
"absolutePath": "combined_json_with_base_path/c.sol",
"file": "./c.sol",
"id": 2,
"nameLocation": "-1:-1:-1",
"nodeType": "ImportDirective",
"scope": 3,
"sourceUnit": 6,
"src": "60:17:1",
"symbolAliases": [],
"unitAlias": ""
}
],
"src": "36:42:1"
}
}
},
"version": "<VERSION REMOVED>"
}

View File

@ -0,0 +1 @@
--combined-json devdoc --pretty-json --allow-paths .

View File

@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
/// @dev This is devdoc.
contract C {}
/// And this is a notice.
contract D {}

View File

@ -0,0 +1,25 @@
{
"contracts":
{
"combined_json_with_devdoc/input.sol:C":
{
"devdoc":
{
"details": "This is devdoc.",
"kind": "dev",
"methods": {},
"version": 1
}
},
"combined_json_with_devdoc/input.sol:D":
{
"devdoc":
{
"kind": "dev",
"methods": {},
"version": 1
}
}
},
"version": "<VERSION REMOVED>"
}

View File

@ -0,0 +1 @@
--combined-json userdoc --pretty-json --allow-paths .

View File

@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0;
/// @notice Description for users.
contract C {}
/// @dev Description for developers.
contract D {}

View File

@ -0,0 +1,25 @@
{
"contracts":
{
"combined_json_with_userdoc/input.sol:C":
{
"userdoc":
{
"kind": "user",
"methods": {},
"notice": "Description for users.",
"version": 1
}
},
"combined_json_with_userdoc/input.sol:D":
{
"userdoc":
{
"kind": "user",
"methods": {},
"version": 1
}
}
},
"version": "<VERSION REMOVED>"
}

View File

@ -1 +1 @@
--error-recovery --ast-compact-json --hashes
--error-recovery --ast-compact-json --pretty-json --hashes

View File

@ -0,0 +1,10 @@
{
"language": "Solidity",
"sources":
{
"A":
{
"content": "// SPDX-License-Identifier: GPL-3.0\npragma solidity >=0.0; contract C {}"
}
}
}

View File

@ -0,0 +1 @@
{"sources":{"A":{"id":0}}}

View File

@ -1 +1 @@
--storage-layout
--storage-layout --pretty-json

View File

@ -1,4 +1,87 @@
======= storage_layout_user_defined/input.sol:C =======
Contract Storage Layout:
{"storage":[{"astId":7,"contract":"storage_layout_user_defined/input.sol:C","label":"a","offset":0,"slot":"0","type":"t_userDefinedValueType(MyInt128)2"},{"astId":10,"contract":"storage_layout_user_defined/input.sol:C","label":"b","offset":16,"slot":"0","type":"t_userDefinedValueType(MyInt128)2"},{"astId":13,"contract":"storage_layout_user_defined/input.sol:C","label":"c","offset":0,"slot":"1","type":"t_userDefinedValueType(MyInt128)2"},{"astId":16,"contract":"storage_layout_user_defined/input.sol:C","label":"d","offset":16,"slot":"1","type":"t_userDefinedValueType(MyInt8)4"},{"astId":19,"contract":"storage_layout_user_defined/input.sol:C","label":"e","offset":17,"slot":"1","type":"t_userDefinedValueType(MyInt8)4"},{"astId":22,"contract":"storage_layout_user_defined/input.sol:C","label":"f","offset":18,"slot":"1","type":"t_userDefinedValueType(MyInt8)4"},{"astId":25,"contract":"storage_layout_user_defined/input.sol:C","label":"g","offset":19,"slot":"1","type":"t_userDefinedValueType(MyInt8)4"},{"astId":28,"contract":"storage_layout_user_defined/input.sol:C","label":"h","offset":20,"slot":"1","type":"t_userDefinedValueType(MyInt8)4"}],"types":{"t_userDefinedValueType(MyInt128)2":{"encoding":"inplace","label":"MyInt128","numberOfBytes":"16"},"t_userDefinedValueType(MyInt8)4":{"encoding":"inplace","label":"MyInt8","numberOfBytes":"1"}}}
{
"storage":
[
{
"astId": 7,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "a",
"offset": 0,
"slot": "0",
"type": "t_userDefinedValueType(MyInt128)2"
},
{
"astId": 10,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "b",
"offset": 16,
"slot": "0",
"type": "t_userDefinedValueType(MyInt128)2"
},
{
"astId": 13,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "c",
"offset": 0,
"slot": "1",
"type": "t_userDefinedValueType(MyInt128)2"
},
{
"astId": 16,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "d",
"offset": 16,
"slot": "1",
"type": "t_userDefinedValueType(MyInt8)4"
},
{
"astId": 19,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "e",
"offset": 17,
"slot": "1",
"type": "t_userDefinedValueType(MyInt8)4"
},
{
"astId": 22,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "f",
"offset": 18,
"slot": "1",
"type": "t_userDefinedValueType(MyInt8)4"
},
{
"astId": 25,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "g",
"offset": 19,
"slot": "1",
"type": "t_userDefinedValueType(MyInt8)4"
},
{
"astId": 28,
"contract": "storage_layout_user_defined/input.sol:C",
"label": "h",
"offset": 20,
"slot": "1",
"type": "t_userDefinedValueType(MyInt8)4"
}
],
"types":
{
"t_userDefinedValueType(MyInt128)2":
{
"encoding": "inplace",
"label": "MyInt128",
"numberOfBytes": "16"
},
"t_userDefinedValueType(MyInt8)4":
{
"encoding": "inplace",
"label": "MyInt8",
"numberOfBytes": "1"
}
}
}

View File

@ -0,0 +1 @@
--storage-layout

View File

@ -0,0 +1,4 @@
// SPDX-License-Identifier: GPL v3
pragma solidity >=0.0;
contract C {}

View File

@ -0,0 +1,4 @@
======= storage_layout_user_defined_no_pretty_print/input.sol:C =======
Contract Storage Layout:
{"storage":[]}

View File

@ -20,6 +20,7 @@
#include <libsolidity/ast/ASTJsonConverter.h>
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/JSON.h>
#include <test/Common.h>
#include <test/libsolidity/ASTJSONTest.h>
@ -248,7 +249,7 @@ bool ASTJSONTest::runTest(
for (size_t i = 0; i < m_sources.size(); i++)
{
ostringstream result;
ASTJsonConverter(_compiler.state(), _sourceIndices).print(result, _compiler.ast(m_sources[i].first));
ASTJsonConverter(_compiler.state(), _sourceIndices).print(result, _compiler.ast(m_sources[i].first), JsonFormat{ JsonFormat::Pretty });
_variant.result += result.str();
if (i != m_sources.size() - 1)
_variant.result += ",";