tldr-cli 0.1.6

CLI binary for TLDR code analysis 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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
//! L2 API Validation Tests for Rust Source Code
//!
//! Hypothesis H1: "tldr_core IR APIs (CFG, DFG, SSA, abstract interpretation,
//! taint) produce correct, usable results when called on Rust source code."
//!
//! The CK-10 finding from the premortem flagged that 23 APIs are signature-
//! verified but semantically unvalidated for Rust. This test file calls each
//! API with REAL Rust code snippets and verifies the results are structurally
//! correct and usable -- not garbage.
//!
//! Run with:
//!   cargo test -p tldr-cli -- l2_api_validation --nocapture
//!
//! For each API the verdict is one of:
//!   WORKS     - Produces correct, usable output for Rust
//!   DEGRADED  - Produces output but with limitations
//!   BROKEN    - Fails or produces garbage
//!   MISSING   - API does not exist or cannot be called

use std::collections::HashMap;
use tldr_core::cfg::get_cfg_context;
use tldr_core::dataflow::{compute_abstract_interp, compute_available_exprs};
use tldr_core::dfg::gvn::compute_gvn;
use tldr_core::dfg::{compute_reaching_definitions, get_dfg_context};
use tldr_core::security::compute_taint;
use tldr_core::ssa::{construct_ssa, SsaType};
use tldr_core::Language;

// =============================================================================
// Test Fixtures: Rust source code snippets
// =============================================================================

/// Simple function with if/else branching
const RUST_SIMPLE_IF: &str = r#"
fn add(a: i32, b: i32) -> i32 {
    if a > 0 {
        a + b
    } else {
        b
    }
}
"#;

/// Function with match arms
const RUST_MATCH: &str = r#"
fn classify(x: i32) -> &'static str {
    match x {
        0 => "zero",
        1..=9 => "small",
        10..=99 => "medium",
        _ => "large",
    }
}
"#;

/// Function with if-let chain
const RUST_IF_LET: &str = r#"
fn extract_value(opt: Option<i32>) -> i32 {
    if let Some(v) = opt {
        v + 1
    } else {
        0
    }
}
"#;

/// Function with a closure
const RUST_CLOSURE: &str = r#"
fn apply_twice(x: i32) -> i32 {
    let double = |n: i32| n * 2;
    let result = double(x);
    double(result)
}
"#;

/// Function with variable shadowing
const RUST_SHADOWING: &str = r#"
fn shadow(x: i32) -> i32 {
    let y = x + 1;
    let y = y * 2;
    let y = y - 3;
    y
}
"#;

/// Function with pattern matching in let
const RUST_PATTERN_LET: &str = r#"
fn destructure(pair: (i32, i32)) -> i32 {
    let (a, b) = pair;
    a + b
}
"#;

/// Function with a loop and counter (for abstract interpretation)
const RUST_LOOP_COUNTER: &str = r#"
fn count_up(n: i32) -> i32 {
    let mut total = 0;
    let mut i = 0;
    while i < n {
        total = total + i;
        i = i + 1;
    }
    total
}
"#;

/// Function with taint: reads env var and passes to Command
const RUST_TAINT: &str = r#"
fn run_user_command() {
    let user_input = std::env::var("CMD").unwrap();
    let output = Command::new(user_input);
}
"#;

/// Constant propagation candidate (for SCCP)
const RUST_CONST_PROP: &str = r#"
fn constants() -> i32 {
    let x = 10;
    let y = 20;
    let z = x + y;
    z
}
"#;

/// Two similar functions for clone detection
const RUST_CLONE_PAIR_A: &str = r#"
fn compute_area(width: f64, height: f64) -> f64 {
    let area = width * height;
    let perimeter = 2.0 * (width + height);
    let ratio = area / perimeter;
    if ratio > 1.0 {
        area
    } else {
        perimeter
    }
}
"#;

const RUST_CLONE_PAIR_B: &str = r#"
fn compute_volume(length: f64, depth: f64) -> f64 {
    let volume = length * depth;
    let surface = 2.0 * (length + depth);
    let ratio = volume / surface;
    if ratio > 1.0 {
        volume
    } else {
        surface
    }
}
"#;

/// Function with nested match for complex CFG
const RUST_NESTED_MATCH: &str = r#"
fn process(x: i32, y: i32) -> i32 {
    match x {
        0 => match y {
            0 => 0,
            _ => y,
        },
        1 => x + y,
        _ => x * y,
    }
}
"#;

