cpp_code_analysis 0.1.4

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
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//! **A file's *effect* on the macro table**: what its directives do to the environment they are read in.
//!
//! `surface_of` says what a file's directives are; this module interprets them. Together they are the replacement
//! for "cook the include closure to find out what a header does":
//!
//! ```text
//! effect(surface, environment) = define/undef events that are *active* under that environment, in order
//! ```
//!
//!
//! ```text
//! ①  composition: a translation unit's macro table = fold(apply, effect of each included file). One hash lookup
//!    per include once the effect is cached, not a parse.
//! ②  a body does not move the effect: function bodies, statements and comments are not events, so
//!    `Surface::signature` is unchanged by them and the effect cannot change either.
//! ③  a guarded header's second inclusion is empty: its `#ifndef GUARD` is already false, so the effect has no
//!    folds at all — which is why including the same file three times costs one evaluation plus two lookups.
//! ```
//!
//! # What "cannot be said" means here, and why it is a first-class answer
//!
//! A condition the table cannot decide (`#if __has_include(<x>)` with no search paths, a value that is not a
//! number) gives [`Effect::certain`] `= false`. A caller must then fall back to the exact path — cooking the file —
//! rather than guess: an effect that quietly assumed `false` would drop every `#define` behind the condition, and
//! the index would be wrong rather than incomplete. That is the same distinction the preprocessor layer makes with
//! its `Value::Unknown`.

use std::collections::HashMap;

use crate::macros::MacroDef;
use crate::preprocess::condition::{self, Lookup, MacroValues, Value};
use crate::surface::{ConditionFlavour, Event, Surface};
use crate::token::Token;

/// **A macro table**: the environment an effect is interpreted against, and what an effect produces.
#[derive(Debug, Clone, Default)]
pub struct Macros {
    definitions: HashMap<String, MacroDef>,
    /// **A running signature of the table**, updated as it changes — the answer to "is this the same environment as
    /// when that file was read?" without copying the table. Kept in step in `define`/`undefine` rather than computed
    /// on demand, because computing it would be a walk of thousands of macros per file, which is the quadratic cost
    /// that made the first measurement of the chain 1.14 s for 266 files.
    signature: u64,
}

impl Macros {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn define(&mut self, definition: MacroDef) {
        let name = definition.name.to_string();
        self.signature = fold(self.signature, 1, &name);
        self.signature = fold(self.signature, 2, &body_text(&definition));
        self.definitions.insert(name, definition);
    }

    pub fn undefine(&mut self, name: &str) {
        self.signature = fold(self.signature, 3, name);
        self.definitions.remove(name);
    }

    /// **The environment's fingerprint**, order-sensitive and cheap: every define and undef moves it.
    pub fn signature(&self) -> u64 {
        self.signature
    }

    pub fn get(&self, name: &str) -> Option<&MacroDef> {
        self.definitions.get(name)
    }

    pub fn len(&self) -> usize {
        self.definitions.len()
    }

    pub fn is_empty(&self) -> bool {
        self.definitions.is_empty()
    }

    /// **The values of the names an effect is read through**, one `Option` per name — `None` for "not defined".
    ///
    /// Owned strings rather than borrows: the value is the body's *text*, which is assembled from the definition's
    /// tokens, and a key only needs it for the length of one hash.
    pub fn values_of(&self, names: &[String]) -> Vec<Option<String>> {
        names
            .iter()
            .map(|name| self.definitions.get(name).map(body_text))
            .collect()
    }
}

impl MacroValues for Macros {
    fn lookup(&self, name: &str) -> Lookup<'_> {
        match self.definitions.get(name) {
            Some(definition) => Lookup::Defined(definition),
            // **`Undefined` rather than `Unanswered`.** This table is the whole environment a caller handed over; a
            // name it does not hold is not defined, which is what the standard's `0` means and what makes
            // `#ifndef GUARD` true.
            None => Lookup::Undefined,
        }
    }
}

/// One step an effect performs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Fold {
    Define(MacroDef),
    Undef(String),
}

/// **What a file does to a macro table**, given the table it is read in.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Effect {
    pub folds: Vec<Fold>,
    /// Whether every condition could be decided. `false` means a caller must not trust this effect as *the*
    /// effect — see the module documentation.
    pub certain: bool,
}

