Attach per-hop guard chains to a trace JSON (guards=1): for each * consecutive path pair, find the CALLS edge's call-site line and run the * on-demand guard extraction in the caller's file. Best-effort — hops * without a resolvable site simply get no guards array. */
| 2504 | * on-demand guard extraction in the caller's file. Best-effort — hops |
| 2505 | * without a resolvable site simply get no guards array. */ |
| 2506 | static char *atlas_trace_attach_guards(cbm_store_t *store, const char *project, char *json) { |
| 2507 | yyjson_doc *doc = yyjson_read(json, strlen(json), 0); |
| 2508 | if (!doc) |
| 2509 | return json; |
| 2510 | yyjson_val *path = yyjson_obj_get(yyjson_doc_get_root(doc), "path"); |
| 2511 | size_t hops = path ? yyjson_arr_size(path) : 0; |
| 2512 | if (hops < 2) { |
| 2513 | yyjson_doc_free(doc); |
| 2514 | return json; |
| 2515 | } |
| 2516 | yyjson_mut_doc *mut = yyjson_doc_mut_copy(doc, cbm_json_writer_allocator()); |
| 2517 | yyjson_doc_free(doc); |
| 2518 | if (!mut) |
| 2519 | return json; |
| 2520 | yyjson_mut_val *mpath = yyjson_mut_obj_get(yyjson_mut_doc_get_root(mut), "path"); |
| 2521 | struct sqlite3 *db = cbm_store_get_db(store); |
| 2522 | for (size_t i = 0; i + 1 < hops && db; i++) { |
| 2523 | yyjson_mut_val *from = yyjson_mut_arr_get(mpath, i); |
| 2524 | yyjson_mut_val *to = yyjson_mut_arr_get(mpath, i + 1); |
| 2525 | int64_t from_id = yyjson_mut_get_int(yyjson_mut_obj_get(from, "id")); |
| 2526 | int64_t to_id = yyjson_mut_get_int(yyjson_mut_obj_get(to, "id")); |
| 2527 | const char *from_file = yyjson_mut_get_str(yyjson_mut_obj_get(from, "file_path")); |
| 2528 | if (!from_file) |
| 2529 | continue; |
| 2530 | sqlite3_stmt *st = NULL; |
| 2531 | int line = -1; |
| 2532 | if (sqlite3_prepare_v2(db, |
| 2533 | "SELECT properties FROM edges WHERE project=?1 AND " |
| 2534 | "source_id=?2 AND target_id=?3 AND type='CALLS' LIMIT 1", |
| 2535 | -1, &st, NULL) == SQLITE_OK) { |
| 2536 | sqlite3_bind_text(st, 1, project, -1, SQLITE_STATIC); |
| 2537 | sqlite3_bind_int64(st, 2, from_id); |
| 2538 | sqlite3_bind_int64(st, 3, to_id); |
| 2539 | if (sqlite3_step(st) == SQLITE_ROW) { |
| 2540 | const char *props = (const char *)sqlite3_column_text(st, 0); |
| 2541 | if (props) { |
| 2542 | yyjson_doc *pd = yyjson_read(props, strlen(props), 0); |
| 2543 | if (pd) { |
| 2544 | line = (int)yyjson_get_int(yyjson_obj_get(yyjson_doc_get_root(pd), "line")); |
| 2545 | yyjson_doc_free(pd); |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | sqlite3_finalize(st); |
| 2550 | } |
| 2551 | if (line <= 0) |
| 2552 | continue; |
| 2553 | char *gj = cbm_atlas_callsite_guards_json(store, project, from_file, line); |
| 2554 | if (!gj) |
| 2555 | continue; |
| 2556 | yyjson_doc *gd = yyjson_read(gj, strlen(gj), 0); |
| 2557 | cbm_free(CBM_MEM_CLASS_OTHER, gj); |
| 2558 | if (!gd) |
| 2559 | continue; |
| 2560 | yyjson_val *garr = yyjson_obj_get(yyjson_doc_get_root(gd), "guards"); |
| 2561 | if (garr) { |
| 2562 | yyjson_mut_val *copy = yyjson_val_mut_copy(mut, garr); |
| 2563 | if (copy) |
no test coverage detected