use cpp_parser::{CppNamespaceDecl, CppParser, ParserConfig};
#[test]
#[ignore]
fn where_the_parser_puts_the_intrinsics() {
let Some(directory) = std::env::var_os("CPPLS_DUMP_RENDERING_DIR") else {
println!("skipped: CPPLS_DUMP_RENDERING_DIR is not set");
return;
};
let directory = std::path::PathBuf::from(directory);
let text = std::fs::read_to_string(directory.join("unit.stream")).expect("the dump reads");
let boundaries: Vec<usize> = std::fs::read_to_string(directory.join("unit.boundaries"))
.expect("the boundaries read")
.lines()
.filter_map(|line| line.trim().parse::<usize>().ok())
.collect();
let files: Vec<String> = std::fs::read_to_string(directory.join("unit.files"))
.expect("the file list reads")
.lines()
.map(|line| line.to_string())
.collect();
println!(
"stream {} bytes, {} boundaries, {} files",
text.len(),
boundaries.len(),
files.len()
);
let started = std::time::Instant::now();
let tree = CppParser::parse(
&text,
ParserConfig::default().with_file_boundaries(boundaries.clone()),
);
let root = tree.get_red_root();
println!(
"parsed in {:.0} ms",
started.elapsed().as_secs_f64() * 1000.0
);
println!(
"{} error(s) in the stitched stream",
tree.get_errors().len()
);
for (index, error) in tree.get_errors().iter().enumerate().take(12) {
let range = cpp_parser::source_range(error.range);
let around = text
.get(range.start_offset.saturating_sub(40)..)
.map(|rest| &rest[..rest.len().min(220)])
.unwrap_or("");
println!(" error {index} at {}: {around:?}", range.start_offset);
}
for name in ["cout", "map"] {
use cpp_parser::CppAstNode;
let found: Vec<cpp_parser::SourceRange> = root
.descendants()
.filter_map(cpp_parser::CppDeclaration::cast)
.filter(|declaration| declaration.get_name_text().as_deref() == Some(name))
.map(|declaration| cpp_parser::source_range(declaration.syntax().text_range()))
.collect();
println!(
"`{name}`: {} declaration node(s) in the tree, at {:?}",
found.len(),
found
.iter()
.take(4)
.map(|range| range.start_offset)
.collect::<Vec<_>>()
);
if let Some(range) = found.first() {
let node = root
.descendants()
.find(|node| usize::from(node.text_range().start()) == range.start_offset);
if let Some(node) = node {
let mut kinds = Vec::new();
let mut current = node.parent();
while let Some(parent) = current {
kinds.push(format!("{:?}", parent.kind()));
current = parent.parent();
}
println!(" ancestors: {kinds:?}");
}
}
}
let cout_at = text.find("extern ostream cout").unwrap_or(0);
let mut around: Vec<usize> = tree
.get_errors()
.iter()
.map(|error| cpp_parser::source_range(error.range).start_offset)
.filter(|at| *at < cout_at + 20_000 && *at > cout_at.saturating_sub(400_000))
.collect();
around.sort_unstable();
println!("errors near `cout` ({cout_at}): {around:?}");
for name in ["_mm_abs_epi16", "vector<bool, _Alloc>", "format"] {
let Some(at) = text.find(name) else {
println!("`{name}`: not in the stream");
continue;
};
let slice = boundaries.partition_point(|boundary| *boundary <= at);
let file = files.get(slice).map(String::as_str).unwrap_or("?");
let namespaces = namespaces_at(&root, at);
println!(
"`{name}` at {at} in {}: namespaces {:?}",
file.rsplit('/').next().unwrap_or(file),
namespaces
);
for (range, _) in namespace_ranges_at(&root, at) {
let opened_in = boundaries.partition_point(|boundary| *boundary <= range.start_offset);
let opened_file = files
.get(opened_in)
.map(|file| file.rsplit('/').next().unwrap_or(file).to_string())
.unwrap_or_default();
let crossings = boundaries
.iter()
.filter(|boundary| {
*boundary > &range.start_offset && *boundary < &range.end_offset()
})
.count();
println!(
" opens {} in {} — ends {}, {} boundary(ies) crossed",
range.start_offset,
opened_file,
if range.end_offset() == text.len() {
"EOF".to_string()
} else {
range.end_offset().to_string()
},
crossings
);
}
assert!(
namespaces.len() <= 1,
"`{name}` is inside {} nested namespaces ({namespaces:?}) — one of them never closed",
namespaces.len()
);
}
}
fn namespace_ranges_at(
root: &cpp_parser::CppSyntaxNode,
at: usize,
) -> Vec<(cpp_parser::SourceRange, usize)> {
use cpp_parser::CppAstNode;
let mut found = None;
for node in root.descendants() {
let range = cpp_parser::source_range(node.text_range());
if range.start_offset <= at && at <= range.end_offset() {
found = Some(node);
}
}
let mut ranges = Vec::new();
let mut node = found;
while let Some(current) = node {
if let Some(_namespace) = CppNamespaceDecl::cast(current.clone()) {
let range = cpp_parser::source_range(current.text_range());
ranges.push((range, range.start_offset));
}
node = current.parent();
}
ranges.reverse();
ranges
}
fn namespaces_at(root: &cpp_parser::CppSyntaxNode, at: usize) -> Vec<String> {
use cpp_parser::CppAstNode;
let mut found = None;
for node in root.descendants() {
let range = cpp_parser::source_range(node.text_range());
if range.start_offset <= at && at <= range.end_offset() {
found = Some(node);
}
}
let mut names = Vec::new();
let mut node = found;
while let Some(current) = node {
if let Some(namespace) = CppNamespaceDecl::cast(current.clone())
&& let Some(name) = namespace.get_name_text()
{
names.push(name);
}
node = current.parent();
}
names.reverse();
names
}