From 1884c0484b71a197eb12e7e65354b30802035602 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 6 Apr 2021 16:48:26 -0600 Subject: include filename in some error logs --- src/main.rs | 2 +- src/makefile/input.rs | 50 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 4b86307..f0e0c1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ fn main() -> Result<()> { for filename in &args.makefile { if filename == &PathBuf::from("-") { let macros = makefile.macros.with_overlay(); - let file = MakefileReader::read(&args, macros, stdin().lock())?.finish(); + let file = MakefileReader::read(&args, macros, stdin().lock(), "-")?.finish(); makefile.extend(file)?; } else { let macros = makefile.macros.with_overlay(); diff --git a/src/makefile/input.rs b/src/makefile/input.rs index 283842f..2f8de49 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -145,6 +145,7 @@ impl>> I } pub struct MakefileReader<'a, 'parent, R: BufRead> { + file_name: String, pub inference_rules: Vec, pub macros: MacroSet<'parent, 'static>, pub targets: HashMap, @@ -182,17 +183,24 @@ impl<'a, 'parent> MakefileReader<'a, 'parent, BufReader> { }, ); } - let file = File::open(path); + let file = File::open(path.as_ref()); // TODO handle errors let file = file.context("couldn't open makefile!")?; let file_reader = BufReader::new(file); - Self::read(args, macros, file_reader) + Self::read(args, macros, file_reader, path.as_ref().to_string_lossy()) } } impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { - pub fn read(args: &'a Args, macros: MacroSet<'parent, 'static>, source: R) -> Result { + pub fn read( + args: &'a Args, + macros: MacroSet<'parent, 'static>, + source: R, + name: impl Into, + ) -> Result { + let name = name.into(); let mut reader = Self { + file_name: name.clone(), inference_rules: Vec::new(), macros, targets: HashMap::new(), @@ -220,7 +228,9 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { }, ); } - reader.read_all()?; + reader + .read_all() + .wrap_err_with(|| format!("while reading {}", name))?; Ok(reader) } @@ -250,7 +260,12 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { } else { if !original_line.starts_with('-') { // TODO handle non-file-not-found errors - log::error!("included makefile {} not found", field); + log::error!( + "{}:{}: included makefile {} not found", + &self.file_name, + line_number, + field + ); self.failed_includes.push(field.to_owned()); } } @@ -285,10 +300,14 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { let eval = self.macros.to_eval.take(); for eval in eval { let child_macros = self.macros.with_overlay(); - let child = - MakefileReader::read(self.args, child_macros, Cursor::new(eval)) - .context("while evaling")? - .finish(); + let child = MakefileReader::read( + self.args, + child_macros, + Cursor::new(eval), + "", + ) + .context("while evaling")? + .finish(); self.extend(child); } } @@ -727,7 +746,8 @@ a: $(x) b \\ \t\td \tfoo"; let args = Args::empty(); - let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?.finish(); + let makefile = + MakefileReader::read(&args, MacroSet::new(), Cursor::new(file), "")?.finish(); assert_eq!( makefile.targets["a"].prerequisites, vec!["3", "4", "5", "b", "c", "d"] @@ -748,7 +768,7 @@ worked = perhaps endif "; let args = Args::empty(); - let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?; + let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file), "")?; assert_eq!( makefile.expand_macros(&TokenString::r#macro("worked"))?, "yes" @@ -766,7 +786,7 @@ ifeq (1,1) endif "; let args = Args::empty(); - let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?; + let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file), "")?; assert_eq!(makefile.targets["a"].commands.len(), 1); Ok(()) } @@ -781,7 +801,7 @@ baz endef "; let args = Args::empty(); - let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?; + let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file), "")?; assert_eq!( makefile.expand_macros(&TokenString::r#macro("foo"))?, "bar\nbaz" @@ -803,7 +823,7 @@ endif FOO = bar "; let args = Args::empty(); - let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?; + let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file), "")?; assert_eq!(makefile.expand_macros(&TokenString::r#macro("FOO"))?, "bar",); Ok(()) } @@ -845,7 +865,7 @@ clean: "; let args = Args::empty(); - let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?; + let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file), "")?; assert!(makefile.targets.contains_key("server")); Ok(()) } -- cgit v1.2.3