2015-10-26 14:13:36 +00:00
|
|
|
/*
|
2019-02-13 15:56:46 +00:00
|
|
|
This file is part of solidity.
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2019-02-13 15:56:46 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-10-26 14:13:36 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Parses and analyses the doc strings.
|
|
|
|
* Stores the parsing results in the AST annotations and reports errors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/analysis/DocStringAnalyser.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
|
2015-10-26 14:13:36 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/parsing/DocStringParser.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2015-10-26 14:13:36 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
2015-10-26 14:13:36 +00:00
|
|
|
|
|
|
|
bool DocStringAnalyser::analyseDocStrings(SourceUnit const& _sourceUnit)
|
|
|
|
{
|
|
|
|
m_errorOccured = false;
|
|
|
|
_sourceUnit.accept(*this);
|
|
|
|
|
|
|
|
return !m_errorOccured;
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:24:02 +00:00
|
|
|
bool DocStringAnalyser::visit(ContractDefinition const& _contract)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2019-02-14 10:53:00 +00:00
|
|
|
static set<string> const validTags = set<string>{"author", "title", "dev", "notice"};
|
2017-08-22 17:24:02 +00:00
|
|
|
parseDocStrings(_contract, _contract.annotation(), validTags, "contracts");
|
2015-10-26 14:13:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:24:02 +00:00
|
|
|
bool DocStringAnalyser::visit(FunctionDefinition const& _function)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2018-03-06 18:26:49 +00:00
|
|
|
if (_function.isConstructor())
|
|
|
|
handleConstructor(_function, _function, _function.annotation());
|
|
|
|
else
|
|
|
|
handleCallable(_function, _function, _function.annotation());
|
2015-10-26 14:13:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:24:02 +00:00
|
|
|
bool DocStringAnalyser::visit(ModifierDefinition const& _modifier)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2017-08-22 17:24:02 +00:00
|
|
|
handleCallable(_modifier, _modifier, _modifier.annotation());
|
2015-10-26 14:13:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:24:02 +00:00
|
|
|
bool DocStringAnalyser::visit(EventDefinition const& _event)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2017-08-22 17:24:02 +00:00
|
|
|
handleCallable(_event, _event, _event.annotation());
|
2015-10-26 14:13:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:26:49 +00:00
|
|
|
void DocStringAnalyser::checkParameters(
|
2015-10-26 16:21:32 +00:00
|
|
|
CallableDeclaration const& _callable,
|
2020-01-23 11:20:44 +00:00
|
|
|
StructurallyDocumented const& _node,
|
2020-01-29 22:13:42 +00:00
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
2015-10-26 14:13:36 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
set<string> validParams;
|
|
|
|
for (auto const& p: _callable.parameters())
|
|
|
|
validParams.insert(p->name());
|
|
|
|
if (_callable.returnParameterList())
|
|
|
|
for (auto const& p: _callable.returnParameterList()->parameters())
|
|
|
|
validParams.insert(p->name());
|
|
|
|
auto paramRange = _annotation.docTags.equal_range("param");
|
|
|
|
for (auto i = paramRange.first; i != paramRange.second; ++i)
|
|
|
|
if (!validParams.count(i->second.paramName))
|
|
|
|
appendError(
|
2020-01-23 11:20:44 +00:00
|
|
|
_node.documentation()->location(),
|
2015-10-26 14:13:36 +00:00
|
|
|
"Documented parameter \"" +
|
|
|
|
i->second.paramName +
|
|
|
|
"\" not found in the parameter list of the function."
|
|
|
|
);
|
2018-03-06 18:26:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringAnalyser::handleConstructor(
|
|
|
|
CallableDeclaration const& _callable,
|
2020-01-29 22:13:42 +00:00
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
2018-03-06 18:26:49 +00:00
|
|
|
)
|
|
|
|
{
|
2019-02-14 10:53:00 +00:00
|
|
|
static set<string> const validTags = set<string>{"author", "dev", "notice", "param"};
|
2018-03-06 18:26:49 +00:00
|
|
|
parseDocStrings(_node, _annotation, validTags, "constructor");
|
2020-01-23 11:20:44 +00:00
|
|
|
checkParameters(_callable, _node, _annotation);
|
2018-03-06 18:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringAnalyser::handleCallable(
|
|
|
|
CallableDeclaration const& _callable,
|
2020-01-29 22:13:42 +00:00
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
2018-03-06 18:26:49 +00:00
|
|
|
)
|
|
|
|
{
|
2019-02-14 10:53:00 +00:00
|
|
|
static set<string> const validTags = set<string>{"author", "dev", "notice", "return", "param"};
|
2018-03-06 18:26:49 +00:00
|
|
|
parseDocStrings(_node, _annotation, validTags, "functions");
|
2020-01-23 11:20:44 +00:00
|
|
|
checkParameters(_callable, _node, _annotation);
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 16:20:29 +00:00
|
|
|
void DocStringAnalyser::parseDocStrings(
|
2020-01-29 22:13:42 +00:00
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation,
|
2015-10-26 16:20:29 +00:00
|
|
|
set<string> const& _validTags,
|
|
|
|
string const& _nodeName
|
|
|
|
)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
|
|
|
DocStringParser parser;
|
2020-01-29 22:13:42 +00:00
|
|
|
if (_node.documentation() && !_node.documentation()->text()->empty())
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2020-01-29 22:13:42 +00:00
|
|
|
if (!parser.parse(*_node.documentation()->text(), m_errorReporter))
|
2015-10-26 14:13:36 +00:00
|
|
|
m_errorOccured = true;
|
|
|
|
_annotation.docTags = parser.tags();
|
|
|
|
}
|
2019-11-28 14:43:55 +00:00
|
|
|
|
|
|
|
size_t returnTagsVisited = 0;
|
2015-10-26 16:20:29 +00:00
|
|
|
for (auto const& docTag: _annotation.docTags)
|
2019-11-28 14:43:55 +00:00
|
|
|
{
|
2015-10-26 16:20:29 +00:00
|
|
|
if (!_validTags.count(docTag.first))
|
2020-01-23 11:20:44 +00:00
|
|
|
appendError(
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Documentation tag @" + docTag.first + " not valid for " + _nodeName + "."
|
|
|
|
);
|
2019-11-28 14:43:55 +00:00
|
|
|
else
|
|
|
|
if (docTag.first == "return")
|
|
|
|
{
|
|
|
|
returnTagsVisited++;
|
|
|
|
if (auto* function = dynamic_cast<FunctionDefinition const*>(&_node))
|
|
|
|
{
|
|
|
|
string content = docTag.second.content;
|
|
|
|
string firstWord = content.substr(0, content.find_first_of(" \t"));
|
|
|
|
|
|
|
|
if (returnTagsVisited > function->returnParameters().size())
|
2020-01-23 11:20:44 +00:00
|
|
|
appendError(
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Documentation tag \"@" + docTag.first + " " + docTag.second.content + "\"" +
|
2020-03-22 13:15:44 +00:00
|
|
|
" exceeds the number of return parameters."
|
2019-11-28 14:43:55 +00:00
|
|
|
);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto parameter = function->returnParameters().at(returnTagsVisited - 1);
|
|
|
|
if (!parameter->name().empty() && parameter->name() != firstWord)
|
2020-01-23 11:20:44 +00:00
|
|
|
appendError(
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Documentation tag \"@" + docTag.first + " " + docTag.second.content + "\"" +
|
2019-11-28 14:43:55 +00:00
|
|
|
" does not contain the name of its return parameter."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 11:20:44 +00:00
|
|
|
void DocStringAnalyser::appendError(SourceLocation const& _location, string const& _description)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
|
|
|
m_errorOccured = true;
|
2020-05-05 22:38:28 +00:00
|
|
|
m_errorReporter.docstringParsingError(7816_error, _location, _description);
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|