cpp_code_analysis 0.4.0

The analysis layer of cpp_ls: the preprocessor, symbol scopes, type inference and the semantic checks.
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
//! **Gate 0**: does the fold answer what the walk answers?
//!
//! `cache::env::compose` composes a file's environment from directive skeletons instead of walking the closure. Before
//! anything production uses it, the two implementations have to be shown to agree — not on an `EnvId` (they are
//! different representations) but on the **answers a consumer reads**: which names are macros here, what their
//! replacement lists are, and which of them the file's own subtree is responsible for.
//!
//! # What is compared, and why it is not everything
//!
//! The walk's [`crate::TranslationUnit::environment_of`] answers "what this file sees **from elsewhere**": the
//! inherited prefix (everything emitted before its frame) *plus the facts of the files it includes*, minus the file's
//! own `#define`s. The fold's `at_entry` is only the inherited prefix. So the comparison excludes the names the file's
//! own subtree defines — those are exactly the difference the two questions intend — by taking the names whose binding
//! differs between `at_entry` and `at_exit` as that file's own.
//!
//! Two more exclusions, each counted rather than silent:
//!
//! * **seed names** — the compiler's predefines and `-D`, which the walk's view does not enumerate either;
//! * **`unknown_guards`** — a directive whose region the fold could not decide is not applied (the conservative arm of
//!   `compose`), so a name can be missing from the fold on purpose. The count is reported beside the mismatches; a
//!   corpus is expected to be non-zero here until `Definition` answers values (`docs/module-cache.md` §8).
//!
//! # The two runs
//!
//! [`the_fold_and_the_walk_agree_on_a_fixture`] is the repeatable gate: a project small enough to reason about, run on
//! every test invocation, and asserted to have **no** mismatches. [`the_fold_and_the_walk_agree_on_a_real_project`] is
//! the probe: `EmmyLuaCodeStyle`, roots named by `CPPLS_PROBE_DIRS`, and the first `CPPLS_PROBE_ROOTS` `.cpp` files
//! walked.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::cache::env::{ComposedFile, Env, Skeleton, UnitComposition, compose_unit};
use crate::file::paths::{FileProvider, MemoryFiles};
use crate::include::config::CompilerConfig;
use crate::index::watch::WatchFilter;
use crate::session::{OpenDocuments, Session, SessionFiles};

/// What one comparison found, by category.
#[derive(Clone, Default, Debug)]
struct Tally {
    files: usize,
    names: usize,
    /// Names the walk's view has and the fold's entry state does not.
    only_walked: usize,
    /// Names the fold has and the walk's view does not.
    only_composed: usize,
    /// The name is in both and the replacement lists differ.
    body_differs: usize,
    /// The name is in both and one says function-like, the other object-like.
    kind_differs: usize,
    /// Files the walk reached that the fold's traversal did not.
    missing_files: usize,
    /// Names excluded because the file's own subtree defines them.
    skipped_own: usize,
    /// Names excluded because they are the compilation's, not a file's.
    skipped_seed: usize,
    /// The first few names that were only walk --- what a definer search is run on.
    missing_names: Vec<String>,
    examples: Vec<String>,
}

impl Tally {
    fn merge(&mut self, other: Tally) {
        self.files += other.files;
        self.names += other.names;
        self.only_walked += other.only_walked;
        self.only_composed += other.only_composed;
        self.body_differs += other.body_differs;
        self.kind_differs += other.kind_differs;
        self.missing_files += other.missing_files;
        self.skipped_own += other.skipped_own;
        self.skipped_seed += other.skipped_seed;
        for example in other.examples {
            if self.examples.len() < 20 {
                self.examples.push(example);
            }
        }
        for name in other.missing_names {
            if self.missing_names.len() < 8 {
                self.missing_names.push(name);
            }
        }
    }

    fn mismatches(&self) -> usize {
        self.only_walked
            + self.only_composed
            + self.body_differs
            + self.kind_differs
            + self.missing_files
    }
}

