codehelion-frontend-c 0.1.0

C Fast-mode lexer and unit-boundary frontend for the codehelion source-audit tool.
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
//! Coarse unit-boundary detection over a C-family token stream.
//!
//! Boundaries are the clone-report anchors: function definitions, methods
//! (functions inside a record body), record bodies (`struct`/`union`/`class`)
//! and, for C++, block-bodied lambdas. Detection is a delimiter-matching
//! heuristic over tokens, not a parse; in Fast mode no syntax tree is built
//! and the preprocessor never runs.
//!
//! A function definition is recognised from its body brace: walking backwards
//! over signature-trailer tokens (`const`, `noexcept`, a trailing return
//! type, ...) must reach the parameter list's `)`, whose matching `(` must be
//! preceded by the function's name. Constructor initialiser lists are walked
//! through entry by entry. The heuristic is deliberately conservative:
//! constructs a lexer cannot classify with confidence (K&R definitions,
//! function-try-blocks, functions returning function pointers) are left
//! un-anchored rather than risking spurious units.

use std::collections::HashMap;

use codehelion_core::frontend::{
    Diagnostic, DiagnosticKind, SourceSpan, Token, TokenKind, Unit, UnitKind,
};

use crate::dialect::Dialect;

/// Keywords that may appear between a parameter list and the body brace
/// (qualifiers and trailing-return-type material).
const TRAILER_KEYWORDS: &[&str] = &[
    "const", "volatile", "noexcept", "throw", "mutable", "auto", "decltype", "unsigned", "signed",
    "long", "short", "int", "char", "float", "double", "bool", "void", "typename", "restrict",
    "_Atomic",
];

/// Punctuation that may appear between a parameter list and the body brace.
const TRAILER_PUNCT: &[&str] = &["::", "<", ">", ">>", "*", "&", "&&", "->"];

/// Tokens that may immediately precede a lambda's `[` (expression position).
const LAMBDA_PRECEDER_PUNCT: &[&str] = &[
    "=", "(", ",", "{", ";", ":", "?", "&&", "||", "!", "<", ">", "<<", ">>", "+", "-", "*", "/",
    "%",
];

/// Keywords that may immediately precede a lambda's `[`.
const LAMBDA_PRECEDER_KEYWORDS: &[&str] = &[
    "return",
    "co_return",
    "co_yield",
    "co_await",
    "case",
    "else",
    "do",
];

/// Keywords whose parenthesised group belongs to the signature trailer
/// (`noexcept(...)`, `throw()`, `decltype(...)`), not to the parameter list.
const TRAILER_GROUP_KEYWORDS: &[&str] = &["noexcept", "throw", "decltype"];

/// Keywords allowed inside a lambda's forward trailer (specifiers and
/// trailing-return-type material between `)` and `{`).
const LAMBDA_TRAILER_KEYWORDS: &[&str] = &[
    "mutable",
    "noexcept",
    "constexpr",
    "consteval",
    "static",
    "throw",
    "decltype",
    "auto",
    "const",
    "unsigned",
    "signed",
    "long",
    "short",
    "int",
    "char",
    "float",
    "double",
    "bool",
    "void",
    "typename",
];

/// Maximum tokens one record-declaration lookahead may inspect before
/// declining an uncertain boundary. This keeps malformed declarations from
/// making every record keyword scan the rest of the file.
const MAX_DECLARATION_LOOKAHEAD: usize = 256;

/// Matched delimiter pairs of every kind (`()`, `{}`, `[]`), in both
/// directions.
struct DelimPairs {
    /// Opening token index -> closing token index.
    close_of: HashMap<usize, usize>,
    /// Closing token index -> opening token index.
    open_of: HashMap<usize, usize>,
}

