Skip to content

Commit

Permalink
Rewrite extract_version_range
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jul 19, 2020
1 parent 60fac34 commit 1314d31
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ impl EarlyProps {
fn ignore_gdb(config: &Config, line: &str) -> bool {
if let Some(actual_version) = config.gdb_version {
if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version);
let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
.unwrap_or_else(|| {
panic!("couldn't parse version range: {:?}", rest);
});

if start_ver != end_ver {
panic!("Expected single GDB version")
Expand All @@ -143,7 +146,9 @@ impl EarlyProps {
return actual_version < start_ver;
} else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
let (min_version, max_version) =
extract_version_range(rest, extract_gdb_version);
extract_version_range(rest, extract_gdb_version).unwrap_or_else(|| {
panic!("couldn't parse version range: {:?}", rest);
});

if max_version < min_version {
panic!("Malformed GDB version range: max < min")
Expand Down Expand Up @@ -197,7 +202,10 @@ impl EarlyProps {
} else if let Some(rest) = line.strip_prefix("ignore-llvm-version:").map(str::trim)
{
// Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
let (v_min, v_max) = extract_version_range(rest, extract_llvm_version);
let (v_min, v_max) = extract_version_range(rest, extract_llvm_version)
.unwrap_or_else(|| {
panic!("couldn't parse version range: {:?}", rest);
});
if v_max < v_min {
panic!("Malformed LLVM version range: max < min")
}
Expand Down Expand Up @@ -940,28 +948,28 @@ pub fn extract_llvm_version(version: &str) -> Option<u32> {
// tuple: (<version1> as u32, <version2> as u32)
// If the <version2> part is omitted, the second component of the tuple
// is the same as <version1>.
fn extract_version_range<F>(line: &str, parse: F) -> (u32, u32)
fn extract_version_range<F>(line: &str, parse: F) -> Option<(u32, u32)>
where
F: Fn(&str) -> Option<u32>,
{
let range_components = line
.split(&[' ', '-'][..])
.filter(|word| !word.is_empty())
.map(parse)
.skip_while(Option::is_none)
.take(3) // 3 or more = invalid, so take at most 3.
.collect::<Vec<Option<u32>>>();

match *range_components {
[v] => {
let v = v.unwrap();
(v, v)
}
[min, max] => {
let v_min = min.unwrap();
let v_max = max.expect("Malformed version directive");
(v_min, v_max)
}
_ => panic!("Malformed version directive"),
let mut splits = line.splitn(2, "- ").map(str::trim);
let min = splits.next().unwrap();
if min.ends_with('-') {
return None;
}

let max = splits.next();

if min.is_empty() {
return None;
}

let min = parse(min)?;
let max = match max {
Some(max) if max.is_empty() => return None,
Some(max) => parse(max)?,
_ => min,
};

Some((min, max))
}
15 changes: 15 additions & 0 deletions src/tools/compiletest/src/header/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,18 @@ fn sanitizers() {
assert!(parse_rs(&config, "// needs-sanitizer-memory").ignore);
assert!(parse_rs(&config, "// needs-sanitizer-thread").ignore);
}

#[test]
fn test_extract_version_range() {
use super::{extract_llvm_version, extract_version_range};

assert_eq!(extract_version_range("1.2.3 - 4.5.6", extract_llvm_version), Some((10203, 40506)));
assert_eq!(extract_version_range("0 - 4.5.6", extract_llvm_version), Some((0, 40506)));
assert_eq!(extract_version_range("1.2.3 -", extract_llvm_version), None);
assert_eq!(extract_version_range("1.2.3 - ", extract_llvm_version), None);
assert_eq!(extract_version_range("- 4.5.6", extract_llvm_version), None);
assert_eq!(extract_version_range("-", extract_llvm_version), None);
assert_eq!(extract_version_range(" - 4.5.6", extract_llvm_version), None);
assert_eq!(extract_version_range(" - 4.5.6", extract_llvm_version), None);
assert_eq!(extract_version_range("0 -", extract_llvm_version), None);
}

0 comments on commit 1314d31

Please sign in to comment.