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
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
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>
|
2022-01-17 16:09:14 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2018-12-17 11:30:08 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2015-10-26 14:13:36 +00:00
|
|
|
|
2021-02-25 12:57:34 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
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
|
|
|
|
2020-06-29 12:16:07 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-08-14 08:37:11 +00:00
|
|
|
void copyMissingTags(std::set<CallableDeclaration const*> const& _baseFunctions, StructurallyDocumentedAnnotation& _target, FunctionType const* _functionType = nullptr)
|
2020-06-29 12:16:07 +00:00
|
|
|
{
|
2020-10-08 09:07:31 +00:00
|
|
|
// Only copy if there is exactly one direct base function.
|
2020-06-29 12:16:07 +00:00
|
|
|
if (_baseFunctions.size() != 1)
|
|
|
|
return;
|
|
|
|
|
2020-12-01 11:45:54 +00:00
|
|
|
CallableDeclaration const& baseFunction = **_baseFunctions.begin();
|
|
|
|
|
|
|
|
auto& sourceDoc = dynamic_cast<StructurallyDocumentedAnnotation const&>(baseFunction.annotation());
|
2020-06-29 12:16:07 +00:00
|
|
|
|
2020-10-08 09:07:31 +00:00
|
|
|
for (auto it = sourceDoc.docTags.begin(); it != sourceDoc.docTags.end();)
|
|
|
|
{
|
2023-08-14 08:37:11 +00:00
|
|
|
std::string const& tag = it->first;
|
2021-02-25 12:57:34 +00:00
|
|
|
// Don't copy tag "inheritdoc", custom tags or already existing tags
|
|
|
|
if (tag == "inheritdoc" || _target.docTags.count(tag) || boost::starts_with(tag, "custom"))
|
2020-10-08 09:07:31 +00:00
|
|
|
{
|
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t n = 0;
|
|
|
|
// Iterate over all values of the current tag (it's a multimap)
|
|
|
|
for (auto next = sourceDoc.docTags.upper_bound(tag); it != next; it++, n++)
|
|
|
|
{
|
|
|
|
DocTag content = it->second;
|
|
|
|
|
|
|
|
// Update the parameter name for @return tags
|
2022-01-17 16:09:14 +00:00
|
|
|
if (_functionType && tag == "return")
|
2020-10-08 09:07:31 +00:00
|
|
|
{
|
|
|
|
size_t docParaNameEndPos = content.content.find_first_of(" \t");
|
2023-08-14 08:37:11 +00:00
|
|
|
std::string const docParameterName = content.content.substr(0, docParaNameEndPos);
|
2020-10-08 09:07:31 +00:00
|
|
|
|
2020-12-01 11:45:54 +00:00
|
|
|
if (
|
2022-01-17 16:09:14 +00:00
|
|
|
_functionType->returnParameterNames().size() > n &&
|
|
|
|
docParameterName != _functionType->returnParameterNames().at(n)
|
2020-12-01 11:45:54 +00:00
|
|
|
)
|
2020-10-08 09:07:31 +00:00
|
|
|
{
|
2020-12-01 11:45:54 +00:00
|
|
|
bool baseHasNoName =
|
2022-01-17 16:09:14 +00:00
|
|
|
baseFunction.returnParameterList() &&
|
|
|
|
baseFunction.returnParameters().size() > n &&
|
2020-12-01 11:45:54 +00:00
|
|
|
baseFunction.returnParameters().at(n)->name().empty();
|
|
|
|
|
2023-08-14 08:37:11 +00:00
|
|
|
std::string paramName = _functionType->returnParameterNames().at(n);
|
2020-10-08 09:07:31 +00:00
|
|
|
content.content =
|
|
|
|
(paramName.empty() ? "" : std::move(paramName) + " ") + (
|
2023-08-14 08:37:11 +00:00
|
|
|
std::string::npos == docParaNameEndPos || baseHasNoName ?
|
2020-10-08 09:07:31 +00:00
|
|
|
content.content :
|
|
|
|
content.content.substr(docParaNameEndPos + 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 12:16:07 +00:00
|
|
|
|
|
|
|
_target.docTags.emplace(tag, content);
|
2020-10-08 09:07:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 12:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 08:37:11 +00:00
|
|
|
CallableDeclaration const* findBaseCallable(std::set<CallableDeclaration const*> const& _baseFunctions, int64_t _contractId)
|
2020-06-29 12:30:09 +00:00
|
|
|
{
|
|
|
|
for (CallableDeclaration const* baseFuncCandidate: _baseFunctions)
|
|
|
|
if (baseFuncCandidate->annotation().contract->id() == _contractId)
|
|
|
|
return baseFuncCandidate;
|
|
|
|
else if (auto callable = findBaseCallable(baseFuncCandidate->annotation().baseFunctions, _contractId))
|
|
|
|
return callable;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:16:07 +00:00
|
|
|
bool parameterNamesEqual(CallableDeclaration const& _a, CallableDeclaration const& _b)
|
|
|
|
{
|
|
|
|
return boost::range::equal(_a.parameters(), _b.parameters(), [](auto const& pa, auto const& pb) { return pa->name() == pb->name(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:13:36 +00:00
|
|
|
bool DocStringAnalyser::analyseDocStrings(SourceUnit const& _sourceUnit)
|
|
|
|
{
|
2020-05-12 19:49:04 +00:00
|
|
|
auto errorWatcher = m_errorReporter.errorWatcher();
|
2015-10-26 14:13:36 +00:00
|
|
|
_sourceUnit.accept(*this);
|
2020-05-12 19:49:04 +00:00
|
|
|
return errorWatcher.ok();
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 17:24:02 +00:00
|
|
|
bool DocStringAnalyser::visit(FunctionDefinition const& _function)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2020-06-29 12:30:09 +00:00
|
|
|
if (!_function.isConstructor())
|
2022-01-17 16:09:14 +00:00
|
|
|
handleCallable(_function, _function, _function.annotation(), TypeProvider::function(_function));
|
2015-10-26 14:13:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:59:26 +00:00
|
|
|
bool DocStringAnalyser::visit(VariableDeclaration const& _variable)
|
|
|
|
{
|
2020-09-25 10:15:31 +00:00
|
|
|
if (!_variable.isStateVariable() && !_variable.isFileLevelVariable())
|
2020-06-29 12:30:09 +00:00
|
|
|
return false;
|
|
|
|
|
2022-01-17 16:09:14 +00:00
|
|
|
auto const* getterType = TypeProvider::function(_variable);
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
if (CallableDeclaration const* baseFunction = resolveInheritDoc(_variable.annotation().baseFunctions, _variable, _variable.annotation()))
|
2022-01-17 16:09:14 +00:00
|
|
|
copyMissingTags({baseFunction}, _variable.annotation(), getterType);
|
2020-06-29 12:30:09 +00:00
|
|
|
else if (_variable.annotation().docTags.empty())
|
2022-01-17 16:09:14 +00:00
|
|
|
copyMissingTags(_variable.annotation().baseFunctions, _variable.annotation(), getterType);
|
2020-06-29 12:16:07 +00:00
|
|
|
|
2020-03-24 22:44:39 +00:00
|
|
|
return false;
|
2020-03-05 20:59:26 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:56:22 +00:00
|
|
|
bool DocStringAnalyser::visit(ErrorDefinition const& _error)
|
|
|
|
{
|
|
|
|
handleCallable(_error, _error, _error.annotation());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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,
|
2022-01-17 16:09:14 +00:00
|
|
|
StructurallyDocumentedAnnotation& _annotation,
|
|
|
|
FunctionType const* _functionType
|
2018-03-06 18:26:49 +00:00
|
|
|
)
|
|
|
|
{
|
2020-06-29 12:30:09 +00:00
|
|
|
if (CallableDeclaration const* baseFunction = resolveInheritDoc(_callable.annotation().baseFunctions, _node, _annotation))
|
2022-01-17 16:09:14 +00:00
|
|
|
copyMissingTags({baseFunction}, _annotation, _functionType);
|
2020-06-29 12:30:09 +00:00
|
|
|
else if (
|
2020-06-29 12:16:07 +00:00
|
|
|
_annotation.docTags.empty() &&
|
|
|
|
_callable.annotation().baseFunctions.size() == 1 &&
|
|
|
|
parameterNamesEqual(_callable, **_callable.annotation().baseFunctions.begin())
|
|
|
|
)
|
2022-01-17 16:09:14 +00:00
|
|
|
copyMissingTags(_callable.annotation().baseFunctions, _annotation, _functionType);
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
CallableDeclaration const* DocStringAnalyser::resolveInheritDoc(
|
2023-08-14 08:37:11 +00:00
|
|
|
std::set<CallableDeclaration const*> const& _baseFuncs,
|
2020-01-29 22:13:42 +00:00
|
|
|
StructurallyDocumented const& _node,
|
2020-06-29 12:30:09 +00:00
|
|
|
StructurallyDocumentedAnnotation& _annotation
|
2015-10-26 16:20:29 +00:00
|
|
|
)
|
2015-10-26 14:13:36 +00:00
|
|
|
{
|
2020-06-29 12:30:09 +00:00
|
|
|
if (_annotation.inheritdocReference == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (auto const callable = findBaseCallable(_baseFuncs, _annotation.inheritdocReference->id()))
|
|
|
|
return callable;
|
2019-11-28 14:43:55 +00:00
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
m_errorReporter.docstringParsingError(
|
|
|
|
4682_error,
|
|
|
|
_node.documentation()->location(),
|
|
|
|
"Documentation tag @inheritdoc references contract \"" +
|
|
|
|
_annotation.inheritdocReference->name() +
|
|
|
|
"\", but the contract does not contain a function that is overridden by this function."
|
|
|
|
);
|
2019-11-28 14:43:55 +00:00
|
|
|
|
2020-06-29 12:30:09 +00:00
|
|
|
return nullptr;
|
2015-10-26 14:13:36 +00:00
|
|
|
}
|