impl Effect {
    /// **The key this effect is cached under**: the file's directive signature plus the *values* of the names it
    /// tests. A file that tests nothing has the same key in every environment.
    pub fn key(signature: u64, values: &[Option<&str>]) -> u64 {
        let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
        let mut eat = |byte: u8| {
            hash ^= u64::from(byte);
            hash = hash.wrapping_mul(0x100_0000_01b3);
        };
        for byte in signature.to_le_bytes() {
            eat(byte);
        }
        for value in values {
            match value {
                Some(text) => {
                    for byte in text.as_bytes() {
                        eat(*byte);
                    }
                }
                None => eat(0xff),
            }
            eat(0xfe);
        }
        hash
    }

    /// Apply to a table: what "including this file" does.
    pub fn apply(&self, table: &mut Macros) {
        for fold in &self.folds {
            match fold {
                Fold::Define(definition) => table.define(definition.clone()),
                Fold::Undef(name) => table.undefine(name),
            }
        }
    }

    /// The names this effect defines or undefines — the *observable* part, for tests and for a caller that wants to
    /// know whether anything downstream can care.
    pub fn touched(&self) -> Vec<&str> {
        self.folds
            .iter()
            .map(|fold| match fold {
                Fold::Define(definition) => definition.name.as_ref(),
                Fold::Undef(name) => name.as_str(),
            })
            .collect()
    }
}

/// **Interpret a file's directives against an environment.**
pub fn effect_of(surface: &Surface, environment: &Macros) -> Effect {
    let mut effect = Effect {
        folds: Vec::new(),
        certain: true,
    };
    // One entry per `#if`: whether the enclosing region is active, whether a branch has been taken, and whether the
    // current branch is the active one.
    struct Region {
        outer_active: bool,
        taken: bool,
        active: bool,
    }
    let mut regions: Vec<Region> = Vec::new();
    let active = |regions: &[Region]| regions.last().is_none_or(|region| region.active);

    for event in &surface.events {
        match event {
            Event::If {
                tokens, flavour, ..
            } => {
                let outer_active = active(&regions);
                let decided = outer_active.then(|| decide(*flavour, tokens, environment));
                let holds = match decided {
                    Some(Value::Known(value)) => value != 0,
                    Some(_) => {
                        effect.certain = false;
                        false
                    }
                    None => false,
                };
                regions.push(Region {
                    outer_active,
                    taken: holds,
                    active: outer_active && holds,
                });
            }
            Event::Elif {
                tokens, flavour, ..
            } => {
                let Some(region) = regions.last_mut() else {
                    continue;
                };
                if region.taken || !region.outer_active {
                    region.active = false;
                    continue;
                }
                let holds = match decide(*flavour, tokens, environment) {
                    Value::Known(value) => value != 0,
                    _ => {
                        effect.certain = false;
                        false
                    }
                };
                region.taken = holds;
                region.active = holds;
            }
            Event::Else { .. } => {
                if let Some(region) = regions.last_mut() {
                    region.active = region.outer_active && !region.taken;
                    region.taken = true;
                }
            }
            Event::Endif { .. } => {
                regions.pop();
            }
            Event::Define { definition } => {
                if active(&regions) {
                    effect.folds.push(Fold::Define(definition.clone()));
                }
            }
            Event::Undef { name, .. } => {
                if active(&regions) {
                    effect.folds.push(Fold::Undef(name.clone()));
                }
            }
            // An `#include` is the caller's step, not this file's: a surface says *that* it includes something, and
            // the fold of the included file's own effect is what moves the table.
            Event::Include { .. } | Event::Other { .. } => {}
        }
    }

    effect
}

