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
//! The structural pipeline over the committed C corpus.
//!
//! The corpus derives three variants from one seed and labels the clone pairs
//! among them, so this fixes what structural mode recovers in C: the copies of
//! each seed function group together, the getter the labels call a deliberate
//! non-clone is not reported, and every group that is reported is cohesive.
//!
//! The line ranges below mirror the corpus label file. They are evaluation
//! input only: identity in this tool is fingerprint-based, never positional.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::path::PathBuf;

use codehelion_core::discovery::{BuildVariant, Language, LanguageSelection};
use codehelion_core::grouping::GroupingConfig;
use codehelion_core::ir::{StructuralFrontend, SyntaxIrFile};
use codehelion_core::structural::{self, StructuralConfig, StructuralReport};
use codehelion_frontend_c::ir::CStructuralFrontend;

const CORPUS: &str = "../../corpus/synthetic/c";
const FILES: [&str; 4] = ["seed.c", "type1.c", "type2.c", "type3.c"];

/// One labelled fragment: the file it lives in and the line it starts on.
type Place = (&'static str, u32);

/// The copies of the seed's first function (`sum_even`), by start line.
const SUM_EVEN: [Place; 3] = [("seed.c", 4), ("type1.c", 5), ("type2.c", 4)];

/// The copies of the seed's second function (`max_run`), by start line.
const MAX_RUN: [Place; 4] = [
    ("seed.c", 14),
    ("type1.c", 17),
    ("type2.c", 14),
    ("type3.c", 17),
];

/// The getter the corpus labels a deliberate non-clone.
const GETTER: [Place; 2] = [("seed.c", 34), ("type2.c", 34)];

fn analyze() -> StructuralReport {
    let files: Vec<SyntaxIrFile> = FILES
        .iter()
        .map(|name| {
            let path = PathBuf::from(CORPUS).join(name);
            let text = std::fs::read_to_string(&path)
                .unwrap_or_else(|e| panic!("reading {}: {e}", path.display()));
            CStructuralFrontend.parse(&text)
        })
        .collect();
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    structural::analyze(&files, &variant, &StructuralConfig::default())
}

/// The unit index of a labelled place.
fn unit_at(report: &StructuralReport, (file, line): Place) -> usize {
    report
        .units
        .iter()
        .position(|unit| FILES[unit.file] == file && unit.start_line == line)
        .unwrap_or_else(|| panic!("no unit starts at {file}:{line}"))
}

/// The index of the group holding `unit`, if any group does.
fn group_of(report: &StructuralReport, unit: usize) -> Option<usize> {
    report
        .groups
        .groups
        .iter()
        .position(|group| group.members.contains(&unit))
}

#[test]
fn the_copies_of_a_labelled_function_are_recovered_as_one_group() {
    let report = analyze();
    for places in [&SUM_EVEN[..], &MAX_RUN[..]] {
        let units: Vec<usize> = places.iter().map(|&p| unit_at(&report, p)).collect();
        let groups: Vec<Option<usize>> = units.iter().map(|&u| group_of(&report, u)).collect();
        assert!(
            groups[0].is_some(),
            "{:?} is reported as a clone of its copies",
            places[0]
        );
        assert!(
            groups.iter().all(|found| *found == groups[0]),
            "{places:?} landed in {groups:?} instead of one group"
        );
    }
}

#[test]
fn the_getter_the_labels_call_a_non_clone_is_not_reported() {
    let report = analyze();
    for place in GETTER {
        let unit = unit_at(&report, place);
        assert_eq!(
            group_of(&report, unit),
            None,
            "{place:?} is a deliberate non-clone"
        );
    }
}

#[test]
fn every_reported_group_clears_the_cohesion_floor() {
    let report = analyze();
    let floor = GroupingConfig::default().min_pairwise_similarity;
    assert!(!report.groups.groups.is_empty(), "the corpus holds clones");
    for group in &report.groups.groups {
        assert!(
            group.min_pairwise >= floor,
            "group around {} has cohesion {:.3}",
            group.canonical,
            group.min_pairwise
        );
        assert!(group.members.len() >= 2, "a singleton is not a group");
    }
}

#[test]
fn two_runs_over_the_same_corpus_agree() {
    assert_eq!(analyze().groups.groups, analyze().groups.groups);
}

/// Two copies of one case, written the way a C test framework makes an author
/// write them, beside the production function they call.
const SUITE: &str = "\
int normalise(int value) {
    int scaled = value * 2;
    int shifted = scaled + 1;
    return shifted;
}

