diff --git a/slither/core/declarations/function.py b/slither/core/declarations/function.py index e803154d0..bd1eda770 100644 --- a/slither/core/declarations/function.py +++ b/slither/core/declarations/function.py @@ -227,6 +227,7 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu # To be improved with a parsing of the documentation self.has_documentation: bool = False + self._comments: Optional[str] = None ################################################################################### ################################################################################### @@ -363,6 +364,20 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu def id(self, new_id: str): self._id = new_id + @property + def comments(self) -> Optional[str]: + """ + Return the comments associated with the function. + + Returns: + the comment as a string + """ + return self._comments + + @comments.setter + def comments(self, comments: str): + self._comments = comments + @property @abstractmethod def file_scope(self) -> "FileScope": diff --git a/slither/solc_parsing/declarations/function.py b/slither/solc_parsing/declarations/function.py index 59940ec1c..a38ff4405 100644 --- a/slither/solc_parsing/declarations/function.py +++ b/slither/solc_parsing/declarations/function.py @@ -94,6 +94,23 @@ class FunctionSolc(CallerContextExpression): if "documentation" in function_data: function.has_documentation = True + # Old solc versions store the comment in attributes["documentation"] + # More recent ones store it in attributes["documentation"]["text"] + if ( + "documentation" in function_data + and function_data["documentation"] is not None + and ( + "text" in function_data["documentation"] + or isinstance(function_data["documentation"], str) + ) + ): + text = ( + function_data["documentation"] + if isinstance(function_data["documentation"], str) + else function_data["documentation"]["text"] + ) + self._function.comments = text + @property def underlying_function(self) -> Function: return self._function