Skip to main content

brokk_bifrost_cpp/
adapter.rs

1//! The C++ answers behind `CppAdapter`.
2//!
3//! `LanguageAdapter` is analysis-owned, so the trait impl itself stays in
4//! `analyzer/cpp/adapter.rs`; every answer it gives comes from here or from
5//! [`crate::test_detection`] and [`crate::queries`].
6
7use crate::declarations::{
8    CppVisitor, collect_cpp_identifiers, collect_cpp_includes, recover_quoted_includes,
9};
10use brokk_bifrost_core::analyzer::ProjectFile;
11use brokk_bifrost_core::analyzer::cognitive_complexity;
12use brokk_bifrost_core::analyzer::model::{Language, LanguageDialect};
13use brokk_bifrost_core::analyzer::parsed_file::ParsedFile;
14use brokk_bifrost_core::analyzer::tree_walk::ParentIndex;
15use brokk_bifrost_core::hash::HashMap;
16use std::sync::LazyLock;
17use tree_sitter::{Node, Tree};
18
19/// The file extension `CppAdapter` reports. `Language::Cpp` also covers `.c`,
20/// `.cc`, `.cxx` and the header spellings; this is only the canonical one.
21pub const CPP_FILE_EXTENSION: &str = "cpp";
22
23/// Tree-sitter node-kind mapping used by the cognitive-complexity scorer for
24/// C++. Node names are from the tree-sitter-cpp grammar.
25pub static CPP_COGNITIVE_CONFIG: LazyLock<cognitive_complexity::Config> =
26    LazyLock::new(|| cognitive_complexity::Config {
27        if_types: &["if_statement"],
28        loop_types: &["for_statement", "while_statement", "do_statement"],
29        catch_types: &["catch_clause"],
30        conditional_types: &["conditional_expression"],
31        case_types: &["case_statement"],
32        binary_types: &["binary_expression"],
33        logical_operators: &["&&", "||", "and", "or"],
34        jump_types: &["break_statement", "continue_statement"],
35        named_function_boundary_types: &["function_definition"],
36        anonymous_function_types: &["lambda_expression"],
37        else_clause_types: &["else_clause"],
38        default_case_predicate: Some(cpp_is_default_case),
39        ..cognitive_complexity::Config::empty()
40    });
41
42fn cpp_is_default_case(node: Node<'_>, _source: &str) -> bool {
43    node.child_by_field_name("value").is_none()
44}
45
46/// Extract `file` under the dialect its own path selects.
47pub fn parse_cpp_file(file: &ProjectFile, source: &str, tree: &Tree) -> ParsedFile {
48    parse_cpp_file_in_dialect(
49        file,
50        source,
51        tree,
52        LanguageDialect::for_path(Language::Cpp, file.rel_path()),
53    )
54}
55
56/// Extract `file` under an explicitly named dialect.
57///
58/// A header carries no compilation language of its own, so its blob has two
59/// legitimate readings: under [`LanguageDialect::CppC`] a tag declared inside
60/// an aggregate member list has file scope (C17 6.2.1), under the plain C++
61/// dialect it is a nested class. Milestone 3 of
62/// `.agents/plans/c-compilation-language-tag-scope.md` stores both readings of
63/// a header when they differ, so extraction has to be reachable under a
64/// dialect the path itself does not name.
65///
66/// This entry point owns the whole tree's work, including the parent index the
67/// walk asks its ancestor questions of. A caller that wants both readings of
68/// one tree must use [`parse_cpp_file_with_ancestry`] and
69/// [`parse_cpp_c_reading`] instead, which share that index.
70pub fn parse_cpp_file_in_dialect(
71    file: &ProjectFile,
72    source: &str,
73    tree: &Tree,
74    dialect: LanguageDialect,
75) -> ParsedFile {
76    let root = tree.root_node();
77    let ancestry = ParentIndex::new(root);
78    parse_cpp_reading(file, source, root, dialect, &ancestry)
79}
80
81/// Extract `file` under the dialect its own path selects, asking its ancestor
82/// questions of a caller-owned index over the same tree.
83///
84/// The index costs one hash entry per node and is a property of the tree, not
85/// of the reading, so a header that gets both readings builds it once. See
86/// [`parse_cpp_c_reading`] for the second reading.
87pub fn parse_cpp_file_with_ancestry<'tree>(
88    file: &ProjectFile,
89    source: &str,
90    root: Node<'tree>,
91    ancestry: &ParentIndex<'tree>,
92) -> ParsedFile {
93    parse_cpp_reading(
94        file,
95        source,
96        root,
97        LanguageDialect::for_path(Language::Cpp, file.rel_path()),
98        ancestry,
99    )
100}
101
102/// The C reading of a tree whose other reading is already in hand.
103///
104/// The dialect decides exactly one thing: where a struct/union/enum tag
105/// declared inside another aggregate's member list is declared (the
106/// `c_tag_semantics` readers in [`crate::declarations`]). Everything else this
107/// file's extraction produces is a property of the tree and of the source text
108/// -- the parent index, the `#include` sweep, the identifier sweep, and the
109/// quoted-include line recovery -- so the C reading takes those from `primary`
110/// rather than recomputing them over the same bytes. A debug build re-runs the
111/// sweeps and asserts they agree, which is what holds the claim honest if a
112/// future walk ever starts contributing to one of those families.
113pub fn parse_cpp_c_reading<'tree>(
114    file: &ProjectFile,
115    source: &str,
116    root: Node<'tree>,
117    ancestry: &ParentIndex<'tree>,
118    primary: &ParsedFile,
119) -> ParsedFile {
120    let mut parsed = ParsedFile::new(String::new());
121    parsed.imports = primary.imports.clone();
122    parsed.type_identifiers = primary.type_identifiers.clone();
123    walk_cpp_declarations(
124        file,
125        source,
126        root,
127        LanguageDialect::CppC,
128        ancestry,
129        &mut parsed,
130    );
131    parsed.finalize_deferred_replacements();
132
133    #[cfg(debug_assertions)]
134    {
135        let mut recomputed = ParsedFile::new(String::new());
136        collect_cpp_includes(root, source, &mut recomputed);
137        collect_cpp_identifiers(root, source, &mut recomputed.type_identifiers);
138        recover_quoted_includes(source, &mut recomputed);
139        assert_eq!(
140            parsed.imports, recomputed.imports,
141            "the C reading's includes are the C++ reading's includes: {:?}",
142            file
143        );
144        assert_eq!(
145            parsed.type_identifiers, recomputed.type_identifiers,
146            "the C reading's identifiers are the C++ reading's identifiers: {:?}",
147            file
148        );
149    }
150
151    parsed
152}
153
154/// One complete reading of `root`, sweeps included.
155fn parse_cpp_reading<'tree>(
156    file: &ProjectFile,
157    source: &str,
158    root: Node<'tree>,
159    dialect: LanguageDialect,
160    ancestry: &ParentIndex<'tree>,
161) -> ParsedFile {
162    let mut parsed = ParsedFile::new(String::new());
163
164    collect_cpp_includes(root, source, &mut parsed);
165    collect_cpp_identifiers(root, source, &mut parsed.type_identifiers);
166
167    walk_cpp_declarations(file, source, root, dialect, ancestry, &mut parsed);
168    // A line scan over the source rather than a tree walk: it recovers the
169    // quoted directives a parse error hid from the tree, skipping any snippet
170    // the sweep above already recorded.
171    recover_quoted_includes(source, &mut parsed);
172    parsed.finalize_deferred_replacements();
173    parsed
174}
175
176/// The declaration walk itself: the only part of an extraction the dialect
177/// changes. The caller finalizes, because the primary reading recovers its
178/// quoted includes between the walk and that compaction.
179fn walk_cpp_declarations<'tree>(
180    file: &ProjectFile,
181    source: &str,
182    root: Node<'tree>,
183    dialect: LanguageDialect,
184    ancestry: &ParentIndex<'tree>,
185    parsed: &mut ParsedFile,
186) {
187    let mut visitor = CppVisitor {
188        file,
189        source,
190        parsed,
191        c_tag_semantics: dialect == LanguageDialect::CppC,
192        recovered_class_sibling_scopes: HashMap::default(),
193        consumed_fragment_regions: Vec::new(),
194        namespace_forward_scans: HashMap::default(),
195        field_owners: None,
196        recovery_captures: Vec::new(),
197    };
198    visitor.visit_container(root, ancestry, "", None, None, None, Vec::new());
199}
200
201/// Whether two readings of one blob disagree about any identity-bearing
202/// output: which declarations exist, what they are named, which are top level
203/// or definition-lookup entries, where they start and end, what they are
204/// nested in, and how they are signed.
205///
206/// This is the "differs" test behind storing a header's C projection only when
207/// it says something the C++ projection does not (issue #1970): absence of the
208/// second row-set must unambiguously mean "identical", so anything a
209/// resolution surface can observe has to be compared here.
210pub fn cpp_projections_differ(left: &ParsedFile, right: &ParsedFile) -> bool {
211    left.declarations() != right.declarations()
212        || left.top_level_declarations != right.top_level_declarations
213        || left.definition_lookup_units != right.definition_lookup_units
214        || left.children != right.children
215        || left.ranges != right.ranges
216        || left.signatures != right.signatures
217        || left.type_aliases != right.type_aliases
218}
219
220pub fn cpp_extract_call_receiver(reference: &str) -> Option<String> {
221    let trimmed = reference.trim();
222    let before_args = trimmed
223        .split_once('(')
224        .map(|(head, _)| head)
225        .unwrap_or(trimmed);
226    before_args
227        .rsplit_once("::")
228        .or_else(|| before_args.rsplit_once('.'))
229        .map(|(receiver, _)| receiver.to_string())
230}
231
232#[cfg(test)]
233mod tests {
234    use super::*;
235    use std::path::PathBuf;
236    use tree_sitter::Parser;
237
238    fn cpp_tree(source: &str) -> tree_sitter::Tree {
239        let mut parser = Parser::new();
240        parser
241            .set_language(&tree_sitter_cpp::LANGUAGE.into())
242            .expect("C++ grammar");
243        parser.parse(source, None).expect("C++ tree")
244    }
245
246    /// Everything one reading publishes, rendered so that two readings can be
247    /// compared without depending on hash iteration order.
248    fn published_facts(parsed: &ParsedFile) -> Vec<String> {
249        let mut facts = vec![
250            format!("package={}", parsed.package_name),
251            format!("content_qualifier={}", parsed.content_qualifier),
252            format!("top_level={:?}", parsed.top_level_declarations),
253            format!("imports={:?}", parsed.imports),
254            format!("materializations={:?}", parsed.materialization_records),
255            format!("rust_usage_facts={:?}", parsed.rust_usage_facts),
256        ];
257        let mut unordered = |label: &str, mut entries: Vec<String>| {
258            entries.sort();
259            facts.push(format!("{label}={entries:?}"));
260        };
261        unordered(
262            "declarations",
263            parsed.declarations().iter().map(debug_of).collect(),
264        );
265        unordered(
266            "definition_lookup",
267            parsed
268                .definition_lookup_units
269                .iter()
270                .map(debug_of)
271                .collect(),
272        );
273        unordered(
274            "type_identifiers",
275            parsed.type_identifiers.iter().map(debug_of).collect(),
276        );
277        unordered(
278            "type_aliases",
279            parsed.type_aliases.iter().map(debug_of).collect(),
280        );
281        unordered(
282            "scala_traits",
283            parsed.scala_traits.iter().map(debug_of).collect(),
284        );
285        unordered(
286            "test_region_units",
287            parsed.test_region_units.iter().map(debug_of).collect(),
288        );
289        unordered(
290            "navigation_truncated",
291            parsed
292                .navigation_ranges_truncated
293                .iter()
294                .map(debug_of)
295                .collect(),
296        );
297        unordered("children", pairs(&parsed.children));
298        unordered("ranges", pairs(&parsed.ranges));
299        unordered("navigation_ranges", pairs(&parsed.navigation_ranges));
300        unordered("signatures", pairs(&parsed.signatures));
301        unordered("signature_metadata", pairs(&parsed.signature_metadata));
302        unordered("raw_supertypes", pairs(&parsed.raw_supertypes));
303        unordered(
304            "supertype_lookup_paths",
305            pairs(&parsed.supertype_lookup_paths),
306        );
307        unordered("scala_exports", pairs(&parsed.scala_exports));
308        unordered(
309            "cpp_template_metadata",
310            pairs(&parsed.cpp_template_metadata),
311        );
312        unordered(
313            "ruby_method_dispatch_modes",
314            pairs(&parsed.ruby_method_dispatch_modes),
315        );
316        facts
317    }
318
319    fn debug_of<T: std::fmt::Debug>(value: T) -> String {
320        format!("{value:?}")
321    }
322
323    fn pairs<K: std::fmt::Debug, V: std::fmt::Debug>(
324        map: &brokk_bifrost_core::hash::HashMap<K, V>,
325    ) -> Vec<String> {
326        map.iter().map(|entry| format!("{entry:?}")).collect()
327    }
328
329    /// The two readings of one header, taken the way production takes them,
330    /// publish exactly what two independent extractions publish.
331    ///
332    /// Milestone 3b of `.agents/plans/immutable-revision-persisted-fact-reuse.md`
333    /// stopped the C reading from rebuilding the parent index, re-sweeping
334    /// includes and identifiers, and re-running the quoted-include line scan
335    /// that the C++ reading of the same tree had already produced. That is a
336    /// deduplication and nothing else: if any published fact moved, something
337    /// believed dialect-insensitive is not.
338    ///
339    /// The error-recovery shapes are the interesting half. Their walks reparse
340    /// byte regions into trees of their own and re-own sibling nodes under
341    /// recovered class scopes, so they are where a shared index would show up
342    /// if sharing were unsound.
343    #[test]
344    fn a_shared_reading_publishes_what_an_independent_one_publishes() {
345        let fixtures: &[(&str, &str)] = &[
346            (
347                "nested tag inside an aggregate, plus a nested include",
348                r#"
349#include <vector>
350struct outer {
351#include "member_list.def"
352    struct inner { int v; } i;
353};
354struct inner *p;
355"#,
356            ),
357            (
358                "a quoted include only the line scan can recover",
359                r#"
360#include "visible.h"
361class Broken {
362    void method(
363#include "hidden.h"
364"#,
365            ),
366            (
367                "forward declarations replaced by their definitions",
368                r#"
369typedef unsigned long long u64;
370namespace generated {
371struct tag0;
372struct tag1;
373struct tag1 {
374    struct nested { int v; } n;
375    u64 first;
376};
377struct tag0 { int second; };
378}
379"#,
380            ),
381            (
382                "a fragmented export-macro class body",
383                r#"
384#define SIMPLECPP_LIB
385namespace simplecpp {
386using TokenString = std::string;
387struct Location { int line{}; };
388class SIMPLECPP_LIB Token {
389  TokenString prefix;
390  void prefix_method() {}
391 public:
392  Token(const TokenString &s, const Location &loc, bool wsahead = false) :
393      whitespaceahead(wsahead), location(loc), string(s)
394      {
395      flags();
396  }
397  struct Nested { int v; } nested;
398  TokenString string;
399  bool whitespaceahead;
400  Location location;
401 private:
402  void flags() {
403      whitespaceahead = true;
404  }
405};
406}
407"#,
408            ),
409        ];
410
411        for (name, source) in fixtures {
412            let file = ProjectFile::new(PathBuf::from("/workspace"), "src/widget.h");
413            let tree = cpp_tree(source);
414            let root = tree.root_node();
415
416            let independent_primary = parse_cpp_file(&file, source, &tree);
417            let independent_c =
418                parse_cpp_file_in_dialect(&file, source, &tree, LanguageDialect::CppC);
419
420            let ancestry = ParentIndex::new(root);
421            let shared_primary = parse_cpp_file_with_ancestry(&file, source, root, &ancestry);
422            let shared_c = parse_cpp_c_reading(&file, source, root, &ancestry, &shared_primary);
423
424            assert_eq!(
425                published_facts(&independent_primary),
426                published_facts(&shared_primary),
427                "C++ reading of {name}"
428            );
429            assert_eq!(
430                published_facts(&independent_c),
431                published_facts(&shared_c),
432                "C reading of {name}"
433            );
434        }
435    }
436
437    /// A tag nested in an aggregate is the whole reason the second reading
438    /// exists, so the fixture above must actually produce two different
439    /// readings; otherwise the comparison would pass on two empty answers.
440    #[test]
441    fn the_nested_tag_fixture_really_has_two_readings() {
442        let source = "struct outer { struct inner { int v; } i; };\nstruct inner *p;\n";
443        let file = ProjectFile::new(PathBuf::from("/workspace"), "src/widget.h");
444        let tree = cpp_tree(source);
445        let root = tree.root_node();
446        let ancestry = ParentIndex::new(root);
447        let primary = parse_cpp_file_with_ancestry(&file, source, root, &ancestry);
448        let c_reading = parse_cpp_c_reading(&file, source, root, &ancestry, &primary);
449        assert!(
450            cpp_projections_differ(&primary, &c_reading),
451            "the C reading should mint `inner` at file scope: {:#?} vs {:#?}",
452            primary.declarations(),
453            c_reading.declarations()
454        );
455    }
456
457    /// Every `#include` is an include claim, wherever it is written. The
458    /// declaration walk descends only through declaration scopes, so a
459    /// directive inside a function body (llama.cpp's `sycl/info/aspects.def`
460    /// inside a `switch`) or inside a class body (Eigen's
461    /// `EIGEN_DENSEBASE_PLUGIN` in `DenseBase.h`) used to be invisible.
462    ///
463    /// The nested directive here is not a quoted include, so the established
464    /// `recover_quoted_includes` line scan cannot supply it: only the preorder
465    /// sweep over the tree can.
466    #[test]
467    fn includes_are_recorded_at_every_depth() {
468        let source = r#"
469#include <vector>
470
471class Widget {
472public:
473    int value() const;
474};
475
476int run() {
477    switch (0) {
478#include <sycl/info/aspects.def>
479    default:
480        return 0;
481    }
482}
483"#;
484        let file = ProjectFile::new(PathBuf::from("/workspace"), "src/widget.cpp");
485        let mut parser = Parser::new();
486        parser
487            .set_language(&tree_sitter_cpp::LANGUAGE.into())
488            .expect("C++ grammar");
489        let tree = parser.parse(source, None).expect("C++ tree");
490
491        let parsed = parse_cpp_file(&file, source, &tree);
492        let includes = parsed
493            .imports
494            .iter()
495            .map(|import| import.raw_snippet.clone())
496            .collect::<Vec<_>>();
497
498        assert_eq!(
499            includes,
500            vec![
501                "#include <vector>".to_string(),
502                "#include <sycl/info/aspects.def>".to_string(),
503            ]
504        );
505        assert!(!parsed.declarations().is_empty());
506    }
507}