c_source_parser_ffi 2025.1.6

Scan C/C++ source tree to get #include dependency and symbols
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
// c
#include <stdio.h>
#include <string.h>

// clang
#include <clang.h>

// dylib
#include "dylib.h"

// self
#include "lib.h"


#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef NULL
#define NULL ((void *)0)
#endif

#ifndef IN
#define IN
#endif

#ifndef OUT
#define OUT
#endif

#ifndef IN_OUT
#define IN_OUT
#endif


func_ptr_clang_createIndex clang_createIndex = NULL;
func_ptr_clang_disposeIndex clang_disposeIndex = NULL;
func_ptr_clang_parseTranslationUnit clang_parseTranslationUnit = NULL;
func_ptr_clang_disposeTranslationUnit clang_disposeTranslationUnit = NULL;
func_ptr_clang_visitChildren clang_visitChildren = NULL;
func_ptr_clang_getTranslationUnitCursor clang_getTranslationUnitCursor = NULL;
func_ptr_clang_getCursorLocation clang_getCursorLocation = NULL;
func_ptr_clang_getFileLocation clang_getFileLocation = NULL;
func_ptr_clang_getCursorKind clang_getCursorKind = NULL;
func_ptr_clang_getIncludedFile clang_getIncludedFile = NULL;
func_ptr_clang_getFileName clang_getFileName = NULL;
func_ptr_clang_getCString clang_getCString = NULL;
func_ptr_clang_disposeString clang_disposeString = NULL;
func_ptr_clang_getCursorSpelling clang_getCursorSpelling = NULL;
func_ptr_clang_Cursor_getNumArguments clang_Cursor_getNumArguments = NULL;
func_ptr_clang_Cursor_getArgument clang_Cursor_getArgument = NULL;
func_ptr_clang_getCursorType clang_getCursorType = NULL;
func_ptr_clang_getCanonicalType clang_getCanonicalType = NULL;
func_ptr_clang_getResultType clang_getResultType = NULL;
func_ptr_clang_getTypeSpelling clang_getTypeSpelling = NULL;
func_ptr_clang_getCursorSemanticParent clang_getCursorSemanticParent = NULL;
func_ptr_clang_Cursor_isNull clang_Cursor_isNull = NULL;


static void replace_chars(IN_OUT char *str, IN const char old_char, IN const char new_char) {
    if (NULL == str) {
        return;
    }

    while (*str) {
        if (*str == old_char) {
            *str = new_char;
        }
        str++;
    }
}


static int starts_with(IN const char *str, IN const char *sub) {
    if (NULL == str || NULL == sub) {
        return FALSE;
    }

    size_t str_len = strlen(str);
    size_t sub_len = strlen(sub);
    if (sub_len > str_len) {
        return FALSE;
    }

    return (0 == strncmp(str, sub, sub_len)) ? TRUE : FALSE;
}


static char *get_namespaces(IN CXCursor cursor) {
    RustVecOfStr vec = rust_vec_of_str_new();

    CXCursor parent_cursor = clang_getCursorSemanticParent(cursor);
    while (!clang_Cursor_isNull(parent_cursor)) {
        if (clang_getCursorKind(parent_cursor) == CXCursor_Namespace) {
            CXString spelling = clang_getCursorSpelling(parent_cursor);
            rust_vec_of_str_push(vec, clang_getCString(spelling));
            // free clang resources
            clang_disposeString(spelling);
        }

        parent_cursor = clang_getCursorSemanticParent(parent_cursor);
    }

    rust_vec_of_str_reverse(vec);
    char *text = rust_vec_of_str_join(vec, "::");

    // free rust resources
    rust_vec_of_str_drop(vec);

    return text;
}


static char *get_classes(IN CXCursor cursor) {
    RustVecOfStr vec = rust_vec_of_str_new();

    CXCursor parent_cursor = clang_getCursorSemanticParent(cursor);
    while (!clang_Cursor_isNull(parent_cursor)) {
        if (clang_getCursorKind(parent_cursor) == CXCursor_ClassDecl) {
            CXString spelling = clang_getCursorSpelling(parent_cursor);
            rust_vec_of_str_push(vec, clang_getCString(spelling));
            // free clang resources
            clang_disposeString(spelling);
        }

        parent_cursor = clang_getCursorSemanticParent(parent_cursor);
    }

    rust_vec_of_str_reverse(vec);
    char *text = rust_vec_of_str_join(vec, "::");

    // free rust resources
    rust_vec_of_str_drop(vec);

    return text;
}