fn delim_pairs(tokens: &[Token]) -> DelimPairs {
    let mut close_of = HashMap::new();
    let mut open_of = HashMap::new();
    let mut parens = Vec::new();
    let mut braces = Vec::new();
    let mut brackets = Vec::new();
    for (i, token) in tokens.iter().enumerate() {
        if token.kind != TokenKind::Punctuation {
            continue;
        }
        let (stack, closing) = match token.text.as_str() {
            "(" | "{" | "[" => {
                match token.text.as_str() {
                    "(" => parens.push(i),
                    "{" => braces.push(i),
                    _ => brackets.push(i),
                }
                continue;
            }
            ")" => (&mut parens, i),
            "}" => (&mut braces, i),
            "]" => (&mut brackets, i),
            _ => continue,
        };
        if let Some(open) = stack.pop() {
            close_of.insert(open, closing);
            open_of.insert(closing, open);
        }
    }
    DelimPairs { close_of, open_of }
}

/// Detect unit boundaries and recoverable boundary errors under `dialect`.
#[must_use]
pub fn detect(tokens: &[Token], dialect: &Dialect) -> (Vec<Unit>, Vec<Diagnostic>) {
    let pairs = delim_pairs(tokens);
    let records = record_units(tokens, &pairs, dialect);

    let mut units = records.clone();
    let mut diagnostics = Vec::new();
    for (i, token) in tokens.iter().enumerate() {
        if token.kind == TokenKind::Punctuation && token.text == "{" {
            if let Some(result) = function_unit(tokens, &pairs, &records, i) {
                match result {
                    Ok(unit) => units.push(unit),
                    Err(span) => diagnostics.push(Diagnostic {
                        kind: DiagnosticKind::UnmatchedDelimiter,
                        span,
                    }),
                }
            }
        }
        if dialect.lambdas && token.kind == TokenKind::Punctuation && token.text == "[" {
            if let Some(unit) = lambda_unit(tokens, &pairs, i) {
                units.push(unit);
            }
        }
    }

    units.sort_by_key(|u| (u.token_start, u.token_end));
    (units, diagnostics)
}

/// Record bodies: `struct`/`union` (and for C++ `class`) definitions.
fn record_units(tokens: &[Token], pairs: &DelimPairs, dialect: &Dialect) -> Vec<Unit> {
    let mut out = Vec::new();
    for (i, token) in tokens.iter().enumerate() {
        if token.kind != TokenKind::Keyword
            || !dialect.record_keywords.contains(&token.text.as_str())
        {
            continue;
        }
        if let Some(prev) = i.checked_sub(1).map(|p| &tokens[p]) {
            // `template<class T>` parameters and `enum class` are not records.
            if prev.kind == TokenKind::Punctuation && matches!(prev.text.as_str(), "<" | ",") {
                continue;
            }
            if prev.kind == TokenKind::Keyword && prev.text == "enum" {
                continue;
            }
        }
        let Some(open) = record_body_open(tokens, i + 1) else {
            continue;
        };
        let Some(&close) = pairs.close_of.get(&open) else {
            continue;
        };
        let name = tokens[i + 1..open]
            .iter()
            .find(|t| t.kind == TokenKind::Identifier)
            .map(|t| t.text.to_string());
        out.push(Unit {
            kind: UnitKind::Record,
            name,
            token_start: i,
            token_end: close + 1,
            span: span_of(tokens, i, close),
        });
    }
    out
}

/// Index of the `{` opening a record body declared at `from`, if any.
///
/// A `;` means a forward declaration or a variable of record type; a paren
/// means the keyword is part of a declarator or expression (`struct Foo
/// *make(void)`, `sizeof(struct S)`), not a definition.
fn record_body_open(tokens: &[Token], from: usize) -> Option<usize> {
    for (offset, token) in tokens[from..]
        .iter()
        .take(MAX_DECLARATION_LOOKAHEAD)
        .enumerate()
    {
        if token.kind == TokenKind::Punctuation {
            match token.text.as_str() {
                "{" => return Some(from + offset),
                ";" | "(" | ")" | "=" => return None,
                _ => {}
            }
        }
    }
    None
}

