codeix 0.5.0

Fast semantic code search for AI agents — find symbols, references, and callers across any codebase
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
//! C symbol and text extraction.

use tree_sitter::{Node, Tree};

use crate::index::format::{ReferenceEntry, SymbolEntry, TextEntry};
use crate::parser::helpers::*;
use crate::parser::treesitter::MAX_DEPTH;

/// C-specific stopwords (keywords, common types, etc.)
const C_STOPWORDS: &[&str] = &[
    // Keywords
    "goto",
    "sizeof",
    "typedef",
    "union",
    "extern",
    "volatile",
    "register",
    "auto",
    "inline",
    // Primitive types
    "int",
    "char",
    "short",
    "long",
    "float",
    "double",
    "signed",
    "unsigned",
    // Common standard types
    "size_t",
    "ssize_t",
    "ptrdiff_t",
    "FILE",
    // Common macros and constants
    "NULL",
    "EOF",
    // Common variable patterns
    "argc",
    "argv",
    "main",
    "ret",
    "len",
    "ptr",
    "buf",
];

/// Filter C-specific stopwords from extracted tokens.
fn filter_c_tokens(tokens: Option<String>) -> Option<String> {
    tokens.and_then(|t| {
        let filtered: Vec<&str> = t
            .split_whitespace()
            .filter(|tok| !C_STOPWORDS.contains(&tok.to_lowercase().as_str()))
            // Filter uppercase constants
            .filter(|tok| !tok.chars().all(|c| c.is_uppercase() || c == '_'))
            .collect();
        if filtered.is_empty() {
            None
        } else {
            Some(filtered.join(" "))
        }
    })
}

pub fn extract(
    tree: &Tree,
    source: &[u8],
    file_path: &str,
    symbols: &mut Vec<SymbolEntry>,
    texts: &mut Vec<TextEntry>,
    references: &mut Vec<ReferenceEntry>,
) {
    let root = tree.root_node();
    walk_node(root, source, file_path, None, symbols, texts, references, 0);
}

// ---------------------------------------------------------------------------
// Builtin detection for filtering noisy references
// ---------------------------------------------------------------------------

/// Check if a function name is a C standard library function.
fn is_c_builtin_call(name: &str) -> bool {
    matches!(
        name,
        // stdio.h
        "printf"
        | "fprintf"
        | "sprintf"
        | "snprintf"
        | "scanf"
        | "fscanf"
        | "sscanf"
        | "fopen"
        | "fclose"
        | "fread"
        | "fwrite"
        | "fgets"
        | "fputs"
        | "getc"
        | "putc"
        | "getchar"
        | "putchar"
        | "fflush"
        | "fseek"
        | "ftell"
        | "rewind"
        | "feof"
        | "ferror"
        | "perror"
        | "remove"
        | "rename"
        // stdlib.h
        | "malloc"
        | "calloc"
        | "realloc"
        | "free"
        | "exit"
        | "abort"
        | "atexit"
        | "atoi"
        | "atol"
        | "atof"
        | "strtol"
        | "strtoul"
        | "strtod"
        | "rand"
        | "srand"
        | "qsort"
        | "bsearch"
        | "abs"
        | "labs"
        | "div"
        | "ldiv"
        | "getenv"
        | "system"
        // string.h
        | "strlen"
        | "strcpy"
        | "strncpy"
        | "strcat"
        | "strncat"
        | "strcmp"
        | "strncmp"
        | "strchr"
        | "strrchr"
        | "strstr"
        | "strtok"
        | "memcpy"
        | "memmove"
        | "memset"
        | "memcmp"
        | "memchr"
        // ctype.h
        | "isalpha"
        | "isdigit"
        | "isalnum"
        | "isspace"
        | "isupper"
        | "islower"
        | "toupper"
        | "tolower"
        // math.h
        | "sin"
        | "cos"
        | "tan"
        | "asin"
        | "acos"
        | "atan"
        | "atan2"
        | "sinh"
        | "cosh"
        | "tanh"
        | "exp"
        | "log"
        | "log10"
        | "pow"
        | "sqrt"
        | "ceil"
        | "floor"
        | "fabs"
        | "fmod"
        // assert.h
        | "assert"
        // Common patterns
        | "sizeof"
        | "offsetof"
    )
}

