Skip to content

Commit 235ef78

Browse files
Add unstable-add-statements-code-locations-debug-info flag (#1538)
Co-authored-by: Maksymilian Demitraszek <[email protected]>
1 parent 4a4ebf9 commit 235ef78

File tree

9 files changed

+342
-13
lines changed

9 files changed

+342
-13
lines changed

extensions/scarb-snforge-test-collector/src/compilation/test_collector.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use cairo_lang_sierra::extensions::NamedType;
2121
use cairo_lang_sierra::ids::GenericTypeId;
2222
use cairo_lang_sierra::program::{GenericArg, ProgramArtifact};
2323
use cairo_lang_sierra_generator::db::SierraGenGroup;
24+
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
2425
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
2526
use cairo_lang_starknet::starknet_plugin_suite;
2627
use cairo_lang_test_plugin::test_plugin_suite;
@@ -143,16 +144,9 @@ pub fn collect_tests(
143144
.context("Compilation failed without any diagnostics")
144145
.context("Failed to get sierra program")?;
145146

146-
let debug_annotations = if compilation_unit.unstable_add_statements_functions_debug_info() {
147-
Some(Annotations::from(
148-
sierra_program
149-
.debug_info
150-
.statements_locations
151-
.extract_statements_functions(db),
152-
))
153-
} else {
154-
None
155-
};
147+
let debug_annotations: Option<Annotations> =
148+
maybe_build_debug_annotations(compilation_unit, &sierra_program, db);
149+
156150
let debug_info = debug_annotations.map(|annotations| DebugInfo {
157151
type_names: Default::default(),
158152
executables: Default::default(),
@@ -208,6 +202,36 @@ pub fn collect_tests(
208202
))
209203
}
210204

205+
fn maybe_build_debug_annotations(
206+
compilation_unit: &CompilationUnit,
207+
sierra_program: &Arc<SierraProgramWithDebug>,
208+
db: &mut RootDatabase,
209+
) -> Option<Annotations> {
210+
if !compilation_unit.unstable_add_statements_functions_debug_info()
211+
&& !compilation_unit.unstable_add_statements_code_locations_debug_info()
212+
{
213+
return None;
214+
};
215+
let mut debug_annotations: Annotations = Annotations::default();
216+
if compilation_unit.unstable_add_statements_functions_debug_info() {
217+
debug_annotations.extend(Annotations::from(
218+
sierra_program
219+
.debug_info
220+
.statements_locations
221+
.extract_statements_functions(db),
222+
));
223+
}
224+
if compilation_unit.unstable_add_statements_code_locations_debug_info() {
225+
debug_annotations.extend(Annotations::from(
226+
sierra_program
227+
.debug_info
228+
.statements_locations
229+
.extract_statements_source_code_locations(db),
230+
));
231+
}
232+
Some(debug_annotations)
233+
}
234+
211235
fn build_test_details(function_finder: &FunctionFinder, test_name: &str) -> Result<TestDetails> {
212236
let func = function_finder.find_function(test_name)?;
213237

extensions/scarb-snforge-test-collector/src/metadata.rs

+9
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ impl CompilationUnit<'_> {
166166
.unwrap_or(false)
167167
}
168168

169+
pub fn unstable_add_statements_code_locations_debug_info(&self) -> bool {
170+
self.unit_metadata
171+
.compiler_config
172+
.as_object()
173+
.and_then(|config| config.get("unstable_add_statements_code_locations_debug_info"))
174+
.and_then(|value| value.as_bool())
175+
.unwrap_or(false)
176+
}
177+
169178
pub fn main_package_source_root(&self) -> Utf8PathBuf {
170179
self.main_package_metadata.source_root().to_path_buf()
171180
}

extensions/scarb-snforge-test-collector/tests/test.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use assert_fs::prelude::*;
22
use assert_fs::TempDir;
33
use cairo_lang_sierra::program::StatementIdx;
4+
use cairo_lang_sierra_generator::statements_code_locations::SourceCodeSpan;
45
use std::collections::HashMap;
56

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

477+
#[test]
478+
fn generates_statements_code_locations_mappings() {
479+
let t = TempDir::new().unwrap();
480+
481+
ProjectBuilder::start()
482+
.name("forge_test")
483+
.lib_cairo(SIMPLE_TEST)
484+
.manifest_extra(indoc! {r#"
485+
[cairo]
486+
unstable-add-statements-code-locations-debug-info = true
487+
"#})
488+
.build(&t);
489+
490+
Scarb::quick_snapbox()
491+
.arg("snforge-test-collector")
492+
.current_dir(&t)
493+
.assert()
494+
.success();
495+
496+
let snforge_sierra = t
497+
.child("target/dev/snforge/forge_test.snforge_sierra.json")
498+
.read_to_string();
499+
500+
let json: Value = serde_json::from_str(&snforge_sierra).unwrap();
501+
502+
let mappings = &json[0]["sierra_program"]["debug_info"]["annotations"]
503+
["github.com/software-mansion/cairo-coverage"]["statements_code_locations"];
504+
505+
assert!(
506+
serde_json::from_value::<HashMap<StatementIdx, Vec<(String, SourceCodeSpan)>>>(
507+
mappings.clone()
508+
)
509+
.is_ok()
510+
);
511+
}
512+
476513
#[test]
477514
fn features_test_build_success() {
478515
let t = TempDir::new().unwrap();

scarb/src/compiler/compilers/test.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ impl Compiler for TestCompiler {
4646
add_statements_functions: unit
4747
.compiler_config
4848
.unstable_add_statements_functions_debug_info,
49-
add_statements_code_locations: false,
49+
add_statements_code_locations: unit
50+
.compiler_config
51+
.unstable_add_statements_code_locations_debug_info,
5052
};
5153
let allow_warnings = unit.compiler_config.allow_warnings;
5254
compile_test_prepared_db(db, config, main_crate_ids, test_crate_ids, allow_warnings)?

scarb/src/compiler/helpers.rs

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ pub fn build_compiler_config<'c>(
6363
add_statements_functions: unit
6464
.compiler_config
6565
.unstable_add_statements_functions_debug_info,
66+
add_statements_code_locations: unit
67+
.compiler_config
68+
.unstable_add_statements_code_locations_debug_info,
6669
..CompilerConfig::default()
6770
}
6871
}

scarb/src/core/manifest/compiler_config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ pub struct ManifestCompilerConfig {
2626
/// Used by [cairo-profiler](https://github.com/software-mansion/cairo-profiler).
2727
/// This feature is unstable and is subject to change.
2828
pub unstable_add_statements_functions_debug_info: bool,
29+
/// Add a mapping between sierra statement indexes and code location in cairo code
30+
/// to debug info. A statement index maps to a vector consisting of a code location which caused the
31+
/// statement to be generated and all code location that were inlined or generated along the way.
32+
/// Used by [cairo-coverage](https://github.com/software-mansion/cairo-coverage).
33+
/// This feature is unstable and is subject to change.
34+
pub unstable_add_statements_code_locations_debug_info: bool,
2935
// Inlining strategy.
3036
pub inlining_strategy: InliningStrategy,
3137
}
@@ -47,6 +53,7 @@ impl DefaultForProfile for ManifestCompilerConfig {
4753
allow_warnings: true,
4854
enable_gas: true,
4955
unstable_add_statements_functions_debug_info: false,
56+
unstable_add_statements_code_locations_debug_info: false,
5057
inlining_strategy: InliningStrategy::default(),
5158
}
5259
}
@@ -61,6 +68,9 @@ impl From<ManifestCompilerConfig> for TomlCairo {
6168
unstable_add_statements_functions_debug_info: Some(
6269
config.unstable_add_statements_functions_debug_info,
6370
),
71+
unstable_add_statements_code_locations_debug_info: Some(
72+
config.unstable_add_statements_code_locations_debug_info,
73+
),
6474
inlining_strategy: Some(config.inlining_strategy),
6575
}
6676
}

scarb/src/core/manifest/toml_manifest.rs

+12
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ pub struct TomlCairo {
321321
/// Used by [cairo-profiler](https://github.com/software-mansion/cairo-profiler).
322322
/// This feature is unstable and is subject to change.
323323
pub unstable_add_statements_functions_debug_info: Option<bool>,
324+
/// Add a mapping between sierra statement indexes and lines in cairo code
325+
/// to debug info. A statement index maps to a vector consisting of a line which caused the
326+
/// statement to be generated and all lines that were inlined or generated along the way.
327+
/// Used by [cairo-coverage](https://github.com/software-mansion/cairo-coverage).
328+
/// This feature is unstable and is subject to change.
329+
pub unstable_add_statements_code_locations_debug_info: Option<bool>,
324330
/// Inlining strategy.
325331
pub inlining_strategy: Option<InliningStrategy>,
326332
}
@@ -856,6 +862,12 @@ impl TomlManifest {
856862
compiler_config.unstable_add_statements_functions_debug_info =
857863
unstable_add_statements_functions_debug_info;
858864
}
865+
if let Some(unstable_add_statements_code_locations_debug_info) =
866+
cairo.unstable_add_statements_code_locations_debug_info
867+
{
868+
compiler_config.unstable_add_statements_code_locations_debug_info =
869+
unstable_add_statements_code_locations_debug_info;
870+
}
859871
}
860872
Ok(compiler_config)
861873
}

0 commit comments

Comments
 (0)