From 2f924527f03d3ad6593e57b75bd0d1137159cf2c Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Fri, 22 Dec 2023 13:03:25 +0900 Subject: [PATCH] chore: add `ModuleGraph::display` --- crates/erg_compiler/build_package.rs | 1 + crates/erg_compiler/module/graph.rs | 40 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/crates/erg_compiler/build_package.rs b/crates/erg_compiler/build_package.rs index 3b5ab6008..7cc5c1918 100644 --- a/crates/erg_compiler/build_package.rs +++ b/crates/erg_compiler/build_package.rs @@ -336,6 +336,7 @@ impl 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 { diff --git a/crates/erg_compiler/module/graph.rs b/crates/erg_compiler/module/graph.rs index d3e979cb7..f67fee424 100644 --- a/crates/erg_compiler/module/graph.rs +++ b/crates/erg_compiler/module/graph.rs @@ -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, + ) -> 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)] @@ -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() + } }