(file_path: &str, source: &str, language: &str)
| 399 | } |
| 400 | |
| 401 | pub fn extract(file_path: &str, source: &str, language: &str) -> Result<EmitOut, String> { |
| 402 | let variant = match language { |
| 403 | "c" => Variant::C, |
| 404 | "cpp" => Variant::Cpp, |
| 405 | other => return Err(format!("ccpp walker got language '{other}'")), |
| 406 | }; |
| 407 | let grammar = crate::langs::grammar_for(language).ok_or("no c/cpp grammar")?; |
| 408 | let t0 = std::time::Instant::now(); |
| 409 | let mut parser = Parser::new(); |
| 410 | parser |
| 411 | .set_language(&grammar) |
| 412 | .map_err(|e| format!("set_language({language}) failed: {e}"))?; |
| 413 | let tree = parser |
| 414 | .parse(source, None) |
| 415 | .ok_or_else(|| "parser returned null tree".to_string())?; |
| 416 | // Measurement hatch (parity sweeps only — never set in production): skip |
| 417 | // the defer so the sweep can QUANTIFY how often UTF-8 vs UTF-16 error |
| 418 | // recovery actually diverges on this language's erroring files. |
| 419 | let no_defer = std::env::var("CODEGRAPH_KERNEL_CCPP_ERROR_EXTRACT").as_deref() == Ok("1"); |
| 420 | if tree.root_node().has_error() && !no_defer { |
| 421 | return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string()); |
| 422 | } |
| 423 | |
| 424 | let brace_scopes = if variant == Variant::Cpp && tree.root_node().has_error() { |
| 425 | brace_scopes::scan(source) |
| 426 | } else { |
| 427 | None |
| 428 | }; |
| 429 | let walk_declared_types = |
| 430 | variant == Variant::C || !tree.root_node().has_error() || brace_scopes.is_some(); |
| 431 | let mut w = Walker { |
| 432 | src: source, |
| 433 | file_path, |
| 434 | variant, |
| 435 | line_starts: util::line_starts(source), |
| 436 | arena: Arena::default(), |
| 437 | node_id_allocator: ids::NodeIdAllocator::default(), |
| 438 | tables: Tables::default(), |
| 439 | stack: Vec::new(), |
| 440 | nodes_meta: Vec::new(), |
| 441 | node_ids: Vec::new(), |
| 442 | namespace_prefix: Vec::new(), |
| 443 | local_fn_ptrs: HashMap::new(), |
| 444 | defined_fn_names: HashSet::new(), |
| 445 | imported_names: HashSet::new(), |
| 446 | fn_ref_cands: Vec::new(), |
| 447 | fs_values: HashMap::new(), |
| 448 | fs_value_counts: HashMap::new(), |
| 449 | value_scopes: Vec::new(), |
| 450 | brace_scopes, |
| 451 | class_scopes: NestedIntervals::new(), |
| 452 | class_scope_rows: HashSet::new(), |
| 453 | walk_declared_types, |
| 454 | }; |
| 455 | |
| 456 | let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1; |
| 457 | let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path); |
| 458 | let mut flags = BoolFlags::default(); |
nothing calls this directly
no test coverage detected