Skip to content

Commit

Permalink
chore: add ModuleGraph::display
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Dec 22, 2023
1 parent 03a45de commit 2f92452
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/erg_compiler/build_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
log!(info "Start dependency resolution process");
let _ = self.resolve(&mut ast, &cfg);
log!(info "Dependency resolution process completed");
log!("graph:\n{}", self.shared.graph.display());
if self.parse_errors.errors.is_empty() {
self.shared.warns.extend(self.parse_errors.warns.flush());
} else {
Expand Down
40 changes: 40 additions & 0 deletions crates/erg_compiler/module/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ impl ModuleGraph {
pub fn initialize(&mut self) {
self.0.clear();
}

pub fn display_parents(
&self,
lev: usize,
id: &NormalizedPathBuf,
appeared: &mut Set<NormalizedPathBuf>,
) -> String {
let mut s = String::new();
let Some(parents) = self.parents(id) else {
return s;
};
for parent in parents.iter() {
s.push_str(&format!("{}-> {}\n", " ".repeat(lev), parent.display()));
if appeared.contains(parent) {
continue;
}
s.push_str(&self.display_parents(lev + 1, parent, appeared));
appeared.insert(parent.clone());
}
s
}

pub fn display(&self) -> String {
let mut s = String::new();
let mut appeared = set! {};
for node in self.0.iter() {
let children = self.children(&node.id);
if !children.is_empty() || appeared.contains(&node.id) {
continue;
}
s.push_str(&format!("{}\n", node.id.display()));
s.push_str(&self.display_parents(1, &node.id, &mut appeared));
appeared.insert(node.id.clone());
}
s
}
}

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -248,4 +284,8 @@ impl SharedModuleGraph {
pub fn clone_inner(&self) -> ModuleGraph {
self.0.borrow().clone()
}

pub fn display(&self) -> String {
self.0.borrow().display()
}
}

0 comments on commit 2f92452

Please sign in to comment.