Skip to content

Commit

Permalink
Output bindings snapshots in addition to graph outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jun 25, 2024
1 parent 007e522 commit 85345f5
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions crates/codegen/runtime/cargo/src/runtime/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,11 @@ impl Debug for Handle<'_> {
f.debug_tuple("BindingsHandle").field(&self.handle).finish()
}
}

impl PartialEq for Handle<'_> {
fn eq(&self, other: &Self) -> bool {
let our_owner: *const Bindings = self.owner;
let other_owner: *const Bindings = other.owner;
our_owner == other_owner && self.handle == other.handle
}
}

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

1 change: 1 addition & 0 deletions crates/solidity/outputs/cargo/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ solidity_language = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
ariadne = { workspace = true }
codegen_language_definition = { workspace = true }
Inflector = { workspace = true }
infra_utils = { workspace = true }
Expand Down
101 changes: 90 additions & 11 deletions crates/solidity/outputs/cargo/tests/src/bindings_output/runner.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::fmt;
use std::fs::{self, create_dir_all};
use std::path::PathBuf;
use std::fs::{self, create_dir_all, File};
use std::ops::Range;
use std::path::{Path, PathBuf};

use anyhow::Result;
use ariadne::{Color, Config, Label, Report, ReportBuilder, ReportKind, Source};
use infra_utils::cargo::CargoWorkspace;
use metaslang_graph_builder::stack_graph;
use semver::Version;
use slang_solidity::bindings::graph_builder::{
ExecutionConfig, Graph, NoCancellation, Value, Variables,
};
use slang_solidity::bindings::Bindings;
use slang_solidity::bindings::{Bindings, Handle};
use slang_solidity::language::Language;
use slang_solidity::parse_output::ParseOutput;

Expand All @@ -18,7 +20,7 @@ pub fn run(group_name: &str, file_name: &str) -> Result<()> {
.join("bindings_output")
.join(group_name);
let input_path = data_dir.join(file_name);
let input = fs::read_to_string(input_path)?;
let input = fs::read_to_string(&input_path)?;

// TODO: de-hardcode this and parse with different versions?
let version = Language::SUPPORTED_VERSIONS.last().unwrap();
Expand All @@ -28,9 +30,19 @@ pub fn run(group_name: &str, file_name: &str) -> Result<()> {
assert!(parse_output.is_valid());

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

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

let bindings_output_path = output_dir.join(format!("{file_name}.txt"));
output_bindings(
version,
&parse_output,
&input,
&input_path,
bindings_output_path,
)?;

Ok(())
}
Expand All @@ -42,11 +54,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, output_path: PathBuf) -> Result<()> {
let graph_builder = Bindings::get_graph_builder()?;

let tree = parse_output.create_tree_cursor();
Expand Down Expand Up @@ -119,3 +127,74 @@ fn print_graph_as_mermaid(graph: &Graph) -> impl fmt::Display + '_ {

DisplayGraph(graph)
}

fn output_bindings(
version: &Version,
parse_output: &ParseOutput,
input: &str,
input_path: &Path,
output_path: PathBuf,
) -> Result<()> {
let mut bindings = Bindings::create(version.clone());
bindings.add_file(
input_path.to_str().unwrap(),
parse_output.create_tree_cursor(),
)?;

let file_id = input_path.file_name().unwrap().to_str().unwrap();
let mut builder: ReportBuilder<'_, (&str, Range<usize>)> = Report::build(
ReportKind::Custom("References and definitions", Color::Unset),
file_id,
0,
)
.with_config(Config::default().with_color(false));

let mut definitions: Vec<Handle<'_>> = Vec::new();

for definition in bindings.all_definitions() {
let Some(cursor) = definition.get_cursor() else {
continue;
};

let range = {
let range = cursor.text_range();
let start = input[..range.start.utf8].chars().count();
let end = input[..range.end.utf8].chars().count();
start..end
};

definitions.push(definition);
let message = format!("def: {}", definitions.len());
builder = builder.with_label(Label::new((file_id, range)).with_message(message));
}

for reference in bindings.all_references() {
let Some(cursor) = reference.get_cursor() else {
continue;
};

let range = {
let range = cursor.text_range();
let start = input[..range.start.utf8].chars().count();
let end = input[..range.end.utf8].chars().count();
start..end
};

let definition = reference.jump_to_definition();
let message = match definition {
None => "unresolved".to_string(),
Some(definition) => {
let def_id = definitions.iter().position(|d| *d == definition).unwrap();
format!("ref: {}", def_id + 1)
}
};

builder = builder.with_label(Label::new((file_id, range)).with_message(message));
}

let report = builder.finish();
let output_file = File::create(output_path)?;
report.write((file_id, Source::from(input)), output_file)?;

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
6 │ return x + 2;
│ ┬
│ ╰── ref: 3
10 │ function baz() returns (int) {
│ ─┬─
│ ╰─── def: 4
11 │ return w + x;
│ ┬ ┬
│ ╰────── unresolved
│ │
│ ╰── unresolved
────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
References and definitions:
╭─[params.sol:1:1]
1 │ contract Foo {
│ ─┬─
│ ╰─── def: 1
2 │ function bar(uint z) returns (uint) {
│ ─┬─ ┬
│ ╰────────── def: 2
│ │
│ ╰── def: 3
5 │ return z + 1;
│ ┬
│ ╰── ref: 3
9 │ function baz() returns (int) {
│ ─┬─
│ ╰─── def: 4
10 │ return z + 2;
│ ┬
│ ╰── unresolved
────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
References and definitions:
╭─[state_vars.sol:1:1]
1 │ contract Foo {
│ ─┬─
│ ╰─── def: 1
2 │ uint x;
│ ┬
│ ╰── def: 2
5 │ function bar() returns (uint) {
│ ─┬─
│ ╰─── def: 3
6 │ return x;
│ ┬
│ ╰── ref: 2
10 │ function baz() returns (int) {
│ ─┬─
│ ╰─── def: 4
11 │ return y;
│ ┬
│ ╰── unresolved
16 │ contract Bar {
│ ─┬─
│ ╰─── def: 5
17 │ function quux() returns (uint) {
│ ──┬─
│ ╰─── def: 6
18 │ return x;
│ ┬
│ ╰── unresolved
────╯

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

0 comments on commit 85345f5

Please sign in to comment.