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
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
use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils;
use tree_sitter::Node;

pub struct Dcl13C;

impl CertRule for Dcl13C {
    fn rule_id(&self) -> &'static str {
        "DCL13-C"
    }

    fn description(&self) -> &'static str {
        "Declare function parameters that are pointers to values not changed by the function as const"
    }

    fn severity(&self) -> Severity {
        Severity::Low
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::Recommendation
    }

    fn cert_id(&self) -> &'static str {
        "DCL13-C"
    }

    fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
        let mut violations = Vec::new();

        // Check all function definitions and declarations
        check_functions_recursively(node, source, &mut violations, self.rule_id());

        violations
    }
}

/// Recursively check all function definitions and declarations in the AST
fn check_functions_recursively(
    node: &Node,
    source: &str,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
) {
    if node.kind() == "function_definition" {
        check_function_definition(node, source, violations, rule_id);
    } else if node.kind() == "declaration" {
        // Check for function declarations (prototypes)
        check_function_declaration(node, source, violations, rule_id);
    }

    // Recursively check children
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            check_functions_recursively(&child, source, violations, rule_id);
        }
    }
}

/// Get the name of the function being defined
fn get_function_name(func_node: &Node, source: &str) -> Option<String> {
    for i in 0..func_node.child_count() {
        if let Some(child) = func_node.child(i) {
            if let Some(name) = find_name_in_declarator(&child, source) {
                return Some(name);
            }
        }
    }
    None
}

fn find_name_in_declarator(node: &Node, source: &str) -> Option<String> {
    if node.kind() == "function_declarator" {
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if child.kind() == "identifier" {
                    return Some(ast_utils::get_node_text(&child, source).to_string());
                }
                if child.kind() == "pointer_declarator" {
                    if let Some(name) = find_name_in_declarator(&child, source) {
                        return Some(name);
                    }
                }
            }
        }
    } else if node.kind() == "pointer_declarator" {
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if let Some(name) = find_name_in_declarator(&child, source) {
                    return Some(name);
                }
            }
        }
    }
    None
}

/// Check a function definition for const-correctness of pointer parameters
fn check_function_definition(
    func_node: &Node,
    source: &str,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
) {
    // Skip main() — its signature (int argc, char *argv[]) is mandated by the C standard;
    // requiring const on argv is incorrect and generates FPs on every Juliet test file.
    if get_function_name(func_node, source).as_deref() == Some("main") {
        return;
    }

    // Extract function parameters with their const-qualification status
    let params = extract_function_parameters(func_node, source);

    // Find the function body
    let body = find_compound_statement(func_node);

    // For each pointer parameter, check if it's modified in the function body
    for (param_name, is_const, is_pointer, line, col) in params {
        if !is_pointer {
            continue; // Only check pointer parameters
        }

        // DCL13-C: "Declare function parameters that are pointers to values
        // not changed by the function as const"
        // If already const-qualified, there's nothing for DCL13-C to flag.
        if is_const {
            continue;
        }

        // Check if this parameter is modified in the function body
        // Also check through local pointer aliases (e.g., `T *cur = param;`)
        let is_modified = if let Some(body_node) = body {
            if is_pointer_param_modified(&body_node, &param_name, source) {
                true
            } else {
                let aliases = collect_pointer_aliases(&body_node, &param_name, source);
                aliases
                    .iter()
                    .any(|alias| is_pointer_param_modified(&body_node, alias, source))
            }
        } else {
            false // No body, assume not modified
        };

        if !is_modified {
            // Non-const pointer parameter that is never modified — should be const.
            violations.push(RuleViolation {
                rule_id: rule_id.to_string(),
                severity: Severity::Low,
                message: format!(
                    "Pointer parameter '{}' is not modified and should be declared const",
                    param_name
                ),
                file_path: String::new(),
                line,
                column: col,
                suggestion: Some(format!(
                    "Declare parameter as 'const <type> *{}'",
                    param_name
                )),
                ..Default::default()
            });
        }
    }
}

