Skip to content

Add support for enum values in NatSpec #15956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Language Features:


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


Bugfixes:
Expand Down
16 changes: 8 additions & 8 deletions docs/natspec-format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Documentation Example
=====================

Documentation is inserted above each ``contract``, ``interface``, ``library``,
``function``, and ``event`` using the Doxygen notation format.
``function``, ``enum``, ``enum`` value and ``event`` using the Doxygen notation format.
A ``public`` state variable is equivalent to a ``function``
for the purposes of NatSpec.

Expand Down Expand Up @@ -116,13 +116,13 @@ in the same way as if it were tagged with ``@notice``.
=============== ====================================================================================== =============================
Tag Context
=============== ====================================================================================== =============================
``@title`` A title that should describe the contract/interface contract, library, interface, struct, enum
``@author`` The name of the author contract, library, interface, struct, enum
``@notice`` Explain to an end user what this does contract, library, interface, function, public state variable, event, struct, enum, error
``@dev`` Explain to a developer any extra details contract, library, interface, function, state variable, event, struct, enum, error
``@param`` Documents a parameter just like in Doxygen (must be followed by parameter name) function, event, error
``@return`` Documents the return variables of a contract's function function, public state variable
``@inheritdoc`` Copies all missing tags from the base function (must be followed by the contract name) function, public state variable
``@title`` A title that should describe the contract/interface contract, library, interface, struct, enum, enum values
``@author`` The name of the author contract, library, interface, struct, enum, enum values
``@notice`` Explain to an end user what this does contract, library, interface, function, public state variable, event, struct, enum, enum values error
``@dev`` Explain to a developer any extra details contract, library, interface, function, state variable, event, struct, enum, enum values, error
``@param`` Documents a parameter just like in Doxygen (must be followed by parameter name) function, event, enum values, error
``@return`` Documents the return variables of a contract's function function, enum, enum values, public state variable
``@inheritdoc`` Copies all missing tags from the base function (must be followed by the contract name) function, enum, enum values, public state variable
``@custom:...`` Custom tag, semantics is application-defined everywhere
=============== ====================================================================================== =============================

Expand Down
14 changes: 11 additions & 3 deletions libsolidity/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,11 +823,19 @@ class EnumDefinition: public Declaration, public StructurallyDocumented, public
/**
* Declaration of an Enum Value
*/
class EnumValue: public Declaration
class EnumValue: public Declaration, public StructurallyDocumented
{
public:
EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name):
Declaration(_id, _location, _name, _location) {}
EnumValue(
int64_t _id,
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
ASTPointer<StructuredDocumentation> _documentation
):
Declaration(_id, _location, _name, _location),
StructurallyDocumented(std::move(_documentation))
{
}

void accept(ASTVisitor& _visitor) override;
void accept(ASTConstVisitor& _visitor) const override;
Expand Down
1 change: 1 addition & 0 deletions libsolidity/ast/ASTJsonExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ bool ASTJsonExporter::visit(EnumValue const& _node)
setJsonNode(_node, "EnumValue", {
std::make_pair("name", _node.name()),
std::make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
std::make_pair("documentation", toJson(_node.documentation().get())),
});
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion libsolidity/ast/ASTJsonImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ ASTPointer<EnumValue> ASTJsonImporter::createEnumValue(Json const& _node)
{
return createASTNode<EnumValue>(
_node,
memberAsASTString(_node, "name")
memberAsASTString(_node, "name"),
_node.contains("documentation") && !_node["documentation"].is_null() ? createDocumentation(member(_node, "documentation")) : nullptr
);
}

Expand Down
3 changes: 2 additions & 1 deletion libsolidity/parsing/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,9 @@ ASTPointer<EnumValue> Parser::parseEnumValue()
{
RecursionGuard recursionGuard(*this);
ASTNodeFactory nodeFactory(*this);
ASTPointer<StructuredDocumentation> documentation = parseStructuredDocumentation();
nodeFactory.markEndPosition();
return nodeFactory.createNode<EnumValue>(expectIdentifierToken());
return nodeFactory.createNode<EnumValue>(expectIdentifierToken(), documentation);
}

ASTPointer<EnumDefinition> Parser::parseEnumDefinition()
Expand Down
56 changes: 56 additions & 0 deletions test/libsolidity/ASTJSON/enum_value_natspec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"absolutePath": "a",
"exportedSymbols": {
"Color": [
6
]
},
"id": 7,
"nodeType": "SourceUnit",
"nodes": [
{
"canonicalName": "Color",
"id": 6,
"members": [
{
"id": 1,
"name": "Red",
"nameLocation": "17:3:1",
"nodeType": "EnumValue",
"src": "17:3:1"
},
{
"documentation": {
"id": 2,
"nodeType": "StructuredDocumentation",
"src": "26:57:1",
"text": "@notice example of notice\n @dev example of dev"
},
"id": 3,
"name": "Green",
"nameLocation": "88:5:1",
"nodeType": "EnumValue",
"src": "88:5:1"
},
{
"documentation": {
"id": 4,
"nodeType": "StructuredDocumentation",
"src": "99:23:1",
"text": "@dev example of dev"
},
"id": 5,
"name": "Blue",
"nameLocation": "127:4:1",
"nodeType": "EnumValue",
"src": "127:4:1"
}
],
"name": "Color",
"nameLocation": "5:5:1",
"nodeType": "EnumDefinition",
"src": "0:164:1"
}
],
"src": "0:165:1"
}
11 changes: 11 additions & 0 deletions test/libsolidity/ASTJSON/enum_value_natspec.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum Color {
Red,
/// @notice example of notice
/// @dev example of dev
Green,
/// @dev example of dev
Blue
/// @dev beyond last value
}

// ----
50 changes: 50 additions & 0 deletions test/libsolidity/ASTJSON/enum_value_natspec_parseOnly.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"absolutePath": "a",
"id": 7,
"nodeType": "SourceUnit",
"nodes": [
{
"id": 6,
"members": [
{
"id": 1,
"name": "Red",
"nameLocation": "17:3:1",
"nodeType": "EnumValue",
"src": "17:3:1"
},
{
"documentation": {
"id": 2,
"nodeType": "StructuredDocumentation",
"src": "26:57:1",
"text": "@notice example of notice\n @dev example of dev"
},
"id": 3,
"name": "Green",
"nameLocation": "88:5:1",
"nodeType": "EnumValue",
"src": "88:5:1"
},
{
"documentation": {
"id": 4,
"nodeType": "StructuredDocumentation",
"src": "99:23:1",
"text": "@dev example of dev"
},
"id": 5,
"name": "Blue",
"nameLocation": "127:4:1",
"nodeType": "EnumValue",
"src": "127:4:1"
}
],
"name": "Color",
"nameLocation": "5:5:1",
"nodeType": "EnumDefinition",
"src": "0:164:1"
}
],
"src": "0:165:1"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
contract C {
/// @title example of title
/// @author example of author
/// @notice example of notice
/// @dev example of dev
enum Color {
/// @custom red color
Red,
/// @title example of title
/// @author example of author
/// @notice example of notice
/// @dev example of dev
Green
/// @notice beyond last value
}
}
// ----
Expand Down