| 108 | } |
| 109 | |
| 110 | static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path, |
| 111 | const CBMFileResult *result, const char ***out_keys, |
| 112 | const char ***out_vals, int *out_count) { |
| 113 | *out_keys = NULL; |
| 114 | *out_vals = NULL; |
| 115 | *out_count = 0; |
| 116 | |
| 117 | /* Fast path: build from cached extraction result (no JSON parsing) */ |
| 118 | if (result && result->imports.count > 0) { |
| 119 | const char **keys = calloc((size_t)result->imports.count, sizeof(const char *)); |
| 120 | const char **vals = calloc((size_t)result->imports.count, sizeof(const char *)); |
| 121 | int count = 0; |
| 122 | |
| 123 | for (int i = 0; i < result->imports.count; i++) { |
| 124 | const CBMImport *imp = &result->imports.items[i]; |
| 125 | if (!imp->local_name || !imp->local_name[0] || !imp->module_path) { |
| 126 | continue; |
| 127 | } |
| 128 | char *target_qn = cbm_pipeline_fqn_module(ctx->project_name, imp->module_path); |
| 129 | const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, target_qn); |
| 130 | free(target_qn); |
| 131 | if (!target) { |
| 132 | continue; |
| 133 | } |
| 134 | keys[count] = strdup(imp->local_name); |
| 135 | vals[count] = target->qualified_name; /* borrowed from gbuf */ |
| 136 | count++; |
| 137 | } |
| 138 | |
| 139 | *out_keys = keys; |
| 140 | *out_vals = vals; |
| 141 | *out_count = count; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* Slow path: scan graph buffer IMPORTS edges + parse JSON properties */ |
| 146 | char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel_path, "__file__"); |
| 147 | const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn); |
| 148 | free(file_qn); |
| 149 | if (!file_node) { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | const cbm_gbuf_edge_t **edges = NULL; |
| 154 | int edge_count = 0; |
| 155 | int rc = cbm_gbuf_find_edges_by_source_type(ctx->gbuf, file_node->id, "IMPORTS", &edges, |
| 156 | &edge_count); |
| 157 | if (rc != 0 || edge_count == 0) { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | const char **keys = calloc(edge_count, sizeof(const char *)); |
| 162 | const char **vals = calloc(edge_count, sizeof(const char *)); |
| 163 | int count = 0; |
| 164 | |
| 165 | for (int i = 0; i < edge_count; i++) { |
| 166 | const cbm_gbuf_edge_t *e = edges[i]; |
| 167 | const cbm_gbuf_node_t *target = cbm_gbuf_find_by_id(ctx->gbuf, e->target_id); |
no test coverage detected