/// Check a function declaration for const-correctness of pointer parameters
fn check_function_declaration(
    decl_node: &Node,
    source: &str,
    violations: &mut Vec<RuleViolation>,
    rule_id: &str,
) {
    // Look for function declarators in the declaration
    for i in 0..decl_node.child_count() {
        if let Some(child) = decl_node.child(i) {
            if child.kind() == "function_declarator" || is_function_declarator(&child) {
                // For function declarations (no body), we can only check basic patterns
                // We'll flag non-const pointer parameters as potential issues
                let params = extract_params_from_declarator(&child, source);

                for (param_name, is_const, is_pointer, line, col) in params {
                    if is_pointer && !is_const && !param_name.is_empty() {
                        // Only flag if it's a clear case where const would be appropriate
                        // (e.g., second parameter of strcat-like functions)
                        // For declarations without bodies, we can't analyze usage,
                        // so we'll be conservative and only flag obvious cases

                        // Check if this looks like a read-only parameter by naming convention
                        // (src, source, input, etc.) or position (second param in string functions)
                        if is_likely_readonly_param(&param_name) {
                            violations.push(RuleViolation {
                                rule_id: rule_id.to_string(),
                                severity: Severity::Low,
                                message: format!(
                                    "Pointer parameter '{}' should likely be declared const",
                                    param_name
                                ),
                                file_path: String::new(),
                                line,
                                column: col,
                                suggestion: Some(format!(
                                    "Consider declaring parameter as 'const <type> *{}'",
                                    param_name
                                )),
                                ..Default::default()
                            });
                        }
                    }
                }
            }
        }
    }
}

/// Check if a parameter name suggests it should be read-only
fn is_likely_readonly_param(name: &str) -> bool {
    let lowercase = name.to_lowercase();
    lowercase.starts_with("src")
        || lowercase.starts_with("source")
        || lowercase.starts_with("input")
        || lowercase.starts_with("in_")
        || lowercase.contains("read")
        || name.ends_with("2") // Common convention for second string parameter (e.g., s2 in strcat)
}

/// Check if a node is a function declarator (recursively checking pointer/array decorators)
fn is_function_declarator(node: &Node) -> bool {
    if node.kind() == "function_declarator" {
        return true;
    }

    // Check children for nested declarators
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if is_function_declarator(&child) {
                return true;
            }
        }
    }

    false
}

/// Extract parameters from a function declarator
fn extract_params_from_declarator(
    declarator: &Node,
    source: &str,
) -> Vec<(String, bool, bool, usize, usize)> {
    let mut params = Vec::new();

    // Find parameter_list
    if let Some(param_list) = find_parameter_list(declarator) {
        for i in 0..param_list.child_count() {
            if let Some(param) = param_list.child(i) {
                if param.kind() == "parameter_declaration" {
                    if let Some((name, is_const, is_pointer, line, col)) =
                        analyze_parameter(&param, source)
                    {
                        params.push((name, is_const, is_pointer, line, col));
                    }
                }
            }
        }
    }

    params
}

/// Find parameter_list in a function declarator
fn find_parameter_list<'a>(declarator: &Node<'a>) -> Option<Node<'a>> {
    for i in 0..declarator.child_count() {
        if let Some(child) = declarator.child(i) {
            if child.kind() == "parameter_list" {
                return Some(child);
            }
            // Recursively search in nested declarators
            if let Some(found) = find_parameter_list(&child) {
                return Some(found);
            }
        }
    }
    None
}

/// Functions known to NOT modify their pointer arguments.
/// If a pointer parameter is passed to a function NOT in this list,
/// we conservatively assume the function may modify the pointed-to data.
const READ_ONLY_FUNCTIONS: &[&str] = &[
    // C stdio - output (read format string and args)
    "printf", "fprintf", "vprintf", "vfprintf", "wprintf", "fwprintf", "puts", "fputs", "putchar",
    "fputc", "putc", // C stdio - file ops (don't modify pointer args)
    "ftell", "feof", "ferror", "fopen", // C string - read-only
    "strlen", "strcmp", "strncmp", "strchr", "strrchr", "strstr", "strpbrk", "strspn", "strcspn",
    "strerror", // C wide string - read-only
    "wcslen", "wcscmp", "wcsncmp", "wcschr", "wcsrchr", "wcsstr",
    // C memory - read-only
    "memcmp", "memchr",
    // C search/sort (bsearch reads the array; qsort modifies so NOT listed)
    "bsearch", // C string - duplication (reads source string)
    "strdup", "strndup", "wcsdup", // C conversion (first arg is read-only string)
    "atoi", "atol", "atof", "atoll", // C ctype
    "isalpha", "isdigit", "isalnum", "isspace", "isupper", "islower", "ispunct", "isprint",
    "iscntrl", "isxdigit", "isgraph", "toupper", "tolower", // C math
    "abs", "labs", "llabs", "fabs", "sqrt", "pow", "ceil", "floor", "log", "log10", "exp", "sin",
    "cos", "tan", // C assert / control flow
    "assert", "exit", "abort", "_exit", "_Exit", // POSIX - read-only for pointer args
    "write", "open", "close", "perror", "access", "stat", "lstat",
];

