forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword_versioning.rs
312 lines (265 loc) · 10.2 KB
/
keyword_versioning.rs
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
310
311
312
use std::collections::HashSet;
use anyhow::Result;
use clap::Parser;
use codegen_language_definition::model::{Item, KeywordDefinition, KeywordItem, Language};
use indicatif::{ProgressBar, ProgressStyle};
use infra_utils::terminal::NumbersDefaultDisplay;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use semver::Version;
use solidity_language::SolidityDefinition;
use crate::utils::{Binary, CliInput, InputSource, LanguageSelector};
/// Makes sure all Solidity definition keywords have the corrent version specifiers.
#[derive(Debug, Parser)]
pub struct KeywordVersioningCommand {
/// By default, all keyword variations are tested:
/// Most keywords generate only one variation (`function`, `contract`, `struct`, ...).
/// A few keywords generate 2-50 variations (`bytes1`, `bytes2`, `bytes3`, ...).
/// And a handful generate thousands of variations (`fixed{N}x{M}`, `ufixed{N}x{M}`, ...).
///
/// For local development, you can adjust the limit as needed.
/// For example, using a limit of 10 will test the majority of the grammar, but in a fraction of the time.
#[arg(long)]
variations_limit: Option<usize>,
}
impl KeywordVersioningCommand {
pub fn execute(self) -> Result<()> {
println!();
println!(" > Checking Solidity keywords:");
println!();
let language = SolidityDefinition::create();
let test_cases = self.generate_test_cases(&language);
println!();
println!(" > Downloading solc binaries:");
println!();
let binaries = Binary::fetch_all(&language)?;
println!();
println!(" > Testing all variations:");
println!();
let progress_bar = ProgressBar::new(test_cases.len() as u64);
let style = "[{elapsed_precise}] [{bar:80.cyan/blue}] {pos}/{len} │ ETA: {eta_precise}";
progress_bar.set_style(ProgressStyle::with_template(style)?);
let total_errors = test_cases
.par_iter()
.map(|test_case| {
let result = test_case.execute(&binaries);
let mut errors = 0;
if !result.should_be_reserved_in.is_empty() {
errors += 1;
progress_bar.println(format!(
"[{item}] '{variation}' should be reserved in: {versions:?}",
item = test_case.item,
variation = test_case.variation,
versions = result.should_be_reserved_in
));
}
if !result.should_be_unreserved_in.is_empty() {
errors += 1;
progress_bar.println(format!(
"[{item}] '{variation}' should be unreserved in: {versions:?}",
item = test_case.item,
variation = test_case.variation,
versions = result.should_be_unreserved_in
));
}
progress_bar.inc(1);
errors
})
.sum::<usize>();
progress_bar.finish();
println!();
println!();
println!("Found {total_errors} errors in keyword definitions.");
println!();
if total_errors > 0 {
std::process::exit(1);
}
Ok(())
}
fn generate_test_cases(&self, language: &Language) -> Vec<TestCase> {
let mut test_cases = vec![];
let mut already_seen = HashSet::new();
for item in language.items() {
let Item::Keyword { item } = item else {
continue;
};
for definition in &item.definitions {
let mut variations = definition.value.collect_variations();
match self.variations_limit {
Some(limit) => {
if variations.len() > limit {
println!(
"WARNING: One of '{name}' definitions generated {total} variations. Based on the '--variations-limit' option provided, only testing the first {limit} variations (skipping {skipped}).",
name = item.name,
total = variations.len().num_display(),
skipped = (variations.len() - limit).num_display(),
);
variations.truncate(limit);
}
}
None => {
if variations.len() > 100 {
// Hint to the user that they can limit the variations:
println!(
"INFO: One of '{name}' definitions generated {total} variations. Testing all of them. You can limit the variations using the '--variations-limit' option.",
name = item.name,
total = variations.len().num_display(),
);
}
}
};
for variation in variations {
assert!(
already_seen.insert((&item.identifier, variation.clone())),
"Duplicate variation: {variation}"
);
test_cases.push(TestCase::new(language, item, definition, variation));
}
}
}
test_cases
}
}
struct TestCase {
item: String,
variation: String,
reserved_in: HashSet<Version>,
source: String,
}
struct TestResult {
should_be_reserved_in: Vec<String>,
should_be_unreserved_in: Vec<String>,
}
impl TestCase {
fn new(
language: &Language,
item: &KeywordItem,
definition: &KeywordDefinition,
variation: String,
) -> Self {
let source = match item.identifier.as_str() {
"Identifier" => format!(
"// SPDX-License-Identifier: GPL-3.0
pragma solidity x.x.x;
contract Foo {{
function bar() public {{
uint256 {variation} = 0;
}}
}}"
),
"YulIdentifier" => format!(
"// SPDX-License-Identifier: GPL-3.0
pragma solidity x.x.x;
contract Foo {{
function bar() public {{
assembly {{
let {variation} := 1
}}
}}
}}"
),
other => {
panic!("Unexpected identifier: {other}");
}
};
let reserved_in = language
.versions
.iter()
.filter(|version| match &definition.reserved {
None => true,
Some(specifier) => specifier.contains(version),
})
.cloned()
.collect();
Self {
item: item.name.to_string(),
variation,
reserved_in,
source,
}
}
fn execute(&self, binaries: &Vec<Binary>) -> TestResult {
let mut should_be_reserved_in = vec![];
let mut should_be_unreserved_in = vec![];
for binary in binaries {
let success = self.test_version(binary);
let is_reserved = self.reserved_in.contains(&binary.version);
if success != is_reserved {
// Language definition is correct.
continue;
}
if is_reserved {
should_be_unreserved_in.push(binary.version.to_string());
} else {
should_be_reserved_in.push(binary.version.to_string());
}
}
TestResult {
should_be_reserved_in,
should_be_unreserved_in,
}
}
fn test_version(&self, binary: &Binary) -> bool {
let input = CliInput {
language: LanguageSelector::Solidity,
sources: [(
"input.sol".into(),
InputSource {
content: self.source.clone(),
},
)]
.into(),
};
let output = match binary.run(&input) {
Ok(output) => output,
Err(error) => {
let error = format!("{error:#?}");
if binary.version == Version::new(0, 4, 11)
&& error.contains("Command failed with code 'UNKNOWN' and signal '11'")
{
// solc 0.4.11 SEGFAULTs when a keyword exists in an identifier position.
return false;
}
println!();
println!(
"Invoking solc failed:\n{error}\n\nInput:\n{input}",
input = serde_json::to_string_pretty(&input).unwrap(),
);
std::process::exit(1);
}
};
for error in output.errors.iter().flatten() {
if [
"Expected identifier, got",
"Expected identifier but got",
"Expected token Identifier got",
"Expected token Semicolon got",
"Expected ';' but got ",
"State mutability can only be specified for address types.",
"Cannot use builtin function name",
"Cannot use instruction name",
"is reserved and can not be used.",
]
.iter()
.any(|part| error.message.contains(part))
{
// Identifier parsed as a keyword.
return false;
}
if [
"Unused local variable",
"Function state mutability can be restricted to pure",
"This declaration shadows a builtin symbol.",
"This declaration shadows a declaration outside",
]
.iter()
.any(|part| error.message.contains(part))
{
// Unrelated
continue;
}
println!("Unexpected error: {error:#?}");
std::process::exit(1);
}
true
}
}