/// Try to recognise the `{` at `body_open` as a function body.
fn function_unit(
    tokens: &[Token],
    pairs: &DelimPairs,
    records: &[Unit],
    body_open: usize,
) -> Option<Result<Unit, SourceSpan>> {
    // Walk backwards over the signature trailer to the parameter list.
    let mut j = body_open.checked_sub(1)?;
    for _ in 0..64 {
        let token = &tokens[j];
        match token.kind {
            TokenKind::Identifier => j = j.checked_sub(1)?,
            TokenKind::Keyword if TRAILER_KEYWORDS.contains(&token.text.as_str()) => {
                j = j.checked_sub(1)?;
            }
            TokenKind::Punctuation if TRAILER_PUNCT.contains(&token.text.as_str()) => {
                j = j.checked_sub(1)?;
            }
            TokenKind::Punctuation if token.text == ")" => {
                let &open = pairs.open_of.get(&j)?;
                // `noexcept(...)` / `throw()` / `decltype(...)` groups belong
                // to the trailer; skip them whole.
                if let Some(before) = open.checked_sub(1) {
                    let b = &tokens[before];
                    if b.kind == TokenKind::Keyword
                        && TRAILER_GROUP_KEYWORDS.contains(&b.text.as_str())
                    {
                        j = before.checked_sub(1)?;
                        continue;
                    }
                }
                return resolve_signature(tokens, pairs, records, j, body_open);
            }
            // A `}` here can only be the last constructor-initialiser entry
            // (`: a(x), b{y} {`); the resolver walks the entries.
            TokenKind::Punctuation if token.text == "}" => {
                return resolve_signature(tokens, pairs, records, j, body_open);
            }
            _ => return None,
        }
    }
    None
}

/// Resolve the group closing at `close` back to the parameter list and the
/// function name, walking constructor-initialiser entries in between.
fn resolve_signature(
    tokens: &[Token],
    pairs: &DelimPairs,
    records: &[Unit],
    close: usize,
    body_open: usize,
) -> Option<Result<Unit, SourceSpan>> {
    let mut close = close;
    for _ in 0..32 {
        let &open = pairs.open_of.get(&close)?;
        let name_i = open.checked_sub(1)?;
        let name_token = &tokens[name_i];

        if name_token.kind == TokenKind::Identifier {
            if let Some(sep_i) = name_i.checked_sub(1) {
                let sep = &tokens[sep_i];
                if sep.kind == TokenKind::Punctuation && matches!(sep.text.as_str(), ":" | ",") {
                    // A constructor-initialiser entry (`name(...)` or
                    // `name{...}`); the group before the separator is the
                    // previous entry or the parameter list itself.
                    let prev_i = sep_i.checked_sub(1)?;
                    let prev = &tokens[prev_i];
                    if prev.kind == TokenKind::Punctuation
                        && matches!(prev.text.as_str(), ")" | "}")
                    {
                        close = prev_i;
                        continue;
                    }
                    return None;
                }
            }
            // The parameter list itself must be parenthesised.
            if tokens[close].text != ")" {
                return None;
            }
            // A bare `MACRO(args) { ... }` is a block-bodied macro
            // invocation, not a C/C++ function definition. Apply this only
            // after walking constructor-initialiser entries back to the
            // actual parameter list.
            let inside_record = records
                .iter()
                .any(|record| record.token_start < name_i && name_i < record.token_end);
            if !inside_record && !has_declaration_prefix(tokens, name_i) {
                return None;
            }
            let tilde = name_i
                .checked_sub(1)
                .is_some_and(|p| tokens[p].kind == TokenKind::Punctuation && tokens[p].text == "~");
            let unit_start = if tilde { name_i - 1 } else { name_i };
            return Some(make_function(
                tokens,
                pairs,
                records,
                unit_start,
                name_i,
                name_token.text.to_string(),
                body_open,
            ));
        }

        // Operator overloads: `operator` plus symbol tokens directly before
        // the parameter list (`operator+`, `operator()`, `operator bool`).
        if tokens[close].text == ")" {
            for back in 1..=3 {
                let Some(k) = open.checked_sub(back) else {
                    break;
                };
                if tokens[k].kind == TokenKind::Keyword && tokens[k].text == "operator" {
                    return Some(make_function(
                        tokens,
                        pairs,
                        records,
                        k,
                        k,
                        "operator".to_string(),
                        body_open,
                    ));
                }
            }
        }
        return None;
    }
    None
}

