From 195f72e844e7cac0907053f6f5a8cd5857bafc23 Mon Sep 17 00:00:00 2001 From: nishant-sachdeva Date: Thu, 10 Feb 2022 19:44:08 +0530 Subject: [PATCH] Added errorSelector field for custom errors --- Changelog.md | 3 +- libsolidity/ast/ASTJsonConverter.cpp | 21 ++++- test/libsolidity/ASTJSON/documentation.json | 1 + .../libsolidity/ASTJSON/event_definition.json | 1 + ...vent_with_variables_of_internal_types.json | 0 ...event_with_variables_of_internal_types.sol | 5 ++ ...variables_of_internal_types_parseOnly.json | 78 +++++++++++++++++++ test/libsolidity/ASTJSON/used_errors.json | 20 ++--- test/libsolidity/ASTJSON/used_errors.sol | 2 +- .../ASTJSON/used_errors_parseOnly.json | 18 ++--- 10 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 test/libsolidity/ASTJSON/event_with_variables_of_internal_types.json create mode 100644 test/libsolidity/ASTJSON/event_with_variables_of_internal_types.sol create mode 100644 test/libsolidity/ASTJSON/event_with_variables_of_internal_types_parseOnly.json diff --git a/Changelog.md b/Changelog.md index 300bc0122..fd7dda584 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,12 +4,11 @@ Language Features: Compiler Features: - +* JSON-AST: Added selector field for errors and events. Bugfixes: - ### 0.8.12 (2022-02-16) Language Features: diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index 326cca43e..97417f81c 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -493,24 +494,36 @@ bool ASTJsonConverter::visit(ModifierInvocation const& _node) bool ASTJsonConverter::visit(EventDefinition const& _node) { m_inEvent = true; - setJsonNode(_node, "EventDefinition", { + std::vector> _attributes = { make_pair("name", _node.name()), make_pair("nameLocation", sourceLocationToString(_node.nameLocation())), make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json::nullValue), make_pair("parameters", toJson(_node.parameterList())), make_pair("anonymous", _node.isAnonymous()) - }); + }; + if (m_stackState >= CompilerStack::State::AnalysisPerformed) + _attributes.emplace_back( + make_pair( + "eventSelector", + toHex(u256(h256::Arith(util::keccak256(_node.functionType(true)->externalSignature())))) + )); + + setJsonNode(_node, "EventDefinition", std::move(_attributes)); return false; } bool ASTJsonConverter::visit(ErrorDefinition const& _node) { - setJsonNode(_node, "ErrorDefinition", { + std::vector> _attributes = { make_pair("name", _node.name()), make_pair("nameLocation", sourceLocationToString(_node.nameLocation())), make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json::nullValue), make_pair("parameters", toJson(_node.parameterList())) - }); + }; + if (m_stackState >= CompilerStack::State::AnalysisPerformed) + _attributes.emplace_back(make_pair("errorSelector", _node.functionType(true)->externalIdentifierHex())); + + setJsonNode(_node, "ErrorDefinition", std::move(_attributes)); return false; } diff --git a/test/libsolidity/ASTJSON/documentation.json b/test/libsolidity/ASTJSON/documentation.json index de512181f..f31382d3a 100644 --- a/test/libsolidity/ASTJSON/documentation.json +++ b/test/libsolidity/ASTJSON/documentation.json @@ -162,6 +162,7 @@ "src": "69:26:3", "text": "Some comment on Evt." }, + "eventSelector": "a69007916fc1145953e5a7032d7c3eab4b8e2f33ec59b0f71e732904eeede3a4", "id": 12, "name": "Evt", "nameLocation": "102:3:3", diff --git a/test/libsolidity/ASTJSON/event_definition.json b/test/libsolidity/ASTJSON/event_definition.json index 627fd13ba..0bb614c22 100644 --- a/test/libsolidity/ASTJSON/event_definition.json +++ b/test/libsolidity/ASTJSON/event_definition.json @@ -30,6 +30,7 @@ [ { "anonymous": false, + "eventSelector": "92bbf6e823a631f3c8e09b1c8df90f378fb56f7fbc9701827e1ff8aad7f6a028", "id": 2, "name": "E", "nameLocation": "19:1:1", diff --git a/test/libsolidity/ASTJSON/event_with_variables_of_internal_types.json b/test/libsolidity/ASTJSON/event_with_variables_of_internal_types.json new file mode 100644 index 000000000..e69de29bb diff --git a/test/libsolidity/ASTJSON/event_with_variables_of_internal_types.sol b/test/libsolidity/ASTJSON/event_with_variables_of_internal_types.sol new file mode 100644 index 000000000..a22910570 --- /dev/null +++ b/test/libsolidity/ASTJSON/event_with_variables_of_internal_types.sol @@ -0,0 +1,5 @@ +contract C { + event E(function() internal); +} + +// ---- diff --git a/test/libsolidity/ASTJSON/event_with_variables_of_internal_types_parseOnly.json b/test/libsolidity/ASTJSON/event_with_variables_of_internal_types_parseOnly.json new file mode 100644 index 000000000..0c90ee840 --- /dev/null +++ b/test/libsolidity/ASTJSON/event_with_variables_of_internal_types_parseOnly.json @@ -0,0 +1,78 @@ +{ + "absolutePath": "a", + "id": 8, + "nodeType": "SourceUnit", + "nodes": + [ + { + "abstract": false, + "baseContracts": [], + "contractDependencies": [], + "contractKind": "contract", + "id": 7, + "name": "C", + "nameLocation": "9:1:1", + "nodeType": "ContractDefinition", + "nodes": + [ + { + "anonymous": false, + "id": 6, + "name": "E", + "nameLocation": "23:1:1", + "nodeType": "EventDefinition", + "parameters": + { + "id": 5, + "nodeType": "ParameterList", + "parameters": + [ + { + "constant": false, + "id": 4, + "indexed": false, + "mutability": "mutable", + "name": "", + "nameLocation": "-1:-1:-1", + "nodeType": "VariableDeclaration", + "src": "25:20:1", + "stateVariable": false, + "storageLocation": "default", + "typeDescriptions": {}, + "typeName": + { + "id": 3, + "nodeType": "FunctionTypeName", + "parameterTypes": + { + "id": 1, + "nodeType": "ParameterList", + "parameters": [], + "src": "33:2:1" + }, + "returnParameterTypes": + { + "id": 2, + "nodeType": "ParameterList", + "parameters": [], + "src": "44:0:1" + }, + "src": "25:20:1", + "stateMutability": "nonpayable", + "typeDescriptions": {}, + "visibility": "internal" + }, + "visibility": "internal" + } + ], + "src": "24:21:1" + }, + "src": "17:29:1" + } + ], + "src": "0:48:1", + "usedErrors": [] + } + ], + "src": "0:49:1" +} diff --git a/test/libsolidity/ASTJSON/used_errors.json b/test/libsolidity/ASTJSON/used_errors.json index a8ca24a28..a0e8e28ba 100644 --- a/test/libsolidity/ASTJSON/used_errors.json +++ b/test/libsolidity/ASTJSON/used_errors.json @@ -20,6 +20,7 @@ "nodes": [ { + "errorSelector": "c1599bd9", "id": 2, "name": "X", "nameLocation": "6:1:1", @@ -127,6 +128,7 @@ "nodes": [ { + "errorSelector": "2bc80f3a", "id": 11, "name": "T", "nameLocation": "63:1:1", @@ -145,7 +147,7 @@ { "id": 17, "nodeType": "Block", - "src": "97:8:1", + "src": "92:8:1", "statements": [ { @@ -160,7 +162,7 @@ "nodeType": "Identifier", "overloadedDeclarations": [], "referencedDeclaration": 9, - "src": "99:1:1", + "src": "94:1:1", "typeDescriptions": { "typeIdentifier": "t_function_internal_nonpayable$__$returns$__$", @@ -175,7 +177,7 @@ "lValueRequested": false, "names": [], "nodeType": "FunctionCall", - "src": "99:3:1", + "src": "94:3:1", "tryCall": false, "typeDescriptions": { @@ -185,7 +187,7 @@ }, "id": 16, "nodeType": "ExpressionStatement", - "src": "99:3:1" + "src": "94:3:1" } ] }, @@ -209,17 +211,17 @@ "id": 13, "nodeType": "ParameterList", "parameters": [], - "src": "97:0:1" + "src": "92:0:1" }, "scope": 19, - "src": "72:33:1", - "stateMutability": "pure", + "src": "72:28:1", + "stateMutability": "nonpayable", "virtual": false, "visibility": "public" } ], "scope": 20, - "src": "40:67:1", + "src": "40:62:1", "usedErrors": [ 2, @@ -227,5 +229,5 @@ ] } ], - "src": "0:108:1" + "src": "0:103:1" } diff --git a/test/libsolidity/ASTJSON/used_errors.sol b/test/libsolidity/ASTJSON/used_errors.sol index e891a20ee..ead3e2dcf 100644 --- a/test/libsolidity/ASTJSON/used_errors.sol +++ b/test/libsolidity/ASTJSON/used_errors.sol @@ -2,7 +2,7 @@ error X(); function f() { revert X(); } contract C { error T(); - function h() public pure { f(); } + function h() public { f(); } } // ---- diff --git a/test/libsolidity/ASTJSON/used_errors_parseOnly.json b/test/libsolidity/ASTJSON/used_errors_parseOnly.json index 162f441c4..58b476005 100644 --- a/test/libsolidity/ASTJSON/used_errors_parseOnly.json +++ b/test/libsolidity/ASTJSON/used_errors_parseOnly.json @@ -108,7 +108,7 @@ { "id": 17, "nodeType": "Block", - "src": "97:8:1", + "src": "92:8:1", "statements": [ { @@ -121,19 +121,19 @@ "name": "f", "nodeType": "Identifier", "overloadedDeclarations": [], - "src": "99:1:1", + "src": "94:1:1", "typeDescriptions": {} }, "id": 15, "names": [], "nodeType": "FunctionCall", - "src": "99:3:1", + "src": "94:3:1", "tryCall": false, "typeDescriptions": {} }, "id": 16, "nodeType": "ExpressionStatement", - "src": "99:3:1" + "src": "94:3:1" } ] }, @@ -156,17 +156,17 @@ "id": 13, "nodeType": "ParameterList", "parameters": [], - "src": "97:0:1" + "src": "92:0:1" }, - "src": "72:33:1", - "stateMutability": "pure", + "src": "72:28:1", + "stateMutability": "nonpayable", "virtual": false, "visibility": "public" } ], - "src": "40:67:1", + "src": "40:62:1", "usedErrors": [] } ], - "src": "0:108:1" + "src": "0:103:1" }