Skip to content

Commit

Permalink
Add unstable-add-statements-code-locations-debug-info flag (#1538)
Browse files Browse the repository at this point in the history
Co-authored-by: Maksymilian Demitraszek <maksymilian.demitraszek@protonmail.com>
  • Loading branch information
maciektr and MaksymilianDemitraszek authored Aug 26, 2024
1 parent 4a4ebf9 commit 235ef78
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use cairo_lang_sierra::extensions::NamedType;
use cairo_lang_sierra::ids::GenericTypeId;
use cairo_lang_sierra::program::{GenericArg, ProgramArtifact};
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_test_plugin::test_plugin_suite;
Expand Down Expand Up @@ -143,16 +144,9 @@ pub fn collect_tests(
.context("Compilation failed without any diagnostics")
.context("Failed to get sierra program")?;

let debug_annotations = if compilation_unit.unstable_add_statements_functions_debug_info() {
Some(Annotations::from(
sierra_program
.debug_info
.statements_locations
.extract_statements_functions(db),
))
} else {
None
};
let debug_annotations: Option<Annotations> =
maybe_build_debug_annotations(compilation_unit, &sierra_program, db);

let debug_info = debug_annotations.map(|annotations| DebugInfo {
type_names: Default::default(),
executables: Default::default(),
Expand Down Expand Up @@ -208,6 +202,36 @@ pub fn collect_tests(
))
}

fn maybe_build_debug_annotations(
compilation_unit: &CompilationUnit,
sierra_program: &Arc<SierraProgramWithDebug>,
db: &mut RootDatabase,
) -> Option<Annotations> {
if !compilation_unit.unstable_add_statements_functions_debug_info()
&& !compilation_unit.unstable_add_statements_code_locations_debug_info()
{
return None;
};
let mut debug_annotations: Annotations = Annotations::default();
if compilation_unit.unstable_add_statements_functions_debug_info() {
debug_annotations.extend(Annotations::from(
sierra_program
.debug_info
.statements_locations
.extract_statements_functions(db),
));
}
if compilation_unit.unstable_add_statements_code_locations_debug_info() {
debug_annotations.extend(Annotations::from(
sierra_program
.debug_info
.statements_locations
.extract_statements_source_code_locations(db),
));
}
Some(debug_annotations)
}

fn build_test_details(function_finder: &FunctionFinder, test_name: &str) -> Result<TestDetails> {
let func = function_finder.find_function(test_name)?;

Expand Down
9 changes: 9 additions & 0 deletions extensions/scarb-snforge-test-collector/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ impl CompilationUnit<'_> {
.unwrap_or(false)
}

pub fn unstable_add_statements_code_locations_debug_info(&self) -> bool {
self.unit_metadata
.compiler_config
.as_object()
.and_then(|config| config.get("unstable_add_statements_code_locations_debug_info"))
.and_then(|value| value.as_bool())
.unwrap_or(false)
}

pub fn main_package_source_root(&self) -> Utf8PathBuf {
self.main_package_metadata.source_root().to_path_buf()
}
Expand Down
37 changes: 37 additions & 0 deletions extensions/scarb-snforge-test-collector/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use assert_fs::prelude::*;
use assert_fs::TempDir;
use cairo_lang_sierra::program::StatementIdx;
use cairo_lang_sierra_generator::statements_code_locations::SourceCodeSpan;
use std::collections::HashMap;

use scarb_test_support::fsx::ChildPathEx;
Expand Down Expand Up @@ -473,6 +474,42 @@ fn generates_statements_functions_mappings() {
assert!(serde_json::from_value::<HashMap<StatementIdx, Vec<String>>>(mappings.clone()).is_ok());
}

#[test]
fn generates_statements_code_locations_mappings() {
let t = TempDir::new().unwrap();

ProjectBuilder::start()
.name("forge_test")
.lib_cairo(SIMPLE_TEST)
.manifest_extra(indoc! {r#"
[cairo]
unstable-add-statements-code-locations-debug-info = true
"#})
.build(&t);

Scarb::quick_snapbox()
.arg("snforge-test-collector")
.current_dir(&t)
.assert()
.success();

let snforge_sierra = t
.child("target/dev/snforge/forge_test.snforge_sierra.json")
.read_to_string();

let json: Value = serde_json::from_str(&snforge_sierra).unwrap();

let mappings = &json[0]["sierra_program"]["debug_info"]["annotations"]
["github.com/software-mansion/cairo-coverage"]["statements_code_locations"];

assert!(
serde_json::from_value::<HashMap<StatementIdx, Vec<(String, SourceCodeSpan)>>>(
mappings.clone()
)
.is_ok()
);
}

#[test]
fn features_test_build_success() {
let t = TempDir::new().unwrap();
Expand Down
4 changes: 3 additions & 1 deletion scarb/src/compiler/compilers/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ impl Compiler for TestCompiler {
add_statements_functions: unit
.compiler_config
.unstable_add_statements_functions_debug_info,
add_statements_code_locations: false,
add_statements_code_locations: unit
.compiler_config
.unstable_add_statements_code_locations_debug_info,
};
let allow_warnings = unit.compiler_config.allow_warnings;
compile_test_prepared_db(db, config, main_crate_ids, test_crate_ids, allow_warnings)?
Expand Down
3 changes: 3 additions & 0 deletions scarb/src/compiler/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub fn build_compiler_config<'c>(
add_statements_functions: unit
.compiler_config
.unstable_add_statements_functions_debug_info,
add_statements_code_locations: unit
.compiler_config
.unstable_add_statements_code_locations_debug_info,
..CompilerConfig::default()
}
}
Expand Down
10 changes: 10 additions & 0 deletions scarb/src/core/manifest/compiler_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub struct ManifestCompilerConfig {
/// Used by [cairo-profiler](https://github.com/software-mansion/cairo-profiler).
/// This feature is unstable and is subject to change.
pub unstable_add_statements_functions_debug_info: bool,
/// Add a mapping between sierra statement indexes and code location in cairo code
/// to debug info. A statement index maps to a vector consisting of a code location which caused the
/// statement to be generated and all code location that were inlined or generated along the way.
/// Used by [cairo-coverage](https://github.com/software-mansion/cairo-coverage).
/// This feature is unstable and is subject to change.
pub unstable_add_statements_code_locations_debug_info: bool,
// Inlining strategy.
pub inlining_strategy: InliningStrategy,
}
Expand All @@ -47,6 +53,7 @@ impl DefaultForProfile for ManifestCompilerConfig {
allow_warnings: true,
enable_gas: true,
unstable_add_statements_functions_debug_info: false,
unstable_add_statements_code_locations_debug_info: false,
inlining_strategy: InliningStrategy::default(),
}
}
Expand All @@ -61,6 +68,9 @@ impl From<ManifestCompilerConfig> for TomlCairo {
unstable_add_statements_functions_debug_info: Some(
config.unstable_add_statements_functions_debug_info,
),
unstable_add_statements_code_locations_debug_info: Some(
config.unstable_add_statements_code_locations_debug_info,
),
inlining_strategy: Some(config.inlining_strategy),
}
}
Expand Down
12 changes: 12 additions & 0 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ pub struct TomlCairo {
/// Used by [cairo-profiler](https://github.com/software-mansion/cairo-profiler).
/// This feature is unstable and is subject to change.
pub unstable_add_statements_functions_debug_info: Option<bool>,
/// Add a mapping between sierra statement indexes and lines in cairo code
/// to debug info. A statement index maps to a vector consisting of a line which caused the
/// statement to be generated and all lines that were inlined or generated along the way.
/// Used by [cairo-coverage](https://github.com/software-mansion/cairo-coverage).
/// This feature is unstable and is subject to change.
pub unstable_add_statements_code_locations_debug_info: Option<bool>,
/// Inlining strategy.
pub inlining_strategy: Option<InliningStrategy>,
}
Expand Down Expand Up @@ -856,6 +862,12 @@ impl TomlManifest {
compiler_config.unstable_add_statements_functions_debug_info =
unstable_add_statements_functions_debug_info;
}
if let Some(unstable_add_statements_code_locations_debug_info) =
cairo.unstable_add_statements_code_locations_debug_info
{
compiler_config.unstable_add_statements_code_locations_debug_info =
unstable_add_statements_code_locations_debug_info;
}
}
Ok(compiler_config)
}
Expand Down
Loading

0 comments on commit 235ef78

Please sign in to comment.