cpp_code_analysis 0.1.1

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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
//! Following includes: the dependency graph, and the visits that build it.
//!
//! # What the graph is for
//!
//! A **reverse** edge is the only way to answer "what does this edit affect?". Without it, changing a header
//! means discarding every analysis in the project, because nothing records which files read it — and
//! discarding everything is the same as having no cache. So [`crate::graph::FileGraph::dependents_of`] is
//! the point of this module, and the forward edges exist to compute it.
//!
//! # Why a header cannot be analysed once and reused
//!
//! `#include` is textual, so the macros in force when a header is read depend on *who included it*: a
//! header reached from two places can see two different macro tables, and its `#if` conditions can go
//! different ways. The graph therefore records an edge per *include*, not per file pair, and each edge
//! carries what the visit decided.
//!
//! # What a guard does here
//!
//! A guarded header is read once — that is what the guard is for — and a second edge to it is marked
//! [`Visit::Skipped`] rather than followed. An **unguarded** header is not: a compiler reads it again every
//! time and re-defines everything in it, and so does this, because pretending otherwise would hide the
//! duplicate definitions that are the reason guards get written.
//!
//! # What this deliberately does not do
//!
//! It does not reconstruct a position-exact macro table for every byte of every file. What a consumer asks
//! at this stage is "is this file included, and did its guard take effect", and the conditions *inside* a
//! file are already carried by the [`crate::guard::Guard`] on each declaration. Building a table per file is
//! a per-keystroke cost nothing needs yet.
//!
//! # What happens when the context is not there
//!
//! A header is written to be included, so its conditions are about macros its **includer** supplies — and when
//! a user opens a header directly there is no includer in the analysis. The macro environment is then
//! genuinely unknown, and the walk says so rather than guessing: the file is recorded with
//! [`FileEntry::missing_context`], and names nothing in the walk has seen become *unknown* instead of
//! undefined, so an `#ifdef` on them neither kills the code behind it nor hides the includes in it.
//!
//! Both directions of the guess are wrong in their own way and they are not equally wrong. Calling an
//! unknown context complete reports errors in code that compiles; calling a complete one unknown only makes
//! the analysis quieter. So the flag errs the second way, and a consumer can always tell "no macros are
//! defined here" apart from "the macros that are defined here are not all of them".

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

use cpp_parser::CppSyntaxTree;

use crate::{
    config::CompilerConfig,
    directive::{Directive, DirectiveKind, IncludeForm, SpannedDirective},
    guards::{Guard, analyse_guards},
    include::{FoundIn, IncludeResolver, Resolution},
    paths::{FileId, FileProvider, PathInterner, parent_normalized},
};

/// How deep includes are followed before the walk gives up.
///
/// A real limit rather than a formality: include cycles exist in real code and are broken by guards rather
/// than by the graph, so a walk that trusted the graph alone would not terminate. Depth also bounds the
/// *work*: a header twenty levels down is not worth re-analysing for a keystroke.
pub const MAX_INCLUDE_DEPTH: usize = 64;

/// Why an include was not followed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SkipReason {
    /// The file is already on the path being walked, so following it would not terminate.
    Cycle,
    /// Reached deeper than [`MAX_INCLUDE_DEPTH`].
    TooDeep,
    /// The file guards itself and has been read already, so a compiler would not read it again either.
    AlreadyVisited,
    /// The walk was asked to look at one file only, so this include was recorded and not read.
    ///
    /// Distinct from the other reasons on purpose: they say "reading this would be wrong or pointless", while
    /// this one says "reading this was not asked for". A consumer showing why a declaration is missing needs
    /// to tell those apart, because only one of them is a fact about the code.
    OutOfScope,
}

/// What happened at the other end of an include.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Visit {
    /// The file was read and its own includes followed.
    Analysed,
    /// The file was not read again.
    Skipped(SkipReason),
}

/// An `#include` that resolved.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Edge {
    pub from: FileId,
    pub to: FileId,
    /// The name as written, so a consumer can show the include rather than the resolved path.
    pub name: Box<str>,
    pub is_angle: bool,
    /// Which search step found it — what `#include_next` needs, and what distinguishes a project header
    /// from a system one.
    pub found_in: FoundIn,
    pub visit: Visit,
    /// Where the directive is.
    pub range: cpp_parser::SourceRange,
}

/// An `#include` that did not resolve, and where it was written.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnresolvedInclude {
    pub from: FileId,
    /// The name as written, without delimiters.
    pub name: Box<str>,
    /// Every candidate that was tried, in order — so a "cannot find header" message can say where it
    /// looked, which is the difference between an actionable message and a useless one.
    pub searched: Vec<PathBuf>,
    pub range: cpp_parser::SourceRange,
}

/// A file the walk reached.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileEntry {
    pub path: PathBuf,
    /// How the file protects itself.
    pub guard: Guard,
    /// The macros it defines, in order.
    ///
    /// Kept even when the visit was skipped, because they are what the skip is *about*: a guarded header
    /// defines its guard, and that definition is why the second visit does nothing.
    pub defines: Vec<Box<str>>,
    /// How deeply it was first reached; `0` for the file the walk started from.
    pub depth: usize,
    /// The macro names in force **when this file was entered**.
    ///
    /// This is the cross-file chain made observable. `#include` is textual, so a header reached from two
    /// places can see two different sets of macros — and its `#if` conditions can go different ways. A
    /// consumer looking up the macros that were in force at this file's first line has the answer here
    /// instead of re-walking the graph to find out.
    ///
    /// Names, not definitions: what a condition asks is whether a name is defined, and a name's *value* is
    /// [`crate::condition`]'s business once expansion has run. The set is sorted, so two runs of the walk
    /// can be compared.
    pub macros: Vec<Box<str>>,
    /// Whether macros that could not be accounted for might still have been in force here.
    ///
    /// True for a header the walk started from, because its includer — the translation unit that supplies the
    /// `-D`s and the `#define`s coming before it — is not part of this walk and cannot be guessed. True also
    /// when the walk was asked not to follow includes ([`WalkScope::FileOnly`]), since then nothing outside
    /// the file was consulted either. False for a translation unit walked normally, whose macro environment
    /// really does start at the command line.
    ///
    /// The distinction is not pedantry, and the two wrong answers are not equally wrong:
    ///
    /// * Say a header's environment is **complete** when it is not, and every macro the includer defines
    ///   reads as undefined. `#ifdef _WIN32` goes the wrong way, and `#if 0`-style dead code is invented out
    ///   of a condition that was never false — which is how a consumer ends up reporting errors in code that
    ///   compiles.
    /// * Say a translation unit's environment is **incomplete** when it is complete, and nothing is decided
    ///   that could have been. Diagnostics get quieter, which is recoverable, and completion gets noisier,
    ///   which is merely unhelpful.
    ///
    /// So the second mistake is the one to make when the answer is genuinely unknown, and the flag is what
    /// lets a consumer tell "no macros are defined here" apart from "the macros that are defined here are not
    /// all of them".
    pub missing_context: bool,
}

impl FileEntry {
    /// Can conditions in this file be decided against the macros recorded here?
    ///
    /// `false` for a header analysed on its own: an `#ifdef` that looks false may be true in the translation
    /// unit that includes it, so a consumer must not drop what it guards — see [`FileEntry::missing_context`].
    pub fn context_is_complete(&self) -> bool {
        !self.missing_context
    }

