lang-parsing-substrate 0.17.3

Shared language-parsing substrate for knots, moldy, aurora-lint, and clew
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
//! Dead-code region detection for C#'s `#if`/`#elif`/`#else`/`#endif`
//! conditional compilation.
//!
//! Tree-sitter-based, like [`crate::dead_code_swift`] and unlike
//! [`crate::dead_code`]'s C/C++ line scanner — but structurally closer to
//! C/C++'s *problem shape* than to Swift's. Verified directly against
//! tree-sitter-c-sharp 0.23's grammar and a parse dump (not assumed):
//! `preproc_if`/`preproc_elif` are real nested nodes with a `condition`
//! field and an `alternative` field (chaining to the next `preproc_elif` or
//! a trailing `preproc_else`), and the guarded content is nested as
//! ordinary children *underneath* them — not spliced in as flat siblings
//! the way Swift's `directive` nodes are. That's possible because C#, like
//! Swift, requires each branch's content to be syntactically complete
//! (`repeat($.declaration)`-shaped in the grammar), so there's no
//! `extern "C" {`-style brace-imbalance failure mode to work around; a
//! proper AST walk following `condition`/`alternative` fields is
//! straightforward and doesn't need Swift's flat-sibling depth-tracking
//! trick either, since nesting is real here.
//!
//! Unlike Swift, C# *does* have `#define`/`#undef` — real top-level nodes
//! (confirmed: `preproc_define` appears as a direct child of
//! `compilation_unit`, sibling to `using_directive`/`class_declaration`),
//! so locally-provable symbol definedness ports over: a symbol
//! `#define`d on every path to a condition is always-defined there, and one
//! `#undef`d on every path is never-defined. The evidence follows the paths
//! through each conditional, as in [`crate::dead_code`]: after `#endif` a
//! symbol keeps a value only if every branch that may compile, and the path
//! that skips the conditional when it has no `#else`, agree on it. A branch
//! of `#if FOO` or `#if !FOO` also knows `FOO` from the test, so `#if !FOO` /
//! `#define FOO` / `#endif` leaves `FOO` defined on both paths. A dead
//! branch's `#define` counts on no path. A commented-out `#define` is no
//! evidence, as in [`crate::dead_code`]: the build can still define the
//! symbol (`-define:`, `DefineConstants`).
//!
//! C# also has no `#ifdef`/`#ifndef` — only `#if`/`#elif` with a boolean
//! condition expression (`identifier`, `true`/`false` literals, `!`, `&&`,
//! `||`, `==`, `!=`, and parens; confirmed against the grammar), so
//! `!SYMBOL` covers what C spells `#ifndef SYMBOL`. Because each operand of
//! `&&`/`||` is a real sub-tree (not a flat token stream like Swift's),
//! short-circuit evaluation falls out for free: `false && SYMBOL` is
//! provably dead even though `SYMBOL` alone isn't classifiable.

use std::collections::{HashMap, HashSet};

use tree_sitter::{Node, Parser};

/// A C# preprocessor-dead region: an `#if`/`#elif`/`#else` branch proven
/// unreachable by a compile-time-constant condition or locally-provable
/// symbol definedness. See the module documentation for exactly what is and
/// isn't recognized.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CSharpDeadCodeRegion {
    /// 1-based, inclusive first line of the dead region.
    pub start_line: usize,
    /// 1-based, inclusive last line of the dead region.
    pub end_line: usize,
}

/// Computes the dead-code regions of `source`, a C# file. See the module
/// documentation for exactly what is and isn't recognized.
pub fn csharp_dead_code_regions(source: &[u8]) -> Vec<CSharpDeadCodeRegion> {
    let mut parser = Parser::new();
    if parser
        .set_language(&tree_sitter_c_sharp::LANGUAGE.into())
        .is_err()
    {
        return Vec::new();
    }
    let Some(tree) = parser.parse(source, None) else {
        return Vec::new();
    };

    let mut defined = Symbols::default();
    let mut regions = Vec::new();
    walk(tree.root_node(), source, &mut defined, &mut regions);
    regions
}

/// What the file itself proves about each symbol at the current point of
/// the walk, `true` defined and `false` undefined (no entry: no evidence),
/// with an undo log so a branch can be walked in place and rolled back.
#[derive(Default)]
struct Symbols {
    values: HashMap<String, bool>,
    /// Each write since the start, with the value it replaced.
    log: Vec<(String, Option<bool>)>,
}