#[allow(clippy::too_many_arguments)]
fn walk_node(
    node: Node,
    source: &[u8],
    file_path: &str,
    parent_ctx: Option<&str>,
    symbols: &mut Vec<SymbolEntry>,
    texts: &mut Vec<TextEntry>,
    references: &mut Vec<ReferenceEntry>,
    depth: usize,
) {
    // Prevent stack overflow on deeply nested code
    if depth > MAX_DEPTH {
        return;
    }

    let kind = node.kind();

    match kind {
        "function_definition" => {
            extract_function(node, source, file_path, symbols, references);
        }
        "declaration" => {
            extract_declaration(node, source, file_path, parent_ctx, symbols);
        }
        "struct_specifier" => {
            extract_struct_or_union(
                node, source, file_path, "struct", parent_ctx, symbols, texts, references,
            );
            return;
        }
        "union_specifier" => {
            extract_struct_or_union(
                node, source, file_path, "struct", parent_ctx, symbols, texts, references,
            );
            return;
        }
        "enum_specifier" => {
            extract_enum(node, source, file_path, parent_ctx, symbols);
        }
        "type_definition" => {
            extract_typedef(node, source, file_path, symbols);
        }
        "preproc_include" => {
            extract_include(node, source, file_path, symbols, references);
        }
        "preproc_def" | "preproc_function_def" => {
            extract_macro(node, source, file_path, symbols);
        }
        "comment" => {
            extract_comment(node, source, file_path, parent_ctx, texts);
            return;
        }
        "string_literal" | "concatenated_string" => {
            extract_string(node, source, file_path, parent_ctx, texts);
            return;
        }

        // --- Reference extraction ---
        "call_expression" => {
            extract_call_ref(node, source, file_path, parent_ctx, references);
        }

        _ => {}
    }

    // Recurse
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_node(
            child,
            source,
            file_path,
            parent_ctx,
            symbols,
            texts,
            references,
            depth + 1,
        );
    }
}

// ---------------------------------------------------------------------------
// Reference extraction
// ---------------------------------------------------------------------------

/// Extract a function call reference.
fn extract_call_ref(
    node: Node,
    source: &[u8],
    file_path: &str,
    parent_ctx: Option<&str>,
    references: &mut Vec<ReferenceEntry>,
) {
    let func = match find_child_by_field(node, "function") {
        Some(f) => f,
        None => return,
    };

    let name = get_call_name(func, source);
    if name.is_empty() || is_c_builtin_call(&name) {
        return;
    }

    let line = node_line_range(node);
    references.push(ReferenceEntry {
        file: file_path.to_string(),
        name,
        kind: "call".to_string(),
        line,
        caller: parent_ctx.map(String::from),
        project: String::new(),
    });
}

/// Get the name of a function call.
fn get_call_name(node: Node, source: &[u8]) -> String {
    match node.kind() {
        "identifier" => node_text(node, source),
        "field_expression" => {
            // obj->method or obj.method
            if let Some(field) = find_child_by_field(node, "field") {
                if let Some(arg) = find_child_by_field(node, "argument") {
                    let arg_name = get_call_name(arg, source);
                    let field_name = node_text(field, source);
                    if arg_name.is_empty() {
                        field_name
                    } else {
                        format!("{}.{}", arg_name, field_name)
                    }
                } else {
                    node_text(field, source)
                }
            } else {
                String::new()
            }
        }
        "parenthesized_expression" => {
            // (*func_ptr)()
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                let name = get_call_name(child, source);
                if !name.is_empty() {
                    return name;
                }
            }
            String::new()
        }
        "pointer_expression" => {
            // *func_ptr
            if let Some(arg) = find_child_by_field(node, "argument") {
                get_call_name(arg, source)
            } else {
                String::new()
            }
        }
        _ => String::new(),
    }
}

