Skip to content

Commit 55c0767

Browse files
committed
refactor ignore- and only- to use decisions
1 parent ffe4ccd commit 55c0767

File tree

2 files changed

+50
-53
lines changed

2 files changed

+50
-53
lines changed

src/tools/compiletest/src/header.rs

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,6 @@ impl Config {
660660
}
661661
}
662662

663-
fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
664-
// returns whether this line contains this prefix or not. For prefix
665-
// "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
666-
// "ignore-android" etc.
667-
line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-')
668-
}
669-
670663
fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
671664
// Ensure the directive is a whole word. Do not match "ignore-x86" when
672665
// the line says "ignore-x86_64".
@@ -878,7 +871,7 @@ pub fn make_test_description<R: Read>(
878871
// The ignore reason must be a &'static str, so we have to leak memory to
879872
// create it. This is fine, as the header is parsed only at the start of
880873
// compiletest so it won't grow indefinitely.
881-
ignore_message = Some(Box::leak(Box::<str>::from(reason)));
874+
ignore_message = Some(&*Box::leak(Box::<str>::from(reason)));
882875
}
883876
IgnoreDecision::Error { message } => {
884877
eprintln!("error: {}: {message}", path.display());
@@ -889,46 +882,8 @@ pub fn make_test_description<R: Read>(
889882
};
890883
}
891884

892-
{
893-
let parsed = parse_cfg_name_directive(config, ln, "ignore");
894-
ignore = match parsed.outcome {
895-
MatchOutcome::Match => {
896-
let reason = parsed.pretty_reason.unwrap();
897-
// The ignore reason must be a &'static str, so we have to leak memory to
898-
// create it. This is fine, as the header is parsed only at the start of
899-
// compiletest so it won't grow indefinitely.
900-
ignore_message = Some(Box::leak(Box::<str>::from(match parsed.comment {
901-
Some(comment) => format!("ignored {reason} ({comment})"),
902-
None => format!("ignored {reason}"),
903-
})) as &str);
904-
true
905-
}
906-
MatchOutcome::NoMatch => ignore,
907-
MatchOutcome::External => ignore,
908-
MatchOutcome::Invalid => panic!("invalid line in {}: {ln}", path.display()),
909-
};
910-
}
911-
912-
if config.has_cfg_prefix(ln, "only") {
913-
let parsed = parse_cfg_name_directive(config, ln, "only");
914-
ignore = match parsed.outcome {
915-
MatchOutcome::Match => ignore,
916-
MatchOutcome::NoMatch => {
917-
let reason = parsed.pretty_reason.unwrap();
918-
// The ignore reason must be a &'static str, so we have to leak memory to
919-
// create it. This is fine, as the header is parsed only at the start of
920-
// compiletest so it won't grow indefinitely.
921-
ignore_message = Some(Box::leak(Box::<str>::from(match parsed.comment {
922-
Some(comment) => format!("only executed {reason} ({comment})"),
923-
None => format!("only executed {reason}"),
924-
})) as &str);
925-
true
926-
}
927-
MatchOutcome::External => ignore,
928-
MatchOutcome::Invalid => panic!("invalid line in {}: {ln}", path.display()),
929-
};
930-
}
931-
885+
decision!(cfg::handle_ignore(config, ln));
886+
decision!(cfg::handle_only(config, ln));
932887
decision!(needs::handle_needs(&needs_cache, config, ln));
933888
decision!(ignore_llvm(config, ln));
934889
decision!(ignore_cdb(config, ln));

src/tools/compiletest/src/header/cfg.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11
use crate::common::{CompareMode, Config, Debugger};
2+
use crate::header::IgnoreDecision;
23
use std::collections::HashSet;
34

45
const EXTRA_ARCHS: &[&str] = &["spirv"];
56

7+
pub(super) fn handle_ignore(config: &Config, line: &str) -> IgnoreDecision {
8+
let parsed = parse_cfg_name_directive(config, line, "ignore");
9+
match parsed.outcome {
10+
MatchOutcome::NoMatch => IgnoreDecision::Continue,
11+
MatchOutcome::Match => IgnoreDecision::Ignore {
12+
reason: match parsed.comment {
13+
Some(comment) => format!("ignored {} ({comment})", parsed.pretty_reason.unwrap()),
14+
None => format!("ignored {}", parsed.pretty_reason.unwrap()),
15+
},
16+
},
17+
MatchOutcome::Invalid => IgnoreDecision::Error { message: format!("invalid line: {line}") },
18+
MatchOutcome::External => IgnoreDecision::Continue,
19+
MatchOutcome::NotADirective => IgnoreDecision::Continue,
20+
}
21+
}
22+
23+
pub(super) fn handle_only(config: &Config, line: &str) -> IgnoreDecision {
24+
let parsed = parse_cfg_name_directive(config, line, "only");
25+
match parsed.outcome {
26+
MatchOutcome::Match => IgnoreDecision::Continue,
27+
MatchOutcome::NoMatch => IgnoreDecision::Ignore {
28+
reason: match parsed.comment {
29+
Some(comment) => {
30+
format!("only executed {} ({comment})", parsed.pretty_reason.unwrap())
31+
}
32+
None => format!("only executed {}", parsed.pretty_reason.unwrap()),
33+
},
34+
},
35+
MatchOutcome::Invalid => IgnoreDecision::Error { message: format!("invalid line: {line}") },
36+
MatchOutcome::External => IgnoreDecision::Continue,
37+
MatchOutcome::NotADirective => IgnoreDecision::Continue,
38+
}
39+
}
40+
641
/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
742
/// or `normalize-stderr-32bit`.
843
pub(super) fn parse_cfg_name_directive<'a>(
@@ -11,10 +46,10 @@ pub(super) fn parse_cfg_name_directive<'a>(
1146
prefix: &str,
1247
) -> ParsedNameDirective<'a> {
1348
if !line.as_bytes().starts_with(prefix.as_bytes()) {
14-
return ParsedNameDirective::invalid();
49+
return ParsedNameDirective::not_a_directive();
1550
}
1651
if line.as_bytes().get(prefix.len()) != Some(&b'-') {
17-
return ParsedNameDirective::invalid();
52+
return ParsedNameDirective::not_a_directive();
1853
}
1954
let line = &line[prefix.len() + 1..];
2055

@@ -24,7 +59,7 @@ pub(super) fn parse_cfg_name_directive<'a>(
2459
// Some of the matchers might be "" depending on what the target information is. To avoid
2560
// problems we outright reject empty directives.
2661
if name == "" {
27-
return ParsedNameDirective::invalid();
62+
return ParsedNameDirective::not_a_directive();
2863
}
2964

3065
let mut outcome = MatchOutcome::Invalid;
@@ -218,8 +253,13 @@ pub(super) struct ParsedNameDirective<'a> {
218253
}
219254

220255
impl ParsedNameDirective<'_> {
221-
fn invalid() -> Self {
222-
Self { name: None, pretty_reason: None, comment: None, outcome: MatchOutcome::NoMatch }
256+
fn not_a_directive() -> Self {
257+
Self {
258+
name: None,
259+
pretty_reason: None,
260+
comment: None,
261+
outcome: MatchOutcome::NotADirective,
262+
}
223263
}
224264
}
225265

@@ -233,6 +273,8 @@ pub(super) enum MatchOutcome {
233273
Invalid,
234274
/// The directive is handled by other parts of our tooling.
235275
External,
276+
/// The line is not actually a directive.
277+
NotADirective,
236278
}
237279

238280
trait CustomContains {

0 commit comments

Comments
 (0)