impl Symbols {
    fn get(&self, name: &str) -> Option<bool> {
        self.values.get(name).copied()
    }

    /// Sets `name` (`None`: no evidence), logging the value it replaces.
    fn set(&mut self, name: &str, value: Option<bool>) {
        let old = match value {
            Some(v) => self.values.insert(name.to_string(), v),
            None => self.values.remove(name),
        };
        self.log.push((name.to_string(), old));
    }

    /// The value at this point of each name written since `mark`.
    fn written_since(&self, mark: usize) -> HashMap<String, Option<bool>> {
        let mut writes = HashMap::new();
        for (name, _) in &self.log[mark..] {
            if !writes.contains_key(name) {
                writes.insert(name.clone(), self.get(name));
            }
        }
        writes
    }

    /// Undoes every write since `mark`.
    fn rollback(&mut self, mark: usize) {
        for (name, old) in self.log.drain(mark..).rev() {
            match old {
                Some(v) => self.values.insert(name, v),
                None => self.values.remove(&name),
            };
        }
    }
}

/// Recurses through `node`'s named children in document order. A dead
/// branch is never recursed into at all, so a `#define`/`#undef` reached
/// here is on some path that may compile.
fn walk(node: Node, source: &[u8], defined: &mut Symbols, out: &mut Vec<CSharpDeadCodeRegion>) {
    let mut cursor = node.walk();
    let children: Vec<Node> = node.named_children(&mut cursor).collect();
    walk_siblings(&children, source, defined, out);
}

/// Same as [`walk`], but over an arbitrary already-collected slice of
/// siblings (used for the body of a live/unknown branch, which is a
/// sub-slice of some ancestor's children — not a node's own
/// `named_children()`). Dispatches each sibling by its own kind, since a
/// sibling might itself be a `preproc_if` chain that needs [`walk_chain`],
/// not a plain container to recurse into via [`walk`].
fn walk_siblings(
    children: &[Node],
    source: &[u8],
    defined: &mut Symbols,
    out: &mut Vec<CSharpDeadCodeRegion>,
) {
    for child in children {
        match child.kind() {
            "preproc_define" | "preproc_undef" => {
                if let Some(name) = preproc_arg_name(*child, source) {
                    defined.set(&name, Some(child.kind() == "preproc_define"));
                }
            }
            "preproc_if" => walk_chain(*child, source, defined, out),
            _ => walk(*child, source, defined, out),
        }
    }
}

/// Walks one `#if`/`#elif`/`#else` chain, following the `alternative` field
/// from `#if` through any `#elif`s to a trailing `#else`, and leaves in
/// `defined` what every path through the chain agrees on.
fn walk_chain(
    node: Node,
    source: &[u8],
    defined: &mut Symbols,
    out: &mut Vec<CSharpDeadCodeRegion>,
) {
    // The symbol a bare `#if FOO` / `#if !FOO` tests, and its value in the
    // first branch. Every later branch, and the path that skips the chain,
    // has the opposite.
    let tested = node
        .child_by_field_name("condition")
        .and_then(|cond| tested_symbol(cond, source));
    // Whether an earlier branch in this chain was proven to always run —
    // if so, every later branch (elif/else) is dead regardless of its own
    // condition.
    let mut chain_resolved_true = false;
    let mut has_else = false;
    // What each branch that may compile wrote, with each name's value at
    // `#endif` on that path.
    let mut paths: Vec<HashMap<String, Option<bool>>> = Vec::new();
    let branches = chain_branches(node);
    let mut first = true;

    for (condition, body) in branches {
        has_else |= condition.is_none();
        // Every condition is evaluated against the chain's entry state: an
        // earlier branch's writes were rolled back.
        let live = !chain_resolved_true
            && match condition.map(|cond| eval_condition(cond, source, defined)) {
                Some(Some(false)) => false,
                Some(Some(true)) => {
                    chain_resolved_true = true;
                    true
                }
                Some(None) | None => true,
            };
        if live {
            let mark = defined.log.len();
            if let Some((name, value_in_first)) = &tested {
                defined.set(name, Some(*value_in_first == first));
            }
            walk_siblings(&body, source, defined, out);
            paths.push(defined.written_since(mark));
            defined.rollback(mark);
        } else {
            push_dead_range(&body, out);
        }
        first = false;
    }

    // A missing `#else` is an empty branch, taken whenever every condition
    // is false; no path skips a chain with a proven-true branch.
    if !has_else && !chain_resolved_true {
        let skip = tested
            .iter()
            .map(|(name, value_in_first)| (name.clone(), Some(!value_in_first)));
        paths.push(skip.collect());
    }
    // Join each written name over the paths that wrote it, then with its
    // entry value if some path did not: a name keeps a value only if every
    // path agrees. Linear in the writes, so a long `#elif` chain stays cheap.
    let total = paths.len();
    let mut joined: HashMap<String, (Option<bool>, usize)> = HashMap::new();
    for (name, value) in paths.into_iter().flatten() {
        joined
            .entry(name)
            .and_modify(|(acc, n)| {
                *acc = agree(*acc, value);
                *n += 1;
            })
            .or_insert((value, 1));
    }
    for (name, (value, n)) in joined {
        let value = if n < total {
            agree(value, defined.get(&name))
        } else {
            value
        };
        if value != defined.get(&name) {
            defined.set(&name, value);
        }
    }
}