/// **One conditional, decided.** `#ifdef`/`#ifndef` ask whether a name exists; `#if`/`#elif` read an expression.
fn decide(flavour: ConditionFlavour, tokens: &[Token], environment: &Macros) -> Value {
    match flavour {
        ConditionFlavour::Expression => condition::evaluate(tokens, environment),
        ConditionFlavour::Defined | ConditionFlavour::NotDefined => {
            // The name is the first identifier of the line, and a condition the line does not spell is undecidable.
            let Some(name) = tokens
                .iter()
                .find(|token| token.kind == cpp_parser::CppTokenKind::Identifier)
            else {
                return Value::Unknown;
            };
            match environment.lookup(name.text.as_ref()).is_defined() {
                Some(defined) => {
                    Value::Known(i128::from(if flavour == ConditionFlavour::Defined {
                        defined
                    } else {
                        !defined
                    }))
                }
                None => Value::Unknown,
            }
        }
    }
}

/// The spelling of a definition's body — what a name's *value* is for a condition, and for the key.
fn body_text(definition: &MacroDef) -> String {
    // **Every token of the body, as written.** Whitespace is kept because `#define A (1)` and
    // `#define A(1)` are different macros, and a caller comparing two readings should see the text
    // it wrote.
    let mut out = String::new();
    for token in &definition.body.tokens {
        out.push_str(&token.text);
    }
    out.trim().to_string()
}

/// **One step of the table`s fingerprint** — FNV-1a over a tag and a string.
fn fold(mut hash: u64, tag: u8, text: &str) -> u64 {
    let mut eat = |byte: u8| {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x100_0000_01b3);
    };
    eat(tag);
    for byte in text.as_bytes() {
        eat(*byte);
    }
    eat(0xff);
    hash
}

/// The names a surface tests, with the values they hold in an environment — the key's material.
pub fn values_for(surface: &Surface, environment: &Macros) -> Vec<Option<String>> {
    surface
        .tests()
        .into_iter()
        .map(|name| {
            environment
                .get(&name)
                .map(|definition| body_text(definition).to_string())
        })
        .collect()
}

/// The tokens of a condition, exposed for callers that evaluate one themselves.
pub fn condition_tokens(tokens: &[Token]) -> &[Token] {
    tokens
}

/// **The surfaces a walk has already scanned**, keyed by the file's content.
///
/// A walk has to lex every file it enters to know what its directives are, and without this the lex is paid again
/// on every walk: measured, 266 files took 1.05 s per walk and the same files' surfaces cost 0.5 ms each to build
/// *once*. Content-hashed rather than path-keyed, because a path with new text is a different surface.
///
/// **How many are kept**: one per distinct content, the table cleared when it fills — see [`Surfaces::of`].
pub const SURFACES_HELD: usize = 512;

#[derive(Default)]
pub struct Surfaces {
    held: std::sync::Mutex<HashMap<u64, std::sync::Arc<Surface>>>,
}

impl Surfaces {
    pub fn new() -> Self {
        Self::default()
    }

    /// The surface of `text`, scanned once per distinct content.
    pub fn of(&self, path: &std::path::Path, text: &str) -> std::sync::Arc<Surface> {
        let key = crate::cache::content_hash(text);
        let mut held = self
            .held
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        // **Bounded**: one surface per distinct content, and content changes on every keystroke — a session
        // left open while typing would keep a scan of every buffer state it ever saw. Cleared rather than
        // evicted because a surface is cheap to rebuild (one scan of one file) and the table is only a warm start.
        if held.len() >= SURFACES_HELD {
            held.clear();
        }
        if let Some(found) = held.get(&key) {
            return std::sync::Arc::clone(found);
        }
        let made = std::sync::Arc::new(crate::surface::surface_of(path, text));
        held.insert(key, std::sync::Arc::clone(&made));
        made
    }

    pub fn len(&self) -> usize {
        self.held.lock().map(|held| held.len()).unwrap_or(0)
    }

    /// **Nothing held** — the same question as `len() == 0`, written where a caller looks for it.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// **How the chain reads files**: a provider and an include resolver, both as plain functions so that a caller can
/// hand over a session, a test's memory files, or a probe.
pub trait Reads {
    /// The text of a file, or `None` when it cannot be read (a missing include is a hole, not an error).
    fn read(&self, path: &std::path::Path) -> Option<String>;
    /// Where an `#include` written in `from` resolves to, or `None` when it does not.
    fn resolve(&self, from: &std::path::Path, spelling: &str) -> Option<std::path::PathBuf>;
}

/// What one walk of an include tree cost, and how often the effect cache answered.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Walked {
    /// Files entered.
    pub files: usize,
    /// Inclusions, counting repeats — a guarded header included four times counts four.
    pub inclusions: usize,
    /// Effect evaluations that had to run.
    pub evaluated: usize,
    /// Effect lookups answered by the cache.
    pub reused: usize,
    /// Effects that could not be trusted (`Effect::certain == false`).
    pub uncertain: usize,
    /// Include edges that did not resolve.
    pub unresolved: usize,
}

