sqc 0.4.13

Software Code Quality - CERT C compliance checker
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
use anyhow::{Context, Result};
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Write;
use std::path::PathBuf;
use walkdir::WalkDir;

#[derive(Deserialize)]
struct RuleConfig {
    rules: Option<HashMap<String, HashMap<String, RuleSettings>>>,
    rule: Option<RuleSingleSettings>,
}

#[derive(Deserialize)]
struct RuleSettings {
    enabled: bool,
}

#[derive(Deserialize)]
struct RuleSingleSettings {
    enabled: bool,
}

fn main() {
    // Only compile resources on Windows
    #[cfg(target_os = "windows")]
    {
        let mut res = winres::WindowsResource::new();
        res.set_icon("assets/icon.ico");
        if let Err(e) = res.compile() {
            eprintln!("Warning: Failed to compile Windows resources: {}", e);
        }
    }

    // Generate rules-all.toml from individual RULE-ID.toml files
    if let Err(e) = generate_rules_all_toml() {
        eprintln!("Error generating rules-all.toml: {}", e);
        std::process::exit(1);
    }

    // Generate integration tests from C test files
    if let Err(e) = generate_integration_tests() {
        eprintln!("Error generating integration tests: {}", e);
        std::process::exit(1);
    }
}

fn generate_rules_all_toml() -> Result<()> {
    let rules_dir = PathBuf::from("src/rules");

    // Find all ruleset directories (direct children of src/rules/)
    let rulesets: Vec<PathBuf> = match fs::read_dir(&rules_dir) {
        Ok(entries) => entries
            .filter_map(|e| match e {
                Ok(entry) => Some(entry.path()),
                Err(err) => {
                    eprintln!("Warning: Skipping directory entry in src/rules: {}", err);
                    None
                }
            })
            .filter(|p| p.is_dir())
            .collect(),
        Err(e) => {
            eprintln!("Warning: Could not read src/rules directory: {}", e);
            return Ok(()); // Not fatal - continue build
        }
    };

    // Process each ruleset
    for ruleset_dir in rulesets {
        let ruleset_name = match ruleset_dir.file_name() {
            Some(name) => name.to_string_lossy().to_string(),
            None => {
                eprintln!(
                    "Warning: Invalid ruleset directory path: {}",
                    ruleset_dir.display()
                );
                continue;
            }
        };

        let output_path = ruleset_dir.join("rules-all.toml");

        // Collect all RULE-ID.toml files in this ruleset
        let mut toml_files: Vec<PathBuf> = WalkDir::new(&ruleset_dir)
            .into_iter()
            .filter_map(|e| match e {
                Ok(entry) => Some(entry),
                Err(err) => {
                    eprintln!(
                        "Warning: Skipping entry in {}: {}",
                        ruleset_dir.display(),
                        err
                    );
                    None
                }
            })
            .filter(|e| {
                e.path().is_file()
                    && e.path().extension().is_some_and(|ext| ext == "toml")
                    && e.path()
                        .file_name()
                        .and_then(|name| name.to_str())
                        .is_some_and(|name_str| {
                            // Match pattern like ARR30-C.toml, but exclude rules-all.toml
                            name_str != "rules-all.toml"
                                && name_str.contains('-')
                                && !name_str.starts_with('.')
                        })
            })
            .map(|e| e.path().to_path_buf())
            .collect();

        if toml_files.is_empty() {
            println!("No rule manifests found in ruleset: {}", ruleset_name);
            continue;
        }

        // Sort by rule ID for consistent output
        toml_files.sort();

        // Combine all TOML files - extract only the rules.cert_c section
        let mut combined_content = String::new();
        combined_content.push_str("# Auto-generated file - do not edit directly\n");
        combined_content.push_str(&format!(
            "# Generated from individual rule manifests in {}\n",
            ruleset_name
        ));
        combined_content.push_str("# To modify, edit the individual TOML files and rebuild\n");
        combined_content.push_str("# Full metadata is in individual rule TOML files\n\n");

        for toml_path in &toml_files {
            // Read full content
            match fs::read_to_string(toml_path) {
                Ok(content) => {
                    // Extract the [rules.*] section (cert_c, brules, etc.)
                    if let Some(start_idx) = content.find("[rules.") {
                        let section_content = &content[start_idx..];

                        // Find the end of this section (next section or end of file)
                        let end_idx = section_content
                            .find("\n[")
                            .map(|i| i + 1)
                            .unwrap_or(section_content.len());

                        let rule_section = &section_content[..end_idx];
                        combined_content.push_str(rule_section);
                        if !rule_section.ends_with('\n') {
                            combined_content.push('\n');
                        }
                        combined_content.push('\n');
                    }
                }
                Err(e) => {
                    eprintln!("Warning: Failed to read {}: {}", toml_path.display(), e);
                }
            }
        }

        // Write combined file
        let mut file = File::create(&output_path).context(format!(
            "Failed to create rules-all.toml at {}",
            output_path.display()
        ))?;

        file.write_all(combined_content.as_bytes())
            .context(format!(
                "Failed to write rules-all.toml to {}",
                output_path.display()
            ))?;

        println!("cargo:rerun-if-changed={}", ruleset_dir.display());
        println!(
            "Generated {} rules-all.toml with {} rule manifests",
            ruleset_name,
            toml_files.len()
        );
    }

    Ok(())
}