/// Whether tokens before a candidate name can introduce a function declarator.
///
/// This deliberately only rejects the unqualified, bare identifier form. It
/// keeps user-defined return types, pointer/reference declarators, qualified
/// C++ names and destructors available to the conservative boundary finder.
fn has_declaration_prefix(tokens: &[Token], name_i: usize) -> bool {
    name_i.checked_sub(1).is_some_and(|previous| {
        let token = &tokens[previous];
        matches!(token.kind, TokenKind::Identifier | TokenKind::Keyword)
            || (token.kind == TokenKind::Punctuation
                && matches!(token.text.as_str(), "*" | "&" | "&&" | "::" | "~"))
    })
}

fn make_function(
    tokens: &[Token],
    pairs: &DelimPairs,
    records: &[Unit],
    unit_start: usize,
    name_i: usize,
    name: String,
    body_open: usize,
) -> Result<Unit, SourceSpan> {
    let end = pairs.close_of.get(&body_open).copied().ok_or_else(|| {
        // Do not turn a malformed body into a function that extends to EOF.
        // The caller records this at the Fast frontend's ordinary diagnostic
        // boundary, where scan summaries already count recovery events.
        span_of(tokens, body_open, body_open)
    })?;
    let inside_record = records
        .iter()
        .any(|r| r.token_start < name_i && name_i < r.token_end);
    let kind = if inside_record {
        UnitKind::Method
    } else {
        UnitKind::Function
    };
    Ok(Unit {
        kind,
        name: Some(name),
        token_start: unit_start,
        token_end: end + 1,
        span: span_of(tokens, unit_start, end),
    })
}

/// Try to recognise the `[` at `i` as the start of a block-bodied lambda.
fn lambda_unit(tokens: &[Token], pairs: &DelimPairs, i: usize) -> Option<Unit> {
    if let Some(prev) = i.checked_sub(1).map(|p| &tokens[p]) {
        let allowed = match prev.kind {
            TokenKind::Punctuation => LAMBDA_PRECEDER_PUNCT.contains(&prev.text.as_str()),
            TokenKind::Keyword => LAMBDA_PRECEDER_KEYWORDS.contains(&prev.text.as_str()),
            _ => false,
        };
        if !allowed {
            return None;
        }
    }
    let &capture_close = pairs.close_of.get(&i)?;

    // Optional parameter list.
    let mut k = capture_close + 1;
    if tokens.get(k).is_some_and(|t| t.text == "(") {
        k = pairs.close_of.get(&k)? + 1;
    }

    // Forward trailer: specifiers and a trailing return type, then the body.
    for _ in 0..32 {
        let token = tokens.get(k)?;
        match token.kind {
            TokenKind::Punctuation if token.text == "{" => {
                let &close = pairs.close_of.get(&k)?;
                return Some(Unit {
                    kind: UnitKind::Closure,
                    name: None,
                    token_start: i,
                    token_end: close + 1,
                    span: span_of(tokens, i, close),
                });
            }
            TokenKind::Punctuation if TRAILER_PUNCT.contains(&token.text.as_str()) => k += 1,
            TokenKind::Punctuation if token.text == "(" => {
                // `noexcept(...)` or a parenthesised return-type part.
                k = pairs.close_of.get(&k)? + 1;
            }
            TokenKind::Identifier => k += 1,
            TokenKind::Keyword if LAMBDA_TRAILER_KEYWORDS.contains(&token.text.as_str()) => k += 1,
            _ => return None,
        }
    }
    None
}