/// **The macro table a translation unit has**, built by folding effects instead of cooking.
///
/// This is the step the whole design exists for: a file's contribution is its effect, an effect is a function of the
/// few names it tests, and a fold is a hash lookup — so building the environment for a program is `|includes|`
/// lookups, not a closure walk per file.
pub struct Chain<'a, R: Reads> {
    reader: &'a R,
    /// `Effect::key` → effect. Shared across roots by a caller that wants the project-wide reuse.
    effects: &'a mut HashMap<u64, Effect>,
    /// Where each file's table ended up, for a caller that wants to read a file's environment afterwards.
    tables: HashMap<std::path::PathBuf, Macros>,
    /// **Each file`s environment as a signature** — the same information as `tables`, without a copy of the macro
    /// table per file. A table is the whole closure`s macros; cloning it once per file is quadratic in a project`s
    /// size, which is what the first measurement showed (1.14 s for 266 files, almost all of it cloning).
    signatures: HashMap<std::path::PathBuf, u64>,
    /// **The surfaces already scanned**, when a caller has one to share across walks.
    surfaces: Option<&'a Surfaces>,
    /// Whether to keep the tables themselves, which only a comparison test needs.
    keep_tables: bool,
    walked: Walked,
}

impl<'a, R: Reads> Chain<'a, R> {
    pub fn new(reader: &'a R, effects: &'a mut HashMap<u64, Effect>) -> Self {
        Self {
            reader,
            effects,
            tables: HashMap::new(),
            signatures: HashMap::new(),
            keep_tables: true,
            surfaces: None,
            walked: Walked::default(),
        }
    }

    /// **Without a copy of the tables** — the signatures only, which is what a caller asking "did this file`s
    /// environment change?" needs. Cloning a table per file is quadratic: the first measurement of this walk on a
    /// real program (266 files) was 1.14 s, and almost all of it was those clones.
    /// **Share the surfaces between walks** — see [`Surfaces`].
    pub fn with_surfaces(mut self, surfaces: &'a Surfaces) -> Self {
        self.surfaces = Some(surfaces);
        self
    }

    pub fn without_tables(mut self) -> Self {
        self.keep_tables = false;
        self
    }

    /// **Enter a program at `root`** and answer the table every file in it was read under.
    /// **The same walk, answering signatures only** — see [`Chain::without_tables`].
    pub fn at_signatures(
        mut self,
        root: &std::path::Path,
        seed: &Macros,
    ) -> (HashMap<std::path::PathBuf, u64>, Walked) {
        self.keep_tables = false;
        let mut table = seed.clone();
        self.enter(root, &mut table, 0);
        (self.signatures, self.walked)
    }

    pub fn at(
        mut self,
        root: &std::path::Path,
        seed: &Macros,
    ) -> (HashMap<std::path::PathBuf, Macros>, Walked) {
        let mut table = seed.clone();
        self.enter(root, &mut table, 0);
        (self.tables, self.walked)
    }

