ripscope 0.1.0

Cscope-compatible code navigation for C with content-aware, atomic index updates
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
//! Saved conditional arms, their selective restoration and delayed joins.
//! Directive recognition and source consumption stay in Scan (reading-c.md ยง6).

use super::{CallContext, CallPrefix, Declaration, Nesting, PendingHead, TypedefPrefix};
use crate::scanner::{Corrections, input::Input, shapes::LookAhead, tails::Tail};

/// Every arm starts at its group's opening brace depth. Closure leaves the deepest
/// arm's depth behind, without restoring the general parenthesis depth.
pub(super) struct ConditionalArms {
    groups: Vec<Group>,
    /// Opening clears this; else sets it even unmatched; elif leaves it unchanged.
    had_else: bool,
    /// A following #if continues the group whose #endif was just read.
    continues: bool,
    heads: bool,
    tails: bool,
    typedefs: bool,
    calls: bool,
    /// An arm that closed more braces than it opened ends its group, and so does one
    /// whose following arm would (closing-arms).
    closing: bool,
    /// Bytes the following-arm look-ahead has read, against its limit.
    looked: usize,
    /// The group just closed ended so (closing-arms): the next opens after it.
    split: bool,
    /// The closing-arms look-ahead's work limit, this many times the file's length.
    factor: usize,
    /// Test-only: how often that limit stopped the look-ahead.
    #[cfg(test)]
    pub(super) refusals: usize,
    /// Each arm at file scope, or one that leaves a head pending, starts at the group's
    /// parenthesis depth, so each arm's head opens its list alike (conditional-lists).
    lists: bool,
}

impl ConditionalArms {
    pub(super) fn new(corrections: Corrections) -> Self {
        Self {
            groups: Vec::new(),
            had_else: false,
            continues: false,
            heads: corrections.definition_bodies,
            tails: corrections.bare_directive_words || corrections.opaque_member_tails,
            typedefs: corrections.typedef_decorations,
            calls: corrections.dereferenced_calls,
            closing: corrections.closing_arms,
            looked: 0,
            split: false,
            factor: 16,
            #[cfg(test)]
            refusals: 0,
            lists: corrections.conditional_lists,
        }
    }

    pub(super) fn open(&mut self, nesting: &mut Nesting, declaration: &mut Declaration) {
        self.had_else = false;
        let after_split = std::mem::replace(&mut self.split, false);
        if self.continues {
            self.continues = false;
            self.next_arm(nesting, declaration);
        } else {
            self.groups.push(Group {
                after_split,
                first_arm: None,
                calls_before: nesting.calls,
                calls_join: None,
                depth_before: nesting.depth,
                parentheses_before: nesting.parentheses,
                deepest_arm: 0,
                head_before: nesting.head_pending,
                function_before: nesting.function,
                type_prefix_before: declaration.type_prefix,
                typedef_prefix_before: declaration.typedef_prefix,
                typedef_prefix_join: None,
                tail_before: declaration.tail,
                tail_join: None,
                structure_before: nesting.file_structure,
                structure_join: None,
                has_else: false,
            });
        }
    }

    /// An elif changes arms without marking an else, even for unmatched directives.
    pub(super) fn alternate(
        &mut self,
        nesting: &mut Nesting,
        declaration: &mut Declaration,
        is_else: bool,
    ) {
        if is_else {
            self.had_else = true;
            if let Some(group) = self.groups.last_mut() {
                group.has_else = true;
            }
        }
        self.next_arm(nesting, declaration);
    }

    /// Called after the endif tail is consumed, before Scan consumes its newline.
    pub(super) fn close(
        &mut self,
        nesting: &mut Nesting,
        declaration: &mut Declaration,
        input: &Input,
        look: &mut LookAhead,
    ) {
        let continued = look.continued_conditional(input);
        let closed = look.endif_gaps(input);
        let continues = match (continued, closed) {
            (Some(continued), Some(closed)) => continued >= closed,
            (Some(_), None) => true,
            _ => false,
        };
        let mut sequential = || {
            let Some(group) = self.groups.last() else {
                return false;
            };
            if nesting.depth < group.depth_before {
                return true;
            }
            // The look-ahead stops reading once it has read 16 times the file's length.
            let limit = self
                .factor
                .saturating_mul(input.source_len())
                .saturating_add(1 << 16);
            if self.looked > limit {
                #[cfg(test)]
                {
                    self.refusals += 1;
                }
                return false;
            }
            let (read, closes) = arm_closes(input, continued.unwrap_or(0), ARM_WINDOW);
            self.looked += read;
            closes
        };
        let split = continues && !self.had_else && self.closing && sequential();
        if continues && !self.had_else && !split {
            self.continues = true;
        } else if let Some(mut group) = self.groups.pop() {
            self.split = split;
            if self.calls {
                group.join_calls(nesting.calls);
                if !group.has_else {
                    group.join_calls(group.calls_before);
                }
                nesting.calls = group.calls_join.expect("current arm joined");
            }
            // A group read after the one it closes a brace of keeps its first arm's
            // depth, as with every first arm taken, rather than its deepest arm's.
            nesting.depth = match group.first_arm {
                Some(first) if group.after_split => first,
                _ => nesting.depth.max(group.deepest_arm),
            };
            if self.typedefs {
                group.join_typedef_prefix(declaration.typedef_prefix);
                if !group.has_else {
                    group.join_typedef_prefix(group.typedef_prefix_before);
                }
                declaration.typedef_prefix = group.typedef_prefix_join.expect("current arm joined");
            }
            if self.tails {
                group.join_tail(declaration.tail, nesting.file_structure);
                if !group.has_else {
                    group.join_tail(group.tail_before, group.structure_before);
                }
                declaration.tail = group.tail_join.expect("current arm joined");
                nesting.file_structure = group.structure_join.expect("current arm joined");
            }
        }
    }

