Reduce repeated macro tracing and input position work

This commit is contained in:
gloamxun committed 2026-11-21 10:54:11 +08:00
1 parent 70822cbd94
commit b681f9791a
5 files changed
+64 -29

No files matched your search

+16 -2
View File
@@ -204,6 +204,7 @@ pub struct Executor {
generated_inputs: std::collections::BTreeMap<usize, GeneratedInput>,
reads: BTreeSet<Dependency>,
meaning_reads: HashSet<CommandName>,
reads_tracing_macros: bool,
conditionals: Vec<Conditional>,
in_csname: bool,
command_trace_mode: Option<&'static str>,
@@ -373,6 +374,7 @@ impl Executor {
generated_inputs: Default::default(),
reads: BTreeSet::new(),
meaning_reads: HashSet::new(),
reads_tracing_macros: false,
conditionals: Vec::new(),
in_csname: false,
command_trace_mode: None,
@@ -1791,7 +1793,17 @@ impl Executor {
call: &Token,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
self.trace_macro_call(&definition, call, files)?;
// A macro only scans raw tokens while matching its arguments, so
// assignments cannot change the trace level during this call.
if !self.reads_tracing_macros {
self.reads
.insert(Dependency::Parameter(Parameter::Tracingmacros));
self.reads_tracing_macros = true;
}
let trace = self.state.parameter(Parameter::Tracingmacros).width() > 0;
if trace {
self.trace_macro_call(&definition, call, files)?;
}
for expected in &definition.prefix {
if self.required_raw(files)?.token.kind != *expected {
return self.fail("macro prefix does not match");
@@ -1860,7 +1872,9 @@ impl Executor {
}
argument = Self::strip_outer_group(argument);
}
self.trace_macro_argument(&definition, index, &argument, files)?;
if trace {
self.trace_macro_argument(&definition, index, &argument, files)?;
}
arguments.push(argument);
}
self.discard_exhausted_tokens();
@@ -635,12 +635,9 @@ impl Executor {
call: &Token,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if self.read_parameter(Parameter::Tracingmacros).width() > 0 {
let command = self.message_token(call);
let body = self.macro_token_text(definition, call.span)?;
self.emit_macro_trace(&format!("{command}{body}"), true, files)?;
}
Ok(())
let command = self.message_token(call);
let body = self.macro_token_text(definition, call.span)?;
self.emit_macro_trace(&format!("{command}{body}"), true, files)
}
pub(super) fn trace_macro_argument(
@@ -650,28 +647,24 @@ impl Executor {
argument: &[Token],
files: &mut impl SourceProvider,
) -> Result<(), Error> {
if self.read_parameter(Parameter::Tracingmacros).width() > 0 {
let mut body = format!("{}{}<-", definition.parameter_chars[index], index + 1);
let mut tally = 0;
for token in argument {
if tally >= 1000 {
body.push_str(
&self
.string_token(&TokenKind::Command(CommandName::Control("ETC.".into()))),
);
break;
}
let piece = self.message_token(token);
tally += piece.chars().count();
if let TokenKind::Character(c, CatCode::Parameter) = token.kind {
body.push(c);
tally += 1;
}
body.push_str(&piece);
let mut body = format!("{}{}<-", definition.parameter_chars[index], index + 1);
let mut tally = 0;
for token in argument {
if tally >= 1000 {
body.push_str(
&self.string_token(&TokenKind::Command(CommandName::Control("ETC.".into()))),
);
break;
}
self.emit_macro_trace(&body, false, files)?;
let piece = self.message_token(token);
tally += piece.chars().count();
if let TokenKind::Character(c, CatCode::Parameter) = token.kind {
body.push(c);
tally += 1;
}
body.push_str(&piece);
}
Ok(())
self.emit_macro_trace(&body, false, files)
}
fn emit_macro_trace(
@@ -148,9 +148,9 @@ impl Executor {
self.inputs.pop();
self.restore_input_position();
}
// Only a source frame changes the current physical line.
Next::Token(None) => {
self.inputs.pop();
self.restore_input_position();
}
Next::Argument(tokens, call) => {
if !tokens.is_empty() {
@@ -60,6 +60,11 @@ fn macro_trace_follows_real_calls_and_parameters_in_each_channel() {
&mut Host,
)
.unwrap();
assert!(
result
.reads
.contains(&Dependency::Parameter(Parameter::Tracingmacros))
);
let mut native_log = String::new();
let mut native_terminal = String::new();
for event in result.events {