/// Compare one file: the fold's entry state against the walk's view.
fn compare_file(
    file: &ComposedFile,
    walked: &[(String, Option<bool>, String)],
    seed_names: &HashSet<String>,
) -> Tally {
    let mut tally = Tally {
        files: 1,
        ..Tally::default()
    };
    // **What this file's own subtree defines** — the names whose binding the file's directives and includes changed.
    let mut own: HashSet<String> = HashSet::new();
    file.at_exit.for_each_in_force(|definition| {
        let entry = file.at_entry.get(definition.name());
        let unchanged = entry.is_some_and(|entry| {
            entry.body() == definition.body()
                && entry.is_function_like() == definition.is_function_like()
        });
        if !unchanged {
            own.insert(definition.name().to_string());
        }
    });

    // **A name the file's own subtree *removed* is its own effect too.** The walk's view excludes a file's own facts,
    // so a name defined by an include and undefined by the file itself is only walk unless it is classed as own: the
    // loop above sees what the subtree added, and this one sees what it took away.
    file.at_entry.for_each_in_force(|definition| {
        if file.at_exit.get(definition.name()).is_none() {
            own.insert(definition.name().to_string());
        }
    });

    let mut walked_map: HashMap<&str, (Option<bool>, &str)> = HashMap::with_capacity(walked.len());
    for (name, function_like, body) in walked {
        walked_map.insert(name.as_str(), (*function_like, body.trim()));
    }

    for (name, (function_like, body)) in &walked_map {
        if own.contains(*name) {
            tally.skipped_own += 1;
            continue;
        }
        if seed_names.contains(*name) {
            tally.skipped_seed += 1;
            continue;
        }
        tally.names += 1;
        match file.at_entry.get(name) {
            None => {
                tally.only_walked += 1;
                if tally.missing_names.len() < 8 {
                    tally.missing_names.push((*name).to_string());
                }
                tally.examples.push(format!(
                    "{}: `{name}` is in the walk only",
                    file.path.display()
                ));
            }
            Some(definition) => {
                if definition.body().unwrap_or("") != *body {
                    tally.body_differs += 1;
                    tally.examples.push(format!(
                        "{}: `{name}` body {:?} vs {:?}",
                        file.path.display(),
                        definition.body().unwrap_or(""),
                        body
                    ));
                }
                if function_like.is_some_and(|kind| kind != definition.is_function_like()) {
                    tally.kind_differs += 1;
                    tally.examples.push(format!(
                        "{}: `{name}` function-like {} vs {}",
                        file.path.display(),
                        definition.is_function_like(),
                        function_like.unwrap_or_default()
                    ));
                }
            }
        }
    }

    let mut composed_only: Vec<String> = Vec::new();
    file.at_entry.for_each_in_force(|definition| {
        let name = definition.name();
        if walked_map.contains_key(name) || own.contains(name) || seed_names.contains(name) {
            return;
        }
        composed_only.push(name.to_string());
    });
    for name in composed_only {
        tally.only_composed += 1;
        tally.examples.push(format!(
            "{}: `{name}` is in the fold only",
            file.path.display()
        ));
    }

    tally
}

