tldr-core 0.1.2

Core analysis engine 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
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
//! Reaching Definitions Extended Tests
//!
//! Comprehensive tests for enhanced reaching definitions analysis.
//! These tests define expected behavior for new capabilities.
//!
//! # Test Categories
//!
//! 1. Def-Use Chain Tests (RD-7)
//! 2. Use-Def Chain Tests (RD-8)
//! 3. Uninitialized Detection Tests (RD-13) - Phase 9
//! 4. Available Expressions Tests (RD-10) - Phase 15
//! 5. Live Variables Tests (RD-12) - Phase 15
//! 6. RPO Worklist Tests (RD-3) - Phase 16
//! 7. Output Format Tests (RD-14, RD-15, RD-16) - Phase 10
//!
//! Reference: session10-spec.md

use std::collections::HashMap;
use std::path::PathBuf;

use crate::types::{BlockType, CfgBlock, CfgEdge, CfgInfo, EdgeType, RefType, VarRef};

// Import the actual implementations from reaching.rs
use super::reaching::{
    build_def_use_chains,
    build_reaching_defs_report,
    build_use_def_chains,
    compute_reaching_definitions,
    detect_uninitialized as detect_uninitialized_with_params,
    detect_uninitialized_simple,
    ReachingDefinitions,
    ReachingDefsReport,
    UninitSeverity,
    UninitializedUse,
};

// Import format functions (Phase 10)
use super::format::{
    filter_reaching_defs_by_variable as filter_by_var_impl,
    format_reaching_defs_json as format_json_impl, format_reaching_defs_text as format_text_impl,
};

// =============================================================================
// Helper wrapper for tests (Phase 9)
// =============================================================================

/// Detect potentially uninitialized variable uses
fn detect_uninitialized(
    reaching: &ReachingDefinitions,
    cfg: &CfgInfo,
    refs: &[VarRef],
) -> Vec<UninitializedUse> {
    detect_uninitialized_simple(reaching, cfg, refs)
}

/// Compute reaching definitions with optimized worklist (RPO) - Phase 16
fn compute_reaching_definitions_rpo(cfg: &CfgInfo, refs: &[VarRef]) -> ReachingDefinitions {
    // Use the actual RPO implementation from reaching.rs
    use super::reaching::compute_reaching_definitions_rpo as rpo_impl;
    rpo_impl(cfg, refs).reaching
}

/// Format reaching definitions as text (Phase 10)
fn format_reaching_defs_text(report: &ReachingDefsReport) -> String {
    format_text_impl(report)
}

/// Format reaching definitions as JSON (Phase 10)
fn format_reaching_defs_json(report: &ReachingDefsReport) -> String {
    format_json_impl(report).unwrap_or_else(|e| format!("Error: {}", e))
}

/// Filter reaching definitions by variable (Phase 10)
fn filter_reaching_defs_by_variable(
    report: ReachingDefsReport,
    variable: &str,
) -> ReachingDefsReport {
    filter_by_var_impl(&report, variable)
}

// =============================================================================
// Test Fixtures
// =============================================================================

mod fixtures {
    use super::*;

    pub fn make_block(id: usize, start: u32, end: u32) -> CfgBlock {
        CfgBlock {
            id,
            block_type: BlockType::Body,
            lines: (start, end),
            calls: Vec::new(),
        }
    }

    pub fn make_def(name: &str, line: u32) -> VarRef {
        VarRef {
            name: name.to_string(),
            ref_type: RefType::Definition,
            line,
            column: 0,
            context: None,
            group_id: None,
        }
    }

    pub fn make_use(name: &str, line: u32) -> VarRef {
        VarRef {
            name: name.to_string(),
            ref_type: RefType::Use,
            line,
            column: 0,
            context: None,
            group_id: None,
        }
    }

    /// Linear CFG: Block 0 -> Block 1 -> Block 2
    pub fn linear_cfg() -> CfgInfo {
        CfgInfo {
            function: "linear".to_string(),
            blocks: vec![
                make_block(0, 1, 2),
                make_block(1, 3, 4),
                make_block(2, 5, 6),
            ],
            edges: vec![
                CfgEdge {
                    from: 0,
                    to: 1,
                    edge_type: EdgeType::Unconditional,
                    condition: None,
                },
                CfgEdge {
                    from: 1,
                    to: 2,
                    edge_type: EdgeType::Unconditional,
                    condition: None,
                },
            ],
            entry_block: 0,
            exit_blocks: vec![2],
            cyclomatic_complexity: 1,
            nested_functions: HashMap::new(),
        }
    }

