codehelion-frontend-rust 0.1.0

Rust 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
//! The structural pipeline end to end over real parsed Rust: two verbatim
//! copies and a renamed copy of one function must land in one clone group, a
//! gapped edit joins as a Type-3 near-clone, and an unrelated function stays
//! out — exercising candidate extraction, near-match, verification and medoid
//! grouping together, with real stable fingerprints as the grouping keys.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use codehelion_core::discovery::{BuildVariant, Language, LanguageSelection};
use codehelion_core::frontend::UnitKind;
use codehelion_core::ir::{StructuralFrontend, SyntaxIrFile};
use codehelion_core::structural::{self, StructuralConfig};
use codehelion_frontend_rust::ir::RustStructuralFrontend;

const ALPHA: &str = "\
fn alpha(data: &[u32]) -> u32 {
    let mut acc = 0u32;
    let mut count = 0u32;
    for value in data {
        if *value > 10 {
            acc = acc.wrapping_add(*value);
        } else {
            acc = acc.wrapping_sub(1);
        }
        count += 1;
    }
    acc = acc.wrapping_mul(3);
    return acc + count;
}
";

// A verbatim copy of alpha under a different name (Type-1 structure).
const ALPHA_COPY: &str = "\
fn alpha_copy(data: &[u32]) -> u32 {
    let mut acc = 0u32;
    let mut count = 0u32;
    for value in data {
        if *value > 10 {
            acc = acc.wrapping_add(*value);
        } else {
            acc = acc.wrapping_sub(1);
        }
        count += 1;
    }
    acc = acc.wrapping_mul(3);
    return acc + count;
}
";

// A consistently renamed copy (Type-2 structure).
const ALPHA_RENAMED: &str = "\
fn beta(feed: &[u32]) -> u32 {
    let mut state = 3u32;
    let mut seen = 7u32;
    for item in feed {
        if *item > 99 {
            state = state.wrapping_add(*item);
        } else {
            state = state.wrapping_sub(2);
        }
        seen += 4;
    }
    state = state.wrapping_mul(8);
    return state + seen;
}
";

// A gapped edit: one extra statement (Type-3).
const ALPHA_TYPE3: &str = "\
fn gamma(data: &[u32]) -> u32 {
    let mut acc = 0u32;
    let mut count = 0u32;
    for value in data {
        if *value > 10 {
            acc = acc.wrapping_add(*value);
        } else {
            acc = acc.wrapping_sub(1);
        }
        count += 1;
    }
    acc = acc.wrapping_mul(3);
    let extra = acc ^ count;
    return acc + count + extra;
}
";

// An unrelated function.
const UNRELATED: &str = "\
fn label(name: &str) -> usize {
    let trimmed = name.trim();
    let width = trimmed.chars().count();
    for _ in 0..width {
        if width > 3 {
            return width;
        }
    }
    return width.saturating_mul(2);
}
";

fn parse_all(sources: &[&str]) -> Vec<SyntaxIrFile> {
    sources
        .iter()
        .map(|source| RustStructuralFrontend.parse(source))
        .collect()
}

fn variant() -> BuildVariant {
    BuildVariant::structural(LanguageSelection::default(), Language::C)
}

#[test]
fn verbatim_and_renamed_copies_group_together() {
    // One file per function keeps the mapping simple: unit i is file i.
    let files = parse_all(&[ALPHA, ALPHA_COPY, ALPHA_RENAMED, UNRELATED]);
    let report = structural::analyze(&files, &variant(), &StructuralConfig::default());

    assert_eq!(report.units.len(), 4, "one unit per file");
    // The three copies (units 0, 1, 2) form a single cohesive group; the
    // unrelated function (unit 3) is not in it.
    let group = report
        .groups
        .groups
        .iter()
        .find(|g| g.members.contains(&0))
        .expect("alpha is grouped with its copies");
    assert!(group.members.contains(&1), "verbatim copy joins");
    assert!(group.members.contains(&2), "renamed copy joins");
    assert!(
        !group.members.contains(&3),
        "the unrelated function stays out"
    );
    assert_eq!(report.stats.units, 4);
    assert!(report.stats.verified_pairs >= 2);

    // Every group carries a parallel detail: a stable clone id and one
    // medoid-to-member breakdown per member.
    assert_eq!(report.details.len(), report.groups.groups.len());
    let group_index = report
        .groups
        .groups
        .iter()
        .position(|g| g.members.contains(&0))
        .unwrap();
    let detail = &report.details[group_index];
    assert_eq!(
        detail.member_breakdowns.len(),
        report.groups.groups[group_index].members.len()
    );
    // The medoid entry (member 0) is a perfect self-match.
    assert!((detail.member_breakdowns[0].composite - 1.0).abs() < 1e-9);
    // Type absent in Structural mode: every breakdown leaves it unmeasured.
    assert!(
        detail
            .member_breakdowns
            .iter()
            .all(|b| b.type_similarity.is_none())
    );
}