    /// Test-only: no work limit, to tell whether the limit changed a reading.
    #[cfg(test)]
    pub(super) fn unbounded(&mut self) {
        self.factor = usize::MAX / 2;
    }

    /// Whether the innermost group opened between a function's head and its body, at
    /// `depth`, the depth where function bodies open (alternative-bodies).
    pub(super) fn after_head(&self, depth: i32) -> bool {
        self.groups
            .last()
            .is_some_and(|group| group.function_before && group.depth_before == depth)
    }

    fn next_arm(&mut self, nesting: &mut Nesting, declaration: &mut Declaration) {
        if let Some(group) = self.groups.last_mut() {
            if self.calls {
                group.join_calls(nesting.calls);
                nesting.calls = group.calls_before;
            }
            if self.typedefs {
                group.join_typedef_prefix(declaration.typedef_prefix);
                declaration.typedef_prefix = group.typedef_prefix_before;
            }
            if self.tails {
                group.join_tail(declaration.tail, nesting.file_structure);
                declaration.tail = group.tail_before;
                nesting.file_structure = group.structure_before;
            }
            group.deepest_arm = group.deepest_arm.max(nesting.depth);
            group.first_arm.get_or_insert(nesting.depth);
            nesting.depth = group.depth_before;
            // At file scope each arm starts at the group's parenthesis depth, so an
            // alternative head whose list goes on after the group reads as the first.
            if self.lists && nesting.depth == 0 && !nesting.function {
                nesting.parentheses = group.parentheses_before;
            }
            // Alternative heads may share a return type before the #if. An
            // annotation conditional opened after a head keeps that head.
            if self.heads && nesting.head_pending.is_some() {
                nesting.head_pending = group.head_before;
                nesting.function = group.function_before;
                declaration.type_prefix = group.type_prefix_before;
                if self.lists {
                    nesting.parentheses = group.parentheses_before;
                }
            }
        }
    }
}

/// How far the closing-arms look-ahead reads into a following arm.
const ARM_WINDOW: usize = 64 * 1024;

/// How far the alternative-bodies look-ahead reads past a body's `}`.
const BODY_WINDOW: usize = 4 * 1024;

/// Whether an `#elif` or `#else` line follows the `}` at the start of `input`, and then
/// `{`, with only whitespace and comments around the directive line: the next arm opens
/// another body for the same head (alternative-bodies).
pub(super) fn alternative_body(input: &Input) -> bool {
    use crate::scanner::bodies::{directive_word, line_end, space};
    let at = space(input, 1, BODY_WINDOW);
    if at >= BODY_WINDOW
        || input.at(at) != Some(b'#')
        || !matches!(
            &directive_word(input, at, BODY_WINDOW)[..],
            b"elif" | b"else"
        )
    {
        return false;
    }
    let brace = space(input, line_end(input, at, BODY_WINDOW), BODY_WINDOW);
    brace < BODY_WINDOW && input.at(brace) == Some(b'{')
}

