Skip to content

Commit 89fdf3b

Browse files
authored
Rollup merge of rust-lang#74237 - lzutao:compiletest, r=Mark-Simulacrum
compiletest: Rewrite extract_*_version functions This makes extract_lldb_version has the same version type like extract_gdb_version.
2 parents aa667a5 + 47a0f69 commit 89fdf3b

File tree

4 files changed

+99
-161
lines changed

4 files changed

+99
-161
lines changed

src/tools/compiletest/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ pub struct Config {
268268
pub gdb_native_rust: bool,
269269

270270
/// Version of LLDB
271-
pub lldb_version: Option<String>,
271+
pub lldb_version: Option<u32>,
272272

273273
/// Whether LLDB has native rust support
274274
pub lldb_native_rust: bool,

src/tools/compiletest/src/header.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,29 @@ impl EarlyProps {
132132

133133
fn ignore_gdb(config: &Config, line: &str) -> bool {
134134
if let Some(actual_version) = config.gdb_version {
135-
if line.starts_with("min-gdb-version") {
136-
let (start_ver, end_ver) = extract_gdb_version_range(line);
135+
if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
136+
let (start_ver, end_ver) = extract_gdb_version_range(rest);
137137

138138
if start_ver != end_ver {
139139
panic!("Expected single GDB version")
140140
}
141141
// Ignore if actual version is smaller the minimum required
142142
// version
143-
actual_version < start_ver
144-
} else if line.starts_with("ignore-gdb-version") {
145-
let (min_version, max_version) = extract_gdb_version_range(line);
143+
return actual_version < start_ver;
144+
} else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
145+
let (min_version, max_version) = extract_gdb_version_range(rest);
146146

147147
if max_version < min_version {
148148
panic!("Malformed GDB version range: max < min")
149149
}
150150

151-
actual_version >= min_version && actual_version <= max_version
152-
} else {
153-
false
151+
return actual_version >= min_version && actual_version <= max_version;
154152
}
155-
} else {
156-
false
157153
}
154+
false
158155
}
159156

160-
// Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]",
157+
// Takes a directive of the form "<version1> [- <version2>]",
161158
// returns the numeric representation of <version1> and <version2> as
162159
// tuple: (<version1> as u32, <version2> as u32)
163160
// If the <version2> part is omitted, the second component of the tuple
@@ -173,31 +170,32 @@ impl EarlyProps {
173170
.take(3) // 3 or more = invalid, so take at most 3.
174171
.collect::<Vec<Option<u32>>>();
175172

176-
match range_components.len() {
177-
1 => {
178-
let v = range_components[0].unwrap();
173+
match *range_components {
174+
[v] => {
175+
let v = v.unwrap();
179176
(v, v)
180177
}
181-
2 => {
182-
let v_min = range_components[0].unwrap();
183-
let v_max = range_components[1].expect(ERROR_MESSAGE);
178+
[min, max] => {
179+
let v_min = min.unwrap();
180+
let v_max = max.expect(ERROR_MESSAGE);
184181
(v_min, v_max)
185182
}
186183
_ => panic!(ERROR_MESSAGE),
187184
}
188185
}
189186

190187
fn ignore_lldb(config: &Config, line: &str) -> bool {
191-
if let Some(ref actual_version) = config.lldb_version {
192-
if line.starts_with("min-lldb-version") {
193-
let min_version = line
194-
.trim_end()
195-
.rsplit(' ')
196-
.next()
197-
.expect("Malformed lldb version directive");
188+
if let Some(actual_version) = config.lldb_version {
189+
if let Some(min_version) = line.strip_prefix("min-lldb-version:").map(str::trim) {
190+
let min_version = min_version.parse().unwrap_or_else(|e| {
191+
panic!(
192+
"Unexpected format of LLDB version string: {}\n{:?}",
193+
min_version, e
194+
);
195+
});
198196
// Ignore if actual version is smaller the minimum required
199197
// version
200-
lldb_version_to_int(actual_version) < lldb_version_to_int(min_version)
198+
actual_version < min_version
201199
} else if line.starts_with("rust-lldb") && !config.lldb_native_rust {
202200
true
203201
} else {
@@ -944,12 +942,6 @@ impl Config {
944942
}
945943
}
946944

947-
pub fn lldb_version_to_int(version_string: &str) -> isize {
948-
let error_string =
949-
format!("Encountered LLDB version string with unexpected format: {}", version_string);
950-
version_string.parse().expect(&error_string)
951-
}
952-
953945
fn expand_variables(mut value: String, config: &Config) -> String {
954946
const CWD: &'static str = "{{cwd}}";
955947
const SRC_BASE: &'static str = "{{src-base}}";

src/tools/compiletest/src/main.rs

Lines changed: 63 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ pub fn parse_config(args: Vec<String>) -> Config {
165165
let cdb = analyze_cdb(matches.opt_str("cdb"), &target);
166166
let (gdb, gdb_version, gdb_native_rust) =
167167
analyze_gdb(matches.opt_str("gdb"), &target, &android_cross_path);
168-
let (lldb_version, lldb_native_rust) = extract_lldb_version(matches.opt_str("lldb-version"));
169-
170-
let color = match matches.opt_str("color").as_ref().map(|x| &**x) {
168+
let (lldb_version, lldb_native_rust) = matches
169+
.opt_str("lldb-version")
170+
.as_deref()
171+
.and_then(extract_lldb_version)
172+
.map(|(v, b)| (Some(v), b))
173+
.unwrap_or((None, false));
174+
let color = match matches.opt_str("color").as_deref() {
171175
Some("auto") | None => ColorConfig::AutoColor,
172176
Some("always") => ColorConfig::AlwaysColor,
173177
Some("never") => ColorConfig::NeverColor,
@@ -251,7 +255,7 @@ pub fn log_config(config: &Config) {
251255
logv(c, format!("stage_id: {}", config.stage_id));
252256
logv(c, format!("mode: {}", config.mode));
253257
logv(c, format!("run_ignored: {}", config.run_ignored));
254-
logv(c, format!("filter: {}", opt_str(&config.filter.as_ref().map(|re| re.to_owned()))));
258+
logv(c, format!("filter: {}", opt_str(&config.filter)));
255259
logv(c, format!("filter_exact: {}", config.filter_exact));
256260
logv(
257261
c,
@@ -400,17 +404,14 @@ fn configure_lldb(config: &Config) -> Option<Config> {
400404
return None;
401405
}
402406

403-
if let Some(lldb_version) = config.lldb_version.as_ref() {
404-
if lldb_version == "350" {
405-
println!(
406-
"WARNING: The used version of LLDB ({}) has a \
407-
known issue that breaks debuginfo tests. See \
408-
issue #32520 for more information. Skipping all \
409-
LLDB-based tests!",
410-
lldb_version
411-
);
412-
return None;
413-
}
407+
if let Some(350) = config.lldb_version {
408+
println!(
409+
"WARNING: The used version of LLDB (350) has a \
410+
known issue that breaks debuginfo tests. See \
411+
issue #32520 for more information. Skipping all \
412+
LLDB-based tests!",
413+
);
414+
return None;
414415
}
415416

416417
// Some older versions of LLDB seem to have problems with multiple
@@ -722,9 +723,7 @@ fn make_test_closure(
722723
let config = config.clone();
723724
let testpaths = testpaths.clone();
724725
let revision = revision.cloned();
725-
test::DynTestFn(Box::new(move || {
726-
runtest::run(config, &testpaths, revision.as_ref().map(|s| s.as_str()))
727-
}))
726+
test::DynTestFn(Box::new(move || runtest::run(config, &testpaths, revision.as_deref())))
728727
}
729728

730729
/// Returns `true` if the given target is an Android target for the
@@ -840,75 +839,40 @@ fn extract_gdb_version(full_version_line: &str) -> Option<u32> {
840839
// This particular form is documented in the GNU coding standards:
841840
// https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion
842841

843-
// don't start parsing in the middle of a number
844-
let mut prev_was_digit = false;
845-
let mut in_parens = false;
846-
for (pos, c) in full_version_line.char_indices() {
847-
if in_parens {
848-
if c == ')' {
849-
in_parens = false;
850-
}
851-
continue;
852-
} else if c == '(' {
853-
in_parens = true;
854-
continue;
855-
}
856-
857-
if prev_was_digit || !c.is_digit(10) {
858-
prev_was_digit = c.is_digit(10);
859-
continue;
842+
let mut splits = full_version_line.rsplit(' ');
843+
let version_string = splits.next().unwrap();
844+
845+
let mut splits = version_string.split('.');
846+
let major = splits.next().unwrap();
847+
let minor = splits.next().unwrap();
848+
let patch = splits.next();
849+
850+
let major: u32 = major.parse().unwrap();
851+
let (minor, patch): (u32, u32) = match minor.find(not_a_digit) {
852+
None => {
853+
let minor = minor.parse().unwrap();
854+
let patch: u32 = match patch {
855+
Some(patch) => match patch.find(not_a_digit) {
856+
None => patch.parse().unwrap(),
857+
Some(idx) if idx > 3 => 0,
858+
Some(idx) => patch[..idx].parse().unwrap(),
859+
},
860+
None => 0,
861+
};
862+
(minor, patch)
860863
}
861-
862-
prev_was_digit = true;
863-
864-
let line = &full_version_line[pos..];
865-
866-
let next_split = match line.find(|c: char| !c.is_digit(10)) {
867-
Some(idx) => idx,
868-
None => continue, // no minor version
869-
};
870-
871-
if line.as_bytes()[next_split] != b'.' {
872-
continue; // no minor version
864+
// There is no patch version after minor-date (e.g. "4-2012").
865+
Some(idx) => {
866+
let minor = minor[..idx].parse().unwrap();
867+
(minor, 0)
873868
}
869+
};
874870

875-
let major = &line[..next_split];
876-
let line = &line[next_split + 1..];
877-
878-
let (minor, patch) = match line.find(|c: char| !c.is_digit(10)) {
879-
Some(idx) => {
880-
if line.as_bytes()[idx] == b'.' {
881-
let patch = &line[idx + 1..];
882-
883-
let patch_len =
884-
patch.find(|c: char| !c.is_digit(10)).unwrap_or_else(|| patch.len());
885-
let patch = &patch[..patch_len];
886-
let patch = if patch_len > 3 || patch_len == 0 { None } else { Some(patch) };
887-
888-
(&line[..idx], patch)
889-
} else {
890-
(&line[..idx], None)
891-
}
892-
}
893-
None => (line, None),
894-
};
895-
896-
if minor.is_empty() {
897-
continue;
898-
}
899-
900-
let major: u32 = major.parse().unwrap();
901-
let minor: u32 = minor.parse().unwrap();
902-
let patch: u32 = patch.unwrap_or("0").parse().unwrap();
903-
904-
return Some(((major * 1000) + minor) * 1000 + patch);
905-
}
906-
907-
None
871+
Some(((major * 1000) + minor) * 1000 + patch)
908872
}
909873

910874
/// Returns (LLDB version, LLDB is rust-enabled)
911-
fn extract_lldb_version(full_version_line: Option<String>) -> (Option<String>, bool) {
875+
fn extract_lldb_version(full_version_line: &str) -> Option<(u32, bool)> {
912876
// Extract the major LLDB version from the given version string.
913877
// LLDB version strings are different for Apple and non-Apple platforms.
914878
// The Apple variant looks like this:
@@ -917,7 +881,7 @@ fn extract_lldb_version(full_version_line: Option<String>) -> (Option<String>, b
917881
// lldb-300.2.51 (new versions)
918882
//
919883
// We are only interested in the major version number, so this function
920-
// will return `Some("179")` and `Some("300")` respectively.
884+
// will return `Some(179)` and `Some(300)` respectively.
921885
//
922886
// Upstream versions look like:
923887
// lldb version 6.0.1
@@ -929,53 +893,24 @@ fn extract_lldb_version(full_version_line: Option<String>) -> (Option<String>, b
929893
// normally fine because the only non-Apple version we test is
930894
// rust-enabled.
931895

932-
if let Some(ref full_version_line) = full_version_line {
933-
if !full_version_line.trim().is_empty() {
934-
let full_version_line = full_version_line.trim();
935-
936-
for (pos, l) in full_version_line.char_indices() {
937-
if l != 'l' && l != 'L' {
938-
continue;
939-
}
940-
if pos + 5 >= full_version_line.len() {
941-
continue;
942-
}
943-
let l = full_version_line[pos + 1..].chars().next().unwrap();
944-
if l != 'l' && l != 'L' {
945-
continue;
946-
}
947-
let d = full_version_line[pos + 2..].chars().next().unwrap();
948-
if d != 'd' && d != 'D' {
949-
continue;
950-
}
951-
let b = full_version_line[pos + 3..].chars().next().unwrap();
952-
if b != 'b' && b != 'B' {
953-
continue;
954-
}
955-
let dash = full_version_line[pos + 4..].chars().next().unwrap();
956-
if dash != '-' {
957-
continue;
958-
}
959-
960-
let vers = full_version_line[pos + 5..]
961-
.chars()
962-
.take_while(|c| c.is_digit(10))
963-
.collect::<String>();
964-
if !vers.is_empty() {
965-
return (Some(vers), full_version_line.contains("rust-enabled"));
966-
}
967-
}
896+
let full_version_line = full_version_line.trim();
968897

969-
if full_version_line.starts_with("lldb version ") {
970-
let vers = full_version_line[13..]
971-
.chars()
972-
.take_while(|c| c.is_digit(10))
973-
.collect::<String>();
974-
if !vers.is_empty() {
975-
return (Some(vers + "00"), full_version_line.contains("rust-enabled"));
976-
}
977-
}
898+
if let Some(apple_ver) =
899+
full_version_line.strip_prefix("LLDB-").or_else(|| full_version_line.strip_prefix("lldb-"))
900+
{
901+
if let Some(idx) = apple_ver.find(not_a_digit) {
902+
let version: u32 = apple_ver[..idx].parse().unwrap();
903+
return Some((version, full_version_line.contains("rust-enabled")));
904+
}
905+
} else if let Some(lldb_ver) = full_version_line.strip_prefix("lldb version ") {
906+
if let Some(idx) = lldb_ver.find(not_a_digit) {
907+
let version: u32 = lldb_ver[..idx].parse().unwrap();
908+
return Some((version * 100, full_version_line.contains("rust-enabled")));
978909
}
979910
}
980-
(None, false)
911+
None
912+
}
913+
914+
fn not_a_digit(c: char) -> bool {
915+
!c.is_digit(10)
981916
}

src/tools/compiletest/src/tests.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22

33
#[test]
44
fn test_extract_gdb_version() {
5-
macro_rules! test { ($($expectation:tt: $input:tt,)*) => {{$(
5+
macro_rules! test { ($($expectation:literal: $input:literal,)*) => {{$(
66
assert_eq!(extract_gdb_version($input), Some($expectation));
77
)*}}}
88

@@ -41,6 +41,17 @@ fn test_extract_gdb_version() {
4141
}
4242
}
4343

44+
#[test]
45+
fn test_extract_lldb_version() {
46+
// Apple variants
47+
assert_eq!(extract_lldb_version("LLDB-179.5"), Some((179, false)));
48+
assert_eq!(extract_lldb_version("lldb-300.2.51"), Some((300, false)));
49+
50+
// Upstream versions
51+
assert_eq!(extract_lldb_version("lldb version 6.0.1"), Some((600, false)));
52+
assert_eq!(extract_lldb_version("lldb version 9.0.0"), Some((900, false)));
53+
}
54+
4455
#[test]
4556
fn is_test_test() {
4657
assert_eq!(true, is_test(&OsString::from("a_test.rs")));

0 commit comments

Comments
 (0)