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
//! Dead-code region detection for Swift's `#if`/`#elseif`/`#else`/`#endif`
//! conditional compilation.
//!
//! Unlike [`crate::dead_code`] (C/C++), this is tree-sitter-based, not
//! line-based. tree-sitter-swift's grammar splices its `directive` node
//! (produced for each `#if`/`#elseif`/`#else`/`#endif` line) into every rule
//! that can contain one — `statements`, `class_body`, `protocol_body`,
//! `enum_class_body`, `source_file`, and others — as an ordinary sibling
//! alongside real content, rather than nesting the guarded content
//! underneath it. Swift's language spec also requires each `#if` branch to
//! contain syntactically complete code, so — unlike C, where a stray
//! `extern "C" {` brace desyncs tree-sitter's error recovery (see
//! [`crate::dead_code`]'s module docs for why that forces a line-based
//! scanner there) — a valid Swift file always parses cleanly regardless of
//! which branch is logically live, and sibling order is reliable. That
//! makes an AST sibling walk both viable and preferable to a text scanner
//! here.
//!
//! Scope is narrower than the C/C++ module's: Swift has no `#define`, so
//! there is no analog to that module's sub-problem 2 (locally-provable
//! named-macro definedness) at all. `os()`, `arch()`, `swift()`,
//! `compiler()`, `canImport()`, `targetEnvironment()`, and bare custom flags
//! (set only via external `-D` compiler flags, never in source) are all
//! determined by the build target, not the file, and are left unclassified
//! rather than guessed at. The one thing that *is* locally provable —
//! Swift's direct analog to C's `#if 0` — is a condition built purely from
//! boolean literals and boolean connectives: `#if false`, `#if true &&
//! false`, `#if !true`, and so on. A condition that mixes a literal with a
//! non-literal (e.g. `#if false && DEBUG`) is intentionally left
//! unclassified too, even though short-circuiting could in principle prove
//! it — that combination doesn't show up in practice, and evaluating it
//! soundly would mean modeling `&&`/`||` short-circuit semantics over
//! partially-unknown operands for no real-world benefit.

use tree_sitter::{Node, Parser};

/// A Swift preprocessor-dead region: a `#if`/`#elseif`/`#else` branch whose
/// condition is a compile-time-constant `false` (built only from
/// `true`/`false` boolean literals, `!`, `&&`, `||`, and parens).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SwiftDeadCodeRegion {
    /// 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 Swift file: branches of a
