From 5a101a96ada9ddbd4be54c46cd7d0125825c2283 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 6 Apr 2021 14:11:43 -0600 Subject: eagerly expand when appending to eagerly-expanded macros --- src/makefile/input.rs | 56 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 19 deletions(-) (limited to 'src/makefile/input.rs') diff --git a/src/makefile/input.rs b/src/makefile/input.rs index 5dfaed7..5883590 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -16,7 +16,7 @@ use super::command_line::CommandLine; #[cfg(feature = "full")] use super::conditional::{Line as ConditionalLine, State as ConditionalState}; use super::inference_rules::InferenceRule; -use super::r#macro::{Set as MacroSet, Source as MacroSource}; +use super::r#macro::{Macro, Set as MacroSet, Source as MacroSource}; use super::target::Target; use super::token::{tokenize, Token, TokenString}; @@ -162,21 +162,21 @@ impl<'a, 'parent> MakefileReader<'a, 'parent, BufReader> { path: impl AsRef, ) -> Result { #[cfg(feature = "full")] - if let Some((_, mut old_makefile_list)) = macros.pop("MAKEFILE_LIST") { - old_makefile_list.extend(TokenString::text(format!( + if let Some(mut old_makefile_list) = macros.pop("MAKEFILE_LIST") { + old_makefile_list.text.extend(TokenString::text(format!( " {}", path.as_ref().to_string_lossy() ))); - macros.set( - "MAKEFILE_LIST".to_owned(), - MacroSource::Builtin, - old_makefile_list, - ); + macros.set("MAKEFILE_LIST".to_owned(), old_makefile_list); } else { macros.set( "MAKEFILE_LIST".to_owned(), - MacroSource::Builtin, - TokenString::text(path.as_ref().to_string_lossy()), + Macro { + source: MacroSource::Builtin, + text: TokenString::text(path.as_ref().to_string_lossy()), + #[cfg(feature = "full")] + eagerly_expanded: false, + }, ); } let file = File::open(path); @@ -606,23 +606,41 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { match self.macros.get(name) { // We always let command line or MAKEFLAGS macros override macros from the file. - Some((MacroSource::CommandLineOrMakeflags, _)) => return Ok(()), + Some(Macro { + source: MacroSource::CommandLineOrMakeflags, + .. + }) => return Ok(()), // We let environment variables override macros from the file only if the command-line argument to do that was given - Some((MacroSource::Environment, _)) if self.args.environment_overrides => return Ok(()), + Some(Macro { + source: MacroSource::Environment, + .. + }) if self.args.environment_overrides => return Ok(()), Some(_) if skip_if_defined => return Ok(()), _ => {} } let value = match self.macros.pop(name) { - Some((_, mut old_value)) if append => { - // TODO eagerly expand if appending to eagerly-expanded macro - old_value.extend(TokenString::text(" ")); - old_value.extend(value); + Some(mut old_value) if append => { + #[cfg(feature = "full")] + let value = if old_value.eagerly_expanded { + TokenString::text(self.expand_macros(&value).wrap_err_with(|| { + format!("while defining {} on line {}", name, line_number) + })?) + } else { + value + }; + old_value.text.extend(TokenString::text(" ")); + old_value.text.extend(value); old_value } - _ => value, + _ => Macro { + source: MacroSource::File, + text: value, + #[cfg(feature = "full")] + eagerly_expanded: expand_value, + }, }; - self.macros.set(name.into(), MacroSource::File, value); + self.macros.set(name.into(), value); Ok(()) } @@ -652,7 +670,7 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { pub struct FinishedMakefileReader { pub inference_rules: Vec, - pub macros: HashMap, + pub macros: HashMap, pub targets: HashMap, pub first_non_special_target: Option, } -- cgit v1.2.3