/// The fold's unit for `root`, with every file's entry and exit state.
fn composition<F: FileProvider + Clone>(
    session: &Session<F>,
    root: &Path,
    seed: &dyn crate::MacroValues,
    known: &[PathBuf],
) -> UnitComposition {
    // **A resolver for the includes the summary could not resolve**, from the paths the walk reached: a spelling
    // with no cached path is matched against the last two segments of a known file. This is a probe's stand-in for
    // the session's resolver, and it exists to test the diagnosis in docs/module-cache.md §6a: the fold reaches
    // 251 of the walk's 1 053 files because 778 includes did not resolve when the summaries were built.
    let mut by_suffix: HashMap<String, PathBuf> = HashMap::new();
    for path in known {
        let normal = path.to_string_lossy().to_lowercase().replace('\\', "/");
        let mut segments = normal.rsplit('/');
        if let (Some(file), Some(directory)) = (segments.next(), segments.next()) {
            by_suffix
                .entry(format!("{directory}/{file}"))
                .or_insert_with(|| path.clone());
        }
    }
    let asked = std::cell::Cell::new(0usize);
    let cached = std::cell::Cell::new(0usize);
    let rescued = std::cell::Cell::new(0usize);
    let missed: std::cell::RefCell<Vec<String>> = std::cell::RefCell::new(Vec::new());
    let missing_summary = std::cell::Cell::new(0usize);
    let missing_text = std::cell::Cell::new(0usize);
    let missing_examples: std::cell::RefCell<Vec<String>> = std::cell::RefCell::new(Vec::new());
    let composed = compose_unit(root, &Env::empty(), seed, &mut |path| {
        let Some(summary) = session.index().summary(path) else {
            missing_summary.set(missing_summary.get() + 1);
            if missing_examples.borrow().len() < 6 {
                missing_examples
                    .borrow_mut()
                    .push(format!("no summary: {}", path.display()));
            }
            return None;
        };
        let Some(text) = session.text(path) else {
            missing_text.set(missing_text.get() + 1);
            if missing_examples.borrow().len() < 6 {
                missing_examples
                    .borrow_mut()
                    .push(format!("no text: {}", path.display()));
            }
            return None;
        };
        let skeleton = Skeleton::resolved_by(summary, &text, &mut |fact| {
            asked.set(asked.get() + 1);
            if let Some(path) = fact.resolved.clone() {
                cached.set(cached.get() + 1);
                return Some(path);
            }
            let spelling = fact.spelling.to_lowercase().replace('\\', "/");
            let found = by_suffix.get(&spelling).cloned().or_else(|| {
                let mut segments = spelling.rsplit('/');
                match (segments.next(), segments.next()) {
                    (Some(file), Some(directory)) => {
                        by_suffix.get(&format!("{directory}/{file}")).cloned()
                    }
                    _ => None,
                }
            });
            if found.is_some() {
                rescued.set(rescued.get() + 1);
            } else if missed.borrow().len() < 8 {
                missed.borrow_mut().push(fact.spelling.clone());
            }
            found
        });
        Some((skeleton, summary.guards.clone()))
    });
    eprintln!(
        "  lookup: {} missing summary, {} missing text; e.g. {:?}",
        missing_summary.get(),
        missing_text.get(),
        missing_examples.borrow()
    );
    eprintln!(
        "  resolver: {} include(s) asked, {} cached, {} rescued by suffix, {} still unresolved; missed e.g. {:?}",
        asked.get(),
        cached.get(),
        rescued.get(),
        composed.unresolved_includes,
        missed.borrow()
    );
    composed
}

/// The seed names of a session — the compiler predefines and the command line's `-D`s.
fn seed_names_of<F: FileProvider + Clone>(session: &Session<F>) -> HashSet<String> {
    session
        .index()
        .macros()
        .defined_names()
        .iter()
        .map(|name| name.to_string())
        .collect()
}