    /// Is this file's own contents all the analysis had to go on?
    ///
    /// The state a consumer has to handle, and the reason it is a question about the *file* rather than about
    /// the walk: a header whose includer is in the walk is analysed with whatever included it, so its macros
    /// are the ones a compiler would use and no caveat is needed even though the walk elsewhere is partial.
    ///
    /// See [`crate::graph::file_only`] for the file-only analysis this describes.
    pub fn is_file_only(&self) -> bool {
        self.depth == 0 && self.missing_context
    }
}

/// The result of walking one translation unit.
#[derive(Debug, Clone, Default)]
pub struct FileGraph {
    /// Every file reached, indexed by `FileId`. A `FileId` the walk did not reach leaves a placeholder.
    pub files: Vec<FileEntry>,
    /// Every resolved include, in the order the walk reached it.
    pub edges: Vec<Edge>,
    /// Every include that could not be resolved.
    pub unresolved: Vec<UnresolvedInclude>,
    /// The files that were read, in the order they were entered.
    pub analysed: Vec<FileId>,
}

impl FileGraph {
    /// Where a file is, if the walk reached it.
    ///
    /// `None` for a file the interner knows but this walk never reached: the interner is shared and may
    /// have minted ids for other walks, so an id is not a promise that there is an entry.
    pub fn entry(&self, file: FileId) -> Option<&FileEntry> {
        self.files.get(file.index())
    }

    /// Where a file is, if it was read.
    pub fn path(&self, file: FileId) -> Option<&Path> {
        self.entry(file).map(|entry| entry.path.as_path())
    }

    /// The files that include `file`, directly.
    ///
    /// The reverse edge, and the reason the graph exists: an edit to a header invalidates exactly these, and
    /// transitively whatever includes them.
    pub fn includers_of(&self, file: FileId) -> Vec<FileId> {
        let mut includers: Vec<FileId> = self
            .edges
            .iter()
            .filter(|edge| edge.to == file)
            .map(|edge| edge.from)
            .collect();
        includers.sort_unstable();
        includers.dedup();
        includers
    }

    /// The files `file` includes, directly.
    pub fn includes_of(&self, file: FileId) -> Vec<FileId> {
        let mut includes: Vec<FileId> = self
            .edges
            .iter()
            .filter(|edge| edge.from == file)
            .map(|edge| edge.to)
            .collect();
        includes.sort_unstable();
        includes.dedup();
        includes
    }

    /// Every file whose analysis an edit to `file` could change, transitively — `file` included.
    ///
    /// The closure a cache invalidation needs. `file` itself is included because editing a file invalidates
    /// its own analysis, and the result is sorted so that a caller can compare two runs.
    pub fn dependents_of(&self, file: FileId) -> Vec<FileId> {
        let mut seen: HashSet<FileId> = HashSet::new();
        let mut pending = vec![file];

        while let Some(current) = pending.pop() {
            if !seen.insert(current) {
                continue;
            }
            pending.extend(self.includers_of(current));
        }

        let mut result: Vec<FileId> = seen.into_iter().collect();
        result.sort_unstable();
        result
    }

    /// The files this walk read.
    pub fn analysed_files(&self) -> &[FileId] {
        &self.analysed
    }

    /// The includes that did not resolve.
    pub fn unresolved_includes(&self) -> &[UnresolvedInclude] {
        &self.unresolved
    }
}

/// Where the walk has been.
///
/// The path and the visited set are separate questions and both are needed: a file on the path is a cycle
/// whatever its guard says, while a file merely visited is a cycle only if it is guarded.
#[derive(Debug, Default)]
struct WalkState {
    visited: HashSet<FileId>,
    on_path: Vec<FileId>,
    /// Whether the macro environment the walk started from was incomplete — see [`walk`].
    ///
    /// Part of the walk's state rather than of each call's arguments because it is a property of the whole
    /// walk and never changes once it starts: every file this walk reaches is reached from the same
    /// environment, so a header reached from a header opened on its own carries the same caveat.
    root_missing_context: bool,
    /// Whether includes are followed at all — see [`WalkScope`].
    follows_includes: bool,
}

impl WalkState {
    /// Must a file reached by this walk report an incomplete macro context?
    ///
    /// One place rather than a condition repeated at each entry constructed, because the two reasons are easy
    /// to get half-right: a header opened on its own has no includer to ask, and a file-only walk consulted
    /// nothing outside the file it was given. Both leave macros unaccounted for, and a consumer's behaviour
    /// depends on being told.
    fn missing_context(&self) -> bool {
        self.root_missing_context || !self.follows_includes
    }
}

/// How much of the project a walk is allowed to read.
///
/// The two answers are not degrees of the same thing; they are answers to different questions, and which one
/// is right depends on what the user is looking at.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WalkScope {
    /// Follow `#include`s and analyse every file reached, inheriting macros along the way.
    ///
    /// What a `.cpp` gets, and what a header gets once some translation unit that includes it is known: this
    /// is the analysis that can answer "which configuration is this declaration compiled under", because it
    /// is the only one that has the macros.
    #[default]
    FollowIncludes,
    /// Analyse **only** the file named, treating its `#include`s as edges to record and nothing more.
    ///
    /// The fallback for a header nothing includes, and the one thing that is always safe to do: everything
    /// decided here is decided from what the file says about itself, so nothing outside it can make the answer
    /// wrong. A header's own `#include`s are not read, so a macro **they** would have defined is unknown
    /// rather than absent — which is exactly the caveat [`FileEntry::missing_context`] records.
    ///
    /// It is a fallback and not a preference: a file analysed this way knows less than one analysed the other
    /// way, and its conditions are unknown where the other's are decided. What it buys is that the answer is
    /// never *wrong* — there is no configuration it can be contradicted by, because it never claims to know
    /// one.
    FileOnly,
}

impl WalkScope {
    /// Does this scope read the files a file includes?
    fn follows_includes(self) -> bool {
        matches!(self, WalkScope::FollowIncludes)
    }
}

/// Follow a translation unit's includes.
///
/// `source` and `tree` are the file the walk starts from, already parsed by the caller — it usually has
/// them, and parsing twice is the most expensive thing this could do.
///
/// # A header as the root
///
/// `root_path` is usually a `.cpp`, and then everything the walk decides is decided against a complete macro
/// environment: a translation unit really does start with the command line and nothing else. A **header** as
/// the root is the editor's case — the user opened the file, and no translation unit is being built — and
/// there the environment is genuinely incomplete, because the `-D`s and the `#define`s that come before the
/// header live in whichever translation unit includes it and cannot be guessed from the header.
///
/// The walk does not pretend otherwise. It records the file as [`FileEntry::missing_context`] and still
/// follows whatever its `#include`s resolve to, because *those* files are reached with the same incomplete
/// environment and a consumer asking about them wants the same caveat rather than silence.
///
/// [`walk_scoped`] is the same walk with the choice made explicitly; use [`file_only`] when the answer wanted
/// is about one file and nothing outside it should be read.
pub fn walk<F: FileProvider>(
    source: &str,
    tree: &CppSyntaxTree,
    root_path: &Path,
    files: &F,
    config: &CompilerConfig,
    interner: &mut PathInterner,
) -> FileGraph {
    walk_scoped(
        source,
        tree,
        root_path,
        files,
        config,
        interner,
        WalkScope::FollowIncludes,
    )
}

