Skip to content

Commit d79badb

Browse files
committed
Added NatSpec support for enum value definitions in the AST
1 parent 34b13c1 commit d79badb

10 files changed

+153
-5
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Compiler Features:
77
* Assembler: Use ``push0`` for placing ``0`` in the stack for EVM versions starting from "Shanghai". This decreases the deployment costs.
88
* EVM: Support for the EVM Version "Shanghai".
99
* NatSpec: Add support for NatSpec documentation in ``enum`` definitions.
10+
* NatSpec: Add support for NatSpec documentation in ``enum`` value definitions.
1011
* NatSpec: Add support for NatSpec documentation in ``struct`` definitions.
1112
* NatSpec: Include NatSpec from events that are emitted by a contract but defined outside of it in userdoc and devdoc output.
1213
* Optimizer: Re-implement simplified version of UnusedAssignEliminator and UnusedStoreEliminator. It can correctly remove some unused assignments in deeply nested loops that were ignored by the old version.

libsolidity/ast/AST.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,11 @@ class EnumDefinition: public Declaration, public StructurallyDocumented, public
780780
/**
781781
* Declaration of an Enum Value
782782
*/
783-
class EnumValue: public Declaration
783+
class EnumValue: public Declaration, public StructurallyDocumented
784784
{
785785
public:
786-
EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name):
787-
Declaration(_id, _location, _name, _location) {}
786+
EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name, ASTPointer<StructuredDocumentation> _documentation):
787+
Declaration(_id, _location, _name, _location), StructurallyDocumented(std::move(_documentation)) {}
788788

789789
void accept(ASTVisitor& _visitor) override;
790790
void accept(ASTConstVisitor& _visitor) const override;

libsolidity/ast/ASTJsonExporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ bool ASTJsonExporter::visit(EnumValue const& _node)
411411
setJsonNode(_node, "EnumValue", {
412412
make_pair("name", _node.name()),
413413
make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
414+
make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json::nullValue)
414415
});
415416
return false;
416417
}

libsolidity/ast/ASTJsonImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ ASTPointer<EnumValue> ASTJsonImporter::createEnumValue(Json::Value const& _node)
472472
{
473473
return createASTNode<EnumValue>(
474474
_node,
475-
memberAsASTString(_node, "name")
475+
memberAsASTString(_node, "name"),
476+
_node["documentation"].isNull() ? nullptr : createDocumentation(member(_node, "documentation"))
476477
);
477478
}
478479

libsolidity/parsing/Parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,9 @@ ASTPointer<EnumValue> Parser::parseEnumValue()
701701
{
702702
RecursionGuard recursionGuard(*this);
703703
ASTNodeFactory nodeFactory(*this);
704+
ASTPointer<StructuredDocumentation> documentation = parseStructuredDocumentation();
704705
nodeFactory.markEndPosition();
705-
return nodeFactory.createNode<EnumValue>(expectIdentifierToken());
706+
return nodeFactory.createNode<EnumValue>(expectIdentifierToken(), documentation);
706707
}
707708

708709
ASTPointer<EnumDefinition> Parser::parseEnumDefinition()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"absolutePath": "a",
3+
"exportedSymbols":
4+
{
5+
"Color":
6+
[
7+
4
8+
]
9+
},
10+
"id": 5,
11+
"nodeType": "SourceUnit",
12+
"nodes":
13+
[
14+
{
15+
"canonicalName": "Color",
16+
"id": 4,
17+
"members":
18+
[
19+
{
20+
"id": 1,
21+
"name": "Red",
22+
"nameLocation": "17:3:1",
23+
"nodeType": "EnumValue",
24+
"src": "17:3:1"
25+
},
26+
{
27+
"documentation":
28+
{
29+
"id": 2,
30+
"nodeType": "StructuredDocumentation",
31+
"src": "22:111:1",
32+
"text": "@title example of title\n @author example of author\n @notice example of notice\n @dev example of dev"
33+
},
34+
"id": 3,
35+
"name": "Green",
36+
"nameLocation": "138:5:1",
37+
"nodeType": "EnumValue",
38+
"src": "138:5:1"
39+
}
40+
],
41+
"name": "Color",
42+
"nameLocation": "5:5:1",
43+
"nodeType": "EnumDefinition",
44+
"src": "0:145:1"
45+
}
46+
],
47+
"src": "0:146:1"
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
enum Color {
2+
Red,
3+
/// @title example of title
4+
/// @author example of author
5+
/// @notice example of notice
6+
/// @dev example of dev
7+
Green
8+
}
9+
10+
// ----
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"absolutePath": "a",
3+
"id": 5,
4+
"nodeType": "SourceUnit",
5+
"nodes":
6+
[
7+
{
8+
"id": 4,
9+
"members":
10+
[
11+
{
12+
"id": 1,
13+
"name": "Red",
14+
"nameLocation": "17:3:1",
15+
"nodeType": "EnumValue",
16+
"src": "17:3:1"
17+
},
18+
{
19+
"documentation":
20+
{
21+
"id": 2,
22+
"nodeType": "StructuredDocumentation",
23+
"src": "22:111:1",
24+
"text": "@title example of title\n @author example of author\n @notice example of notice\n @dev example of dev"
25+
},
26+
"id": 3,
27+
"name": "Green",
28+
"nameLocation": "138:5:1",
29+
"nodeType": "EnumValue",
30+
"src": "138:5:1"
31+
}
32+
],
33+
"name": "Color",
34+
"nameLocation": "5:5:1",
35+
"nodeType": "EnumDefinition",
36+
"src": "0:145:1"
37+
}
38+
],
39+
"src": "0:146:1"
40+
}

test/libsolidity/SolidityNatspecJSON.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,41 @@ BOOST_AUTO_TEST_CASE(enum_no_docs)
15481548
checkNatspec(sourceCode, "C", userDoc, true);
15491549
}
15501550

1551+
BOOST_AUTO_TEST_CASE(enum_value_no_docs)
1552+
{
1553+
char const* sourceCode = R"(
1554+
contract C {
1555+
enum Color {
1556+
Red,
1557+
/// @title example of title
1558+
/// @author example of author
1559+
/// @notice example of notice
1560+
/// @dev example of dev
1561+
Green
1562+
}
1563+
1564+
}
1565+
)";
1566+
1567+
char const* devDoc = R"ABCDEF(
1568+
{
1569+
"kind": "dev",
1570+
"methods": {},
1571+
"version": 1
1572+
})ABCDEF";
1573+
1574+
checkNatspec(sourceCode, "C", devDoc, false);
1575+
1576+
char const* userDoc = R"ABCDEF(
1577+
{
1578+
"kind": "user",
1579+
"methods": {},
1580+
"version": 1
1581+
})ABCDEF";
1582+
1583+
checkNatspec(sourceCode, "C", userDoc, true);
1584+
}
1585+
15511586
BOOST_AUTO_TEST_CASE(natspec_notice_without_tag)
15521587
{
15531588
char const* sourceCode = R"(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
contract C {
2+
enum Color {
3+
Red,
4+
/// @title example of title
5+
/// @author example of author
6+
/// @notice example of notice
7+
/// @dev example of dev
8+
Green
9+
}
10+
}
11+
// ----

0 commit comments

Comments
 (0)