brokk-bifrost-cpp 0.11.0

C++ language knowledge for brokk-bifrost: declarations and macro-sentinel recovery, include-graph visibility, out-of-line member identity reconciliation, and usage-graph resolution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! Structured declaration extraction for external C++ headers.
//!
//! This module is the language-only scanner shared by dependency-pack
//! production and external-boundary evidence. It emits neutral records so this
//! crate stays below `brokk-bifrost-analysis` in the dependency graph.

use crate::adapter::parse_cpp_file;
use crate::declarations::{
    CppComparableSlot, CppParameterType, cpp_callable_parameter_type_identities,
    cpp_callable_return_type_identity, cpp_comparable_parameter_shapes, cpp_function_declarator_at,
    node_text,
};
use crate::graph::resolver::cpp_name_for;
use brokk_bifrost_core::analyzer::ProjectFile;
use brokk_bifrost_core::analyzer::model::{
    CallableArity, CodeUnit, CodeUnitType, CppTemplateMetadata, SignatureMetadata,
    StructuredTypeIdentity,
};
use brokk_bifrost_core::analyzer::tree_walk::{ParentIndex, collect_parse_errors};
use brokk_bifrost_core::hash::HashMap;
use std::path::{Path, PathBuf};
use tree_sitter::{Node, Parser};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CppExternalDeclarationLimits {
    pub max_records: usize,
}