/// Function with while-let for Rust-specific control flow
const RUST_WHILE_LET: &str = r#"
fn drain_iter(values: &mut Vec<i32>) -> i32 {
    let mut sum = 0;
    while let Some(v) = values.pop() {
        sum = sum + v;
    }
    sum
}
"#;

/// Function with multiple variable assignments for reaching defs
const RUST_REACHING_DEFS: &str = r#"
fn multi_assign(flag: bool) -> i32 {
    let mut x = 1;
    if flag {
        x = 2;
    }
    let y = x + 10;
    y
}
"#;

// =============================================================================
// API 1: get_cfg_context -- Control Flow Graph
// =============================================================================

#[test]
fn cfg_works_for_rust_simple_function() {
    let result = get_cfg_context(RUST_SIMPLE_IF, "add", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust simple function: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "add", "Function name should be 'add'");
    // A simple if/else should produce at least 3 blocks: entry, if-true, if-false
    // (or an entry + branches + merge pattern)
    assert!(
        !cfg.blocks.is_empty(),
        "CFG should have at least one block, got 0"
    );
    assert!(
        !cfg.edges.is_empty(),
        "CFG should have edges for branching, got 0"
    );
    println!(
        "[CFG simple] blocks={}, edges={}, cyclomatic={}",
        cfg.blocks.len(),
        cfg.edges.len(),
        cfg.cyclomatic_complexity
    );
}

#[test]
fn cfg_works_for_rust_match_arms() {
    let result = get_cfg_context(RUST_MATCH, "classify", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust match: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "classify");
    // match with 4 arms should create multiple blocks
    assert!(
        cfg.blocks.len() >= 2,
        "Match with 4 arms should have multiple blocks, got {}",
        cfg.blocks.len()
    );
    println!(
        "[CFG match] blocks={}, edges={}, cyclomatic={}",
        cfg.blocks.len(),
        cfg.edges.len(),
        cfg.cyclomatic_complexity
    );
}

#[test]
fn cfg_works_for_rust_if_let() {
    let result = get_cfg_context(RUST_IF_LET, "extract_value", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust if-let: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "extract_value");
    assert!(
        !cfg.blocks.is_empty(),
        "If-let should produce blocks, got 0"
    );
    println!(
        "[CFG if-let] blocks={}, edges={}, cyclomatic={}",
        cfg.blocks.len(),
        cfg.edges.len(),
        cfg.cyclomatic_complexity
    );
}

#[test]
fn cfg_works_for_rust_closure() {
    let result = get_cfg_context(RUST_CLOSURE, "apply_twice", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust closure: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "apply_twice");
    // Even with closures, the outer function should have a valid CFG
    assert!(
        !cfg.blocks.is_empty(),
        "Function with closure should have blocks"
    );
    println!(
        "[CFG closure] blocks={}, edges={}",
        cfg.blocks.len(),
        cfg.edges.len()
    );
}

#[test]
fn cfg_works_for_rust_nested_match() {
    let result = get_cfg_context(RUST_NESTED_MATCH, "process", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust nested match: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "process");
    // Nested match should produce a complex CFG
    assert!(
        cfg.blocks.len() >= 2,
        "Nested match should have multiple blocks, got {}",
        cfg.blocks.len()
    );
    println!(
        "[CFG nested_match] blocks={}, edges={}, cyclomatic={}",
        cfg.blocks.len(),
        cfg.edges.len(),
        cfg.cyclomatic_complexity
    );
}

#[test]
fn cfg_works_for_rust_while_let() {
    let result = get_cfg_context(RUST_WHILE_LET, "drain_iter", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust while-let: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "drain_iter");
    // while-let is a loop construct; should have back edges
    assert!(
        !cfg.blocks.is_empty(),
        "While-let should produce blocks"
    );
    println!(
        "[CFG while_let] blocks={}, edges={}",
        cfg.blocks.len(),
        cfg.edges.len()
    );
}