void store_symbol(IN_OUT RustBtreeMapOfStrSet map_source_symbol, IN const char *source_path, IN const char *type_name, IN CXCursor cursor) {
    RustVecOfStr vec = rust_vec_of_str_new();
    rust_vec_of_str_push(vec, type_name);

    char *namespaces = get_namespaces(cursor);
    if (strlen(namespaces) != 0) {
        rust_vec_of_str_push(vec, namespaces);
        rust_vec_of_str_push(vec, "::");
    }

    char *classes = get_classes(cursor);
    if (strlen(classes) != 0) {
        rust_vec_of_str_push(vec, classes);
        rust_vec_of_str_push(vec, "::");
    }

    CXString spell = clang_getCursorSpelling(cursor);
    const char *name = clang_getCString(spell);
    rust_vec_of_str_push(vec, strlen(name) > 0 ? name : "@UNNAMED@");
    char *symbol = rust_vec_of_str_join(vec, " ");

    rust_btree_map_of_str_set_insert(map_source_symbol, source_path, symbol);

    // free clang resources
    clang_disposeString(spell);
    // free rust resources
    rust_vec_of_str_drop(vec);
    rust_c_str_drop(namespaces);
    rust_c_str_drop(classes);
    rust_c_str_drop(symbol);
}


static enum CXChildVisitResult visit_symbols_and_inclusions(IN CXCursor cursor, IN CXCursor parent, IN_OUT CXClientData client_data) {
    (void)parent;
    
    ClangParsedResult *result = (ClangParsedResult *)client_data;

    // get location
    CXSourceLocation location = clang_getCursorLocation(cursor);
    CXFile cx_file = NULL;
    unsigned int line = 0;
    unsigned int column = 0;
    clang_getFileLocation(location, &cx_file, &line, &column, NULL);
    CXString cx_str_source_path = clang_getFileName(cx_file);

    // skip null
    if(NULL == cx_str_source_path.data) {
        return CXChildVisit_Continue;
    }
    char *source_path = (char *)cx_str_source_path.data;
    replace_chars(source_path, '\\', '/');
    // skip parsed
    if (TRUE == rust_btree_set_of_str_contains(result->last_parsed_files, source_path)) {
        return CXChildVisit_Continue;
    }
    // skip third party files
    if (FALSE == starts_with(source_path, result->source_dir) && FALSE == starts_with(source_path, result->target_dir)) {
        return CXChildVisit_Continue;
    }
    rust_btree_set_of_str_insert(result->current_parsed_files, source_path);

    // format symbol signature
    enum CXCursorKind cursor_type = clang_getCursorKind(cursor);
    switch (cursor_type) {
    case CXCursor_InclusionDirective:
    {
        CXFile include_file = clang_getIncludedFile(cursor);
        if (include_file != NULL) {
            CXString cx_str_include_path = clang_getFileName(include_file);
            char *include_path = (char *)clang_getCString(cx_str_include_path);
            replace_chars(include_path, '\\', '/');

            // skip third-party
            if (TRUE == starts_with(include_path, result->source_dir) || TRUE == starts_with(include_path, result->target_dir)) {
                // collect inclusions
                rust_btree_map_of_str_set_insert(result->header_include_by_sources, include_path, source_path);
                rust_btree_map_of_str_set_insert(result->source_include_headers, source_path, include_path);
            }

            // free clang resources
            clang_disposeString(cx_str_include_path);
        }
        break;
    }

    case CXCursor_FunctionDecl:
    case CXCursor_CXXMethod:
    case CXCursor_Constructor:
    case CXCursor_Destructor:
    {
        RustVecOfStr vec = rust_vec_of_str_new();

        const char *func_type = (cursor_type == CXCursor_FunctionDecl) ? "function " : "method ";
        rust_vec_of_str_push(vec, func_type);

        char *namespaces = get_namespaces(cursor);
        if (strlen(namespaces) != 0) {
            rust_vec_of_str_push(vec, namespaces);
            rust_vec_of_str_push(vec, "::");
        }

        char *classes = get_classes(cursor);
        if (strlen(classes) != 0) {
            rust_vec_of_str_push(vec, classes);
            rust_vec_of_str_push(vec, "::");
        }

        CXString cx_str_func_name = clang_getCursorSpelling(cursor);
        const char *func_name = clang_getCString(cx_str_func_name);
        rust_vec_of_str_push(vec, func_name);
        rust_vec_of_str_push(vec, "(");
        // free clang resources
        clang_disposeString(cx_str_func_name);

        int num_args = clang_Cursor_getNumArguments(cursor);
        for (int i = 0; i < num_args; ++i) {
            CXCursor arg_cursor = clang_Cursor_getArgument(cursor, i);
            CXType arg_type = clang_getCursorType(arg_cursor);
            CXType arg_canonical_type = clang_getCanonicalType(arg_type);

            CXString arg_type_name = (arg_canonical_type.kind == arg_type.kind)
                                         ? clang_getTypeSpelling(arg_type)
                                         : clang_getTypeSpelling(arg_canonical_type);

            if (i > 0) {
                rust_vec_of_str_push(vec, ", ");
            }
            rust_vec_of_str_push(vec, clang_getCString(arg_type_name));
            // free clang resources
            clang_disposeString(arg_type_name);
        }

        CXType return_type = clang_getResultType(clang_getCursorType(cursor));
        CXString return_type_name = clang_getTypeSpelling(return_type);
        rust_vec_of_str_push(vec, ") -> ");
        rust_vec_of_str_push(vec, clang_getCString(return_type_name));

        char *symbol = rust_vec_of_str_join(vec, "");
        rust_btree_map_of_str_set_insert(result->source_symbols, source_path, symbol);

        // free clang resources
        clang_disposeString(return_type_name);

        // free rust resources
        rust_c_str_drop(namespaces);
        rust_c_str_drop(classes);
        rust_vec_of_str_drop(vec);
        rust_c_str_drop(symbol);

        break;
    }

    case CXCursor_ClassDecl:
    {
        store_symbol(result->source_symbols, source_path, "class", cursor);
        break;
    }

    case CXCursor_StructDecl:
    {
        store_symbol(result->source_symbols, source_path, "struct", cursor);
        break;
    }

    case CXCursor_EnumDecl:
    {
        store_symbol(result->source_symbols, source_path, "enum", cursor);
        break;
    }

    case CXCursor_UnionDecl:
    {
        store_symbol(result->source_symbols, source_path, "union", cursor);
        break;
    }

    case CXCursor_VarDecl:
    {
        store_symbol(result->source_symbols, source_path, "var", cursor);
        break;
    }

    case CXCursor_TypedefDecl:
    {
        store_symbol(result->source_symbols, source_path, "typedef", cursor);
        break;
    }

    default:
        break;
    }

