Locate the call at `line` (1-based) and emit its guards into `arr`. * Returns false when the file could not be parsed. */
| 228 | /* Locate the call at `line` (1-based) and emit its guards into `arr`. |
| 229 | * Returns false when the file could not be parsed. */ |
| 230 | static bool why_emit_guards(why_cache_t *cache, const char *file_path, int line, |
| 231 | yyjson_mut_doc *doc, yyjson_mut_val *entry) { |
| 232 | why_file_t *file = why_file_get(cache, file_path); |
| 233 | if (!file) |
| 234 | return false; |
| 235 | TSNode root = ts_tree_root_node(file->result->cached_tree); |
| 236 | TSPoint start = {(uint32_t)(line > 0 ? line - 1 : 0), 0}; |
| 237 | TSPoint end = {(uint32_t)(line > 0 ? line - 1 : 0), UINT32_MAX}; |
| 238 | TSNode at = ts_node_descendant_for_point_range(root, start, end); |
| 239 | if (ts_node_is_null(at)) |
| 240 | return false; |
| 241 | |
| 242 | why_guard_t guards[WHY_MAX_GUARDS]; |
| 243 | bool in_loop = false; |
| 244 | int count = why_guards_at(file->source, file->source_len, at, guards, &in_loop); |
| 245 | |
| 246 | yyjson_mut_val *arr = yyjson_mut_arr(doc); |
| 247 | for (int i = count - 1; i >= 0; i--) { /* outermost first */ |
| 248 | yyjson_mut_val *row = yyjson_mut_obj(doc); |
| 249 | yyjson_mut_obj_add_str(doc, row, "kind", guards[i].kind); |
| 250 | if (guards[i].cond[0]) |
| 251 | yyjson_mut_obj_add_strcpy(doc, row, "cond", guards[i].cond); |
| 252 | if (guards[i].negated) |
| 253 | yyjson_mut_obj_add_bool(doc, row, "negated", true); |
| 254 | yyjson_mut_arr_append(arr, row); |
| 255 | } |
| 256 | yyjson_mut_obj_add_val(doc, entry, "guards", arr); |
| 257 | if (in_loop) |
| 258 | yyjson_mut_obj_add_bool(doc, entry, "loop", true); |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | /* ── Shared plumbing ────────────────────────────────────────────── */ |
| 263 |
no test coverage detected