/// Two paths' values for one symbol joined: the value if they agree, no
/// evidence otherwise.
fn agree(a: Option<bool>, b: Option<bool>) -> Option<bool> {
    if a == b {
        a
    } else {
        None
    }
}

/// The symbol whose definedness alone decides a `#if FOO` or `#if !FOO`
/// condition (parentheses allowed), and whether the condition is true when
/// it is defined. `None` for any other condition.
fn tested_symbol(node: Node, source: &[u8]) -> Option<(String, bool)> {
    match node.kind() {
        "identifier" => Some((node.utf8_text(source).ok()?.to_string(), true)),
        "parenthesized_expression" => tested_symbol(node.named_child(0)?, source),
        "unary_expression" if node.child_by_field_name("operator")?.kind() == "!" => {
            let (name, when) = tested_symbol(node.child_by_field_name("argument")?, source)?;
            Some((name, !when))
        }
        _ => None,
    }
}

/// Each branch of the chain starting at `node`, in order: its condition
/// (`None` for `#else`) and its guarded content.
///
/// The content is assigned by line, not by tree parent. tree-sitter-c-sharp
/// parses directives as extras, so a branch's trailing `#define`/`#undef`
/// can hang off an outer node of the chain (an `#else` holding only
/// `#undef FOO` comes out empty, with the `#undef` a child of the `#if`
/// after its `alternative`). A branch owns everything from its own
/// directive's line to the next branch's.
fn chain_branches(node: Node) -> Vec<(Option<Node>, Vec<Node>)> {
    let mut heads = Vec::new();
    let mut fields = HashSet::new();
    let mut current = Some(node);
    while let Some(n) = current {
        let condition = n.child_by_field_name("condition");
        current = n.child_by_field_name("alternative");
        fields.extend(condition.iter().chain(current.iter()).map(|f| f.id()));
        heads.push((n, condition));
    }
    let mut content: Vec<Node> = heads
        .iter()
        .flat_map(|(n, _)| {
            let mut cursor = n.walk();
            n.named_children(&mut cursor).collect::<Vec<_>>()
        })
        .filter(|c| !fields.contains(&c.id()))
        .collect();
    content.sort_by_key(|c| c.start_byte());
    let mut branches: Vec<(Option<Node>, Vec<Node>)> = heads
        .iter()
        .map(|(_, condition)| (*condition, Vec::new()))
        .collect();
    let starts: Vec<usize> = heads.iter().map(|(n, _)| n.start_position().row).collect();
    // Rows only grow along `content`, so one pass assigns every node.
    let mut i = 0;
    for c in content {
        let row = c.start_position().row;
        while starts.get(i + 1).is_some_and(|&next| next <= row) {
            i += 1;
        }
        if row >= starts[0] {
            branches[i].1.push(c);
        }
    }
    branches
}

fn push_dead_range(body: &[Node], out: &mut Vec<CSharpDeadCodeRegion>) {
    let (Some(first), Some(last)) = (body.first(), body.last()) else {
        return;
    };
    out.push(CSharpDeadCodeRegion {
        start_line: first.start_position().row + 1,
        end_line: last_line(*last),
    });
}

/// The 1-based last line of `node`'s own text. A directive node's range
/// takes in its line ending, so it ends at column 0 of the next line.
fn last_line(node: Node) -> usize {
    let end = node.end_position();
    if end.column == 0 && end.row > node.start_position().row {
        end.row
    } else {
        end.row + 1
    }
}