    clang_disposeString(cx_str_source_path);

    return CXChildVisit_Recurse;
}


AstCErrorCode load_library_clang(const char *library_clang_path) {
    dylib_handle lib_clang = dylib_open(library_clang_path);
    if(INVALID_DYLIB_HANDLE == lib_clang) {
        return AstCErrorLibraryClangNotFound;
    }

    clang_createIndex = dylib_get(lib_clang, $clang_createIndex$);
    if(INVALID_DYLIB_SYMBOL == clang_createIndex) {
        return AstCErrorSymbolClangCreateIndexNotFound;
    }
    clang_disposeIndex = dylib_get(lib_clang, $clang_disposeIndex$);
    if(INVALID_DYLIB_SYMBOL == clang_disposeIndex) {
        return AstCErrorSymbolClangDisposeIndexNotFound;
    }
    clang_parseTranslationUnit = dylib_get(lib_clang, $clang_parseTranslationUnit$);
    if(INVALID_DYLIB_SYMBOL == clang_parseTranslationUnit) {
        return AstCErrorSymbolClangParseTranslationUnitNotFound;
    }
    clang_disposeTranslationUnit = dylib_get(lib_clang, $clang_disposeTranslationUnit$);
    if(INVALID_DYLIB_SYMBOL == clang_disposeTranslationUnit) {
        return AstCErrorSymbolClangDisposeTranslationUnitNotFound;
    }
    clang_visitChildren = dylib_get(lib_clang, $clang_visitChildren$);
    if(INVALID_DYLIB_SYMBOL == clang_visitChildren) {
        return AstCErrorSymbolClangVisitChildrenNotFound;
    }
    clang_getTranslationUnitCursor = dylib_get(lib_clang, $clang_getTranslationUnitCursor$);
    if(INVALID_DYLIB_SYMBOL == clang_getTranslationUnitCursor) {
        return AstCErrorSymbolClangGetTranslationUnitCursorNotFound;
    }
    clang_getCursorLocation = dylib_get(lib_clang, $clang_getCursorLocation$);
    if(INVALID_DYLIB_SYMBOL == clang_getCursorLocation) {
        return AstCErrorSymbolClangGetCursorLocationNotFound;
    }
    clang_getFileLocation = dylib_get(lib_clang, $clang_getFileLocation$);
    if(INVALID_DYLIB_SYMBOL == clang_getFileLocation) {
        return AstCErrorSymbolClangGetFileLocationNotFound;
    }
    clang_getCursorKind = dylib_get(lib_clang, $clang_getCursorKind$);
    if(INVALID_DYLIB_SYMBOL == clang_getCursorKind) {
        return AstCErrorSymbolClangGetCursorKindNotFound;
    }
    clang_getIncludedFile = dylib_get(lib_clang, $clang_getIncludedFile$);
    if(INVALID_DYLIB_SYMBOL == clang_getIncludedFile) {
        return AstCErrorSymbolClangGetIncludedFileNotFound;
    }
    clang_getFileName = dylib_get(lib_clang, $clang_getFileName$);
    if(INVALID_DYLIB_SYMBOL == clang_getFileName) {
        return AstCErrorSymbolClangGetFileNameNotFound;
    }
    clang_getCString = dylib_get(lib_clang, $clang_getCString$);
    if(INVALID_DYLIB_SYMBOL == clang_getCString) {
        return AstCErrorSymbolClangGetCStringNotFound;
    }
    clang_disposeString = dylib_get(lib_clang, $clang_disposeString$);
    if(INVALID_DYLIB_SYMBOL == clang_disposeString) {
        return AstCErrorSymbolClangDisposeStringNotFound;
    }
    clang_getCursorSpelling = dylib_get(lib_clang, $clang_getCursorSpelling$);
    if(INVALID_DYLIB_SYMBOL == clang_getCursorSpelling) {
        return AstCErrorSymbolClangGetCursorSpellingNotFound;
    }
    clang_Cursor_getNumArguments = dylib_get(lib_clang, $clang_Cursor_getNumArguments$);
    if(INVALID_DYLIB_SYMBOL == clang_Cursor_getNumArguments) {
        return AstCErrorSymbolClangCursorGetNumArgumentsNotFound;
    }
    clang_Cursor_getArgument = dylib_get(lib_clang, $clang_Cursor_getArgument$);
    if(INVALID_DYLIB_SYMBOL == clang_Cursor_getArgument) {
        return AstCErrorSymbolClangCursorGetArgumentNotFound;
    }
    clang_getCursorType = dylib_get(lib_clang, $clang_getCursorType$);
    if(INVALID_DYLIB_SYMBOL == clang_getCursorType) {
        return AstCErrorSymbolClangGetCursorTypeNotFound;
    }
    clang_getCanonicalType = dylib_get(lib_clang, $clang_getCanonicalType$);
    if(INVALID_DYLIB_SYMBOL == clang_getCanonicalType) {
        return AstCErrorSymbolClangGetCanonicalTypeNotFound;
    }
    clang_getResultType = dylib_get(lib_clang, $clang_getResultType$);
    if(INVALID_DYLIB_SYMBOL == clang_getResultType) {
        return AstCErrorSymbolClangGetResultTypeNotFound;
    }
    clang_getTypeSpelling = dylib_get(lib_clang, $clang_getTypeSpelling$);
    if(INVALID_DYLIB_SYMBOL == clang_getTypeSpelling) {
        return AstCErrorSymbolClangGetTypeSpellingNotFound;
    }
    clang_getCursorSemanticParent = dylib_get(lib_clang, $clang_getCursorSemanticParent$);
    if(INVALID_DYLIB_SYMBOL == clang_getCursorSemanticParent) {
        return AstCErrorSymbolClangGetCursorSemanticParentNotFound;
    }
    clang_Cursor_isNull = dylib_get(lib_clang, $clang_Cursor_isNull$);
    if(INVALID_DYLIB_SYMBOL == clang_Cursor_isNull) {
        return AstCErrorSymbolClangCursorIsNullNotFound;
    }

    return AstCErrorNone;
}


