use std::path::PathBuf;
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
fn text_before(path: &std::path::Path, at: usize) -> usize {
let Ok(text) = std::fs::read_to_string(path) else {
return 0;
};
let end = at.min(text.len());
text[..end].rfind('\n').map_or(0, |line| line + 1)
}
fn project() -> PathBuf {
std::env::var_os("CPPLS_NEOVIM")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"C:\Users\zc\Desktop\neovim_test\neovim"))
}
#[test]
#[ignore = "a probe: needs the neovim checkout"]
fn why_the_two_platform_includes_still_warn() {
let root = project();
let files = ["src/nvim/log.c", "src/nvim/mbyte.c"];
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files(files.iter().map(|file| root.join(file)));
for _ in 0..8 {
session.index_everything();
if session.pending_work() == 0 {
break;
}
}
let environment = session.index().macros();
eprintln!(
"[guard] the compilation environment is {}",
if environment.is_incomplete() {
"INCOMPLETE (a name nobody mentions is Unknown)"
} else {
"complete (a name nobody mentions is not defined)"
}
);
for (file, spelling, name) in [
("src/nvim/log.c", "execinfo.h", "HAVE_EXECINFO_BACKTRACE"),
("src/nvim/mbyte.c", "langinfo.h", "HAVE_LANGINFO_H"),
] {
let path = root.join(file);
let Some(summary) = session.index().summary(&path) else {
eprintln!("[guard] {file}: no summary — the file was not read");
continue;
};
let Some(include) = summary.includes.iter().find(|it| it.spelling == spelling) else {
eprintln!("[guard] {file}: no include of {spelling}");
continue;
};
let at = include.range.start_offset;
let taken = session.index().the_guard_is_taken(&path, include.guard, at);
let here = session.index().macros_at(&path, at);
let before = text_before(&path, at);
let earlier = session.index().macros_at(&path, before);
eprintln!(
"[guard] walk state: complete_before={} incomplete_here={} base_complete={}",
!earlier.is_incomplete(),
here.is_incomplete(),
!environment.is_incomplete()
);
eprintln!(
"[guard] {file}: {spelling} guard {:?} (region {}) -> taken={taken}; {name}: unknown={} mentioned={}",
include.guard,
match include.guard {
cpp_code_analysis::FactGuard::Region(region) => region as usize,
cpp_code_analysis::FactGuard::Unconditional => usize::MAX,
},
here.is_unknown(name),
here.names_the_compilation(name),
);
if let cpp_code_analysis::FactGuard::Region(region) = include.guard
&& let Some(range) = summary.guards.regions.get(region as usize)
&& let Some(text) = std::fs::read_to_string(&path).ok()
&& let Some(condition) = text.get(range.start_offset..range.end_offset())
{
eprintln!("[guard] condition: {}", condition.trim());
}
}
}
#[test]
#[ignore = "a probe: needs the neovim checkout"]
fn which_include_in_the_closure_does_not_resolve() {
let root = project();
let start = root.join("src/nvim/log.c");
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files([start.clone()]);
for _ in 0..8 {
session.index_everything();
if session.pending_work() == 0 {
break;
}
}
let mut pending = vec![start.clone()];
let mut seen: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
let mut missing: Vec<(PathBuf, String)> = Vec::new();
let mut read = 0usize;
while let Some(path) = pending.pop() {
if !seen.insert(path.clone()) {
continue;
}
let summary = match session.index().summary(&path) {
Some(summary) => summary.clone(),
None => {
session.add_project_files([path.clone()]);
for _ in 0..4 {
session.index_everything();
if session.index().summary(&path).is_some() || session.pending_work() == 0 {
break;
}
}
read += 1;
match session.index().summary(&path) {
Some(summary) => summary.clone(),
None => continue,
}
}
};
for include in &summary.includes {
match &include.resolved {
Some(target) => pending.push(target.clone()),
None => missing.push((path.clone(), include.spelling.clone())),
}
}
}
eprintln!(
"[closure] {read} file(s) read on demand, {} unresolved include(s) in {} file(s)",
missing.len(),
seen.len()
);
missing.sort();
missing.dedup();
for (file, spelling) in missing.iter().take(40) {
eprintln!(
"[closure] {} -> {spelling}",
file.strip_prefix(&root).unwrap_or(file).display()
);
}
}