Skip to content

Commit 9cb4373

Browse files
committed
move cfg handling into its own module
1 parent 8eb3def commit 9cb4373

File tree

2 files changed

+239
-231
lines changed

2 files changed

+239
-231
lines changed

src/tools/compiletest/src/header.rs

Lines changed: 6 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,16 @@ use std::process::Command;
88

99
use tracing::*;
1010

11-
use crate::common::CompareMode;
1211
use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
12+
use crate::header::cfg::parse_cfg_name_directive;
13+
use crate::header::cfg::MatchOutcome;
1314
use crate::util;
1415
use crate::{extract_cdb_version, extract_gdb_version};
1516

17+
mod cfg;
1618
#[cfg(test)]
1719
mod tests;
1820

19-
/// The result of parse_cfg_name_directive.
20-
#[derive(Clone, PartialEq, Debug)]
21-
struct ParsedNameDirective<'a> {
22-
name: Option<&'a str>,
23-
pretty_reason: Option<String>,
24-
comment: Option<&'a str>,
25-
outcome: MatchOutcome,
26-
}
27-
28-
impl ParsedNameDirective<'_> {
29-
fn invalid() -> Self {
30-
Self { name: None, pretty_reason: None, comment: None, outcome: MatchOutcome::NoMatch }
31-
}
32-
}
33-
34-
#[derive(Clone, Copy, PartialEq, Debug)]
35-
enum MatchOutcome {
36-
/// No match.
37-
NoMatch,
38-
/// Match.
39-
Match,
40-
/// The directive was invalid.
41-
Invalid,
42-
/// The directive is handled by other parts of our tooling.
43-
External,
44-
}
45-
4621
/// Properties which must be known very early, before actually running
4722
/// the test.
4823
#[derive(Default)]
@@ -666,7 +641,7 @@ impl Config {
666641
}
667642

