Skip to content

Commit

Permalink
Output bindings and graph snapshots on versions with changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jun 25, 2024
1 parent 85345f5 commit 7455bd6
Show file tree
Hide file tree
Showing 17 changed files with 688 additions and 167 deletions.
24 changes: 22 additions & 2 deletions crates/codegen/testing/src/bindings_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use infra_utils::codegen::CodegenFileSystem;
use infra_utils::paths::FileWalker;

pub fn generate_bindings_output_tests(
_language: &Language,
language: &Language,
data_dir: &Path,
output_dir: &Path,
) -> Result<()> {
let tests = collect_bindings_tests(data_dir)?;

let mut fs = CodegenFileSystem::new(data_dir)?;

generate_mod_file(&mut fs, &output_dir.join("mod.rs"), &tests)?;
generate_mod_file(language, &mut fs, &output_dir.join("mod.rs"), &tests)?;

for (group_name, test_files) in &tests {
generate_unit_test_file(
Expand Down Expand Up @@ -58,6 +58,7 @@ fn collect_bindings_tests(data_dir: &Path) -> Result<BTreeMap<String, BTreeSet<S
}

fn generate_mod_file(
language: &Language,
fs: &mut CodegenFileSystem,
mod_file_path: &Path,
bindings_tests: &BTreeMap<String, BTreeSet<String>>,
Expand All @@ -70,9 +71,28 @@ fn generate_mod_file(
buffer
});

let version_breaks = language.collect_breaking_versions();
let version_breaks_len = version_breaks.len();
let version_breaks_str = version_breaks
.iter()
.fold(String::new(), |mut buffer, version| {
writeln!(
buffer,
"Version::new({}, {}, {}),",
version.major, version.minor, version.patch
)
.unwrap();
buffer
});

let contents = format!(
"
use semver::Version;
{module_declarations_str}
pub const VERSION_BREAKS: [Version; {version_breaks_len}] = [
{version_breaks_str}
];
",
);

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 40 additions & 28 deletions crates/solidity/outputs/cargo/tests/src/bindings_output/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt;
use std::fs::{self, create_dir_all, File};
use std::fs::{self, create_dir_all};
use std::io::BufWriter;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::path::Path;

use anyhow::Result;
use ariadne::{Color, Config, Label, Report, ReportBuilder, ReportKind, Source};
Expand All @@ -15,34 +16,47 @@ use slang_solidity::bindings::{Bindings, Handle};
use slang_solidity::language::Language;
use slang_solidity::parse_output::ParseOutput;

use super::generated::VERSION_BREAKS;

pub fn run(group_name: &str, file_name: &str) -> Result<()> {
let data_dir = CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?
.join("bindings_output")
.join(group_name);
let input_path = data_dir.join(file_name);
let input = fs::read_to_string(&input_path)?;

// TODO: de-hardcode this and parse with different versions?
let version = Language::SUPPORTED_VERSIONS.last().unwrap();
let language = Language::new(version.clone())?;
let mut last_graph_output = None;
let mut last_bindings_output = None;

let parse_output = language.parse(Language::ROOT_KIND, &input);
assert!(parse_output.is_valid());
for version in &VERSION_BREAKS {
let language = Language::new(version.clone())?;

let output_dir = data_dir.join("generated");
create_dir_all(&output_dir)?;
let parse_output = language.parse(Language::ROOT_KIND, &input);
assert!(parse_output.is_valid());

let graph_output_path = output_dir.join(format!("{file_name}.mmd"));
output_graph(version, &parse_output, graph_output_path)?;
let output_dir = data_dir.join("generated");
create_dir_all(&output_dir)?;

let bindings_output_path = output_dir.join(format!("{file_name}.txt"));
output_bindings(
version,
&parse_output,
&input,
&input_path,
bindings_output_path,
)?;
let graph_output = output_graph(version, &parse_output)?;
match last_graph_output {
Some(ref last) if last == &graph_output => (),
_ => {
let graph_output_path = output_dir.join(format!("{file_name}-{version}.mmd"));
fs::write(graph_output_path, &graph_output)?;
last_graph_output = Some(graph_output);
}
};

let bindings_output = output_bindings(version, &parse_output, &input, &input_path)?;
match last_bindings_output {
Some(ref last) if last == &bindings_output => (),
_ => {
let bindings_output_path = output_dir.join(format!("{file_name}-{version}.txt"));
fs::write(bindings_output_path, &bindings_output)?;
last_bindings_output = Some(bindings_output);
}
}
}

Ok(())
}
Expand All @@ -54,7 +68,7 @@ const VARIABLE_DEBUG_ATTR: &str = "__variable";
const LOCATION_DEBUG_ATTR: &str = "__location";
const MATCH_DEBUG_ATTR: &str = "__match";

fn output_graph(version: &Version, parse_output: &ParseOutput, output_path: PathBuf) -> Result<()> {
fn output_graph(version: &Version, parse_output: &ParseOutput) -> Result<String> {
let graph_builder = Bindings::get_graph_builder()?;

let tree = parse_output.create_tree_cursor();
Expand All @@ -77,9 +91,7 @@ fn output_graph(version: &Version, parse_output: &ParseOutput, output_path: Path

graph_builder.execute_into(&mut graph, &tree, &execution_config, &NoCancellation)?;

fs::write(output_path, format!("{}", print_graph_as_mermaid(&graph)))?;

Ok(())
Ok(format!("{}", print_graph_as_mermaid(&graph)))
}

fn print_graph_as_mermaid(graph: &Graph) -> impl fmt::Display + '_ {
Expand Down Expand Up @@ -133,8 +145,7 @@ fn output_bindings(
parse_output: &ParseOutput,
input: &str,
input_path: &Path,
output_path: PathBuf,
) -> Result<()> {
) -> Result<String> {
let mut bindings = Bindings::create(version.clone());
bindings.add_file(
input_path.to_str().unwrap(),
Expand Down Expand Up @@ -193,8 +204,9 @@ fn output_bindings(
}

let report = builder.finish();
let output_file = File::create(output_path)?;
report.write((file_id, Source::from(input)), output_file)?;
let mut buffer = BufWriter::new(Vec::new());
report.write((file_id, Source::from(input)), &mut buffer)?;

Ok(())
let result = String::from_utf8(buffer.buffer().to_vec())?;
Ok(result)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
graph TD
N0["`**0** @(1, 1)
ROOT_NODE
#null`"]
N1["`**1** @(1, 1)
@source_unit.lexical_scope
line 20 column 21`"]
N1 --> N4
N2["`**2** @(1, 1)
@source_unit.defs
line 23 column 21`"]
N2 --> N4
N3["`**3** @(1, 1)
@definition.lexical_scope
line 39 column 20`"]
N3 --> N1
N3 --> N6
N3 --> N8
N4["`**4** @(1, 1)
@definition.defs
line 40 column 20`"]
N4 --> N9
N5["`**5** @(2, 1)
@definition.lexical_scope
line 39 column 20`"]
N5 --> N3
N5 --> N13
N6["`**6** @(2, 1)
@definition.defs
line 40 column 20`"]
N6 --> N10
N7["`**7** @(6, 1)
@definition.lexical_scope
line 39 column 20`"]
N7 --> N3
N7 --> N15
N8["`**8** @(6, 1)
@definition.defs
line 40 column 20`"]
N8 --> N11
N9[\"`**Foo** @(1, 1)
def
line 75 column 8`"/]
N10[\"`**bar** @(2, 1)
def
line 96 column 8`"/]
N11[\"`**baz** @(6, 1)
def
line 96 column 8`"/]
N12["`**12** @(2, 29)
@param.lexical_scope
line 108 column 15`"]
N12 --> N5
N13["`**13** @(2, 29)
@param.defs
line 109 column 15`"]
N14["`**14** @(7, 29)
@param.lexical_scope
line 108 column 15`"]
N14 --> N7
N15["`**15** @(7, 29)
@param.defs
line 109 column 15`"]
N16["`**16** @(2, 34)
@block.lexical_scope
line 183 column 15`"]
N16 --> N5
N16 --> N21
N16 --> N23
N17["`**17** @(2, 34)
@block.defs
line 184 column 15`"]
N17 --> N21
N17 --> N23
N18["`**18** @(7, 33)
@block.lexical_scope
line 183 column 15`"]
N18 --> N7
N18 --> N25
N19["`**19** @(7, 33)
@block.defs
line 184 column 15`"]
N19 --> N25
N20["`**20** @(3, 1)
@stmt.lexical_scope
line 188 column 14`"]
N20 --> N16
N21["`**21** @(3, 1)
@stmt.defs
line 189 column 14`"]
N21 --> N26
N22["`**22** @(4, 1)
@stmt.lexical_scope
line 188 column 14`"]
N22 --> N16
N23["`**23** @(4, 1)
@stmt.defs
line 189 column 14`"]
N24["`**24** @(8, 1)
@stmt.lexical_scope
line 188 column 14`"]
N24 --> N18
N25["`**25** @(8, 1)
@stmt.defs
line 189 column 14`"]
N26[\"`**x** @(3, 1)
def
line 250 column 8`"/]
N27["`**27** @(3, 17)
@expr.lexical_scope
line 338 column 14`"]
N27 --> N20
N28["`**28** @(4, 15)
@expr.lexical_scope
line 338 column 14`"]
N28 --> N22
N29["`**29** @(4, 15)
@expr.lexical_scope
line 338 column 14`"]
N29 --> N28
N30["`**30** @(4, 19)
@expr.lexical_scope
line 338 column 14`"]
N30 --> N28
N31["`**31** @(8, 15)
@expr.lexical_scope
line 338 column 14`"]
N31 --> N24
N32["`**32** @(8, 15)
@expr.lexical_scope
line 338 column 14`"]
N32 --> N31
N33["`**33** @(8, 19)
@expr.lexical_scope
line 338 column 14`"]
N33 --> N31
N34[/"`**x** @(4, 15)
ref
line 342 column 8`"\]
N34 --> N29
N35[/"`**w** @(8, 15)
ref
line 342 column 8`"\]
N35 --> N32
N36[/"`**x** @(8, 19)
ref
line 342 column 8`"\]
N36 --> N33
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
References and definitions:
╭─[local_vars.sol:1:1]
1 │ contract Foo {
│ ─┬─
│ ╰─── def: 1
2 │ function bar() returns (uint) {
│ ─┬─
│ ╰─── def: 2
3 │ uint x = 10;
│ ┬
│ ╰── def: 3
4 │ return x + 2;
│ ┬
│ ╰── ref: 3
7 │ function baz() returns (int) {
│ ─┬─
│ ╰─── def: 4
8 │ return w + x;
│ ┬ ┬
│ ╰────── unresolved
│ │
│ ╰── unresolved
───╯
Loading

0 comments on commit 7455bd6

Please sign in to comment.