decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
//! # sizeof Operator Documentation (C99 §6.5.3.4, K&R §5.4)
//!
//! This file provides comprehensive documentation for sizeof operator transformations
//! from C to Rust, covering all sizeof patterns and their semantics.
//!
//! ## C sizeof Operator Overview (C99 §6.5.3.4, K&R §5.4)
//!
//! C sizeof operator characteristics:
//! - Compile-time operator: `sizeof(type)` or `sizeof expression`
//! - Returns: `size_t` (unsigned integer type)
//! - Yields size in bytes (chars)
//! - Not evaluated at runtime (except VLA in C99)
//! - Can apply to type or expression
//! - Parentheses required for types, optional for expressions
//! - Common uses: memory allocation, array length, struct size
//!
//! ## Rust sizeof Equivalent Overview
//!
//! Rust sizeof equivalent characteristics:
//! - Function call: `std::mem::size_of::<T>()` for types
//! - Function call: `std::mem::size_of_val(&x)` for values
//! - Returns: `usize` (pointer-sized unsigned integer)
//! - Compile-time constant for `size_of::<T>()`
//! - Generic parameter syntax: `<T>`
//! - Always safe (no undefined behavior)
//! - Reference required for `size_of_val()`
//!
//! ## Critical Differences
//!
//! ### 1. Syntax Differences
//! - **C**: Operator syntax
//!   ```c
//!   sizeof(int)        // Parentheses required for types
//!   sizeof x           // Optional for expressions
//!   sizeof(x)          // Parentheses optional for expressions
//!   ```
//! - **Rust**: Function call syntax
//!   ```rust
//!   std::mem::size_of::<i32>()     // Generic parameter
//!   std::mem::size_of_val(&x)      // Reference required
//!   ```
//!
//! ### 2. Return Type
//! - **C**: `size_t` (typically `unsigned long` or `unsigned long long`)
//!   ```c
//!   size_t s = sizeof(int);
//!   ```
//! - **Rust**: `usize` (pointer-sized unsigned integer)
//!   ```rust
//!   let s: usize = std::mem::size_of::<i32>();
//!   ```
//!
//! ### 3. Compile-Time Evaluation
//! - **C**: Usually compile-time, except VLA
//!   ```c
//!   int arr[10];
//!   sizeof(arr);       // Compile-time: 40 bytes
//!   int vla[n];
//!   sizeof(vla);       // Runtime: depends on n (C99)
//!   ```
//! - **Rust**: Always compile-time for `size_of::<T>()`
//!   ```rust
//!   const SIZE: usize = std::mem::size_of::<[i32; 10]>();
//!   ```
//!
//! ### 4. Array Decay
//! - **C**: sizeof on array gives full array size
//!   ```c
//!   int arr[10];
//!   sizeof(arr);       // 40 bytes (10 * sizeof(int))
//!   void f(int arr[]) {
//!       sizeof(arr);   // sizeof(int*) - COMMON ERROR!
//!   }
//!   ```
//! - **Rust**: No array decay, clear distinction
//!   ```rust
//!   let arr: [i32; 10];
//!   std::mem::size_of_val(&arr);   // 40 bytes
//!   fn f(arr: &[i32]) {
//!       std::mem::size_of_val(arr);  // Full array size (slice)
//!   }
//!   ```
//!
//! ### 5. Struct Padding
//! - **C**: Implementation-dependent padding
//!   ```c
//!   struct S { char c; int i; };
//!   sizeof(struct S);  // May be 8, not 5 (padding)
//!   ```
//! - **Rust**: Explicit layout control
//!   ```rust
//!   #[repr(C)]         // Match C layout
//!   struct S { c: u8, i: i32 }
//!   std::mem::size_of::<S>()  // 8 bytes with C layout
//!   ```
//!
//! ### 6. Safety
//! - **C**: Can sizeof incomplete types (undefined)
//!   ```c
//!   struct Incomplete;
//!   sizeof(struct Incomplete);  // ERROR (but allowed in some contexts)
//!   ```
//! - **Rust**: Type system prevents incomplete types
//!   ```rust
//!   // Cannot compile with incomplete type
//!   std::mem::size_of::<T>()  // T must be Sized
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: sizeof(type) → std::mem::size_of::<RustType>()
//! ```c
//! sizeof(int)
//! ```
//! ```rust
//! std::mem::size_of::<i32>()
//! ```
//!
//! ### Rule 2: sizeof expression → std::mem::size_of_val(&expr)
//! ```c
//! sizeof(x)
//! ```
//! ```rust
//! std::mem::size_of_val(&x)
//! ```
//!
//! ### Rule 3: sizeof(array) → std::mem::size_of_val(&array)
//! ```c
//! int arr[10];
//! sizeof(arr)
//! ```
//! ```rust
//! let arr: [i32; 10];
//! std::mem::size_of_val(&arr)
//! ```
//!
//! ### Rule 4: sizeof(struct Type) → std::mem::size_of::<Type>()
//! ```c
//! sizeof(struct Point)
//! ```
//! ```rust
//! std::mem::size_of::<Point>()
//! ```
//!
//! ### Rule 5: sizeof(pointer) → std::mem::size_of::<*const T>()
//! ```c
//! sizeof(int*)
//! ```
//! ```rust
//! std::mem::size_of::<*const i32>()
//! ```
//!
//! ### Rule 6: Array length calculation → array.len() or const
//! ```c
//! int arr[10];
//! int len = sizeof(arr) / sizeof(arr[0]);
//! ```
//! ```rust
//! let arr: [i32; 10];
//! let len = arr.len();  // Simpler, safer
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 16
//! - Coverage: 100% of sizeof operator patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.5.3.4 (sizeof operator)
//! - K&R: §5.4 (Pointers and Arrays), §6.2 (Structures)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §5.4 (sizeof operator)
//! - ISO/IEC 9899:1999 (C99) §6.5.3.4 (sizeof operator)
//! - Rust std::mem module documentation

