-
Notifications
You must be signed in to change notification settings - Fork 13.4k
compiletest: improve robustness of LLVM version handling #132315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use std::io::prelude::*; | |
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
use semver::Version; | ||
use tracing::*; | ||
|
||
use crate::common::{Config, Debugger, FailMode, Mode, PassMode}; | ||
|
@@ -1113,34 +1114,47 @@ fn parse_normalize_rule(header: &str) -> Option<(String, String)> { | |
Some((regex, replacement)) | ||
} | ||
|
||
pub fn extract_llvm_version(version: &str) -> Option<u32> { | ||
let pat = |c: char| !c.is_ascii_digit() && c != '.'; | ||
let version_without_suffix = match version.find(pat) { | ||
Some(pos) => &version[..pos], | ||
/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this | ||
/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting | ||
/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of | ||
/// having to write `//@ min-llvm-version: 19.0.0`. | ||
/// | ||
/// Currently panics if the input string is malformed, though we really should not use panic as an | ||
/// error handling strategy. | ||
/// | ||
/// FIXME(jieyouxu): improve error handling | ||
pub fn extract_llvm_version(version: &str) -> Version { | ||
// The version substring we're interested in usually looks like the `1.2.3`, without any of the | ||
// fancy suffix like `-rc1` or `meow`. | ||
let version = version.trim(); | ||
let uninterested = |c: char| !c.is_ascii_digit() && c != '.'; | ||
let version_without_suffix = match version.split_once(uninterested) { | ||
Some((prefix, _suffix)) => prefix, | ||
None => version, | ||
}; | ||
let components: Vec<u32> = version_without_suffix | ||
|
||
let components: Vec<u64> = version_without_suffix | ||
.split('.') | ||
.map(|s| s.parse().expect("Malformed version component")) | ||
.map(|s| s.parse().expect("llvm version component should consist of only digits")) | ||
.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"), | ||
}; | ||
Comment on lines
-1126
to
-1131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifically, this |
||
Some(version) | ||
|
||
match &components[..] { | ||
[major] => Version::new(*major, 0, 0), | ||
[major, minor] => Version::new(*major, *minor, 0), | ||
[major, minor, patch] => Version::new(*major, *minor, *patch), | ||
_ => panic!("malformed llvm version string, expected only 1-3 components: {version}"), | ||
} | ||
} | ||
|
||
pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<u32> { | ||
pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> { | ||
let output = Command::new(binary_path).arg("--version").output().ok()?; | ||
if !output.status.success() { | ||
return None; | ||
} | ||
let version = String::from_utf8(output.stdout).ok()?; | ||
for line in version.lines() { | ||
if let Some(version) = line.split("LLVM version ").nth(1) { | ||
return extract_llvm_version(version); | ||
return Some(extract_llvm_version(version)); | ||
} | ||
} | ||
None | ||
|
@@ -1247,15 +1261,17 @@ pub fn llvm_has_libzstd(config: &Config) -> bool { | |
false | ||
} | ||
|
||
/// Takes a directive of the form `"<version1> [- <version2>]"`, | ||
/// returns the numeric representation of `<version1>` and `<version2>` as | ||
/// tuple: `(<version1> as u32, <version2> as u32)`. | ||
/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation | ||
/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`. | ||
/// | ||
/// 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) -> Option<(u32, u32)> | ||
/// If the `<version2>` part is omitted, the second component of the tuple is the same as | ||
/// `<version1>`. | ||
fn extract_version_range<'a, F, VersionTy: Clone>( | ||
line: &'a str, | ||
parse: F, | ||
) -> Option<(VersionTy, VersionTy)> | ||
where | ||
F: Fn(&str) -> Option<u32>, | ||
F: Fn(&'a str) -> Option<VersionTy>, | ||
{ | ||
let mut splits = line.splitn(2, "- ").map(str::trim); | ||
let min = splits.next().unwrap(); | ||
|
@@ -1273,7 +1289,7 @@ where | |
let max = match max { | ||
Some("") => return None, | ||
Some(max) => parse(max)?, | ||
_ => min, | ||
_ => min.clone(), | ||
}; | ||
|
||
Some((min, max)) | ||
|
@@ -1489,43 +1505,55 @@ fn ignore_llvm(config: &Config, line: &str) -> IgnoreDecision { | |
}; | ||
} | ||
} | ||
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 | ||
if actual_version < min_version { | ||
if let Some(actual_version) = &config.llvm_version { | ||
// Note that these `min` versions will check for not just major versions. | ||
|
||
if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") { | ||
let min_version = extract_llvm_version(&version_string); | ||
// Ignore if actual version is smaller than the minimum required version. | ||
if *actual_version < min_version { | ||
return IgnoreDecision::Ignore { | ||
reason: format!("ignored when the LLVM version is older than {rest}"), | ||
reason: format!( | ||
"ignored when the LLVM version {actual_version} is older than {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(); | ||
} else if let Some(version_string) = | ||
config.parse_name_value_directive(line, "min-system-llvm-version") | ||
{ | ||
let min_version = extract_llvm_version(&version_string); | ||
// Ignore if using system LLVM and actual version | ||
// is smaller the minimum required version | ||
if config.system_llvm && actual_version < min_version { | ||
if config.system_llvm && *actual_version < min_version { | ||
return IgnoreDecision::Ignore { | ||
reason: format!("ignored when the system LLVM version is older than {rest}"), | ||
reason: format!( | ||
"ignored when the system LLVM version {actual_version} is older than {min_version}" | ||
), | ||
}; | ||
} | ||
} else if let Some(rest) = line.strip_prefix("ignore-llvm-version:").map(str::trim) { | ||
} else if let Some(version_range) = | ||
config.parse_name_value_directive(line, "ignore-llvm-version") | ||
{ | ||
// Syntax is: "ignore-llvm-version: <version1> [- <version2>]" | ||
let (v_min, v_max) = | ||
extract_version_range(rest, extract_llvm_version).unwrap_or_else(|| { | ||
panic!("couldn't parse version range: {:?}", rest); | ||
}); | ||
extract_version_range(&version_range, |s| Some(extract_llvm_version(s))) | ||
.unwrap_or_else(|| { | ||
panic!("couldn't parse version range: \"{version_range}\""); | ||
}); | ||
if v_max < v_min { | ||
panic!("Malformed LLVM version range: max < min") | ||
panic!("malformed LLVM version range where {v_max} < {v_min}") | ||
} | ||
// Ignore if version lies inside of range. | ||
if actual_version >= v_min && actual_version <= v_max { | ||
if *actual_version >= v_min && *actual_version <= v_max { | ||
if v_min == v_max { | ||
return IgnoreDecision::Ignore { | ||
reason: format!("ignored when the LLVM version is {rest}"), | ||
reason: format!("ignored when the LLVM version is {actual_version}"), | ||
}; | ||
} else { | ||
return IgnoreDecision::Ignore { | ||
reason: format!("ignored when the LLVM version is between {rest}"), | ||
reason: format!( | ||
"ignored when the LLVM version is between {v_min} and {v_max}" | ||
), | ||
}; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.