    /// Diamond CFG for branching tests
    pub fn diamond_cfg() -> CfgInfo {
        CfgInfo {
            function: "diamond".to_string(),
            blocks: vec![
                CfgBlock {
                    id: 0,
                    block_type: BlockType::Entry,
                    lines: (1, 2),
                    calls: Vec::new(),
                },
                make_block(1, 3, 4),
                make_block(2, 5, 6),
                make_block(3, 7, 8),
            ],
            edges: vec![
                CfgEdge {
                    from: 0,
                    to: 1,
                    edge_type: EdgeType::True,
                    condition: Some("cond".to_string()),
                },
                CfgEdge {
                    from: 0,
                    to: 2,
                    edge_type: EdgeType::False,
                    condition: Some("cond".to_string()),
                },
                CfgEdge {
                    from: 1,
                    to: 3,
                    edge_type: EdgeType::Unconditional,
                    condition: None,
                },
                CfgEdge {
                    from: 2,
                    to: 3,
                    edge_type: EdgeType::Unconditional,
                    condition: None,
                },
            ],
            entry_block: 0,
            exit_blocks: vec![3],
            cyclomatic_complexity: 2,
            nested_functions: HashMap::new(),
        }
    }

    /// Loop CFG for iteration tests
    pub fn loop_cfg() -> CfgInfo {
        CfgInfo {
            function: "loop".to_string(),
            blocks: vec![
                make_block(0, 1, 2), // entry
                make_block(1, 3, 4), // loop header
                make_block(2, 5, 6), // loop body
                make_block(3, 7, 8), // exit
            ],
            edges: vec![
                CfgEdge {
                    from: 0,
                    to: 1,
                    edge_type: EdgeType::Unconditional,
                    condition: None,
                },
                CfgEdge {
                    from: 1,
                    to: 2,
                    edge_type: EdgeType::True,
                    condition: Some("i < 10".to_string()),
                },
                CfgEdge {
                    from: 1,
                    to: 3,
                    edge_type: EdgeType::False,
                    condition: Some("i < 10".to_string()),
                },
                CfgEdge {
                    from: 2,
                    to: 1,
                    edge_type: EdgeType::BackEdge,
                    condition: None,
                },
            ],
            entry_block: 0,
            exit_blocks: vec![3],
            cyclomatic_complexity: 2,
            nested_functions: HashMap::new(),
        }
    }
}

// =============================================================================
// Def-Use Chain Tests (RD-7)
// =============================================================================

#[cfg(test)]
mod def_use_chain_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: Definition reaches all subsequent uses
    #[test]
    fn test_def_reaches_all_uses() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1), // definition at line 1
            make_use("x", 3), // use at line 3
            make_use("x", 5), // use at line 5
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_def_use_chains(&reaching, &cfg, &refs);

        // Should have one chain for x's definition
        let x_chain = chains.iter().find(|c| c.definition.var == "x");
        assert!(x_chain.is_some(), "Should have def-use chain for x");

        let x_chain = x_chain.unwrap();
        // Definition should reach both uses
        assert_eq!(x_chain.uses.len(), 2, "Definition should reach 2 uses");
        assert!(x_chain.uses.iter().any(|u| u.line == 3));
        assert!(x_chain.uses.iter().any(|u| u.line == 5));
    }

    /// Test: Killed definition doesn't reach past kill point
    #[test]
    fn test_killed_def_doesnt_reach() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1), // first definition
            make_use("x", 2), // use before kill
            make_def("x", 3), // kill (redefine)
            make_use("x", 5), // use after kill
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_def_use_chains(&reaching, &cfg, &refs);

        // First definition (line 1) should only reach use at line 2
        let first_chain = chains
            .iter()
            .find(|c| c.definition.var == "x" && c.definition.line == 1);
        assert!(first_chain.is_some());
        let first_chain = first_chain.unwrap();
        assert_eq!(first_chain.uses.len(), 1);
        assert_eq!(first_chain.uses[0].line, 2);

        // Second definition (line 3) should reach use at line 5
        let second_chain = chains
            .iter()
            .find(|c| c.definition.var == "x" && c.definition.line == 3);
        assert!(second_chain.is_some());
        let second_chain = second_chain.unwrap();
        assert!(second_chain.uses.iter().any(|u| u.line == 5));
    }

    /// Test: Multiple definitions reaching same use via different paths
    #[test]
    fn test_multiple_defs_to_one_use() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 3), // definition in true branch
            make_def("x", 5), // definition in false branch
            make_use("x", 7), // use at merge
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_def_use_chains(&reaching, &cfg, &refs);

        // Both definitions should reach the use at line 7
        let chain_at_3 = chains.iter().find(|c| c.definition.line == 3);
        let chain_at_5 = chains.iter().find(|c| c.definition.line == 5);

        assert!(chain_at_3.is_some());
        assert!(chain_at_5.is_some());
        assert!(chain_at_3.unwrap().uses.iter().any(|u| u.line == 7));
        assert!(chain_at_5.unwrap().uses.iter().any(|u| u.line == 7));
    }

    /// Test: Independent variables have independent chains
    #[test]
    fn test_independent_variables() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1),
            make_def("y", 2),
            make_use("x", 3),
            make_use("y", 4),
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_def_use_chains(&reaching, &cfg, &refs);

        // Should have separate chains for x and y
        let x_chain = chains.iter().find(|c| c.definition.var == "x").unwrap();
        let y_chain = chains.iter().find(|c| c.definition.var == "y").unwrap();

        // x's chain should only have x's use
        assert!(x_chain.uses.iter().all(|u| {
            // Check that this use corresponds to x
            refs.iter().any(|r| r.line == u.line && r.name == "x")
        }));

        // y's chain should only have y's use
        assert!(y_chain
            .uses
            .iter()
            .all(|u| { refs.iter().any(|r| r.line == u.line && r.name == "y") }));
    }
}