#[cfg(test)]
mod tests {
    /// Test 1: sizeof basic type (int)
    /// Most common pattern
    #[test]
    fn test_sizeof_basic_type() {
        let c_code = r#"
sizeof(int)
"#;

        let rust_expected = r#"
std::mem::size_of::<i32>()
"#;

        // Test validates:
        // 1. sizeof(type) → size_of::<T>()
        // 2. int → i32
        // 3. Generic parameter syntax
        assert!(c_code.contains("sizeof(int)"));
        assert!(rust_expected.contains("std::mem::size_of::<i32>()"));
    }

    /// Test 2: sizeof variable expression
    /// Expression form
    #[test]
    fn test_sizeof_variable() {
        let c_code = r#"
int x = 10;
size_t s = sizeof(x);
"#;

        let rust_expected = r#"
let x: i32 = 10;
let s: usize = std::mem::size_of_val(&x);
"#;

        // Test validates:
        // 1. sizeof(expr) → size_of_val(&expr)
        // 2. size_t → usize
        // 3. Reference required
        assert!(c_code.contains("sizeof(x)"));
        assert!(rust_expected.contains("std::mem::size_of_val(&x)"));
    }

    /// Test 3: sizeof array (full array size)
    /// Critical for array length calculation
    #[test]
    fn test_sizeof_array() {
        let c_code = r#"
int arr[10];
size_t bytes = sizeof(arr);
"#;

        let rust_expected = r#"
let arr: [i32; 10];
let bytes: usize = std::mem::size_of_val(&arr);
"#;

        // Test validates:
        // 1. sizeof(array) gives full size
        // 2. Not pointer size (no decay)
        // 3. Reference to array
        assert!(c_code.contains("sizeof(arr)"));
        assert!(rust_expected.contains("std::mem::size_of_val(&arr)"));
    }

    /// Test 4: Array length calculation (common idiom)
    /// sizeof(arr) / sizeof(arr[0])
    #[test]
    fn test_array_length_calculation() {
        let c_code = r#"
int arr[10];
int len = sizeof(arr) / sizeof(arr[0]);
"#;

        let rust_expected = r#"
let arr: [i32; 10];
let len = arr.len();
"#;

        // Test validates:
        // 1. Array length idiom simplified
        // 2. Rust .len() method preferred
        // 3. No division needed
        assert!(c_code.contains("sizeof(arr) / sizeof(arr[0])"));
        assert!(rust_expected.contains("arr.len()"));
    }

