aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis/DocStringAnalyser.cpp
blob: b3fb52583ccb145310ec8d790260ee298c0c1c13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
    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/DocStringAnalyser.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/interface/ErrorReporter.h>
#include <libsolidity/parsing/DocStringParser.h>

using namespace std;
using namespace dev;
using namespace dev::solidity;

bool DocStringAnalyser::analyseDocStrings(SourceUnit const& _sourceUnit)
{
    m_errorOccured = false;
    _sourceUnit.accept(*this);

    return !m_errorOccured;
}

bool DocStringAnalyser::visit(ContractDefinition const& _contract)
{
    static const set<string> validTags = set<string>{"author", "title", "dev", "notice"};
    parseDocStrings(_contract, _contract.annotation(), validTags, "contracts");

    return true;
}

bool DocStringAnalyser::visit(FunctionDefinition const& _function)
{
    handleCallable(_function, _function, _function.annotation());
    return true;
}

bool DocStringAnalyser::visit(ModifierDefinition const& _modifier)
{
    handleCallable(_modifier, _modifier, _modifier.annotation());

    return true;
}

bool DocStringAnalyser::visit(EventDefinition const& _event)
{
    handleCallable(_event, _event, _event.annotation());

    return true;
}

void DocStringAnalyser::handleCallable(
    CallableDeclaration const& _callable,
    Documented const& _node,
    DocumentedAnnotation& _annotation
)
{
    static const set<string> validTags = set<string>{"author", "dev", "notice", "return", "param"};
    parseDocStrings(_node, _annotation, validTags, "functions");

    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(
                "Documented parameter \"" +
                i->second.paramName +
                "\" not found in the parameter list of the function."
            );
}

void DocStringAnalyser::parseDocStrings(
    Documented const& _node,
    DocumentedAnnotation& _annotation,
    set<string> const& _validTags,
    string const& _nodeName
)
{
    DocStringParser parser;
    if (_node.documentation() && !_node.documentation()->empty())
    {
        if (!parser.parse(*_node.documentation(), m_errorReporter))
            m_errorOccured = true;
        _annotation.docTags = parser.tags();
    }
    for (auto const& docTag: _annotation.docTags)
        if (!_validTags.count(docTag.first))
            appendError("Doc tag @" + docTag.first + " not valid for " + _nodeName + ".");
}

void DocStringAnalyser::appendError(string const& _description)
{
    m_errorOccured = true;
    m_errorReporter.docstringParsingError(_description);
}