/// Whether the arm whose `#if` condition starts at `from` closes a brace it did not
/// open, reading its lines up to its own `#elif`, `#else` or `#endif`, and in a group
/// nested in it only the first arm, outside comments and literals; and the bytes read.
/// An arm that runs past `window` bytes, or to the end of input, counts as not closing.
fn arm_closes(input: &Input, from: usize, window: usize) -> (usize, bool) {
    let mut at = from;
    let (mut balance, mut nested, mut skipping) = (0_i32, Vec::<bool>::new(), 0_usize);
    let mut line_start = false;
    // The rest of the `#if` line, with its continuations.
    let directive_end = |mut at: usize| {
        while let Some(byte) = input.at(at) {
            if byte == b'\\' && input.at(at + 1) == Some(b'\n') {
                at += 2;
            } else if byte == b'\n' {
                break;
            } else {
                at += 1;
            }
            if at >= window {
                break;
            }
        }
        at
    };
    at = directive_end(at);
    while at < window {
        let Some(byte) = input.at(at) else {
            return (at, false);
        };
        if byte == b'\n' {
            (at, line_start) = (at + 1, true);
            continue;
        }
        if line_start && (byte == b' ' || byte == b'\t') {
            at += 1;
            continue;
        }
        if line_start && byte == b'#' {
            let word_at = at + 1 + input.run_within(at + 1, window, |b| b == b' ' || b == b'\t');
            let length = input.run_within(word_at, window, |b| b.is_ascii_alphabetic());
            match &input.peek_at(word_at, length)[..] {
                b"if" | b"ifdef" | b"ifndef" => nested.push(false),
                b"elif" | b"else" => match nested.last_mut() {
                    None => return (at, false),
                    Some(other) => {
                        if !*other {
                            *other = true;
                            skipping += 1;
                        }
                    }
                },
                b"endif" => match nested.pop() {
                    None => return (at, false),
                    Some(other) => skipping -= usize::from(other),
                },
                _ => {}
            }
            at = directive_end(word_at + length);
            line_start = false;
            continue;
        }
        line_start = false;
        match byte {
            b'/' if input.at(at + 1) == Some(b'*') => {
                at += 2;
                while at < window && input.at(at).is_some() {
                    if input.at(at) == Some(b'*') && input.at(at + 1) == Some(b'/') {
                        at += 2;
                        break;
                    }
                    at += 1;
                }
            }
            b'/' if input.at(at + 1) == Some(b'/') => {
                at += 2 + input.run_within(at + 2, window, |b| b != b'\n');
            }
            b'"' | b'\'' => {
                at += 1;
                while let Some(next) = input.at(at) {
                    if at >= window || next == b'\n' {
                        break;
                    }
                    at += if next == b'\\' { 2 } else { 1 };
                    if next == byte {
                        break;
                    }
                }
            }
            b'{' => {
                balance += i32::from(skipping == 0);
                at += 1;
            }
            b'}' => {
                if skipping == 0 {
                    balance -= 1;
                    if balance < 0 {
                        return (at, true);
                    }
                }
                at += 1;
            }
            _ => at += 1,
        }
    }
    (at, false)
}

/// Only the state each conditional reading restores or joins is saved here.
struct Group {
    /// It opened right after a group that closing-arms ended, and its first arm's depth.
    after_split: bool,
    first_arm: Option<i32>,
    calls_before: CallContext,
    calls_join: Option<CallContext>,
    depth_before: i32,
    parentheses_before: i32,
    deepest_arm: i32,
    head_before: Option<PendingHead>,
    function_before: bool,
    type_prefix_before: bool,
    typedef_prefix_before: TypedefPrefix,
    typedef_prefix_join: Option<TypedefPrefix>,
    tail_before: Tail,
    tail_join: Option<Tail>,
    structure_before: bool,
    structure_join: Option<bool>,
    has_else: bool,
}

impl Group {
    fn join_calls(&mut self, calls: CallContext) {
        self.calls_join = Some(
            self.calls_join
                .map_or(calls, |previous| previous.merge(calls)),
        );
    }

    fn join_typedef_prefix(&mut self, prefix: TypedefPrefix) {
        self.typedef_prefix_join = Some(
            self.typedef_prefix_join
                .map_or(prefix, |previous| previous.merge(prefix)),
        );
    }

    fn join_tail(&mut self, tail: Tail, structure: bool) {
        self.tail_join = Some(self.tail_join.map_or(tail, |previous| previous.merge(tail)));
        self.structure_join = Some(self.structure_join.unwrap_or(true) && structure);
    }
}

impl CallContext {
    fn merge(self, other: Self) -> Self {
        Self {
            prefix: if self.prefix == other.prefix {
                self.prefix
            } else if matches!(self.prefix, CallPrefix::Boundary | CallPrefix::Expression)
                && matches!(other.prefix, CallPrefix::Boundary | CallPrefix::Expression)
            {
                CallPrefix::Expression
            } else {
                CallPrefix::Other
            },
            closing: if self.closing == other.closing {
                self.closing
            } else {
                None
            },
            declaration: self.declaration.into_iter().chain(other.declaration).min(),
            cast: if self.cast == other.cast {
                self.cast
            } else {
                None
            },
            initializer: if self.initializer == other.initializer {
                self.initializer
            } else {
                None
            },
            arrays: self.arrays.max(other.arrays),
        }
    }
}

impl TypedefPrefix {
    fn merge(self, other: Self) -> Self {
        Self {
            words: self.words && other.words,
            pointer: self.pointer || other.pointer,
        }
    }
}