// =============================================================================
// Use-Def Chain Tests (RD-8)
// =============================================================================

#[cfg(test)]
mod use_def_chain_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: Use maps back to all reaching definitions
    #[test]
    fn test_use_maps_to_reaching_defs() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 3), // true branch
            make_def("x", 5), // false branch
            make_use("x", 7), // merge point
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_use_def_chains(&reaching, &cfg, &refs);

        // Use at line 7 should have both definitions reaching it
        let use_chain = chains.iter().find(|c| c.use_site.line == 7);
        assert!(
            use_chain.is_some(),
            "Should have use-def chain for use at line 7"
        );

        let use_chain = use_chain.unwrap();
        assert_eq!(
            use_chain.reaching_defs.len(),
            2,
            "Use should have 2 reaching definitions"
        );
        assert!(use_chain.reaching_defs.iter().any(|d| d.line == 3));
        assert!(use_chain.reaching_defs.iter().any(|d| d.line == 5));
    }

    /// Test: Use with single reaching definition
    #[test]
    fn test_use_single_reaching_def() {
        let cfg = linear_cfg();
        let refs = vec![make_def("x", 1), make_use("x", 5)];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_use_def_chains(&reaching, &cfg, &refs);

        let use_chain = chains.iter().find(|c| c.use_site.line == 5).unwrap();
        assert_eq!(use_chain.reaching_defs.len(), 1);
        assert_eq!(use_chain.reaching_defs[0].line, 1);
    }

    /// Test: Multiple uses with different reaching defs
    #[test]
    fn test_multiple_uses_different_reaching() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1),
            make_use("x", 2),
            make_def("x", 3),
            make_use("x", 5),
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let chains = build_use_def_chains(&reaching, &cfg, &refs);

        // Use at line 2 should have def from line 1
        let use_at_2 = chains.iter().find(|c| c.use_site.line == 2).unwrap();
        assert!(use_at_2.reaching_defs.iter().any(|d| d.line == 1));
        assert!(!use_at_2.reaching_defs.iter().any(|d| d.line == 3));

        // Use at line 5 should have def from line 3 (line 1 is killed)
        let use_at_5 = chains.iter().find(|c| c.use_site.line == 5).unwrap();
        assert!(use_at_5.reaching_defs.iter().any(|d| d.line == 3));
        assert!(!use_at_5.reaching_defs.iter().any(|d| d.line == 1));
    }
}

// =============================================================================
// Chain Consistency Tests
// =============================================================================

#[cfg(test)]
mod chain_consistency_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: Def-use and use-def chains are consistent inverses
    #[test]
    fn test_chains_consistency() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 3), // true branch
            make_def("x", 5), // false branch
            make_use("x", 7), // merge point
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let def_use = build_def_use_chains(&reaching, &cfg, &refs);
        let use_def = build_use_def_chains(&reaching, &cfg, &refs);

        // For each def-use chain: if def D reaches use U,
        // then use U should have D in its reaching_defs
        for du_chain in &def_use {
            for use_site in &du_chain.uses {
                let ud_chain = use_def
                    .iter()
                    .find(|c| c.use_site.line == use_site.line && c.var == du_chain.definition.var);
                assert!(
                    ud_chain.is_some(),
                    "Use at line {} should have a use-def chain",
                    use_site.line
                );
                let ud_chain = ud_chain.unwrap();
                assert!(
                    ud_chain
                        .reaching_defs
                        .iter()
                        .any(|d| d.line == du_chain.definition.line),
                    "Use at line {} should have def at line {} in reaching_defs",
                    use_site.line,
                    du_chain.definition.line
                );
            }
        }
    }
}