#[test]
fn cfg_works_for_rust_loop_counter() {
    let result = get_cfg_context(RUST_LOOP_COUNTER, "count_up", Language::Rust);
    assert!(
        result.is_ok(),
        "CFG construction failed for Rust loop counter: {:?}",
        result.err()
    );
    let cfg = result.unwrap();
    assert_eq!(cfg.function, "count_up");
    // while loop should produce loop structure with back edges
    assert!(
        cfg.blocks.len() >= 2,
        "Loop should have at least 2 blocks (header + body), got {}",
        cfg.blocks.len()
    );
    println!(
        "[CFG loop] blocks={}, edges={}, cyclomatic={}",
        cfg.blocks.len(),
        cfg.edges.len(),
        cfg.cyclomatic_complexity
    );
    // FINDING: Rust while-loop CFG produces only 1 edge instead of the expected
    // 2+ (entry->body + back-edge from body->header). This means loop analysis
    // (phi placement, back-edge detection) will be DEGRADED for Rust while loops.
    if cfg.edges.len() < 2 {
        println!(
            "[CFG loop] DEGRADED: Only {} edge(s) for while loop -- \
             back-edge missing, loop analysis will be limited",
            cfg.edges.len()
        );
    }
}

// =============================================================================
// API 2: get_dfg_context + compute_reaching_definitions
// =============================================================================

#[test]
fn dfg_works_for_rust_simple() {
    let result = get_dfg_context(RUST_SIMPLE_IF, "add", Language::Rust);
    assert!(
        result.is_ok(),
        "DFG extraction failed for Rust: {:?}",
        result.err()
    );
    let dfg = result.unwrap();
    assert_eq!(dfg.function, "add");
    // Should detect variable references for a, b
    println!(
        "[DFG simple] refs={}, edges={}, variables={:?}",
        dfg.refs.len(),
        dfg.edges.len(),
        dfg.variables
    );
    // The function should have at least the parameters as variables
    assert!(
        !dfg.refs.is_empty(),
        "DFG should detect variable references in Rust, got 0"
    );
}

#[test]
fn dfg_works_for_rust_shadowing() {
    let result = get_dfg_context(RUST_SHADOWING, "shadow", Language::Rust);
    assert!(
        result.is_ok(),
        "DFG extraction failed for Rust shadowing: {:?}",
        result.err()
    );
    let dfg = result.unwrap();
    assert_eq!(dfg.function, "shadow");
    // Variable 'y' is defined 3 times (shadowing)
    let y_defs: Vec<_> = dfg
        .refs
        .iter()
        .filter(|r| r.name == "y" && matches!(r.ref_type, tldr_core::types::RefType::Definition))
        .collect();
    println!(
        "[DFG shadowing] refs={}, y_defs={}, variables={:?}",
        dfg.refs.len(),
        y_defs.len(),
        dfg.variables
    );
    // Rust shadowing means 'y' should appear as multiple definitions
    assert!(
        y_defs.len() >= 2,
        "Rust shadowing: expected at least 2 definitions of 'y', got {}",
        y_defs.len()
    );
}

#[test]
fn reaching_defs_works_for_rust() {
    // First get CFG and DFG
    let cfg = get_cfg_context(RUST_REACHING_DEFS, "multi_assign", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_REACHING_DEFS, "multi_assign", Language::Rust)
        .expect("DFG should work");

    // Now compute reaching definitions
    let rd = compute_reaching_definitions(&cfg, &dfg.refs);

    println!(
        "[ReachingDefs] blocks_with_in={}, blocks_with_out={}",
        rd.reaching_in.len(),
        rd.reaching_out.len()
    );

    // Should have IN/OUT sets for every block in the CFG
    for block in &cfg.blocks {
        assert!(
            rd.reaching_in.contains_key(&block.id),
            "Missing reaching_in for block {}",
            block.id
        );
        assert!(
            rd.reaching_out.contains_key(&block.id),
            "Missing reaching_out for block {}",
            block.id
        );
    }

    // The OUT set of the last block should contain some definitions
    if let Some(last_block) = cfg.exit_blocks.first() {
        let out = &rd.reaching_out[last_block];
        println!(
            "[ReachingDefs] exit block {} has {} reaching defs",
            last_block,
            out.len()
        );
        assert!(
            !out.is_empty(),
            "Exit block should have reaching definitions"
        );
    }
}

#[test]
fn reaching_defs_works_for_rust_pattern_matching() {
    let cfg = get_cfg_context(RUST_MATCH, "classify", Language::Rust).expect("CFG should work");
    let dfg =
        get_dfg_context(RUST_MATCH, "classify", Language::Rust).expect("DFG should work");

    let rd = compute_reaching_definitions(&cfg, &dfg.refs);

    println!(
        "[ReachingDefs match] IN sets={}, OUT sets={}",
        rd.reaching_in.len(),
        rd.reaching_out.len()
    );

    // Should have data for each block
    assert_eq!(
        rd.reaching_in.len(),
        cfg.blocks.len(),
        "Should have IN set for every block"
    );
}