fn extract_function(
    node: Node,
    source: &[u8],
    file_path: &str,
    symbols: &mut Vec<SymbolEntry>,
    references: &mut Vec<ReferenceEntry>,
) {
    let declarator = match find_child_by_field(node, "declarator") {
        Some(d) => d,
        None => return,
    };

    let name = extract_declarator_name(declarator, source);
    if name.is_empty() {
        return;
    }

    let line = node_line_range(node);

    // Check for static (file-scoped)
    let is_static = has_storage_class(node, source, "static");
    let visibility = if is_static { "private" } else { "public" };

    let _sig = extract_signature_to_brace(node, source);

    // Extract return type reference (if not primitive)
    if let Some(type_node) = find_child_by_field(node, "type") {
        extract_type_ref(type_node, source, file_path, Some(&name), references);
    }

    // Extract tokens from function body
    let tokens = find_child_by_field(node, "body")
        .and_then(|body| filter_c_tokens(extract_tokens(body, source)));

    push_symbol(
        symbols,
        file_path,
        name,
        "function",
        line,
        None,
        tokens,
        None,
        Some(visibility.to_string()),
    );
}

/// Extract a type reference if it's a user-defined type.
fn extract_type_ref(
    node: Node,
    source: &[u8],
    file_path: &str,
    parent_ctx: Option<&str>,
    references: &mut Vec<ReferenceEntry>,
) {
    let name = match node.kind() {
        "type_identifier" => node_text(node, source),
        "struct_specifier" | "union_specifier" | "enum_specifier" => {
            find_child_by_field(node, "name")
                .map(|n| node_text(n, source))
                .unwrap_or_default()
        }
        _ => return,
    };

    if name.is_empty() {
        return;
    }

    // Skip primitive types (they are in C_STOPWORDS but we check explicitly)
    if matches!(
        name.as_str(),
        "int" | "char" | "short" | "long" | "float" | "double" | "void" | "signed" | "unsigned"
    ) {
        return;
    }

    let line = node_line_range(node);
    references.push(ReferenceEntry {
        file: file_path.to_string(),
        name,
        kind: "type_annotation".to_string(),
        line,
        caller: parent_ctx.map(String::from),
        project: String::new(),
    });
}

fn extract_declaration(
    node: Node,
    source: &[u8],
    file_path: &str,
    parent_ctx: Option<&str>,
    symbols: &mut Vec<SymbolEntry>,
) {
    // Top-level declarations: variables, function prototypes, extern declarations
    // Skip if inside a function body (we only want top-level)
    if let Some(p) = node.parent()
        && (p.kind() == "compound_statement" || p.kind() == "case_statement")
    {
        return;
    }

    let line = node_line_range(node);
    let is_static = has_storage_class(node, source, "static");
    let _is_extern = has_storage_class(node, source, "extern");
    let visibility = if is_static { "private" } else { "public" };

    // Walk declarators
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "function_declarator" => {
                // Function prototype
                let name = extract_declarator_name(child, source);
                if !name.is_empty() {
                    let _sig = collapse_whitespace(node_text(node, source).trim());
                    let kind = "function";
                    // Prototypes don't have a body, so no tokens
                    push_symbol(
                        symbols,
                        file_path,
                        name,
                        kind,
                        line,
                        parent_ctx,
                        None,
                        None,
                        Some(visibility.to_string()),
                    );
                }
            }
            "init_declarator" => {
                if let Some(decl) = find_child_by_field(child, "declarator") {
                    let name = extract_declarator_name(decl, source);
                    if !name.is_empty() {
                        let kind = if name.chars().all(|c| c.is_uppercase() || c == '_')
                            && name.len() > 1
                        {
                            "constant"
                        } else {
                            "variable"
                        };
                        push_symbol(
                            symbols,
                            file_path,
                            name,
                            kind,
                            line,
                            parent_ctx,
                            None,
                            None,
                            Some(visibility.to_string()),
                        );
                    }
                }
            }
            "identifier" => {
                let name = extract_declarator_name(child, source);
                if !name.is_empty() {
                    push_symbol(
                        symbols,
                        file_path,
                        name,
                        "variable",
                        line,
                        parent_ctx,
                        None,
                        None,
                        Some(visibility.to_string()),
                    );
                }
            }
            "pointer_declarator" => {
                // Check if this is a function returning a pointer vs a pointer variable
                let has_func_decl = child
                    .children(&mut child.walk())
                    .any(|c| c.kind() == "function_declarator");
                let name = extract_declarator_name(child, source);
                if !name.is_empty() {
                    let kind = if has_func_decl {
                        "function"
                    } else {
                        "variable"
                    };
                    push_symbol(
                        symbols,
                        file_path,
                        name,
                        kind,
                        line,
                        parent_ctx,
                        None,
                        None,
                        Some(visibility.to_string()),
                    );
                }
            }
            _ => {}
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn extract_struct_or_union(
    node: Node,
    source: &[u8],
    file_path: &str,
    kind: &str,
    parent_ctx: Option<&str>,
    symbols: &mut Vec<SymbolEntry>,
    texts: &mut Vec<TextEntry>,
    references: &mut Vec<ReferenceEntry>,
) {
    let name = find_child_by_field(node, "name")
        .map(|n| node_text(n, source))
        .unwrap_or_default();

    if name.is_empty() {
        // Anonymous struct/union, skip symbol but recurse for comments
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "comment" {
                extract_comment(child, source, file_path, parent_ctx, texts);
            }
        }
        return;
    }

    let line = node_line_range(node);

    push_symbol(
        symbols,
        file_path,
        name.clone(),
        kind,
        line,
        parent_ctx,
        None,
        None,
        Some("public".to_string()),
    );

    // Extract fields and their type references
    if let Some(body) = find_child_by_field(node, "body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            if child.kind() == "field_declaration" {
                // Extract field type reference
                if let Some(type_node) = find_child_by_field(child, "type") {
                    extract_type_ref(type_node, source, file_path, Some(&name), references);
                }

                let mut field_cursor = child.walk();
                for field_child in child.children(&mut field_cursor) {
                    if field_child.kind() == "field_identifier" {
                        let field_name = node_text(field_child, source);
                        let field_line = node_line_range(child);
                        push_symbol(
                            symbols,
                            file_path,
                            format!("{name}.{field_name}"),
                            "property",
                            field_line,
                            Some(&name),
                            None,
                            None,
                            Some("public".to_string()),
                        );
                    }
                }
            }
            if child.kind() == "comment" {
                extract_comment(child, source, file_path, Some(&name), texts);
            }
        }
    }
}

