| 3775 | } |
| 3776 | |
| 3777 | static int sh_link(cbm_pipeline_ctx_t *ctx, const sh_hits_t *h) { |
| 3778 | CBMHashTable *files = cbm_ht_create(0); |
| 3779 | CBMHashTable *modules = cbm_ht_create(0); |
| 3780 | if (!files || !modules) { |
| 3781 | if (files) { |
| 3782 | cbm_ht_free(files); |
| 3783 | } |
| 3784 | if (modules) { |
| 3785 | cbm_ht_free(modules); |
| 3786 | } |
| 3787 | return 0; |
| 3788 | } |
| 3789 | for (int i = 0; i < h->count; i++) { |
| 3790 | const sh_hit_t *x = &h->items[i]; |
| 3791 | cbm_ht_set(files, x->file, (void *)x->file); |
| 3792 | if ((x->spawn && x->tgt > 0) || x->build) { |
| 3793 | cbm_ht_set(files, x->path, |
| 3794 | (void *)x->path); /* a spawned script's / Dockerfile's Module */ |
| 3795 | } |
| 3796 | } |
| 3797 | sh_span_t *spans = NULL; |
| 3798 | int n = 0; |
| 3799 | int cap = 0; |
| 3800 | const cbm_gbuf_node_t **nodes = NULL; |
| 3801 | int count = 0; |
| 3802 | if (cbm_gbuf_find_by_label(ctx->gbuf, "Function", &nodes, &count) == 0) { |
| 3803 | for (int i = 0; i < count; i++) { |
| 3804 | const char *fp = nodes[i]->file_path; |
| 3805 | const char *key = fp ? cbm_ht_get_key(files, fp) : NULL; |
| 3806 | if (key && sh_grow((void **)&spans, &cap, n, sizeof(*spans))) { |
| 3807 | spans[n++] = (sh_span_t){key, nodes[i]}; |
| 3808 | } |
| 3809 | } |
| 3810 | } |
| 3811 | if (n > SKIP_ONE) { |
| 3812 | qsort(spans, (size_t)n, sizeof(*spans), sh_cmp_span); |
| 3813 | } |
| 3814 | if (cbm_gbuf_find_by_label(ctx->gbuf, "Module", &nodes, &count) == 0) { |
| 3815 | for (int i = 0; i < count; i++) { |
| 3816 | const char *fp = nodes[i]->file_path; |
| 3817 | const char *key = fp ? cbm_ht_get_key(files, fp) : NULL; |
| 3818 | if (key && !cbm_ht_get(modules, key)) { |
| 3819 | cbm_ht_set(modules, key, (void *)(intptr_t)nodes[i]->id); |
| 3820 | } |
| 3821 | } |
| 3822 | } |
| 3823 | int edges = 0; |
| 3824 | sh_srcs_t twins = {0}; |
| 3825 | for (int i = 0; i < h->count; i++) { |
| 3826 | const sh_hit_t *x = &h->items[i]; |
| 3827 | if (x->build) { |
| 3828 | edges += sh_link_build(ctx, x, modules); |
| 3829 | continue; |
| 3830 | } |
| 3831 | twins.count = 0; |
| 3832 | int64_t one = sh_source(ctx, spans, n, modules, x->file, x->line, &twins); |
| 3833 | for (int k = 0; k < (twins.count ? twins.count : SKIP_ONE); k++) { |
| 3834 | edges += sh_link_hit(ctx, x, twins.count ? twins.items[k]->id : one, modules); |
no test coverage detected