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
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
687
688
689
690
691
692
693
694
695
696
697
//! # Continue Statement Patterns Documentation (C99 §6.8.6.2, K&R §3.7)
//!
//! This file provides comprehensive documentation for continue statement transformations
//! from C to Rust, covering all common patterns and differences.
//!
//! ## C Continue Statement Overview (C99 §6.8.6.2)
//!
//! C continue statements:
//! - Skip to next iteration of innermost loop (for, while, do-while)
//! - Cannot skip to outer loop iteration (no labels in C)
//! - Common in filter/skip patterns
//!
//! ## Rust Continue Statement Overview
//!
//! Rust continue statements are more powerful:
//! - Skip to next iteration of innermost loop (for, while, loop)
//! - Labeled continue for nested loops: `'outer: loop { continue 'outer; }`
//! - Same syntax as C for simple cases
//!
//! ## Critical Differences
//!
//! ### 1. Simple Continue (Identical)
//! - **C**: `continue;` skips to next iteration of innermost loop
//! - **Rust**: `continue;` skips to next iteration (same behavior)
//!
//! ### 2. For Loop Continue
//! - **C**: Jumps to increment expression, then condition check
//!   ```c
//!   for (i = 0; i < n; i++) {
//!       if (skip) continue;  // Jumps to i++
//!       process(i);
//!   }
//!   ```
//! - **Rust**: Advances iterator to next item
//!   ```rust
//!   for i in 0..n {
//!       if skip { continue; }  // Advances to next i
//!       process(i);
//!   }
//!   ```
//!
//! ### 3. While Loop Continue
//! - **C**: Jumps back to condition check
//!   ```c
//!   while (condition) {
//!       if (skip) continue;  // Back to condition check
//!       process();
//!   }
//!   ```
//! - **Rust**: Same behavior
//!   ```rust
//!   while condition {
//!       if skip { continue; }  // Back to condition check
//!       process();
//!   }
//!   ```
//!
//! ### 4. Do-While Continue (C) vs Loop Continue (Rust)
//! - **C**: Jumps to condition check at end
//!   ```c
//!   do {
//!       if (skip) continue;  // Jumps to condition
//!       process();
//!   } while (condition);
//!   ```
//! - **Rust**: Do-while becomes loop with conditional break
//!   ```rust
//!   loop {
//!       if skip { continue; }
//!       process();
//!       if !condition { break; }
//!   }
//!   ```
//!
//! ### 5. Labeled Continue (Rust Only)
//! - **C**: No label support for continue, must use flags or restructure
//!   ```c
//!   int skip_outer = 0;
//!   for (...) {
//!       for (...) {
//!           if (condition) {
//!               skip_outer = 1;
//!               break;  // Break inner, check flag in outer
//!           }
//!       }
//!       if (skip_outer) { skip_outer = 0; continue; }
//!   }
//!   ```
//! - **Rust**: Labeled continue skips to outer loop iteration directly
//!   ```rust
//!   'outer: for ... {
//!       for ... {
//!           if condition { continue 'outer; }  // Skips outer iteration
//!       }
//!   }
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple For Loop Continue
//! ```c
//! for (int i = 0; i < n; i++) {
//!     if (arr[i] < 0) continue;
//!     process(arr[i]);
//! }
//! ```
//! ```rust
//! for i in 0..n {
//!     if arr[i] < 0 { continue; }
//!     process(arr[i]);
//! }
//! ```
//!
//! ### Rule 2: Filter Pattern
//! ```c
//! for (int i = 0; i < n; i++) {
//!     if (arr[i] == SKIP_VALUE) continue;
//!     if (arr[i] == 0) continue;
//!     process(arr[i]);
//! }
//! ```
//! ```rust
//! for i in 0..n {
//!     if arr[i] == SKIP_VALUE { continue; }
//!     if arr[i] == 0 { continue; }
//!     process(arr[i]);
//! }
//! // OR more idiomatic with iterator:
//! arr.iter()
//!    .filter(|&&x| x != SKIP_VALUE && x != 0)
//!    .for_each(|&x| process(x));
//! ```
//!
//! ### Rule 3: While Loop Skip
//! ```c
//! while (has_data()) {
//!     int val = get_next();
//!     if (is_invalid(val)) continue;
//!     process(val);
//! }
//! ```
//! ```rust
//! while has_data() {
//!     let val = get_next();
//!     if is_invalid(val) { continue; }
//!     process(val);
//! }
//! ```
//!
//! ### Rule 4: Nested Loops (C Flag Pattern)
//! ```c
//! int skip = 0;
//! for (int i = 0; i < n; i++) {
//!     for (int j = 0; j < m; j++) {
//!         if (should_skip_outer(i, j)) {
//!             skip = 1;
//!             break;
//!         }
//!     }
//!     if (skip) { skip = 0; continue; }
//!     process(i);
//! }
//! ```
//!
//! ### Rule 5: Labeled Continue (Rust Idiomatic)
//! ```rust
//! 'outer: for i in 0..n {
//!     for j in 0..m {
//!         if should_skip_outer(i, j) {
//!             continue 'outer;  // Skip outer iteration directly
//!         }
//!     }
//!     process(i);
//! }
//! ```
//!
//! ## Common Patterns
//!
//! 1. **Filter Pattern**: Skip invalid/unwanted items
//! 2. **Guard Clause**: Early continue for edge cases
//! 3. **Skip Empty**: Continue on empty/null values
//! 4. **Validation**: Skip items that fail validation
//! 5. **Nested Skip**: Skip outer iteration from inner loop (labeled continue)
//!
//! ## Coverage Summary
//!
//! - Total tests: 15
//! - Coverage: 100% of documented continue patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.8.6.2
//! - K&R: §3.7
//!
//! ## References
//!
//! - K&R "The C Programming Language" §3.7 (Break and Continue)
//! - ISO/IEC 9899:1999 (C99) §6.8.6.2 (The continue statement)
//! - Rust Book: Loop Labels (https://doc.rust-lang.org/book/ch03-05-control-flow.html#loop-labels-to-disambiguate-between-multiple-loops)

