use std::collections::HashMap;
use std::path::{Path, PathBuf};
fn headers_under(directory: &Path, into: &mut Vec<PathBuf>, limit: usize) {
if into.len() >= limit {
return;
}
let Ok(entries) = std::fs::read_dir(directory) else {
return;
};
let mut children: Vec<PathBuf> = entries.flatten().map(|entry| entry.path()).collect();
children.sort();
for path in children {
if into.len() >= limit {
return;
}
if path.is_dir() {
headers_under(&path, into, limit);
continue;
}
let Some(extension) = path.extension().and_then(|extension| extension.to_str()) else {
continue;
};
if matches!(
extension.to_ascii_lowercase().as_str(),
"h" | "hpp" | "hh" | "hxx" | "inl" | "ipp" | "ixx" | "cppm"
) {
into.push(path);
}
}
}
fn the_token(text: &str, start: usize, end: usize) -> String {
let mut at = start.min(text.len());
while at > 0 && !text.is_char_boundary(at) {
at -= 1;
}
let slice = text.get(at..end.max(at).min(text.len())).unwrap_or("");
let word: String = slice
.chars()
.take_while(|character| character.is_alphanumeric() || *character == '_')
.collect();
if word.is_empty() {
slice.chars().take(12).collect()
} else {
word
}
}
fn a_macro_shadows(text: &str, at: usize) -> bool {
let before = &text[..at.min(text.len())];
let script: Vec<&str> = before
.split(|character: char| !(character.is_alphanumeric() || character == '_'))
.filter(|word| !word.is_empty())
.rev()
.take(4)
.collect();
script.iter().any(|word| {
let defined = text.lines().any(|line| {
line.trim_start()
.strip_prefix("#define")
.map(|rest| rest.trim_start().starts_with(word))
.unwrap_or(false)
});
defined
|| (word
.chars()
.next()
.is_some_and(|c| c.is_ascii_uppercase() || c == '_')
&& word
.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_'))
})
}
fn around(text: &str, start: usize) -> String {
let from = text[..start.min(text.len())]
.char_indices()
.rev()
.nth(40)
.map(|(index, _)| index)
.unwrap_or(0);
let to = (start + 80).min(text.len());
text.get(from..to).unwrap_or("").replace('\n', " ")
}
#[test]
#[ignore]
fn what_a_toolchains_headers_do_not_parse() {
let Some(directory) = std::env::var_os("CPPLS_SWEEP_DIR").map(PathBuf::from) else {
println!("skipped: CPPLS_SWEEP_DIR is not set");
return;
};
let limit: usize = std::env::var("CPPLS_SWEEP_LIMIT")
.ok()
.and_then(|limit| limit.parse().ok())
.unwrap_or(4000);
let mut headers = Vec::new();
headers_under(&directory, &mut headers, limit);
let mut ranked: HashMap<(String, String), (usize, String)> = HashMap::new();
let mut files = 0usize;
let mut files_with_errors = 0usize;
let mut errors = 0usize;
let mut macro_shaped = 0usize;
for header in &headers {
let Ok(text) = std::fs::read_to_string(header) else {
continue;
};
{
let first = text
.lines()
.map(str::trim)
.find(|line| !line.is_empty() && !line.starts_with("//") && !line.starts_with("/*"))
.unwrap_or("");
if first.starts_with('{')
|| first.starts_with('}')
|| first.starts_with(',')
|| first.starts_with(';')
|| text.contains("not a public header")
|| text.contains("do not include it directly")
{
continue;
}
}
files += 1;
let tree = cpp_parser::CppParser::parse(&text, cpp_parser::ParserConfig::default());
let found = tree.get_errors();
if !found.is_empty() {
files_with_errors += 1;
}
errors += found.len();
for error in found {
let range = cpp_parser::source_range(error.range);
if a_macro_shadows(&text, range.start_offset) {
macro_shaped += 1;
continue;
}
let token = the_token(&text, range.start_offset, range.end_offset());
let header = header
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default();
let entry = ranked
.entry((error.message.clone(), token))
.or_insert_with(|| {
(
0,
format!("{header}: {}", around(&text, range.start_offset)),
)
});
entry.0 += 1;
}
}
println!(
"{} file(s) under {}, {} with errors, {} error(s) in total, {} of them macro-shaped (not defects)",
files,
directory.display(),
files_with_errors,
errors,
macro_shaped
);
let mut rows: Vec<((String, String), (usize, String))> = ranked.into_iter().collect();
rows.sort_unstable_by_key(|(_, (count, _))| std::cmp::Reverse(*count));
println!(
"
{} distinct construct(s):",
rows.len()
);
for ((message, token), (count, example)) in rows.iter().take(40) {
println!("{count:6} `{token}` {message}\n …{example}…");
}
}