fn generate_integration_tests() -> Result<()> {
    let out_dir = std::env::var("OUT_DIR").context("OUT_DIR environment variable not set")?;
    let out_dir_path = PathBuf::from(&out_dir);

    // Create tests subdirectory
    let tests_dir = out_dir_path.join("tests");
    fs::create_dir_all(&tests_dir).context("Failed to create tests directory")?;

    // Track all rule modules for the main file
    let mut rule_modules = Vec::new();

    let cert_c_dir = PathBuf::from("src/rules/cert_c");

    // Walk through CATEGORY directories
    let category_entries = fs::read_dir(&cert_c_dir)
        .context("Failed to read src/rules/cert_c directory - does it exist?")?;

    for category_entry in category_entries {
        let category_entry = match category_entry {
            Ok(entry) => entry,
            Err(e) => {
                eprintln!("Warning: Skipping category entry: {}", e);
                continue;
            }
        };

        let category_path = category_entry.path();

        if !category_path.is_dir() {
            continue;
        }

        let category_name = match category_path.file_name().and_then(|n| n.to_str()) {
            Some(name) => name,
            None => {
                eprintln!(
                    "Warning: Skipping category with invalid path: {}",
                    category_path.display()
                );
                continue;
            }
        };

        // Skip special directories
        if category_name == "tests" || category_name == "utils" || category_name.starts_with('.') {
            continue;
        }

        // Walk through RULE-ID directories
        let rule_entries = match fs::read_dir(&category_path) {
            Ok(entries) => entries,
            Err(e) => {
                eprintln!("Warning: Skipping category {}: {}", category_name, e);
                continue;
            }
        };

        for rule_entry in rule_entries {
            let rule_entry = match rule_entry {
                Ok(entry) => entry,
                Err(e) => {
                    eprintln!("Warning: Skipping rule entry in {}: {}", category_name, e);
                    continue;
                }
            };

            let rule_path = rule_entry.path();

            if !rule_path.is_dir() {
                continue;
            }

            let rule_id = match rule_path.file_name().and_then(|n| n.to_str()) {
                Some(id) => id,
                None => {
                    eprintln!(
                        "Warning: Skipping rule with invalid path: {}",
                        rule_path.display()
                    );
                    continue;
                }
            };

            let rule_base_path = format!("src/rules/cert_c/{}/{}", category_name, rule_id);
            let rule_tests_dir = rule_path.join("tests");

            if !rule_tests_dir.exists() {
                continue;
            }

            // Create per-rule test file
            let rule_snake = rule_id.to_lowercase().replace('-', "_");
            let rule_test_file = tests_dir.join(format!("{}_tests.rs", rule_snake));

            let mut rule_file = File::create(&rule_test_file).context(format!(
                "Failed to create test file for {}: {}",
                rule_id,
                rule_test_file.display()
            ))?;

            // Write per-rule file header (no use statements - they're in the main file)
            writeln!(rule_file, "// Auto-generated tests for {}", rule_id)?;
            writeln!(rule_file, "// DO NOT EDIT - Generated by build.rs\n")?;

            // Track this module for the main file
            rule_modules.push(rule_snake.clone());

            // Generate tests for fail/ directory
            let fail_dir = rule_tests_dir.join("fail");
            if fail_dir.exists() {
                let fail_entries = match fs::read_dir(&fail_dir) {
                    Ok(entries) => entries,
                    Err(e) => {
                        eprintln!(
                            "Warning: Could not read fail directory for {}: {}",
                            rule_id, e
                        );
                        continue;
                    }
                };

                for test_file in fail_entries {
                    let test_file = match test_file {
                        Ok(file) => file,
                        Err(e) => {
                            eprintln!("Warning: Skipping test file in {}/fail: {}", rule_id, e);
                            continue;
                        }
                    };

                    let test_path = test_file.path();

                    if test_path.extension().is_some_and(|e| e == "c") {
                        generate_test_function(
                            &mut rule_file,
                            rule_id,
                            &rule_base_path,
                            &test_path,
                            "fail",
                        )
                        .context(format!("Failed to generate test for {:?}", test_path))?;
                    }
                }
            }

            // Generate tests for expected_fail/ directory (known limitations)
            let expected_fail_dir = rule_tests_dir.join("expected_fail");
            if expected_fail_dir.exists() {
                let ef_entries = match fs::read_dir(&expected_fail_dir) {
                    Ok(entries) => entries,
                    Err(e) => {
                        eprintln!(
                            "Warning: Could not read expected_fail directory for {}: {}",
                            rule_id, e
                        );
                        continue;
                    }
                };

                for test_file in ef_entries {
                    let test_file = match test_file {
                        Ok(file) => file,
                        Err(e) => {
                            eprintln!(
                                "Warning: Skipping test file in {}/expected_fail: {}",
                                rule_id, e
                            );
                            continue;
                        }
                    };

                    let test_path = test_file.path();

                    if test_path.extension().is_some_and(|e| e == "c") {
                        generate_test_function(
                            &mut rule_file,
                            rule_id,
                            &rule_base_path,
                            &test_path,
                            "expected_fail",
                        )
                        .context(format!("Failed to generate test for {:?}", test_path))?;
                    }
                }
            }

            // Generate tests for pass/ directory
            let pass_dir = rule_tests_dir.join("pass");
            if pass_dir.exists() {
                let pass_entries = match fs::read_dir(&pass_dir) {
                    Ok(entries) => entries,
                    Err(e) => {
                        eprintln!(
                            "Warning: Could not read pass directory for {}: {}",
                            rule_id, e
                        );
                        continue;
                    }
                };

                for test_file in pass_entries {
                    let test_file = match test_file {
                        Ok(file) => file,
                        Err(e) => {
                            eprintln!("Warning: Skipping test file in {}/pass: {}", rule_id, e);
                            continue;
                        }
                    };

                    let test_path = test_file.path();

                    if test_path.extension().is_some_and(|e| e == "c") {
                        generate_test_function(
                            &mut rule_file,
                            rule_id,
                            &rule_base_path,
                            &test_path,
                            "pass",
                        )
                        .context(format!("Failed to generate test for {:?}", test_path))?;
                    }
                }
            }
        }
    }

    // Walk through src/rules/brules/ (flat: RULE-ID directories, no category level)
    let brules_dir = PathBuf::from("src/rules/brules");
    if brules_dir.exists() {
        if let Ok(brule_entries) = fs::read_dir(&brules_dir) {
            for brule_entry in brule_entries {
                let brule_entry = match brule_entry {
                    Ok(entry) => entry,
                    Err(_) => continue,
                };

                let rule_path = brule_entry.path();
                if !rule_path.is_dir() {
                    continue;
                }

                let rule_id = match rule_path.file_name().and_then(|n| n.to_str()) {
                    Some(id) => id,
                    None => continue,
                };

                let rule_base_path = format!("src/rules/brules/{}", rule_id);
                let rule_tests_dir = rule_path.join("tests");
                if !rule_tests_dir.exists() {
                    continue;
                }

                let rule_snake = rule_id.to_lowercase().replace('-', "_");
                let rule_test_file = tests_dir.join(format!("{}_tests.rs", rule_snake));
                let mut rule_file = match File::create(&rule_test_file) {
                    Ok(f) => f,
                    Err(_) => continue,
                };

                writeln!(rule_file, "// Auto-generated tests for {}", rule_id)?;
                writeln!(rule_file, "// DO NOT EDIT - Generated by build.rs\n")?;
                rule_modules.push(rule_snake.clone());

                for test_type in &["fail", "expected_fail", "pass"] {
                    let type_dir = rule_tests_dir.join(test_type);
                    if !type_dir.exists() {
                        continue;
                    }
                    if let Ok(entries) = fs::read_dir(&type_dir) {
                        for entry in entries.flatten() {
                            let test_path = entry.path();
                            if test_path.extension().is_some_and(|e| e == "c") {
                                generate_test_function(
                                    &mut rule_file,
                                    rule_id,
                                    &rule_base_path,
                                    &test_path,
                                    test_type,
                                )
                                .context(format!("Failed to generate test for {:?}", test_path))?;
                            }
                        }
                    }
                }
            }
        }
    }

    // Generate main integration_tests.rs with module includes using include!()
    let dest_path = out_dir_path.join("integration_tests.rs");
    let mut main_file =
        File::create(&dest_path).context("Failed to create integration_tests.rs")?;

    writeln!(main_file, "// Auto-generated test includes")?;
    writeln!(main_file, "// DO NOT EDIT - Generated by build.rs\n")?;
    writeln!(main_file, "#[cfg(test)]")?;
    writeln!(main_file, "mod generated_tests {{")?;
    writeln!(main_file, "    use crate::parser::CParser;")?;
    writeln!(main_file, "    use crate::rules::RuleRegistry;")?;
    writeln!(main_file, "    use std::path::Path;")?;
    writeln!(main_file, "    use std::collections::HashMap;\n")?;

    // Sort for consistent output
    rule_modules.sort();

    for rule_module in &rule_modules {
        // Use include!() to inline the test file contents
        writeln!(
            main_file,
            "    include!(concat!(env!(\"OUT_DIR\"), \"/tests/{}_tests.rs\"));",
            rule_module
        )?;
    }

    writeln!(main_file, "}}")?;

    println!("cargo:rerun-if-changed=src/rules/cert_c");
    println!("cargo:rerun-if-changed=src/rules/brules");
    println!("Generated {} per-rule test files", rule_modules.len());
    Ok(())
}