/// Follow a translation unit's includes, to the extent `scope` allows.
///
/// See [`WalkScope`] for why the choice exists. The root is analysed either way — it is the file the caller
/// handed over, so reading it is the one thing that was certainly asked for.
pub fn walk_scoped<F: FileProvider>(
    source: &str,
    tree: &CppSyntaxTree,
    root_path: &Path,
    files: &F,
    config: &CompilerConfig,
    interner: &mut PathInterner,
    scope: WalkScope,
) -> FileGraph {
    let mut graph = FileGraph::default();
    let mut state = WalkState {
        // A header analysed on its own is the case where the macro environment cannot be known: it is written
        // to be included, and what it is included *by* is what decides its conditions.
        root_missing_context: is_probably_a_header(root_path),
        follows_includes: scope.follows_includes(),
        ..WalkState::default()
    };

    // The starting state carries the same caveat, which is what makes it reach the decisions: a name the
    // command line and the header itself do not mention is *unknown* rather than undefined, so an `#ifdef`
    // on it neither guards dead code nor hides it.
    //
    // A file-only walk is incomplete for a second reason and it lands in the same place: nothing outside the
    // file was read, so a name the file does not mention may well have been defined by an `#include` above —
    // and "may well have been" is precisely what the state has to say.
    let mut starting = Marked::from_config(config);
    if state.missing_context() {
        starting = starting.incomplete();
    }

    let root = interner.intern(root_path);

    // The root is read here rather than through `follow`, because the caller already has it: parsing it again
    // would be the most expensive thing this function could do. It is still an analysed file, and recording it
    // as one is not bookkeeping — `analysed_files` is what a consumer walks to do per-file work, so a root
    // missing from it means a file that is silently never analysed.
    graph.analysed.push(root);

    visit(
        source, tree, root, &starting, &mut graph, &mut state, files, config, interner, 0,
    );

    graph
}

/// Analyse **one file** and nothing it includes.
///
/// The degraded analysis for a header whose translation unit is not known — see [`WalkScope::FileOnly`]. It
/// reads `source`, reads no other file, and reports what the file says about itself: its guard, the macros it
/// defines, and its `#include` edges, which are recorded as [`Visit::Skipped`] with
/// [`SkipReason::OutOfScope`] rather than resolved. The result is the same shape as [`walk`]'s, so a consumer can swap one for the other
/// without a second code path, and every entry it produces reports
/// [`FileEntry::context_is_complete`] as `false`.
pub fn file_only<F: FileProvider>(
    source: &str,
    tree: &CppSyntaxTree,
    path: &Path,
    files: &F,
    config: &CompilerConfig,
    interner: &mut PathInterner,
) -> FileGraph {
    walk_scoped(
        source,
        tree,
        path,
        files,
        config,
        interner,
        WalkScope::FileOnly,
    )
}

/// Does this path name a header, judged by its extension?
///
/// A guess, and deliberately a conservative one: getting it wrong for a `.hpp` costs a less confident but not
/// incorrect analysis, while inferring *from content* — "it has no `main`, so it must be a header" — would be
/// wrong in a way that is hard to notice. An unrecognised extension is treated as a translation unit, because
/// that is the reading that commits to the least.
fn is_probably_a_header(path: &Path) -> bool {
    let Some(extension) = path.extension().and_then(|extension| extension.to_str()) else {
        return false;
    };

    // Case-insensitively, because `Foo.H` is a header on the case-insensitive filesystems where it can exist
    // at all, and the cost of matching is nothing.
    matches!(
        extension.to_ascii_lowercase().as_str(),
        "h" | "hh" | "hpp" | "hxx" | "h++" | "inc" | "ipp" | "tpp" | "tcc" | "inl"
    )
}

#[allow(clippy::too_many_arguments)]
fn visit<F: FileProvider>(
    source: &str,
    tree: &CppSyntaxTree,
    file: FileId,
    inherited: &Marked,
    graph: &mut FileGraph,
    state: &mut WalkState,
    files: &F,
    config: &CompilerConfig,
    interner: &mut PathInterner,
    depth: usize,
) -> Marked {
    let root = tree.get_red_root();
    let preprocessing = crate::preprocess::preprocess(source, tree.get_tokens());
    let guard_analysis = analyse_guards(&preprocessing, &root);

    // What this file was reached with, recorded before anything in it changes the picture.
    let mut macros = inherited.clone();
    let inherited_names = macros.defined_names();

    let path = interner
        .path(file)
        .map(Path::to_path_buf)
        .unwrap_or_default();
    record(
        graph,
        file,
        FileEntry {
            path: path.clone(),
            guard: guard_analysis.guard.clone(),
            defines: guard_analysis.defines.clone(),
            depth,
            macros: inherited_names,
            missing_context: state.missing_context(),
        },
    );

    state.visited.insert(file);
    state.on_path.push(file);

    // **This file's compilation, not the project's.** A real project is several targets with different include
    // directories, and asking with the wrong one resolves another target's header — see CompilerConfig::for_file.
    let per_file = config.for_file(&path);
    let resolver = IncludeResolver::new(files, &per_file);
    let including = parent_normalized(&path, files.is_case_insensitive());

    // The directives are walked **in order**, because the order is the whole point: a `#define` is in force
    // for the includes after it and not for the ones before, which is what makes two includes of one header
    // see two different macro tables. Walking only the includes would lose that.
    //
    // The macro state is snapshotted before each directive, because a condition has to be decided against the
    // macros in force where it was *written* — see [`directive_visibilities`].
    //
    // Snapshotting first and deciding afterwards is what makes the two uses of the state consistent: the
    // visibility of a directive is decided against the states of the directives *before* it, and this file's
    // own walk then applies the same verdict to decide whether to apply the directive at all.
    let mut states: Vec<Marked> = Vec::with_capacity(preprocessing.directives.len());

    for _ in &preprocessing.directives {
        states.push(macros.clone());
    }

    let visibilities = directive_visibilities(&states, &preprocessing.directives);

    for (index, spanned) in preprocessing.directives.iter().enumerate() {
        // A define inside a region that is definitely not compiled never takes effect. Applying it anyway
        // would put a macro in force that a compiler never defines — the failure that makes a consumer believe
        // a branch is live when it is not. An undecidable region is applied, because a macro that *might* be
        // defined is closer to what a compiler sees than one that is certainly not.
        if visibilities[index] == Some(false) {
            continue;
        }

        match &spanned.directive {
            Directive::Define(define) => {
                if let Some(definition) = &define.macro_def {
                    macros.define(definition.clone());
                }
            }
            Directive::Undef { name: Some(name) } => macros.undefine(name),
            _ => {}
        }

        let Some(include) = spanned.directive.as_include() else {
            continue;
        };

        // `#include_next` searches from where *this* file was found, and the walk does not record that per
        // file — a header reached by two routes has two answers. Passing `None` makes `#include_next`
        // behave as an ordinary include: wrong for a wrapper header, right for every other use. Written
        // down rather than left implicit, because it is the one part of resolution the walk cannot do.
        let resolution = resolver.resolve(include, &including, None, interner);

        match resolution {
            Resolution::Unresolved(unresolved) => graph.unresolved.push(UnresolvedInclude {
                from: file,
                name: unresolved.name,
                searched: unresolved.searched,
                range: spanned.range,
            }),
            Resolution::Resolved(resolved) => {
                let visit = decide_visit(resolved.file, graph, state, depth);

                graph.edges.push(Edge {
                    from: file,
                    to: resolved.file,
                    name: include.target.clone(),
                    is_angle: include.form == IncludeForm::Angle,
                    found_in: resolved.found_in,
                    visit,
                    range: spanned.range,
                });

                if visit == Visit::Analysed {
                    // The snapshot is of *this* moment: the macros in force here, at this include, which is
                    // what the included file's conditions are decided by.
                    let reached_with = macros.clone();
                    let after = follow(
                        resolved.file,
                        &resolved.path,
                        &reached_with,
                        graph,
                        state,
                        files,
                        config,
                        interner,
                        depth + 1,
                    );

                    // Textual inclusion: whatever the included file defined is in force from here on. A
                    // guarded file that was skipped contributes nothing new — it was read earlier and its
                    // definitions are already in `macros` — which is why a skipped visit needs no case of
                    // its own.
                    if let Some(after) = after {
                        macros = after;
                    }
                }
            }
        }
    }

    // What this file leaves behind, for its includer: everything in force at its end. The result — not the
    // *difference* — because the includer replaces its state with it, and a file that undefined something
    // must be able to remove it again.
    //
    // This is also what makes the chain correct rather than merely plausible. Passing the ancestor's final
    // state down instead of this one is the obvious mistake, and it fails visibly: a header's own guard is
    // already in force by the time its includer reaches the next include, so a second header is treated as
    // having been seen and its body is skipped.
    state.on_path.pop();
    macros
}

