/src/solidity/liblangutil/ErrorReporter.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * @author Rhett <roadriverrail@gmail.com> |
20 | | * @date 2017 |
21 | | * Error reporting helper class. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <libsolutil/CommonData.h> |
27 | | #include <libsolutil/Exceptions.h> |
28 | | |
29 | | #include <liblangutil/Exceptions.h> |
30 | | #include <liblangutil/SourceLocation.h> |
31 | | #include <libsolutil/StringUtils.h> |
32 | | |
33 | | #include <range/v3/range/conversion.hpp> |
34 | | #include <range/v3/view/filter.hpp> |
35 | | |
36 | | namespace solidity::langutil |
37 | | { |
38 | | |
39 | | class ErrorReporter |
40 | | { |
41 | | public: |
42 | | |
43 | | explicit ErrorReporter(ErrorList& _errors): |
44 | 623k | m_errorList(_errors) { } |
45 | | |
46 | | ErrorReporter(ErrorReporter const& _errorReporter) noexcept: |
47 | 0 | m_errorList(_errorReporter.m_errorList) { } |
48 | | |
49 | | ErrorReporter& operator=(ErrorReporter const& _errorReporter); |
50 | | |
51 | | void append(ErrorList const& _errorList) |
52 | 16.2k | { |
53 | 16.2k | m_errorList += _errorList; |
54 | 16.2k | } |
55 | | |
56 | | void warning(ErrorId _error, std::string const& _description); |
57 | | |
58 | | void warning(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
59 | | |
60 | | void warning( |
61 | | ErrorId _error, |
62 | | SourceLocation const& _location, |
63 | | std::string const& _description, |
64 | | SecondarySourceLocation const& _secondaryLocation |
65 | | ); |
66 | | |
67 | | void info(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
68 | | |
69 | | void error( |
70 | | ErrorId _error, |
71 | | Error::Type _type, |
72 | | SourceLocation const& _location, |
73 | | std::string const& _description |
74 | | ); |
75 | | |
76 | | void info(ErrorId _error, std::string const& _description); |
77 | | |
78 | | void declarationError( |
79 | | ErrorId _error, |
80 | | SourceLocation const& _location, |
81 | | SecondarySourceLocation const& _secondaryLocation, |
82 | | std::string const& _description |
83 | | ); |
84 | | |
85 | | void declarationError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
86 | | |
87 | | void fatalDeclarationError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
88 | | |
89 | | void parserError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
90 | | void parserError(ErrorId _error, SourceLocation const& _location, SecondarySourceLocation const& _secondaryLocation, std::string const& _description); |
91 | | |
92 | | void fatalParserError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
93 | | |
94 | | void syntaxError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
95 | | |
96 | | void typeError( |
97 | | ErrorId _error, |
98 | | SourceLocation const& _location, |
99 | | SecondarySourceLocation const& _secondaryLocation = SecondarySourceLocation(), |
100 | | std::string const& _description = std::string() |
101 | | ); |
102 | | |
103 | | void typeError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
104 | | |
105 | | template <typename... Strings> |
106 | | void typeErrorConcatenateDescriptions(ErrorId _error, SourceLocation const& _location, Strings const&... _descriptions) |
107 | 3.95k | { |
108 | 3.95k | std::initializer_list<std::string> const descs = { _descriptions... }; |
109 | 3.95k | solAssert(descs.size() > 0, "Need error descriptions!"); |
110 | | |
111 | 11.8k | auto nonEmpty = [](std::string const& _s) { return !_s.empty(); }; |
112 | 3.95k | std::string errorStr = util::joinHumanReadable(descs | ranges::views::filter(nonEmpty) | ranges::to_vector, " "); |
113 | | |
114 | 3.95k | error(_error, Error::Type::TypeError, _location, errorStr); |
115 | 3.95k | } |
116 | | |
117 | | void fatalTypeError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
118 | | void fatalTypeError(ErrorId _error, SourceLocation const& _location, SecondarySourceLocation const& _secondLocation, std::string const& _description); |
119 | | |
120 | | void docstringParsingError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
121 | | |
122 | | void unimplementedFeatureError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
123 | | |
124 | | void codeGenerationError(ErrorId _error, SourceLocation const& _location, std::string const& _description); |
125 | | void codeGenerationError(Error const& _error); |
126 | | |
127 | | ErrorList const& errors() const; |
128 | | |
129 | | void clear(); |
130 | | |
131 | | /// @returns true iff there is any error (ignores warnings and infos). |
132 | | bool hasErrors() const |
133 | 556k | { |
134 | 556k | return m_errorCount > 0; |
135 | 556k | } |
136 | | |
137 | | /// @returns true if there is any error, warning or info. |
138 | | bool hasErrorsWarningsOrInfos() const |
139 | 159k | { |
140 | 159k | return m_errorCount + m_warningCount + m_infoCount > 0; |
141 | 159k | } |
142 | | |
143 | | /// @returns the number of errors (ignores warnings and infos). |
144 | | unsigned errorCount() const |
145 | 74.5M | { |
146 | 74.5M | return m_errorCount; |
147 | 74.5M | } |
148 | | |
149 | | // @returns true if the maximum error count has been reached. |
150 | | bool hasExcessiveErrors() const; |
151 | | |
152 | | /// @returns true if there is at least one occurrence of error |
153 | | bool hasError(ErrorId _errorId) const; |
154 | | |
155 | | class ErrorWatcher |
156 | | { |
157 | | public: |
158 | | ErrorWatcher(ErrorReporter const& _errorReporter): |
159 | | m_errorReporter(_errorReporter), |
160 | | m_initialErrorCount(_errorReporter.errorCount()) |
161 | 29.1M | {} |
162 | | bool ok() const |
163 | 22.6M | { |
164 | 22.6M | solAssert(m_initialErrorCount <= m_errorReporter.errorCount(), "Unexpected error count."); |
165 | 22.6M | return m_initialErrorCount == m_errorReporter.errorCount(); |
166 | 22.6M | } |
167 | | private: |
168 | | ErrorReporter const& m_errorReporter; |
169 | | unsigned const m_initialErrorCount; |
170 | | }; |
171 | | |
172 | | ErrorWatcher errorWatcher() const |
173 | 29.1M | { |
174 | 29.1M | return ErrorWatcher(*this); |
175 | 29.1M | } |
176 | | |
177 | | private: |
178 | | void error( |
179 | | ErrorId _error, |
180 | | Error::Type _type, |
181 | | SourceLocation const& _location, |
182 | | SecondarySourceLocation const& _secondaryLocation, |
183 | | std::string const& _description = std::string()); |
184 | | |
185 | | void fatalError( |
186 | | ErrorId _error, |
187 | | Error::Type _type, |
188 | | SourceLocation const& _location, |
189 | | SecondarySourceLocation const& _secondaryLocation, |
190 | | std::string const& _description = std::string()); |
191 | | |
192 | | void fatalError( |
193 | | ErrorId _error, |
194 | | Error::Type _type, |
195 | | SourceLocation const& _location = SourceLocation(), |
196 | | std::string const& _description = std::string()); |
197 | | |
198 | | // @returns true if error shouldn't be stored |
199 | | bool checkForExcessiveErrors(Error::Type _type); |
200 | | |
201 | | ErrorList& m_errorList; |
202 | | |
203 | | unsigned m_errorCount = 0; |
204 | | unsigned m_warningCount = 0; |
205 | | unsigned m_infoCount = 0; |
206 | | |
207 | | unsigned const c_maxWarningsAllowed = 256; |
208 | | unsigned const c_maxErrorsAllowed = 256; |
209 | | unsigned const c_maxInfosAllowed = 256; |
210 | | }; |
211 | | |
212 | | } |