codegraph-c 0.1.3

C parser for CodeGraph - extracts code entities and relationships from C source files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
//! Integration tests for the C parser

use codegraph::{CodeGraph, NodeType};
use codegraph_c::{CParser, CodeParser};
use std::path::Path;

#[test]
fn test_parse_hello_world() {
    let source = r#"
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("hello.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 1);
    assert_eq!(file_info.imports.len(), 1);
}

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

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("math.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 3);
}

#[test]
fn test_parse_struct_with_typedef() {
    let source = r#"
struct Node {
    int data;
    struct Node *next;
};

typedef struct Node Node;

Node* create_node(int data) {
    return NULL;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("node.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.classes.len(), 1); // struct Node
    assert_eq!(file_info.functions.len(), 1); // create_node
}

#[test]
fn test_parse_enum() {
    let source = r#"
enum Status {
    OK = 0,
    ERROR = 1,
    PENDING = 2
};

int get_status() {
    return OK;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("status.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.classes.len(), 1); // enum Status
    assert_eq!(file_info.functions.len(), 1); // get_status
}

#[test]
fn test_parse_union() {
    let source = r#"
union Data {
    int i;
    float f;
    char str[20];
};

void process_data(union Data *d) {
    d->i = 10;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("data.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.classes.len(), 1); // union Data
    assert_eq!(file_info.functions.len(), 1); // process_data
}

#[test]
fn test_parse_static_functions() {
    let source = r#"
static int helper(int x) {
    return x * 2;
}

int public_func(int x) {
    return helper(x) + 1;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("module.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 2);
}

#[test]
fn test_parse_multiple_includes() {
    let source = r#"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myheader.h"

void test() {}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("test.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.imports.len(), 4);
}

#[test]
fn test_parse_pointer_parameters() {
    let source = r#"
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void process_array(int arr[], int size) {
    // Process array
}

void process_strings(char **strings, int count) {
    // Process strings
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("pointers.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 3);
}

#[test]
fn test_parse_variadic_function() {
    let source = r#"
#include <stdarg.h>

int sum(int count, ...) {
    va_list args;
    va_start(args, count);

    int total = 0;
    for (int i = 0; i < count; i++) {
        total += va_arg(args, int);
    }

    va_end(args);
    return total;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("varargs.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 1);
}

#[test]
fn test_parse_complex_function() {
    let source = r#"
int complex_function(int x, int y) {
    int result = 0;

    if (x > 0) {
        for (int i = 0; i < x; i++) {
            if (i % 2 == 0) {
                result += i;
            } else {
                result -= i;
            }
        }
    } else if (x < 0) {
        while (y > 0) {
            result += y;
            y--;
        }
    } else {
        switch (y) {
            case 1:
                result = 100;
                break;
            case 2:
                result = 200;
                break;
            default:
                result = 0;
                break;
        }
    }

    return result;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("complex.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 1);
}

#[test]
fn test_parse_header_file() {
    let parser = CParser::new();

    assert!(parser.can_parse(Path::new("header.h")));
    assert!(parser.can_parse(Path::new("source.c")));
    assert!(!parser.can_parse(Path::new("source.cpp")));
    assert!(!parser.can_parse(Path::new("source.rs")));
}

#[test]
fn test_parse_nested_structs() {
    let source = r#"
struct Address {
    char street[100];
    char city[50];
    int zip;
};

struct Person {
    char name[100];
    int age;
    struct Address address;
};
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("person.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.classes.len(), 2);
}

#[test]
fn test_parse_syntax_error() {
    let source = r#"
int broken( {
    // Missing closing brace
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("broken.c"), &mut graph);

    assert!(result.is_err());
}

#[test]
fn test_parse_empty_file() {
    let source = "";

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("empty.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 0);
    assert_eq!(file_info.classes.len(), 0);
}

#[test]
fn test_parse_comments_only() {
    let source = r#"
// This is a comment
/* This is a
   multi-line comment */
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("comments.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 0);
}

#[test]
fn test_parser_metrics() {
    let source = r#"
int test() { return 0; }
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let mut parser = CParser::new();

    // Reset metrics
    parser.reset_metrics();
    let metrics = parser.metrics();
    assert_eq!(metrics.files_attempted, 0);

    // Parse a file
    let _ = parser.parse_source(source, Path::new("test.c"), &mut graph);

    // Note: parse_source doesn't update metrics, only parse_file does
    // This is by design to avoid double counting
}

#[test]
fn test_parser_language() {
    let parser = CParser::new();
    assert_eq!(parser.language(), "c");
}

#[test]
fn test_parser_file_extensions() {
    let parser = CParser::new();
    let extensions = parser.file_extensions();
    assert!(extensions.contains(&".c"));
    assert!(extensions.contains(&".h"));
}

#[test]
fn test_parse_function_pointer_param() {
    let source = r#"
void register_callback(void (*callback)(int)) {
    // Store callback
}

void process(int (*compare)(const void*, const void*)) {
    // Use comparator
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("callbacks.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 2);
}

#[test]
fn test_parse_const_params() {
    let source = r#"
void print_string(const char *str) {
    // Print string
}

int compare(const int *a, const int *b) {
    return *a - *b;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("const.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 2);
}

#[test]
fn test_parse_goto_complexity() {
    let source = r#"
void cleanup(int *resources, int count) {
    for (int i = 0; i < count; i++) {
        if (resources[i] < 0) {
            goto error;
        }
    }
    return;

error:
    // Cleanup code
    return;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("goto.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 1);
}

#[test]
fn test_parse_ternary_operator() {
    let source = r#"
int max(int a, int b) {
    return a > b ? a : b;
}

int abs_val(int x) {
    return x >= 0 ? x : -x;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("ternary.c"), &mut graph);

    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 2);
}

// ============================================================================
// End-to-End Tests: Verify properties are correctly stored in CodeGraph
// ============================================================================

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

static void helper(void) {
    // Internal helper
}

char *get_name(const char *prefix, int id) {
    return NULL;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("functions.c"), &mut graph);
    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 3);

    // Query all functions from the graph
    let function_ids = graph
        .query()
        .node_type(NodeType::Function)
        .execute()
        .unwrap();
    assert_eq!(function_ids.len(), 3);

    // Verify each function has correct properties
    for func_id in &function_ids {
        let node = graph.get_node(*func_id).unwrap();
        assert_eq!(node.node_type, NodeType::Function);

        // All functions must have a name
        let name = node.properties.get_string("name").unwrap();
        assert!(!name.is_empty(), "Function should have a name");

        // All functions must have line numbers
        assert!(
            node.properties.get_int("line_start").is_some(),
            "Function should have line_start"
        );
        assert!(
            node.properties.get_int("line_end").is_some(),
            "Function should have line_end"
        );

        // Visibility should be set
        assert!(
            node.properties.get_string("visibility").is_some(),
            "Function should have visibility"
        );
    }

    // Find 'add' function and verify specific properties
    let add_funcs: Vec<_> = function_ids
        .iter()
        .filter_map(|id| {
            let node = graph.get_node(*id).ok()?;
            let name = node.properties.get_string("name")?;
            if name == "add" {
                Some(node)
            } else {
                None
            }
        })
        .collect();
    assert_eq!(add_funcs.len(), 1, "Should find exactly one 'add' function");
    let add_func = add_funcs[0];
    assert_eq!(
        add_func.properties.get_string("visibility").unwrap(),
        "public"
    );

    // Find 'helper' function and verify it's static (private)
    let helper_funcs: Vec<_> = function_ids
        .iter()
        .filter_map(|id| {
            let node = graph.get_node(*id).ok()?;
            let name = node.properties.get_string("name")?;
            if name == "helper" {
                Some(node)
            } else {
                None
            }
        })
        .collect();
    assert_eq!(
        helper_funcs.len(),
        1,
        "Should find exactly one 'helper' function"
    );
    let helper_func = helper_funcs[0];
    // Static functions should be marked as private
    assert_eq!(
        helper_func.properties.get_string("visibility").unwrap(),
        "private",
        "Static function should have private visibility"
    );
}

#[test]
fn test_e2e_struct_properties_stored_in_graph() {
    let source = r#"
struct Point {
    int x;
    int y;
};

struct Rectangle {
    struct Point top_left;
    struct Point bottom_right;
    int area;
};

enum Color {
    RED = 0,
    GREEN = 1,
    BLUE = 2
};

union Value {
    int i;
    float f;
    double d;
};
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("types.c"), &mut graph);
    assert!(result.is_ok());
    let file_info = result.unwrap();
    assert_eq!(file_info.classes.len(), 4); // 2 structs + 1 enum + 1 union

    // Query all classes from the graph
    let class_ids = graph.query().node_type(NodeType::Class).execute().unwrap();
    assert_eq!(class_ids.len(), 4);

    // Verify each class has correct properties
    for class_id in &class_ids {
        let node = graph.get_node(*class_id).unwrap();
        assert_eq!(node.node_type, NodeType::Class);

        // All classes must have a name
        let name = node.properties.get_string("name").unwrap();
        assert!(!name.is_empty(), "Class should have a name");

        // All classes must have line numbers
        assert!(
            node.properties.get_int("line_start").is_some(),
            "Class should have line_start"
        );
        assert!(
            node.properties.get_int("line_end").is_some(),
            "Class should have line_end"
        );
    }

    // Verify specific classes exist
    let class_names: Vec<String> = class_ids
        .iter()
        .filter_map(|id| {
            let node = graph.get_node(*id).ok()?;
            node.properties.get_string("name").map(String::from)
        })
        .collect();

    assert!(
        class_names.contains(&"Point".to_string()),
        "Should find Point struct"
    );
    assert!(
        class_names.contains(&"Rectangle".to_string()),
        "Should find Rectangle struct"
    );
    assert!(
        class_names.contains(&"Color".to_string()),
        "Should find Color enum"
    );
    assert!(
        class_names.contains(&"Value".to_string()),
        "Should find Value union"
    );
}

#[test]
fn test_e2e_query_builder_filters() {
    let source = r#"
static int private_func1(void) { return 1; }
static int private_func2(void) { return 2; }
int public_func1(void) { return 3; }
int public_func2(void) { return 4; }
int public_func3(void) { return 5; }
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("visibility.c"), &mut graph);
    assert!(result.is_ok());
    assert_eq!(result.unwrap().functions.len(), 5);

    // Query only public functions
    let public_funcs = graph
        .query()
        .node_type(NodeType::Function)
        .property("visibility", "public")
        .execute()
        .unwrap();
    assert_eq!(public_funcs.len(), 3, "Should find 3 public functions");

    // Query only private (static) functions
    let private_funcs = graph
        .query()
        .node_type(NodeType::Function)
        .property("visibility", "private")
        .execute()
        .unwrap();
    assert_eq!(
        private_funcs.len(),
        2,
        "Should find 2 private (static) functions"
    );

    // Query functions by name pattern
    let funcs_with_1 = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("1")
        .execute()
        .unwrap();
    assert_eq!(
        funcs_with_1.len(),
        2,
        "Should find 2 functions containing '1'"
    );

    // Query with limit
    let limited = graph
        .query()
        .node_type(NodeType::Function)
        .limit(2)
        .execute()
        .unwrap();
    assert_eq!(
        limited.len(),
        2,
        "Should return only 2 results when limited"
    );

    // Count query
    let count = graph.query().node_type(NodeType::Function).count().unwrap();
    assert_eq!(count, 5, "Count should return total number of functions");

    // Exists query
    let exists = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("public")
        .exists()
        .unwrap();
    assert!(
        exists,
        "Should find at least one function containing 'public'"
    );

    let not_exists = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("nonexistent")
        .exists()
        .unwrap();
    assert!(
        !not_exists,
        "Should not find any function containing 'nonexistent'"
    );
}

#[test]
fn test_e2e_file_node_and_contains_edges() {
    let source = r#"
struct Data {
    int value;
};

int process_data(struct Data *d) {
    return d->value;
}

void cleanup(void) {}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("data_processor.c"), &mut graph);
    assert!(result.is_ok());
    let file_info = result.unwrap();

    // File node should exist
    let file_nodes = graph
        .query()
        .node_type(NodeType::CodeFile)
        .execute()
        .unwrap();
    assert_eq!(file_nodes.len(), 1, "Should have exactly one file node");

    let file_node = graph.get_node(file_nodes[0]).unwrap();
    let file_path = file_node.properties.get_string("path").unwrap();
    assert_eq!(file_path, "data_processor.c");

    // Verify file contains functions and classes
    // The file_info should track the node IDs
    assert_eq!(file_info.functions.len(), 2);
    assert_eq!(file_info.classes.len(), 1);

    // Verify we can retrieve the functions by their IDs
    for func_id in &file_info.functions {
        let func_node = graph.get_node(*func_id).unwrap();
        assert_eq!(func_node.node_type, NodeType::Function);
    }

    for class_id in &file_info.classes {
        let class_node = graph.get_node(*class_id).unwrap();
        assert_eq!(class_node.node_type, NodeType::Class);
    }
}

#[test]
fn test_e2e_import_nodes_created() {
    let source = r#"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "local_header.h"

int main() {
    printf("Hello\n");
    return 0;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("imports.c"), &mut graph);
    assert!(result.is_ok());
    let file_info = result.unwrap();

    // Should have 4 imports
    assert_eq!(file_info.imports.len(), 4);

    // Verify import nodes can be retrieved
    for import_id in &file_info.imports {
        let _import_node = graph.get_node(*import_id).unwrap();
        // Import nodes should exist and be retrievable
    }
}

#[test]
fn test_e2e_kernel_style_code_with_macros() {
    // Test kernel-style C code that exercises the macro pipeline
    let source = r#"
#include <linux/module.h>
#include <linux/kernel.h>

#define DRIVER_NAME "test_driver"
#define MAX_DEVICES 16

struct device_info {
    int id;
    char name[64];
    unsigned long flags;
};

static struct device_info devices[MAX_DEVICES];

static int driver_init(void) {
    return 0;
}

static void driver_exit(void) {
}

static int process_device(struct device_info *dev) {
    if (!dev) {
        return -1;
    }
    return dev->id;
}

int public_api_function(int device_id) {
    if (device_id < 0 || device_id >= MAX_DEVICES) {
        return -1;
    }
    return process_device(&devices[device_id]);
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("driver.c"), &mut graph);

    // Should parse successfully despite kernel macros
    assert!(
        result.is_ok(),
        "Kernel-style code should parse successfully: {:?}",
        result.err()
    );
    let file_info = result.unwrap();

    // Should find functions
    assert!(
        file_info.functions.len() >= 2,
        "Should find at least 2 functions, found {}",
        file_info.functions.len()
    );

    // Should find the struct
    assert!(
        !file_info.classes.is_empty(),
        "Should find at least 1 struct, found {}",
        file_info.classes.len()
    );

    // Verify we can query the functions
    let func_count = graph.query().node_type(NodeType::Function).count().unwrap();
    assert!(func_count >= 2, "Graph should contain at least 2 functions");
}

#[test]
fn test_e2e_multiline_define_handling() {
    // Test that multi-line #define macros are handled correctly
    let source = r#"
#define COMPLEX_MACRO(x, y) \
    do { \
        int temp = (x); \
        (x) = (y); \
        (y) = temp; \
    } while(0)

#define SIMPLE_MACRO 42

int use_macros(int a, int b) {
    COMPLEX_MACRO(a, b);
    return a + SIMPLE_MACRO;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("macros.c"), &mut graph);

    assert!(result.is_ok(), "Code with multi-line macros should parse");
    let file_info = result.unwrap();
    assert_eq!(
        file_info.functions.len(),
        1,
        "Should find use_macros function"
    );
}

#[test]
fn test_e2e_ifdef_handling() {
    // Test that #ifdef blocks are handled correctly
    let source = r#"
#ifdef CONFIG_DEBUG
#define DEBUG_PRINT(x) printf("%s\n", x)
#else
#define DEBUG_PRINT(x)
#endif

#ifndef MAX_SIZE
#define MAX_SIZE 1024
#endif

int main(void) {
    DEBUG_PRINT("test");
    return 0;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("conditionals.c"), &mut graph);

    assert!(result.is_ok(), "Code with #ifdef should parse");
    let file_info = result.unwrap();
    assert_eq!(file_info.functions.len(), 1, "Should find main function");
}

#[test]
fn test_e2e_for_each_macro_handling() {
    // Test that for_each macros are handled correctly when defined in the same file
    // Note: This tests the macro neutralization - complex macro usages may need
    // a separate preprocessing step in real-world codebases
    let source = r#"
struct list_head {
    struct list_head *next;
    struct list_head *prev;
};

void iterate_list(struct list_head *head) {
    struct list_head *pos;
    for (pos = head->next; pos != head; pos = pos->next) {
        // Process item
    }
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("list_ops.c"), &mut graph);

    assert!(
        result.is_ok(),
        "Code with for loop should parse: {:?}",
        result.err()
    );
    let file_info = result.unwrap();
    assert_eq!(
        file_info.functions.len(),
        1,
        "Should find iterate_list function"
    );
    assert_eq!(file_info.classes.len(), 1, "Should find list_head struct");
}

#[test]
fn test_e2e_cleanup_attribute_handling() {
    // Test that GCC attributes are handled correctly
    // Note: Complex cleanup attributes with macro expansion may require
    // additional preprocessing in real-world codebases
    let source = r#"
void free_ptr(void **ptr) {
    // Free implementation
}

int use_cleanup(void) {
    void *ptr = NULL;
    return 0;
}

// Function with common GCC attributes
__attribute__((unused))
static void unused_func(void) {}

__attribute__((always_inline))
static inline int fast_add(int a, int b) {
    return a + b;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("cleanup.c"), &mut graph);

    assert!(
        result.is_ok(),
        "Code with attributes should parse: {:?}",
        result.err()
    );
    let file_info = result.unwrap();
    assert!(
        file_info.functions.len() >= 2,
        "Should find at least 2 functions, found {}",
        file_info.functions.len()
    );
}

#[test]
fn test_e2e_line_numbers_accuracy() {
    let source = r#"int first_func(void) {
    return 1;
}

int second_func(void) {
    return 2;
}

int third_func(void) {
    return 3;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("lines.c"), &mut graph);
    assert!(result.is_ok());

    // Find first_func and verify line numbers
    let first_func = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("first")
        .execute()
        .unwrap();
    assert_eq!(first_func.len(), 1);

    let first_node = graph.get_node(first_func[0]).unwrap();
    let line_start = first_node.properties.get_int("line_start").unwrap();
    let line_end = first_node.properties.get_int("line_end").unwrap();

    // first_func starts at line 1
    assert_eq!(line_start, 1, "first_func should start at line 1");
    // first_func ends at line 3
    assert_eq!(line_end, 3, "first_func should end at line 3");

    // Find second_func and verify line numbers
    let second_func = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("second")
        .execute()
        .unwrap();
    assert_eq!(second_func.len(), 1);

    let second_node = graph.get_node(second_func[0]).unwrap();
    let line_start = second_node.properties.get_int("line_start").unwrap();

    // second_func starts at line 5
    assert_eq!(line_start, 5, "second_func should start at line 5");
}

#[test]
fn test_e2e_function_signature_stored() {
    let source = r#"
int simple(void) { return 0; }

char *complex_signature(const int *arr, size_t len, void (*callback)(int)) {
    return NULL;
}

static unsigned long long very_long_return_type(void) {
    return 0ULL;
}
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("signatures.c"), &mut graph);
    assert!(result.is_ok());

    let functions = graph
        .query()
        .node_type(NodeType::Function)
        .execute()
        .unwrap();
    assert_eq!(functions.len(), 3);

    // Verify all functions have signatures
    for func_id in &functions {
        let node = graph.get_node(*func_id).unwrap();
        let signature = node.properties.get_string("signature");
        assert!(
            signature.is_some(),
            "Function should have a signature stored"
        );
        assert!(
            !signature.unwrap().is_empty(),
            "Signature should not be empty"
        );
    }
}

#[test]
fn test_e2e_return_type_stored() {
    let source = r#"
int returns_int(void) { return 0; }
void returns_void(void) {}
char *returns_pointer(void) { return NULL; }
struct Data *returns_struct_ptr(void) { return NULL; }
"#;

    let mut graph = CodeGraph::in_memory().unwrap();
    let parser = CParser::new();
    let result = parser.parse_source(source, Path::new("return_types.c"), &mut graph);
    assert!(result.is_ok());

    // Find returns_int and check return type
    let int_func = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("returns_int")
        .execute()
        .unwrap();
    assert_eq!(int_func.len(), 1);

    let node = graph.get_node(int_func[0]).unwrap();
    let return_type = node.properties.get_string("return_type");
    assert!(
        return_type.is_some(),
        "Function should have return_type property"
    );
    assert_eq!(return_type.unwrap(), "int");

    // Find returns_void and check return type
    let void_func = graph
        .query()
        .node_type(NodeType::Function)
        .name_contains("returns_void")
        .execute()
        .unwrap();
    assert_eq!(void_func.len(), 1);

    let node = graph.get_node(void_func[0]).unwrap();
    let return_type = node.properties.get_string("return_type");
    assert!(return_type.is_some());
    assert_eq!(return_type.unwrap(), "void");
}