/// Check if a pointer parameter is modified in the function body
fn is_pointer_param_modified(body: &Node, param_name: &str, source: &str) -> bool {
    check_node_for_pointer_modification(body, param_name, source)
}

/// Collect local pointer variables that are initialized from a parameter.
///
/// Detects patterns like `T *cur = param;` or `T *alias = (T *)param;`.
/// Returns the names of local aliases so modifications through them
/// can be attributed back to the original parameter.
fn collect_pointer_aliases(body: &Node, param_name: &str, source: &str) -> Vec<String> {
    let mut aliases = Vec::new();
    collect_aliases_recursive(body, param_name, source, &mut aliases);
    aliases
}

fn collect_aliases_recursive(
    node: &Node,
    param_name: &str,
    source: &str,
    aliases: &mut Vec<String>,
) {
    if node.kind() == "declaration" {
        // Look for init_declarator children with pointer declarator
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if child.kind() == "init_declarator" {
                    // Check if the initializer value is the parameter
                    if let Some(value) = child.child_by_field_name("value") {
                        if arg_is_param(&value, param_name, source) {
                            // Extract the declared name from the declarator
                            if let Some(declarator) = child.child_by_field_name("declarator") {
                                if let Some(name) =
                                    find_identifier_in_declarator(&declarator, source)
                                {
                                    aliases.push(name);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            collect_aliases_recursive(&child, param_name, source, aliases);
        }
    }
}

/// Extract the identifier name from a declarator node (handles pointer_declarator wrapping).
fn find_identifier_in_declarator(node: &Node, source: &str) -> Option<String> {
    if node.kind() == "identifier" {
        return Some(ast_utils::get_node_text(node, source).to_string());
    }
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if let Some(name) = find_identifier_in_declarator(&child, source) {
                return Some(name);
            }
        }
    }
    None
}

/// Recursively check if a node contains modifications through a pointer parameter.
///
/// Detects:
/// - Direct dereference writes: `*param = expr`
/// - Struct member writes via arrow: `param->field = expr`
/// - Array subscript writes: `param[i] = expr`, `param->field[i] = expr`
/// - Compound assignments: `param->field += expr`
/// - Increment/decrement: `param->field++`, `(*param)++`
/// - Function calls passing param to potentially-modifying functions
fn check_node_for_pointer_modification(node: &Node, param_name: &str, source: &str) -> bool {
    // Check if this is an assignment where LHS writes through param
    if node.kind() == "assignment_expression" {
        if let Some(left) = node.child_by_field_name("left") {
            if is_write_through_param(&left, param_name, source) {
                return true;
            }
        }
    }

    // Check for increment/decrement through param (e.g., (*p)++, p->field++)
    if node.kind() == "update_expression" {
        if let Some(argument) = node.child_by_field_name("argument") {
            if is_write_through_param(&argument, param_name, source) {
                return true;
            }
        }
    }

    // Check if param is passed to a function that may modify it
    if node.kind() == "call_expression" {
        if is_param_passed_to_modifying_call(node, param_name, source) {
            return true;
        }
    }

    // Recursively check children
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i) {
            if check_node_for_pointer_modification(&child, param_name, source) {
                return true;
            }
        }
    }

    false
}

/// Check if a node represents writing through a parameter (dereferencing the pointed-to data).
///
/// Handles: `*param`, `param->field`, `param[i]`, `param->field[i]`,
/// `param->field1->field2`, `(*param).field`, and nested combinations.
fn is_write_through_param(node: &Node, param_name: &str, source: &str) -> bool {
    match node.kind() {
        "pointer_expression" => {
            // *param
            if let Some(argument) = node.child_by_field_name("argument") {
                let text = ast_utils::get_node_text(&argument, source);
                if text == param_name {
                    return true;
                }
            }
        }
        "field_expression" => {
            // param->field or param->field1->field2
            if let Some(argument) = node.child_by_field_name("argument") {
                let text = ast_utils::get_node_text(&argument, source);
                if text == param_name {
                    return true;
                }
                // Nested: param->field1->field2, (*param).field, etc.
                return is_write_through_param(&argument, param_name, source);
            }
        }
        "subscript_expression" => {
            // param[i] or param->items[i]
            if let Some(argument) = node.child_by_field_name("argument") {
                let text = ast_utils::get_node_text(&argument, source);
                if text == param_name {
                    return true;
                }
                // Nested: param->items[i]
                return is_write_through_param(&argument, param_name, source);
            }
        }
        "parenthesized_expression" => {
            // (*param) — unwrap parens
            for i in 0..node.child_count() {
                if let Some(child) = node.child(i) {
                    if child.kind() != "(" && child.kind() != ")" {
                        return is_write_through_param(&child, param_name, source);
                    }
                }
            }
        }
        _ => {}
    }
    false
}

