From a0cdbc9100947fc1d9d030debd8f31da46fdd1e4 Mon Sep 17 00:00:00 2001 From: Stephen Nicholas Swatman Date: Tue, 18 Jun 2024 11:31:28 +0200 Subject: [PATCH] Increase robustness to exotic clang versions As seen in #162, not all clang installations neatly follow the "clang version MAJOR.MINOR" template. This causes crofiler to fail when using, e.g., Debian's provided version of clang. This commit increases the robustness of the version checking by relying on a regular expression which can match a broader range of possible versions. Resolves #162. --- Cargo.toml | 3 +++ src/clang.rs | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 271d1ad..947547b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,9 @@ env_logger = "0.10" # Used for logging log = "0.4" +# Used to parse versions +regex = "1.10" + # Used for logging in TUI mode syslog = "6.1" diff --git a/src/clang.rs b/src/clang.rs index cf89c37..2d3b0fd 100644 --- a/src/clang.rs +++ b/src/clang.rs @@ -1,5 +1,6 @@ //! Interactions with clang +use regex::Regex; use std::{ io, process::{Command, ExitStatus}, @@ -31,9 +32,11 @@ pub fn find_clangpp() -> Result + 'static, ClangError> { Some(line) => line, None => return Err(ClangError::EmptyVersion), }; - if let Some(version) = version_line.strip_prefix("clang version ") { - if let Some((major_version, _)) = version.split_once('.') { - if let Ok(major_version) = major_version.parse::() { + let version_regex = + Regex::new(r"clang version (?\d+)\.(?\d+)").unwrap(); + if let Some(captures) = version_regex.captures(version_line) { + if let Some(major_version) = captures.name("major_version") { + if let Ok(major_version) = major_version.as_str().parse::() { if major_version >= MIN_VERSION { return Ok(CLANG_PROGRAM); } else {