use std::collections::HashMap;
use std::path::{Path, PathBuf};
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
fn project() -> PathBuf {
PathBuf::from(
std::env::var("CPPLS_REAL_PROJECT")
.unwrap_or_else(|_| r"C:\Users\zc\Desktop\test_cpp".to_string()),
)
}
fn entries(root: &Path) -> Vec<PathBuf> {
std::env::var("CPPLS_UNIT_ENTRIES")
.map(|list| {
list.split(';')
.filter(|entry| !entry.is_empty())
.map(|entry| root.join(entry))
.collect()
})
.unwrap_or_else(|_| vec![root.join("src/containers.cpp"), root.join("src/basics.cpp")])
}
type Keys = HashMap<PathBuf, (u64, u64)>;
fn keys_of(session: &Session, root: &Path) -> Keys {
let Some(unit) = session.translation_unit_of(root) else {
println!("{}: no unit", root.display());
return Keys::new();
};
let mut keys = Keys::new();
for file in unit.files() {
let Some(view) = unit.environment_of(file) else {
continue;
};
let Ok(text) = std::fs::read_to_string(file) else {
continue;
};
let (tokens, _) = cpp_parser::lex(&text, &cpp_parser::LexerConfig::default());
let content = cpp_code_analysis::content_hash(&text);
keys.insert(
file.to_path_buf(),
(content, view.environment_fingerprint(&text, &tokens)),
);
}
keys
}
#[test]
#[ignore = "a probe: opens a real project and prints instead of asserting"]
fn how_much_of_one_program_is_another_programs() {
let root = project();
let entries = entries(&root);
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files(entries.clone());
session.index_everything();
let mut slices = 0;
while session.pending_work() > 0 && slices < 10_000 {
session.advance(256);
slices += 1;
}
let mut per_root: Vec<(PathBuf, Keys)> = Vec::new();
for entry in &entries {
let started = std::time::Instant::now();
let keys = keys_of(&session, entry);
println!(
"{}: {} file(s), {} distinct key(s), {:?}",
entry.display(),
keys.len(),
keys.values()
.collect::<std::collections::HashSet<_>>()
.len(),
started.elapsed(),
);
per_root.push((entry.clone(), keys));
}
let (Some((left_root, left)), Some((right_root, right))) = (per_root.first(), per_root.get(1))
else {
return;
};
let shared: Vec<&PathBuf> = left
.keys()
.filter(|path| right.contains_key(*path))
.collect();
let agreeing: Vec<&&PathBuf> = shared
.iter()
.filter(|path| left.get(**path) == right.get(**path))
.collect();
println!(
"{} vs {}: {} file(s) in both, {} of them with the same text and environment",
left_root.display(),
right_root.display(),
shared.len(),
agreeing.len()
);
let mut differing: Vec<&&PathBuf> = shared
.iter()
.filter(|path| left.get(**path) != right.get(**path))
.collect();
differing.sort();
for path in differing.iter().take(12) {
println!(
" differs: {}",
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_default()
);
}
let unique: usize = per_root.iter().map(|(_, keys)| keys.len()).sum::<usize>() - shared.len();
let distinct = per_root
.iter()
.flat_map(|(_, keys)| keys.values().copied())
.collect::<std::collections::HashSet<_>>()
.len();
println!(
"{unique} per-file reading(s) belong to one program only, and the two together need {distinct} key(s)"
);
}