Repository navigation
Expand file tree
/
Copy pathSourceTransform.h
More file actions
309 lines (266 loc) · 8.15 KB
/
Copy pathSourceTransform.h
File metadata and controls
309 lines (266 loc) · 8.15 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/analysis/OverrideChecker.h>
#include <libsolidity/ast/AST.h>
#include <liblangutil/CharStreamProvider.h>
#include <liblangutil/Scanner.h>
#include <sstream>
#include <regex>
namespace solidity::tools
{
/**
* Helper for displaying location during asserts
*/
class LocationHelper
{
std::stringstream m_stream;
public:
template <typename T>
LocationHelper& operator<<(T const& data)
{
m_stream << data;
return *this;
}
operator std::string() { return m_stream.str(); }
};
class SourceTextRetriever
{
public:
explicit SourceTextRetriever(langutil::CharStreamProvider const& _charStreamProvider):
m_charStreamProvider(_charStreamProvider) {}
std::string text(langutil::SourceLocation const& _location) const
{
solAssert(_location.hasText(), "");
return std::string{m_charStreamProvider.charStream(*_location.sourceName).text(_location)};
}
protected:
langutil::CharStreamProvider const& m_charStreamProvider;
};
/**
* Helper that provides functions which analyze certain source locations
* on a textual base. They utilize regular expression to search for
* keywords or to determine formatting.
*/
class SourceAnalysis: public SourceTextRetriever
{
public:
using SourceTextRetriever::SourceTextRetriever;
bool isMultilineKeyword(
langutil::SourceLocation const& _location,
std::string const& _keyword
) const
{
return regex_search(
text(_location),
std::regex{"(\\b" + _keyword + "\\b\\n|\\r|\\r\\n)"}
);
}
bool hasMutabilityKeyword(langutil::SourceLocation const& _location) const
{
return regex_search(
text(_location),
std::regex{"(\\b(pure|view|nonpayable|payable)\\b)"}
);
}
bool hasVirtualKeyword(langutil::SourceLocation const& _location) const
{
return regex_search(text(_location), std::regex{"(\\b(virtual)\\b)"});
}
bool hasVisibilityKeyword(langutil::SourceLocation const& _location) const
{
return regex_search(text(_location), std::regex{"\\bpublic\\b"});
}
};
/**
* Helper that provides functions which can analyse declarations and
* generate source snippets based on the information retrieved.
*/
class SourceGeneration
{
public:
using CompareFunction = frontend::OverrideChecker::CompareByID;
using Contracts = std::set<frontend::ContractDefinition const*, CompareFunction>;
/// Generates an `override` declaration for single overrides
/// or `override(...)` with contract list for multiple overrides.
static std::string functionOverride(Contracts const& _contracts)
{
if (_contracts.size() <= 1)
return "override";
std::string overrideList;
for (auto inheritedContract: _contracts)
overrideList += inheritedContract->name() + ",";
// Note: Can create incorrect replacements
return "override(" + overrideList.substr(0, overrideList.size() - 1) + ")";
}
};
/**
* Helper that provides functions which apply changes to Solidity source code
* on a textual base. In general, these utilize regular expressions applied
* to the given source location.
*/
class SourceTransform: public SourceTextRetriever
{
public:
using SourceTextRetriever::SourceTextRetriever;
/// Searches for the keyword given and prepends the expression.
/// E.g. `function f() view;` -> `function f() public view;`
std::string insertBeforeKeyword(
langutil::SourceLocation const& _location,
std::string const& _keyword,
std::string const& _expression
) const
{
auto _regex = std::regex{"(\\b" + _keyword + "\\b)"};
if (regex_search(text(_location), _regex))
return regex_replace(
text(_location),
_regex,
_expression + " " + _keyword,
std::regex_constants::format_first_only
);
else
solAssert(
false,
LocationHelper() << "Could not fix: " << text(_location) << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
/// Searches for the keyword given and appends the expression.
/// E.g. `function f() public {}` -> `function f() public override {}`
std::string insertAfterKeyword(
langutil::SourceLocation const& _location,
std::string const& _keyword,
std::string const& _expression
) const
{
bool isMultiline = SourceAnalysis{m_charStreamProvider}.isMultilineKeyword(_location, _keyword);
std::string toAppend = isMultiline ? ("\n " + _expression) : (" " + _expression);
std::regex keyword{"(\\b" + _keyword + "\\b)"};
if (regex_search(text(_location), keyword))
return regex_replace(text(_location), keyword, _keyword + toAppend);
else
solAssert(
false,
LocationHelper() << "Could not fix: " << text(_location) << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
/// Searches for the first right parenthesis and appends the expression
/// given.
/// E.g. `function f() {}` -> `function f() public {}`
std::string insertAfterRightParenthesis(
langutil::SourceLocation const& _location,
std::string const& _expression
) const
{
auto _regex = std::regex{"(\\))"};
if (regex_search(text(_location), _regex))
return regex_replace(
text(_location),
std::regex{"(\\))"},
") " + _expression
);
else
solAssert(
false,
LocationHelper() << "Could not fix: " << text(_location) << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
/// Searches for the `function` keyword and its identifier and replaces
/// both by the expression given.
/// E.g. `function Storage() {}` -> `constructor() {}`
std::string replaceFunctionName(
langutil::SourceLocation const& _location,
std::string const& _name,
std::string const& _expression
) const
{
auto _regex = std::regex{ "(\\bfunction\\s*" + _name + "\\b)"};
if (regex_search(text(_location), _regex))
return regex_replace(
text(_location),
_regex,
_expression
);
else
solAssert(
false,
LocationHelper() << "Could not fix: " << text(_location) << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
std::string gasUpdate(langutil::SourceLocation const& _location) const
{
// dot, "gas", any number of whitespaces, left bracket
std::regex gasReg{"\\.gas\\s*\\("};
if (regex_search(text(_location), gasReg))
{
std::string out = regex_replace(
text(_location),
gasReg,
"{gas: ",
std::regex_constants::format_first_only
);
return regex_replace(out, std::regex{"\\)$"}, "}");
}
else
solAssert(
false,
LocationHelper() << "Could not fix: " << text(_location) << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
std::string valueUpdate(langutil::SourceLocation const& _location) const
{
// dot, "value", any number of whitespaces, left bracket
std::regex valueReg{"\\.value\\s*\\("};
if (regex_search(text(_location), valueReg))
{
std::string out = regex_replace(
text(_location),
valueReg,
"{value: ",
std::regex_constants::format_first_only
);
return regex_replace(out, std::regex{"\\)$"}, "}");
}
else
solAssert(
false,
LocationHelper() << "Could not fix: " << text(_location) << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
std::string nowUpdate(langutil::SourceLocation const& _location) const
{
return regex_replace(text(_location), std::regex{"now"}, "block.timestamp");
}
std::string removeVisibility(langutil::SourceLocation const& _location) const
{
std::string replacement = text(_location);
for (auto const& replace: {"public ", "public", "internal ", "internal", "external ", "external"})
replacement = regex_replace(replacement, std::regex{replace}, "");
return replacement;
}
};
}