fn generate_test_function(
    f: &mut File,
    rule_id: &str,
    rule_base_path: &str, // e.g. "src/rules/cert_c/EXP/EXP34-C" or "src/rules/brules/BRULE-065"
    test_path: &std::path::Path,
    test_type: &str, // "fail", "pass", or "expected_fail"
) -> Result<()> {
    // Convert rule_id to snake_case for function name
    let rule_snake = rule_id.to_lowercase().replace('-', "_");

    // Get test file name without extension
    let test_file_stem = test_path
        .file_stem()
        .and_then(|s| s.to_str())
        .context("Invalid test file name")?;
    let test_name_safe = test_file_stem.replace(['-', '.'], "_");

    // Generate test function name: test_arr00_c_fail_wiki_noncompliant_1
    let test_fn_name = format!("test_{}_{}_{}", rule_snake, test_type, test_name_safe);

    // Get relative path from project root
    let test_filename = test_path
        .file_name()
        .and_then(|n| n.to_str())
        .context("Invalid test filename")?;
    let relative_path = format!("{}/tests/{}/{}", rule_base_path, test_type, test_filename);

    // Check if the test file requests intra-file prescan context
    let needs_prescan = check_test_needs_prescan(test_path);

    // Check if rule is implemented by reading the TOML file
    let toml_path = format!("{}/{}.toml", rule_base_path, rule_id);
    let is_enabled = check_if_rule_enabled(&toml_path)?;

    writeln!(f, "#[test]")?;
    writeln!(f, "#[allow(non_snake_case)]")?;

    // If rule is not enabled/implemented, mark test as ignored
    if !is_enabled {
        writeln!(f, "#[ignore = \"Rule {} not yet implemented\"]", rule_id)?;
    } else if test_type == "expected_fail" {
        writeln!(
            f,
            "#[ignore = \"Known limitation: {} cannot detect this pattern yet\"]",
            rule_id
        )?;
    }
    writeln!(f, "fn {}() {{", test_fn_name)?;
    writeln!(f, "    let registry = RuleRegistry::new();")?;
    writeln!(
        f,
        "    let rule = registry.get_rule(\"{}\").expect(\"Rule {} not found in registry\");",
        rule_id, rule_id
    )?;
    writeln!(f, "    ")?;
    writeln!(
        f,
        "    let test_path = Path::new(env!(\"CARGO_MANIFEST_DIR\")).join(\"{}\");",
        relative_path
    )?;
    writeln!(f, "    let source = std::fs::read_to_string(&test_path)")?;
    writeln!(
        f,
        "        .unwrap_or_else(|e| panic!(\"Failed to read {{:?}}: {{}}\", test_path, e));"
    )?;
    writeln!(f, "    ")?;
    writeln!(
        f,
        "    let mut parser = CParser::new().expect(\"Failed to create parser\");"
    )?;
    writeln!(f, "    let tree = parser.parse_source(&source)")?;
    writeln!(
        f,
        "        .unwrap_or_else(|e| panic!(\"Failed to parse {{:?}}: {{}}\", test_path, e));"
    )?;
    writeln!(f, "    ")?;

    if needs_prescan {
        // Build intra-file prescan context before calling rule.check()
        writeln!(
            f,
            "    let context = crate::analyze::prescan::prescan_single_tree(&tree.root_node(), &source);"
        )?;
        writeln!(f, "    rule.set_project_context(&context);")?;
        writeln!(f, "    let mut function_cfgs = HashMap::new();")?;
        writeln!(
            f,
            "    crate::analyze::collect_function_cfgs(&tree.root_node(), &source, &mut function_cfgs);"
        )?;
        writeln!(f, "    rule.set_function_cfgs(&function_cfgs);")?;
        writeln!(f, "    ")?;
    }

    writeln!(
        f,
        "    let violations = rule.check(&tree.root_node(), &source);"
    )?;
    writeln!(f, "    ")?;

    if test_type == "fail" || test_type == "expected_fail" {
        writeln!(f, "    let detected_violation = !violations.is_empty();")?;
        writeln!(f, "    ")?;
        writeln!(f, "    // Record result for report generation")?;
        if test_type == "expected_fail" {
            writeln!(f, "    // For expected_fail tests: known limitation — violation expected but tool cannot detect it yet")?;
        } else {
            writeln!(f, "    // For fail tests: we expect violations to be detected (detected_violation should be true)")?;
        }
        writeln!(
            f,
            "    super::record_test_result(\"{}\", detected_violation, true);",
            test_fn_name
        )?;
        writeln!(f, "    ")?;
        writeln!(f, "    assert!(")?;
        writeln!(f, "        detected_violation,")?;
        writeln!(
            f,
            "        \"[{}] Expected violation in {{:?}} but found none\",",
            rule_id
        )?;
        writeln!(f, "        test_path.file_name().unwrap()")?;
        writeln!(f, "    );")?;
    } else {
        writeln!(f, "    let no_violation = violations.is_empty();")?;
        writeln!(f, "    ")?;
        writeln!(f, "    // Record result for report generation")?;
        writeln!(f, "    // For pass tests: we expect NO violations to be detected (no_violation should be true)")?;
        writeln!(
            f,
            "    super::record_test_result(\"{}\", no_violation, false);",
            test_fn_name
        )?;
        writeln!(f, "    ")?;
        writeln!(f, "    assert!(")?;
        writeln!(f, "        no_violation,")?;
        writeln!(
            f,
            "        \"[{}] Unexpected violation in {{:?}}: {{}}\",",
            rule_id
        )?;
        writeln!(f, "        test_path.file_name().unwrap(),")?;
        writeln!(
            f,
            "        violations.first().map(|v| &v.message).unwrap_or(&String::from(\"unknown\"))"
        )?;
        writeln!(f, "    );")?;
    }

    writeln!(f, "}}")?;
    writeln!(f)?;

    Ok(())
}