/// The visibility of every directive, by position: `Some(true)` for one that a compiler compiles,
/// `Some(false)` for one inside a region it does not, and `None` for one that cannot be decided.
///
/// One entry per directive in the file, in file order and index-aligned with it, so a caller can ask about any
/// of them — including the ones that are not conditions at all, which is what the walk needs: a `#define` or an
/// `#include` is skipped exactly when it sits inside a region that is not compiled.
///
/// # Why each open region remembers where its arms start
///
/// Asking "did some arm of this region hold" is not the same question as "is the code here inside the arm that
/// is in force", and the two differ wherever a region has more than one. Under the first reading
/// `#if 1 ... #else ... #endif` is compiled throughout, so the `#else` arm's `#define`s are applied although a
/// compiler never reads them. The region therefore keeps its arms' *starting positions* as well as their
/// verdicts, and the code at a position belongs to the last arm that started at or before it.
///
/// Positions rather than indices-within-the-region, because the question is about the file: an arm's index
/// within its region says nothing about where in the file it is.
///
/// # Why the verdicts are decided against a snapshot
///
/// Each arm's condition is decided against the macros in force **where it was written** — `states[position]`,
/// captured by the walk as it passed. Asking at the end gives the opposite answer for the one pattern that
/// matters most:
///
/// ```text
/// #ifndef GUARDED_H     <- true here: nothing has defined it yet
/// #define GUARDED_H     <- and now it is defined
/// ...                   <- everything from here reads as inside a condition that is now false
/// #endif
/// ```
///
/// Decide the guard from the final state and every guarded header's body looks disabled — its includes
/// included, so they are never followed.
///
/// # Why `#endif` is the one directive that changes nothing
///
/// Every directive's visibility is decided by the regions open **before** it, and `#endif` is no exception
/// however strange that reads: the `#endif` line is inside the region it closes, and a region that is disabled
/// makes its own `#endif` part of the disabled text. Applying that uniformly is what lets this be one loop
/// with no special case — the region is popped as the `#endif` is *passed*, which is what puts the directive
/// after it outside.
fn directive_visibilities(states: &[Marked], directives: &[SpannedDirective]) -> Vec<Option<bool>> {
    let mut open: Vec<OpenRegion> = Vec::new();
    let mut visibilities: Vec<Option<bool>> = Vec::with_capacity(directives.len());

    for (position, spanned) in directives.iter().enumerate() {
        // At a directive that is itself a condition, the region's own verdict is about the arm it opens: an
        // `#else` sits *inside* the region, in the arm it introduces. `inside_active_arm` places it there
        // because it counts arms starting at or before the position, and this arm starts here.
        visibilities.push(inside_active_arm(&open, position));

        // Every kind of condition is resolved here, `#endif` included, so that one branch of the match cannot
        // advance the state differently from another.
        let Some(branch) = branches_at(spanned) else {
            continue;
        };

        // The macros in force where this branch was written, which is what its expression is about.
        let state = states.get(position).unwrap_or_else(|| &states[0]);
        let holds = branch.decide(state);

        match branch.kind {
            DirectiveKind::If | DirectiveKind::Ifdef | DirectiveKind::Ifndef => {
                open.push(OpenRegion {
                    starts: vec![position],
                    holds: vec![holds],
                    active: if holds == Some(true) { Some(0) } else { None },
                })
            }
            DirectiveKind::Elif | DirectiveKind::Else => {
                if let Some(region) = open.last_mut() {
                    // An arm is eligible only when **every** arm before it is false. An `#else` tests nothing,
                    // so its own verdict says nothing about whether it is the arm in force — letting it
                    // activate itself from that verdict is what makes both halves of an `#if`/`#else` look
                    // compiled at once.
                    let eligible = region.holds.iter().all(|holds| *holds == Some(false));

                    region.starts.push(position);
                    region.holds.push(holds);
                    let this = region.holds.len() - 1;

                    if eligible && holds != Some(false) {
                        region.active = Some(this);
                    }
                }
            }
            DirectiveKind::Endif => {
                open.pop();
            }
            _ => {}
        }
    }

    visibilities
}

/// The conditional branch a directive introduces, if it introduces one.
fn branches_at(spanned: &SpannedDirective) -> Option<BranchAt> {
    let kind = spanned.directive.kind();
    if !(kind.opens_a_condition() || kind.closes_a_condition()) {
        return None;
    }

    let (tokens, name) = match &spanned.directive {
        Directive::Conditional { condition, .. } => (condition.clone(), None),
        Directive::Ifdef { name, .. } => (Vec::new(), Some(name.clone())),
        _ => (Vec::new(), None),
    };

    Some(BranchAt { kind, tokens, name })
}

/// Is the directive at `position` inside the arm in force of every open region?
///
/// `None` when some region could still take a later arm, which is the honest answer for a directive under an
/// `#if` on a macro the walk cannot value: it is compiled under some configurations and not others. Only a
/// region whose arms are all definitely false is a confident `Some(false)`.
fn inside_active_arm(open: &[OpenRegion], position: usize) -> Option<bool> {
    let mut undecided = false;

    for region in open {
        // The arm this directive falls in is the last one that started at or before it.
        let Some(current) = region.starts.iter().rposition(|start| *start <= position) else {
            continue;
        };

        match region.active {
            // Inside the arm in force: this region is no obstacle.
            Some(active) if active == current => continue,
            // Inside an arm that is definitely not the one in force — either a later one, or any arm of a
            // region where nothing holds at all.
            Some(_) => return Some(false),
            // No arm holds yet. An undecidable condition could still take a later arm, so this is not a
            // confident "no".
            None => {
                if region.holds.iter().all(|holds| *holds == Some(false)) {
                    return Some(false);
                }
                undecided = true;
            }
        }
    }

    if undecided { None } else { Some(true) }
}