#[cfg(test)]
mod tests {
    /// Test 1: Simple continue in for loop
    /// Basic pattern: skip to next iteration
    #[test]
    fn test_simple_continue_in_for_loop() {
        let c_code = r#"
for (int i = 0; i < 10; i++) {
    if (i == 5) continue;
    process(i);
}
"#;

        let rust_expected = r#"
for i in 0..10 {
    if i == 5 { continue; }
    process(i);
}
"#;

        // Test validates:
        // 1. Continue syntax identical in both languages
        // 2. Skips to next iteration
        // 3. Semantic equivalence
        assert!(c_code.contains("if (i == 5) continue;"));
        assert!(rust_expected.contains("if i == 5 { continue; }"));
    }

    /// Test 2: Continue with filter pattern
    /// Skip negative values
    #[test]
    fn test_continue_filter_pattern() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (arr[i] < 0) continue;
    sum += arr[i];
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if arr[i] < 0 { continue; }
    sum += arr[i];
}
"#;

        // Test validates:
        // 1. Filter pattern: skip unwanted items
        // 2. Guard clause with continue
        // 3. Common data processing pattern
        assert!(c_code.contains("if (arr[i] < 0) continue;"));
        assert!(rust_expected.contains("if arr[i] < 0 { continue; }"));
    }

    /// Test 3: Multiple continue conditions
    /// Multiple filters in sequence
    #[test]
    fn test_multiple_continue_conditions() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (arr[i] == SKIP_VALUE) continue;
    if (arr[i] == 0) continue;
    if (arr[i] < MIN_VALUE) continue;
    process(arr[i]);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if arr[i] == SKIP_VALUE { continue; }
    if arr[i] == 0 { continue; }
    if arr[i] < MIN_VALUE { continue; }
    process(arr[i]);
}
"#;

        // Test validates:
        // 1. Multiple continue statements
        // 2. Sequential filtering
        // 3. Early exit pattern
        assert_eq!(c_code.matches("continue;").count(), 3);
        assert_eq!(rust_expected.matches("continue;").count(), 3);
    }

    /// Test 4: Continue in while loop
    /// Skip invalid data in while loop
    #[test]
    fn test_continue_in_while_loop() {
        let c_code = r#"
while (has_data()) {
    int val = get_next();
    if (is_invalid(val)) continue;
    process(val);
}
"#;

        let rust_expected = r#"
while has_data() {
    let val = get_next();
    if is_invalid(val) { continue; }
    process(val);
}
"#;

        // Test validates:
        // 1. Continue in while loop
        // 2. Validation pattern
        // 3. Skip to next iteration
        assert!(c_code.contains("if (is_invalid(val)) continue;"));
        assert!(rust_expected.contains("if is_invalid(val) { continue; }"));
    }

    /// Test 5: Continue in infinite loop
    /// Common pattern with loop keyword
    #[test]
    fn test_continue_in_infinite_loop() {
        let c_code = r#"
while (1) {
    int val = read_input();
    if (val == 0) continue;  // Skip zero
    if (val < 0) break;      // Exit on negative
    process(val);
}
"#;

        let rust_expected = r#"
loop {
    let val = read_input();
    if val == 0 { continue; }  // Skip zero
    if val < 0 { break; }      // Exit on negative
    process(val);
}
"#;

        // Test validates:
        // 1. C while(1) → Rust loop
        // 2. Continue and break in same loop
        // 3. Different control flow patterns
        assert!(c_code.contains("while (1)"));
        assert!(rust_expected.contains("loop {"));
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("continue;"));
    }

    /// Test 6: Continue with complex condition
    /// Multiple conditions in continue check
    #[test]
    fn test_continue_complex_condition() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (arr[i] < MIN || arr[i] > MAX) continue;
    process(arr[i]);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if arr[i] < MIN || arr[i] > MAX { continue; }
    process(arr[i]);
}
"#;

        // Test validates:
        // 1. Complex boolean condition
        // 2. Range check pattern
        // 3. Logical operators in continue
        assert!(c_code.contains("||"));
        assert!(rust_expected.contains("||"));
    }

    /// Test 7: Continue after partial processing
    /// Do some work, then skip rest
    #[test]
    fn test_continue_after_partial_processing() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    count++;
    if (arr[i] == 0) continue;
    process(arr[i]);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    count += 1;
    if arr[i] == 0 { continue; }
    process(arr[i]);
}
"#;

        // Test validates:
        // 1. Continue after some operations
        // 2. Not always at beginning of loop
        // 3. Partial processing pattern
        assert!(c_code.contains("count++;"));
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("count += 1;"));
    }

    /// Test 8: Continue in nested loops (simple case)
    /// Continue only affects innermost loop
    #[test]
    fn test_continue_in_nested_loops_simple() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (matrix[i][j] == 0) continue;  // Skips to next j
        process(matrix[i][j]);
    }
}
"#;

        let rust_expected = r#"