// =============================================================================
// Report Generation Tests
// =============================================================================

#[cfg(test)]
mod report_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: Report generation produces correct structure
    #[test]
    fn test_report_generation() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1),
            make_use("x", 3),
            make_def("y", 4),
            make_use("y", 5),
        ];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));

        // Check basic structure
        assert_eq!(report.function, "linear");
        assert_eq!(report.file, PathBuf::from("test.py"));
        assert_eq!(report.blocks.len(), 3); // linear_cfg has 3 blocks

        // Check stats
        assert_eq!(report.stats.definitions, 2); // x and y
        assert_eq!(report.stats.uses, 2); // x and y uses
        assert_eq!(report.stats.blocks, 3);

        // Check def-use chains exist
        assert!(!report.def_use_chains.is_empty());

        // Check use-def chains exist
        assert!(!report.use_def_chains.is_empty());
    }

    /// Test: Block-level IN/OUT sets are correct
    #[test]
    fn test_block_in_out_sets() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1), // in block 0
            make_use("x", 5), // in block 2
        ];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));

        // Block 0 should have x in its GEN set
        let block_0 = report.blocks.iter().find(|b| b.id == 0).unwrap();
        assert!(block_0.gen.iter().any(|d| d.var == "x" && d.line == 1));

        // Block 2 should have x in its IN set
        let block_2 = report.blocks.iter().find(|b| b.id == 2).unwrap();
        assert!(block_2.in_set.iter().any(|d| d.var == "x" && d.line == 1));
    }
}

// =============================================================================
// Uninitialized Detection Tests (RD-13) - Phase 9 placeholder
// =============================================================================