fn session_with(files: &[(&str, &str)]) -> Session<MemoryFiles> {
    let mut memory = MemoryFiles::new();
    for (path, text) in files {
        memory = memory.with_file(*path, *text);
    }
    let providers = SessionFiles::new(OpenDocuments::new(), memory);
    let mut session = Session::with_config(
        "/p",
        providers,
        WatchFilter::new("/p"),
        CompilerConfig::default(),
    );
    session.add_project_files(
        files
            .iter()
            .map(|(path, _)| PathBuf::from(*path))
            .collect::<Vec<_>>(),
    );
    session.index_everything();
    session
}
/// **The repeatable gate.** Nested includes, an include guard, a `#define` before an include, an `#undef`, and a
/// condition decided by a macro a header defined — walked and folded, compared name by name.
#[test]
fn the_fold_and_the_walk_agree_on_a_fixture() {
    let mut session = session_with(&[
        (
            "/p/api.h",
            "#ifndef API_H\n#define API_H\n#define API_VERSION 3\nstruct Api { int field; };\n#endif\n",
        ),
        (
            "/p/impl.h",
            "#include \"api.h\"\n#define IMPL_ONLY 1\n#ifdef API_VERSION\n#define IMPL_NEW 1\n#endif\n",
        ),
        (
            "/p/a.cpp",
            "#define BEFORE 1\n#include \"api.h\"\n#include \"impl.h\"\n#undef BEFORE\nint x = API_VERSION;\n",
        ),
    ]);
    let walked = session
        .walked_bindings(Path::new("/p/a.cpp"))
        .expect("the unit walks");
    let seed_names = seed_names_of(&session);
    let composition = composition(
        &session,
        Path::new("/p/a.cpp"),
        session.index().macros(),
        &[],
    );
    let mut tally = Tally::default();
    for (path, bindings) in &walked {
        let Some(file) = composition.file(path) else {
            tally.missing_files += 1;
            tally
                .examples
                .push(format!("{}: not composed", path.display()));
            continue;
        };
        tally.merge(compare_file(file, bindings, &seed_names));
    }
    assert!(
        tally.files >= 3,
        "the unit reached {} file(s), the fixture has three",
        tally.files
    );
    assert!(
        tally.names > 0,
        "the comparison covered no names, so it proves nothing: {tally:#?}"
    );
    assert_eq!(
        composition.unknown_guards, 0,
        "the fixture is written to be decidable; an unknown guard means the fold cannot see a branch"
    );
    assert_eq!(
        tally.only_composed + tally.body_differs + tally.kind_differs + tally.missing_files,
        0,
        "the fold invented a name, or disagrees about one the walk has: {tally:#?}"
    );
}

/// **A macro from an earlier include is visible to a later one, and that is C++.** .cpp includes .h and then
/// .h; a compiler's state at the second #include holds the first one's definitions, so .h sees FROM_A`n/// even though it does not include .h itself. This is a test of the *semantics* the fold implements, not a leak:
/// an attempt to call it one is how a comparison against the walk's view would be misread.
#[test]
fn a_macro_from_an_earlier_include_is_visible_to_a_later_one() {
    let mut session = session_with(&[
        ("/p/a.h", "#define FROM_A 1\n"),
        ("/p/b.h", "int b;\n"),
        (
            "/p/a.cpp",
            r#"#include "a.h"
#include "b.h"
"#,
        ),
    ]);
    let walked = session
        .walked_bindings(Path::new("/p/a.cpp"))
        .expect("the unit walks");
    let b = walked
        .iter()
        .find(|(path, _)| path.ends_with("b.h"))
        .map(|(_, bindings)| bindings)
        .expect("b.h is in the unit");
    let seen = b.iter().any(|(name, _, _)| name == "FROM_A");
    assert!(
        seen,
        "b.h must see a macro defined before it in the same translation unit: {:?}",
        b.iter().map(|(name, _, _)| name).collect::<Vec<_>>()
    );
}