668643
fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
669-
if self.parse_cfg_name_directive(line, prefix).outcome == MatchOutcome::Match {
644+
if parse_cfg_name_directive(self, line, prefix).outcome == MatchOutcome::Match {
670645
let from = parse_normalization_string(&mut line)?;
671646
let to = parse_normalization_string(&mut line)?;
672647
Some((from, to))
@@ -683,206 +658,6 @@ impl Config {
683658
self.parse_name_directive(line, "needs-profiler-support")
684659
}
685660

686-
/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
687-
/// or `normalize-stderr-32bit`.
688-
fn parse_cfg_name_directive<'a>(&self, line: &'a str, prefix: &str) -> ParsedNameDirective<'a> {
689-
if !line.as_bytes().starts_with(prefix.as_bytes()) {
690-
return ParsedNameDirective::invalid();
691-
}
692-
if line.as_bytes().get(prefix.len()) != Some(&b'-') {
693-
return ParsedNameDirective::invalid();
694-
}
695-
let line = &line[prefix.len() + 1..];
696-
697-
let (name, comment) =
698-
line.split_once(&[':', ' ']).map(|(l, c)| (l, Some(c))).unwrap_or((line, None));
699-
700-
// Some of the matchers might be "" depending on what the target information is. To avoid
701-
// problems we outright reject empty directives.
702-
if name == "" {
703-
return ParsedNameDirective::invalid();
704-
}
705-
706-
let mut outcome = MatchOutcome::Invalid;
707-
let mut message = None;
708-
709-
macro_rules! maybe_condition {
710-
(
711-
name: $name:expr,
712-
$(allowed_names: $allowed_names:expr,)?
713-
$(condition: $condition:expr,)?
714-
message: $($message:tt)*
715-
) => {{
716-
// This is not inlined to avoid problems with macro repetitions.
717-
let format_message = || format!($($message)*);
718-
719-
if outcome != MatchOutcome::Invalid {
720-
// Ignore all other matches if we already found one
721-
} else if $name.as_ref().map(|n| n == &name).unwrap_or(false) {
722-
message = Some(format_message());
723-
if true $(&& $condition)? {
724-
outcome = MatchOutcome::Match;
725-
} else {
726-
outcome = MatchOutcome::NoMatch;
727-
}
728-
}
729-
$(else if $allowed_names.contains(name) {
730-
message = Some(format_message());
731-
outcome = MatchOutcome::NoMatch;
732-
})?
733-
}};
734-
}
735-
macro_rules! condition {
736-
(
737-
name: $name:expr,
738-
$(allowed_names: $allowed_names:expr,)?
739-
$(condition: $condition:expr,)?
740-
message: $($message:tt)*
741-
) => {
742-
maybe_condition! {
743-
name: Some($name),
744-
$(allowed_names: $allowed_names,)*
745-
$(condition: $condition,)*
746-
message: $($message)*
747-
}
748-
};
749-
}
750-
macro_rules! hashset {
751-
($($value:expr),* $(,)?) => {{
752-
let mut set = HashSet::new();
753-
$(set.insert($value);)*
754-
set
755-
}}
756-
}
757-
758-
let target_cfgs = self.target_cfgs();
759-
let target_cfg = self.target_cfg();
760-
761-
condition! {
762-
name: "test",
763-
message: "always"
764-
}
765-
condition! {
766-
name: &self.target,
767-
allowed_names: &target_cfgs.all_targets,
768-
message: "when the target is {name}"
769-
}
770-
condition! {
771-
name: &target_cfg.os,
772-
allowed_names: &target_cfgs.all_oses,
773-
message: "when the operative system is {name}"
774-
}
775-
condition! {
776-
name: &target_cfg.env,
777-
allowed_names: &target_cfgs.all_envs,
778-
message: "when the target environment is {name}"
779-
}
780-
condition! {
781-
name: &target_cfg.abi,
782-
allowed_names: &target_cfgs.all_abis,
783-
message: "when the ABI is {name}"
784-
}
785-
condition! {
786-
name: &target_cfg.arch,
787-
allowed_names: &target_cfgs.all_archs,
788-
message: "when the architecture is {name}"
789-
}
790-
condition! {
791-
name: format!("{}bit", target_cfg.pointer_width),
792-
allowed_names: &target_cfgs.all_pointer_widths,
793-
message: "when the pointer width is {name}"
794-
}
795-
for family in &target_cfg.families {
796-
condition! {
797-
name: family,
798-
allowed_names: &target_cfgs.all_families,
799-
message: "when the target family is {name}"
800-
}
801-
}
802-
803-
// If something is ignored for emscripten, it likely also needs to be
804-
// ignored for wasm32-unknown-unknown.
805-
// `wasm32-bare` is an alias to refer to just wasm32-unknown-unknown
806-
// (in contrast to `wasm32` which also matches non-bare targets like
807-
// asmjs-unknown-emscripten).
808-
condition! {
809-
name: "emscripten",
810-
condition: self.target == "wasm32-unknown-unknown",
811-
message: "when the target is WASM",
812-
}
813-
condition! {
814-
name: "wasm32-bare",
815-
condition: self.target == "wasm32-unknown-unknown",
816-
message: "when the target is WASM"
817-
}
818-
819-
condition! {
820-
name: &self.channel,
821-
allowed_names: hashset!["stable", "beta", "nightly"],
822-
message: "when the release channel is {name}",
823-
}
824-
condition! {
825-
name: "cross-compile",
826-
condition: self.target != self.host,
827-
message: "when cross-compiling"
828-
}
829-
condition! {
830-
name: "endian-big",
831-
condition: self.is_big_endian(),
832-
message: "on big-endian targets",
833-
}
834-
condition! {
835-
name: self.stage_id.split('-').next().unwrap(),
836-
allowed_names: hashset!["stable", "beta", "nightly"],
837-
message: "when the bootstrapping stage is {name}",
838-
}
839-
condition! {
840-
name: "remote",
841-
condition: self.remote_test_client.is_some(),
842-
message: "when running tests remotely",
843-
}
844-
condition! {
845-
name: "debug",
846-
condition: cfg!(debug_assertions),
847-
message: "when building with debug assertions",
848-
}
849-
maybe_condition! {
850-
name: self.debugger.as_ref().map(|d| d.to_str()),
851-
allowed_names: Debugger::VARIANTS
852-
.iter()
853-
.map(|v| v.to_str())
854-
.collect::<HashSet<_>>(),
855-
message: "when the debugger is {name}",
856-
}
857-
maybe_condition! {
858-
name: self.compare_mode
859-
.as_ref()
860-
.map(|d| format!("compare-mode-{}", d.to_str())),
861-
allowed_names: CompareMode::VARIANTS
862-
.iter()
863-
.map(|cm| format!("compare-mode-{}", cm.to_str()))
864-
.collect::<HashSet<_>>(),
865-
message: "when comparing with {name}",
866-
}
867-
868-
// Don't error out for ignore-tidy-* diretives, as those are not handled by compiletest.
869-
if prefix == "ignore" && name.starts_with("tidy-") && outcome == MatchOutcome::Invalid {
870-
outcome = MatchOutcome::External;
871-
}
872-
873-
// Don't error out for ignore-pass, as that is handled elsewhere.
874-
if prefix == "ignore" && name == "pass" && outcome == MatchOutcome::Invalid {
875-
outcome = MatchOutcome::External;
876-
}
877-
878-
ParsedNameDirective {
879-
name: Some(name),
880-
comment: comment.map(|c| c.trim().trim_start_matches('-').trim()),
881-
outcome,
882-
pretty_reason: message,
883-
}
884-
}
885-
886661
fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
887662
// returns whether this line contains this prefix or not. For prefix
888663
// "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
@@ -1151,7 +926,7 @@ pub fn make_test_description<R: Read>(
1151926
}
1152927

1153928
{
1154-
let parsed = config.parse_cfg_name_directive(ln, "ignore");
929+
let parsed = parse_cfg_name_directive(config, ln, "ignore");
1155930
ignore = match parsed.outcome {
1156931
MatchOutcome::Match => {
1157932
let reason = parsed.pretty_reason.unwrap();
@@ -1171,7 +946,7 @@ pub fn make_test_description<R: Read>(
1171946
}
1172947

1173948
if config.has_cfg_prefix(ln, "only") {
1174-
let parsed = config.parse_cfg_name_directive(ln, "only");
949+
let parsed = parse_cfg_name_directive(config, ln, "only");
1175950
ignore = match parsed.outcome {
1176951
MatchOutcome::Match => ignore,
1177952
MatchOutcome::NoMatch => {

0 commit comments

Comments
 (0)