[DEV-7133] Clearly show the cycles in the output

master
Joseph Frazer 2020-03-25 10:20:25 -04:00
parent 8af93d9339
commit 6386e096b4
2 changed files with 30 additions and 6 deletions

View File

@ -259,9 +259,8 @@ impl<Ix: Debug> std::fmt::Display for AsgError<Ix> {
Self::UnexpectedNode(msg) => {
write!(fmt, "unexpected node: {}", msg)
}
Self::Cycles(path) => {
write!(fmt, "Cyclic dependencies detected: {:?}", path)
// write!(fmt, "Cyclic dependencies detected:")
Self::Cycles(cycles) => {
write!(fmt, "Cyclic dependencies detected: {:?}", cycles)
}
}
}

View File

@ -22,8 +22,8 @@
use crate::global;
use crate::ir::asg::{
Asg, DefaultAsg, IdentKind, IdentObject, ObjectRef, Sections, SortableAsg,
Source,
Asg, AsgError, DefaultAsg, IdentKind, IdentObject, IdentObjectData,
ObjectRef, Sections, SortableAsg, Source,
};
use crate::obj::xmle::writer::XmleWriter;
use crate::obj::xmlo::reader::{XmloError, XmloEvent, XmloReader};
@ -78,7 +78,32 @@ pub fn main(package_path: &str, output: &str) -> Result<(), Box<dyn Error>> {
.filter_map(|sym| depgraph.lookup(sym)),
);
let mut sorted = depgraph.sort(&roots)?;
let mut sorted = match depgraph.sort(&roots) {
Ok(sections) => sections,
Err(AsgError::Cycles(cycles)) => {
let msg: Vec<String> = cycles
.into_iter()
.map(|cycle| {
let mut path: Vec<String> = cycle
.into_iter()
.map(|obj| {
format!(
"{}",
depgraph.get(obj).unwrap().name().unwrap()
)
})
.collect();
path.reverse();
path.push(path[0].clone());
format!("cycle: {}", path.join(" -> "))
})
.collect();
return Err(msg.join("\n").into());
}
Err(e) => return Err(e.into()),
};
//println!("Sorted ({}): {:?}", sorted.len(), sorted);