/// A conditional region the walk is inside, with the arm that is in force.
#[derive(Debug)]
struct OpenRegion {
    /// Where each arm starts, by directive position.
    starts: Vec<usize>,
    /// Each arm's own verdict, in the same order: `Some(true)` when its condition held where it was written.
    holds: Vec<Option<bool>>,
    /// Index into the two above of the arm in force, or `None` when none holds yet.
    active: Option<usize>,
}

/// A conditional branch, remembered with what it tests.
///
/// The guard layer's [`crate::guard::Branch`] holds tokens and answers `holds` against a table; this holds the
/// same information in a form that can be decided later, against a *state* that is captured separately. The
/// split exists because the two are needed at different times: which branches exist is known from a file's
/// text, and what they mean is known only during the walk.
#[derive(Debug, Clone)]
pub struct BranchAt {
    pub kind: DirectiveKind,
    /// The expression's tokens, for `#if` and `#elif`.
    pub tokens: Vec<crate::token::Token>,
    /// The name tested, for `#ifdef` and `#ifndef`.
    pub name: Option<Box<str>>,
}

impl BranchAt {
    /// Does this branch's own expression hold under `state`?
    ///
    /// `None` when it cannot be decided, which is what makes the enclosing region `Unknown` rather than
    /// inactive. An `#else` has no expression and always holds: whether the region takes it is a question
    /// about the branches before it, which the region in force answers.
    ///
    /// `#ifdef` and `#ifndef` are where [`Marked::is_unknown`] earns its keep. A name the state does not
    /// mention is normally undefined, so `#ifdef` is false. In a header analysed on its own it may well have
    /// been defined by the includer, and answering `false` there would call the guarded code dead — the
    /// answer that makes a consumer report errors in code that compiles. Answering `None` instead says the
    /// only thing that is certainly true: this configuration does not decide it.
    pub fn decide(&self, state: &Marked) -> Option<bool> {
        match self.kind {
            DirectiveKind::If | DirectiveKind::Elif => {
                crate::condition::evaluate(&self.tokens, state).is_true()
            }
            DirectiveKind::Ifdef => self.named_status(state),
            DirectiveKind::Ifndef => self.named_status(state).map(|defined| !defined),
            DirectiveKind::Else => Some(true),
            _ => None,
        }
    }

    /// Whether the `#ifdef`/`#ifndef` name is defined, or `None` when this state cannot say.
    ///
    /// A directive that names nothing — `#ifdef` with no operand, which malformed source produces — reports
    /// the name as undefined rather than as unknown: there is nothing to be uncertain about.
    fn named_status(&self, state: &Marked) -> Option<bool> {
        let name = self.name.as_deref()?;

        if state.is_unknown(name) {
            return None;
        }

        Some(state.is_defined(name))
    }
}

/// What a visit to `file` should do, given where the walk has been.
fn decide_visit(file: FileId, graph: &FileGraph, state: &WalkState, depth: usize) -> Visit {
    // Asked for one file only. The include is still *recorded* — it is a fact about the file that was read,
    // and a consumer wants it — but reading the other end was not part of the question, so the reason says
    // that rather than pretending to be one of the facts about the code.
    if !state.follows_includes {
        return Visit::Skipped(SkipReason::OutOfScope);
    }

    if state.on_path.contains(&file) {
        return Visit::Skipped(SkipReason::Cycle);
    }

    if depth + 1 > MAX_INCLUDE_DEPTH {
        return Visit::Skipped(SkipReason::TooDeep);
    }

    // A guarded file already read is what the guard is for. An *unguarded* one is read again, because a
    // compiler reads it again — and a consumer that did not would miss the duplicate definitions that are
    // the reason guards get written in the first place.
    let guarded = graph
        .entry(file)
        .is_some_and(|entry| entry.guard.is_guarded());

    if guarded && state.visited.contains(&file) {
        return Visit::Skipped(SkipReason::AlreadyVisited);
    }

    Visit::Analysed
}

/// Read a file and visit it, returning the macros it left in force.
///
/// `None` when the file could not be read, which is not the same as "it left nothing behind": an unreadable
/// file is one whose contents are unknown, so its includer's macro state is left as it was rather than
/// replaced by an empty one.
#[allow(clippy::too_many_arguments)]
fn follow<F: FileProvider>(
    file: FileId,
    path: &Path,
    inherited: &Marked,
    graph: &mut FileGraph,
    state: &mut WalkState,
    files: &F,
    config: &CompilerConfig,
    interner: &mut PathInterner,
    depth: usize,
) -> Option<Marked> {
    // A file that resolved but cannot be read is a file that exists to `exists` and not to `read`: a
    // directory, a permission problem, or a buffer closed between the two calls. Recorded so that a consumer
    // asking about it does not get `None` and conclude it was never reached.
    let Some(source) = files.read(path) else {
        record(
            graph,
            file,
            FileEntry {
                path: path.to_path_buf(),
                guard: Guard::None,
                defines: Vec::new(),
                depth,
                macros: inherited.defined_names(),
                // An unreadable file is not a file whose context is *known*: whatever it might have defined is
                // unknown along with the rest of it.
                missing_context: state.missing_context(),
            },
        );
        return None;
    };

    graph.analysed.push(file);
    let tree = cpp_parser::CppParser::parse(&source, cpp_parser::ParserConfig::default());

    Some(visit(
        &source, &tree, file, inherited, graph, state, files, config, interner, depth,
    ))
}

/// Store an entry at the id's index, growing the table with placeholders for ids in between.
fn record(graph: &mut FileGraph, file: FileId, entry: FileEntry) {
    while graph.files.len() <= file.index() {
        graph.files.push(FileEntry {
            path: PathBuf::new(),
            guard: Guard::None,
            defines: Vec::new(),
            depth: usize::MAX,
            macros: Vec::new(),
            // A placeholder is not a file, so there is nothing here to have context about. `false` keeps a
            // consumer from reading `context_is_complete` as a statement about a file this walk never reached.
            missing_context: false,
        });
    }

    graph.files[file.index()] = entry;
}

