From 1d41d392949aa3891d93813ae30467026c80c48a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Mon, 16 Oct 2023 17:21:48 -0400 Subject: [PATCH] feat: show all active locations when listing source files Also, use codespan-reporting when possible --- Cargo.lock | 1 + tooling/debugger/Cargo.toml | 1 + tooling/debugger/src/lib.rs | 94 ++++++++++++++++--------------------- 3 files changed, 42 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9133f29f46c..a95721fd8ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2529,6 +2529,7 @@ name = "noir_debugger" version = "0.16.0" dependencies = [ "acvm", + "codespan-reporting", "easy-repl", "fm", "nargo", diff --git a/tooling/debugger/Cargo.toml b/tooling/debugger/Cargo.toml index a70d7a44066..560b3cfa736 100644 --- a/tooling/debugger/Cargo.toml +++ b/tooling/debugger/Cargo.toml @@ -15,5 +15,6 @@ fm.workspace = true noirc_printable_type.workspace = true noirc_errors.workspace = true noirc_driver.workspace = true +codespan-reporting.workspace = true thiserror.workspace = true easy-repl = "0.2.1" \ No newline at end of file diff --git a/tooling/debugger/src/lib.rs b/tooling/debugger/src/lib.rs index 9b85f47de27..6afead89ac3 100644 --- a/tooling/debugger/src/lib.rs +++ b/tooling/debugger/src/lib.rs @@ -11,69 +11,46 @@ use nargo::ops::ForeignCallExecutor; use easy_repl::{command, CommandStatus, Critical, Repl}; use fm::FileId; -use noirc_driver::DebugFile; use noirc_errors::Location; use std::cell::{Cell, RefCell}; use std::collections::HashSet; +use codespan_reporting::files::{Files, SimpleFile}; + enum SolveResult { Done, Ok, } -fn print_with_line_numbers(contents: &str, current: usize) { - for (number, line) in contents.lines().enumerate() { - let number = number + 1; - let marker = if current == number { "->" } else { "" }; +fn print_with_line_numbers(contents: &str, marker_indices: HashSet) { + for (index, line) in contents.lines().enumerate() { + let number = index + 1; + let marker = if marker_indices.contains(&index) { "->" } else { "" }; println!("{:>3} {:2} {}", number, marker, line); } } -fn find_line_number(contents: &str, start: usize) -> usize { - if start > contents.len() { - 0 - } else { - let mut linenum = 1; - let mut skipped = 0; - for line in contents.split_inclusive('\n') { - if start < skipped + line.len() { - return linenum; - } - linenum += 1; - skipped += line.len(); - } - linenum - } -} - -fn find_line_number_in_file( +fn get_indices_for_locations( + debug_artifact: &DebugArtifact, + file_id: FileId, locations: &Option>, - file_id: &FileId, - file: &DebugFile, -) -> usize { - if let Some(ref locations) = locations { - if let Some(this_file_loc) = &locations.iter().find(|l| l.file == *file_id) { - find_line_number(file.source.as_str(), this_file_loc.span.start() as usize) - } else { - 0 +) -> HashSet { + let Some(ref locations) = locations else { + return HashSet::new(); + }; + let filename = Files::name(debug_artifact, file_id).unwrap(); + let source = Files::source(debug_artifact, file_id).unwrap(); + let file = SimpleFile::new(filename, source); + let mut markers = HashSet::new(); + for loc in locations { + if loc.file != file_id { + continue; + }; + if let Ok(index) = file.line_index((), loc.span.start() as usize) { + markers.insert(index); } - } else { - 0 - } -} - -#[cfg(test)] -mod tests { - use crate::find_line_number; - - #[test] - fn test_find_line_number() { - assert_eq!(1, find_line_number("", 0)); - assert_eq!(1, find_line_number("\n", 0)); - assert_eq!(0, find_line_number("\n", 2)); - assert_eq!(1, find_line_number("foo\nbar\n", 3)); - assert_eq!(2, find_line_number("foo\nbar\n", 4)); } + markers } struct DebugContext<'backend, B: BlackBoxFunctionSolver> { @@ -164,11 +141,17 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { let locations = debug_artifact.debug_symbols[0].opcode_location(location); if let Some(locations) = locations { for loc in locations { - let file = &debug_artifact.file_map[&loc.file]; - let source = &file.source.as_str(); + let source = Files::source(debug_artifact, loc.file).unwrap(); let start = loc.span.start() as usize; let end = loc.span.end() as usize; - println!("At {}.nr:{start}-{end}", file.path.as_path().display()); + let line_index = Files::line_index(debug_artifact, loc.file, start).unwrap(); + let line_number = Files::line_number(debug_artifact, loc.file, line_index).unwrap(); + let column_number = + Files::column_number(debug_artifact, loc.file, line_index, start).unwrap(); + println!( + "At {}.nr:{line_number}:{column_number}", + Files::name(debug_artifact, loc.file).unwrap() + ); println!("\n{}\n", &source[start..end]); } } @@ -192,10 +175,13 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { fn display_sources(&self) { let locations = self.get_current_debug_locations(); - for (file_id, file) in &self.debug_artifact.file_map { - let current_line_number = find_line_number_in_file(&locations, file_id, file); - println!("File {}.nr:", file.path.as_path().display()); - print_with_line_numbers(file.source.as_str(), current_line_number); + for (file_id, _) in &self.debug_artifact.file_map { + let filename = Files::name(&self.debug_artifact, *file_id).unwrap(); + let source = Files::source(&self.debug_artifact, *file_id).unwrap(); + let marker_indices = + get_indices_for_locations(&self.debug_artifact, *file_id, &locations); + println!("File {filename}.nr:"); + print_with_line_numbers(source, marker_indices); } }