Skip to content

Commit

Permalink
feat: show all active locations when listing source files
Browse files Browse the repository at this point in the history
Also, use codespan-reporting when possible
  • Loading branch information
ggiraldez committed Oct 16, 2023
1 parent 8111bea commit 1d41d39
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 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.

1 change: 1 addition & 0 deletions tooling/debugger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
94 changes: 40 additions & 54 deletions tooling/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) {
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<Vec<Location>>,
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<usize> {
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> {
Expand Down Expand Up @@ -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]);
}
}
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 1d41d39

Please sign in to comment.