Skip to content

Commit

Permalink
Don't use parse_cfg_name_directive for normalize directives
Browse files Browse the repository at this point in the history
This is a little more verbose, but also more explicit, and avoids invoking the
full condition engine when only the pointer-width conditions are used.
  • Loading branch information
Zalathar committed Dec 25, 2024
1 parent d6f4ac1 commit 71a3575
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
57 changes: 43 additions & 14 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use tracing::*;
use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
use crate::debuggers::{extract_cdb_version, extract_gdb_version};
use crate::header::auxiliary::{AuxProps, parse_and_update_aux};
use crate::header::cfg::{MatchOutcome, parse_cfg_name_directive};
use crate::header::needs::CachedNeedsConditions;
use crate::util::static_regex;

Expand Down Expand Up @@ -472,11 +471,24 @@ impl TestProps {

config.set_name_directive(ln, IGNORE_PASS, &mut self.ignore_pass);

if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
self.normalize_stdout.push(rule);
}
if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
self.normalize_stderr.push(rule);
if let Some(NormalizeRule { kind, regex, replacement }) =
config.parse_custom_normalization(ln)
{
let rule_tuple = (regex, replacement);
match kind {
NormalizeKind::Stdout => self.normalize_stdout.push(rule_tuple),
NormalizeKind::Stderr => self.normalize_stderr.push(rule_tuple),
NormalizeKind::Stderr32bit => {
if config.target_cfg().pointer_width == 32 {
self.normalize_stderr.push(rule_tuple);
}
}
NormalizeKind::Stderr64bit => {
if config.target_cfg().pointer_width == 64 {
self.normalize_stderr.push(rule_tuple);
}
}
}
}

if let Some(code) = config
Expand Down Expand Up @@ -966,20 +978,24 @@ impl Config {
}
}

fn parse_custom_normalization(&self, line: &str, prefix: &str) -> Option<(String, String)> {
let parsed = parse_cfg_name_directive(self, line, prefix);
if parsed.outcome != MatchOutcome::Match {
return None;
}
let name = parsed.name.expect("successful match always has a name");
fn parse_custom_normalization(&self, line: &str) -> Option<NormalizeRule> {
let directive_name = line.split_once(':')?.0;

let kind = match directive_name {
"normalize-stdout-test" => NormalizeKind::Stdout,
"normalize-stderr-test" => NormalizeKind::Stderr,
"normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
"normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
_ => return None,
};

let Some((regex, replacement)) = parse_normalize_rule(line) else {
panic!(
"couldn't parse custom normalization rule: `{line}`\n\
help: expected syntax is: `{prefix}-{name}: \"REGEX\" -> \"REPLACEMENT\"`"
help: expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`"
);
};
Some((regex, replacement))
Some(NormalizeRule { kind, regex, replacement })
}

fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
Expand Down Expand Up @@ -1105,6 +1121,19 @@ fn expand_variables(mut value: String, config: &Config) -> String {
value
}

struct NormalizeRule {
kind: NormalizeKind,
regex: String,
replacement: String,
}

enum NormalizeKind {
Stdout,
Stderr,
Stderr32bit,
Stderr64bit,
}

/// Parses the regex and replacement values of a `//@ normalize-*` header,
/// in the format:
/// ```text
Expand Down
4 changes: 2 additions & 2 deletions src/tools/compiletest/src/header/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub(super) fn handle_only(config: &Config, line: &str) -> IgnoreDecision {
}

/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
/// or `normalize-stderr-32bit`.
pub(super) fn parse_cfg_name_directive<'a>(
/// or `only-windows`.
fn parse_cfg_name_directive<'a>(
config: &Config,
line: &'a str,
prefix: &str,
Expand Down

0 comments on commit 71a3575

Please sign in to comment.