Skip to content

Commit

Permalink
Rewrite extract_llvm_version
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jul 19, 2020
1 parent 99e3a3c commit 60fac34
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub struct Config {
pub lldb_native_rust: bool,

/// Version of LLVM
pub llvm_version: Option<String>,
pub llvm_version: Option<u32>,

/// Is LLVM a system LLVM
pub system_llvm: bool,
Expand Down
83 changes: 32 additions & 51 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,69 +181,35 @@ impl EarlyProps {
if config.system_llvm && line.starts_with("no-system-llvm") {
return true;
}
if let Some(ref actual_version) = config.llvm_version {
let actual_version = version_to_int(actual_version);
if line.starts_with("min-llvm-version") {
let min_version = line
.trim_end()
.rsplit(' ')
.next()
.expect("Malformed llvm version directive");
if let Some(actual_version) = config.llvm_version {
if let Some(rest) = line.strip_prefix("min-llvm-version:").map(str::trim) {
let min_version = extract_llvm_version(rest).unwrap();
// Ignore if actual version is smaller the minimum required
// version
actual_version < version_to_int(min_version)
} else if line.starts_with("min-system-llvm-version") {
let min_version = line
.trim_end()
.rsplit(' ')
.next()
.expect("Malformed llvm version directive");
actual_version < min_version
} else if let Some(rest) =
line.strip_prefix("min-system-llvm-version:").map(str::trim)
{
let min_version = extract_llvm_version(rest).unwrap();
// Ignore if using system LLVM and actual version
// is smaller the minimum required version
config.system_llvm && actual_version < version_to_int(min_version)
} else if line.starts_with("ignore-llvm-version") {
// Syntax is: "ignore-llvm-version <version1> [- <version2>]"
let range_components = line
.split(' ')
.skip(1) // Skip the directive.
.map(|s| s.trim())
.filter(|word| !word.is_empty() && word != &"-")
.take(3) // 3 or more = invalid, so take at most 3.
.collect::<Vec<&str>>();
match range_components.len() {
1 => actual_version == version_to_int(range_components[0]),
2 => {
let v_min = version_to_int(range_components[0]);
let v_max = version_to_int(range_components[1]);
if v_max < v_min {
panic!("Malformed LLVM version range: max < min")
}
// Ignore if version lies inside of range.
actual_version >= v_min && actual_version <= v_max
}
_ => panic!("Malformed LLVM version directive"),
config.system_llvm && actual_version < min_version
} 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);
if v_max < v_min {
panic!("Malformed LLVM version range: max < min")
}
// Ignore if version lies inside of range.
actual_version >= v_min && actual_version <= v_max
} else {
false
}
} else {
false
}
}

fn version_to_int(version: &str) -> u32 {
let version_without_suffix = version.trim_end_matches("git").split('-').next().unwrap();
let components: Vec<u32> = version_without_suffix
.split('.')
.map(|s| s.parse().expect("Malformed version component"))
.collect();
match components.len() {
1 => components[0] * 10000,
2 => components[0] * 10000 + components[1] * 100,
3 => components[0] * 10000 + components[1] * 100 + components[2],
_ => panic!("Malformed version"),
}
}
}
}

Expand Down Expand Up @@ -954,6 +920,21 @@ fn parse_normalization_string(line: &mut &str) -> Option<String> {
Some(result)
}

pub fn extract_llvm_version(version: &str) -> Option<u32> {
let version_without_suffix = version.trim_end_matches("git").split('-').next().unwrap();
let components: Vec<u32> = version_without_suffix
.split('.')
.map(|s| s.parse().expect("Malformed version component"))
.collect();
let version = match *components {
[a] => a * 10_000,
[a, b] => a * 10_000 + b * 100,
[a, b, c] => a * 10_000 + b * 100 + c,
_ => panic!("Malformed version"),
};
Some(version)
}

// Takes a directive of the form "<version1> [- <version2>]",
// returns the numeric representation of <version1> and <version2> as
// tuple: (<version1> as u32, <version2> as u32)
Expand Down
8 changes: 4 additions & 4 deletions src/tools/compiletest/src/header/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ fn no_system_llvm() {
fn llvm_version() {
let mut config = config();

config.llvm_version = Some("8.1.2-rust".to_owned());
config.llvm_version = Some(80102);
assert!(parse_rs(&config, "// min-llvm-version: 9.0").ignore);

config.llvm_version = Some("9.0.1-rust-1.43.0-dev".to_owned());
config.llvm_version = Some(90001);
assert!(parse_rs(&config, "// min-llvm-version: 9.2").ignore);

config.llvm_version = Some("9.3.1-rust-1.43.0-dev".to_owned());
config.llvm_version = Some(90301);
assert!(!parse_rs(&config, "// min-llvm-version: 9.2").ignore);

config.llvm_version = Some("10.0.0-rust".to_owned());
config.llvm_version = Some(100000);
assert!(!parse_rs(&config, "// min-llvm-version: 9.0").ignore);
}

Expand Down
4 changes: 3 additions & 1 deletion src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ pub fn parse_config(args: Vec<String>) -> Config {
Some("never") => ColorConfig::NeverColor,
Some(x) => panic!("argument for --color must be auto, always, or never, but found `{}`", x),
};
let llvm_version =
matches.opt_str("llvm-version").as_deref().and_then(header::extract_llvm_version);

let src_base = opt_path(matches, "src-base");
let run_ignored = matches.opt_present("ignored");
Expand Down Expand Up @@ -217,7 +219,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
gdb_native_rust,
lldb_version,
lldb_native_rust,
llvm_version: matches.opt_str("llvm-version"),
llvm_version,
system_llvm: matches.opt_present("system-llvm"),
android_cross_path,
adb_path: opt_str2(matches.opt_str("adb-path")),
Expand Down
9 changes: 9 additions & 0 deletions src/tools/compiletest/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::header::extract_llvm_version;
use super::*;

#[test]
Expand Down Expand Up @@ -60,3 +61,11 @@ fn is_test_test() {
assert_eq!(false, is_test(&OsString::from("#a_dog_gif")));
assert_eq!(false, is_test(&OsString::from("~a_temp_file")));
}

#[test]
fn test_extract_llvm_version() {
assert_eq!(extract_llvm_version("8.1.2-rust"), Some(80102));
assert_eq!(extract_llvm_version("9.0.1-rust-1.43.0-dev"), Some(90001));
assert_eq!(extract_llvm_version("9.3.1-rust-1.43.0-dev"), Some(90301));
assert_eq!(extract_llvm_version("10.0.0-rust"), Some(100000));
}

0 comments on commit 60fac34

Please sign in to comment.