Skip to content

Commit 5864cf4

Browse files
committed
Add support for enum values in NatSpec
1 parent a04f946 commit 5864cf4

File tree

10 files changed

+140
-9
lines changed

10 files changed

+140
-9
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Language Features:
44

55

66
Compiler Features:
7+
* NatSpec: Capture Natspec documentation of `enum` values in the AST.
78

89

910
Bugfixes:

libsolidity/ast/AST.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,11 +823,19 @@ class EnumDefinition: public Declaration, public StructurallyDocumented, public
823823
/**
824824
* Declaration of an Enum Value
825825
*/
826-
class EnumValue: public Declaration
826+
class EnumValue: public Declaration, public StructurallyDocumented
827827
{
828828
public:
829-
EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name):
830-
Declaration(_id, _location, _name, _location) {}
829+
EnumValue(
830+
int64_t _id,
831+
SourceLocation const& _location,
832+
ASTPointer<ASTString> const& _name,
833+
ASTPointer<StructuredDocumentation> _documentation
834+
):
835+
Declaration(_id, _location, _name, _location),
836+
StructurallyDocumented(std::move(_documentation))
837+
{
838+
}
831839

832840
void accept(ASTVisitor& _visitor) override;
833841
void accept(ASTConstVisitor& _visitor) const override;

libsolidity/ast/ASTJsonExporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ bool ASTJsonExporter::visit(EnumValue const& _node)
430430
setJsonNode(_node, "EnumValue", {
431431
std::make_pair("name", _node.name()),
432432
std::make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
433+
std::make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json()),
433434
});
434435
return false;
435436
}

libsolidity/ast/ASTJsonImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ ASTPointer<EnumValue> ASTJsonImporter::createEnumValue(Json const& _node)
489489
{
490490
return createASTNode<EnumValue>(
491491
_node,
492-
memberAsASTString(_node, "name")
492+
memberAsASTString(_node, "name"),
493+
_node.contains("documentation") && !_node["documentation"].is_null() ? createDocumentation(member(_node, "documentation")) : nullptr
493494
);
494495
}
495496

libsolidity/parsing/Parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,9 @@ ASTPointer<EnumValue> Parser::parseEnumValue()
824824
{
825825
RecursionGuard recursionGuard(*this);
826826
ASTNodeFactory nodeFactory(*this);
827+
ASTPointer<StructuredDocumentation> documentation = parseStructuredDocumentation();
827828
nodeFactory.markEndPosition();
828-
return nodeFactory.createNode<EnumValue>(expectIdentifierToken());
829+
return nodeFactory.createNode<EnumValue>(expectIdentifierToken(), documentation);
829830
}
830831

831832
ASTPointer<EnumDefinition> Parser::parseEnumDefinition()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"absolutePath": "a",
3+
"exportedSymbols": {
4+
"Color": [
5+
6
6+
]
7+
},
8+
"id": 7,
9+
"nodeType": "SourceUnit",
10+
"nodes": [
11+
{
12+
"canonicalName": "Color",
13+
"id": 6,
14+
"members": [
15+
{
16+
"id": 1,
17+
"name": "Red",
18+
"nameLocation": "17:3:1",
19+
"nodeType": "EnumValue",
20+
"src": "17:3:1"
21+
},
22+
{
23+
"documentation": {
24+
"id": 2,
25+
"nodeType": "StructuredDocumentation",
26+
"src": "26:57:1",
27+
"text": "@notice example of notice\n @dev example of dev"
28+
},
29+
"id": 3,
30+
"name": "Green",
31+
"nameLocation": "88:5:1",
32+
"nodeType": "EnumValue",
33+
"src": "88:5:1"
34+
},
35+
{
36+
"documentation": {
37+
"id": 4,
38+
"nodeType": "StructuredDocumentation",
39+
"src": "99:23:1",
40+
"text": "@dev example of dev"
41+
},
42+
"id": 5,
43+
"name": "Blue",
44+
"nameLocation": "127:4:1",
45+
"nodeType": "EnumValue",
46+
"src": "127:4:1"
47+
}
48+
],
49+
"name": "Color",
50+
"nameLocation": "5:5:1",
51+
"nodeType": "EnumDefinition",
52+
"src": "0:164:1"
53+
}
54+
],
55+
"src": "0:165:1"
56+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
/// @dev beyond last value
9+
}
10+
11+
// ----
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"absolutePath": "a",
3+
"id": 7,
4+
"nodeType": "SourceUnit",
5+
"nodes": [
6+
{
7+
"id": 6,
8+
"members": [
9+
{
10+
"id": 1,
11+
"name": "Red",
12+
"nameLocation": "17:3:1",
13+
"nodeType": "EnumValue",
14+
"src": "17:3:1"
15+
},
16+
{
17+
"documentation": {
18+
"id": 2,
19+
"nodeType": "StructuredDocumentation",
20+
"src": "26:57:1",
21+
"text": "@notice example of notice\n @dev example of dev"
22+
},
23+
"id": 3,
24+
"name": "Green",
25+
"nameLocation": "88:5:1",
26+
"nodeType": "EnumValue",
27+
"src": "88:5:1"
28+
},
29+
{
30+
"documentation": {
31+
"id": 4,
32+
"nodeType": "StructuredDocumentation",
33+
"src": "99:23:1",
34+
"text": "@dev example of dev"
35+
},
36+
"id": 5,
37+
"name": "Blue",
38+
"nameLocation": "127:4:1",
39+
"nodeType": "EnumValue",
40+
"src": "127:4:1"
41+
}
42+
],
43+
"name": "Color",
44+
"nameLocation": "5:5:1",
45+
"nodeType": "EnumDefinition",
46+
"src": "0:164:1"
47+
}
48+
],
49+
"src": "0:165:1"
50+
}

test/libsolidity/natspecJSON/docstring_enum.sol renamed to test/libsolidity/natspecJSON/enum_value.sol

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
contract C {
2-
/// @title example of title
3-
/// @author example of author
4-
/// @notice example of notice
5-
/// @dev example of dev
62
enum Color {
3+
/// @custom red color
74
Red,
5+
/// @title example of title
6+
/// @author example of author
7+
/// @notice example of notice
8+
/// @dev example of dev
89
Green
10+
/// @notice beyond last value
911
}
1012
}
1113
// ----

0 commit comments

Comments
 (0)