fossil-mcp 0.1.8

Multi-language static analysis toolkit with MCP server. Detects dead code, code clones, and AI scaffolding.
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
//! Feature-flag detection for dead code analysis.
//!
//! Scans source files for feature-flag patterns (compile-time and runtime) and
//! identifies conditional blocks that may be always-dead based on static analysis
//! of the flag conditions.
//!
//! Supported patterns per language:
//! - **Rust**: `#[cfg(...)]`, `#[cfg(not(...))]`, `#[cfg(test)]`, `#[cfg(target_os = "...")]`
//! - **C/C++**: `#ifdef`, `#ifndef`, `#if defined(...)`, `#if 0`
//! - **Python**: `os.environ.get(...)`, `settings.FEATURE_FLAG`, `if DEBUG:`
//! - **JavaScript/TypeScript**: `process.env.FEATURE`, `config.featureFlag`, `import.meta.env.FEATURE`

use crate::core::Language;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::sync::OnceLock;

// =============================================================================
// Types
// =============================================================================

/// The kind of feature flag detected.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FlagType {
    /// Rust `#[cfg(...)]` attribute.
    RustCfg,
    /// C/C++ `#ifdef` / `#ifndef` / `#if defined(...)` / `#if 0`.
    CppIfdef,
    /// Runtime conditional feature check (Python `settings.FLAG`, etc.).
    ConditionalFeatureCheck,
    /// Environment variable check (`os.environ`, `process.env`, `import.meta.env`).
    EnvironmentVariable,
}

/// A single detected feature flag in source code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureFlag {
    /// Name or expression of the flag (e.g. `"feature = \"foo\""`, `"MY_FEATURE"`).
    pub name: String,
    /// File path where the flag was found.
    pub file: String,
    /// 1-based line number of the flag.
    pub line: usize,
    /// Classification of the flag pattern.
    pub flag_type: FlagType,
    /// Whether static analysis determines the flag to be always dead.
    pub is_always_dead: bool,
}

/// A block of code controlled by a feature flag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConditionalBlock {
    /// The feature flag that controls this block.
    pub flag: FeatureFlag,
    /// 1-based start line of the conditional block.
    pub start_line: usize,
    /// 1-based end line of the conditional block.
    pub end_line: usize,
    /// Number of non-empty source lines within the block.
    pub lines_of_code: usize,
}

// =============================================================================
// Compiled regex cache (one-time init)
// =============================================================================

struct RustPatterns {
    cfg_feature: Regex,
    cfg_not: Regex,
    cfg_test: Regex,
    cfg_target_os: Regex,
    cfg_generic: Regex,
}

struct CppPatterns {
    ifdef: Regex,
    ifndef: Regex,
    if_defined: Regex,
    if_zero: Regex,
    endif: Regex,
    else_directive: Regex,
    elif_directive: Regex,
}

struct PythonPatterns {
    environ_get: Regex,
    environ_bracket: Regex,
    settings_flag: Regex,
    if_debug: Regex,
    if_false: Regex,
    if_zero: Regex,
}

struct JsPatterns {
    process_env: Regex,
    config_flag: Regex,
    import_meta_env: Regex,
}