/// Check if an argument is `&(param->field)`, `&param[i]`, or similar address-of
/// expressions that take the address of the parameter's member data.
///
/// Passing `&(param->field)` to a non-read-only function means the callee can
/// modify `param->field`, so the struct pointed to by `param` is modified.
fn arg_addresses_param_member(node: &Node, param_name: &str, source: &str) -> bool {
    // tree-sitter C parses &expr as pointer_expression (same node kind as *expr)
    if node.kind() != "pointer_expression" {
        return false;
    }
    // Check for & operator (vs * dereference)
    if let Some(op_node) = node.child_by_field_name("operator") {
        if ast_utils::get_node_text(&op_node, source) != "&" {
            return false;
        }
    } else {
        return false;
    }
    // The operand of & should derive from param (e.g., param->field, param[i])
    if let Some(argument) = node.child_by_field_name("argument") {
        return expr_derives_from_param(&argument, param_name, source);
    }
    false
}

/// Check if an expression is rooted in a parameter (for address-of detection).
///
/// Matches: `param->field`, `param[i]`, `(*param).field`, `((param->field))`,
/// and nested combinations like `param->a->b`.
fn expr_derives_from_param(node: &Node, param_name: &str, source: &str) -> bool {
    match node.kind() {
        "field_expression" => {
            if let Some(argument) = node.child_by_field_name("argument") {
                let text = ast_utils::get_node_text(&argument, source);
                if text == param_name {
                    return true;
                }
                return expr_derives_from_param(&argument, param_name, source);
            }
            false
        }
        "subscript_expression" => {
            if let Some(argument) = node.child(0) {
                let text = ast_utils::get_node_text(&argument, source);
                if text == param_name {
                    return true;
                }
                return expr_derives_from_param(&argument, param_name, source);
            }
            false
        }
        "pointer_expression" => {
            if let Some(argument) = node.child_by_field_name("argument") {
                let text = ast_utils::get_node_text(&argument, source);
                if text == param_name {
                    return true;
                }
                return expr_derives_from_param(&argument, param_name, source);
            }
            false
        }
        "parenthesized_expression" => {
            for i in 0..node.child_count() {
                if let Some(child) = node.child(i) {
                    if child.kind() != "(" && child.kind() != ")" {
                        return expr_derives_from_param(&child, param_name, source);
                    }
                }
            }
            false
        }
        _ => false,
    }
}

/// Check if an argument expression is the parameter itself (directly or through a cast).
///
/// Only matches direct usage: `param`, `(void*)param`, `((param))`.
/// Does NOT match derived expressions like `param->field` or `param[i]`,
/// because passing a member's value to a function doesn't constitute
/// modification of the struct the parameter points to.
fn arg_is_param(node: &Node, param_name: &str, source: &str) -> bool {
    match node.kind() {
        "identifier" => ast_utils::get_node_text(node, source) == param_name,
        "cast_expression" => {
            // (void*)param
            if let Some(value) = node.child_by_field_name("value") {
                return arg_is_param(&value, param_name, source);
            }
            false
        }
        "parenthesized_expression" => {
            for i in 0..node.child_count() {
                if let Some(child) = node.child(i) {
                    if child.kind() != "(" && child.kind() != ")" {
                        return arg_is_param(&child, param_name, source);
                    }
                }
            }
            false
        }
        _ => false,
    }
}

/// Check if a pointer parameter is passed to a function that may modify it.
///
/// Returns true if the param (or an expression rooted in it) appears as an
/// argument to a function call, and that function is NOT in the read-only
/// whitelist. This conservatively assumes unknown functions may write through
/// their pointer arguments.
fn is_param_passed_to_modifying_call(call_node: &Node, param_name: &str, source: &str) -> bool {
    let args = match call_node.child_by_field_name("arguments") {
        Some(a) => a,
        None => return false,
    };

    // Check if param appears as any argument (direct, cast, or address-of-member)
    let mut param_is_arg = false;
    for i in 0..args.child_count() {
        if let Some(arg) = args.child(i) {
            if arg.kind() == "," || arg.kind() == "(" || arg.kind() == ")" {
                continue;
            }
            if arg_is_param(&arg, param_name, source)
                || arg_addresses_param_member(&arg, param_name, source)
            {
                param_is_arg = true;
                break;
            }
        }
    }

    if !param_is_arg {
        return false;
    }

    // Get function name — if it's a known read-only function, not a modification
    if let Some(func) = call_node.child_by_field_name("function") {
        let func_name = ast_utils::get_node_text(&func, source);
        if READ_ONLY_FUNCTIONS.contains(&func_name) {
            return false;
        }
    }

    // Unknown or non-read-only function with our param as argument → assume modification
    true
}