/// The macro names in force at a point in a walk.
///
/// Names, not definitions, and deliberately so. Two questions need answering at this stage — "is `FOO`
/// defined" for an `#ifdef`, and "start this file off with these names" for the chain — and both are about
/// names. A macro's *value* matters once expansion runs, and that is [`crate::expand::expand`]'s job with a real
/// [`crate::macros::MacroTable`]; carrying values here would duplicate the table without the ordering rules
/// that make it correct.
///
/// The command line is the exception: `-DFOO` and `-DFOO=1` are parsed into definitions rather than names,
/// because that is where they come from and because dropping them would make every `#ifdef FOO` in a
/// configured project answer the wrong way.
#[derive(Debug, Clone, Default)]
pub struct Marked {
    /// Bindings in order, each name either present or explicitly undefined. Later binds shadow earlier ones,
    /// which is what makes `#undef` and redefinition work without deleting history.
    bindings: Vec<(Box<str>, bool)>,
    /// **Every name `bindings` has ever held, for membership only.** The same names as `bindings`, in no order —
    /// an index, not a second copy of the state.
    ///
    /// It exists because `bindings` is a `Vec` and the questions asked of it are membership questions: `mentions`,
    /// `is_defined` and `get` each scanned it end to end, once per identifier of every condition of every guard. The
    /// `Vec` grows with the compiler's own predefines — a thousand entries for MSVC with a configuration — so every
    /// `#ifdef` in every file was a thousand string comparisons. Measured: `a_guard_holds` was 165 s of a 391 s
    /// index of `EmmyLuaCodeStyle` after the trace-variable fix, and it is one of the last things on that path.
    ///
    /// The `Vec` itself stays: order is what decides shadowing, and the reverse scan in `is_defined` is over a
    /// handful of entries once this has said the name is there at all.
    mentioned: std::collections::HashSet<Box<str>>,
    /// Definitions from the command line, kept so that a lookup can answer about a *value* as well as a name.
    command_line: Vec<crate::macros::MacroDef>,
    /// Whether macros this state does not mention might still have been in force.
    ///
    /// A state cannot tell "nothing defined this" apart from "this never saw the file that did", and for a
    /// header those are different answers. With this set, a name with no binding is **unknown** rather than
    /// undefined — which is what keeps an `#ifdef` from a header's includer going the wrong way.
    incomplete: bool,
    /// Names this state passed **without being able to decide** what they are: a `#define` inside an `#if`
    /// nobody can evaluate, or any fact of a file that may not have been included at all.
    ///
    /// Kept apart from [`Marked::incomplete`] because the two are different sizes of the same doubt: that one
    /// says "a file I never read may define anything", this one says "**this** name may or may not be defined,
    /// and I know which fact put it in doubt". A condition asking about such a name is `Unknown` — which is what
    /// keeps `#ifndef NAME / #define NAME` in an undecidable branch from being read as "NAME is not defined",
    /// the reading that would make the rest of the file's conditions wrong rather than unknown.
    uncertain: HashSet<Box<str>>,
}

/// **Equality is over the state, not over the index.** `mentioned` holds exactly the names `bindings` holds, so two
/// states that differ in it have already differed in `bindings` — comparing it would be comparing a fact twice, and
/// a caller that builds the same state two ways would see two unequal states for no reason a reader could act on.
impl PartialEq for Marked {
    fn eq(&self, other: &Self) -> bool {
        self.bindings == other.bindings
            && self.command_line == other.command_line
            && self.incomplete == other.incomplete
            && self.uncertain == other.uncertain
    }
}

impl Eq for Marked {}

impl crate::condition::MacroValues for Marked {
    /// The definition behind a name, for evaluating a condition.
    ///
    /// Only names that came from the command line have one here: `-DFOO=2` is a definition this layer read, so
    /// `#if FOO == 2` can be decided. A name a `#define` introduced in a file the walk has passed through has
    /// no body in this state, so `#ifdef FOO` is answered correctly by [`Marked::is_defined`] while a *value*
    /// question answers `Unknown` — which [`crate::guard`] already reports as undecidable rather than false.
    ///
    /// That gap is deliberate and bounded: carrying every body would mean duplicating the macro table without
    /// its ordering rules, and what needs deciding at this stage is whether a region is `#if 0`d out.
    ///
    /// A name this state has not seen in an [`Marked::incomplete`] walk is
    /// [`Lookup::Unanswered`](crate::Lookup::Unanswered) rather than
    /// [`Lookup::Undefined`](crate::Lookup::Undefined), which is the same distinction [`Marked::is_unknown`] makes
    /// for `#ifdef` — and it has to be made here too, or `#if defined(FOO)` in a header analysed on its own would
    /// answer "no" about a macro its includer defines.
    fn lookup(&self, name: &str) -> crate::condition::Lookup<'_> {
        use crate::condition::Lookup;

        // **A builtin operator is defined**, which is what the implementation says about its own predefined macros:
        // `defined(__has_cpp_attribute)` is true on clang, gcc and MSVC alike. See
        // [`crate::preprocess::guard`], where the same answer is given to the cook's `#ifdef` — the two paths have to
        // agree, and this is the one the **include walk** uses.
        //
        // Missing it here was enough to defeat the whole `_HAS_MSVC_ATTRIBUTE` chain: `yvals_core.h:655` writes
        // `#ifndef __has_cpp_attribute`, we answered "not defined", and the header then defined
        // `_HAS_MSVC_ATTRIBUTE(x)` as the literal `0` — so `_NO_SPECIALIZATIONS_MSG` was never defined and
        // `[[msvc::no_specializations(...)]]` vanished from `xtr1common:28`.
        if crate::preprocess::condition::is_a_builtin_operator(name) {
            return Lookup::DefinedWithoutAValue;
        }

        if self.is_unknown(name) {
            return Lookup::Unanswered;
        }

        if !self.is_defined(name) {
            return Lookup::Undefined;
        }

        self.get(name)
            .map_or(Lookup::DefinedWithoutAValue, Lookup::Defined)
    }

    /// [`crate::condition::MacroValues::builtin_operator`] for the **walk's** state, so that
    /// `#if __has_cpp_attribute(msvc::no_specializations)` is decided here the way the cook decides it.
    ///
    /// Without this the walk and the cook would answer the same question differently, which is the defect this file
    /// has already been fixed for once (see `UnitMacros::lookup`): a condition has one answer, and both layers have
    /// to reach it.
    fn builtin_operator(&self, name: &str, operand: &str) -> Option<crate::condition::Value> {
        if name == "__has_cpp_attribute" {
            let msvc = self.is_defined("_MSC_VER") && !self.is_defined("__clang__");
            return crate::preprocess::cooked::attribute_support_in(operand, msvc)
                .map(crate::condition::Value::Known);
        }

        None
    }
}

impl Marked {
    /// A state that knows only what it has seen, for a file whose includers are not part of the analysis.
    ///
    /// See [`FileEntry::missing_context`] for why this is the honest reading rather than the pessimistic one.
    pub fn incomplete(mut self) -> Self {
        self.incomplete = true;
        self
    }

    /// The same, for a walk that has just found out it cannot know everything: an `#include` that did not
    /// resolve, or a file nobody indexed.
    ///
    /// Once set it cannot be unset, and that is the point: what the walk did not read stays unread, and every
    /// condition after the gap is a question about a translation unit this state only knows part of.
    pub fn mark_incomplete(&mut self) {
        self.incomplete = true;
    }

    pub fn is_incomplete(&self) -> bool {
        self.incomplete
    }

    /// Note that `name` **may or may not** be defined here, without saying which.
    ///
    /// What a walk does with a fact it could not place: a `#define NAME 1` inside an `#if` nobody can evaluate,
    /// or anything at all in a file that may not have been included. From that point on the name is
    /// [`Lookup::Unanswered`](crate::Lookup::Unanswered) — not `0`, and not "defined".
    pub fn mark_uncertain(&mut self, name: &str) {
        if !self.uncertain.iter().any(|held| &**held == name) {
            self.uncertain.insert(name.into());
        }
    }

