Skip to content

Commit df5e28e

Browse files
committed
Adds NatSpec field to AST node for enum value definitions
1 parent 9eaa5ce commit df5e28e

10 files changed

+153
-5
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Compiler Features:
2323
* EVM: Set default EVM version to "Shanghai".
2424
* EVM: Support for the EVM Version "Shanghai".
2525
* NatSpec: Add support for NatSpec documentation in ``enum`` definitions.
26+
* NatSpec: Add support for NatSpec documentation in ``enum`` value definitions.
2627
* NatSpec: Add support for NatSpec documentation in ``struct`` definitions.
2728
* NatSpec: Include NatSpec from events that are emitted by a contract but defined outside of it in userdoc and devdoc output.
2829
* 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
@@ -787,11 +787,11 @@ class EnumDefinition: public Declaration, public StructurallyDocumented, public
787787
/**
788788
* Declaration of an Enum Value
789789
*/
790-
class EnumValue: public Declaration
790+
class EnumValue: public Declaration, public StructurallyDocumented
791791
{
792792
public:
793-
EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name):
794-
Declaration(_id, _location, _name, _location) {}
793+
EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name, ASTPointer<StructuredDocumentation> _documentation):
794+
Declaration(_id, _location, _name, _location), StructurallyDocumented(std::move(_documentation)) {}
795795

796796
void accept(ASTVisitor& _visitor) override;
797797
void accept(ASTConstVisitor& _visitor) const override;

libsolidity/ast/ASTJsonExporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ bool ASTJsonExporter::visit(EnumValue const& _node)
414414
setJsonNode(_node, "EnumValue", {
415415
make_pair("name", _node.name()),
416416
make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
417+
make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json::nullValue)
417418
});
418419
return false;
419420
}

libsolidity/ast/ASTJsonImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ ASTPointer<EnumValue> ASTJsonImporter::createEnumValue(Json::Value const& _node)
476476
{
477477
return createASTNode<EnumValue>(
478478
_node,
479-
memberAsASTString(_node, "name")
479+
memberAsASTString(_node, "name"),
480+
_node["documentation"].isNull() ? nullptr : createDocumentation(member(_node, "documentation"))
480481
);
481482
}
482483

libsolidity/parsing/Parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,9 @@ ASTPointer<EnumValue> Parser::parseEnumValue()
715715
{
716716
RecursionGuard recursionGuard(*this);
717717
ASTNodeFactory nodeFactory(*this);
718+
ASTPointer<StructuredDocumentation> documentation = parseStructuredDocumentation();
718719
nodeFactory.markEndPosition();
719-
return nodeFactory.createNode<EnumValue>(expectIdentifierToken());
720+
return nodeFactory.createNode<EnumValue>(expectIdentifierToken(), documentation);
720721
}
721722

722723
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+
/// @notice example of notice
4+
/// @dev example of dev
5+
Green,
6+
/// @dev example of dev
7+
Blue
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+
/// @notice example of notice
5+
/// @dev example of dev
6+
Green,
7+
/// @dev example of dev
8+
Blue
9+
}
10+
}
11+
// ----

0 commit comments

Comments
 (0)