#[cfg(test)]
mod uninitialized_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: Use with no reaching definition is flagged
    #[test]
    fn test_definitely_uninitialized() {
        let cfg = linear_cfg();
        let refs = vec![
            make_use("x", 1), // use before any definition
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        assert!(!uninit.is_empty(), "Should detect uninitialized use");
        assert!(uninit.iter().any(|u| u.var == "x" && u.line == 1));
    }

    /// Test: Conditional initialization path detected as warning
    #[test]
    fn test_possibly_uninitialized() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 3), // only defined in true branch
            // x NOT defined in false branch
            make_use("x", 7), // use at merge - possibly uninitialized
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        // Should detect that x might not be initialized (false branch has no def)
        assert!(!uninit.is_empty(), "Should detect possibly uninitialized");
    }

    /// Test: Fully initialized variable not flagged
    #[test]
    fn test_fully_initialized_not_flagged() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 3), // defined in true branch
            make_def("x", 5), // defined in false branch
            make_use("x", 7), // use at merge - fully initialized
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        assert!(
            uninit.is_empty(),
            "Should not flag fully initialized variable"
        );
    }

    /// Test: Parameter is considered initialized
    #[test]
    fn test_parameter_initialized() {
        // Parameters should be treated as initialized at function entry
        let cfg = linear_cfg();
        let refs = vec![
            make_use("x", 1), // use of parameter
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        // Note: detect_uninitialized would need parameter info
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        // For now this will fail since we don't track parameters
        // Phase 9 will handle this properly
        let _ = uninit;
    }

    /// Test: Multiple uninitialized variables
    #[test]
    fn test_multiple_uninitialized() {
        let cfg = linear_cfg();
        let refs = vec![make_use("x", 1), make_use("y", 2)];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        assert!(
            uninit.len() >= 2,
            "Should detect both uninitialized variables"
        );
    }

    /// Test: Global variable is not flagged as uninitialized
    #[test]
    fn test_global_not_flagged() {
        let cfg = linear_cfg();
        let refs = vec![
            make_use("CONFIG", 1), // use of global
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        // Pass CONFIG as a global variable
        let uninit = detect_uninitialized_with_params(
            &reaching,
            &cfg,
            &refs,
            &[],                     // no params
            &["CONFIG".to_string()], // CONFIG is a global
        );

        assert!(uninit.is_empty(), "Global variables should not be flagged");
    }

    /// Test: Parameter is not flagged when passed to detect_uninitialized
    #[test]
    fn test_parameter_with_params_arg() {
        let cfg = linear_cfg();
        let refs = vec![
            make_use("x", 1), // use of parameter x
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        // Pass x as a parameter
        let uninit = detect_uninitialized_with_params(
            &reaching,
            &cfg,
            &refs,
            &["x".to_string()], // x is a parameter
            &[],
        );

        assert!(uninit.is_empty(), "Parameters should not be flagged");
    }

    /// Test: Severity is DEFINITE when no definition exists anywhere
    #[test]
    fn test_severity_definite() {
        let cfg = linear_cfg();
        let refs = vec![
            make_use("x", 1), // use with no definition anywhere
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        assert!(!uninit.is_empty(), "Should detect uninitialized use");
        assert_eq!(
            uninit[0].severity,
            UninitSeverity::Definite,
            "Should be DEFINITE when no definition exists"
        );
    }

    /// Test: Severity is POSSIBLE when definition exists but doesn't reach
    #[test]
    fn test_severity_possible() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 3), // only defined in true branch
            make_use("x", 7), // use at merge
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        assert!(!uninit.is_empty(), "Should detect possibly uninitialized");
        assert_eq!(
            uninit[0].severity,
            UninitSeverity::Possible,
            "Should be POSSIBLE when definition exists but doesn't reach all paths"
        );
    }

    /// Test: Loop where variable is used before definition in first iteration
    #[test]
    fn test_loop_first_iteration_uninit() {
        let cfg = loop_cfg();
        let refs = vec![
            // No init before loop
            make_use("total", 3), // use in header (first iteration has no def)
            make_def("total", 5), // update in body
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let uninit = detect_uninitialized(&reaching, &cfg, &refs);

        // On first iteration, total has no reaching def
        // (The def at line 5 is in the loop body, which comes AFTER the use at line 3)
        assert!(
            !uninit.is_empty(),
            "Should detect possibly uninitialized in loop"
        );
    }
}

// =============================================================================
// RPO Worklist Tests (RD-3) - Phase 16 placeholder
// =============================================================================

#[cfg(test)]
mod rpo_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: RPO produces same result as naive algorithm
    #[test]
    fn test_rpo_same_result_as_naive() {
        let cfg = diamond_cfg();
        let refs = vec![make_def("x", 3), make_def("x", 5), make_use("x", 7)];

        let naive = compute_reaching_definitions(&cfg, &refs);
        let rpo = compute_reaching_definitions_rpo(&cfg, &refs);

        // Should produce identical results
        for block in &cfg.blocks {
            let naive_in = naive.reaching_in.get(&block.id);
            let rpo_in = rpo.reaching_in.get(&block.id);
            assert_eq!(
                naive_in, rpo_in,
                "IN sets should match for block {}",
                block.id
            );
        }
    }

    /// Test: RPO handles loops correctly
    #[test]
    fn test_rpo_handles_loops() {
        let cfg = loop_cfg();
        let refs = vec![
            make_def("i", 1), // init in entry
            make_use("i", 3), // use in header
            make_def("i", 5), // update in body
        ];

        let reaching = compute_reaching_definitions_rpo(&cfg, &refs);

        // Both definitions should reach the header (loop back edge)
        let header_in = reaching.reaching_in.get(&1).unwrap();
        // At minimum, the first def should reach
        assert!(!header_in.is_empty());
    }
}

// =============================================================================
// Output Format Tests (RD-14, RD-15, RD-16) - Phase 10 placeholder
// =============================================================================

#[cfg(test)]
mod output_format_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: JSON output matches spec schema (RD-15)
    #[test]
    fn test_json_schema() {
        let cfg = linear_cfg();
        let refs = vec![make_def("x", 1), make_use("x", 3)];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));
        let json = format_reaching_defs_json(&report);

        // Should produce valid JSON
        assert!(!json.is_empty(), "JSON should not be empty");
        // Should contain expected fields
        assert!(
            json.contains("\"function\""),
            "JSON should have function field"
        );
        assert!(json.contains("\"blocks\""), "JSON should have blocks field");
        assert!(
            json.contains("\"def_use_chains\""),
            "JSON should have def_use_chains field"
        );
        assert!(json.contains("\"stats\""), "JSON should have stats field");
    }

    /// Test: JSON 'in' field naming (spec requirement)
    #[test]
    fn test_json_in_field_name() {
        let cfg = linear_cfg();
        let refs = vec![make_def("x", 1), make_use("x", 3)];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));
        let json = format_reaching_defs_json(&report);

        // Per spec, the field should be "in" not "in_set"
        assert!(
            json.contains("\"in\""),
            "Field should be serialized as 'in'"
        );
    }

    /// Test: Text format is readable (RD-14)
    #[test]
    fn test_text_format_readable() {
        let cfg = linear_cfg();
        let refs = vec![make_def("x", 1), make_use("x", 3)];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));
        let text = format_reaching_defs_text(&report);

        // Should produce non-empty output
        assert!(!text.is_empty(), "Text should not be empty");
        // Should have header
        assert!(
            text.contains("Reaching Definitions for:"),
            "Should have header"
        );
        // Should have GEN/KILL/IN/OUT
        assert!(text.contains("GEN:"), "Should have GEN set");
        assert!(text.contains("OUT:"), "Should have OUT set");
    }

    /// Test: Variable filtering works (RD-16)
    #[test]
    fn test_variable_filtering() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1),
            make_def("y", 2),
            make_use("x", 3),
            make_use("y", 4),
        ];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));
        let filtered = filter_reaching_defs_by_variable(report.clone(), "x");

        // Should only contain x's chains
        assert!(
            filtered
                .def_use_chains
                .iter()
                .all(|c| c.definition.var == "x"),
            "All def-use chains should be for x"
        );
        // Should filter block GEN/KILL/IN/OUT too
        for block in &filtered.blocks {
            assert!(
                block.gen.iter().all(|d| d.var == "x"),
                "Block GEN should only contain x"
            );
        }
    }

    /// Test: Stats iterations field is tracked
    #[test]
    fn test_stats_iterations() {
        let cfg = linear_cfg();
        let refs = vec![make_def("x", 1), make_use("x", 3)];

        let report = build_reaching_defs_report(&cfg, &refs, PathBuf::from("test.py"));

        // Stats should be populated
        assert!(
            report.stats.definitions > 0 || report.stats.uses > 0,
            "Stats should be populated"
        );
        assert!(report.stats.blocks > 0, "Should have block count");
    }
}

