2020-06-29 12:30:09 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @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/DocStringTagParser.h>
|
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/parsing/DocStringParser.h>
|
|
|
|
#include <libsolidity/analysis/NameAndTypeResolver.h>
|
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2021-03-01 16:13:28 +00:00
|
|
|
#include <liblangutil/Common.h>
|
|
|
|
|
|
|
|
#include <range/v3/algorithm/any_of.hpp>
|
2022-02-03 10:49:16 +00:00
|
|
|
#include <range/v3/view/filter.hpp>
|
2020-06-29 12:30:09 +00:00
|
|
|
|
2021-02-25 12:57:34 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2021-03-01 16:13:28 +00:00
|
|
|
#include <regex>
|
2021-08-14 18:29:59 +00:00
|
|
|
#include <string_view>
|
2021-03-01 16:13:28 +00:00
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
|
|
|
|
bool DocStringTagParser::parseDocStrings(SourceUnit const& _sourceUnit)
|
|
|
|
{
|
|
|
|
auto errorWatcher = m_errorReporter.errorWatcher();
|
|
|
|
_sourceUnit.accept(*this);
|
|
|
|
return errorWatcher.ok();
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:37:49 +00:00
|
|
|
bool DocStringTagParser::validateDocStringsUsingTypes(SourceUnit const& _sourceUnit)
|
|
|
|
{
|
2021-05-26 15:37:49 +00:00
|
|
|
ErrorReporter::ErrorWatcher errorWatcher = m_errorReporter.errorWatcher();
|
2021-05-26 15:37:49 +00:00
|
|
|
|
|
|
|
SimpleASTVisitor visitReturns(
|
|
|
|
[](ASTNode const&) { return true; },
|
|
|
|
[&](ASTNode const& _node)
|
|
|
|
{
|
|
|
|
if (auto const* annotation = dynamic_cast<StructurallyDocumentedAnnotation const*>(&_node.annotation()))
|
|
|
|
{
|
|
|
|
auto const& documentationNode = dynamic_cast<StructurallyDocumented const&>(_node);
|
|
|
|
|
|
|
|
size_t returnTagsVisited = 0;
|
|
|
|
|
|
|
|
for (auto const& [tagName, tagValue]: annotation->docTags)
|
|
|
|
if (tagName == "return")
|
|
|
|
{
|
|
|
|
returnTagsVisited++;
|
2021-05-26 15:37:49 +00:00
|
|
|
vector<string> returnParameterNames;
|
|
|
|
|
2021-05-26 15:37:49 +00:00
|
|
|
if (auto const* varDecl = dynamic_cast<VariableDeclaration const*>(&_node))
|
|
|
|
{
|
2021-05-26 15:37:49 +00:00
|
|
|
if (!varDecl->isPublic())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// FunctionType() requires the DeclarationTypeChecker to have run.
|
|
|
|
returnParameterNames = FunctionType(*varDecl).returnParameterNames();
|
2021-05-26 15:37:49 +00:00
|
|
|
}
|
|
|
|
else if (auto const* function = dynamic_cast<FunctionDefinition const*>(&_node))
|
2021-05-26 15:37:49 +00:00
|
|
|
returnParameterNames = FunctionType(*function).returnParameterNames();
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
string content = tagValue.content;
|
|
|
|
string firstWord = content.substr(0, content.find_first_of(" \t"));
|
|
|
|
|
|
|
|
if (returnTagsVisited > returnParameterNames.size())
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
2604_error,
|
|
|
|
documentationNode.documentation()->location(),
|
|
|
|
"Documentation tag \"@" + tagName + " " + content + "\"" +
|
|
|
|
" exceeds the number of return parameters."
|
|
|
|
);
|
|
|
|
else
|
2021-05-26 15:37:49 +00:00
|
|
|
{
|
2021-05-26 15:37:49 +00:00
|
|
|
string const& parameter = returnParameterNames.at(returnTagsVisited - 1);
|
|
|
|
if (!parameter.empty() && parameter != firstWord)
|
2021-05-26 15:37:49 +00:00
|
|
|
m_errorReporter.docstringParsingError(
|
2021-05-26 15:37:49 +00:00
|
|
|
5856_error,
|
2021-05-26 15:37:49 +00:00
|
|
|
documentationNode.documentation()->location(),
|
2021-05-26 15:37:49 +00:00
|
|
|
"Documentation tag \"@" + tagName + " " + content + "\"" +
|
|
|
|
" does not contain the name of its return parameter."
|
2021-05-26 15:37:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_sourceUnit.accept(visitReturns);
|
|
|
|
return errorWatcher.ok();
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
bool DocStringTagParser::visit(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
static set<string> const validTags = set<string>{"author", "title", "dev", "notice"};
|
|
|
|
parseDocStrings(_contract, _contract.annotation(), validTags, "contracts");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocStringTagParser::visit(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
if (_function.isConstructor())
|
|
|
|
handleConstructor(_function, _function, _function.annotation());
|
|
|
|
else
|
|
|
|
handleCallable(_function, _function, _function.annotation());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocStringTagParser::visit(VariableDeclaration const& _variable)
|
|
|
|
{
|
|
|
|
if (_variable.isStateVariable())
|
|
|
|
{
|
|
|
|
if (_variable.isPublic())
|
2020-09-25 10:15:31 +00:00
|
|
|
parseDocStrings(_variable, _variable.annotation(), {"dev", "notice", "return", "inheritdoc"}, "public state variables");
|
2020-06-29 12:30:09 +00:00
|
|
|
else
|
2021-04-01 12:28:19 +00:00
|
|
|
parseDocStrings(_variable, _variable.annotation(), {"dev", "notice", "inheritdoc"}, "non-public state variables");
|
2020-06-29 12:30:09 +00:00
|
|
|
}
|
2020-09-25 10:15:31 +00:00
|
|
|
else if (_variable.isFileLevelVariable())
|
|
|
|
parseDocStrings(_variable, _variable.annotation(), {"dev"}, "file-level variables");
|
2020-06-29 12:30:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocStringTagParser::visit(ModifierDefinition const& _modifier)
|
|
|
|
{
|
|
|
|
handleCallable(_modifier, _modifier, _modifier.annotation());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocStringTagParser::visit(EventDefinition const& _event)
|
|
|
|
{
|
|
|
|
handleCallable(_event, _event, _event.annotation());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:56:22 +00:00
|
|
|
bool DocStringTagParser::visit(ErrorDefinition const& _error)
|
|
|
|
{
|
|
|
|
handleCallable(_error, _error, _error.annotation());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:49:16 +00:00
|
|
|
bool DocStringTagParser::visit(InlineAssembly const& _assembly)
|
|
|
|
{
|
|
|
|
if (!_assembly.documentation())
|
|
|
|
return true;
|
|
|
|
StructuredDocumentation documentation{-1, _assembly.location(), _assembly.documentation()};
|
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter errorReporter{errors};
|
|
|
|
auto docTags = DocStringParser{documentation, errorReporter}.parse();
|
|
|
|
|
|
|
|
if (!errors.empty())
|
|
|
|
{
|
|
|
|
SecondarySourceLocation ssl;
|
|
|
|
for (auto const& error: errors)
|
|
|
|
if (error->comment())
|
|
|
|
ssl.append(
|
|
|
|
*error->comment(),
|
|
|
|
_assembly.location()
|
|
|
|
);
|
|
|
|
m_errorReporter.warning(
|
|
|
|
7828_error,
|
|
|
|
_assembly.location(),
|
|
|
|
"Inline assembly has invalid NatSpec documentation.",
|
|
|
|
ssl
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& [tagName, tagValue]: docTags)
|
|
|
|
{
|
|
|
|
if (tagName == "solidity")
|
|
|
|
{
|
|
|
|
vector<string> values;
|
|
|
|
boost::split(values, tagValue.content, isWhiteSpace);
|
|
|
|
|
|
|
|
set<string> valuesSeen;
|
|
|
|
set<string> duplicates;
|
|
|
|
for (auto const& value: values | ranges::views::filter(not_fn(&string::empty)))
|
|
|
|
if (valuesSeen.insert(value).second)
|
|
|
|
{
|
|
|
|
if (value == "memory-safe-assembly")
|
2022-02-14 12:21:15 +00:00
|
|
|
{
|
|
|
|
if (_assembly.annotation().markedMemorySafe)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
8544_error,
|
|
|
|
_assembly.location(),
|
|
|
|
"Inline assembly marked as memory safe using both a NatSpec tag and an assembly flag. "
|
|
|
|
"If you are not concerned with backwards compatibility, only use the assembly flag, "
|
|
|
|
"otherwise only use the NatSpec tag."
|
|
|
|
);
|
2022-02-10 16:57:27 +00:00
|
|
|
_assembly.annotation().markedMemorySafe = true;
|
2022-02-14 12:21:15 +00:00
|
|
|
}
|
2022-02-03 10:49:16 +00:00
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
8787_error,
|
|
|
|
_assembly.location(),
|
|
|
|
"Unexpected value for @solidity tag in inline assembly: " + value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (duplicates.insert(value).second)
|
|
|
|
m_errorReporter.warning(
|
|
|
|
4377_error,
|
|
|
|
_assembly.location(),
|
|
|
|
"Value for @solidity tag in inline assembly specified multiple times: " + value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_errorReporter.warning(
|
|
|
|
6269_error,
|
|
|
|
_assembly.location(),
|
|
|
|
"Unexpected NatSpec tag \"" + tagName + "\" with value \"" + tagValue.content + "\" in inline assembly."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
void DocStringTagParser::checkParameters(
|
|
|
|
CallableDeclaration const& _callable,
|
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
3881_error,
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Documented parameter \"" +
|
|
|
|
i->second.paramName +
|
|
|
|
"\" not found in the parameter list of the function."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringTagParser::handleConstructor(
|
|
|
|
CallableDeclaration const& _callable,
|
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
static set<string> const validTags = set<string>{"author", "dev", "notice", "param"};
|
|
|
|
parseDocStrings(_node, _annotation, validTags, "constructor");
|
|
|
|
checkParameters(_callable, _node, _annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringTagParser::handleCallable(
|
|
|
|
CallableDeclaration const& _callable,
|
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
|
|
|
)
|
|
|
|
{
|
2020-07-21 09:39:25 +00:00
|
|
|
static set<string> const validEventTags = set<string>{"dev", "notice", "return", "param"};
|
2021-01-28 11:56:22 +00:00
|
|
|
static set<string> const validErrorTags = set<string>{"dev", "notice", "param"};
|
2020-12-01 11:45:54 +00:00
|
|
|
static set<string> const validModifierTags = set<string>{"dev", "notice", "param", "inheritdoc"};
|
2020-07-21 09:39:25 +00:00
|
|
|
static set<string> const validTags = set<string>{"dev", "notice", "return", "param", "inheritdoc"};
|
2020-06-29 12:30:09 +00:00
|
|
|
|
|
|
|
if (dynamic_cast<EventDefinition const*>(&_callable))
|
|
|
|
parseDocStrings(_node, _annotation, validEventTags, "events");
|
2021-01-28 11:56:22 +00:00
|
|
|
else if (dynamic_cast<ErrorDefinition const*>(&_callable))
|
|
|
|
parseDocStrings(_node, _annotation, validErrorTags, "errors");
|
2020-12-01 11:45:54 +00:00
|
|
|
else if (dynamic_cast<ModifierDefinition const*>(&_callable))
|
|
|
|
parseDocStrings(_node, _annotation, validModifierTags, "modifiers");
|
2020-06-29 12:30:09 +00:00
|
|
|
else
|
|
|
|
parseDocStrings(_node, _annotation, validTags, "functions");
|
|
|
|
|
|
|
|
checkParameters(_callable, _node, _annotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocStringTagParser::parseDocStrings(
|
|
|
|
StructurallyDocumented const& _node,
|
|
|
|
StructurallyDocumentedAnnotation& _annotation,
|
|
|
|
set<string> const& _validTags,
|
|
|
|
string const& _nodeName
|
|
|
|
)
|
|
|
|
{
|
2021-03-01 15:54:21 +00:00
|
|
|
if (!_node.documentation())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_annotation.docTags = DocStringParser{*_node.documentation(), m_errorReporter}.parse();
|
2020-06-29 12:30:09 +00:00
|
|
|
|
2021-02-25 12:57:34 +00:00
|
|
|
for (auto const& [tagName, tagValue]: _annotation.docTags)
|
2020-06-29 12:30:09 +00:00
|
|
|
{
|
2021-08-14 18:29:59 +00:00
|
|
|
string_view static constexpr customPrefix("custom:");
|
2021-03-25 12:43:03 +00:00
|
|
|
if (tagName == "custom" || tagName == "custom:")
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
6564_error,
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Custom documentation tag must contain a chosen name, i.e. @custom:mytag."
|
|
|
|
);
|
|
|
|
else if (boost::starts_with(tagName, customPrefix) && tagName.size() > customPrefix.size())
|
2021-03-01 16:13:28 +00:00
|
|
|
{
|
|
|
|
regex static const customRegex("^custom:[a-z][a-z-]*$");
|
|
|
|
if (!regex_match(tagName, customRegex))
|
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
2968_error,
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Invalid character in custom tag @" + tagName + ". Only lowercase letters and \"-\" are permitted."
|
|
|
|
);
|
2021-02-25 12:57:34 +00:00
|
|
|
continue;
|
2021-03-01 16:13:28 +00:00
|
|
|
}
|
2021-02-25 12:57:34 +00:00
|
|
|
else if (!_validTags.count(tagName))
|
2020-06-29 12:30:09 +00:00
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
6546_error,
|
|
|
|
_node.documentation()->location(),
|
2021-02-25 12:57:34 +00:00
|
|
|
"Documentation tag @" + tagName + " not valid for " + _nodeName + "."
|
2020-06-29 12:30:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 15:37:49 +00:00
|
|
|
|