Skip to content

Commit e600e46

Browse files
Change implementation per remaining review
1 parent bc315aa commit e600e46

File tree

3 files changed

+26
-104
lines changed

3 files changed

+26
-104
lines changed

src/tools/compiletest/src/header.rs

+25-40
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::fs::File;
44
use std::io::prelude::*;
55
use std::io::BufReader;
66
use std::path::{Path, PathBuf};
7+
use std::process::Command;
78

8-
use lazy_static::lazy_static;
9-
use regex::Regex;
10-
use tracing::*;
9+
use tracing::{self, *};
1110

1211
use crate::common::{CompareMode, Config, Debugger, FailMode, Mode, PanicStrategy, PassMode};
1312
use crate::util;
@@ -707,35 +706,9 @@ impl Config {
707706
}
708707

709708
fn evaluate_prop_for_target(&self, prop: &str, target: &str) -> EvaluatedProp {
710-
// This matches optional whitespace, followed by a group containing a series of word
711-
// characters (including '_' and '-'), followed optionally by a sequence consisting
712-
// of a colon, optional whitespace, and another group containing word characters.
713-
//
714-
// Matches in full:
715-
// cfg-target-has-atomic: 128
716-
// cfg-target-has-atomic:128
717-
// cfg-target-has-atomic
718-
//
719-
// Matches up to the first space (exclusive):
720-
// ignore-test - This test really shouldn't ever be run
721-
//
722-
// Matches up to the second space (exclusive):
723-
// cfg-target-has-atomic: 128 - this requires fancy newfangled atomics
724-
//
725-
// Matches up to the second colon (exclusive)
726-
// cfg-target-has-atomic:128: I put an extra colon here to confuse other programmers!
727-
//
728-
// Does not match:
729-
// (a line consisting solely of whitespace)
730-
// &*#$ cfg-target-has-atomic
731-
//
732-
lazy_static! {
733-
static ref CFG_REGEX: Regex = Regex::new(r"^\s*([\w-]+)(?::\s*([\w-]+))?").unwrap();
734-
}
735-
736-
let captures = CFG_REGEX.captures(&prop).unwrap();
737-
let name = captures.get(1).unwrap().as_str();
738-
let maybe_value = captures.get(2).map(|v| v.as_str().trim());
709+
let mut iter = prop.split(&[':', ' '][..]);
710+
let name = iter.next().unwrap();
711+
let maybe_value = iter.find(|s| !s.is_empty());
739712

740713
let is_match = name == "test" ||
741714
target == name || // triple
@@ -763,18 +736,30 @@ impl Config {
763736
Some(Debugger::Gdb) => name == "gdb",
764737
Some(Debugger::Lldb) => name == "lldb",
765738
None => false,
766-
} ||
767-
match name.strip_prefix("cfg-") {
768-
Some(rustc_cfg_name) => {
769-
let cfg_data = util::fetch_cfg_from_rustc_for_target(&self.rustc_path, target);
770-
util::cfg_has(&cfg_data, rustc_cfg_name, maybe_value)
771-
},
772-
None => false
773-
};
739+
} || name == "cfg" && self.cfg_matches_for_target(maybe_value.unwrap(), target);
774740

775741
if is_match { EvaluatedProp::Match } else { EvaluatedProp::NoMatch }
776742
}
777743