// =============================================================================
// Integration Tests
// =============================================================================

#[cfg(test)]
mod integration_tests {
    use super::fixtures::*;
    use super::*;

    /// Test: Full pipeline with linear CFG
    #[test]
    fn test_full_pipeline_linear() {
        let cfg = linear_cfg();
        let refs = vec![
            make_def("x", 1),
            make_use("x", 3),
            make_def("x", 4),
            make_use("x", 5),
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let def_use = build_def_use_chains(&reaching, &cfg, &refs);
        let use_def = build_use_def_chains(&reaching, &cfg, &refs);

        // Verify chains are non-empty
        assert!(!def_use.is_empty());
        assert!(!use_def.is_empty());
    }

    /// Test: Complex scenario with multiple variables
    #[test]
    fn test_complex_multiple_variables() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 1),
            make_def("y", 2),
            make_def("x", 3), // true branch
            make_def("y", 5), // false branch
            make_use("x", 7),
            make_use("y", 8),
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let def_use = build_def_use_chains(&reaching, &cfg, &refs);
        let use_def = build_use_def_chains(&reaching, &cfg, &refs);

        // Should have chains for both variables
        assert!(def_use.iter().any(|c| c.definition.var == "x"));
        assert!(def_use.iter().any(|c| c.definition.var == "y"));
        assert!(use_def.iter().any(|c| c.var == "x"));
        assert!(use_def.iter().any(|c| c.var == "y"));
    }

    /// Test: Loop accumulator pattern
    #[test]
    fn test_loop_accumulator() {
        let cfg = loop_cfg();
        let refs = vec![
            make_def("total", 1), // total = 0
            make_use("total", 5), // total + i
            make_def("total", 5), // total = total + i
            make_use("total", 7), // return total
        ];

        let reaching = compute_reaching_definitions(&cfg, &refs);
        let use_def = build_use_def_chains(&reaching, &cfg, &refs);

        // The use at line 7 should see definitions from both line 1 and line 5
        let final_use = use_def.iter().find(|c| c.use_site.line == 7);
        assert!(
            final_use.is_some(),
            "Should have use-def chain for final use"
        );
    }
}

// =============================================================================
// Phase 15: Available Expressions Tests (RD-10)
// =============================================================================

#[cfg(test)]
mod available_expressions_tests {
    use super::fixtures::*;
    use super::*;

    // Note: Available expressions tests use SSA analysis module
    // See ssa/ssa_tests.rs for comprehensive available expressions tests

    /// Test: Expression availability basic concept
    #[test]
    fn test_available_exprs_concept() {
        // This test validates that the concept is implemented
        // Detailed testing is in ssa_tests.rs
        let cfg = linear_cfg();
        let refs = vec![
            make_def("a", 1),
            make_def("b", 1),
            make_use("a", 3),
            make_use("b", 3),
        ];

        // Reaching definitions should still work
        let reaching = compute_reaching_definitions(&cfg, &refs);
        assert!(!reaching.reaching_in.is_empty() || !reaching.reaching_out.is_empty());
    }
}

// =============================================================================
// Phase 15: Live Variables Extended Tests (RD-12)
// =============================================================================