impl Default for CppExternalDeclarationLimits {
    fn default() -> Self {
        Self {
            max_records: 250_000,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CppExternalDeclarationCompleteness {
    Complete,
    Partial,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CppExternalMemberKind {
    Function,
    Field,
    Macro,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CppExternalVisibility {
    Public,
    Protected,
    Private,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CppExternalType {
    pub name: String,
    pub source_name: String,
    pub is_type_alias: bool,
    pub underlying_type: Option<StructuredTypeIdentity>,
    /// Structured template declaration metadata, when the class declaration
    /// was reached through a template declaration.  Keeping this alongside
    /// the neutral record lets a consumer prove a primary template's exact
    /// arity/defaults without re-reading source text.
    pub template_metadata: Option<CppTemplateMetadata>,
    pub visibility: CppExternalVisibility,
    pub source_path: PathBuf,
    pub direct_bases: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CppExternalMember {
    pub owner: Option<String>,
    pub name: String,
    pub qualified_name: String,
    pub kind: CppExternalMemberKind,
    pub visibility: CppExternalVisibility,
    pub is_constructor: bool,
    pub signature: Option<String>,
    /// The structured type of each invocation parameter, or `None` when the
    /// declaration is not a callable or its parameter list was not reached.
    ///
    /// These are structured identities rather than rendered spellings because
    /// the consumer publishes them into a type model, and a spelling such as
    /// `const T&` is a source text, not a type name.
    pub parameter_types: Option<Vec<CppParameterType>>,
    /// Parser-derived parameter shapes retaining C++ cv-qualification.
    pub parameter_shapes: Option<Vec<CppComparableSlot>>,
    /// Invocation arity, including default and repeated parameters, when the
    /// declaration is callable and its parameter list was reached.
    pub callable_arity: Option<CallableArity>,
    /// Whether a constructor is available to implicit conversion.
    pub explicitness: Option<CppCallableExplicitness>,
    pub return_type: Option<StructuredTypeIdentity>,
    pub source_path: PathBuf,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CppCallableExplicitness {
    Implicit,
    Explicit,
    Conditional,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CppExternalDeclarationDiagnostic {
    pub code: &'static str,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CppExternalDeclarationSet {
    pub types: Vec<CppExternalType>,
    pub members: Vec<CppExternalMember>,
    pub completeness: CppExternalDeclarationCompleteness,
    pub diagnostics: Vec<CppExternalDeclarationDiagnostic>,
}

/// Return the literal paths from angle-bracket includes in one C++ source.
///
/// Quoted and computed includes are not external-root evidence. Conditional
/// includes are retained because compile-context resolution still proves the
/// root, while pack completeness records preprocessor uncertainty separately.
pub fn external_angle_include_paths(source: &str) -> Vec<PathBuf> {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_cpp::LANGUAGE.into())
        .expect("the linked tree-sitter-cpp grammar matches this tree-sitter version");
    let tree = parser.parse(source, None).expect("uncancelled C++ parse");
    external_angle_include_paths_from_root(source, tree.root_node())
}

/// Return literal unconditional angle includes from an existing C++ syntax tree.
///
/// The source and root must describe the same immutable snapshot. Analyzer
/// callers use this form to reuse prepared workspace syntax; standalone
/// external headers use [`external_angle_include_paths`].
pub fn external_angle_include_paths_from_root(source: &str, root: Node<'_>) -> Vec<PathBuf> {
    let mut paths = Vec::new();
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        if node.kind() == "preproc_include"
            && !has_conditional_preprocessor_ancestor(node)
            && let Some(path) = node.child_by_field_name("path")
            && path.kind() == "system_lib_string"
            && let Some(path) = node_text(path, source)
                .strip_prefix('<')
                .and_then(|path| path.strip_suffix('>'))
                .filter(|path| !path.is_empty())
        {
            paths.push(PathBuf::from(path));
        }
        let mut cursor = node.walk();
        stack.extend(node.named_children(&mut cursor));
    }
    paths.sort();
    paths.dedup();
    paths
}

fn has_conditional_preprocessor_ancestor(mut node: Node<'_>) -> bool {
    while let Some(parent) = node.parent() {
        if matches!(
            parent.kind(),
            "preproc_if" | "preproc_ifdef" | "preproc_ifndef" | "preproc_elif" | "preproc_else"
        ) {
            return true;
        }
        node = parent;
    }
    false
}

/// Extract declarations from one exact header source entry.
///
/// `source_path` is relative to `source_set_root`. The caller owns source-set
/// containment, byte limits, cancellation, and stable hashing. This function
/// owns only C++ syntax interpretation and its record limit.
pub fn extract_external_declarations(
    source_set_root: &Path,
    source_path: &Path,
    source: &str,
    limits: CppExternalDeclarationLimits,
) -> CppExternalDeclarationSet {
    let file = ProjectFile::new(source_set_root.to_path_buf(), source_path.to_path_buf());
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_cpp::LANGUAGE.into())
        .expect("the linked tree-sitter-cpp grammar matches this tree-sitter version");
    let tree = parser.parse(source, None).expect("uncancelled C++ parse");

    let mut diagnostics = Vec::new();
    let mut completeness = CppExternalDeclarationCompleteness::Complete;
    let mut parse_errors = Vec::new();
    collect_parse_errors(tree.root_node(), &mut parse_errors);
    if !parse_errors.is_empty() {
        completeness = CppExternalDeclarationCompleteness::Partial;
        diagnostics.push(CppExternalDeclarationDiagnostic {
            code: "cpp.external.parse_error",
            message: format!(
                "external header `{}` has parse errors",
                source_path.display()
            ),
        });
    }
    if has_unsupported_preprocessing(tree.root_node()) {
        completeness = CppExternalDeclarationCompleteness::Partial;
        diagnostics.push(CppExternalDeclarationDiagnostic {
            code: "cpp.external.preprocessor_partial",
            message: format!(
                "external header `{}` has conditional or generated declarations",
                source_path.display()
            ),
        });
    }

    let parsed = parse_cpp_file(&file, source, &tree);
    let mut parent_by_child = HashMap::default();
    for (parent, children) in &parsed.children {
        for child in children {
            parent_by_child.insert(child.clone(), parent.clone());
        }
    }

    let mut declarations = parsed.declarations().iter().cloned().collect::<Vec<_>>();
    declarations.sort_by_key(|declaration| {
        (
            declaration.fq_name(),
            declaration.kind(),
            declaration.signature().map(str::to_owned),
        )
    });

    // One index for the file's tree rather than one ancestor walk per
    // declaration: a large generated header has tens of thousands of them.
    let ancestry = ParentIndex::new(tree.root_node());
    let mut types = Vec::new();
    let mut members = Vec::new();
    for declaration in declarations {
        if types.len().saturating_add(members.len()) >= limits.max_records {
            completeness = CppExternalDeclarationCompleteness::Partial;
            diagnostics.push(CppExternalDeclarationDiagnostic {
                code: "cpp.external.record_limit",
                message: format!(
                    "external header `{}` exceeded the declaration record limit",
                    source_path.display()
                ),
            });
            break;
        }
        match declaration.kind() {
            CodeUnitType::Class => types.push(CppExternalType {
                name: declaration.fq_name(),
                source_name: cpp_name_for(&declaration),
                is_type_alias: parsed.type_aliases.contains(&declaration),
                underlying_type: parsed
                    .signature_metadata
                    .get(&declaration)
                    .and_then(|records| records.first())
                    .and_then(|metadata| metadata.underlying_type_identity())
                    .cloned(),
                template_metadata: parsed.cpp_template_metadata.get(&declaration).cloned(),
                visibility: parsed
                    .ranges
                    .get(&declaration)
                    .and_then(|ranges| ranges.iter().map(|range| range.start_byte).min())
                    .map(|start| cpp_member_visibility(tree.root_node(), source, start))
                    .unwrap_or(CppExternalVisibility::Private),
                source_path: source_path.to_path_buf(),
                direct_bases: parsed
                    .raw_supertypes
                    .get(&declaration)
                    .cloned()
                    .unwrap_or_default(),
            }),
            CodeUnitType::Function | CodeUnitType::Field | CodeUnitType::Macro => {
                let metadata = parsed
                    .signature_metadata
                    .get(&declaration)
                    .and_then(|records| records.first());
                let declaration_start = parsed
                    .ranges
                    .get(&declaration)
                    .and_then(|ranges| ranges.iter().map(|range| range.start_byte).min());
                let function_declarator = (declaration.kind() == CodeUnitType::Function)
                    .then(|| {
                        declaration_start
                            .and_then(|start| cpp_function_declarator_at(tree.root_node(), start))
                    })
                    .flatten();
                let parameter_types = function_declarator.map(|declarator| {
                    cpp_callable_parameter_type_identities(declarator, source, &ancestry)
                });
                let parameter_shapes = function_declarator.map(|declarator| {
                    cpp_comparable_parameter_shapes(declarator, source, &ancestry)
                });
                let return_type = metadata
                    .and_then(|metadata| metadata.return_type_identity())
                    .cloned()
                    .or_else(|| {
                        function_declarator.and_then(|declarator| {
                            cpp_callable_return_type_identity(declarator, source, &ancestry)
                        })
                    });
                members.push(CppExternalMember {
                    owner: nearest_type_owner(&declaration, &parent_by_child),
                    name: declaration.terminal_name().to_owned(),
                    qualified_name: cpp_name_for(&declaration),
                    kind: match declaration.kind() {
                        CodeUnitType::Function => CppExternalMemberKind::Function,
                        CodeUnitType::Field => CppExternalMemberKind::Field,
                        CodeUnitType::Macro => CppExternalMemberKind::Macro,
                        _ => unreachable!("the outer match admits exactly member kinds"),
                    },
                    visibility: declaration_start
                        .map(|start| cpp_member_visibility(tree.root_node(), source, start))
                        .unwrap_or(CppExternalVisibility::Private),
                    is_constructor: metadata
                        .is_some_and(|metadata| metadata.callable_is_constructor()),
                    signature: declaration.signature().map(str::to_owned),
                    parameter_types,
                    parameter_shapes,
                    callable_arity: metadata.and_then(SignatureMetadata::callable_arity),
                    explicitness: function_declarator.and_then(cpp_callable_explicitness),
                    return_type,
                    source_path: source_path.to_path_buf(),
                });
            }
            CodeUnitType::Module | CodeUnitType::FileScope => {}
        }
    }

    CppExternalDeclarationSet {
        types,
        members,
        completeness,
        diagnostics,
    }
}

fn cpp_member_visibility(root: Node<'_>, source: &str, start_byte: usize) -> CppExternalVisibility {
    let mut current = root.descendant_for_byte_range(start_byte, start_byte);
    while let Some(node) = current {
        let Some(parent) = node.parent() else {
            break;
        };
        if parent.kind() == "field_declaration_list" {
            let default = match parent.parent().map(|owner| owner.kind()) {
                Some("struct_specifier" | "union_specifier") => CppExternalVisibility::Public,
                _ => CppExternalVisibility::Private,
            };
            let mut visibility = default;
            let mut cursor = parent.walk();
            for child in parent.named_children(&mut cursor) {
                if child.start_byte() > start_byte {
                    break;
                }
                if child.kind() == "access_specifier" {
                    visibility = match node_text(child, source).trim_end_matches(':').trim() {
                        "public" => CppExternalVisibility::Public,
                        "protected" => CppExternalVisibility::Protected,
                        "private" => CppExternalVisibility::Private,
                        _ => CppExternalVisibility::Private,
                    };
                }
            }
            return visibility;
        }
        current = Some(parent);
    }
    CppExternalVisibility::Public
}

fn cpp_callable_explicitness(mut declarator: Node<'_>) -> Option<CppCallableExplicitness> {
    while !matches!(
        declarator.kind(),
        "declaration" | "field_declaration" | "function_definition"
    ) {
        declarator = declarator.parent()?;
    }
    if declarator.has_error() {
        return None;
    }
    let mut stack = vec![declarator];
    let mut explicit = None;
    while let Some(node) = stack.pop() {
        if node.kind() == "explicit_function_specifier" {
            if explicit.is_some() {
                return None;
            }
            explicit = Some(if node.named_child_count() == 0 {
                CppCallableExplicitness::Explicit
            } else {
                CppCallableExplicitness::Conditional
            });
            continue;
        }
        let mut cursor = node.walk();
        stack.extend(node.named_children(&mut cursor));
    }
    Some(explicit.unwrap_or(CppCallableExplicitness::Implicit))
}

fn nearest_type_owner(
    declaration: &CodeUnit,
    parent_by_child: &HashMap<CodeUnit, CodeUnit>,
) -> Option<String> {
    let mut current = declaration;
    while let Some(parent) = parent_by_child.get(current) {
        if parent.kind() == CodeUnitType::Class {
            return Some(parent.fq_name());
        }
        current = parent;
    }
    None
}

fn has_unsupported_preprocessing(root: Node<'_>) -> bool {
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        if matches!(
            node.kind(),
            "preproc_def"
                | "preproc_function_def"
                | "preproc_if"
                | "preproc_ifdef"
                | "preproc_ifndef"
                | "preproc_elif"
                | "preproc_else"
        ) {
            return true;
        }
        let mut cursor = node.walk();
        stack.extend(node.named_children(&mut cursor));
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use brokk_bifrost_core::analyzer::model::CallableArity;
    use brokk_bifrost_core::analyzer::model::StructuredTypeNodeView;

    fn extract(source: &str) -> CppExternalDeclarationSet {
        let temp = tempfile::tempdir().expect("temp root");
        extract_external_declarations(
            temp.path(),
            Path::new("vector"),
            source,
            CppExternalDeclarationLimits::default(),
        )
    }

    #[test]
    fn extracts_only_literal_angle_include_paths() {
        let source = "#include <vector>\n#include \"local.hpp\"\n#include HEADER\n#if FEATURE\n#include <conditional.hpp>\n#endif\n";
        assert_eq!(
            vec![PathBuf::from("vector")],
            external_angle_include_paths(source)
        );
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_cpp::LANGUAGE.into())
            .expect("C++ grammar");
        let tree = parser.parse(source, None).expect("tree");
        assert_eq!(
            vec![PathBuf::from("vector")],
            external_angle_include_paths_from_root(source, tree.root_node())
        );
    }

    #[test]
    fn extracts_namespaced_template_type_and_owned_members() {
        let declarations = extract(
            r#"
            namespace std {
            template <typename T> class vector : public sequence<T> {
            public:
                vector();
                void push_back(const T& value);
                T size;
            };
            }
            "#,
        );

        assert_eq!(
            CppExternalDeclarationCompleteness::Complete,
            declarations.completeness
        );
        assert!(
            declarations.types.iter().any(|record| {
                record.name == "std.vector" && record.direct_bases == ["sequence<T>"]
            }),
            "{declarations:#?}"
        );
        assert!(
            declarations.members.iter().any(|record| {
                record.owner.as_deref() == Some("std.vector")
                    && record.name == "push_back"
                    && record.visibility == CppExternalVisibility::Public
            }),
            "{declarations:#?}"
        );
        assert!(
            declarations.members.iter().any(|record| {
                record.owner.as_deref() == Some("std.vector") && record.name == "size"
            }),
            "{declarations:#?}"
        );
    }

    #[test]
    fn preserves_copy_and_move_reference_parameter_shapes() {
        let source = r#"
            class Widget {
            public:
                Widget(const Widget&);
                Widget(Widget&&);
            };
        "#;
        let declarations = extract(source);
        let constructors = declarations
            .members
            .iter()
            .filter(|member| member.owner.as_deref() == Some("Widget") && member.name == "Widget")
            .collect::<Vec<_>>();

        assert_eq!(constructors.len(), 2, "{declarations:#?}");
        let shapes = constructors
            .iter()
            .map(|constructor| {
                let parameters = constructor
                    .parameter_types
                    .as_ref()
                    .expect("constructor parameters");
                assert_eq!(parameters.len(), 1, "{constructor:#?}");
                let CppParameterType::Structured(identity) = &parameters[0] else {
                    panic!("constructor parameter shape: {constructor:#?}");
                };
                (
                    identity.clone(),
                    matches!(
                        identity.view(identity.root_id()),
                        Some(StructuredTypeNodeView::Reference(_))
                    ),
                    matches!(
                        identity.view(identity.root_id()),
                        Some(StructuredTypeNodeView::RvalueReference(_))
                    ),
                )
            })
            .collect::<Vec<_>>();

        assert_ne!(shapes[0].0, shapes[1].0);
        assert!(shapes.iter().any(|(_, is_lvalue, _)| *is_lvalue));
        assert!(shapes.iter().any(|(_, _, is_rvalue)| *is_rvalue));
        assert_eq!(declarations, extract(source));
    }

    #[test]
    fn preserves_callable_arities_for_defaulted_and_required_parameters() {
        let declarations = extract(
            r#"
            namespace std {
            template <class C, class Alloc> class basic_string {
            public:
                basic_string(const C*, const Alloc& = Alloc());
                basic_string(const C&, const Alloc&);
            };
            }
            "#,
        );
        let constructors = declarations
            .members
            .iter()
            .filter(|member| {
                member.owner.as_deref() == Some("std.basic_string") && member.name == "basic_string"
            })
            .collect::<Vec<_>>();

        assert_eq!(constructors.len(), 2, "{declarations:#?}");
        assert!(
            constructors
                .iter()
                .any(|member| { member.callable_arity == Some(CallableArity::new(1, 2, false)) }),
            "defaulted constructor arity must be required=1,total=2: {constructors:#?}"
        );
        assert!(
            constructors
                .iter()
                .any(|member| { member.callable_arity == Some(CallableArity::new(2, 2, false)) }),
            "non-defaulted constructor arity must be required=2,total=2: {constructors:#?}"
        );
    }

    #[test]
    fn preserves_callable_explicitness_for_implicit_binding() {
        for (declaration, expected) in [
            ("Widget(const char*);", CppCallableExplicitness::Implicit),
            (
                "explicit Widget(const char*);",
                CppCallableExplicitness::Explicit,
            ),
            (
                "explicit(true) Widget(const char*);",
                CppCallableExplicitness::Conditional,
            ),
        ] {
            let declarations = extract(&format!("class Widget {{ public: {declaration} }};"));
            let member = declarations
                .members
                .first()
                .unwrap_or_else(|| panic!("constructor declaration: {declarations:#?}"));
            assert_eq!(Some(expected), member.explicitness, "{member:#?}");
        }
    }

    #[test]
    fn preserves_callable_return_reference_shapes() {
        let declarations = extract(
            r#"
            namespace std {
            class basic_string {
            public:
                basic_string& operator=(const basic_string&);
                basic_string&& operator=(basic_string&&);
                void operator=(int);
            };
            }
            "#,
        );
        let assignments = declarations
            .members
            .iter()
            .filter(|member| member.name == "operator=")
            .collect::<Vec<_>>();
        assert_eq!(assignments.len(), 3, "{declarations:#?}");

        let copy = assignments
            .iter()
            .find(|member| member.signature.as_deref() == Some("(const basic_string &)"))
            .unwrap_or_else(|| panic!("copy assignment: {declarations:#?}"));
        let Some(StructuredTypeNodeView::Reference(inner)) = copy
            .return_type
            .as_ref()
            .and_then(|identity| identity.view(identity.root_id()))
        else {
            panic!("copy return type: {copy:#?}");
        };
        let Some(StructuredTypeNodeView::Named(name)) = copy
            .return_type
            .as_ref()
            .and_then(|identity| identity.view(inner))
        else {
            panic!("copy return target: {copy:#?}");
        };
        assert_eq!(["basic_string"], name.path());
        assert_eq!(["std", "basic_string"], name.lexical_scope());

        let move_assignment = declarations
            .members
            .iter()
            .find(|member| member.signature.as_deref() == Some("(basic_string &&)"))
            .unwrap_or_else(|| panic!("move assignment: {declarations:#?}"));
        assert!(matches!(
            move_assignment
                .return_type
                .as_ref()
                .and_then(|identity| identity.view(identity.root_id())),
            Some(StructuredTypeNodeView::RvalueReference(_))
        ));

        let void_assignment = declarations
            .members
            .iter()
            .find(|member| member.signature.as_deref() == Some("(int)"))
            .unwrap_or_else(|| panic!("void assignment: {declarations:#?}"));
        let Some(StructuredTypeNodeView::Named(name)) = void_assignment
            .return_type
            .as_ref()
            .and_then(|identity| identity.view(identity.root_id()))
        else {
            panic!("void return type: {void_assignment:#?}");
        };
        assert_eq!(["void"], name.path());
    }

    #[test]
    fn preserves_type_alias_underlying_structured_identity() {
        let declarations = extract(
            r#"
            namespace std {
            template<class C, class Traits, class Alloc> class basic_string;
            using string = basic_string<char, char_traits<char>, allocator<char>>;
            }
            "#,
        );
        let alias = declarations
            .types
            .iter()
            .find(|record| record.name == "std.string")
            .unwrap_or_else(|| panic!("string alias: {declarations:#?}"));
        assert!(alias.is_type_alias, "{alias:#?}");
        let identity = alias
            .underlying_type
            .as_ref()
            .unwrap_or_else(|| panic!("structured alias target: {alias:#?}"));
        let Some(StructuredTypeNodeView::Generic { base, arguments }) =
            identity.view(identity.root_id())
        else {
            panic!("generic basic_string alias target: {identity:#?}");
        };
        assert_eq!(3, arguments.len());
        let Some(StructuredTypeNodeView::Named(name)) = identity.view(base) else {
            panic!("named basic_string alias base: {identity:#?}");
        };
        assert_eq!(["basic_string"], name.path());
        assert_eq!(["std"], name.lexical_scope());
    }

    #[test]
    fn keeps_same_short_names_under_distinct_owners() {
        let declarations = extract(
            "namespace first { class box { void add(int); }; }\nnamespace second { class box { void add(int); }; }",
        );
        let mut owners = declarations
            .members
            .iter()
            .filter(|member| member.name == "add")
            .filter_map(|member| member.owner.clone())
            .collect::<Vec<_>>();
        owners.sort();

        assert_eq!(vec!["first.box", "second.box"], owners);
        assert!(
            declarations
                .members
                .iter()
                .filter(|member| member.name == "add")
                .all(|member| member.visibility == CppExternalVisibility::Private)
        );
    }

    #[test]
    fn nested_type_visibility_follows_the_enclosing_access_section() {
        let declarations = extract(
            "class Outer { class Hidden {}; public: struct Visible {}; protected: class Guarded {}; };",
        );
        assert!(declarations.types.iter().any(|record| {
            record.name == "Outer$Hidden" && record.visibility == CppExternalVisibility::Private
        }));
        assert!(declarations.types.iter().any(|record| {
            record.name == "Outer$Visible" && record.visibility == CppExternalVisibility::Public
        }));
        assert!(declarations.types.iter().any(|record| {
            record.name == "Outer$Guarded" && record.visibility == CppExternalVisibility::Protected
        }));
    }

    #[test]
    fn preprocessor_and_record_limits_make_the_surface_partial() {
        let temp = tempfile::tempdir().expect("temp root");
        let declarations = extract_external_declarations(
            temp.path(),
            Path::new("limited.hpp"),
            "#ifdef FEATURE\nclass Conditional {};\n#endif\nclass Always {};",
            CppExternalDeclarationLimits { max_records: 1 },
        );

        assert_eq!(
            CppExternalDeclarationCompleteness::Partial,
            declarations.completeness
        );
        assert!(
            declarations
                .diagnostics
                .iter()
                .any(|diagnostic| diagnostic.code == "cpp.external.preprocessor_partial")
        );
        assert!(
            declarations
                .diagnostics
                .iter()
                .any(|diagnostic| diagnostic.code == "cpp.external.record_limit")
        );
    }
}