fn rust_patterns() -> &'static RustPatterns {
    static INSTANCE: OnceLock<RustPatterns> = OnceLock::new();
    INSTANCE.get_or_init(|| RustPatterns {
        cfg_feature: Regex::new(r#"#\[cfg\(feature\s*=\s*"([^"]+)"\)\]"#).unwrap(),
        cfg_not: Regex::new(r#"#\[cfg\(not\((.+?)\)\)\]"#).unwrap(),
        cfg_test: Regex::new(r"#\[cfg\(test\)\]").unwrap(),
        cfg_target_os: Regex::new(r#"#\[cfg\(target_os\s*=\s*"([^"]+)"\)\]"#).unwrap(),
        cfg_generic: Regex::new(r"#\[cfg\((.+?)\)\]").unwrap(),
    })
}

fn cpp_patterns() -> &'static CppPatterns {
    static INSTANCE: OnceLock<CppPatterns> = OnceLock::new();
    INSTANCE.get_or_init(|| CppPatterns {
        ifdef: Regex::new(r"^\s*#\s*ifdef\s+(\w+)").unwrap(),
        ifndef: Regex::new(r"^\s*#\s*ifndef\s+(\w+)").unwrap(),
        if_defined: Regex::new(r"^\s*#\s*if\s+defined\s*\(\s*(\w+)\s*\)").unwrap(),
        if_zero: Regex::new(r"^\s*#\s*if\s+0\s*$").unwrap(),
        endif: Regex::new(r"^\s*#\s*endif").unwrap(),
        else_directive: Regex::new(r"^\s*#\s*else").unwrap(),
        elif_directive: Regex::new(r"^\s*#\s*elif").unwrap(),
    })
}

fn python_patterns() -> &'static PythonPatterns {
    static INSTANCE: OnceLock<PythonPatterns> = OnceLock::new();
    INSTANCE.get_or_init(|| PythonPatterns {
        environ_get: Regex::new(r#"os\.environ\.get\(\s*["'](\w+)["']"#).unwrap(),
        environ_bracket: Regex::new(r#"os\.environ\[\s*["'](\w+)["']"#).unwrap(),
        settings_flag: Regex::new(r"settings\.([A-Z_][A-Z_0-9]*)").unwrap(),
        if_debug: Regex::new(r"^\s*if\s+DEBUG\s*:").unwrap(),
        if_false: Regex::new(r"^\s*if\s+False\s*:").unwrap(),
        if_zero: Regex::new(r"^\s*if\s+0\s*:").unwrap(),
    })
}

fn js_patterns() -> &'static JsPatterns {
    static INSTANCE: OnceLock<JsPatterns> = OnceLock::new();
    INSTANCE.get_or_init(|| JsPatterns {
        process_env: Regex::new(r"process\.env\.(\w+)").unwrap(),
        config_flag: Regex::new(r"config\.([a-zA-Z_]\w*)").unwrap(),
        import_meta_env: Regex::new(r"import\.meta\.env\.(\w+)").unwrap(),
    })
}

// =============================================================================
// FeatureFlagDetector
// =============================================================================

/// Detects feature flags and conditional blocks in source code.
pub struct FeatureFlagDetector;

impl FeatureFlagDetector {
    /// Scan source text for feature flag patterns appropriate to the given language.
    pub fn detect_flags(source: &str, file_path: &str, language: Language) -> Vec<FeatureFlag> {
        match language {
            Language::Rust => Self::detect_rust_flags(source, file_path),
            Language::C | Language::Cpp => Self::detect_cpp_flags(source, file_path),
            Language::Python => Self::detect_python_flags(source, file_path),
            Language::JavaScript | Language::TypeScript => Self::detect_js_flags(source, file_path),
            _ => Vec::new(),
        }
    }

    /// Return only the flags that are statically determined to be always dead.
    pub fn find_always_dead_flags(flags: &[FeatureFlag]) -> Vec<&FeatureFlag> {
        flags.iter().filter(|f| f.is_always_dead).collect()
    }

    /// Identify conditional blocks controlled by feature flags.
    pub fn find_conditional_blocks(
        source: &str,
        file_path: &str,
        language: Language,
    ) -> Vec<ConditionalBlock> {
        match language {
            Language::Rust => Self::find_rust_conditional_blocks(source, file_path),
            Language::C | Language::Cpp => Self::find_cpp_conditional_blocks(source, file_path),
            Language::Python => Self::find_python_conditional_blocks(source, file_path),
            Language::JavaScript | Language::TypeScript => {
                Self::find_js_conditional_blocks(source, file_path)
            }
            _ => Vec::new(),
        }
    }

    // =========================================================================
    // Rust
    // =========================================================================

    fn detect_rust_flags(source: &str, file_path: &str) -> Vec<FeatureFlag> {
        let pats = rust_patterns();
        let mut flags = Vec::new();

        for (line_idx, line) in source.lines().enumerate() {
            let line_num = line_idx + 1;

            // #[cfg(feature = "...")]
            if let Some(cap) = pats.cfg_feature.captures(line) {
                flags.push(FeatureFlag {
                    name: format!("feature = \"{}\"", &cap[1]),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::RustCfg,
                    is_always_dead: false,
                });
                continue;
            }

            // #[cfg(test)]
            if pats.cfg_test.is_match(line) {
                flags.push(FeatureFlag {
                    name: "test".to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::RustCfg,
                    is_always_dead: false,
                });
                continue;
            }

            // #[cfg(target_os = "...")]
            if let Some(cap) = pats.cfg_target_os.captures(line) {
                flags.push(FeatureFlag {
                    name: format!("target_os = \"{}\"", &cap[1]),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::RustCfg,
                    is_always_dead: false,
                });
                continue;
            }

            // #[cfg(not(...))] -- check for always-dead contradictions
            if let Some(cap) = pats.cfg_not.captures(line) {
                let inner = cap[1].trim();
                let is_always_dead = Self::is_rust_cfg_not_always_dead(inner);
                flags.push(FeatureFlag {
                    name: format!("not({})", inner),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::RustCfg,
                    is_always_dead,
                });
                continue;
            }

            // Generic #[cfg(...)] that wasn't caught above
            if let Some(cap) = pats.cfg_generic.captures(line) {
                let inner = cap[1].trim();
                // Skip if we already matched a more specific pattern
                if !flags.iter().any(|f| f.line == line_num) {
                    flags.push(FeatureFlag {
                        name: inner.to_string(),
                        file: file_path.to_string(),
                        line: line_num,
                        flag_type: FlagType::RustCfg,
                        is_always_dead: false,
                    });
                }
            }
        }

        flags
    }

    /// Heuristic: `not(any(...))` with contradictory conditions is always dead.
    ///
    /// For example `not(any(unix, windows, target_os = "macos"))` is considered
    /// always dead because the disjunction covers all practical targets.
    fn is_rust_cfg_not_always_dead(inner: &str) -> bool {
        // `not(any(unix, windows))` -- covers all mainstream platforms
        if inner.starts_with("any(") && inner.ends_with(')') {
            let body = &inner[4..inner.len() - 1];
            let parts: Vec<&str> = body.split(',').map(|s| s.trim()).collect();
            let has_unix = parts.contains(&"unix");
            let has_windows = parts.contains(&"windows");
            if has_unix && has_windows {
                return true;
            }
        }
        false
    }

    // =========================================================================
    // C / C++
    // =========================================================================

    fn detect_cpp_flags(source: &str, file_path: &str) -> Vec<FeatureFlag> {
        let pats = cpp_patterns();
        let mut flags = Vec::new();

        for (line_idx, line) in source.lines().enumerate() {
            let line_num = line_idx + 1;

            // #if 0  (always dead)
            if pats.if_zero.is_match(line) {
                flags.push(FeatureFlag {
                    name: "0".to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::CppIfdef,
                    is_always_dead: true,
                });
                continue;
            }

            // #ifdef FEATURE
            if let Some(cap) = pats.ifdef.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::CppIfdef,
                    is_always_dead: false,
                });
                continue;
            }

            // #ifndef FEATURE
            if let Some(cap) = pats.ifndef.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::CppIfdef,
                    is_always_dead: false,
                });
                continue;
            }

            // #if defined(FEATURE)
            if let Some(cap) = pats.if_defined.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::CppIfdef,
                    is_always_dead: false,
                });
            }
        }

        flags
    }

    // =========================================================================
    // Python
    // =========================================================================

    fn detect_python_flags(source: &str, file_path: &str) -> Vec<FeatureFlag> {
        let pats = python_patterns();
        let mut flags = Vec::new();

        for (line_idx, line) in source.lines().enumerate() {
            let line_num = line_idx + 1;

            // if False:  (always dead)
            if pats.if_false.is_match(line) {
                flags.push(FeatureFlag {
                    name: "False".to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::ConditionalFeatureCheck,
                    is_always_dead: true,
                });
                continue;
            }

            // if 0:  (always dead)
            if pats.if_zero.is_match(line) {
                flags.push(FeatureFlag {
                    name: "0".to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::ConditionalFeatureCheck,
                    is_always_dead: true,
                });
                continue;
            }

            // if DEBUG:
            if pats.if_debug.is_match(line) {
                flags.push(FeatureFlag {
                    name: "DEBUG".to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::ConditionalFeatureCheck,
                    is_always_dead: false,
                });
                continue;
            }

            // os.environ.get("FEATURE")
            if let Some(cap) = pats.environ_get.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::EnvironmentVariable,
                    is_always_dead: false,
                });
                continue;
            }

            // os.environ["FEATURE"]
            if let Some(cap) = pats.environ_bracket.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::EnvironmentVariable,
                    is_always_dead: false,
                });
                continue;
            }

            // settings.FEATURE_FLAG
            if let Some(cap) = pats.settings_flag.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::ConditionalFeatureCheck,
                    is_always_dead: false,
                });
            }
        }

        flags
    }

    // =========================================================================
    // JavaScript / TypeScript
    // =========================================================================

    fn detect_js_flags(source: &str, file_path: &str) -> Vec<FeatureFlag> {
        let pats = js_patterns();
        let mut flags = Vec::new();

        for (line_idx, line) in source.lines().enumerate() {
            let line_num = line_idx + 1;

            // import.meta.env.FEATURE  (must come before config.* to avoid false match)
            if let Some(cap) = pats.import_meta_env.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::EnvironmentVariable,
                    is_always_dead: false,
                });
                continue;
            }

            // process.env.FEATURE
            if let Some(cap) = pats.process_env.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::EnvironmentVariable,
                    is_always_dead: false,
                });
                continue;
            }

            // config.featureFlag
            if let Some(cap) = pats.config_flag.captures(line) {
                flags.push(FeatureFlag {
                    name: cap[1].to_string(),
                    file: file_path.to_string(),
                    line: line_num,
                    flag_type: FlagType::ConditionalFeatureCheck,
                    is_always_dead: false,
                });
            }
        }

        flags
    }

    // =========================================================================
    // Conditional block detection — Rust
    // =========================================================================

    fn find_rust_conditional_blocks(source: &str, file_path: &str) -> Vec<ConditionalBlock> {
        let flags = Self::detect_rust_flags(source, file_path);
        let lines: Vec<&str> = source.lines().collect();
        let mut blocks = Vec::new();

        for flag in flags {
            let start_line = flag.line;
            // The block controlled by a #[cfg(..)] starts on the line after the attribute.
            // Look for the end of the item: balanced braces or single-line item.
            let block_start = start_line; // inclusive, attribute line
            if let Some(end_line) = Self::find_rust_item_end(&lines, start_line) {
                let loc = Self::count_non_empty_lines(&lines, start_line, end_line);
                blocks.push(ConditionalBlock {
                    flag,
                    start_line: block_start,
                    end_line,
                    lines_of_code: loc,
                });
            }
        }

        blocks
    }

    /// Starting from the line after the attribute, find the end of the Rust item.
    fn find_rust_item_end(lines: &[&str], attr_line: usize) -> Option<usize> {
        // attr_line is 1-based; start scanning from the item line itself
        let item_start_idx = attr_line; // 0-based index = attr_line (because attr_line is 1-based, so next line is index attr_line)
        if item_start_idx >= lines.len() {
            return None;
        }

        // If the next line after the attribute opens a brace block, find closing brace
        let mut depth: i32 = 0;
        let mut found_open = false;
        for (idx, line) in lines.iter().enumerate().skip(item_start_idx) {
            for ch in line.chars() {
                match ch {
                    '{' => {
                        depth += 1;
                        found_open = true;
                    }
                    '}' => {
                        depth -= 1;
                    }
                    _ => {}
                }
            }
            if found_open && depth <= 0 {
                return Some(idx + 1); // 1-based
            }
            // Single-line item (e.g. `fn foo();` or `use bar;`)
            if !found_open && line.trim_end().ends_with(';') {
                return Some(idx + 1); // 1-based
            }
        }

        // Fallback: just the attribute line plus one
        if !found_open {
            return Some(attr_line + 1);
        }

        None
    }

    // =========================================================================
    // Conditional block detection -- C/C++
    // =========================================================================

    fn find_cpp_conditional_blocks(source: &str, file_path: &str) -> Vec<ConditionalBlock> {
        let flags = Self::detect_cpp_flags(source, file_path);
        let pats = cpp_patterns();
        let lines: Vec<&str> = source.lines().collect();
        let mut blocks = Vec::new();

        for flag in flags {
            let start_line = flag.line;
            // Scan forward from the directive to find matching #endif, tracking nesting
            let start_idx = start_line; // 0-based index of first line inside the block
            let mut depth: u32 = 1;
            let mut end_line = None;

            for (idx, line) in lines.iter().enumerate().skip(start_idx) {
                // Nested #if / #ifdef / #ifndef
                if pats.ifdef.is_match(line)
                    || pats.ifndef.is_match(line)
                    || pats.if_defined.is_match(line)
                    || pats.if_zero.is_match(line)
                {
                    depth += 1;
                }
                if pats.endif.is_match(line) {
                    depth -= 1;
                    if depth == 0 {
                        end_line = Some(idx + 1); // 1-based
                        break;
                    }
                }
            }

            if let Some(end) = end_line {
                // For always-dead #if 0 blocks, the controlled region is between the
                // #if 0 and the matching #endif (or #else/#elif).
                let effective_end = Self::find_cpp_effective_end(&lines, start_line, end, pats);
                let loc = Self::count_non_empty_lines(&lines, start_line, effective_end);
                blocks.push(ConditionalBlock {
                    flag,
                    start_line,
                    end_line: effective_end,
                    lines_of_code: loc,
                });
            }
        }

        blocks
    }

    /// For `#if 0`, the dead region ends at the first `#else` or `#elif` at the
    /// same nesting depth, or at `#endif` if neither is present.
    fn find_cpp_effective_end(
        lines: &[&str],
        start_line: usize,
        endif_line: usize,
        pats: &CppPatterns,
    ) -> usize {
        let start_idx = start_line; // 0-based index of line after directive
        let end_idx = endif_line - 1; // 0-based index of #endif line
        let mut depth: u32 = 0;

        for (idx, line) in lines.iter().enumerate().take(end_idx).skip(start_idx) {
            if pats.ifdef.is_match(line)
                || pats.ifndef.is_match(line)
                || pats.if_defined.is_match(line)
                || pats.if_zero.is_match(line)
            {
                depth += 1;
            }
            if pats.endif.is_match(line) {
                depth = depth.saturating_sub(1);
            }
            if depth == 0
                && (pats.else_directive.is_match(line) || pats.elif_directive.is_match(line))
            {
                return idx + 1; // 1-based
            }
        }

        endif_line
    }

    // =========================================================================
    // Conditional block detection -- Python
    // =========================================================================

    fn find_python_conditional_blocks(source: &str, file_path: &str) -> Vec<ConditionalBlock> {
        let flags = Self::detect_python_flags(source, file_path);
        let lines: Vec<&str> = source.lines().collect();
        let mut blocks = Vec::new();

        for flag in flags {
            let start_line = flag.line;
            if let Some(end_line) = Self::find_python_block_end(&lines, start_line) {
                let loc = Self::count_non_empty_lines(&lines, start_line, end_line);
                blocks.push(ConditionalBlock {
                    flag,
                    start_line,
                    end_line,
                    lines_of_code: loc,
                });
            }
        }

        blocks
    }

    /// Find the end of a Python indented block starting from the `if ...:` line.
    fn find_python_block_end(lines: &[&str], if_line: usize) -> Option<usize> {
        let if_idx = if_line - 1; // 0-based
        if if_idx >= lines.len() {
            return None;
        }

        // Determine the indentation of the `if` line
        let if_indent = Self::leading_spaces(lines[if_idx]);

        // Everything indented more than the `if` line belongs to its block
        let mut last_body_line = if_line; // 1-based
        for (idx, line) in lines.iter().enumerate().skip(if_idx + 1) {
            if line.trim().is_empty() {
                continue; // blank lines don't end the block
            }
            let indent = Self::leading_spaces(line);
            if indent <= if_indent {
                break;
            }
            last_body_line = idx + 1; // 1-based
        }

        Some(last_body_line)
    }

    // =========================================================================
    // Conditional block detection -- JS/TS
    // =========================================================================

    fn find_js_conditional_blocks(source: &str, file_path: &str) -> Vec<ConditionalBlock> {
        let flags = Self::detect_js_flags(source, file_path);
        let lines: Vec<&str> = source.lines().collect();
        let mut blocks = Vec::new();

        for flag in flags {
            let start_line = flag.line;
            if let Some(end_line) = Self::find_js_block_end(&lines, start_line) {
                let loc = Self::count_non_empty_lines(&lines, start_line, end_line);
                blocks.push(ConditionalBlock {
                    flag,
                    start_line,
                    end_line,
                    lines_of_code: loc,
                });
            }
        }

        blocks
    }

    /// Find the end of a JS/TS block by balanced braces starting from the flag line.
    fn find_js_block_end(lines: &[&str], flag_line: usize) -> Option<usize> {
        let start_idx = flag_line - 1; // 0-based
        if start_idx >= lines.len() {
            return None;
        }

        let mut depth: i32 = 0;
        let mut found_open = false;

        for (idx, line) in lines.iter().enumerate().skip(start_idx) {
            for ch in line.chars() {
                match ch {
                    '{' => {
                        depth += 1;
                        found_open = true;
                    }
                    '}' => depth -= 1,
                    _ => {}
                }
            }
            if found_open && depth <= 0 {
                return Some(idx + 1); // 1-based
            }
        }

        // No braces found -- single-line conditional
        Some(flag_line)
    }

    // =========================================================================
    // Helpers
    // =========================================================================

    /// Count non-empty lines between `start` and `end` (both 1-based, inclusive).
    fn count_non_empty_lines(lines: &[&str], start: usize, end: usize) -> usize {
        let from = start.saturating_sub(1); // to 0-based
        let to = end.min(lines.len()); // exclusive upper bound
        lines[from..to]
            .iter()
            .filter(|l| !l.trim().is_empty())
            .count()
    }

    /// Count leading space characters (spaces and tabs, where tab = 4 spaces).
    fn leading_spaces(line: &str) -> usize {
        let mut count = 0;
        for ch in line.chars() {
            match ch {
                ' ' => count += 1,
                '\t' => count += 4,
                _ => break,
            }
        }
        count
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    // -------------------------------------------------------------------------
    // Rust cfg detection
    // -------------------------------------------------------------------------

    #[test]
    fn test_rust_cfg_feature() {
        let source = r#"
#[cfg(feature = "serde")]
fn serialize() {}

#[cfg(feature = "async")]
fn async_run() {}
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "lib.rs", Language::Rust);
        assert_eq!(flags.len(), 2);
        assert_eq!(flags[0].name, r#"feature = "serde""#);
        assert_eq!(flags[0].flag_type, FlagType::RustCfg);
        assert_eq!(flags[0].line, 2);
        assert!(!flags[0].is_always_dead);
        assert_eq!(flags[1].name, r#"feature = "async""#);
    }

    #[test]
    fn test_rust_cfg_test() {
        let source = r#"
#[cfg(test)]
mod tests {
    fn test_something() {}
}
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "lib.rs", Language::Rust);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "test");
        assert_eq!(flags[0].flag_type, FlagType::RustCfg);
    }

    #[test]
    fn test_rust_cfg_target_os() {
        let source = r#"#[cfg(target_os = "linux")]
fn linux_only() {}
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "lib.rs", Language::Rust);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, r#"target_os = "linux""#);
    }

    #[test]
    fn test_rust_cfg_not_always_dead() {
        let source = r#"
#[cfg(not(any(unix, windows)))]
fn exotic_platform() {}
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "lib.rs", Language::Rust);
        assert_eq!(flags.len(), 1);
        assert!(
            flags[0].is_always_dead,
            "not(any(unix, windows)) should be always dead"
        );
        assert_eq!(flags[0].name, "not(any(unix, windows))");
    }

    #[test]
    fn test_rust_cfg_not_not_always_dead() {
        // not(unix) alone is NOT always dead -- Windows exists
        let source = r#"
#[cfg(not(unix))]
fn non_unix() {}
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "lib.rs", Language::Rust);
        assert_eq!(flags.len(), 1);
        assert!(!flags[0].is_always_dead);
    }

    // -------------------------------------------------------------------------
    // C/C++ ifdef detection
    // -------------------------------------------------------------------------

    #[test]
    fn test_cpp_ifdef() {
        let source = r#"
#ifdef ENABLE_LOGGING
    log("hello");
#endif

#ifndef NDEBUG
    assert(x > 0);
#endif
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "main.c", Language::C);
        assert_eq!(flags.len(), 2);
        assert_eq!(flags[0].name, "ENABLE_LOGGING");
        assert_eq!(flags[0].flag_type, FlagType::CppIfdef);
        assert!(!flags[0].is_always_dead);
        assert_eq!(flags[1].name, "NDEBUG");
    }

    #[test]
    fn test_cpp_if_defined() {
        let source = "#if defined(MY_FEATURE)\n    do_stuff();\n#endif\n";
        let flags = FeatureFlagDetector::detect_flags(source, "main.cpp", Language::Cpp);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "MY_FEATURE");
        assert_eq!(flags[0].flag_type, FlagType::CppIfdef);
    }

    #[test]
    fn test_cpp_if_zero_always_dead() {
        let source = "#if 0\n    dead_code();\n#endif\n";
        let flags = FeatureFlagDetector::detect_flags(source, "main.c", Language::C);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "0");
        assert!(flags[0].is_always_dead, "#if 0 should be always dead");
    }

    #[test]
    fn test_cpp_always_dead_filter() {
        let source = "#ifdef FOO\nstuff();\n#endif\n#if 0\ndead();\n#endif\n";
        let flags = FeatureFlagDetector::detect_flags(source, "main.c", Language::C);
        let dead = FeatureFlagDetector::find_always_dead_flags(&flags);
        assert_eq!(dead.len(), 1);
        assert_eq!(dead[0].name, "0");
    }

    // -------------------------------------------------------------------------
    // Python feature flag patterns
    // -------------------------------------------------------------------------

    #[test]
    fn test_python_environ_get() {
        let source = r#"
if os.environ.get("FEATURE_X"):
    enable_feature()
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "app.py", Language::Python);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "FEATURE_X");
        assert_eq!(flags[0].flag_type, FlagType::EnvironmentVariable);
    }

    #[test]
    fn test_python_environ_bracket() {
        let source = "val = os.environ[\"MY_VAR\"]\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.py", Language::Python);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "MY_VAR");
        assert_eq!(flags[0].flag_type, FlagType::EnvironmentVariable);
    }

    #[test]
    fn test_python_settings_flag() {
        let source = "if settings.FEATURE_FLAG:\n    do_thing()\n";
        let flags = FeatureFlagDetector::detect_flags(source, "views.py", Language::Python);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "FEATURE_FLAG");
        assert_eq!(flags[0].flag_type, FlagType::ConditionalFeatureCheck);
    }

    #[test]
    fn test_python_if_debug() {
        let source = "if DEBUG:\n    print('debug mode')\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.py", Language::Python);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "DEBUG");
        assert_eq!(flags[0].flag_type, FlagType::ConditionalFeatureCheck);
        assert!(!flags[0].is_always_dead);
    }

    #[test]
    fn test_python_if_false_always_dead() {
        let source = "if False:\n    dead_code()\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.py", Language::Python);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "False");
        assert!(flags[0].is_always_dead);
    }

    #[test]
    fn test_python_if_zero_always_dead() {
        let source = "if 0:\n    dead_code()\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.py", Language::Python);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "0");
        assert!(flags[0].is_always_dead);
    }

    // -------------------------------------------------------------------------
    // JavaScript / TypeScript feature flag patterns
    // -------------------------------------------------------------------------

    #[test]
    fn test_js_process_env() {
        let source = "if (process.env.ENABLE_CACHE) {\n    setupCache();\n}\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.js", Language::JavaScript);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "ENABLE_CACHE");
        assert_eq!(flags[0].flag_type, FlagType::EnvironmentVariable);
    }

    #[test]
    fn test_js_config_flag() {
        let source = "if (config.featureFlag) {\n    doSomething();\n}\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.js", Language::JavaScript);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "featureFlag");
        assert_eq!(flags[0].flag_type, FlagType::ConditionalFeatureCheck);
    }

    #[test]
    fn test_ts_import_meta_env() {
        let source = "if (import.meta.env.VITE_FEATURE) {\n    activate();\n}\n";
        let flags = FeatureFlagDetector::detect_flags(source, "app.ts", Language::TypeScript);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "VITE_FEATURE");
        assert_eq!(flags[0].flag_type, FlagType::EnvironmentVariable);
    }

    #[test]
    fn test_ts_process_env() {
        let source = "const x = process.env.NODE_ENV;\n";
        let flags = FeatureFlagDetector::detect_flags(source, "server.ts", Language::TypeScript);
        assert_eq!(flags.len(), 1);
        assert_eq!(flags[0].name, "NODE_ENV");
    }

    // -------------------------------------------------------------------------
    // Always-dead detection (cross-language)
    // -------------------------------------------------------------------------

    #[test]
    fn test_find_always_dead_flags_mixed() {
        let mut flags = vec![
            FeatureFlag {
                name: "FOO".to_string(),
                file: "a.c".to_string(),
                line: 1,
                flag_type: FlagType::CppIfdef,
                is_always_dead: false,
            },
            FeatureFlag {
                name: "0".to_string(),
                file: "a.c".to_string(),
                line: 5,
                flag_type: FlagType::CppIfdef,
                is_always_dead: true,
            },
            FeatureFlag {
                name: "False".to_string(),
                file: "b.py".to_string(),
                line: 3,
                flag_type: FlagType::ConditionalFeatureCheck,
                is_always_dead: true,
            },
        ];
        let dead = FeatureFlagDetector::find_always_dead_flags(&flags);
        assert_eq!(dead.len(), 2);
        assert!(dead.iter().all(|f| f.is_always_dead));

        // Mutate to verify independence
        flags[1].is_always_dead = false;
        let dead2 = FeatureFlagDetector::find_always_dead_flags(&flags);
        assert_eq!(dead2.len(), 1);
    }

    // -------------------------------------------------------------------------
    // Conditional block detection
    // -------------------------------------------------------------------------

    #[test]
    fn test_rust_conditional_blocks() {
        let source = r#"
#[cfg(feature = "serde")]
fn serialize() {
    do_serialize();
}
"#;
        let blocks = FeatureFlagDetector::find_conditional_blocks(source, "lib.rs", Language::Rust);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].flag.name, r#"feature = "serde""#);
        assert_eq!(blocks[0].start_line, 2);
        assert_eq!(blocks[0].end_line, 5);
        assert!(blocks[0].lines_of_code >= 3); // attribute + fn + body + closing brace
    }

    #[test]
    fn test_cpp_conditional_blocks() {
        let source = "\
#if 0
    dead_code();
    more_dead();
#endif
";
        let blocks = FeatureFlagDetector::find_conditional_blocks(source, "main.c", Language::C);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].flag.name, "0");
        assert!(blocks[0].flag.is_always_dead);
        assert_eq!(blocks[0].start_line, 1);
        assert_eq!(blocks[0].end_line, 4);
        assert!(blocks[0].lines_of_code >= 3);
    }

    #[test]
    fn test_cpp_conditional_block_with_else() {
        let source = "\
#if 0
    dead_code();
#else
    live_code();
#endif
";
        let blocks = FeatureFlagDetector::find_conditional_blocks(source, "main.c", Language::C);
        assert_eq!(blocks.len(), 1);
        // The dead region should end at #else, not at #endif
        assert_eq!(blocks[0].start_line, 1);
        assert_eq!(blocks[0].end_line, 3);
    }

    #[test]
    fn test_python_conditional_blocks() {
        let source = "\
if False:
    dead_a()
    dead_b()
live_code()
";
        let blocks =
            FeatureFlagDetector::find_conditional_blocks(source, "app.py", Language::Python);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].start_line, 1);
        assert_eq!(blocks[0].end_line, 3);
        assert_eq!(blocks[0].lines_of_code, 3); // if False: + 2 body lines
    }

    #[test]
    fn test_js_conditional_blocks() {
        let source = "\
if (process.env.FEATURE) {
    enableFeature();
    setupStuff();
}
";
        let blocks =
            FeatureFlagDetector::find_conditional_blocks(source, "app.js", Language::JavaScript);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].start_line, 1);
        assert_eq!(blocks[0].end_line, 4);
        assert_eq!(blocks[0].lines_of_code, 4);
    }

    // -------------------------------------------------------------------------
    // Unsupported languages return empty
    // -------------------------------------------------------------------------

    #[test]
    fn test_unsupported_language() {
        let flags = FeatureFlagDetector::detect_flags("fn main() {}", "foo.go", Language::Go);
        assert!(flags.is_empty());
        let blocks =
            FeatureFlagDetector::find_conditional_blocks("fn main() {}", "foo.go", Language::Go);
        assert!(blocks.is_empty());
    }

    // -------------------------------------------------------------------------
    // Edge cases
    // -------------------------------------------------------------------------

    #[test]
    fn test_empty_source() {
        let flags = FeatureFlagDetector::detect_flags("", "empty.rs", Language::Rust);
        assert!(flags.is_empty());
    }

    #[test]
    fn test_no_flags_in_source() {
        let source = "fn main() {\n    println!(\"Hello\");\n}\n";
        let flags = FeatureFlagDetector::detect_flags(source, "main.rs", Language::Rust);
        assert!(flags.is_empty());
    }

    #[test]
    fn test_multiple_flags_same_file() {
        let source = r#"
#[cfg(feature = "a")]
fn feat_a() {}

#[cfg(feature = "b")]
fn feat_b() {}

#[cfg(test)]
mod tests {}
"#;
        let flags = FeatureFlagDetector::detect_flags(source, "lib.rs", Language::Rust);
        assert_eq!(flags.len(), 3);
        assert!(flags.iter().all(|f| f.file == "lib.rs"));
    }

    #[test]
    fn test_cpp_nested_ifdef() {
        let source = "\
#ifdef OUTER
    #ifdef INNER
        inner_code();
    #endif
    outer_code();
#endif
";
        let flags = FeatureFlagDetector::detect_flags(source, "test.c", Language::C);
        assert_eq!(flags.len(), 2);
        assert_eq!(flags[0].name, "OUTER");
        assert_eq!(flags[1].name, "INNER");

        let blocks = FeatureFlagDetector::find_conditional_blocks(source, "test.c", Language::C);
        assert_eq!(blocks.len(), 2);
        // Outer block should span from #ifdef OUTER to the matching #endif
        assert_eq!(blocks[0].start_line, 1);
        assert_eq!(blocks[0].end_line, 6);
    }
}