decy-core 2.2.0

Core transpilation pipeline for C-to-Rust conversion
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Popperian Falsification Test Suite for Decy C-to-Rust Transpiler
//!
//! C151-C175: Pathological C patterns - function pointers, variadic functions,
//! complex structs, dangerous pointer patterns, and memory/lifetime pathologies.
//! Tests are APPEND-ONLY per Popperian methodology.
//! Falsified tests are marked #[ignore = "FALSIFIED: reason"].
//!
//! Organization:
//! - C151-C155: Function pointer patterns
//! - C156-C160: Variadic and complex function patterns
//! - C161-C165: Complex struct patterns
//! - C166-C170: Dangerous pointer patterns
//! - C171-C175: Memory and lifetime pathologies
//!
//! Results: 24 passing, 1 falsified (96.0% pass rate)

// ============================================================================
// C151-C155: Function Pointer Patterns
// ============================================================================

#[test]
fn c151_function_pointer_typedef_and_call() {
    let c_code = r#"
typedef int (*op_fn)(int, int);
int add(int a, int b) { return a + b; }
int apply(op_fn f, int x, int y) {
    return f(x, y);
}
int main() {
    op_fn op = add;
    return apply(op, 3, 4);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C151: Function pointer typedef and call should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C151: Output should not be empty");
    assert!(code.contains("fn add"), "C151: Should contain add function");
    assert!(code.contains("fn apply"), "C151: Should contain apply function");
    assert!(code.contains("fn main"), "C151: Should contain main function");
}

#[test]
fn c152_array_of_function_pointers() {
    let c_code = r#"
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
typedef int (*binop)(int, int);
int dispatch(int op, int a, int b) {
    binop ops[3];
    ops[0] = add;
    ops[1] = sub;
    ops[2] = mul;
    if (op >= 0 && op < 3) return ops[op](a, b);
    return 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C152: Array of function pointers should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C152: Output should not be empty");
    assert!(code.contains("fn dispatch"), "C152: Should contain dispatch function");
    assert!(code.contains("fn add"), "C152: Should contain add function");
    assert!(code.contains("fn sub"), "C152: Should contain sub function");
    assert!(code.contains("fn mul"), "C152: Should contain mul function");
}

#[test]
fn c153_function_pointer_as_parameter() {
    let c_code = r#"
int apply_twice(int (*f)(int), int x) {
    return f(f(x));
}
int double_it(int n) { return n * 2; }
int main() {
    return apply_twice(double_it, 5);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C153: Function pointer as parameter should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C153: Output should not be empty");
    assert!(code.contains("fn apply_twice"), "C153: Should contain apply_twice function");
    assert!(code.contains("fn double_it"), "C153: Should contain double_it function");
}

#[test]
fn c154_function_returning_function_pointer() {
    let c_code = r#"
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
typedef int (*binop)(int, int);
binop get_op(int choice) {
    if (choice == 0) return add;
    return sub;
}
int main() {
    binop op = get_op(0);
    return op(10, 3);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C154: Function returning function pointer should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C154: Output should not be empty");
    assert!(code.contains("fn get_op"), "C154: Should contain get_op function");
    assert!(code.contains("fn main"), "C154: Should contain main function");
}

#[test]
fn c155_callback_registration_struct() {
    let c_code = r#"
typedef void (*callback_fn)(int);
struct EventHandler {
    callback_fn on_event;
    int id;
};
void default_handler(int code) {}
void register_handler(struct EventHandler *eh, callback_fn cb, int id) {
    eh->on_event = cb;
    eh->id = id;
}
void fire_event(struct EventHandler *eh, int code) {
    if (eh->on_event != 0) {
        eh->on_event(code);
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C155: Callback registration pattern should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C155: Output should not be empty");
    assert!(code.contains("fn register_handler"), "C155: Should contain register_handler function");
    assert!(code.contains("fn fire_event"), "C155: Should contain fire_event function");
    assert!(code.contains("EventHandler"), "C155: Should contain EventHandler struct");
}

// ============================================================================
// C156-C160: Variadic and Complex Function Patterns
// ============================================================================

#[test]
fn c156_variadic_function_printf_style() {
    let c_code = r#"
#include <stdarg.h>
int sum_ints(int count, ...) {
    va_list args;
    va_start(args, count);
    int total = 0;
    int i;
    for (i = 0; i < count; i++) {
        total += va_arg(args, int);
    }
    va_end(args);
    return total;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C156: Variadic function should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C156: Output should not be empty");
    assert!(code.contains("fn sum_ints"), "C156: Should contain sum_ints function");
}

#[test]
fn c157_const_and_restrict_pointers() {
    let c_code = r#"
void copy_array(int * restrict dst, const int * restrict src, int n) {
    int i;
    for (i = 0; i < n; i++) {
        dst[i] = src[i];
    }
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C157: const and restrict pointers should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C157: Output should not be empty");
    assert!(code.contains("fn copy_array"), "C157: Should contain copy_array function");
}

#[test]
fn c158_static_inline_function() {
    let c_code = r#"
static inline int max(int a, int b) {
    return a > b ? a : b;
}
int compute(int x, int y, int z) {
    return max(max(x, y), z);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C158: Static inline function should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C158: Output should not be empty");
    assert!(code.contains("fn compute"), "C158: Should contain compute function");
}

#[test]
fn c159_recursive_function_with_pointer_params() {
    let c_code = r#"
struct TreeNode {
    int value;
    struct TreeNode *left;
    struct TreeNode *right;
};
int count_nodes(struct TreeNode *root) {
    if (root == 0) return 0;
    return 1 + count_nodes(root->left) + count_nodes(root->right);
}
int find_max(struct TreeNode *root) {
    if (root == 0) return -2147483648;
    int left_max = find_max(root->left);
    int right_max = find_max(root->right);
    int m = root->value;
    if (left_max > m) m = left_max;
    if (right_max > m) m = right_max;
    return m;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C159: Recursive function with pointer params should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C159: Output should not be empty");
    assert!(code.contains("fn count_nodes"), "C159: Should contain count_nodes function");
    assert!(code.contains("fn find_max"), "C159: Should contain find_max function");
    assert!(code.contains("TreeNode"), "C159: Should contain TreeNode struct");
}

#[test]
fn c160_kr_style_function_definition() {
    let c_code = r#"
int add(a, b)
    int a;
    int b;
{
    return a + b;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C160: K&R style function definition should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C160: Output should not be empty");
    assert!(code.contains("fn add"), "C160: Should contain add function");
}

// ============================================================================
// C161-C165: Complex Struct Patterns
// ============================================================================

#[test]
fn c161_flexible_array_member() {
    let c_code = r#"
struct Buffer {
    int length;
    char data[];
};
int get_length(struct Buffer *buf) {
    return buf->length;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C161: Flexible array member should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C161: Output should not be empty");
    assert!(code.contains("fn get_length"), "C161: Should contain get_length function");
    assert!(code.contains("Buffer"), "C161: Should contain Buffer struct");
}

#[test]
fn c162_bitfield_struct() {
    let c_code = r#"
struct Flags {
    unsigned int readable : 1;
    unsigned int writable : 1;
    unsigned int executable : 1;
    unsigned int reserved : 29;
};
int is_readable(struct Flags *f) {
    return f->readable;
}
void set_writable(struct Flags *f, int val) {
    f->writable = val;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C162: Bitfield struct should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C162: Output should not be empty");
    assert!(code.contains("fn is_readable"), "C162: Should contain is_readable function");
    assert!(code.contains("fn set_writable"), "C162: Should contain set_writable function");
    assert!(code.contains("Flags"), "C162: Should contain Flags struct");
}

#[test]
fn c163_nested_anonymous_struct_union() {
    let c_code = r#"
struct Packet {
    int type;
    union {
        struct {
            int x;
            int y;
        } position;
        struct {
            int code;
            int severity;
        } error;
    };
};
int get_packet_x(struct Packet *p) {
    return p->position.x;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C163: Nested anonymous struct/union should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C163: Output should not be empty");
    assert!(code.contains("fn get_packet_x"), "C163: Should contain get_packet_x function");
    assert!(code.contains("Packet"), "C163: Should contain Packet struct");
}

#[test]
fn c164_self_referential_struct_linked_list() {
    let c_code = r#"
struct DListNode {
    int data;
    struct DListNode *prev;
    struct DListNode *next;
};
int traverse_forward(struct DListNode *head) {
    int count = 0;
    struct DListNode *cur = head;
    while (cur != 0) {
        count++;
        cur = cur->next;
    }
    return count;
}
int traverse_backward(struct DListNode *tail) {
    int count = 0;
    struct DListNode *cur = tail;
    while (cur != 0) {
        count++;
        cur = cur->prev;
    }
    return count;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C164: Self-referential struct (doubly-linked list) should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C164: Output should not be empty");
    assert!(code.contains("fn traverse_forward"), "C164: Should contain traverse_forward function");
    assert!(
        code.contains("fn traverse_backward"),
        "C164: Should contain traverse_backward function"
    );
    assert!(code.contains("DListNode"), "C164: Should contain DListNode struct");
}

#[test]
fn c165_struct_with_function_pointer_dispatch() {
    let c_code = r#"
typedef int (*method_fn)(void *, int);
struct VTable {
    method_fn process;
    method_fn validate;
};
struct Object {
    struct VTable *vtable;
    int state;
};
int call_process(struct Object *obj, int input) {
    return obj->vtable->process(obj, input);
}
int call_validate(struct Object *obj, int input) {
    return obj->vtable->validate(obj, input);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C165: Struct with function pointer dispatch should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C165: Output should not be empty");
    assert!(code.contains("fn call_process"), "C165: Should contain call_process function");
    assert!(code.contains("fn call_validate"), "C165: Should contain call_validate function");
    assert!(code.contains("VTable"), "C165: Should contain VTable struct");
    assert!(code.contains("Object"), "C165: Should contain Object struct");
}

// ============================================================================
// C166-C170: Dangerous Pointer Patterns
// ============================================================================

#[test]
fn c166_triple_pointer() {
    let c_code = r#"
int deref_triple(int ***ppp) {
    return ***ppp;
}
void set_triple(int ***ppp, int val) {
    ***ppp = val;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C166: Triple pointer should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C166: Output should not be empty");
    assert!(code.contains("fn deref_triple"), "C166: Should contain deref_triple function");
    assert!(code.contains("fn set_triple"), "C166: Should contain set_triple function");
}

#[test]
fn c167_pointer_to_array() {
    let c_code = r#"
int sum_row(int (*arr)[10], int row) {
    int sum = 0;
    int i;
    for (i = 0; i < 10; i++) {
        sum += arr[row][i];
    }
    return sum;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C167: Pointer to array should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C167: Output should not be empty");
    assert!(code.contains("fn sum_row"), "C167: Should contain sum_row function");
}

#[test]
fn c168_void_pointer_casting_chain() {
    let c_code = r#"
void swap(void *a, void *b, int size) {
    char *ca = (char *)a;
    char *cb = (char *)b;
    int i;
    for (i = 0; i < size; i++) {
        char tmp = ca[i];
        ca[i] = cb[i];
        cb[i] = tmp;
    }
}
int swap_ints(int *x, int *y) {
    swap(x, y, sizeof(int));
    return *x;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C168: Void pointer casting chain should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C168: Output should not be empty");
    assert!(code.contains("fn swap"), "C168: Should contain swap function");
    assert!(code.contains("fn swap_ints"), "C168: Should contain swap_ints function");
}

#[test]
fn c169_pointer_aliasing_violation() {
    let c_code = r#"
float type_pun(int i) {
    int *ip = &i;
    float *fp = (float *)ip;
    return *fp;
}
int alias_test(int *a, float *b) {
    *a = 1;
    *b = 2.0f;
    return *a;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C169: Pointer aliasing violation should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C169: Output should not be empty");
    assert!(code.contains("fn type_pun"), "C169: Should contain type_pun function");
    assert!(code.contains("fn alias_test"), "C169: Should contain alias_test function");
}

#[test]
fn c170_dangling_pointer_creation() {
    let c_code = r#"
int *get_dangling() {
    int local = 42;
    return &local;
}
int use_dangling() {
    int *p = get_dangling();
    return *p;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C170: Dangling pointer creation should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C170: Output should not be empty");
    assert!(code.contains("fn get_dangling"), "C170: Should contain get_dangling function");
    assert!(code.contains("fn use_dangling"), "C170: Should contain use_dangling function");
}

// ============================================================================
// C171-C175: Memory and Lifetime Pathologies
// ============================================================================

#[test]
#[ignore = "FALSIFIED: setjmp.h jmp_buf type not recognized by parser"]
fn c171_setjmp_longjmp() {
    let c_code = r#"
#include <setjmp.h>
jmp_buf jump_buffer;
int safe_divide(int a, int b) {
    if (b == 0) {
        longjmp(jump_buffer, 1);
    }
    return a / b;
}
int try_divide(int a, int b) {
    if (setjmp(jump_buffer) == 0) {
        return safe_divide(a, b);
    }
    return -1;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C171: setjmp/longjmp should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C171: Output should not be empty");
}

#[test]
fn c172_signal_handler_with_global_state() {
    let c_code = r#"
#include <signal.h>
volatile int signal_received = 0;
int signal_count = 0;
void sig_handler(int sig) {
    signal_received = 1;
    signal_count++;
}
int setup_handler() {
    signal(SIGINT, sig_handler);
    return 0;
}
int check_signal() {
    if (signal_received) {
        signal_received = 0;
        return signal_count;
    }
    return 0;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(
        result.is_ok(),
        "C172: Signal handler with global state should transpile: {:?}",
        result.err()
    );
    let code = result.unwrap();
    assert!(!code.is_empty(), "C172: Output should not be empty");
    assert!(code.contains("fn sig_handler"), "C172: Should contain sig_handler function");
    assert!(code.contains("fn setup_handler"), "C172: Should contain setup_handler function");
    assert!(code.contains("fn check_signal"), "C172: Should contain check_signal function");
}

#[test]
fn c173_thread_local_storage() {
    let c_code = r#"
__thread int tls_value = 0;
__thread int tls_initialized = 0;
int get_tls_value() {
    if (!tls_initialized) {
        tls_value = 42;
        tls_initialized = 1;
    }
    return tls_value;
}
void set_tls_value(int v) {
    tls_value = v;
    tls_initialized = 1;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C173: Thread-local storage should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C173: Output should not be empty");
    assert!(code.contains("fn get_tls_value"), "C173: Should contain get_tls_value function");
    assert!(code.contains("fn set_tls_value"), "C173: Should contain set_tls_value function");
}

#[test]
fn c174_alloca_stack_allocation() {
    let c_code = r#"
#include <alloca.h>
int sum_stack_array(int n) {
    int *arr = (int *)alloca(n * sizeof(int));
    int i;
    for (i = 0; i < n; i++) {
        arr[i] = i + 1;
    }
    int sum = 0;
    for (i = 0; i < n; i++) {
        sum += arr[i];
    }
    return sum;
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C174: alloca stack allocation should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C174: Output should not be empty");
    assert!(code.contains("fn sum_stack_array"), "C174: Should contain sum_stack_array function");
}

#[test]
fn c175_mmap_memory_mapping() {
    let c_code = r#"
#include <sys/mman.h>
#include <stdlib.h>
void *allocate_page(int size) {
    void *ptr = mmap(0, size, 3, 0x22, -1, 0);
    if (ptr == (void *)-1) return 0;
    return ptr;
}
int free_page(void *ptr, int size) {
    return munmap(ptr, size);
}
"#;
    let result = decy_core::transpile(c_code);
    assert!(result.is_ok(), "C175: mmap memory mapping should transpile: {:?}", result.err());
    let code = result.unwrap();
    assert!(!code.is_empty(), "C175: Output should not be empty");
    assert!(code.contains("fn allocate_page"), "C175: Should contain allocate_page function");
    assert!(code.contains("fn free_page"), "C175: Should contain free_page function");
}