    /// Is `name`'s status unknown — neither defined nor definitely not?
    ///
    /// Two ways to be unknown, and both are asked about here: the state is [`Marked::incomplete`] and nothing in
    /// it mentions the name (a file this walk never read may define it), or a fact the walk passed left the name
    /// [`Marked::mark_uncertain`]. A name that was explicitly `#undef`ed is *not* unknown: the file that
    /// undefined it is one this state has seen, so the answer is a definite no.
    pub fn is_unknown(&self, name: &str) -> bool {
        self.is_uncertain(name) || (self.incomplete && !self.mentions(name))
    }

    /// Did anything in this state mention the name, one way or the other?
    fn mentions(&self, name: &str) -> bool {
        self.mentioned.contains(name)
    }

    /// Is the name one this state passed without being able to decide?
    pub fn is_uncertain(&self, name: &str) -> bool {
        self.uncertain.iter().any(|held| &**held == name)
    }

    /// The state a translation unit starts in: the compiler's own definitions.
    pub fn from_config(config: &CompilerConfig) -> Self {
        let mut marked = Marked::default();

        for definition in &config.defines {
            marked.define_on_the_command_line(&definition.name, definition.value.as_deref());
        }

        for name in &config.undefines {
            marked.bindings.push((name.clone(), false));
            marked.mentioned.insert(name.clone());
        }

        marked
    }

    /// Define one name the way a command line does: `-DNAME=value`, or `-DNAME` for `NAME` as `1`.
    ///
    /// The two spellings are re-parsed through the ordinary `#define` reader so that they cannot drift apart
    /// from what a file writing the same line means — and so that a *value* is available to a condition
    /// (`#if FOO == 2`) rather than only the name.
    ///
    /// Public because the command line is not the only source of definitions a compilation starts with: a
    /// compiler **predefines** about five hundred names (`_WIN32`, `__x86_64__`, `__cplusplus`) that no file
    /// and no `-D` wrote, and they arrive in the same shape. See `Toolchain::macros`.
    pub fn define_on_the_command_line(&mut self, name: &str, value: Option<&str>) {
        let source = match value {
            Some(value) => format!("#define {name} {value}\n"),
            None => format!("#define {name} 1\n"),
        };

        let Some(parsed) = parse_define_source(&source) else {
            return;
        };

        self.bindings.push((parsed.name.clone(), true));
        self.mentioned.insert(parsed.name.clone());
        self.command_line
            .retain(|existing| existing.name != parsed.name);
        self.command_line.push(parsed);
    }

    pub fn define(&mut self, definition: crate::macros::MacroDef) {
        self.bindings.push((definition.name.clone(), true));
        self.mentioned.insert(definition.name.clone());
        self.command_line
            .retain(|existing| existing.name != definition.name);
        self.command_line.push(definition);
    }

    /// Account for one `#define` or `#undef` a walk has just passed.
    ///
    /// The translation unit's macro state is built by *reading it in order*, which is what this is for: the walk
    /// calls it for every fact of every file it enters, and the state after the call is what a condition written
    /// below that fact sees. See [`crate::index::environment`] — until this existed the environment was only what
    /// the compiler and the command line define, and every condition a header's own feature macro answers stayed
    /// `Unknown`.
    ///
    /// The value comes from [`crate::MacroFact::value`], which is the macro's body only when that body is one
    /// integer literal — the one kind of body a condition can read. A fact without one defines the name and leaves
    /// its value unknown, which is the same answer a condition gets from a `#define NAME something` it must expand
    /// itself: `defined(NAME)` is true and `#if NAME` is undecidable.
    pub fn observe(&mut self, fact: &crate::MacroFact) {
        match fact.kind {
            crate::MacroKind::Undefinition => self.undefine(&fact.name),
            crate::MacroKind::Definition => match fact.value.as_deref() {
                // The literal's own text, with the token kind the reader of a *value* accepts. No lexer is needed
                // for that: `MacroFact::value` is written only when the body is one integer literal, so the text
                // and the kind are both already known — which is why they are stored that way.
                Some(value) => self.define(crate::macros::MacroDef {
                    name: fact.name.as_str().into(),
                    params: None,
                    body: crate::macros::MacroBody {
                        tokens: vec![crate::Token::new(
                            cpp_parser::CppTokenKind::IntegerLiteral,
                            value,
                            fact.range,
                        )],
                        stringize: Vec::new(),
                        paste: Vec::new(),
                    },
                    range: fact.range,
                    name_range: fact.range,
                    // A definition a **summary** describes: the fact has a range in the file the summary is of,
                    // and the caller that asked for this state is walking that file — see [`MacroFile::Here`].
                    // (What is *not* here is the file's identity: a `Marked` is one file's state, and the ranges
                    // are positions in it.)
                    written_in: Some(crate::macros::MacroFile::Here),
                    value: std::sync::OnceLock::new(),
                }),
                // **The name the body aliased, when the body *is* one name** — `#define _STL_LANG __cplusplus`.
                //
                // The third shape a condition can read, beside a literal and nothing at all, and the one that
                // decides MSVC's whole language mode. `vcruntime.h` writes `_STL_LANG` as an alias of either
                // `_MSVC_LANG` or `__cplusplus`, and then asks `#if _STL_LANG > 201402L` to set `_HAS_CXX17`. With
                // this arm missing, `_STL_LANG` was a name with no body, the comparison was undecidable,
                // `_HAS_CXX17` was never defined, and every `#if _HAS_CXX17` below it — including the one around
                // `#include <atomic>` in `<memory>` — was undecidable too.
                //
                // The body is the alias as an **identifier token**, which is what the expander walks: it looks the
                // name up in this same state and substitutes that definition, so `_STL_LANG` → `__cplusplus` →
                // `202400L` resolves in one step per link, exactly as a preprocessor would.
                None => match fact.alias.as_deref() {
                    Some(alias) => self.define(crate::macros::MacroDef {
                        name: fact.name.as_str().into(),
                        params: None,
                        body: crate::macros::MacroBody {
                            tokens: vec![crate::Token::new(
                                cpp_parser::CppTokenKind::Identifier,
                                alias,
                                fact.range,
                            )],
                            stringize: Vec::new(),
                            paste: Vec::new(),
                        },
                        range: fact.range,
                        name_range: fact.range,
                        written_in: Some(crate::macros::MacroFile::Here),
                        value: std::sync::OnceLock::new(),
                    }),
                    None => self.define_name(&fact.name),
                },
            },
        }
    }

    /// Define a name whose **value is not known** — a `#define NAME` a file wrote whose body this state does not
    /// carry, or a name a conditional certainly defines without saying as what.
    ///
    /// Any value the name had is **forgotten**, and that is not tidiness: a definition that shadows an earlier one
    /// may define the name as anything, so keeping the old value would answer `#if NAME == 2` with the value of a
    /// definition that is no longer the one in force.
    pub fn define_name(&mut self, name: &str) {
        self.bindings.push((name.into(), true));
        self.mentioned.insert(name.into());
        self.command_line.retain(|existing| &*existing.name != name);
    }
    pub fn undefine(&mut self, name: &str) {
        self.bindings.push((name.into(), false));
        self.mentioned.insert(name.into());
        self.command_line.retain(|existing| &*existing.name != name);
    }