/// Build a reporting span covering the inclusive token range `[start, end]`.
fn span_of(tokens: &[Token], start: usize, end: usize) -> SourceSpan {
    let first = tokens[start].span;
    let last = tokens[end].span;
    SourceSpan {
        start_byte: first.start_byte,
        end_byte: last.end_byte,
        start_line: first.start_line,
        start_column: first.start_column,
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::dialect;
    use crate::lexer::lex;

    fn units_of(source: &str) -> Vec<Unit> {
        detect(&lex(source, &dialect::C).0, &dialect::C).0
    }

    #[test]
    fn detects_a_free_function() {
        let units = units_of("int add(int a, int b) { return a + b; }");
        assert_eq!(units.len(), 1);
        assert_eq!(units[0].kind, UnitKind::Function);
        assert_eq!(units[0].name.as_deref(), Some("add"));
    }

    #[test]
    fn prototypes_are_not_units() {
        assert!(units_of("int add(int a, int b);").is_empty());
        assert!(units_of("extern void log_msg(const char *fmt, ...);").is_empty());
    }

    #[test]
    fn control_flow_braces_are_not_functions() {
        let src = "void f(int n) { if (n) { g(); } while (n--) { h(); } \
                   for (;;) { break; } switch (n) { default: break; } do { i(); } while (0); }";
        let units = units_of(src);
        assert_eq!(units.len(), 1, "only `f` itself: {units:#?}");
        assert_eq!(units[0].name.as_deref(), Some("f"));
    }

    #[test]
    fn pointer_returning_and_static_functions_are_detected() {
        let units = units_of("static const char *dup(const char *s) { return s; }");
        assert_eq!(units.len(), 1);
        assert_eq!(units[0].kind, UnitKind::Function);
        assert_eq!(units[0].name.as_deref(), Some("dup"));
    }

    #[test]
    fn struct_definitions_are_records_but_declarators_are_not() {
        let units = units_of("struct point { int x; int y; };");
        assert_eq!(units.len(), 1);
        assert_eq!(units[0].kind, UnitKind::Record);
        assert_eq!(units[0].name.as_deref(), Some("point"));

        // `struct` in a return type must not produce a record unit.
        let units = units_of("struct point *make(void) { return 0; }");
        assert_eq!(units.len(), 1, "{units:#?}");
        assert_eq!(units[0].kind, UnitKind::Function);
        assert_eq!(units[0].name.as_deref(), Some("make"));
    }

    #[test]
    fn anonymous_typedef_struct_is_a_record_without_a_name() {
        let units = units_of("typedef struct { int a; } pair;");
        assert_eq!(units.len(), 1);
        assert_eq!(units[0].kind, UnitKind::Record);
        // The first identifier before the brace names the record; an
        // anonymous struct has none.
        assert_eq!(units[0].name, None);
    }

    #[test]
    fn function_like_macro_bodies_do_not_produce_units() {
        // The whole definition is a directive, dropped before unit detection.
        let units = units_of("#define ADD(a, b) ((a) + (b))\n");
        assert!(units.is_empty());
    }

    #[test]
    fn block_bodied_macro_invocations_are_not_function_units() {
        for invocation in [
            "TEST_F(QueueTest, Pushes) { ASSERT_TRUE(1); }",
            "list_for_each(node, head) { visit(node); }",
            "TAILQ_FOREACH(entry, queue, links) { consume(entry); }",
        ] {
            assert!(units_of(invocation).is_empty(), "{invocation}");
        }
    }

    #[test]
    fn initializer_braces_are_not_functions() {
        assert!(units_of("int a[] = {1, 2, 3};").is_empty());
        assert!(
            units_of("struct p q = {1, 2};")
                .iter()
                .all(|u| u.kind != UnitKind::Function)
        );
    }

    #[test]
    fn a_units_token_range_covers_its_body() {
        let src = "int f(void) { return 1; }";
        let tokens = lex(src, &dialect::C).0;
        let (units, diagnostics) = detect(&tokens, &dialect::C);
        assert!(diagnostics.is_empty());
        let f = &units[0];
        assert_eq!(tokens[f.token_end - 1].text, "}");
        assert_eq!(tokens[f.token_start].text, "f");
    }

    #[test]
    fn record_declaration_lookahead_is_bounded() {
        let source = format!(
            "struct {} {{ int value; }};",
            "field ".repeat(MAX_DECLARATION_LOOKAHEAD)
        );
        let tokens = lex(&source, &dialect::C).0;
        assert_eq!(record_body_open(&tokens, 1), None);
    }

    #[test]
    fn an_unclosed_function_body_is_not_stretched_to_end_of_file() {
        let tokens = lex("int tail(void) { int value = 1;", &dialect::C).0;
        let (units, diagnostics) = detect(&tokens, &dialect::C);

        assert!(units.is_empty());
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].kind, DiagnosticKind::UnmatchedDelimiter);
    }
}