/// **The probe.** `EmmyLuaCodeStyle` (or `CPPLS_REAL_PROJECT`), the project's own directories, one unit per root.
#[test]
#[ignore = "a probe: walks a real project and compares instead of asserting"]
fn the_fold_and_the_walk_agree_on_a_real_project() {
    let root = PathBuf::from(
        std::env::var("CPPLS_REAL_PROJECT").unwrap_or_else(|_| r"E:\EmmyLuaCodeStyle".to_string()),
    );
    let directories: Vec<String> = std::env::var("CPPLS_PROBE_DIRS")
        .unwrap_or_else(|_| {
            "CodeFormat,CodeFormatCLib,CodeFormatCore,CodeFormatLib,CodeFormatServer,LuaParser,Util"
                .to_string()
        })
        .split(',')
        .map(|directory| directory.trim().to_string())
        .collect();
    let mut sources: Vec<PathBuf> = Vec::new();
    for directory in &directories {
        let mut pending = vec![root.join(directory)];
        while let Some(next) = pending.pop() {
            let Ok(entries) = std::fs::read_dir(&next) else {
                continue;
            };
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    pending.push(path);
                } else if path.extension().is_some_and(|extension| extension == "cpp") {
                    sources.push(path);
                }
            }
        }
    }
    sources.sort();
    println!("project {} ({} source(s))", root.display(), sources.len());

    let mut session = Session::open(
        root.clone(),
        SessionFiles::new(OpenDocuments::new(), crate::file::paths::DiskFiles),
        WatchFilter::new(&root),
    );
    session.add_project_files(sources.clone());
    session.index_everything();

    let seed_names = seed_names_of(&session);
    let roots: usize = std::env::var("CPPLS_PROBE_ROOTS")
        .ok()
        .and_then(|value| value.parse().ok())
        .unwrap_or(3);
    for root_source in sources.iter().take(roots) {
        let started = std::time::Instant::now();
        let Some(walked) = session.walked_bindings(root_source) else {
            println!("{}: the walk declined", root_source.display());
            continue;
        };
        let known: Vec<PathBuf> = walked.iter().map(|(path, _)| path.clone()).collect();
        let composition = composition(&session, root_source, session.index().macros(), &known);
        let mut tally = Tally::default();
        let mut per_file: Vec<(PathBuf, Tally)> = Vec::new();
        for (path, bindings) in &walked {
            let Some(file) = composition.file(path) else {
                tally.missing_files += 1;
                continue;
            };
            let one = compare_file(file, bindings, &seed_names);
            if one.only_walked + one.only_composed > 0 {
                per_file.push((path.clone(), one.clone()));
            }
            tally.merge(one);
        }
        per_file.sort_by_key(|(_, one)| std::cmp::Reverse(one.only_walked));
        for (path, one) in per_file.iter().take(8) {
            println!(
                "  worst {}: only walk {}, only fold {}, unknown {}, names {}",
                path.display(),
                one.only_walked,
                one.only_composed,
                composition.file(path).map_or(0, |file| file.unknown_guards),
                one.names
            );
            for example in one.examples.iter().take(3) {
                println!("    {example}");
            }
            if let Some(file) = composition.file(path) {
                let _ = file;
            }
        }
        println!(
            "{}: {} file(s), {} name(s) compared, {} mismatch(es), {} unknown guard(s) | only walk {}, only fold {}, \
             body {}, kind {}, missing {} | skipped own {} seed {} | {:?}",
            root_source
                .file_name()
                .unwrap_or_default()
                .to_string_lossy(),
            tally.files,
            tally.names,
            tally.mismatches(),
            composition.unknown_guards,
            tally.only_walked,
            tally.only_composed,
            tally.body_differs,
            tally.kind_differs,
            tally.missing_files,
            tally.skipped_own,
            tally.skipped_seed,
            started.elapsed(),
        );
        for name in tally.missing_names.iter().take(6) {
            let definer = composition
                .files
                .iter()
                .find(|file| file.at_exit.get(name).is_some())
                .map(|file| file.path.display().to_string());
            println!("  missing {name}: definer {definer:?}");
        }
        for example in tally.examples.iter().take(10) {
            println!("  {example}");
        }
        let mut undecided: Vec<&ComposedFile> = composition
            .files
            .iter()
            .filter(|file| file.unknown_guards > 0)
            .collect();
        undecided.sort_by_key(|file| std::cmp::Reverse(file.unknown_guards));
        for file in undecided.iter().take(5) {
            println!(
                "  undecided {}: {} directive(s)",
                file.path.display(),
                file.unknown_guards
            );
        }
        // **The environment a shared header is entered with**, for the two names the last diagnosis needed: a header
        // that defines a name and a downstream header that reads it are one question about the fold's propagation.
        for probe_name in ["_GLIBCXX_HOSTED", "__cplusplus"] {
            println!(
                "  probe {probe_name}: seed has {}",
                seed_names.contains(probe_name)
            );
            let shared = composition.files.iter().find(|file| {
                let name = file.path.to_string_lossy().to_lowercase();
                name.ends_with("bits/version.h") || name.ends_with("c++config.h")
            });
            if let Some(file) = shared {
                println!(
                    "    {}: entry {} {:?} / exit {} {:?} / unknown {}",
                    file.path.display(),
                    file.at_entry.get(probe_name).is_some(),
                    file.at_entry.get(probe_name).and_then(|found| found.body()),
                    file.at_exit.get(probe_name).is_some(),
                    file.at_exit.get(probe_name).and_then(|found| found.body()),
                    file.unknown_guards
                );
            }
        }
        println!(
            "  composition: {} file(s), unresolved includes {}",
            composition.files.len(),
            composition.unresolved_includes
        );
        // **The set difference, which is the question left**: the walk reached files the fold never composed, and
        // neither a guard nor a look-up failed on the fold's side — so the edges themselves differ. Printing the
        // first ten names the shape without asking the reader to guess it.
        let composed_paths: std::collections::HashSet<&PathBuf> =
            composition.files.iter().map(|file| &file.path).collect();
        let mut went_elsewhere: Vec<&PathBuf> = walked
            .iter()
            .map(|(path, _)| path)
            .filter(|path| !composed_paths.contains(*path))
            .collect();
        went_elsewhere.sort();
        println!(
            "  walk-only files: {}; e.g. {:?}",
            went_elsewhere.len(),
            went_elsewhere
                .iter()
                .take(10)
                .map(|path| path.display().to_string())
                .collect::<Vec<_>>()
        );
        for wanted in ["_mingw.h", "wchar.h", "time.h", "bits/chrono.h", "cstdlib"] {
            let mut found = false;
            let mut skeletons = 0;
            for file in &composition.files {
                let name = file.path.to_string_lossy().to_lowercase();
                if name.ends_with(wanted) {
                    found = true;
                    skeletons += 1;
                }
            }
            println!("  contains {wanted}: {found} ({skeletons})");
            let _ = skeletons;
        }
        for (path, offset, what, visibility, condition) in
            composition.skipped_includes.iter().take(12)
        {
            println!(
                "  skipped include [{visibility}] {}:{offset} {what} under {condition}",
                path.display()
            );
        }
        let mut classes: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for (_path, _offset, what) in &composition.unknown {
            let condition = what.split(" under ").nth(1).unwrap_or(what);
            // **The outermost region is the one that decides the chain**: `visibility_of` answers `Unknown` if any
            // enclosing region is undecided, and `guard_text` lists them outermost first, so the class is the last
            // segment, not the first.
            let first = condition
                .split(" && ")
                .last()
                .unwrap_or(condition)
                .trim_start_matches("If ")
                .split_whitespace()
                .next()
                .unwrap_or("?")
                .to_string();
            *classes.entry(first).or_insert(0) += 1;
        }
        let mut classes: Vec<(String, usize)> = classes.into_iter().collect();
        classes.sort_by_key(|(_, count)| std::cmp::Reverse(*count));
        println!("  unknown classes: {classes:?}");
        let over = crate::cache::env::Over::new(Env::empty(), session.index().macros());
        let bit_in_fold = composition.files.iter().find(|file| {
            let name = file.path.to_string_lossy().to_lowercase();
            name.ends_with("/bit") && !name.ends_with("/orbit")
        });
        let bit_in_walk = walked.iter().find(|(path, _)| {
            let name = path.to_string_lossy().to_lowercase();
            name.ends_with("/bit")
        });
        println!(
            "  <bit>: in fold {:?} (unknown {}), in walk {}",
            bit_in_fold.map(|file| file.path.display().to_string()),
            bit_in_fold.map_or(0, |file| file.unknown_guards),
            bit_in_walk.is_some()
        );
        if let Some(file) = bit_in_fold
            && let Some(summary) = session.index().summary(&file.path)
        {
            let wants: Vec<(String, String)> = summary
                .macros
                .iter()
                .filter(|fact| fact.name.contains("__glibcxx_want"))
                .take(4)
                .map(|fact| (fact.name.clone(), format!("{:?}", fact.guard)))
                .collect();
            let text = session.text(&file.path).unwrap_or_default();
            let skeleton = Skeleton::of(summary, &text);
            let region: Vec<String> = summary
                .guards
                .conditionals
                .get(2)
                .map(|conditional| {
                    conditional
                        .branches
                        .iter()
                        .map(|branch| format!("{:?} {:?}", branch.kind, branch.condition))
                        .collect()
                })
                .unwrap_or_default();
            println!("  <bit> region 2: {region:?}");
            println!(
                "  <bit> facts {}, want {} {:?}, steps {}, unreadable {}, unresolved {}",
                summary.macros.len(),
                wants.len(),
                wants,
                skeleton.steps().len(),
                skeleton.unreadable(),
                skeleton.unresolved_includes()
            );
        }
        let marked = session.index().macros();
        for name in [
            "__cpp_lib_bit_cast",
            "__cpp_lib_source_location",
            "__glibcxx_want_bit_cast",
        ] {
            let in_walk = walked
                .iter()
                .any(|(_, bindings)| bindings.iter().any(|(known, _, _)| known == name));
            let in_exit = composition
                .files
                .iter()
                .any(|file| file.at_exit.get(name).is_some());
            let in_entry = composition
                .files
                .iter()
                .any(|file| file.at_entry.get(name).is_some());
            println!(
                "  {name}: defined {}, unknown {}, in walk {}, in some exit {}, in some entry {}",
                marked.is_defined(name),
                marked.is_unknown(name),
                in_walk,
                in_exit,
                in_entry
            );
        }
        println!(
            "  __cplusplus: defined {}, body {}, incomplete {}, over {:?}",
            session.index().macros().is_defined("__cplusplus"),
            session.index().macros().get("__cplusplus").is_some(),
            session.index().macros().is_incomplete(),
            crate::MacroValues::lookup(&over, "__cplusplus")
        );
        for (path, offset, what) in composition.unknown.iter().take(20) {
            println!("  undecided {}:{offset}: {what}", path.display());
        }
    }
}