/// Evaluates a `#if`/`#elif` condition expression node to a constant
/// boolean, if locally provable — `None` as soon as any operand can't be
/// resolved (an unrecognized construct, or a symbol with no local
/// evidence), except where `&&`/`||` short-circuits past it.
fn eval_condition(node: Node, source: &[u8], defined: &Symbols) -> Option<bool> {
    match node.kind() {
        "boolean_literal" => match node.utf8_text(source) {
            Ok("true") => Some(true),
            Ok("false") => Some(false),
            _ => None,
        },
        "identifier" => {
            let name = node.utf8_text(source).ok()?;
            defined.get(name)
        }
        "parenthesized_expression" => eval_condition(node.named_child(0)?, source, defined),
        "unary_expression" => {
            if node.child_by_field_name("operator")?.kind() != "!" {
                return None;
            }
            let arg = node.child_by_field_name("argument")?;
            Some(!eval_condition(arg, source, defined)?)
        }
        "binary_expression" => {
            let op = node.child_by_field_name("operator")?.kind();
            let left = node.child_by_field_name("left")?;
            let right = node.child_by_field_name("right")?;
            let l = eval_condition(left, source, defined);
            let r = eval_condition(right, source, defined);
            match op {
                "&&" => match (l, r) {
                    (Some(false), _) | (_, Some(false)) => Some(false),
                    (Some(a), Some(b)) => Some(a && b),
                    _ => None,
                },
                "||" => match (l, r) {
                    (Some(true), _) | (_, Some(true)) => Some(true),
                    (Some(a), Some(b)) => Some(a || b),
                    _ => None,
                },
                "==" => Some(l? == r?),
                "!=" => Some(l? != r?),
                _ => None,
            }
        }
        _ => None,
    }
}