/// Extract function parameters with const-qualification and pointer status
///
/// Returns: Vec<(parameter_name, is_const, is_pointer, line_number, column_number)>
fn extract_function_parameters(
    func_node: &Node,
    source: &str,
) -> Vec<(String, bool, bool, usize, usize)> {
    let mut parameters = Vec::new();

    // Find the function_declarator
    for i in 0..func_node.child_count() {
        if let Some(child) = func_node.child(i) {
            if child.kind() == "function_declarator"
                || is_pointer_or_array_with_func_declarator(&child)
            {
                if let Some(param_list) = find_parameter_list(&child) {
                    // Extract each parameter
                    for j in 0..param_list.child_count() {
                        if let Some(param) = param_list.child(j) {
                            if param.kind() == "parameter_declaration" {
                                if let Some((name, is_const, is_pointer, line, col)) =
                                    analyze_parameter(&param, source)
                                {
                                    parameters.push((name, is_const, is_pointer, line, col));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    parameters
}

/// Check if node is a pointer/array declarator that contains a function declarator
fn is_pointer_or_array_with_func_declarator(node: &Node) -> bool {
    if node.kind() == "pointer_declarator" || node.kind() == "array_declarator" {
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if child.kind() == "function_declarator"
                    || is_pointer_or_array_with_func_declarator(&child)
                {
                    return true;
                }
            }
        }
    }
    false
}

/// Analyze a parameter declaration to extract name, const status, and pointer status
///
/// Returns: Some((name, is_const, is_pointer, line, col)) or None
fn analyze_parameter(param: &Node, source: &str) -> Option<(String, bool, bool, usize, usize)> {
    let mut is_const = false;
    let mut is_pointer = false;
    let mut param_name = String::new();
    let mut line = 0;
    let mut col = 0;

    // Check for type qualifiers (const)
    for i in 0..param.child_count() {
        if let Some(child) = param.child(i) {
            match child.kind() {
                "type_qualifier" => {
                    let text = ast_utils::get_node_text(&child, source);
                    if text == "const" {
                        is_const = true;
                    }
                }
                "pointer_declarator" => {
                    is_pointer = true;
                    param_name = ast_utils::get_identifier_from_declarator(&child, source);
                    if !param_name.is_empty() {
                        let pos = child.start_position();
                        line = pos.row + 1;
                        col = pos.column + 1;
                    }
                }
                "array_declarator" => {
                    is_pointer = true; // Arrays decay to pointers in function parameters
                    param_name = ast_utils::get_identifier_from_declarator(&child, source);
                    if !param_name.is_empty() {
                        let pos = child.start_position();
                        line = pos.row + 1;
                        col = pos.column + 1;
                    }
                }
                "identifier"
                    // Direct identifier (might be non-pointer parameter)
                    if param_name.is_empty() => {
                        param_name = ast_utils::get_node_text(&child, source).to_string();
                        let pos = child.start_position();
                        line = pos.row + 1;
                        col = pos.column + 1;
                    }
                "primitive_type" | "type_identifier" | "struct_specifier" | "union_specifier"
                | "enum_specifier"
                    // Type specifier - check if followed by pointer
                    if i + 1 < param.child_count() => {
                        if let Some(next) = param.child(i + 1) {
                            if next.kind() == "*" || next.kind() == "abstract_pointer_declarator" {
                                is_pointer = true;
                            }
                        }
                    }
                _ => {}
            }
        }
    }

    if !param_name.is_empty() {
        Some((param_name, is_const, is_pointer, line, col))
    } else {
        None
    }
}

/// Find the compound_statement (body) of a function
fn find_compound_statement<'a>(func_node: &Node<'a>) -> Option<Node<'a>> {
    for i in 0..func_node.child_count() {
        if let Some(child) = func_node.child(i) {
            if child.kind() == "compound_statement" {
                return Some(child);
            }
        }
    }
    None
}