Write a node's gathered spans into its properties: sorted, distinct, the * list left out only when it would say no more than the node's own span. */
| 1103 | /* Write a node's gathered spans into its properties: sorted, distinct, the |
| 1104 | * list left out only when it would say no more than the node's own span. */ |
| 1105 | static void gb_write_variants(cbm_gbuf_node_t *node, gb_span_vec_t *vec) { |
| 1106 | qsort(vec->items, vec->count, sizeof(vec->items[0]), gb_span_cmp); |
| 1107 | size_t unique = 0; |
| 1108 | for (size_t i = 0; i < vec->count; i++) { |
| 1109 | if (unique == 0 || gb_span_cmp(&vec->items[unique - SKIP_ONE], &vec->items[i]) != 0) { |
| 1110 | vec->items[unique++] = vec->items[i]; |
| 1111 | } |
| 1112 | } |
| 1113 | gb_span_t own = {node->file_path ? node->file_path : "", node->start_line, node->end_line}; |
| 1114 | bool own_is_span = gb_variant_label(node->label) && node->start_line > 0; |
| 1115 | bool only_own = |
| 1116 | own_is_span ? unique == SKIP_ONE && gb_span_cmp(&vec->items[0], &own) == 0 : unique == 0; |
| 1117 | if (only_own && !gb_props_have_variants(node->properties_json)) { |
| 1118 | return; |
| 1119 | } |
| 1120 | /* Numbers stay as written: a pass printed them (importance 0.000000). */ |
| 1121 | const char *props = node->properties_json; |
| 1122 | yyjson_doc *doc = props ? yyjson_read_opts((char *)props, strlen(props), |
| 1123 | YYJSON_READ_NUMBER_AS_RAW, &gb_yy_alc, NULL) |
| 1124 | : NULL; |
| 1125 | yyjson_mut_doc *mut = yyjson_mut_doc_new(&gb_yy_alc); |
| 1126 | yyjson_mut_val *root = NULL; |
| 1127 | if (mut) { |
| 1128 | root = doc && yyjson_is_obj(yyjson_doc_get_root(doc)) |
| 1129 | ? yyjson_val_mut_copy(mut, yyjson_doc_get_root(doc)) |
| 1130 | : yyjson_mut_obj(mut); |
| 1131 | } |
| 1132 | yyjson_mut_val *list = root && !only_own ? yyjson_mut_arr(mut) : NULL; |
| 1133 | for (size_t i = 0; list && i < unique; i++) { |
| 1134 | yyjson_mut_val *v = yyjson_mut_arr_add_obj(mut, list); |
| 1135 | yyjson_mut_obj_add_strcpy(mut, v, "file_path", vec->items[i].file); |
| 1136 | yyjson_mut_obj_add_int(mut, v, "start_line", vec->items[i].start); |
| 1137 | yyjson_mut_obj_add_int(mut, v, "end_line", vec->items[i].end); |
| 1138 | } |
| 1139 | if (root && (only_own || list)) { |
| 1140 | yyjson_mut_obj_remove_key(root, "variants"); |
| 1141 | if (list) { |
| 1142 | yyjson_mut_obj_add_val(mut, root, "variants", list); |
| 1143 | } |
| 1144 | yyjson_mut_doc_set_root(mut, root); |
| 1145 | char *out = yyjson_mut_write_opts(mut, 0, &gb_yy_alc, NULL, NULL); |
| 1146 | if (out) { |
| 1147 | cbm_free(CBM_MEM_CLASS_GBUF_STRING, node->properties_json); |
| 1148 | node->properties_json = out; |
| 1149 | } |
| 1150 | } |
| 1151 | yyjson_mut_doc_free(mut); |
| 1152 | yyjson_doc_free(doc); |
| 1153 | } |
| 1154 | |
| 1155 | static void gb_settle_one(const char *qn, void *value, void *ud) { |
| 1156 | cbm_gbuf_t *gb = ud; |
no test coverage detected