Skip to content

Commit 38df8e5

Browse files
committed
fix(swift): scope extension owners by module
Signed-off-by: DavidHLP <[email protected]>
1 parent c652f23 commit 38df8e5

14 files changed

Lines changed: 603 additions & 31 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,61 @@ int cbm_test_enclosing_sweep_mismatches(const CBMFileResult *result) {
36343634
}
36353635
#endif
36363636

3637+
/* Extension visibility uses the file's direct imports, including imports
3638+
* written after a type declaration. The joined text is shared by its types. */
3639+
static void cbm_swift_type_imports(CBMFileResult *result, CBMLanguage language) {
3640+
if (language != CBM_LANG_SWIFT) {
3641+
return;
3642+
}
3643+
size_t length = SKIP_ONE;
3644+
for (int i = 0; i < result->imports.count; i++) {
3645+
const char *path = result->imports.items[i].module_path;
3646+
if (path && path[0]) {
3647+
length += strlen(path) + SKIP_ONE;
3648+
}
3649+
}
3650+
char *imports = cbm_arena_alloc(&result->arena, length);
3651+
if (!imports) {
3652+
return;
3653+
}
3654+
size_t used = 0;
3655+
for (int i = 0; i < result->imports.count; i++) {
3656+
const char *path = result->imports.items[i].module_path;
3657+
if (!path || !path[0]) {
3658+
continue;
3659+
}
3660+
if (used) {
3661+
imports[used++] = '|';
3662+
}
3663+
size_t n = strlen(path);
3664+
memcpy(imports + used, path, n);
3665+
used += n;
3666+
}
3667+
imports[used] = '\0';
3668+
for (int i = 0; i < result->defs.count; i++) {
3669+
CBMDefinition *def = &result->defs.items[i];
3670+
size_t count = 0;
3671+
bool is_type = false;
3672+
for (const char **kv = def->extra_props; kv && kv[0] && kv[SKIP_ONE]; kv += PAIR_LEN) {
3673+
is_type = is_type || strcmp(kv[0], "swift_type_path") == 0 ||
3674+
strcmp(kv[0], "swift_extension") == 0;
3675+
count += PAIR_LEN;
3676+
}
3677+
if (!is_type) {
3678+
continue;
3679+
}
3680+
const char **props =
3681+
cbm_arena_alloc(&result->arena, (count + PAIR_LEN + SKIP_ONE) * sizeof(*props));
3682+
if (props) {
3683+
memcpy(props, def->extra_props, count * sizeof(*props));
3684+
props[count] = "swift_imports";
3685+
props[count + SKIP_ONE] = imports;
3686+
props[count + PAIR_LEN] = NULL;
3687+
def->extra_props = props;
3688+
}
3689+
}
3690+
}
3691+
36373692
static CBMFileResult *extract_file_ex_body(const char *source, int source_len, CBMLanguage language,
36383693
const char *project, const char *rel_path,
36393694
int64_t timeout_micros, const char **extra_defines,
@@ -3901,6 +3956,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
39013956
cbm_extract_imports(&ctx);
39023957
cbm_extract_python_namespace_facts(&ctx);
39033958
cbm_extract_unified(&ctx);
3959+
cbm_swift_type_imports(result, language);
39043960
if (cbm_lang_needs_cpp_branch_views(language)) {
39053961
extract_cpp_branch_views(&ctx, ts_lang, timeout_micros);
39063962
}

‎internal/cbm/extract_imports.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,18 @@ static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type) {
22932293
continue;
22942294
}
22952295

2296+
if (ctx->language == CBM_LANG_SWIFT) {
2297+
/* Attributes and scoped import kinds precede the actual module path. */
2298+
TSNode identifier = cbm_find_child_by_kind(node, "identifier");
2299+
const char *path = ts_node_is_null(identifier)
2300+
? NULL
2301+
: cbm_node_text(ctx->arena, identifier, ctx->source);
2302+
if (path && path[0]) {
2303+
CBMImport imp = {.local_name = path_last(ctx->arena, path), .module_path = path};
2304+
cbm_imports_push(&ctx->result->imports, ctx->arena, imp);
2305+
}
2306+
continue;
2307+
}
22962308
if (!try_generic_path_fields(ctx, node)) {
22972309
generic_import_from_text(ctx, node);
22982310
}