fn extract_enum(
    node: Node,
    source: &[u8],
    file_path: &str,
    parent_ctx: Option<&str>,
    symbols: &mut Vec<SymbolEntry>,
) {
    let name = find_child_by_field(node, "name")
        .map(|n| node_text(n, source))
        .unwrap_or_default();

    if name.is_empty() {
        return;
    }

    let line = node_line_range(node);

    push_symbol(
        symbols,
        file_path,
        name.clone(),
        "enum",
        line,
        parent_ctx,
        None,
        None,
        Some("public".to_string()),
    );

    // Extract enum constants
    if let Some(body) = find_child_by_field(node, "body") {
        let mut cursor = body.walk();
        for child in body.children(&mut cursor) {
            if child.kind() == "enumerator"
                && let Some(name_node) = find_child_by_field(child, "name")
            {
                let const_name = node_text(name_node, source);
                let const_line = node_line_range(child);
                push_symbol(
                    symbols,
                    file_path,
                    format!("{name}.{const_name}"),
                    "constant",
                    const_line,
                    Some(&name),
                    None,
                    None,
                    Some("public".to_string()),
                );
            }
        }
    }
}

fn extract_typedef(node: Node, source: &[u8], file_path: &str, symbols: &mut Vec<SymbolEntry>) {
    let line = node_line_range(node);

    // The typedef name is typically the last declarator
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "type_identifier" || child.kind() == "identifier" {
            let name = node_text(child, source);
            push_symbol(
                symbols,
                file_path,
                name,
                "type_alias",
                line,
                None,
                None,
                None,
                Some("public".to_string()),
            );
        }
    }
}

fn extract_include(
    node: Node,
    source: &[u8],
    file_path: &str,
    symbols: &mut Vec<SymbolEntry>,
    references: &mut Vec<ReferenceEntry>,
) {
    let line = node_line_range(node);

    if let Some(path_node) = find_child_by_field(node, "path") {
        let path = node_text(path_node, source);
        // Strip < > or " "
        let path = path
            .trim_start_matches(['<', '"'])
            .trim_end_matches(['>', '"'])
            .to_string();
        push_symbol(
            symbols,
            file_path,
            path.clone(),
            "import",
            line,
            None,
            None,
            None,
            Some("private".to_string()),
        );
        // Also add import reference
        references.push(ReferenceEntry {
            file: file_path.to_string(),
            name: path,
            kind: "import".to_string(),
            line,
            caller: None,
            project: String::new(),
        });
    }
}