// =============================================================================
// API 3: construct_ssa + run_sccp
// =============================================================================

#[test]
fn ssa_construction_works_for_rust_simple() {
    let result = construct_ssa(RUST_SIMPLE_IF, "add", Language::Rust, SsaType::Minimal);
    assert!(
        result.is_ok(),
        "SSA construction failed for Rust simple: {:?}",
        result.err()
    );
    let ssa = result.unwrap();
    assert_eq!(ssa.function, "add");
    assert!(
        !ssa.blocks.is_empty(),
        "SSA should have blocks, got 0"
    );
    assert!(
        !ssa.ssa_names.is_empty(),
        "SSA should have named values, got 0"
    );
    println!(
        "[SSA simple] blocks={}, names={}, stats={:?}",
        ssa.blocks.len(),
        ssa.ssa_names.len(),
        ssa.stats
    );
}

#[test]
fn ssa_construction_works_for_rust_shadowing() {
    let result = construct_ssa(RUST_SHADOWING, "shadow", Language::Rust, SsaType::Minimal);
    assert!(
        result.is_ok(),
        "SSA construction failed for Rust shadowing: {:?}",
        result.err()
    );
    let ssa = result.unwrap();
    // Shadowing should produce multiple versions of 'y'
    let y_names: Vec<_> = ssa
        .ssa_names
        .iter()
        .filter(|n| n.variable == "y")
        .collect();
    println!(
        "[SSA shadowing] blocks={}, total_names={}, y_versions={}",
        ssa.blocks.len(),
        ssa.ssa_names.len(),
        y_names.len()
    );
    assert!(
        y_names.len() >= 2,
        "SSA should have multiple versions of 'y' due to shadowing, got {}",
        y_names.len()
    );
}

#[test]
fn ssa_construction_works_for_rust_loop() {
    let result = construct_ssa(
        RUST_LOOP_COUNTER,
        "count_up",
        Language::Rust,
        SsaType::Minimal,
    );
    assert!(
        result.is_ok(),
        "SSA construction failed for Rust loop: {:?}",
        result.err()
    );
    let ssa = result.unwrap();
    // Loop should produce phi functions at the loop header
    let total_phis: usize = ssa.blocks.iter().map(|b| b.phi_functions.len()).sum();
    println!(
        "[SSA loop] blocks={}, names={}, phis={}",
        ssa.blocks.len(),
        ssa.ssa_names.len(),
        total_phis
    );
    // With a while loop modifying 'total' and 'i', we expect phi functions
    // (may be 0 if the CFG doesn't create a proper loop back-edge in Rust)
    // This is informational -- we document what we get
    if total_phis == 0 {
        println!("[SSA loop] WARNING: No phi functions generated for loop -- Rust loop CFG may not produce back-edges");
    }
}

#[test]
fn ssa_pruned_works_for_rust() {
    let result = construct_ssa(RUST_SIMPLE_IF, "add", Language::Rust, SsaType::Pruned);
    assert!(
        result.is_ok(),
        "Pruned SSA construction failed for Rust: {:?}",
        result.err()
    );
    let ssa = result.unwrap();
    println!(
        "[SSA pruned] blocks={}, names={}, type={:?}",
        ssa.blocks.len(),
        ssa.ssa_names.len(),
        ssa.ssa_type
    );
}

#[test]
fn sccp_works_for_rust_constants() {
    use tldr_core::ssa::analysis::run_sccp;

    let ssa = construct_ssa(RUST_CONST_PROP, "constants", Language::Rust, SsaType::Minimal)
        .expect("SSA construction should work");

    let result = run_sccp(&ssa);
    assert!(
        result.is_ok(),
        "SCCP failed for Rust constants: {:?}",
        result.err()
    );
    let sccp = result.unwrap();
    println!(
        "[SCCP] constants={}, unreachable_blocks={}, dead_names={}",
        sccp.constants.len(),
        sccp.unreachable_blocks.len(),
        sccp.dead_names.len()
    );
    // Even if SCCP doesn't propagate perfectly for Rust, it should not crash
    // and should return a valid result
}

#[test]
fn sccp_works_for_rust_match() {
    use tldr_core::ssa::analysis::run_sccp;

    let ssa = construct_ssa(RUST_MATCH, "classify", Language::Rust, SsaType::Minimal)
        .expect("SSA construction should work");

    let result = run_sccp(&ssa);
    assert!(
        result.is_ok(),
        "SCCP failed for Rust match: {:?}",
        result.err()
    );
    let sccp = result.unwrap();
    println!(
        "[SCCP match] constants={}, unreachable={}, dead={}",
        sccp.constants.len(),
        sccp.unreachable_blocks.len(),
        sccp.dead_names.len()
    );
}

