MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / build_import_map

Function build_import_map

src/pipeline/pass_semantic.c:78–156  ·  view source on GitHub ↗

Build per-file import map from cached extraction result or graph buffer edges. */

Source from the content-addressed store, hash-verified

76
77/* Build per-file import map from cached extraction result or graph buffer edges. */
78static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
79 const CBMFileResult *result, const char ***out_keys,
80 const char ***out_vals, int *out_count) {
81 *out_keys = NULL;
82 *out_vals = NULL;
83 *out_count = 0;
84
85 /* Fast path: build from cached extraction result (no JSON parsing) */
86 if (result && result->imports.count > 0) {
87 const char **keys = calloc((size_t)result->imports.count, sizeof(const char *));
88 const char **vals = calloc((size_t)result->imports.count, sizeof(const char *));
89 int count = 0;
90
91 for (int i = 0; i < result->imports.count; i++) {
92 const CBMImport *imp = &result->imports.items[i];
93 if (!imp->local_name || !imp->local_name[0] || !imp->module_path) {
94 continue;
95 }
96 char *target_qn = cbm_pipeline_fqn_module(ctx->project_name, imp->module_path);
97 const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, target_qn);
98 free(target_qn);
99 if (!target) {
100 continue;
101 }
102 keys[count] = strdup(imp->local_name);
103 vals[count] = target->qualified_name;
104 count++;
105 }
106
107 *out_keys = keys;
108 *out_vals = vals;
109 *out_count = count;
110 return 0;
111 }
112
113 /* Slow path: scan graph buffer IMPORTS edges + parse JSON properties */
114 char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel_path, "__file__");
115 const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
116 free(file_qn);
117 if (!file_node) {
118 return 0;
119 }
120
121 const cbm_gbuf_edge_t **edges = NULL;
122 int edge_count = 0;
123 int rc = cbm_gbuf_find_edges_by_source_type(ctx->gbuf, file_node->id, "IMPORTS", &edges,
124 &edge_count);
125 if (rc != 0 || edge_count == 0) {
126 return 0;
127 }
128
129 const char **keys = calloc(edge_count, sizeof(const char *));
130 const char **vals = calloc(edge_count, sizeof(const char *));
131 int count = 0;
132
133 for (int i = 0; i < edge_count; i++) {
134 const cbm_gbuf_edge_t *e = edges[i];
135 const cbm_gbuf_node_t *target = cbm_gbuf_find_by_id(ctx->gbuf, e->target_id);

Callers 1

Calls 6

cbm_pipeline_fqn_moduleFunction · 0.85
cbm_gbuf_find_by_qnFunction · 0.85
cbm_pipeline_fqn_computeFunction · 0.85
cbm_gbuf_find_by_idFunction · 0.85
cbm_strndupFunction · 0.85

Tested by

no test coverage detected