#[test]
fn a_type3_edit_is_grouped_as_a_near_clone() {
    let files = parse_all(&[ALPHA, ALPHA_TYPE3, UNRELATED]);
    let report = structural::analyze(&files, &variant(), &StructuralConfig::default());
    let group = report
        .groups
        .groups
        .iter()
        .find(|g| g.members.contains(&0))
        .expect("alpha is grouped with its gapped edit");
    assert!(
        group.members.contains(&1),
        "the Type-3 edit joins alpha's group"
    );
    assert!(!group.members.contains(&2));
}

#[test]
fn units_carry_the_anchors_a_report_needs() {
    let files = parse_all(&[ALPHA, UNRELATED]);
    let report = structural::analyze(&files, &variant(), &StructuralConfig::default());

    let alpha = &report.units[0];
    assert_eq!(alpha.file, 0);
    assert_eq!(alpha.kind, UnitKind::Function);
    assert_eq!(alpha.name.as_deref(), Some("alpha"));
    // ALPHA's body spans its whole source, first line to last.
    assert_eq!(alpha.start_line, 1);
    assert_eq!(
        alpha.end_line,
        u32::try_from(ALPHA.lines().count()).unwrap(),
        "the unit ends on the closing brace's line"
    );
    assert!(
        alpha.token_end - alpha.token_start > 40,
        "a whole function's tokens"
    );
    assert!(alpha.range.start < alpha.range.end);

    // Anchors are reporting data only: they never enter the identity.
    let other = &report.units[1];
    assert_ne!(alpha.fingerprint, other.fingerprint);
    assert_eq!(other.file, 1);
    assert_eq!(other.start_line, 1);
}

#[test]
fn analysis_is_deterministic() {
    let files = parse_all(&[ALPHA, ALPHA_COPY, ALPHA_RENAMED, UNRELATED]);
    let first = structural::analyze(&files, &variant(), &StructuralConfig::default());
    let second = structural::analyze(&files, &variant(), &StructuralConfig::default());
    assert_eq!(first, second);
}

/// Accessors, delegating wrappers and macro runs, next to a function that
/// does real work.
const BOILERPLATE: &str = "\
struct Config {
    retries: u32,
    inner: Inner,
}

impl Config {
    fn retries(&self) -> u32 {
        self.retries
    }

    fn set_retries(&mut self, value: u32) {
        self.retries = value;
    }

    fn resolve(&self, name: &str) -> u32 {
        self.inner.resolve(name)
    }