// =============================================================================
// API 4: compute_taint -- Taint Analysis
// =============================================================================

#[test]
fn taint_works_for_rust_env_to_command() {
    // Build CFG and DFG for the taint function
    let cfg = get_cfg_context(RUST_TAINT, "run_user_command", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_TAINT, "run_user_command", Language::Rust)
        .expect("DFG should work");

    // Build statements map from source lines
    let statements: HashMap<u32, String> = RUST_TAINT
        .lines()
        .enumerate()
        .map(|(i, line)| ((i + 1) as u32, line.to_string()))
        .collect();

    let result = compute_taint(&cfg, &dfg.refs, &statements, Language::Rust);
    assert!(
        result.is_ok(),
        "Taint analysis failed for Rust: {:?}",
        result.err()
    );
    let taint = result.unwrap();
    println!(
        "[Taint Rust] sources={}, sinks={}, flows={}, tainted_vars_blocks={}",
        taint.sources.len(),
        taint.sinks.len(),
        taint.flows.len(),
        taint.tainted_vars.len()
    );
    // Rust patterns should detect std::env::var as a source
    if taint.sources.is_empty() {
        println!("[Taint Rust] WARNING: No sources detected -- Rust taint patterns may not match this snippet");
    } else {
        println!("[Taint Rust] Sources detected: {:?}",
            taint.sources.iter().map(|s| format!("{:?} at line {}", s.source_type, s.line)).collect::<Vec<_>>()
        );
    }
    // Command::new should be detected as a sink
    if taint.sinks.is_empty() {
        println!("[Taint Rust] WARNING: No sinks detected -- Rust taint patterns may not match Command::new");
    } else {
        println!("[Taint Rust] Sinks detected: {:?}",
            taint.sinks.iter().map(|s| format!("{:?} at line {}", s.sink_type, s.line)).collect::<Vec<_>>()
        );
    }
}

#[test]
fn taint_with_tree_works_for_rust() {
    use tldr_core::ast::parser::parse;
    use tldr_core::security::compute_taint_with_tree;

    let cfg = get_cfg_context(RUST_TAINT, "run_user_command", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_TAINT, "run_user_command", Language::Rust)
        .expect("DFG should work");

    let tree = parse(RUST_TAINT, Language::Rust).expect("Parse should work");

    let statements: HashMap<u32, String> = RUST_TAINT
        .lines()
        .enumerate()
        .map(|(i, line)| ((i + 1) as u32, line.to_string()))
        .collect();

    let result = compute_taint_with_tree(
        &cfg,
        &dfg.refs,
        &statements,
        Some(&tree),
        Some(RUST_TAINT.as_bytes()),
        Language::Rust,
    );
    assert!(
        result.is_ok(),
        "Taint with tree failed for Rust: {:?}",
        result.err()
    );
    let taint = result.unwrap();
    println!(
        "[Taint+Tree Rust] sources={}, sinks={}, flows={}",
        taint.sources.len(),
        taint.sinks.len(),
        taint.flows.len()
    );
}

// =============================================================================
// API 5: compute_abstract_interp -- Abstract Interpretation
// =============================================================================

#[test]
fn abstract_interp_works_for_rust_loop() {
    let cfg = get_cfg_context(RUST_LOOP_COUNTER, "count_up", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_LOOP_COUNTER, "count_up", Language::Rust)
        .expect("DFG should work");

    let source_lines: Vec<&str> = RUST_LOOP_COUNTER.lines().collect();
    let result = compute_abstract_interp(&cfg, &dfg, Some(&source_lines), "rust");
    assert!(
        result.is_ok(),
        "Abstract interpretation failed for Rust loop: {:?}",
        result.err()
    );
    let interp = result.unwrap();
    println!(
        "[AbstractInterp loop] block_states={}, div_zero_warnings={}, null_deref_warnings={}",
        interp.state_in.len(),
        interp.potential_div_zero.len(),
        interp.potential_null_deref.len()
    );
    // Should have state for each block
    assert!(
        !interp.state_in.is_empty(),
        "Abstract interpretation should produce block states"
    );
}