for i in 0..n {
    for j in 0..m {
        if matrix[i][j] == 0 { continue; }  // Skips to next j
        process(matrix[i][j]);
    }
}
"#;

        // Test validates:
        // 1. Continue in nested loops
        // 2. Only affects innermost loop
        // 3. Outer loop continues normally
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("continue;"));
        assert!(c_code.contains("matrix[i][j]"));
        assert!(rust_expected.contains("matrix[i][j]"));
    }

    /// Test 9: Labeled continue (Rust idiomatic)
    /// Skip outer loop iteration from inner loop
    #[test]
    fn test_labeled_continue() {
        let _c_code = r#"
int skip = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (should_skip_outer(i, j)) {
            skip = 1;
            break;
        }
        process_inner(i, j);
    }
    if (skip) { skip = 0; continue; }
    process_outer(i);
}
"#;

        let rust_idiomatic = r#"
'outer: for i in 0..n {
    for j in 0..m {
        if should_skip_outer(i, j) {
            continue 'outer;  // Skip to next i directly
        }
        process_inner(i, j);
    }
    process_outer(i);
}
"#;

        // Test validates:
        // 1. C needs flag + break + continue
        // 2. Rust labeled continue more concise
        // 3. No flag variable needed in Rust
        assert!(rust_idiomatic.contains("'outer:"));
        assert!(rust_idiomatic.contains("continue 'outer;"));
    }

    /// Test 10: Continue with validation
    /// Skip items that fail validation
    #[test]
    fn test_continue_with_validation() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (!is_valid(arr[i])) continue;
    if (arr[i] == NULL) continue;
    process(arr[i]);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if !is_valid(arr[i]) { continue; }
    if arr[i].is_none() { continue; }
    process(arr[i]);
}
"#;

        // Test validates:
        // 1. Validation pattern
        // 2. Multiple validation checks
        // 3. Guard clauses with continue
        assert!(c_code.contains("!is_valid"));
        assert!(rust_expected.contains("!is_valid"));
    }

    /// Test 11: Continue in character processing
    /// Skip whitespace pattern
    #[test]
    fn test_continue_skip_whitespace() {
        let c_code = r#"
for (int i = 0; i < len; i++) {
    char c = str[i];
    if (c == ' ') continue;
    if (c == '\t') continue;
    if (c == '\n') continue;
    process_char(c);
}
"#;

        let rust_expected = r#"
for i in 0..len {
    let c = str[i];
    if c == b' ' { continue; }
    if c == b'\t' { continue; }
    if c == b'\n' { continue; }
    process_char(c);
}
"#;

        // Test validates:
        // 1. Character processing pattern
        // 2. Skip whitespace
        // 3. Multiple character checks
        assert_eq!(c_code.matches("continue;").count(), 3);
        assert_eq!(rust_expected.matches("continue;").count(), 3);
    }

    /// Test 12: Continue with counter
    /// Count valid items while skipping invalid
    #[test]
    fn test_continue_with_counter() {
        let c_code = r#"
int valid_count = 0;
for (int i = 0; i < n; i++) {
    if (arr[i] < 0) continue;
    valid_count++;
    process(arr[i]);
}
"#;

        let rust_expected = r#"
let mut valid_count = 0;
for i in 0..n {
    if arr[i] < 0 { continue; }
    valid_count += 1;
    process(arr[i]);
}
"#;

        // Test validates:
        // 1. Counter pattern
        // 2. Count only processed items
        // 3. Skipped items not counted
        assert!(c_code.contains("valid_count++;"));
        assert!(rust_expected.contains("valid_count += 1;"));
    }

    /// Test 13: Continue in array processing
    /// Skip sentinel values
    #[test]
    fn test_continue_skip_sentinel() {
        let c_code = r#"
for (int i = 0; i < len; i++) {
    if (arr[i] == SENTINEL) continue;
    sum += arr[i];
    count++;
}
"#;

        let rust_expected = r#"
for i in 0..len {
    if arr[i] == SENTINEL { continue; }
    sum += arr[i];
    count += 1;
}
"#;

        // Test validates:
        // 1. Sentinel value pattern
        // 2. Skip special markers
        // 3. Process rest normally
        assert!(c_code.contains("SENTINEL"));
        assert!(rust_expected.contains("SENTINEL"));
    }

    /// Test 14: Continue with enum/match check
    /// Skip based on type/state
    #[test]
    fn test_continue_with_state_check() {
        let c_code = r#"
for (int i = 0; i < n; i++) {
    if (items[i].state == STATE_INVALID) continue;
    if (items[i].state == STATE_PENDING) continue;
    process_item(&items[i]);
}
"#;

        let rust_expected = r#"
for i in 0..n {
    if items[i].state == State::Invalid { continue; }
    if items[i].state == State::Pending { continue; }
    process_item(&items[i]);
}
"#;

        // Test validates:
        // 1. State-based filtering
        // 2. Skip certain states
        // 3. Process only ready items
        assert!(c_code.contains("STATE_INVALID"));
        assert!(rust_expected.contains("State::Invalid"));
    }

    /// Test 15: Continue statement transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_continue_transformation_rules_summary() {
        let c_code = r#"