fn extract_macro(node: Node, source: &[u8], file_path: &str, symbols: &mut Vec<SymbolEntry>) {
    let name = match find_child_by_field(node, "name") {
        Some(n) => node_text(n, source),
        None => return,
    };

    let line = node_line_range(node);
    let kind = if node.kind() == "preproc_function_def" {
        "macro"
    } else {
        "constant"
    };

    push_symbol(
        symbols,
        file_path,
        name,
        kind,
        line,
        None,
        None,
        None,
        Some("public".to_string()),
    );
}

fn extract_declarator_name(node: Node, source: &[u8]) -> String {
    match node.kind() {
        "identifier" => node_text(node, source),
        "field_identifier" => node_text(node, source),
        "pointer_declarator" => {
            // *name — recurse into the declarator
            find_child_by_field(node, "declarator")
                .map(|d| extract_declarator_name(d, source))
                .unwrap_or_default()
        }
        "function_declarator" => find_child_by_field(node, "declarator")
            .map(|d| extract_declarator_name(d, source))
            .unwrap_or_default(),
        "array_declarator" => find_child_by_field(node, "declarator")
            .map(|d| extract_declarator_name(d, source))
            .unwrap_or_default(),
        "parenthesized_declarator" => {
            // (name) — look inside
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                let name = extract_declarator_name(child, source);
                if !name.is_empty() {
                    return name;
                }
            }
            String::new()
        }
        _ => String::new(),
    }
}

fn has_storage_class(node: Node, source: &[u8], class: &str) -> bool {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "storage_class_specifier" {
            let text = node_text(child, source);
            if text == class {
                return true;
            }
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::treesitter::parse_file;

    fn find_sym<'a>(symbols: &'a [SymbolEntry], name: &str) -> &'a SymbolEntry {
        symbols
            .iter()
            .find(|s| s.name == name)
            .unwrap_or_else(|| panic!("symbol not found: {name}"))
    }

    #[test]
    fn test_c_functions() {
        let source = b"int add(int a, int b) {
    return a + b;
}