#[test]
fn abstract_interp_works_for_rust_simple() {
    let cfg = get_cfg_context(RUST_SIMPLE_IF, "add", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_SIMPLE_IF, "add", Language::Rust)
        .expect("DFG should work");

    let source_lines: Vec<&str> = RUST_SIMPLE_IF.lines().collect();
    let result = compute_abstract_interp(&cfg, &dfg, Some(&source_lines), "rust");
    assert!(
        result.is_ok(),
        "Abstract interpretation failed for Rust simple: {:?}",
        result.err()
    );
    let interp = result.unwrap();
    println!(
        "[AbstractInterp simple] block_states={}, variables tracked: {:?}",
        interp.state_in.len(),
        interp
            .state_in
            .values()
            .flat_map(|s| s.values.keys())
            .collect::<std::collections::HashSet<_>>()
    );
}

// =============================================================================
// API 6: compute_gvn -- Global Value Numbering
// PREMORTEM FLAG: CK-10 says this hardcodes Python. VERIFY.
// =============================================================================

#[test]
fn gvn_hardcodes_python_confirmed() {
    // CK-10 premortem finding: compute_gvn hardcodes Language::Python
    // at engine.rs:1090 and searches for "function_definition" |
    // "async_function_definition" (Python AST node kinds).
    //
    // VERIFIED: The source code at engine.rs:1090 reads:
    //   let tree = match parse(source, Language::Python) {
    // And find_functions looks for "function_definition" which is Python-specific.
    //
    // This means calling compute_gvn with Rust source will:
    // 1. Parse the Rust source as Python (tree-sitter will fail or produce garbage)
    // 2. Return an empty Vec since no Python function_definitions are found

    let reports = compute_gvn(RUST_SIMPLE_IF, Some("add"));
    println!(
        "[GVN] Rust source returned {} reports (expected 0 because Python is hardcoded)",
        reports.len()
    );
    // CONFIRMED: GVN returns empty for Rust because it parses as Python
    assert!(
        reports.is_empty(),
        "GVN should return empty for Rust since it hardcodes Language::Python -- \
         if this fails, GVN may have been updated to support multiple languages"
    );
    println!("[GVN] CONFIRMED: compute_gvn hardcodes Language::Python -- BROKEN for Rust");
}

#[test]
fn gvn_works_for_python_baseline() {
    // Confirm GVN works for Python (baseline that it's not entirely broken)
    let python_source = r#"
def example(a, b):
    x = a + b
    y = a + b
    return x + y
"#;
    let reports = compute_gvn(python_source, Some("example"));
    assert!(
        !reports.is_empty(),
        "GVN should work for Python (baseline check)"
    );
    let report = &reports[0];
    println!(
        "[GVN Python] function={}, expressions={}, equivalences={}, redundancies={}",
        report.function,
        report.total_expressions,
        report.equivalences.len(),
        report.redundancies.len()
    );
    // a+b appears twice, should be detected as redundant
    assert!(
        !report.redundancies.is_empty(),
        "GVN should detect redundancy in Python baseline"
    );
}

// =============================================================================
// API 7: detect_clones -- Clone Detection (language-agnostic tokenization)
// =============================================================================

#[test]
fn clone_detection_works_for_rust_files() {
    use std::fs;
    use tempfile::tempdir;
    use tldr_core::analysis::clones::{detect_clones, ClonesOptions};

    let dir = tempdir().expect("Failed to create temp dir");

    // Write two similar Rust files
    let file_a = dir.path().join("area.rs");
    let file_b = dir.path().join("volume.rs");
    fs::write(&file_a, RUST_CLONE_PAIR_A).expect("Write file A");
    fs::write(&file_b, RUST_CLONE_PAIR_B).expect("Write file B");

    let options = ClonesOptions {
        language: Some("rust".to_string()),
        min_tokens: 10,
        min_lines: 3,
        ..ClonesOptions::default()
    };

    let result = detect_clones(dir.path(), &options);
    assert!(
        result.is_ok(),
        "Clone detection failed for Rust: {:?}",
        result.err()
    );
    let report = result.unwrap();
    println!(
        "[Clones] files_scanned={}, total_clones={}, type1={}, type2={}, type3={}",
        report.stats.files_analyzed,
        report.stats.clones_found,
        report.stats.type1_count,
        report.stats.type2_count,
        report.stats.type3_count,
    );
    // The two functions are structurally identical with different names
    // so Type-2 or Type-3 clones should be detected
    // Even if no clones are found, the API should work without error
    assert!(
        report.stats.files_analyzed >= 2,
        "Should scan at least 2 Rust files, got {}",
        report.stats.files_analyzed
    );
}