    pub fn is_defined(&self, name: &str) -> bool {
        // **The index answers "is it there at all" first, and it is the common answer that is *no*.** A condition
        // asks about names the compiler never heard of — `__cplusplus`, a project's own feature macro — so the scan
        // below used to run to the end of the `Vec` for every one of them, against the compiler's own thousand
        // predefines. With the index that case is one hash, and the reverse scan only happens for a name that is
        // really in the list, where it stops at the binding in force.
        if !self.mentioned.contains(name) {
            return false;
        }
        self.bindings
            .iter()
            .rev()
            .find(|(bound, _)| &**bound == name)
            .is_some_and(|(_, defined)| *defined)
    }

    pub fn get(&self, name: &str) -> Option<&crate::macros::MacroDef> {
        if !self.is_defined(name) {
            return None;
        }

        self.command_line
            .iter()
            .rev()
            .find(|definition| &*definition.name == name)
    }

    /// Every name currently defined, sorted and deduplicated — a name that was undefined must not appear,
    /// and one that was redefined must not appear twice.
    ///
    /// The shadowing has to be resolved **before** the sort, not after: a name bound twice appears as two
    /// entries in `bindings`, and only the last one is in force. Filtering for `defined` first and deduping
    /// afterwards keeps the *earlier* binding and throws the later one away, which is exactly backwards — it
    /// makes `#undef FOO` invisible and a definition that has been removed look as if it is still in force.
    pub fn defined_names(&self) -> Vec<Box<str>> {
        let mut seen: HashSet<&str> = HashSet::new();
        let mut names: Vec<Box<str>> = Vec::new();

        for (name, defined) in self.bindings.iter().rev() {
            if !seen.insert(name) {
                continue;
            }

            if *defined {
                names.push(name.clone());
            }
        }

        names.sort_unstable();
        names
    }

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

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Read one `#define` from a source string, the way a directive's tokens are read.
fn parse_define_source(source: &str) -> Option<crate::macros::MacroDef> {
    let tree = cpp_parser::CppParser::parse(source, cpp_parser::ParserConfig::default());
    let preprocessing = crate::preprocess::preprocess(source, tree.get_tokens());
    preprocessing.macros.iter().next().cloned()
}

#[cfg(test)]
mod tests {
    use super::{CompilerConfig, Marked, Path, is_probably_a_header};

    /// A name bound twice is in force according to its **last** binding, and `defined_names` has to resolve
    /// that before it dedupes.
    ///
    /// Getting the order wrong is a silently wrong answer rather than a crash: excluding the later binding and
    /// keeping the earlier one makes `#undef FOO` invisible, so a macro that has been removed still looks
    /// defined to everything downstream — including an `#ifdef` in a header the walk has not reached yet, and
    /// so including the macros that header contributes.
    #[test]
    fn a_later_binding_shadows_an_earlier_one() {
        let mut marked = Marked::default();
        marked.define_name("REDEFINED");
        marked.define_name("REMOVED");
        marked.define_name("REDEFINED");
        marked.undefine("REMOVED");

        assert_eq!(
            marked
                .defined_names()
                .iter()
                .map(|n| &**n)
                .collect::<Vec<_>>(),
            vec!["REDEFINED"],
            "the later bindings are the ones in force"
        );

        // A name defined, removed, and defined again is defined: the resurrection is a later binding too.
        let mut marked = Marked::default();
        marked.define_name("BACK");
        marked.undefine("BACK");
        marked.define_name("BACK");

        assert_eq!(
            marked
                .defined_names()
                .iter()
                .map(|n| &**n)
                .collect::<Vec<_>>(),
            vec!["BACK"]
        );
    }

    /// A name defined twice is reported once, and the result is sorted so that two walks over the same file
    /// cannot produce two different answers to "what is in force here".
    #[test]
    fn defined_names_is_sorted_and_deduplicated() {
        let mut marked = Marked::default();
        marked.define_name("ZED");
        marked.define_name("ALPHA");
        marked.define_name("ZED");
        marked.define_name("MID");

        assert_eq!(
            marked
                .defined_names()
                .iter()
                .map(|n| &**n)
                .collect::<Vec<_>>(),
            vec!["ALPHA", "MID", "ZED"]
        );
        assert_eq!(marked.len(), 3);
    }

    /// An empty state defines nothing, which is what makes `is_empty` usable as "no macros are in force".
    #[test]
    fn an_empty_state_has_no_names() {
        let marked = Marked::default();
        assert!(marked.is_empty());
        assert!(marked.defined_names().is_empty());
        assert!(!marked.is_defined("ANYTHING"));
    }

    /// In a complete state, a name nothing mentions is **undefined** — not unknown.
    ///
    /// This is the answer that decides `#if 0`-style pruning for a translation unit, and weakening it to
    /// "unknown" would make every `#ifdef` in a configured project undecidable — which reports dead code as
    /// live, and is precisely the false positive the whole design is arranged to avoid.
    #[test]
    fn a_complete_state_reports_an_unmentioned_name_as_undefined() {
        let marked = Marked::from_config(&CompilerConfig::new());

        assert!(!marked.is_unknown("ANYTHING"));
        assert!(!marked.is_defined("ANYTHING"));
    }

    /// An incomplete state reports a name nothing mentions as **unknown**, because the file that would have
    /// defined it may not be part of the analysis.
    ///
    /// A name the state *has* seen is not unknown, and the distinction is what makes `#undef` still mean
    /// something in a header: the header undefined it itself, so the answer is a definite no rather than an
    /// open question.
    #[test]
    fn an_incomplete_state_reports_an_unmentioned_name_as_unknown() {
        let marked = Marked::default().incomplete();

        assert!(marked.is_unknown("FROM_THE_INCLUDER"));
        assert!(!marked.is_defined("FROM_THE_INCLUDER"));

        let mut seen = Marked::default().incomplete();
        seen.define_name("DEFINED_HERE");
        seen.undefine("UNDEFINED_HERE");

        assert!(!seen.is_unknown("DEFINED_HERE"));
        assert!(!seen.is_unknown("UNDEFINED_HERE"));
        assert!(seen.is_defined("DEFINED_HERE"));
        assert!(!seen.is_defined("UNDEFINED_HERE"));
    }

    /// The header guess is made from the extension, and an unknown extension is treated as a translation
    /// unit: that is the reading which commits to the least, since it claims no caveat it cannot support.
    #[test]
    fn a_header_is_recognised_by_its_extension() {
        for header in [
            "a.h",
            "a.hh",
            "a.hpp",
            "a.hxx",
            "a.h++",
            "a.inc",
            "a.ipp",
            "a.tpp",
            "a.tcc",
            "a.inl",
            "A.H",
            "dir/sub/a.hpp",
        ] {
            assert!(
                is_probably_a_header(Path::new(header)),
                "{header} is a header"
            );
        }

        for source in [
            "a.cpp",
            "a.cc",
            "a.cxx",
            "a.c",
            "a.c++",
            "a.m",
            "a.mm",
            "a",
            "a.hpp.cpp",
            "a.txt",
            "Makefile",
        ] {
            assert!(
                !is_probably_a_header(Path::new(source)),
                "{source} is not a header"
            );
        }
    }
}