fn check_if_rule_enabled(toml_path: &str) -> Result<bool> {
    // Read the TOML file and check if rule is enabled
    let content = match fs::read_to_string(toml_path) {
        Ok(content) => content,
        Err(_) => return Ok(false), // If TOML file doesn't exist, assume not implemented
    };

    // Parse TOML properly using serde
    let config: RuleConfig = match toml::from_str(&content) {
        Ok(config) => config,
        Err(e) => {
            eprintln!("Warning: Failed to parse TOML {}: {}", toml_path, e);
            eprintln!("         Falling back to string matching");
            // Fallback to string matching if TOML parse fails
            return Ok(content.contains("[rules.") && content.contains("enabled = true"));
        }
    };

    // Check for implemented rule format: [rules.<namespace>.RULE-ID] enabled = true
    if let Some(rules) = config.rules {
        for namespace_rules in rules.values() {
            for settings in namespace_rules.values() {
                if settings.enabled {
                    return Ok(true);
                }
            }
        }
    }

    // Check for unimplemented rule format: [rule] enabled = false
    if let Some(rule_settings) = config.rule {
        return Ok(rule_settings.enabled);
    }

    // Default to false if format is unclear
    Ok(false)
}

/// Check if a `.c` test file contains the `// sqc-test: prescan` marker.
///
/// When present, the generated test will build an intra-file prescan context
/// (function summaries + call-site null states + CFGs) before calling
/// `rule.check()`. This enables testing inter-procedural analysis patterns
/// within a single translation unit.
fn check_test_needs_prescan(test_path: &std::path::Path) -> bool {
    fs::read_to_string(test_path)
        .map(|content| content.contains("// sqc-test: prescan"))
        .unwrap_or(false)
}