// =============================================================================
// API 8: detect_smells -- Code Smell Detection
// =============================================================================

#[test]
fn smell_detection_works_for_rust_file() {
    use std::fs;
    use tempfile::tempdir;
    use tldr_core::quality::smells::{detect_smells, ThresholdPreset};

    let dir = tempdir().expect("Failed to create temp dir");

    // Write a Rust file with a long function (potential smell)
    let long_fn = r#"
fn very_long_function(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32) -> i32 {
    let x1 = a + b;
    let x2 = c + d;
    let x3 = e + f;
    let x4 = x1 + x2;
    let x5 = x3 + g;
    let x6 = x4 + x5;
    let x7 = x6 * 2;
    let x8 = x7 - 1;
    let x9 = x8 + a;
    let x10 = x9 * b;
    let x11 = x10 + c;
    let x12 = x11 - d;
    let x13 = x12 * e;
    let x14 = x13 + f;
    let x15 = x14 - g;
    let x16 = x15 * 2;
    let x17 = x16 + 1;
    let x18 = x17 - 2;
    let x19 = x18 * 3;
    let x20 = x19 + 4;
    x20
}
"#;
    let file = dir.path().join("smelly.rs");
    fs::write(&file, long_fn).expect("Write smelly file");

    let result = detect_smells(dir.path(), ThresholdPreset::Strict, None, false);
    assert!(
        result.is_ok(),
        "Smell detection failed for Rust: {:?}",
        result.err()
    );
    let report = result.unwrap();
    println!(
        "[Smells] total_smells={}, files_scanned={}",
        report.smells.len(),
        report.files_scanned
    );
    // Should detect LongParameterList (7 params > 5 threshold)
    let long_params: Vec<_> = report
        .smells
        .iter()
        .filter(|s| s.smell_type == tldr_core::quality::smells::SmellType::LongParameterList)
        .collect();
    println!(
        "[Smells] LongParameterList findings: {}",
        long_params.len()
    );
    // Even if the specific smell isn't detected, the scan should complete
    assert!(
        report.files_scanned >= 1,
        "Should scan at least 1 file, got {}",
        report.files_scanned
    );
}

// =============================================================================
// API 9: Available Expressions (dataflow layer)
// =============================================================================

#[test]
fn available_expressions_works_for_rust() {
    let cfg = get_cfg_context(RUST_SIMPLE_IF, "add", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_SIMPLE_IF, "add", Language::Rust)
        .expect("DFG should work");

    let result = compute_available_exprs(&cfg, &dfg);
    assert!(
        result.is_ok(),
        "Available expressions failed for Rust: {:?}",
        result.err()
    );
    let avail = result.unwrap();
    println!(
        "[AvailExprs] block_data={}, total_expressions={}, redundant={}",
        avail.avail_in.len(),
        avail.all_exprs.len(),
        avail.redundant_computations().len()
    );
}

#[test]
fn available_expressions_works_for_rust_shadowing() {
    let cfg = get_cfg_context(RUST_SHADOWING, "shadow", Language::Rust)
        .expect("CFG should work");
    let dfg = get_dfg_context(RUST_SHADOWING, "shadow", Language::Rust)
        .expect("DFG should work");

    let result = compute_available_exprs(&cfg, &dfg);
    assert!(
        result.is_ok(),
        "Available expressions failed for Rust shadowing: {:?}",
        result.err()
    );
    let avail = result.unwrap();
    println!(
        "[AvailExprs shadow] expressions={}, redundant={}",
        avail.all_exprs.len(),
        avail.redundant_computations().len()
    );
}

// =============================================================================
// API Comprehensive: Full pipeline CFG -> DFG -> SSA -> SCCP for Rust
// =============================================================================