‎src/graph_buffer/graph_buffer.c‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,56 @@ static int gb_qn_claim_rank(const char *label) {
12341234
return (label && strcmp(label, "Macro") == 0) ? 0 : 1;
12351235
}
12361236

1237+
/* A Swift declaration and its same-file extensions share one QN. Keep the
1238+
* declaration's metadata even when a later extension wins the source span. */
1239+
static char *gb_swift_type_props(const char *preferred, const char *other) {
1240+
if (!preferred || !other || !strstr(preferred, "\"swift_") || !strstr(other, "\"swift_")) {
1241+
return NULL;
1242+
}
1243+
yyjson_doc *first = yyjson_read_opts((char *)preferred, strlen(preferred),
1244+
YYJSON_READ_NUMBER_AS_RAW, &gb_yy_alc, NULL);
1245+
yyjson_doc *second =
1246+
yyjson_read_opts((char *)other, strlen(other), YYJSON_READ_NUMBER_AS_RAW, &gb_yy_alc, NULL);
1247+
yyjson_val *left = yyjson_doc_get_root(first);
1248+
yyjson_val *right = yyjson_doc_get_root(second);
1249+
yyjson_val *declaration = yyjson_is_str(yyjson_obj_get(left, "swift_type_path")) ? left : right;
1250+
const char *target = yyjson_get_str(yyjson_obj_get(left, "swift_extension"));
1251+
if (!target) {
1252+
target = yyjson_get_str(yyjson_obj_get(right, "swift_extension"));
1253+
}
1254+
const char *path = yyjson_get_str(yyjson_obj_get(declaration, "swift_type_path"));
1255+
char *merged = NULL;
1256+
if (path && target && strcmp(path, target) == 0) {
1257+
yyjson_mut_doc *doc = yyjson_mut_doc_new(&gb_yy_alc);
1258+
if (doc) {
1259+
yyjson_mut_val *root = yyjson_val_mut_copy(doc, declaration);
1260+
if (root) {
1261+
yyjson_mut_obj_remove_key(root, "swift_extension");
1262+
if (yyjson_mut_obj_add_strcpy(doc, root, "swift_extension", target)) {
1263+
yyjson_mut_doc_set_root(doc, root);
1264+
merged = yyjson_mut_write_opts(doc, 0, &gb_yy_alc, NULL, NULL);
1265+
}
1266+
}
1267+
yyjson_mut_doc_free(doc);
1268+
}
1269+
}
1270+
yyjson_doc_free(second);
1271+
yyjson_doc_free(first);
1272+
return merged;
1273+
}
1274+
1275+
static const char *gb_swift_merge_node_props(cbm_gbuf_node_t *existing, const char *arrival,
1276+
bool arrival_wins) {
1277+
char *merged = arrival_wins ? gb_swift_type_props(arrival, existing->properties_json)
1278+
: gb_swift_type_props(existing->properties_json, arrival);
1279+
if (!merged) {
1280+
return arrival;
1281+
}
1282+
cbm_free(CBM_MEM_CLASS_GBUF_STRING, existing->properties_json);
1283+
existing->properties_json = merged;
1284+
return merged;
1285+
}
1286+
12371287
int64_t cbm_gbuf_upsert_node(cbm_gbuf_t *gb, const char *label, const char *name,
12381288
const char *qualified_name, const char *file_path, int start_line,
12391289
int end_line, const char *properties_json) {
@@ -1295,6 +1345,7 @@ int64_t cbm_gbuf_upsert_node(cbm_gbuf_t *gb, const char *label, const char *name
12951345
}
12961346
gb_def_t arrival = {label, properties_json, file_path, start_line, end_line};
12971347
gb_note_collision(gb, existing, &arrival, NULL);
1348+
properties_json = gb_swift_merge_node_props(existing, properties_json, c <= 0);
12981349
if (c > 0) {
12991350
return existing->id; /* existing entity is the canonical winner */
13001351
}
@@ -2077,6 +2128,7 @@ static void merge_update_existing(cbm_gbuf_t *dst, cbm_gbuf_node_t *existing,
20772128
gb_def_t src_def = {sn->label, sn->properties_json, sn->file_path, sn->start_line,
20782129
sn->end_line};
20792130
gb_note_collision(dst, existing, &src_def, sn_spans);
2131+
const char *properties = gb_swift_merge_node_props(existing, sn->properties_json, sn_wins);
20802132
if (sn_wins) {
20812133
/* src's properties; the variant spans both carry are written on
20822134
* export (cbm_gbuf_settle_variants). */
@@ -2105,8 +2157,7 @@ static void merge_update_existing(cbm_gbuf_t *dst, cbm_gbuf_node_t *existing,
21052157
existing->start_line = sn->start_line;
21062158
existing->end_line = sn->end_line;
21072159
if (sn->properties_json) {
2108-
cbm_free(CBM_MEM_CLASS_GBUF_STRING, existing->properties_json);
2109-
existing->properties_json = heap_strdup(sn->properties_json);
2160+
(void)cbm_gbuf_node_set_properties_json(existing, properties);
21102161
}
21112162
if (label_changed) {
21122163
node_ptr_array_t *by_label = get_or_create_node_array(

‎src/pipeline/pass_definitions.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,8 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
12281228
}
12291229
}
12301230

1231+
cbm_registry_build_swift_owners(ctx->registry, ctx->gbuf, ctx->repo_path);
1232+
12311233
cbm_log_info("pass.done", "pass", "definitions", "defs", itoa_log(total_defs), "calls",
12321234
itoa_log(total_calls), "imports", itoa_log(total_imports), "errors",
12331235
itoa_log(errors));

‎src/pipeline/pass_parallel.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,8 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
20682068
cbm_pipeline_namespace_map_free(cbm_pipeline_get_nsmap());
20692069
cbm_pipeline_set_nsmap(namespace_map);
20702070

2071+
cbm_registry_build_swift_owners(ctx->registry, ctx->gbuf, ctx->repo_path);
2072+
20712073
cbm_log_info("parallel.registry.done", "entries", itoa_log(reg_entries), "defines",
20722074
itoa_log(defines_edges), "imports", itoa_log(imports_edges));
20732075
return 0;

‎src/pipeline/pipeline_delta.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,8 @@ int64_t cbm_delta_preseed(cbm_store_t *store, const char *project, cbm_gbuf_t *g
691691
" OR instr(properties, '\"swift_extension\"') > 0"
692692
" THEN json_object('base_classes', json_extract(properties, '$.base_classes'),"
693693
" 'swift_type_path', json_extract(properties, '$.swift_type_path'),"
694-
" 'swift_extension', json_extract(properties, '$.swift_extension'))"
694+
" 'swift_extension', json_extract(properties, '$.swift_extension'),"
695+
" 'swift_imports', json_extract(properties, '$.swift_imports'))"
695696
" WHEN instr(properties, '\"base_classes\"') > 0"
696697
" THEN json_object('base_classes', json_extract(properties, '$.base_classes')) END,"
697698
" start_line, end_line,"

‎src/pipeline/pipeline_internal.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ bool cbm_pipeline_frozen_manifests_equal(cbm_pipeline_t *p, const cbm_file_hash_
248248
int left_count, const cbm_file_hash_t *right,
249249
int right_count);
250250

251+
/* Freeze Swift declaration/extension ownership after all definitions are registered. */
252+
void cbm_registry_build_swift_owners(cbm_registry_t *registry, const cbm_gbuf_t *graph,
253+
const char *repo_path);
254+
251255
/* Internal explicit-language resolution; public raw/lineage APIs stay raw. */
252256
cbm_resolution_t cbm_registry_resolve_lang(const cbm_registry_t *r, const char *callee_name,
253257
const char *module_qn, const char **import_map_keys,

0 commit comments

Comments
 (0)