@@ -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+
12371287int64_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 (
0 commit comments