static void helper() {
    printf(\"helper\");
}";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let add = find_sym(&symbols, "add");
        assert_eq!(add.kind, "function");
        // Token extraction is enabled (may be None if body has no tokens after filtering)
        assert_eq!(add.visibility.as_deref(), Some("public"));

        let helper = find_sym(&symbols, "helper");
        assert_eq!(helper.visibility.as_deref(), Some("private"));
    }

    #[test]
    fn test_c_struct() {
        let source = b"struct Point {
    int x;
    int y;
};";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let point = find_sym(&symbols, "Point");
        assert_eq!(point.kind, "struct");

        let x = find_sym(&symbols, "Point.x");
        assert_eq!(x.kind, "property");
        assert_eq!(x.parent.as_deref(), Some("Point"));

        let y = find_sym(&symbols, "Point.y");
        assert_eq!(y.kind, "property");
    }

    #[test]
    fn test_c_enum() {
        let source = b"enum Status {
    OK,
    ERROR,
    PENDING
};";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let status = find_sym(&symbols, "Status");
        assert_eq!(status.kind, "enum");

        let ok = find_sym(&symbols, "Status.OK");
        assert_eq!(ok.kind, "constant");
        assert_eq!(ok.parent.as_deref(), Some("Status"));

        let error = find_sym(&symbols, "Status.ERROR");
        assert_eq!(error.kind, "constant");
    }

    #[test]
    fn test_c_typedef() {
        let source = b"typedef int MyInt;

int add(MyInt a, MyInt b) {
    return a + b;
}";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        // Function should definitely be extracted
        let add = find_sym(&symbols, "add");
        assert_eq!(add.kind, "function");
    }

    #[test]
    fn test_c_variables() {
        let source = b"int global = 100;
static int file_scoped = 200;
extern int external;

#define MAX_SIZE 1000";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let global = find_sym(&symbols, "global");
        assert_eq!(global.kind, "variable");
        assert_eq!(global.visibility.as_deref(), Some("public"));

        let file_scoped = find_sym(&symbols, "file_scoped");
        assert_eq!(file_scoped.visibility.as_deref(), Some("private"));

        let max_size = find_sym(&symbols, "MAX_SIZE");
        assert_eq!(max_size.kind, "constant");
    }

    #[test]
    fn test_c_includes() {
        let source = b"#include <stdio.h>
#include \"myheader.h\"";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let stdio = symbols.iter().find(|s| s.name == "stdio.h").unwrap();
        assert_eq!(stdio.kind, "import");

        let myheader = symbols.iter().find(|s| s.name == "myheader.h").unwrap();
        assert_eq!(myheader.kind, "import");
    }

    #[test]
    fn test_c_macros() {
        let source = b"#define PI 3.14159
#define MAX(a, b) ((a) > (b) ? (a) : (b))";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let pi = find_sym(&symbols, "PI");
        assert_eq!(pi.kind, "constant");

        let max = find_sym(&symbols, "MAX");
        assert_eq!(max.kind, "macro");
    }

    #[test]
    fn test_c_union() {
        let source = b"union Data {
    int i;
    float f;
    char str[20];
};";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let data = find_sym(&symbols, "Data");
        assert_eq!(data.kind, "struct"); // unions mapped to struct

        let i = find_sym(&symbols, "Data.i");
        assert_eq!(i.kind, "property");
    }

    #[test]
    fn test_c_comments() {
        let source = b"/* Block comment */
// Single line comment
int foo() { return 0; }";
        let (_symbols, texts, _refs) = parse_file(source, "c", "test.c").unwrap();
        assert!(texts.iter().any(|t| t.kind == "comment"));
    }

    #[test]
    fn test_c_function_prototype() {
        let source = b"int add(int a, int b);
extern void print(const char* msg);";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let add = find_sym(&symbols, "add");
        assert_eq!(add.kind, "function");
        // Prototypes don't have bodies, so no tokens
        assert!(add.tokens.is_none());
    }

    #[test]
    fn test_c_function_returning_pointer() {
        // Functions returning pointers should be detected as functions, not variables
        let source = b"const char *crypto_secretbox_primitive(void);
char* get_buffer(int size);
int* allocate_array(void);";
        let (symbols, _texts, _refs) = parse_file(source, "c", "test.c").unwrap();

        let crypto = find_sym(&symbols, "crypto_secretbox_primitive");
        assert_eq!(
            crypto.kind, "function",
            "function returning const char* should be function"
        );

        let get_buffer = find_sym(&symbols, "get_buffer");
        assert_eq!(
            get_buffer.kind, "function",
            "function returning char* should be function"
        );

        let alloc = find_sym(&symbols, "allocate_array");
        assert_eq!(
            alloc.kind, "function",
            "function returning int* should be function"
        );
    }

    #[test]
    fn test_c_call_references() {
        let source = b"void foo() {}
void bar() {
    foo();
    my_custom_function();
}";
        let (_symbols, _texts, refs) = parse_file(source, "c", "test.c").unwrap();

        let calls: Vec<_> = refs.iter().filter(|r| r.kind == "call").collect();
        assert!(calls.iter().any(|r| r.name == "foo"));
        assert!(calls.iter().any(|r| r.name == "my_custom_function"));
    }

    #[test]
    fn test_c_include_references() {
        let source = b"#include <stdio.h>
#include \"myheader.h\"";
        let (_symbols, _texts, refs) = parse_file(source, "c", "test.c").unwrap();

        let imports: Vec<_> = refs.iter().filter(|r| r.kind == "import").collect();
        assert!(imports.iter().any(|r| r.name == "stdio.h"));
        assert!(imports.iter().any(|r| r.name == "myheader.h"));
    }

    #[test]
    fn test_c_type_references() {
        let source = b"typedef struct MyStruct MyStruct;

MyStruct* create_mystruct() {
    return NULL;
}

struct Container {
    MyStruct* data;
};";
        let (_symbols, _texts, refs) = parse_file(source, "c", "test.c").unwrap();

        let type_refs: Vec<_> = refs
            .iter()
            .filter(|r| r.kind == "type_annotation")
            .collect();
        assert!(type_refs.iter().any(|r| r.name == "MyStruct"));
    }
}