/// `#if`/`#elseif`/`#else` chain that a compile-time-constant condition
/// proves unreachable. See the module documentation for exactly what is and
/// isn't recognized.
pub fn swift_dead_code_regions(source: &[u8]) -> Vec<SwiftDeadCodeRegion> {
    let mut parser = Parser::new();
    if parser
        .set_language(&tree_sitter_swift::LANGUAGE.into())
        .is_err()
    {
        return Vec::new();
    }
    let Some(tree) = parser.parse(source, None) else {
        return Vec::new();
    };

    let mut regions = Vec::new();
    scan_children(tree.root_node(), source, &mut regions);
    regions
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum DirectiveKind {
    If,
    ElseIf,
    Else,
    EndIf,
}

fn directive_kind(node: Node) -> Option<DirectiveKind> {
    if node.kind() != "directive" {
        return None;
    }
    match node.child(0)?.kind() {
        "#if" => Some(DirectiveKind::If),
        "#elseif" => Some(DirectiveKind::ElseIf),
        "#else" => Some(DirectiveKind::Else),
        "#endif" => Some(DirectiveKind::EndIf),
        _ => None,
    }
}

/// Recurses through `parent`'s named children, matching up
/// `#if`/`#elseif`/`#else`/`#endif` chains among them (they appear as
/// ordinary siblings — see module docs) and recursing into every live
/// child, whether or not it's part of a chain.
fn scan_children(parent: Node, source: &[u8], out: &mut Vec<SwiftDeadCodeRegion>) {
    let mut cursor = parent.walk();
    let children: Vec<Node> = parent.named_children(&mut cursor).collect();
    scan_slice(&children, source, out);
}

/// Same as [`scan_children`], but over an arbitrary already-collected slice
/// of siblings (used for the body of a live branch, which is a sub-slice of
/// some ancestor's children — not a node's own `named_children()`).
fn scan_slice(children: &[Node], source: &[u8], out: &mut Vec<SwiftDeadCodeRegion>) {
    let mut i = 0;
    while i < children.len() {
        if directive_kind(children[i]) == Some(DirectiveKind::If) {
            i = scan_chain(children, i, source, out);
        } else {
            scan_children(children[i], source, out);
            i += 1;
        }
    }
}

/// Processes one `#if...#endif` chain starting at `children[start]` (an
/// `#if` directive). Returns the index just past the chain's `#endif`.
fn scan_chain(
    children: &[Node],
    start: usize,
    source: &[u8],
    out: &mut Vec<SwiftDeadCodeRegion>,
) -> usize {
    let mut i = start;
    // Whether an earlier branch in this chain was proven to always run —
    // if so, every later branch (elseif/else) is dead regardless of its
    // own condition.
    let mut chain_resolved_true = false;

    loop {
        let directive_node = children[i];
        let Some(kind) = directive_kind(directive_node) else {
            return i;
        };
        if kind == DirectiveKind::EndIf {
            return i + 1;
        }

        // A nested #if...#endif chain's directives are flat siblings too
        // (same reason as this chain's own), so the body boundary search
        // has to track nesting depth — otherwise a nested chain's own
        // #if/#endif would be mistaken for this branch's terminator.
        let body_start = i + 1;
        let mut body_end = body_start;
        let mut depth = 0usize;
        while body_end < children.len() {
            match directive_kind(children[body_end]) {
                Some(DirectiveKind::If) => depth += 1,
                Some(DirectiveKind::EndIf) => {
                    if depth == 0 {
                        break;
                    }
                    depth -= 1;
                }
                Some(DirectiveKind::ElseIf | DirectiveKind::Else) if depth == 0 => break,
                _ => {}
            }
            body_end += 1;
        }
        let body = &children[body_start..body_end];

        if chain_resolved_true {
            push_dead_range(body, out);
        } else {
            match kind {
                DirectiveKind::Else => scan_slice(body, source, out),
                DirectiveKind::If | DirectiveKind::ElseIf => {
                    let tokens = condition_tokens(directive_node, source);
                    match classify_constant_condition(&tokens) {
                        Some(true) => {
                            chain_resolved_true = true;
                            scan_slice(body, source, out);
                        }
                        Some(false) => push_dead_range(body, out),
                        None => scan_slice(body, source, out),
                    }
                }
                DirectiveKind::EndIf => unreachable!(),
            }
        }

        i = body_end;
    }
}

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

#[derive(Clone, Copy, PartialEq, Eq)]
enum Tok {
    True,
    False,
    Not,
    And,
    Or,
    LParen,
    RParen,
    /// Anything else — `os(...)`, a bare custom flag, an integer literal,
    /// `.`, etc. Its presence anywhere in a condition makes the whole
    /// condition unclassifiable.
    Other,
}

/// The condition tokens of an `#if`/`#elseif` `directive` node — its
/// children after the leading `#if`/`#elseif` keyword token.
fn condition_tokens(directive: Node, source: &[u8]) -> Vec<Tok> {
    let mut cursor = directive.walk();
    directive
        .children(&mut cursor)
        .skip(1)
        .map(|n| classify_token(n, source))
        .collect()
}

fn classify_token(node: Node, source: &[u8]) -> Tok {
    match node.kind() {
        "boolean_literal" => match node.utf8_text(source) {
            Ok("true") => Tok::True,
            Ok("false") => Tok::False,
            _ => Tok::Other,
        },
        "!" => Tok::Not,
        "&&" => Tok::And,
        "||" => Tok::Or,
        "(" => Tok::LParen,
        ")" => Tok::RParen,
        _ => Tok::Other,
    }
}

/// Evaluates `tokens` as a boolean expression (`!` > `&&` > `||`, with
/// parens) if and only if every token is a literal or connective — `None`
/// as soon as an `Other` token appears anywhere, or on malformed syntax.
fn classify_constant_condition(tokens: &[Tok]) -> Option<bool> {
    let mut pos = 0;
    let value = eval_or(tokens, &mut pos)?;
    if pos == tokens.len() {
        Some(value)
    } else {
        None
    }
}

fn eval_or(tokens: &[Tok], pos: &mut usize) -> Option<bool> {
    let mut acc = eval_and(tokens, pos)?;
    while tokens.get(*pos) == Some(&Tok::Or) {
        *pos += 1;
        acc = eval_and(tokens, pos)? || acc;
    }
    Some(acc)
}

fn eval_and(tokens: &[Tok], pos: &mut usize) -> Option<bool> {
    let mut acc = eval_unary(tokens, pos)?;
    while tokens.get(*pos) == Some(&Tok::And) {
        *pos += 1;
        acc = eval_unary(tokens, pos)? && acc;
    }
    Some(acc)
}

fn eval_unary(tokens: &[Tok], pos: &mut usize) -> Option<bool> {
    if tokens.get(*pos) == Some(&Tok::Not) {
        *pos += 1;
        return Some(!eval_unary(tokens, pos)?);
    }
    eval_primary(tokens, pos)
}

fn eval_primary(tokens: &[Tok], pos: &mut usize) -> Option<bool> {
    match tokens.get(*pos)? {
        Tok::True => {
            *pos += 1;
            Some(true)
        }
        Tok::False => {
            *pos += 1;
            Some(false)
        }
        Tok::LParen => {
            *pos += 1;
            let value = eval_or(tokens, pos)?;
            if tokens.get(*pos) == Some(&Tok::RParen) {
                *pos += 1;
                Some(value)
            } else {
                None
            }
        }
        _ => None,
    }
}

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

    fn ranges(source: &str) -> Vec<(usize, usize)> {
        swift_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 = "func f() {\n#if false\n    live()\n#endif\n}\n";
        assert_eq!(ranges(src), vec![(3, 3)]);
    }

    #[test]
    fn if_true_else_branch_dead() {
        let src = "func f() {\n#if true\n    live()\n#else\n    dead()\n#endif\n}\n";
        assert_eq!(ranges(src), vec![(5, 5)]);
    }

    #[test]
    fn boolean_combination_true_and_false_is_dead() {
        let src = "#if true && false\ndead()\n#endif\n";
        assert_eq!(ranges(src), vec![(2, 2)]);
    }

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

    #[test]
    fn elseif_true_kills_trailing_else() {
        let src = concat!(
            "#if false\n",    // 1
            "dead1()\n",      // 2
            "#elseif true\n", // 3
            "live()\n",       // 4
            "#else\n",        // 5
            "dead2()\n",      // 6
            "#endif\n",       // 7
        );
        assert_eq!(ranges(src), vec![(2, 2), (6, 6)]);
    }

    #[test]
    fn os_condition_is_not_locally_provable() {
        let src = "#if os(iOS)\nmaybe_dead()\n#endif\n";
        assert!(ranges(src).is_empty());
    }

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

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

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

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

    #[test]
    fn dead_branch_inside_live_class_body_is_found() {
        let src = concat!(
            "class C {\n",     // 1
            "#if false\n",     // 2
            "    var x = 1\n", // 3
            "#endif\n",        // 4
            "}\n",             // 5
        );
        assert_eq!(ranges(src), vec![(3, 3)]);
    }

    #[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("func f() -> Int { return 1 }\n").is_empty());
    }

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