From 44b3ea4dac1cc60eeda54ae7b53facbb025c6f68 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Wed, 13 Nov 2024 00:27:03 -0700 Subject: fix rule macro detection when macro expansion in macro name --- src/makefile/input.rs | 51 +++++++++++++++++++++++---------------------------- src/makefile/token.rs | 13 +++++++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/makefile/input.rs b/src/makefile/input.rs index 2c4a6bb..865b68b 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -22,7 +22,6 @@ use super::parse::{MacroAssignment, MacroAssignmentOutcome}; use super::r#macro::ExportConfig; use super::r#macro::Macro; use super::target::StaticTargetSet; -use super::token::Token; use super::{ builtin_targets, CommandLine, InferenceRule, InferenceRuleSet, ItemSource, LookupInternal, MacroScopeStack, MacroSet, Target, TokenString, @@ -58,35 +57,31 @@ impl LineType { if line_tokens.starts_with("unexport ") || line_tokens == "unexport" { return Self::Unexport; } - for token in line_tokens.tokens() { - if let Token::Text(text) = token { - let colon_idx = text.find(':'); - #[cfg(not(feature = "full"))] - let equals_idx = text.find('='); + let colon_idx = line_tokens.find(":"); + #[cfg(not(feature = "full"))] + let equals_idx = line_tokens.find('='); + #[cfg(feature = "full")] + let equals_idx = ["=", ":=", "::=", "?=", "+="] + .iter() + .filter_map(|p| line_tokens.find(p)) + .min(); + match (colon_idx, equals_idx) { + (Some(_), None) => { + return Self::Rule; + } + (Some(c), Some(e)) if c < e => { #[cfg(feature = "full")] - let equals_idx = ["=", ":=", "::=", "?=", "+="] - .iter() - .filter_map(|p| text.find(p)) - .min(); - match (colon_idx, equals_idx) { - (Some(_), None) => { - return Self::Rule; - } - (Some(c), Some(e)) if c < e => { - #[cfg(feature = "full")] - return Self::RuleMacro; - #[cfg(not(feature = "full"))] - return Self::Rule; - } - (None, Some(_)) => { - return Self::Macro; - } - (Some(c), Some(e)) if e <= c => { - return Self::Macro; - } - _ => {} - } + return Self::RuleMacro; + #[cfg(not(feature = "full"))] + return Self::Rule; + } + (None, Some(_)) => { + return Self::Macro; + } + (Some(c), Some(e)) if e <= c => { + return Self::Macro; } + _ => {} } Self::Unknown } diff --git a/src/makefile/token.rs b/src/makefile/token.rs index dad4694..7b88823 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -155,6 +155,19 @@ impl TokenString { } }) } + + /// Returns (token index within string, pattern index within token). + pub fn find(&self, pattern: &str) -> Option<(usize, usize)> { + self.0 + .iter() + .enumerate() + .find_map(|(token_index, token)| match token { + Token::Text(text) => text + .find(pattern) + .map(|pattern_index| (token_index, pattern_index)), + _ => None, + }) + } } impl fmt::Display for TokenString { -- cgit v1.2.3