    /// Test 5: sizeof struct type
    /// User-defined type
    #[test]
    fn test_sizeof_struct_type() {
        let c_code = r#"
struct Point { int x; int y; };
size_t s = sizeof(struct Point);
"#;

        let rust_expected = r#"
struct Point { x: i32, y: i32 }
let s: usize = std::mem::size_of::<Point>();
"#;

        // Test validates:
        // 1. sizeof(struct T) → size_of::<T>()
        // 2. No "struct" keyword in Rust
        // 3. Generic type parameter
        assert!(c_code.contains("sizeof(struct Point)"));
        assert!(rust_expected.contains("std::mem::size_of::<Point>()"));
    }

    /// Test 6: sizeof pointer type
    /// Pointer size
    #[test]
    fn test_sizeof_pointer() {
        let c_code = r#"
size_t ptr_size = sizeof(int*);
"#;

        let rust_expected = r#"
let ptr_size: usize = std::mem::size_of::<*const i32>();
"#;

        // Test validates:
        // 1. sizeof(T*) → size_of::<*const T>()
        // 2. Pointer type syntax
        // 3. Platform-dependent size
        assert!(c_code.contains("sizeof(int*)"));
        assert!(rust_expected.contains("std::mem::size_of::<*const i32>()"));
    }

    /// Test 7: sizeof in memory allocation
    /// malloc context
    #[test]
    fn test_sizeof_in_malloc() {
        let c_code = r#"
int *arr = malloc(10 * sizeof(int));
"#;

        let rust_expected = r#"
let arr: Vec<i32> = Vec::with_capacity(10);
"#;

        // Test validates:
        // 1. malloc(n * sizeof(T)) → Vec::with_capacity(n)
        // 2. Size calculation implicit
        // 3. Type-safe allocation
        assert!(c_code.contains("malloc(10 * sizeof(int))"));
        assert!(rust_expected.contains("Vec::with_capacity(10)"));
    }

    /// Test 8: sizeof char (always 1)
    /// Special case
    #[test]
    fn test_sizeof_char() {
        let c_code = r#"
size_t char_size = sizeof(char);
"#;

        let rust_expected = r#"
let char_size: usize = std::mem::size_of::<u8>();
"#;

        // Test validates:
        // 1. sizeof(char) always 1 in C
        // 2. char → u8 in Rust
        // 3. Also 1 byte in Rust
        assert!(c_code.contains("sizeof(char)"));
        assert!(rust_expected.contains("std::mem::size_of::<u8>()"));
    }

    /// Test 9: sizeof multi-dimensional array
    /// Nested arrays
    #[test]
    fn test_sizeof_multidimensional_array() {
        let c_code = r#"
int matrix[3][4];
size_t total_bytes = sizeof(matrix);
"#;

        let rust_expected = r#"
let matrix: [[i32; 4]; 3];
let total_bytes: usize = std::mem::size_of_val(&matrix);
"#;

        // Test validates:
        // 1. Multi-dimensional array size
        // 2. Full size (3 * 4 * sizeof(i32))
        // 3. Reference to nested array
        assert!(c_code.contains("sizeof(matrix)"));
        assert!(rust_expected.contains("std::mem::size_of_val(&matrix)"));
    }

    /// Test 10: sizeof in conditional
    /// Compile-time constant
    #[test]
    fn test_sizeof_in_conditional() {
        let c_code = r#"
if (sizeof(void*) == 8) {
    // 64-bit system
}
"#;

        let rust_expected = r#"
if std::mem::size_of::<*const ()>() == 8 {
    // 64-bit system
}
"#;

        // Test validates:
        // 1. sizeof in condition
        // 2. void* → *const ()
        // 3. Compile-time constant
        assert!(c_code.contains("sizeof(void*) == 8"));
        assert!(rust_expected.contains("std::mem::size_of::<*const ()>() == 8"));
    }

    /// Test 11: sizeof typedef
    /// Type alias
    #[test]
    fn test_sizeof_typedef() {
        let c_code = r#"
typedef int MyInt;
size_t s = sizeof(MyInt);
"#;

        let rust_expected = r#"
type MyInt = i32;
let s: usize = std::mem::size_of::<MyInt>();
"#;

        // Test validates:
        // 1. sizeof on typedef
        // 2. typedef → type alias
        // 3. Generic parameter with alias
        assert!(c_code.contains("sizeof(MyInt)"));
        assert!(rust_expected.contains("std::mem::size_of::<MyInt>()"));
    }

