use std::path::{Path, PathBuf};
use cpp_code_analysis::{DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter};
fn project() -> Option<PathBuf> {
let named = std::env::var_os("CPPLS_MODULES_PROJECT")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"C:\Users\zc\Desktop\cpp_modules"));
named
.join("src")
.join("report.cppm")
.is_file()
.then_some(named)
}
fn name_of(path: &str) -> String {
Path::new(path)
.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| path.to_string())
}
fn kind_name(kind: cpp_code_analysis::DeclKind) -> &'static str {
match kind {
cpp_code_analysis::DeclKind::Type => "Type",
cpp_code_analysis::DeclKind::Function => "Function",
cpp_code_analysis::DeclKind::Variable => "Variable",
cpp_code_analysis::DeclKind::Namespace => "Namespace",
_ => "Other",
}
}
#[test]
#[ignore = "a probe: needs the external fixture"]
fn what_the_index_of_std_holds_and_who_can_see_it() {
let Some(root) = project() else {
eprintln!("no fixture: set CPPLS_MODULES_PROJECT");
return;
};
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
let interface = root.join("src").join("report.cppm");
let main = root.join("src").join("main.cpp");
let text = std::fs::read_to_string(&interface).expect("report.cppm");
session.did_open(&interface, &text);
session.add_project_files([interface.clone(), main.clone()]);
session.index_everything();
println!(
"module read: {:?}",
session.read_the_modules_a_file_imports(&interface)
);
for (label, file) in [("main.cpp", &main), ("report.cppm", &interface)] {
if session.index().summary(file).is_none() {
continue;
}
let holders = session.index().declarations_in("std", file);
let mut found: Vec<String> = Vec::new();
for wanted in [
"string",
"string_view",
"size_t",
"vector",
"format",
"stringbuf",
"strong_ordering",
] {
let kinds: Vec<&str> = holders
.iter()
.filter(|declaration| declaration.fact.name.as_str() == wanted)
.map(|declaration| kind_name(declaration.fact.kind))
.collect();
found.push(format!("{wanted}={kinds:?}"));
}
println!(
" {label}: {} visible file(s), {} in `std`: {}",
session.index().visible_files(file).len(),
holders.len(),
found.join(" ")
);
}
let interface_files: std::collections::BTreeSet<String> = session
.index()
.visible_files(&interface)
.into_iter()
.map(|(path, _)| name_of(&path))
.collect();
let main_files: std::collections::BTreeSet<String> = session
.index()
.visible_files(&main)
.into_iter()
.map(|(path, _)| name_of(&path))
.collect();
let only_main: Vec<&String> = main_files.difference(&interface_files).collect();
println!(
" in main and not in the interface ({}): {only_main:?}",
only_main.len()
);
let implementation = root.join("src").join("report.cpp");
for path in [&interface, &implementation] {
let Some(summary) = session.index().summary(path) else {
continue;
};
let facts: Vec<String> = summary
.declarations
.iter()
.filter(|fact| fact.name.as_str() == "joined" || fact.name.as_str() == "add")
.map(|fact| format!("{} scope {:?}", fact.name, fact.scope))
.collect();
println!(
" {}: {facts:?}",
path.file_name().unwrap().to_string_lossy()
);
}
for typed in [
"std::",
"std::s",
"std::st",
"std::str",
"std::string",
"std::string_v",
] {
let probe = format!("{text}\nvoid probe_scope() {{\n {typed}\n}}\n");
session.did_open(&interface, &probe);
session.index_everything();
let view = session.view_of_the_file(&interface).expect("a view");
let offset = probe.rfind(typed).expect("the prefix") + typed.len();
let started = std::time::Instant::now();
let offered = session.completions(
&view,
cpp_code_analysis::file::coordinates::FileOffset::new(offset),
);
println!(
" `{typed}`: {} item(s), truncated {}, {:?}",
offered.items.len(),
offered.truncated,
started.elapsed()
);
}
{
let holders = session.index().declarations_in("std", &interface);
let intrinsics: Vec<&str> = holders
.iter()
.filter(|declaration| declaration.fact.name.starts_with("_mm"))
.map(|declaration| declaration.fact.name.as_str())
.take(10)
.collect();
println!(
" `_mm*` in the index of `std`: {} total, first {:?}",
holders
.iter()
.filter(|declaration| declaration.fact.name.starts_with("_mm"))
.count(),
intrinsics
);
}
for (file, fact) in session.index().every_fact_named("_mm_abs_epi16") {
println!(
" `_mm_abs_epi16`: {} scope {:?} kind {:?}",
file.display(),
fact.scope,
fact.kind
);
}
for typed in ["std::", "std::s"] {
let probe = format!("{text}\\nvoid probe_scope() {{\\n {typed}\\n}}\\n");
session.did_open(&interface, &probe);
session.index_everything();
let view = session.view_of_the_file(&interface).expect("a view");
let offset = probe.rfind(typed).expect("the prefix") + typed.len();
let offered = session.completions(
&view,
cpp_code_analysis::file::coordinates::FileOffset::new(offset),
);
let labels: Vec<String> = offered
.items
.iter()
.take(20)
.map(|item| item.label.clone())
.collect();
println!(
" `{typed}` first 20 (scope {:?}, prefix {:?}): {labels:?}",
offered.scope, offered.prefix
);
}
let typed = "std::str";
let probe = format!("{text}\nvoid probe_scope() {{\n {typed}\n}}\n");
session.did_open(&interface, &probe);
session.index_everything();
let view = session.view_of_the_file(&interface).expect("a view");
let offset = probe.rfind(typed).expect("the prefix") + typed.len();
let offered = session.completions(
&view,
cpp_code_analysis::file::coordinates::FileOffset::new(offset),
);
let labels: Vec<String> = offered
.items
.iter()
.map(|item| item.label.clone())
.collect();
println!(
"completion after `{typed}` in report.cppm: {} item(s) [{}]",
labels.len(),
labels[..labels.len().min(14)].join(", ")
);
}
#[test]
#[ignore = "a probe: needs the machine's standard library, and costs ~6 s"]
fn std_endl_resolves_through_the_servers_path() {
let root = std::env::temp_dir().join("cppls-std-endl");
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("a project directory");
let file = root.join("endl.cpp");
let source = "#include <iostream>\nvoid f() { std::cout << std::endl; }\n";
std::fs::write(&file, source).expect("the fixture writes");
let mut session = Session::open(
&root,
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
session.add_project_files([file.clone()]);
session.did_open(&file, source);
session.catch_up(&file);
let _ = session.cook(&file);
session.want_cooked_reading(&file);
for _ in 0..6 {
session.index_everything();
if session.pending_work() == 0 {
break;
}
}
let found = session.index().definition("std::endl", &file);
eprintln!("[std] std::endl -> {found:?}");
assert!(
matches!(found, cpp_code_analysis::Known::Yes(_)),
"std::endl is declared in <__msvc_ostream.hpp> and reached through <ostream>: {found:?}"
);
let _ = std::fs::remove_dir_all(&root);
}