744+
fn cfg_matches_for_target(&self, value: &str, target: &str) -> bool {
745+
let whitespace_or_double_quote = |c: char| c.is_whitespace() || c == '"';
746+
747+
let value = value.replace(whitespace_or_double_quote, "");
748+
749+
let cfg_data = Command::new(&self.rustc_path)
750+
.args(&["--target", &target])
751+
.args(&["--print", "cfg"])
752+
.output()
753+
.unwrap()
754+
.stdout;
755+
let cfg_data = String::from_utf8(cfg_data).unwrap();
756+
let cfg_data = cfg_data.replace('"', "");
757+
758+
let matches_value = |line: &str| line == value || line.split('=').next().unwrap() == value;
759+
760+
cfg_data.lines().any(matches_value)
761+
}
762+
778763
fn has_prop_prefix(&self, line: &str, prefix: &str) -> bool {
779764
// returns whether this line contains this prefix or not. For prefix
780765
// "ignore", returns true if line says "ignore-x86_64", "ignore-arch",

src/tools/compiletest/src/main.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ pub fn parse_config(args: Vec<String>) -> Config {
201201
let llvm_version =
202202
matches.opt_str("llvm-version").as_deref().and_then(header::extract_llvm_version);
203203

204-
let rustc_path = opt_path(matches, "rustc-path");
205204
let src_base = opt_path(matches, "src-base");
206205
let run_ignored = matches.opt_present("ignored");
207206
let mode = matches.opt_str("mode").unwrap().parse().expect("invalid mode");
@@ -215,12 +214,11 @@ pub fn parse_config(args: Vec<String>) -> Config {
215214
// Avoid spawning an external command when we know tidy won't be used.
216215
false
217216
};
218-
219217
Config {
220218
bless: matches.opt_present("bless"),
221219
compile_lib_path: make_absolute(opt_path(matches, "compile-lib-path")),
222220
run_lib_path: make_absolute(opt_path(matches, "run-lib-path")),
223-
rustc_path: rustc_path,
221+
rustc_path: opt_path(matches, "rustc-path"),
224222
rustdoc_path: matches.opt_str("rustdoc-path").map(PathBuf::from),
225223
rust_demangler_path: matches.opt_str("rust-demangler-path").map(PathBuf::from),
226224
lldb_python: matches.opt_str("lldb-python").unwrap(),

src/tools/compiletest/src/util.rs

-61
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use crate::common::Config;
2-
3-
use std::collections::HashMap;
42
use std::env;
53
use std::ffi::OsStr;
64
use std::path::PathBuf;
7-
use std::process::Command;
85

96
use tracing::*;
107

@@ -218,64 +215,6 @@ pub fn logv(config: &Config, s: String) {
218215
}
219216
}
220217

221-
pub fn fetch_cfg_from_rustc_for_target<P: AsRef<OsStr>>(
222-
rustc_path: &P,
223-
target: &str,
224-
) -> HashMap<String, Vec<String>> {
225-
let mut target_cfg = HashMap::new();
226-
227-
if !cfg!(test) {
228-
let rustc_output = Command::new(&rustc_path)
229-
.args(&["--target", &target])
230-
.args(&["--print", "cfg"])
231-
.output()
232-
.unwrap()
233-
.stdout;
234-
let rustc_output = String::from_utf8(rustc_output).unwrap();
235-
236-
for line in rustc_output.lines() {
237-
if let Some((name, value)) = line.split_once('=') {
238-
let normalized_value = value.trim_matches('"');
239-
cfg_add(&mut target_cfg, name, normalized_value);
240-
} else {
241-
cfg_add(&mut target_cfg, line, "");
242-
}
243-
}
244-
}
245-
target_cfg
246-
}
247-
248-
/// Adds the given name and value to the provided cfg [`HashMap`]. If the `name` already
249-
/// points to a vector, this adds `value` to the vector. If `name` does not point
250-
/// to a vector, this adds a new vector containing only `value` to the [`HashMap`].
251-
fn cfg_add(map: &mut HashMap<String, Vec<String>>, name: &str, value: &str) {
252-
let name = name.to_string();
253-
let value = value.to_string();
254-
255-
if let Some(values) = map.get_mut(&name) {
256-
values.push(value.to_string());
257-
} else {
258-
map.insert(name, vec![value.to_string()]);
259-
}
260-
}
261-
262-
/// Checks if the cfg HashMap has the given `name`. If the `required_value` is
263-
/// `Some(value)`, this will only return `true` if `name` is associated with `value`.
264-
pub fn cfg_has(
265-
map: &HashMap<String, Vec<String>>,
266-
name: &str,
267-
required_value: Option<&str>,
268-
) -> bool {
269-
let name = name.replace("-", "_");
270-
let required_value = required_value.map(str::trim).map(str::to_string);
271-
272-
match (map.get(&name), required_value) {
273-
(None, _) => false,
274-
(Some(_), None) => true,
275-
(Some(values), Some(required_value)) => values.contains(&required_value),
276-
}
277-
}
278-
279218
pub trait PathBufExt {
280219
/// Append an extension to the path, even if it already has one.
281220
fn with_extra_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf;

0 commit comments

Comments
 (0)