    fn dump(&self) {
        println!(\"retries\");
        println!(\"inner\");
        println!(\"done\");
    }

    fn clamp(&self, value: u32) -> u32 {
        if value > self.retries {
            return self.retries;
        }
        value
    }

    fn reload(&self, name: &str) -> Result<u32, Error> {
        Ok(self.inner.reload(name)?)
    }
}
";

#[test]
fn boilerplate_shapes_are_classified_on_real_parsed_code() {
    use codehelion_core::boilerplate::Boilerplate;

    let files = parse_all(&[BOILERPLATE]);
    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
    };
    // A getter's body is a tail expression, a setter's is one assignment.
    assert_eq!(category("retries"), Some(Boilerplate::TrivialBody));
    assert_eq!(category("set_retries"), Some(Boilerplate::TrivialBody));
    assert_eq!(category("resolve"), Some(Boilerplate::Forwarding));
    assert_eq!(category("dump"), Some(Boilerplate::MacroRepetition));
    // Branching is behaviour, whatever else the body looks like.
    assert_eq!(category("clamp"), None);
    // `?` hands the error upwards and leaves one path through the body, so
    // the wrapper is still a wrapper.
    assert_eq!(category("reload"), Some(Boilerplate::Forwarding));
}

/// Two dump routines: nothing but macro invocations, so every occurrence of
/// any group they form is macro repetition.
const DUMP_A: &str = "\
fn dump_config(config: &Config) {
    println!(\"retries: {}\", config.retries);
    println!(\"timeout: {}\", config.timeout);
    println!(\"verbose: {}\", config.verbose);
    println!(\"backend: {}\", config.backend);
    println!(\"workers: {}\", config.workers);
    println!(\"root: {}\", config.root);
}
";

const DUMP_B: &str = "\
fn dump_limits(limits: &Limits) {
    println!(\"files: {}\", limits.files);
    println!(\"bytes: {}\", limits.bytes);
    println!(\"depth: {}\", limits.depth);
    println!(\"jobs: {}\", limits.jobs);
    println!(\"budget: {}\", limits.budget);
    println!(\"cap: {}\", limits.cap);
}
";

#[test]
fn a_group_of_macro_runs_is_classified_as_boilerplate() {
    use codehelion_core::boilerplate::Boilerplate;

    let files = parse_all(&[DUMP_A, DUMP_B, ALPHA]);
    let report = structural::analyze(&files, &variant(), &StructuralConfig::default());
    let index = report
        .groups
        .groups
        .iter()
        .position(|group| group.members.contains(&0))
        .expect("the two dump routines are clones of each other");
    assert!(report.groups.groups[index].members.contains(&1));
    assert_eq!(
        report.details[index].boilerplate,
        Some(Boilerplate::MacroRepetition),
        "every member is a run of macro invocations, so the group is"
    );

    // The function that does real work is in no boilerplate group.
    for (group, detail) in report.groups.groups.iter().zip(&report.details) {
        if group.members.contains(&2) {
            assert_eq!(detail.boilerplate, None);
        }
    }
}

/// A short routine whose whole body reappears at the head of a much longer
/// one. The shared statements seed a candidate, but the two functions are
/// nothing like each other as units.
const SHORT_HEAD: &str = "\
fn short_head(seed: u32) -> u32 {
    let mut acc = seed;
    let mut count = 0u32;
    acc = acc.wrapping_add(7);
    count = count.wrapping_add(3);
    acc = acc.wrapping_mul(5);
    count = count.wrapping_mul(2);
    return acc + count;
}
";

const LONG_BODY: &str = "\
fn long_body(seed: u32, data: &[u32]) -> u32 {
    let mut acc = seed;
    let mut count = 0u32;
    acc = acc.wrapping_add(7);
    count = count.wrapping_add(3);
    acc = acc.wrapping_mul(5);
    count = count.wrapping_mul(2);
    for value in data {
        if *value > 10 {
            acc = acc.wrapping_add(*value);
        } else if *value > 5 {
            acc = acc.wrapping_sub(1);
        } else {
            acc = acc ^ *value;
        }
        count += 1;
    }
    while count > 0 {
        count -= 1;
        acc = acc.rotate_left(1);
    }
    let mut index = 0u32;
    loop {
        index += 1;
        if index > 4 {
            break;
        }
        acc = acc.wrapping_add(index);
    }
    for step in 0..8u32 {
        if step % 2 == 0 {
            acc = acc.wrapping_sub(step);
        } else {
            count = count.wrapping_add(step);
        }
    }
    return acc + count + index;
}
";

#[test]
fn a_pair_too_far_apart_in_shape_never_reaches_verification() {
    let files = parse_all(&[SHORT_HEAD, LONG_BODY]);
    let report = structural::analyze(&files, &variant(), &StructuralConfig::default());

    assert!(
        report.stats.divergent_shape_pairs >= 1,
        "the shared statements propose the pair, and the shape mixes drop it"
    );
    assert!(
        !report
            .groups
            .groups
            .iter()
            .any(|group| group.members.contains(&0) && group.members.contains(&1)),
        "a unit and one five times its size are not copies of each other"
    );
}

#[test]
fn dropping_a_divergent_pair_leaves_the_shared_run_reported() {
    // The filter withdraws the claim "these two functions are copies", not
    // the claim "this stretch of statements appears in both".
    let files = parse_all(&[SHORT_HEAD, LONG_BODY]);
    let report = structural::analyze(&files, &variant(), &StructuralConfig::default());

    assert!(
        report.regions.iter().any(|region| {
            region.occurrences.iter().any(|o| o.file == 0)
                && region.occurrences.iter().any(|o| o.file == 1)
        }),
        "the statements the two share are still reported as a duplicated run"
    );
}