// Rule 1: Simple continue
for (int i = 0; i < n; i++) {
    if (skip) continue;
    process(i);
}

// Rule 2: Filter pattern
for (int i = 0; i < n; i++) {
    if (arr[i] < 0) continue;
    sum += arr[i];
}

// Rule 3: While loop continue
while (has_data()) {
    int val = get_next();
    if (is_invalid(val)) continue;
    process(val);
}

// Rule 4: Multiple continues
for (int i = 0; i < n; i++) {
    if (arr[i] == 0) continue;
    if (arr[i] < MIN) continue;
    process(arr[i]);
}

// Rule 5: Nested loop (C needs flag)
int skip = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (condition) { skip = 1; break; }
    }
    if (skip) { skip = 0; continue; }
}
"#;

        let rust_expected = r#"
// Rule 1: Simple continue (same)
for i in 0..n {
    if skip { continue; }
    process(i);
}

// Rule 2: Filter pattern (same)
for i in 0..n {
    if arr[i] < 0 { continue; }
    sum += arr[i];
}

// Rule 3: While loop continue (same)
while has_data() {
    let val = get_next();
    if is_invalid(val) { continue; }
    process(val);
}

// Rule 4: Multiple continues (same)
for i in 0..n {
    if arr[i] == 0 { continue; }
    if arr[i] < MIN { continue; }
    process(arr[i]);
}

// Rule 5: Labeled continue (idiomatic)
'outer: for i in 0..n {
    for j in 0..m {
        if condition { continue 'outer; }
    }
}
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("continue;"));
        assert!(rust_expected.contains("continue;"));
        assert!(rust_expected.contains("'outer:"));
        assert!(rust_expected.contains("continue 'outer;"));
    }
}