Skip to main content

noirc_driver/
debug.rs

1use fm::{FileId, FileManager};
2use noirc_errors::debug_info::DebugInfo;
3use serde::{Deserialize, Serialize};
4use std::{
5    collections::{BTreeMap, BTreeSet},
6    path::PathBuf,
7};
8
9/// For a given file, we store the source code and the path to the file
10/// so consumers of the debug artifact can reconstruct the original source code structure.
11#[derive(Clone, Debug, Serialize, Deserialize, Hash)]
12pub struct DebugFile {
13    pub source: String,
14    pub path: PathBuf,
15}
16
17pub(crate) fn filter_relevant_files(
18    debug_symbols: &[DebugInfo],
19    file_manager: &FileManager,
20) -> BTreeMap<FileId, DebugFile> {
21    let mut files_with_debug_symbols: BTreeSet<FileId> = debug_symbols
22        .iter()
23        .flat_map(|function_symbols| {
24            function_symbols.acir_locations.values().flat_map(|call_stack_id| {
25                function_symbols
26                    .location_tree
27                    .get_call_stack(*call_stack_id)
28                    .into_iter()
29                    .map(|location| location.file)
30            })
31        })
32        .collect();
33
34    let files_with_brillig_debug_symbols: BTreeSet<FileId> = debug_symbols
35        .iter()
36        .flat_map(|function_symbols| {
37            function_symbols.brillig_locations.values().flat_map(|brillig_location_map| {
38                brillig_location_map.values().flat_map(|call_stack_id| {
39                    function_symbols
40                        .location_tree
41                        .get_call_stack(*call_stack_id)
42                        .into_iter()
43                        .map(|location| location.file)
44                })
45            })
46        })
47        .collect();
48
49    files_with_debug_symbols.extend(files_with_brillig_debug_symbols);
50
51    let mut file_map = BTreeMap::new();
52
53    for file_id in files_with_debug_symbols {
54        let file_path = file_manager.path(file_id).expect("file should exist");
55        let file_source = file_manager.fetch_file(file_id).expect("file should exist");
56
57        file_map.insert(
58            file_id,
59            DebugFile { source: file_source.to_string(), path: file_path.to_path_buf() },
60        );
61    }
62    file_map
63}