/// Extracts the leading identifier from a `preproc_define`/`preproc_undef`
/// node's `preproc_arg` child (which matches arbitrary trailing text, not
/// just an identifier, so this trims to the symbol name itself).
fn preproc_arg_name(node: Node, source: &[u8]) -> Option<String> {
    let arg = node.named_child(0)?;
    let text = arg.utf8_text(source).ok()?.trim_start();
    let end = text
        .find(|c: char| !(c.is_alphanumeric() || c == '_'))
        .unwrap_or(text.len());
    if end == 0 {
        None
    } else {
        Some(text[..end].to_string())
    }
}

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

    fn ranges(source: &str) -> Vec<(usize, usize)> {
        csharp_dead_code_regions(source.as_bytes())
            .into_iter()
            .map(|r| (r.start_line, r.end_line))
            .collect()
    }

    #[test]
    fn if_false_is_dead() {
        let src = "class C {\n#if false\n    int x = 1;\n#endif\n}\n";
        assert_eq!(ranges(src), vec![(3, 3)]);
    }

    #[test]
    fn if_true_else_branch_dead() {
        let src = "#if true\nint x = 1;\n#else\nint y = 2;\n#endif\n";
        assert_eq!(ranges(src), vec![(4, 4)]);
    }

    #[test]
    fn negated_false_makes_branch_live() {
        let src = "#if !false\nint x = 1;\n#endif\n";
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn elif_true_kills_trailing_else() {
        let src = concat!(
            "#if false\n",  // 1
            "int a = 1;\n", // 2
            "#elif true\n", // 3
            "int b = 2;\n", // 4
            "#else\n",      // 5
            "int c = 3;\n", // 6
            "#endif\n",     // 7
        );
        assert_eq!(ranges(src), vec![(2, 2), (6, 6)]);
    }

    #[test]
    fn always_defined_symbol_else_branch_dead() {
        let src = concat!(
            "#define FOO\n", // 1
            "#if FOO\n",     // 2
            "int a = 1;\n",  // 3
            "#else\n",       // 4
            "int b = 2;\n",  // 5
            "#endif\n",      // 6
        );
        assert_eq!(ranges(src), vec![(5, 5)]);
    }

    #[test]
    fn commented_out_define_is_not_evidence() {
        // The build can still define FOO (`-define:FOO`, `DefineConstants`).
        for comment in ["//#define FOO\n", "// #define FOO\n", "/* #define FOO */\n"] {
            let src = format!("{comment}#if FOO\nint a = 1;\n#else\nint b = 2;\n#endif\n");
            assert!(ranges(&src).is_empty(), "{comment}");
        }
    }

    #[test]
    fn real_define_still_decides_next_to_a_commented_one() {
        let src = concat!(
            "// #define BAR\n", // 1
            "#define FOO\n",    // 2
            "#if FOO\n",        // 3
            "int a = 1;\n",     // 4
            "#else\n",          // 5
            "int b = 2;\n",     // 6
            "#endif\n",         // 7
            "#if !FOO\n",       // 8
            "int c = 3;\n",     // 9
            "#endif\n",         // 10
        );
        assert_eq!(ranges(src), vec![(6, 6), (9, 9)]);
    }

    #[test]
    fn undef_with_no_later_define_makes_if_dead() {
        let src = "#define FOO\n#undef FOO\n#if FOO\nint a = 1;\n#endif\n";
        assert_eq!(ranges(src), vec![(4, 4)]);
    }

    #[test]
    fn undef_inside_an_undecided_branch_leaves_the_symbol_undecided() {
        // With X defined, FOO is undefined at line 5, so the #else compiles.
        let src = concat!(
            "#define FOO\n", // 1
            "#if X\n",       // 2
            "#undef FOO\n",  // 3
            "#endif\n",      // 4
            "#if FOO\n",     // 5
            "int a = 1;\n",  // 6
            "#else\n",       // 7
            "int b = 2;\n",  // 8
            "#endif\n",      // 9
        );
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn undef_nested_two_branches_deep_leaves_the_symbol_undecided() {
        let src = concat!(
            "#define FOO\n", // 1
            "#if X\n",       // 2
            "#if Y\n",       // 3
            "#undef FOO\n",  // 4
            "#endif\n",      // 5
            "#endif\n",      // 6
            "#if !FOO\n",    // 7
            "int a = 1;\n",  // 8
            "#endif\n",      // 9
        );
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn a_define_on_every_branch_holds_after_endif() {
        let src = concat!(
            "#if X\n",       // 1
            "#define FOO\n", // 2
            "#elif Y\n",     // 3
            "#define FOO\n", // 4
            "#else\n",       // 5
            "#define FOO\n", // 6
            "#endif\n",      // 7
            "#if FOO\n",     // 8
            "int a = 1;\n",  // 9
            "#else\n",       // 10
            "int b = 2;\n",  // 11
            "#endif\n",      // 12
        );
        assert_eq!(ranges(src), vec![(11, 11)]);
    }

    #[test]
    fn a_define_on_some_branches_is_undecided_after_endif() {
        let src = concat!(
            "#if X\n",       // 1
            "#define FOO\n", // 2
            "#elif Y\n",     // 3
            "#define FOO\n", // 4
            "#endif\n",      // 5
            "#if FOO\n",     // 6
            "int a = 1;\n",  // 7
            "#else\n",       // 8
            "int b = 2;\n",  // 9
            "#endif\n",      // 10
        );
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn the_default_value_idiom_defines_the_symbol_on_both_paths() {
        for (test, write) in [("!FOO", "#define FOO"), ("(FOO)", "#undef BAR")] {
            let src = format!(
                "#if {test}\n{write}\n#endif\n#if FOO\nint a = 1;\n#else\nint b = 2;\n#endif\n"
            );
            let expected = if test == "!FOO" { vec![(7, 7)] } else { vec![] };
            assert_eq!(ranges(&src), expected, "{test}");
        }
    }

    #[test]
    fn an_undef_in_the_else_of_a_tested_symbol_leaves_it_undefined() {
        let src = concat!(
            "#if FOO\n",    // 1
            "#undef FOO\n", // 2
            "#else\n",      // 3
            "#endif\n",     // 4
            "#if FOO\n",    // 5
            "int a = 1;\n", // 6
            "#endif\n",     // 7
        );
        assert_eq!(ranges(src), vec![(6, 6)]);
    }

    #[test]
    fn a_define_in_a_proven_branch_holds_after_endif() {
        let src = concat!(
            "#if true\n",    // 1
            "#define FOO\n", // 2
            "#else\n",       // 3
            "#undef FOO\n",  // 4
            "#endif\n",      // 5
            "#if !FOO\n",    // 6
            "int a = 1;\n",  // 7
            "#endif\n",      // 8
        );
        assert_eq!(ranges(src), vec![(4, 4), (7, 7)]);
    }

    #[test]
    fn a_define_in_a_dead_branch_counts_on_no_path() {
        let src = concat!(
            "#if false\n",   // 1
            "#define FOO\n", // 2
            "#endif\n",      // 3
            "#if FOO\n",     // 4
            "int a = 1;\n",  // 5
            "#else\n",       // 6
            "int b = 2;\n",  // 7
            "#endif\n",      // 8
        );
        assert_eq!(ranges(src), vec![(2, 2)]);
    }

    #[test]
    fn a_later_branch_condition_ignores_an_earlier_branch_define() {
        let src = concat!(
            "#if X\n",       // 1
            "#define FOO\n", // 2
            "#elif FOO\n",   // 3
            "int a = 1;\n",  // 4
            "#endif\n",      // 5
        );
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn an_else_holding_only_a_directive_is_found_dead() {
        let src = concat!(
            "#if true\n",   // 1
            "int a = 1;\n", // 2
            "#else\n",      // 3
            "#undef FOO\n", // 4
            "#endif\n",     // 5
        );
        assert_eq!(ranges(src), vec![(4, 4)]);
    }

    #[test]
    fn a_dead_branch_ending_in_a_directive_stops_before_the_next_branch() {
        let src = concat!(
            "#if false\n",   // 1
            "int a = 1;\n",  // 2
            "#define FOO\n", // 3
            "#elif BAR\n",   // 4
            "#define BAZ\n", // 5
            "#endif\n",      // 6
            "#if !BAZ\n",    // 7
            "int b = 2;\n",  // 8
            "#endif\n",      // 9
        );
        assert_eq!(ranges(src), vec![(2, 3)]);
    }

    /// Whether scanning `shape(4 * n)` costs well under 16 times
    /// `shape(n)`, as a quadratic scan would.
    fn scales_linearly(shape: impl Fn(usize) -> String) -> bool {
        crate::scaling_check::costs_under(
            &shape(2_500),
            &shape(10_000),
            crate::scaling_check::LINEAR_BOUND,
            |source| {
                csharp_dead_code_regions(source.as_bytes());
            },
        )
    }

    #[test]
    fn many_defines_and_chains_scan_in_linear_time() {
        let shape = |n: usize| {
            let defines: String = (0..n).map(|i| format!("#define S{i}\n")).collect();
            let chains: String = (0..n / 2)
                .map(|i| format!("#if X{i}\nint a{i};\n#endif\n"))
                .collect();
            defines + &chains
        };
        assert!(scales_linearly(shape));
    }

    #[test]
    fn a_long_elif_chain_scans_in_linear_time() {
        let shape = |n: usize| {
            let arms: String = (1..n)
                .map(|i| format!("#elif X{i}\n#define D{i}\nint b{i};\n"))
                .collect();
            format!("#if X0\nint a;\n{arms}#endif\n")
        };
        assert!(scales_linearly(shape));
    }

    #[test]
    fn symbol_never_mentioned_is_neutral() {
        let src = "#if DEBUG\nint a = 1;\n#endif\n";
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn short_circuit_and_with_false_literal_is_dead() {
        let src = "#if false && DEBUG\nint a = 1;\n#endif\n";
        assert_eq!(ranges(src), vec![(2, 2)]);
    }

    #[test]
    fn short_circuit_or_with_true_literal_makes_else_dead() {
        let src = "#if true || DEBUG\nint a = 1;\n#else\nint b = 2;\n#endif\n";
        assert_eq!(ranges(src), vec![(4, 4)]);
    }

    #[test]
    fn define_inside_conditional_does_not_count_as_unconditional() {
        let src = "#if BAR\n#define FOO\n#endif\n#if FOO\nint a = 1;\n#endif\n";
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn nested_dead_chain_inside_dead_branch_does_not_split_region() {
        let src = concat!(
            "#if false\n",  // 1
            "int a = 1;\n", // 2
            "#if true\n",   // 3
            "int b = 2;\n", // 4
            "#endif\n",     // 5
            "int c = 3;\n", // 6
            "#endif\n",     // 7
        );
        assert_eq!(ranges(src), vec![(2, 6)]);
    }

    #[test]
    fn nested_dead_chain_inside_live_branch_is_still_found() {
        let src = concat!(
            "#if true\n",   // 1
            "int a = 1;\n", // 2
            "#if false\n",  // 3
            "int b = 2;\n", // 4
            "#endif\n",     // 5
            "int c = 3;\n", // 6
            "#endif\n",     // 7
        );
        assert_eq!(ranges(src), vec![(4, 4)]);
    }

    #[test]
    fn empty_dead_branch_yields_no_region() {
        let src = "#if false\n#endif\n";
        assert!(ranges(src).is_empty());
    }

    #[test]
    fn no_directives_yields_no_regions() {
        assert!(ranges("class C {}\n").is_empty());
    }

    #[test]
    fn empty_source_yields_no_regions() {
        assert!(ranges("").is_empty());
    }
}