#[cfg(test)]
mod live_variables_extended_tests {
    use super::fixtures::*;
    use super::*;

    // Note: Live variables analysis is in ssa/analysis.rs
    // These tests validate integration with reaching definitions

    /// Test: Live range concept validation
    #[test]
    fn test_live_range_concept() {
        let cfg = linear_cfg();
        let refs = vec![make_def("x", 1), make_use("x", 3), make_use("x", 5)];

        // Reaching definitions provides the foundation for liveness
        let reaching = compute_reaching_definitions(&cfg, &refs);

        // x should be reachable at blocks 1 and 2
        assert!(reaching.reaching_in.contains_key(&1));
        assert!(reaching.reaching_in.contains_key(&2));
    }
}

// =============================================================================
// Phase 16: RPO Worklist Extended Tests (RD-3)
// =============================================================================

#[cfg(test)]
mod rpo_extended_tests {
    use super::super::reaching::{compute_reaching_definitions_rpo, compute_rpo};
    use super::fixtures::*;
    use super::*;

    /// Test: RPO order is valid (predecessors before successors in acyclic regions)
    #[test]
    fn test_rpo_order() {
        let cfg = diamond_cfg();
        let rpo = compute_rpo(&cfg);

        // Entry block should come first
        assert_eq!(rpo.first(), Some(&0), "Entry should be first in RPO");

        // Should contain all blocks
        assert_eq!(rpo.len(), cfg.blocks.len(), "RPO should contain all blocks");

        // Build position map
        let pos: std::collections::HashMap<usize, usize> =
            rpo.iter().enumerate().map(|(i, &b)| (b, i)).collect();

        // For each edge, non-back-edge predecessors should come before successors
        // (This is the RPO property for acyclic regions)
        for edge in &cfg.edges {
            if edge.edge_type != EdgeType::BackEdge {
                let from_pos = pos.get(&edge.from).unwrap();
                let to_pos = pos.get(&edge.to).unwrap();
                // In RPO, predecessors come before successors
                assert!(
                    from_pos < to_pos,
                    "In RPO, block {} should come before {} (non-back-edge)",
                    edge.from,
                    edge.to
                );
            }
        }
    }

    /// Test: RPO converges in fewer iterations than arbitrary order
    #[test]
    fn test_rpo_fewer_iterations() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 1),
            make_def("x", 3),
            make_def("x", 5),
            make_use("x", 7),
        ];

        let rpo_result = compute_reaching_definitions_rpo(&cfg, &refs);

        // For a simple diamond CFG, should converge quickly
        assert!(
            rpo_result.iterations <= 3,
            "RPO should converge in 2-3 iterations for diamond CFG, got {}",
            rpo_result.iterations
        );

        // Results should be valid (match naive algorithm)
        let naive = compute_reaching_definitions(&cfg, &refs);
        for block in &cfg.blocks {
            assert_eq!(
                rpo_result.reaching.reaching_in.get(&block.id),
                naive.reaching_in.get(&block.id),
                "RPO IN should match naive for block {}",
                block.id
            );
        }
    }

    /// Test: RPO handles complex loop correctly
    #[test]
    fn test_rpo_complex_loop() {
        let cfg = loop_cfg();
        let refs = vec![make_def("i", 1), make_use("i", 3), make_def("i", 5)];

        let rpo_result = compute_reaching_definitions_rpo(&cfg, &refs);

        // Should converge (not infinite loop)
        assert!(
            rpo_result.iterations <= 10,
            "Should converge within reasonable iterations"
        );

        // Results should be valid
        let naive = compute_reaching_definitions(&cfg, &refs);
        for block in &cfg.blocks {
            assert_eq!(
                rpo_result.reaching.reaching_in.get(&block.id),
                naive.reaching_in.get(&block.id),
                "RPO IN should match naive for block {}",
                block.id
            );
        }
    }
}

// =============================================================================
// Phase 16: Bit Vector Tests (RD-4)
// =============================================================================

#[cfg(test)]
mod bitvec_tests {
    use super::super::reaching::{compute_reaching_definitions_bitvec, create_dense_def_mapping};
    use super::fixtures::*;
    use super::*;

