pub(crate) fn is_cfg_gated(file_path: &str, fn_line: u32) -> bool {
let content = match std::fs::read_to_string(file_path) {
Ok(c) => c,
Err(_) => return false,
};
let lines: Vec<&str> = content.lines().collect();
let fn_idx = fn_line.saturating_sub(1) as usize;
has_cfg_attribute_above(&lines, fn_idx) || is_inside_cfg_module(&lines, fn_idx)
}
fn is_conditional_attr(trimmed: &str) -> bool {
trimmed.starts_with("#[cfg(")
|| trimmed.starts_with("#[cfg_attr(")
|| trimmed.starts_with("#[target_feature(")
}
fn is_annotation_or_preamble(trimmed: &str) -> bool {
trimmed.is_empty()
|| trimmed.starts_with("#[")
|| trimmed.starts_with("///")
|| trimmed.starts_with("//!")
|| trimmed.starts_with("//")
|| trimmed.starts_with("pub ")
|| trimmed.starts_with("pub(")
|| trimmed.starts_with("async ")
|| trimmed.starts_with("unsafe ")
|| trimmed == "{"
|| trimmed == "}"
}
fn has_cfg_attribute_above(lines: &[&str], fn_idx: usize) -> bool {
let start = fn_idx.saturating_sub(10);
for i in start..fn_idx {
let Some(line) = lines.get(i) else { continue };
let trimmed = line.trim();
if is_conditional_attr(trimmed) {
return true;
}
if !is_annotation_or_preamble(trimmed) {
break;
}
}
false
}
fn is_inside_cfg_module(lines: &[&str], fn_idx: usize) -> bool {
let mut brace_depth: i32 = 0;
let mut cfg_depth: Option<i32> = None;
let mut pending_cfg = false;
for (i, line) in lines.iter().enumerate() {
if i > fn_idx {
break;
}
let trimmed = line.trim();
if is_conditional_attr(trimmed) {
pending_cfg = true;
continue;
}
if pending_cfg && is_cfg_mod_start(trimmed) {
cfg_depth = Some(brace_depth);
}
pending_cfg = false;
brace_depth = update_brace_depth(trimmed, brace_depth, &mut cfg_depth);
}
cfg_depth.is_some()
}
fn is_cfg_mod_start(trimmed: &str) -> bool {
(trimmed.starts_with("mod ") || trimmed.starts_with("pub mod ")) && trimmed.contains('{')
}
fn update_brace_depth(trimmed: &str, mut depth: i32, cfg_depth: &mut Option<i32>) -> i32 {
for ch in trimmed.chars() {
match ch {
'{' => depth += 1,
'}' => {
depth -= 1;
if let Some(d) = *cfg_depth {
if depth <= d {
*cfg_depth = None;
}
}
}
_ => {}
}
}
depth
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_cfg_gated_direct_cfg() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.rs");
std::fs::write(
&test_file,
"#[cfg(target_arch = \"x86_64\")]\nfn simd_func() {}\n",
)
.unwrap();
assert!(is_cfg_gated(&test_file.to_string_lossy(), 2));
}
#[test]
fn test_is_cfg_gated_target_feature() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.rs");
std::fs::write(
&test_file,
"#[target_feature(enable = \"avx2\")]\nunsafe fn avx2_func() {}\n",
)
.unwrap();
assert!(is_cfg_gated(&test_file.to_string_lossy(), 2));
}
#[test]
fn test_is_cfg_gated_cfg_attr() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.rs");
std::fs::write(
&test_file,
"#[cfg_attr(feature = \"simd\", target_feature(enable = \"sse4.1\"))]\nfn conditional_simd() {}\n",
)
.unwrap();
assert!(is_cfg_gated(&test_file.to_string_lossy(), 2));
}
#[test]
fn test_is_cfg_gated_not_gated() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.rs");
std::fs::write(&test_file, "fn normal_func() {}\n").unwrap();
assert!(!is_cfg_gated(&test_file.to_string_lossy(), 1));
}
#[test]
fn test_is_cfg_gated_module_level() {
use tempfile::TempDir;
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.rs");
std::fs::write(
&test_file,
"#[cfg(target_arch = \"x86_64\")]\nmod simd {\n fn inner_func() {}\n}\n",
)
.unwrap();
assert!(is_cfg_gated(&test_file.to_string_lossy(), 3));
}
#[test]
fn test_is_inside_cfg_module_basic() {
let lines: Vec<&str> = vec![
"#[cfg(target_arch = \"x86_64\")]",
"mod simd {",
" fn a() {}",
" fn b() {}",
"}",
"fn outside() {}",
];
assert!(is_inside_cfg_module(&lines, 2));
assert!(is_inside_cfg_module(&lines, 3));
assert!(!is_inside_cfg_module(&lines, 5));
}
#[test]
fn test_is_inside_cfg_module_no_cfg() {
let lines: Vec<&str> = vec!["mod normal {", " fn inside() {}", "}"];
assert!(!is_inside_cfg_module(&lines, 1));
}
}