Skip to content

Commit 60fac34

Browse files
committed
Rewrite extract_llvm_version
1 parent 99e3a3c commit 60fac34

File tree

5 files changed

+49
-57
lines changed

5 files changed

+49
-57
lines changed

src/tools/compiletest/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ pub struct Config {
274274
pub lldb_native_rust: bool,
275275

276276
/// Version of LLVM
277-
pub llvm_version: Option<String>,
277+
pub llvm_version: Option<u32>,
278278

279279
/// Is LLVM a system LLVM
280280
pub system_llvm: bool,

src/tools/compiletest/src/header.rs

+32-51
Original file line numberDiff line numberDiff line change
@@ -181,69 +181,35 @@ impl EarlyProps {
181181
if config.system_llvm && line.starts_with("no-system-llvm") {
182182
return true;
183183
}
184-
if let Some(ref actual_version) = config.llvm_version {
185-
let actual_version = version_to_int(actual_version);
186-
if line.starts_with("min-llvm-version") {
187-
let min_version = line
188-
.trim_end()
189-
.rsplit(' ')
190-
.next()
191-
.expect("Malformed llvm version directive");
184+
if let Some(actual_version) = config.llvm_version {
185+
if let Some(rest) = line.strip_prefix("min-llvm-version:").map(str::trim) {
186+
let min_version = extract_llvm_version(rest).unwrap();
192187
// Ignore if actual version is smaller the minimum required
193188
// version
194-
actual_version < version_to_int(min_version)
195-
} else if line.starts_with("min-system-llvm-version") {
196-
let min_version = line
197-
.trim_end()
198-
.rsplit(' ')
199-
.next()
200-
.expect("Malformed llvm version directive");
189+
actual_version < min_version
190+
} else if let Some(rest) =
191+
line.strip_prefix("min-system-llvm-version:").map(str::trim)
192+
{
193+
let min_version = extract_llvm_version(rest).unwrap();
201194
// Ignore if using system LLVM and actual version
202195
// is smaller the minimum required version
203-
config.system_llvm && actual_version < version_to_int(min_version)
204-
} else if line.starts_with("ignore-llvm-version") {
205-
// Syntax is: "ignore-llvm-version <version1> [- <version2>]"
206-
let range_components = line
207-
.split(' ')
208-
.skip(1) // Skip the directive.
209-
.map(|s| s.trim())
210-
.filter(|word| !word.is_empty() && word != &"-")
211-
.take(3) // 3 or more = invalid, so take at most 3.
212-
.collect::<Vec<&str>>();
213-
match range_components.len() {
214-
1 => actual_version == version_to_int(range_components[0]),
215-
2 => {
216-
let v_min = version_to_int(range_components[0]);
217-
let v_max = version_to_int(range_components[1]);
218-
if v_max < v_min {
219-
panic!("Malformed LLVM version range: max < min")
220-
}
221-
// Ignore if version lies inside of range.
222-
actual_version >= v_min && actual_version <= v_max
223-
}
224-
_ => panic!("Malformed LLVM version directive"),
196+
config.system_llvm && actual_version < min_version
197+
} else if let Some(rest) = line.strip_prefix("ignore-llvm-version:").map(str::trim)
198+
{
199+
// Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
200+
let (v_min, v_max) = extract_version_range(rest, extract_llvm_version);
201+
if v_max < v_min {
202+
panic!("Malformed LLVM version range: max < min")
225203
}
204+
// Ignore if version lies inside of range.
205+
actual_version >= v_min && actual_version <= v_max
226206
} else {
227207
false
228208
}
229209
} else {
230210
false
231211
}
232212
}
233-
234-
fn version_to_int(version: &str) -> u32 {
235-
let version_without_suffix = version.trim_end_matches("git").split('-').next().unwrap();
236-
let components: Vec<u32> = version_without_suffix
237-
.split('.')
238-
.map(|s| s.parse().expect("Malformed version component"))
239-
.collect();
240-
match components.len() {
241-
1 => components[0] * 10000,
242-
2 => components[0] * 10000 + components[1] * 100,
243-
3 => components[0] * 10000 + components[1] * 100 + components[2],
244-
_ => panic!("Malformed version"),
245-
}
246-
}
247213
}
248214
}
249215

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

923+
pub fn extract_llvm_version(version: &str) -> Option<u32> {
924+
let version_without_suffix = version.trim_end_matches("git").split('-').next().unwrap();
925+
let components: Vec<u32> = version_without_suffix
926+
.split('.')
927+
.map(|s| s.parse().expect("Malformed version component"))
928+
.collect();
929+
let version = match *components {
930+
[a] => a * 10_000,
931+
[a, b] => a * 10_000 + b * 100,
932+
[a, b, c] => a * 10_000 + b * 100 + c,
933+
_ => panic!("Malformed version"),
934+
};
935+
Some(version)
936+
}
937+
957938
// Takes a directive of the form "<version1> [- <version2>]",
958939
// returns the numeric representation of <version1> and <version2> as
959940
// tuple: (<version1> as u32, <version2> as u32)

src/tools/compiletest/src/header/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ fn no_system_llvm() {
119119
fn llvm_version() {
120120
let mut config = config();
121121

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

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

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

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

src/tools/compiletest/src/main.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ pub fn parse_config(args: Vec<String>) -> Config {
177177
Some("never") => ColorConfig::NeverColor,
178178
Some(x) => panic!("argument for --color must be auto, always, or never, but found `{}`", x),
179179
};
180+
let llvm_version =
181+
matches.opt_str("llvm-version").as_deref().and_then(header::extract_llvm_version);
180182

181183
let src_base = opt_path(matches, "src-base");
182184
let run_ignored = matches.opt_present("ignored");
@@ -217,7 +219,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
217219
gdb_native_rust,
218220
lldb_version,
219221
lldb_native_rust,
220-
llvm_version: matches.opt_str("llvm-version"),
222+
llvm_version,
221223
system_llvm: matches.opt_present("system-llvm"),
222224
android_cross_path,
223225
adb_path: opt_str2(matches.opt_str("adb-path")),

src/tools/compiletest/src/tests.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::header::extract_llvm_version;
12
use super::*;
23

34
#[test]
@@ -60,3 +61,11 @@ fn is_test_test() {
6061
assert_eq!(false, is_test(&OsString::from("#a_dog_gif")));
6162
assert_eq!(false, is_test(&OsString::from("~a_temp_file")));
6263
}
64+
65+
#[test]
66+
fn test_extract_llvm_version() {
67+
assert_eq!(extract_llvm_version("8.1.2-rust"), Some(80102));
68+
assert_eq!(extract_llvm_version("9.0.1-rust-1.43.0-dev"), Some(90001));
69+
assert_eq!(extract_llvm_version("9.3.1-rust-1.43.0-dev"), Some(90301));
70+
assert_eq!(extract_llvm_version("10.0.0-rust"), Some(100000));
71+
}

0 commit comments

Comments
 (0)