    fn enter(&mut self, path: &std::path::Path, table: &mut Macros, depth: usize) {
        // A cycle or an absurdly deep chain: a compiler refuses at its own depth limit, and a walk that does not
        // stop here does not stop at all.
        if depth > 64 {
            return;
        }
        let Some(text) = self.reader.read(path) else {
            return;
        };
        self.walked.files += 1;
        let surface = match self.surfaces {
            Some(surfaces) => surfaces.of(path, &text),
            None => std::sync::Arc::new(crate::surface::surface_of(path, &text)),
        };
        if surface.events.is_empty() {
            return;
        }

        // **What a file saw when its own directives began** — recorded before its first own define or undef, which is
        // the moment a reader asks about ("what was in force when this header started writing?"), and the moment the
        // compiler-shaped walk answers for too (it excludes the file`s own facts from its view).
        let mut seen: Option<Macros> = None;

        // **A file with no includes is its own effect**, and that effect is cacheable whole: nothing outside it can
        // move the table between its first and last directive.
        let has_includes = surface
            .events
            .iter()
            .any(|event| matches!(event, Event::Include { .. }));
        if !has_includes {
            let values = values_for(&surface, table);
            let values: Vec<Option<&str>> = values.iter().map(|value| value.as_deref()).collect();
            let key = Effect::key(surface.signature(), &values);
            let effect = match self.effects.get(&key) {
                Some(known) => {
                    self.walked.reused += 1;
                    known.clone()
                }
                None => {
                    let made = effect_of(&surface, table);
                    self.walked.evaluated += 1;
                    self.effects.insert(key, made.clone());
                    made
                }
            };
            if !effect.certain {
                self.walked.uncertain += 1;
            }
            self.tables
                .entry(path.to_path_buf())
                .or_insert_with(|| table.clone());
            effect.apply(table);
            return;
        }

        // **The general case: folded in order, with the conditional state tracked.** An `#include` inside a false
        // branch is not an include — folding it is how a guarded header dragged its dependencies in on its second
        // inclusion, which the oracle test below found by comparing every macro against the compiler-shaped walk.
        self.walked.evaluated += 1;
        let mut regions: Vec<(bool, bool, bool)> = Vec::new();
        let active =
            |regions: &Vec<(bool, bool, bool)>| regions.last().is_none_or(|region| region.2);
        for event in &surface.events {
            match event {
                Event::If {
                    tokens, flavour, ..
                } => {
                    let outer = active(&regions);
                    let holds = outer
                        && matches!(decide(*flavour, tokens, table), Value::Known(value) if value != 0);
                    regions.push((outer, holds, outer && holds));
                }
                Event::Elif {
                    tokens, flavour, ..
                } => {
                    if let Some(region) = regions.last_mut() {
                        if region.1 || !region.0 {
                            region.2 = false;
                        } else {
                            let holds = matches!(
                                decide(*flavour, tokens, table),
                                Value::Known(value) if value != 0
                            );
                            region.1 = holds;
                            region.2 = holds;
                        }
                    }
                }
                Event::Else { .. } => {
                    if let Some(region) = regions.last_mut() {
                        region.2 = region.0 && !region.1;
                        region.1 = true;
                    }
                }
                Event::Endif { .. } => {
                    regions.pop();
                }
                Event::Include { spelling, .. } => {
                    if !active(&regions) {
                        continue;
                    }
                    self.walked.inclusions += 1;
                    match self.reader.resolve(path, spelling) {
                        Some(included) => {
                            let included = included.clone();
                            self.enter(&included, table, depth + 1);
                        }
                        None => self.walked.unresolved += 1,
                    }
                }
                Event::Define { definition } => {
                    if seen.is_none() {
                        seen = Some(table.clone());
                    }
                    if active(&regions) {
                        table.define(definition.clone());
                    }
                }
                Event::Undef { name, .. } => {
                    if seen.is_none() {
                        seen = Some(table.clone());
                    }
                    if active(&regions) {
                        table.undefine(name);
                    }
                }
                Event::Other { .. } => {}
            }
        }
        // **The first time this file was entered**, not the last: a header included from three places has three
        // tables, and the one a reader asks about is the one its first inclusion saw. Keeping the last made the
        // oracle report deep.h macros as belonging to guarded.h.
        self.signatures
            .entry(path.to_path_buf())
            .or_insert_with(|| seen.as_ref().unwrap_or(table).signature());
        if self.keep_tables {
            self.tables
                .entry(path.to_path_buf())
                .or_insert_with(|| seen.unwrap_or_else(|| table.clone()));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    fn surface(text: &str) -> Surface {
        crate::surface::surface_of(Path::new("/p/a.h"), text)
    }

    fn table(defines: &[(&str, &str)]) -> Macros {
        let mut macros = Macros::new();
        for (name, body) in defines {
            let definition = crate::preprocess::macros::parse_define(
                &[
                    Token::new(
                        cpp_parser::CppTokenKind::Identifier,
                        *name,
                        cpp_parser::SourceRange::new(0, 0),
                    ),
                    Token::new(
                        cpp_parser::CppTokenKind::Whitespace,
                        " ",
                        cpp_parser::SourceRange::new(0, 0),
                    ),
                    Token::new(
                        cpp_parser::CppTokenKind::Identifier,
                        *body,
                        cpp_parser::SourceRange::new(0, 0),
                    ),
                ],
                cpp_parser::SourceRange::new(0, 0),
            );
            if let Some(definition) = definition {
                macros.define(definition);
            }
        }
        macros
    }

    #[test]
    fn an_unconditional_define_is_an_export_in_every_environment() {
        let surface = surface("#define A 1\n");
        let empty = Macros::new();
        let effect = effect_of(&surface, &empty);
        assert_eq!(effect.touched(), vec!["A"]);
        assert!(effect.certain);

        let mut applied = Macros::new();
        effect.apply(&mut applied);
        assert!(applied.get("A").is_some(), "the define reached the table");
    }

    #[test]
    fn a_conditional_define_follows_the_environment() {
        let surface = surface("#ifdef WIN32\n#define API __stdcall\n#endif\n");

        let without = Macros::new();
        let effect = effect_of(&surface, &without);
        assert!(effect.touched().is_empty(), "WIN32 is not defined here");

        let with = table(&[("WIN32", "1")]);
        let effect = effect_of(&surface, &with);
        assert_eq!(
            effect.touched(),
            vec!["API"],
            "certain={} folds={:?} tokens={:?}",
            effect.certain,
            effect.folds.len(),
            surface.events
        );
    }

    #[test]
    fn the_guarded_second_inclusion_is_empty() {
        // **Property ③**: a header that guards itself defines nothing the second time, and that is why including it
        // three times costs one evaluation and two empty ones.
        let surface = surface("#ifndef GUARD_H\n#define GUARD_H\n#define INSIDE 1\n#endif\n");
        let fresh = Macros::new();
        let first = effect_of(&surface, &fresh);
        assert_eq!(first.touched(), vec!["GUARD_H", "INSIDE"]);

        let mut table = Macros::new();
        first.apply(&mut table);
        let second = effect_of(&surface, &table);
        assert!(
            second.touched().is_empty(),
            "the guard is defined, so nothing is exported again: {:?}",
            second.touched()
        );
    }

    #[test]
    fn an_undef_takes_a_name_away() {
        let surface = surface("#define A 1\n#undef A\n");
        let mut table = Macros::new();
        effect_of(&surface, &Macros::new()).apply(&mut table);
        assert!(table.get("A").is_none(), "defined and then undefined");
    }

    #[test]
    fn a_condition_that_cannot_be_decided_is_not_certain() {
        // `#if TARGET` with `TARGET` undefined is `0` and decidable; `#if __has_include(<vector>)` is not: the
        // table has no search paths, so it answers `Unknown` and the effect must say so rather than guess.
        let surface = surface("#if __has_include(<vector>)\n#define A 1\n#endif\n");
        let effect = effect_of(&surface, &Macros::new());
        assert!(!effect.certain, "the condition cannot be decided here");
    }

    #[test]
    fn the_value_of_a_macro_is_its_whole_body() {
        // The oracle found this: the value of `F(x)` answered `"("` because only the first token of the body was read.
        // A condition that asks `#if F(1) == 2` would then have been decided from a parenthesis.
        let surface = surface("#define WITH_ARGS(x) ((x) + 1)\n#define PLAIN (1)\n");
        let mut applied = Macros::new();
        effect_of(&surface, &Macros::new()).apply(&mut applied);
        assert_eq!(
            applied.get("WITH_ARGS").map(body_text),
            Some("((x) + 1)".to_string())
        );
        assert_eq!(applied.get("PLAIN").map(body_text), Some("(1)".to_string()));
    }

    /// A small include graph, in memory: the reader and resolver a chain needs.
    struct Fixture(std::collections::HashMap<std::path::PathBuf, String>);

    impl Fixture {
        fn new(files: &[(&str, &str)]) -> Self {
            let mut held = std::collections::HashMap::new();
            for (path, text) in files {
                held.insert(std::path::PathBuf::from(*path), text.to_string());
            }
            Self(held)
        }
    }

    impl Reads for Fixture {
        fn read(&self, path: &std::path::Path) -> Option<String> {
            self.0.get(path).cloned()
        }

        fn resolve(&self, _from: &std::path::Path, spelling: &str) -> Option<std::path::PathBuf> {
            // Every fixture is in one directory, so the spelling names the file: `"a.h"` and `<a.h>` alike.
            let name = spelling.trim_matches(|c| c == '"' || c == '<' || c == '>');
            let path = std::path::PathBuf::from("/p").join(name);
            self.0.contains_key(&path).then_some(path)
        }
    }

    #[test]
    fn a_chain_folds_what_the_includes_define() {
        let files = Fixture::new(&[
            ("/p/a.h", "#define A 1\n"),
            (
                "/p/b.h",
                "#include \"a.h\"\n#ifdef A\n#define B 2\n#endif\n",
            ),
        ]);
        let mut cache = std::collections::HashMap::new();
        let chain = Chain::new(&files, &mut cache);
        let (tables, walked) = chain.at(std::path::Path::new("/p/b.h"), &Macros::new());

        let at_b = tables
            .get(std::path::Path::new("/p/b.h"))
            .expect("b was entered");
        assert!(at_b.get("A").is_some(), "a.h`s define is in b.h`s table");
        assert_eq!(walked.files, 2, "a.h and b.h");
        assert_eq!(walked.inclusions, 1);
    }

    #[test]
    fn a_guarded_header_included_twice_exports_once() {
        let files = Fixture::new(&[
            (
                "/p/g.h",
                "#ifndef G_H\n#define G_H\n#define INSIDE 1\n#endif\n",
            ),
            ("/p/root.cpp", "#include \"g.h\"\n#include \"g.h\"\n"),
        ]);
        let mut cache = std::collections::HashMap::new();
        let chain = Chain::new(&files, &mut cache);
        let (tables, walked) = chain.at(std::path::Path::new("/p/root.cpp"), &Macros::new());

        let at_root = tables
            .get(std::path::Path::new("/p/root.cpp"))
            .expect("root was entered");
        assert!(at_root.get("G_H").is_some());
        assert_eq!(walked.inclusions, 2, "both includes are walked");
        // **Not a cache hit, and that is the point**: the guard is defined by the first inclusion, so the second
        // inclusion tests `G_H` against a different value — a different key, a different effect, and that effect has
        // nothing to do. The cheapness here comes from the effect being empty, not from a shared entry.
        assert_eq!(
            walked.evaluated, 3,
            "the root and both inclusions — two different environments for g.h, one effect each"
        );
        assert_eq!(walked.reused, 0);
    }

    #[test]
    fn a_define_before_an_include_is_in_the_table_that_include_reads() {
        // **The interleaved shape**: the include is read under a table this file has already changed, so a chain
        // that folded "all includes, then all defines" would answer `C` undefined here.
        let files = Fixture::new(&[
            ("/p/c.h", "#ifdef C\n#define SEEN 1\n#endif\n"),
            ("/p/root.cpp", "#define C 1\n#include \"c.h\"\n"),
        ]);
        let mut cache = std::collections::HashMap::new();
        let chain = Chain::new(&files, &mut cache);
        let (tables, _) = chain.at(std::path::Path::new("/p/root.cpp"), &Macros::new());

        let at_c = tables
            .get(std::path::Path::new("/p/c.h"))
            .expect("c was entered");
        assert!(
            at_c.get("C").is_some(),
            "the host`s define comes before the include that reads it"
        );
    }

    #[test]
    fn the_key_of_an_environment_insensitive_file_does_not_depend_on_it() {
        let plain = surface("#define A 1\n#include <vector>\n");
        assert!(plain.tests().is_empty());
        assert_eq!(
            Effect::key(plain.signature(), &[]),
            Effect::key(plain.signature(), &[])
        );
        // And a file that does test a name changes its key with that name's value.
        let sensitive = surface("#ifdef WIN32\n#define A 1\n#endif\n");
        let key_without = Effect::key(sensitive.signature(), &[None]);
        let key_with = Effect::key(sensitive.signature(), &[Some("1")]);
        assert_ne!(key_without, key_with);
    }
    /// **The oracle**: the same fixture, read two ways — the effect chain, and the compiler-shaped walk that the
    /// index uses today. Every macro the chain says a file sees must be what the walk says it sees.
    #[test]
    fn the_chain_agrees_with_the_walk_on_every_file() {
        use crate::summary::MacroDefinitions;
        use crate::{CompilerConfig, FileIndexer, MemoryFiles, SummaryKey, TranslationUnit};
        use cpp_parser::MacroFacts;

        let files: &[(&str, &str)] = &[
            (
                "/p/base.h",
                "#define BASE 1\n#define WITH_ARGS(x) ((x) + 1)\n",
            ),
            (
                "/p/guarded.h",
                "#ifndef GUARDED_H\n#define GUARDED_H\n#include \"base.h\"\n#ifdef BASE\n#define FROM_INSIDE 2\n#endif\n#endif\n",
            ),
            (
                "/p/deep.h",
                "#include \"guarded.h\"\n#define AFTER 3\n#if AFTER == 3\n#define READS_ITS_OWN 4\n#endif\n",
            ),
            (
                "/p/root.cpp",
                "#include \"deep.h\"\n#include \"guarded.h\"\n#ifndef UNDEFINED_HERE\n#define FALLBACK 5\n#endif\n",
            ),
        ];

        // The chain.
        let fixture = Fixture::new(files);
        let mut cache = std::collections::HashMap::new();
        let (tables, walked) = Chain::new(&fixture, &mut cache)
            .at(std::path::Path::new("/p/root.cpp"), &Macros::new());

        // The walk.
        let mut provider = MemoryFiles::new();
        for (path, text) in files {
            provider.insert(*path, *text);
        }
        let config = CompilerConfig::default().with_include_path(std::path::PathBuf::from("/p"));
        let indexer = FileIndexer::new(&provider, &config);
        let mut summaries: std::collections::HashMap<std::path::PathBuf, crate::FileSummary> =
            std::collections::HashMap::new();
        let mut texts: std::collections::HashMap<std::path::PathBuf, String> =
            std::collections::HashMap::new();
        for (path, text) in files {
            let path = std::path::PathBuf::from(*path);
            summaries.insert(
                path.clone(),
                indexer.index(&path, text, SummaryKey::new(0, 0)),
            );
            texts.insert(path, text.to_string());
        }
        let mut definitions = MacroDefinitions::default();
        let unit = TranslationUnit::walk(
            summaries
                .get(std::path::Path::new("/p/root.cpp"))
                .expect("the root was indexed"),
            |wanted| {
                summaries
                    .get(wanted)
                    .map(|summary| (summary, texts.get(wanted).map(String::as_str).unwrap_or("")))
            },
            &crate::include::graph::Marked::default(),
            &mut definitions,
            false,
            &crate::WalkCache::new(),
        );

        // **Every file both sides reach**, and every macro the chain says that file sees.
        let mut compared = 0usize;
        let mut pathless = 0usize;
        let mut disagreements: Vec<String> = Vec::new();
        for (path, table) in &tables {
            let Some(view) = unit.environment_of(path) else {
                pathless += 1;
                continue;
            };
            let mut names: Vec<&String> = table.definitions.keys().collect();
            names.sort();
            for name in names {
                let from_chain = table
                    .get(name)
                    .map(body_text)
                    .map(|text| text.trim().to_string());
                let from_walk = view
                    .body_text_of(name, usize::MAX)
                    .map(|text| text.trim().to_string());
                compared += 1;
                if from_walk != from_chain {
                    disagreements.push(format!(
                        "{}: {name} walk={from_walk:?} chain={from_chain:?}",
                        path.display()
                    ));
                }
            }
        }
        assert!(
            disagreements.is_empty(),
            "{} of {compared} name(s) disagree ({pathless} file(s) the walk does not reach): {:?}",
            disagreements.len(),
            &disagreements[..disagreements.len().min(6)]
        );
        assert!(compared > 0, "the two readings share at least one file");
        assert!(
            walked.files >= 4,
            "the chain entered every file: {walked:?}"
        );
    }
}