    /// Test: Bit vector produces same result as HashSet
    #[test]
    fn test_bitvec_same_result() {
        let cfg = diamond_cfg();
        let refs = vec![
            make_def("x", 1),
            make_def("y", 2),
            make_def("x", 3),
            make_def("y", 5),
            make_use("x", 7),
            make_use("y", 7),
        ];

        let hashset_result = compute_reaching_definitions(&cfg, &refs);
        let bitvec_result = compute_reaching_definitions_bitvec(&cfg, &refs);
        let converted = bitvec_result.to_standard();

        // Compare IN and OUT sets for each block
        for block in &cfg.blocks {
            let hashset_in = hashset_result.reaching_in.get(&block.id);
            let bitvec_in = converted.reaching_in.get(&block.id);
            assert_eq!(
                hashset_in, bitvec_in,
                "IN sets should match for block {}",
                block.id
            );

            let hashset_out = hashset_result.reaching_out.get(&block.id);
            let bitvec_out = converted.reaching_out.get(&block.id);
            assert_eq!(
                hashset_out, bitvec_out,
                "OUT sets should match for block {}",
                block.id
            );
        }
    }

    /// Test: Dense mapping is correct
    #[test]
    fn test_dense_mapping() {
        let refs = vec![
            make_def("x", 1),
            make_use("x", 2),
            make_def("y", 3),
            make_def("x", 4), // Second def of x
        ];

        let mapping = create_dense_def_mapping(&refs);

        // Should have 3 definitions
        assert_eq!(mapping.num_defs, 3, "Should have 3 definitions");

        // Each bit position should map back to a valid DefId
        for (i, def_id) in mapping.bit_to_def.iter().enumerate() {
            let reverse_lookup = mapping.def_to_bit.get(def_id);
            assert_eq!(reverse_lookup, Some(&i), "Mapping should be bijective");
        }
    }

    /// Test: Bit vector handles empty CFG
    #[test]
    fn test_bitvec_empty_cfg() {
        let cfg = CfgInfo {
            function: "empty".to_string(),
            blocks: Vec::new(),
            edges: Vec::new(),
            entry_block: 0,
            exit_blocks: Vec::new(),
            cyclomatic_complexity: 0,
            nested_functions: HashMap::new(),
        };
        let refs: Vec<VarRef> = Vec::new();

        let result = compute_reaching_definitions_bitvec(&cfg, &refs);

        assert!(result.in_sets.is_empty());
        assert!(result.out_sets.is_empty());
        assert_eq!(result.iterations, 0);
    }

    /// Test: Bit vector handles loop correctly
    #[test]
    fn test_bitvec_loop() {
        let cfg = loop_cfg();
        let refs = vec![make_def("i", 1), make_use("i", 3), make_def("i", 5)];

        let bitvec_result = compute_reaching_definitions_bitvec(&cfg, &refs);
        let hashset_result = compute_reaching_definitions(&cfg, &refs);

        // Should converge to same result
        let converted = bitvec_result.to_standard();
        for block in &cfg.blocks {
            assert_eq!(
                hashset_result.reaching_in.get(&block.id),
                converted.reaching_in.get(&block.id),
                "Loop IN should match for block {}",
                block.id
            );
        }
    }

    /// Test: Bit vector performance (large definition set)
    #[test]
    fn test_bitvec_many_definitions() {
        // Create a linear CFG with many definitions
        let mut blocks = Vec::new();
        let mut edges = Vec::new();
        let mut refs = Vec::new();

        let num_blocks = 20;
        let defs_per_block = 10;

        for i in 0..num_blocks {
            blocks.push(CfgBlock {
                id: i,
                block_type: if i == 0 {
                    BlockType::Entry
                } else if i == num_blocks - 1 {
                    BlockType::Exit
                } else {
                    BlockType::Body
                },
                lines: (i as u32 * 10, i as u32 * 10 + 9),
                calls: Vec::new(),
            });

            if i < num_blocks - 1 {
                edges.push(CfgEdge {
                    from: i,
                    to: i + 1,
                    edge_type: EdgeType::Unconditional,
                    condition: None,
                });
            }

            // Add definitions
            for j in 0..defs_per_block {
                refs.push(make_def(&format!("v{}", j), i as u32 * 10 + j as u32));
            }
        }

        let cfg = CfgInfo {
            function: "many_defs".to_string(),
            blocks,
            edges,
            entry_block: 0,
            exit_blocks: vec![num_blocks - 1],
            cyclomatic_complexity: 1,
            nested_functions: HashMap::new(),
        };

        // Both should complete
        let bitvec_result = compute_reaching_definitions_bitvec(&cfg, &refs);
        let hashset_result = compute_reaching_definitions(&cfg, &refs);

        // Results should match
        let converted = bitvec_result.to_standard();
        for block in &cfg.blocks {
            assert_eq!(
                hashset_result.reaching_in.get(&block.id),
                converted.reaching_in.get(&block.id),
                "Many defs: IN should match for block {}",
                block.id
            );
        }

        // Verify we processed all definitions
        assert_eq!(
            bitvec_result.mapping.num_defs,
            num_blocks * defs_per_block,
            "Should have all definitions mapped"
        );
    }
}