/// **Does the walk's view leak a macro from a subtree the file does not include?** `a.cpp` includes `p.h` (which
/// includes `m.h`, defining `M`) and then `q.h` (which includes `b.h`). A compiler never lets `b.h` see `M`; if the
/// walk's view of `b.h` reports it, the view is the global walk prefix rather than the include path — and every
/// `only walk` name in Gate 0 is that leak rather than a fold error.
#[test]
fn the_walk_view_leaks_an_unrelated_subtree() {
    let mut session = session_with(&[
        ("/p/m.h", "#define M 1\n"),
        ("/p/p.h", "#include \"m.h\"\n"),
        ("/p/b.h", "int b;\n"),
        ("/p/q.h", "#include \"b.h\"\n"),
        ("/p/a.cpp", "#include \"p.h\"\n#include \"q.h\"\n"),
    ]);
    let walked = session
        .walked_bindings(Path::new("/p/a.cpp"))
        .expect("the unit walks");
    let b = walked
        .iter()
        .find(|(path, _)| path.to_string_lossy().to_lowercase().ends_with("b.h"))
        .map(|(_, bindings)| bindings)
        .expect("b.h is in the unit");
    let leaked = b.iter().any(|(known, _, _)| known == "M");
    assert!(
        leaked,
        "MEASURED: b.h's view carries M, defined in a subtree it does not include: {:?}",
        b.iter().map(|(name, _, _)| name).collect::<Vec<_>>()
    );
}