#[test]
fn full_pipeline_works_for_rust() {
    use tldr_core::ssa::analysis::run_sccp;

    // Step 1: CFG
    let cfg = get_cfg_context(RUST_REACHING_DEFS, "multi_assign", Language::Rust)
        .expect("CFG failed");
    println!(
        "[Pipeline] CFG: blocks={}, edges={}",
        cfg.blocks.len(),
        cfg.edges.len()
    );

    // Step 2: DFG
    let dfg = get_dfg_context(RUST_REACHING_DEFS, "multi_assign", Language::Rust)
        .expect("DFG failed");
    println!(
        "[Pipeline] DFG: refs={}, variables={:?}",
        dfg.refs.len(),
        dfg.variables
    );

    // Step 3: Reaching Definitions
    let rd = compute_reaching_definitions(&cfg, &dfg.refs);
    println!(
        "[Pipeline] ReachingDefs: IN={}, OUT={}",
        rd.reaching_in.len(),
        rd.reaching_out.len()
    );

    // Step 4: SSA Construction
    let ssa = construct_ssa(
        RUST_REACHING_DEFS,
        "multi_assign",
        Language::Rust,
        SsaType::Minimal,
    )
    .expect("SSA failed");
    let total_phis: usize = ssa.blocks.iter().map(|b| b.phi_functions.len()).sum();
    println!(
        "[Pipeline] SSA: blocks={}, names={}, phis={}",
        ssa.blocks.len(),
        ssa.ssa_names.len(),
        total_phis
    );

    // Step 5: SCCP
    let sccp = run_sccp(&ssa).expect("SCCP failed");
    println!(
        "[Pipeline] SCCP: constants={}, unreachable={}, dead={}",
        sccp.constants.len(),
        sccp.unreachable_blocks.len(),
        sccp.dead_names.len()
    );

    // Step 6: Available Expressions
    let avail = compute_available_exprs(&cfg, &dfg).expect("AvailExprs failed");
    println!(
        "[Pipeline] AvailExprs: expressions={}, redundant={}",
        avail.all_exprs.len(),
        avail.redundant_computations().len()
    );

    // Step 7: Abstract Interpretation
    let source_lines: Vec<&str> = RUST_REACHING_DEFS.lines().collect();
    let interp = compute_abstract_interp(&cfg, &dfg, Some(&source_lines), "rust")
        .expect("AbstractInterp failed");
    println!(
        "[Pipeline] AbstractInterp: states={}, div_zero={}, null_deref={}",
        interp.state_in.len(),
        interp.potential_div_zero.len(),
        interp.potential_null_deref.len()
    );

    // Step 8: Taint Analysis
    let statements: HashMap<u32, String> = RUST_REACHING_DEFS
        .lines()
        .enumerate()
        .map(|(i, line)| ((i + 1) as u32, line.to_string()))
        .collect();
    let taint = compute_taint(&cfg, &dfg.refs, &statements, Language::Rust)
        .expect("Taint failed");
    println!(
        "[Pipeline] Taint: sources={}, sinks={}, flows={}",
        taint.sources.len(),
        taint.sinks.len(),
        taint.flows.len()
    );

    println!("[Pipeline] COMPLETE: Full IR pipeline succeeded for Rust");
}

// =============================================================================
// Edge case: Empty / missing function
// =============================================================================

#[test]
fn dfg_works_for_rust_pattern_destructuring() {
    let result = get_dfg_context(RUST_PATTERN_LET, "destructure", Language::Rust);
    assert!(
        result.is_ok(),
        "DFG extraction failed for Rust pattern destructuring: {:?}",
        result.err()
    );
    let dfg = result.unwrap();
    assert_eq!(dfg.function, "destructure");
    println!(
        "[DFG destructure] refs={}, variables={:?}",
        dfg.refs.len(),
        dfg.variables
    );
    // Should detect variables from tuple destructuring: a, b, pair
    assert!(
        !dfg.refs.is_empty(),
        "DFG should detect refs in destructured patterns"
    );
}

#[test]
fn cfg_returns_empty_for_missing_rust_function() {
    let result = get_cfg_context(RUST_SIMPLE_IF, "nonexistent", Language::Rust);
    assert!(result.is_ok(), "Should return Ok with empty CFG for missing function");
    let cfg = result.unwrap();
    assert!(
        cfg.blocks.is_empty(),
        "Missing function should return empty CFG"
    );
    println!("[CFG missing] Correctly returns empty CFG for nonexistent function");
}

#[test]
fn ssa_handles_empty_rust_function() {
    // A function with no variable assignments
    let empty_fn = r#"
fn noop() {
}
"#;
    let result = construct_ssa(empty_fn, "noop", Language::Rust, SsaType::Minimal);
    // This might fail or produce empty SSA -- both are valid outcomes
    match result {
        Ok(ssa) => {
            println!(
                "[SSA empty] blocks={}, names={}",
                ssa.blocks.len(),
                ssa.ssa_names.len()
            );
        }
        Err(e) => {
            println!("[SSA empty] Error (may be expected for empty function): {:?}", e);
        }
    }
}