TEST(NormaliseSuite, DoublesAndShifts) {
    int input = 3;
    int result = normalise(input);
    ASSERT_EQ(result, 7);
    ASSERT_NE(result, 0);
}

TEST(NormaliseSuite, HandlesZero) {
    int input = 0;
    int result = normalise(input);
    ASSERT_EQ(result, 1);
    ASSERT_NE(result, 0);
}
";

#[test]
fn a_case_written_as_a_framework_macro_is_no_unit_in_c() {
    // C++ reads `MACRO(suite, name) { ... }` as a definition named after the
    // macro, which is what makes the name usable as a test marker. The C
    // grammar does not: it reads a call and then a block that belongs to
    // nobody. So a C suite contributes no units at all, and the two identical
    // cases below are not reported as duplicates of each other.
    //
    // This is pinned rather than fixed because the marker already handles the
    // case the moment a unit appears — if this assertion ever fails, the
    // grammar started producing one and the classification will be waiting.
    let files = vec![CStructuralFrontend.parse(SUITE)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    let names: Vec<Option<&str>> = report
        .units
        .iter()
        .map(|unit| unit.name.as_deref())
        .collect();
    assert_eq!(
        names,
        vec![Some("normalise")],
        "only the function is a unit"
    );
    assert!(
        report.groups.groups.is_empty(),
        "nothing in the suite reaches a group"
    );
}

/// A store written out one byte at a time, at two widths — the shape C reaches
/// for when a loop would cost more than it saves.
const UNROLLED: &str = "\
static void write_le32(void *dst, unsigned int value32)
{
    unsigned char *const p = (unsigned char *)dst;
    p[0] = (unsigned char)value32;
    p[1] = (unsigned char)(value32 >> 8);
    p[2] = (unsigned char)(value32 >> 16);
    p[3] = (unsigned char)(value32 >> 24);
}

static void write_le64(void *dst, unsigned long long value64)
{
    unsigned char *const p = (unsigned char *)dst;
    p[0] = (unsigned char)value64;
    p[1] = (unsigned char)(value64 >> 8);
    p[2] = (unsigned char)(value64 >> 16);
    p[3] = (unsigned char)(value64 >> 24);
    p[4] = (unsigned char)(value64 >> 32);
    p[5] = (unsigned char)(value64 >> 40);
    p[6] = (unsigned char)(value64 >> 48);
    p[7] = (unsigned char)(value64 >> 56);
}
";

#[test]
fn an_unrolled_run_is_not_a_clone_of_itself() {
    // Every statement of the wider store summarises like every other, so each
    // window of the run matches every shifted window of itself. Those pairs
    // are rejected one by one for covering one stretch of code rather than
    // two — and then arrive together anyway, as occurrences of one run, joined
    // by the narrower store they all match. Reported that way, the run comes
    // back as a clone of itself at four offsets.
    let files = vec![CStructuralFrontend.parse(UNROLLED)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    assert!(
        report.stats.region_overlapping > 0,
        "the shifted windows have to be recognised, not merely absent"
    );
    for region in &report.regions {
        let units: Vec<usize> = region
            .occurrences
            .iter()
            .map(|occurrence| occurrence.unit)
            .collect();
        assert!(
            units.iter().any(|unit| *unit != units[0]),
            "a run reported inside one function only: {:?}",
            region
                .occurrences
                .iter()
                .map(|occurrence| (occurrence.start_line, occurrence.end_line))
                .collect::<Vec<_>>()
        );
    }

    // What the two functions share is reported above as a cross-unit region.
    // Their whole bodies are not a group: one store is only a prefix of the
    // other, so calling them a unit clone would duplicate the region finding.
    assert!(
        report.groups.groups.is_empty(),
        "the partial duplication must not become a whole-unit group"
    );
}

/// One function written twice, once per platform, the way a portable C source
/// writes it — beside an unguarded pair that really is duplicated.
const PORTABLE: &str = "\
#ifdef _WIN32
int wait_ticks(int ms) {
    int ticks = ms * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
#else
int wait_ticks(int ms) {
    int ticks = ms * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
#endif

int scale_a(int v) {
    int ticks = v * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}

int scale_b(int v) {
    int ticks = v * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
";

#[test]
fn the_two_arms_of_one_conditional_are_not_a_clone_pair() {
    // The guarded pair is identical, so every measure agrees on it — and
    // reporting it would tell the reader to delete one of two functions only
    // one of which is ever compiled. The unguarded pair below it is the same
    // code and is reported, so the drop is about the conditional and not
    // about the similarity.
    let files = vec![CStructuralFrontend.parse(PORTABLE)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    let unit_at_line = |line: u32| {
        report
            .units
            .iter()
            .position(|unit| unit.start_line == line)
            .unwrap_or_else(|| panic!("no unit starts at line {line}"))
    };
    let (guarded, otherwise) = (unit_at_line(2), unit_at_line(9));
    let (open_a, open_b) = (unit_at_line(17), unit_at_line(24));

    // Three, not one: the funnel counts proposals, and all three candidate
    // stages propose this pair — it shares fragments, shingles and a
    // control-flow skeleton. The `nested` counter beside it counts the same
    // way.
    assert_eq!(
        report.stats.alternative_pairs, 3,
        "the guarded pair is dropped, and the funnel says so"
    );
    for group in &report.groups.groups {
        assert!(
            !(group.members.contains(&guarded) && group.members.contains(&otherwise)),
            "no group holds both arms of one conditional"
        );
    }
    assert!(
        report
            .groups
            .groups
            .iter()
            .any(|group| group.members.contains(&open_a) && group.members.contains(&open_b)),
        "the same code outside any conditional is still a clone"
    );
}

/// The same portable pair, followed by an item the parser cannot follow: the
/// trailing function is truncated, well after the conditional has closed.
const BROKEN_AFTERWARDS: &str = "\
#ifdef _WIN32
int wait_ticks(int ms) {
    int ticks = ms * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
#else
int wait_ticks(int ms) {
    int ticks = ms * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
#endif

int broken(int v) { return v +
";

/// The same portable pair with the unparsable item moved inside the first arm,
/// so error recovery is what decides where that arm ends.
const BROKEN_INSIDE: &str = "\
#ifdef _WIN32
int wait_ticks(int ms) {
    int ticks = ms * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
int broken(int v) { return v + }
#else
int wait_ticks(int ms) {
    int ticks = ms * 10;
    int capped = ticks > 1000 ? 1000 : ticks;
    int slept = capped;
    return slept;
}
#endif
";

#[test]
fn a_stumble_elsewhere_in_the_file_leaves_the_conditional_readable() {
    // Error recovery is not local to what broke: one truncated item puts an
    // error region in the file, and a header whose include guard encloses
    // everything gets one spanning all of it. Neither says anything about a
    // conditional the parser did read, and refusing that conditional would
    // report two platform variants as a clone.
    let files = vec![CStructuralFrontend.parse(BROKEN_AFTERWARDS)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    assert!(
        !files[0].error_ranges.is_empty(),
        "the fixture is meant to be a file the parser struggled with"
    );
    assert!(
        report.stats.alternative_pairs > 0,
        "the conditional itself parsed, so its arms still rule each other out"
    );
}

#[test]
fn a_stumble_inside_the_conditional_excludes_nothing() {
    // Arms are read off the tree, so an arm whose end the parser guessed at is
    // not worth reading. Dropping a pair hides a finding, so the tool would
    // rather report the platform pair than invent an exclusion.
    let files = vec![CStructuralFrontend.parse(BROKEN_INSIDE)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    assert!(
        !files[0].error_ranges.is_empty(),
        "the fixture is meant to be a file the parser struggled with"
    );
    assert_eq!(
        report.stats.alternative_pairs, 0,
        "no exclusion is claimed from an arm the parser guessed at"
    );
    // And the pair really is reported: a missed exclusion is meant to cost a
    // noisy finding, not to be lost somewhere else and look like a clean run.
    let unit_at_line = |line: u32| {
        report
            .units
            .iter()
            .position(|unit| unit.start_line == line)
            .unwrap_or_else(|| panic!("no unit starts at line {line}"))
    };
    let (guarded, otherwise) = (unit_at_line(2), unit_at_line(10));
    assert!(
        report
            .groups
            .groups
            .iter()
            .any(|group| group.members.contains(&guarded) && group.members.contains(&otherwise)),
        "the two arms are reported as the clone they measure as"
    );
}

/// Two readers written the way C requires when the callee answers through a
/// pointer, and one that computes instead of delegating.
const OUT_PARAMETER: &str = "\
static unsigned read32(const void *src)
{
    unsigned value;
    copy_bytes(&value, src, sizeof(value));
    return value;
}

static unsigned long long read64(const void *src)
{
    unsigned long long value;
    copy_bytes(&value, src, sizeof(value));
    return value;
}

static unsigned mix32(unsigned h)
{
    unsigned value = h * 31u + 7u;
    return value;
}
";

#[test]
fn a_local_the_callee_answers_through_does_not_make_a_wrapper_into_work() {
    // `unsigned value; copy_bytes(&value, ...); return value;` is one
    // delegation spelled the only way C allows when the answer comes back
    // through a pointer. Counting the local as work said this body did three
    // things, which left the commonest small C wrapper unclassified.
    use codehelion_core::boilerplate::Boilerplate;

    let files = vec![CStructuralFrontend.parse(OUT_PARAMETER)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    let category = |name: &str| {
        report
            .units
            .iter()
            .find(|unit| unit.name.as_deref() == Some(name))
            .unwrap_or_else(|| panic!("{name} is an analysed unit"))
            .boilerplate
    };
    assert_eq!(category("read32"), Some(Boilerplate::Forwarding));
    assert_eq!(category("read64"), Some(Boilerplate::Forwarding));
    // With nothing delegated there is nothing for the local to belong to, and
    // the IR cannot see that this one is filled with arithmetic.
    assert_eq!(category("mix32"), None);
}

/// A four-lane accumulator, unrolled by hand the way a hash core is.
const FOUR_LANE: &str = "\
static void accumulate(unsigned *v, const unsigned char *p)
{
    v[0] = mix(v[0], read32(p)); p += 4;
    v[1] = mix(v[1], read32(p)); p += 4;
    v[2] = mix(v[2], read32(p)); p += 4;
    v[3] = mix(v[3], read32(p)); p += 4;
}
";

#[test]
fn the_halves_of_an_unrolled_run_are_its_period_not_two_copies() {
    // The first four statements match the next four exactly, and the two
    // stretches sit end to end. Reported as a pair they say "these lines
    // duplicate the lines directly below", which sends a reader nowhere: the
    // repetition is the whole block, and the block is already in front of
    // them. Only a second site is worth pointing at.
    let files = vec![CStructuralFrontend.parse(FOUR_LANE)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    assert!(
        report.stats.region_adjoining > 0,
        "the tiling halves have to be recognised, not merely absent"
    );
    assert_eq!(
        report.regions,
        vec![],
        "one stretch of code repeating is not two instances of anything"
    );
}

/// Two predicates written per type, a teardown guarded against a null
/// pointer, and a routine that decides rather than answering.
const GUARDED: &str = "\
static int is_false(const item_t *item)
{
    if (item == NULL) { return 0; }
    return (item->type & 0xFF) == TYPE_FALSE;
}

static int is_true(const item_t *item)
{
    if (item == NULL) { return 0; }
    return (item->type & 0xFF) == TYPE_TRUE;
}

static int release(state_t *state)
{
    if (!state) { return 0; }
    free_state(state);
    return 0;
}

static int rank(int a, int b, int c)
{
    if (a) { return 1; }
    if (b) { return 2; }
    if (c) { return 3; }
    return 4;
}
";

#[test]
fn a_body_that_chooses_an_answer_is_not_a_body_that_works_one_out() {
    // One guard with an answer on each side of it is the language standing in
    // for a parameter: written once per type, every copy says the same thing.
    // Two guards are a decision table, and two tables differing in their
    // constants is duplication a reader can act on.
    use codehelion_core::boilerplate::Boilerplate;

    let files = vec![CStructuralFrontend.parse(GUARDED)];
    let variant = BuildVariant::structural(LanguageSelection::default(), Language::C);
    let report = structural::analyze(&files, &variant, &StructuralConfig::default());

    let category = |name: &str| {
        report
            .units
            .iter()
            .find(|unit| unit.name.as_deref() == Some(name))
            .unwrap_or_else(|| panic!("{name} is an analysed unit"))
            .boilerplate
    };
    assert_eq!(category("is_false"), Some(Boilerplate::GuardedDispatch));
    assert_eq!(category("is_true"), Some(Boilerplate::GuardedDispatch));
    // A guard, one thing done and a fixed answer is the same shape.
    assert_eq!(category("release"), Some(Boilerplate::GuardedDispatch));
    assert_eq!(category("rank"), None);
}