solidity/libsolidity/analysis/DocStringAnalyser.cpp

153 lines
4.7 KiB
C++
Raw Normal View History

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
*/
// 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>
#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
namespace
{
void copyMissingTags(StructurallyDocumentedAnnotation& _target, set<CallableDeclaration const*> const& _baseFunctions)
{
if (_baseFunctions.size() != 1)
return;
auto& sourceDoc = dynamic_cast<StructurallyDocumentedAnnotation const&>((*_baseFunctions.begin())->annotation());
set<string> existingTags;
for (auto const& iterator: _target.docTags)
existingTags.insert(iterator.first);
for (auto const& [tag, content]: sourceDoc.docTags)
if (tag != "inheritdoc" && !existingTags.count(tag))
_target.docTags.emplace(tag, content);
}
2020-06-29 12:30:09 +00:00
CallableDeclaration const* findBaseCallable(set<CallableDeclaration const*> const& _baseFunctions, int64_t _contractId)
{
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;
}
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)
{
auto errorWatcher = m_errorReporter.errorWatcher();
2015-10-26 14:13:36 +00:00
_sourceUnit.accept(*this);
return errorWatcher.ok();
2015-10-26 14:13:36 +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())
handleCallable(_function, _function, _function.annotation());
2015-10-26 14:13:36 +00:00
return true;
}
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;
if (CallableDeclaration const* baseFunction = resolveInheritDoc(_variable.annotation().baseFunctions, _variable, _variable.annotation()))
copyMissingTags(_variable.annotation(), {baseFunction});
else if (_variable.annotation().docTags.empty())
copyMissingTags(_variable.annotation(), _variable.annotation().baseFunctions);
return false;
}
bool DocStringAnalyser::visit(ModifierDefinition const& _modifier)
2015-10-26 14:13:36 +00:00
{
handleCallable(_modifier, _modifier, _modifier.annotation());
2015-10-26 14:13:36 +00:00
return true;
}
bool DocStringAnalyser::visit(EventDefinition const& _event)
2015-10-26 14:13:36 +00:00
{
handleCallable(_event, _event, _event.annotation());
2015-10-26 14:13:36 +00:00
return true;
}
void DocStringAnalyser::handleCallable(
CallableDeclaration const& _callable,
StructurallyDocumented const& _node,
StructurallyDocumentedAnnotation& _annotation
)
{
2020-06-29 12:30:09 +00:00
if (CallableDeclaration const* baseFunction = resolveInheritDoc(_callable.annotation().baseFunctions, _node, _annotation))
copyMissingTags(_annotation, {baseFunction});
else if (
_annotation.docTags.empty() &&
_callable.annotation().baseFunctions.size() == 1 &&
parameterNamesEqual(_callable, **_callable.annotation().baseFunctions.begin())
)
copyMissingTags(_annotation, _callable.annotation().baseFunctions);
2015-10-26 14:13:36 +00:00
}
2020-06-29 12:30:09 +00:00
CallableDeclaration const* DocStringAnalyser::resolveInheritDoc(
set<CallableDeclaration const*> const& _baseFuncs,
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;
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."
);
2020-06-29 12:30:09 +00:00
return nullptr;
2015-10-26 14:13:36 +00:00
}