ClangParsedResult scan_source_and_symbols(
    IN const char *source_path,
    IN const char *source_dir,
    IN const char *target_dir,
    IN const RustBtreeSetOfStr last_parsed_files
) {
    ClangParsedResult result;
    result.error_code = AstCErrorNone;
    result.source_path = source_path;
    result.source_dir = source_dir;
    result.target_dir = target_dir;
    result.last_parsed_files = last_parsed_files;
    result.current_parsed_files = rust_btree_set_of_str_new();
    result.source_symbols = rust_btree_map_of_str_set_new();
    result.source_include_headers = rust_btree_map_of_str_set_new();
    result.header_include_by_sources = rust_btree_map_of_str_set_new();

    const char *args[4] = {
        "-I",
        source_dir,
        "-I",
        target_dir,
    };

    CXIndex index = clang_createIndex(0, 0);
    CXTranslationUnit translation_unit = clang_parseTranslationUnit(
        index,
        source_path,
        args,
        4,
        NULL,
        0,
        CXTranslationUnit_DetailedPreprocessingRecord | CXTranslationUnit_SkipFunctionBodies | CXTranslationUnit_KeepGoing
    );
    if (NULL == translation_unit) {
        clang_disposeIndex(index);
        result.error_code = AstCErrorSymbolClangParseTranslationUnitCall;
        return result;
    }

    clang_visitChildren(
        clang_getTranslationUnitCursor(translation_unit),
        visit_symbols_and_inclusions,
        (CXClientData)&result
    );

    clang_disposeTranslationUnit(translation_unit);
    clang_disposeIndex(index);

    return result;
}