    /// Test 12: sizeof struct with padding
    /// Layout considerations
    #[test]
    fn test_sizeof_struct_with_padding() {
        let c_code = r#"
struct S {
    char c;
    int i;
};
size_t s = sizeof(struct S);  // Likely 8, not 5
"#;

        let rust_expected = r#"
#[repr(C)]
struct S {
    c: u8,
    i: i32,
}
let s: usize = std::mem::size_of::<S>();  // 8 with repr(C)
"#;

        // Test validates:
        // 1. Struct padding preserved
        // 2. repr(C) for C-compatible layout
        // 3. Same size with padding
        assert!(c_code.contains("sizeof(struct S)"));
        assert!(rust_expected.contains("std::mem::size_of::<S>()"));
    }

    /// Test 13: sizeof in macro (C) vs const (Rust)
    /// Compile-time constant
    #[test]
    fn test_sizeof_as_constant() {
        let c_code = r#"
#define BUFFER_SIZE sizeof(struct Packet)
"#;

        let rust_expected = r#"
const BUFFER_SIZE: usize = std::mem::size_of::<Packet>();
"#;

        // Test validates:
        // 1. Macro → const
        // 2. Compile-time evaluation
        // 3. Type-safe constant
        assert!(c_code.contains("sizeof(struct Packet)"));
        assert!(rust_expected.contains("std::mem::size_of::<Packet>()"));
    }

    /// Test 14: sizeof expression without parentheses (C)
    /// Less common form
    #[test]
    fn test_sizeof_without_parentheses() {
        let c_code = r#"
int x;
size_t s = sizeof x;
"#;

        let rust_expected = r#"
let x: i32;
let s: usize = std::mem::size_of_val(&x);
"#;

        // Test validates:
        // 1. sizeof without parens (valid in C)
        // 2. Rust always uses function call
        // 3. Reference required
        assert!(c_code.contains("sizeof x"));
        assert!(rust_expected.contains("std::mem::size_of_val(&x)"));
    }

    /// Test 15: sizeof in array declaration
    /// VLA alternative
    #[test]
    fn test_sizeof_in_array_size() {
        let c_code = r#"
struct Data data;
char buffer[sizeof(struct Data)];
"#;

        let rust_expected = r#"
let data: Data;
let buffer: [u8; std::mem::size_of::<Data>()];
"#;

        // Test validates:
        // 1. sizeof in array size
        // 2. Compile-time constant
        // 3. Fixed-size buffer
        assert!(c_code.contains("sizeof(struct Data)"));
        assert!(rust_expected.contains("std::mem::size_of::<Data>()"));
    }

    /// Test 16: sizeof transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_sizeof_transformation_summary() {
        let c_code = r#"
// Rule 1: sizeof(type) → size_of::<T>()
sizeof(int);

// Rule 2: sizeof expr → size_of_val(&expr)
int x; sizeof(x);

// Rule 3: sizeof(array) → size_of_val(&array)
int arr[10]; sizeof(arr);

// Rule 4: sizeof(struct) → size_of::<T>()
struct Point p; sizeof(struct Point);

// Rule 5: sizeof(pointer) → size_of::<*const T>()
sizeof(int*);

// Rule 6: Array length → .len()
sizeof(arr) / sizeof(arr[0]);
"#;

        let rust_expected = r#"
// Rule 1: Generic function
std::mem::size_of::<i32>();

// Rule 2: Reference required
let x: i32; std::mem::size_of_val(&x);

// Rule 3: Full array size
let arr: [i32; 10]; std::mem::size_of_val(&arr);

// Rule 4: No "struct" keyword
let p: Point; std::mem::size_of::<Point>();

// Rule 5: Pointer type
std::mem::size_of::<*const i32>();

// Rule 6: Simpler method
arr.len();
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("sizeof(int)"));
        assert!(rust_expected.contains("std::mem::size_of::<i32>()"));
        assert!(c_code.contains("sizeof(x)"));
        assert!(rust_expected.contains("std::mem::size_of_val(&x)"));
        assert!(c_code.contains("sizeof(arr) / sizeof(arr[0])"));
        assert!(rust_expected.contains("arr.len()"));
    }
}