Skip to content

Commit a7f24ea

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/php-global-use-known-absent
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2 parents 7cb58d3 + 658639e commit a7f24ea

6 files changed

Lines changed: 337 additions & 72 deletions

File tree

‎internal/cbm/extract_unified.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,20 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
11761176
if (!name || !name[0]) {
11771177
return NULL;
11781178
}
1179+
/* Rust: the impl's type text carries its generic arguments (`impl<T> Gen<T>`,
1180+
* `impl<'a> Parser<'a>`), which the def side strips before naming the type
1181+
* (extract_defs.c extract_rust_impl: `Buffer<T>` -> `Buffer`). Strip them the
1182+
* same way, or every method of a generic impl gets a scope QN no node carries
1183+
* and all its calls fall back to the File node. */
1184+
if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
1185+
char *lt = strchr(name, '<');
1186+
if (lt == name) {
1187+
return NULL; /* `<T as Tr>`-style self type: no name to scope by */
1188+
}
1189+
if (lt) {
1190+
*lt = '\0';
1191+
}
1192+
}
11791193

11801194
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
11811195
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ static void rust_strip_turbofish(char *path) {
306306
*write = '\0';
307307
}
308308

309+
/* In place: `Gen<T>` -> `Gen`, `foo::Bar<'a, T>` -> `foo::Bar` (#763). An
310+
* impl's self type keeps its generic arguments in the source text, but every
311+
* def-side QN (a method's receiver, the extractor's call scope) strips them.
312+
* Unstripped, the LSP's caller QN, `self`/`Self` dispatch and the return-type
313+
* harvest never matched a generic impl's methods. Text starting with `<` (a
314+
* qualified path) is left alone. Safe on NULL. */
315+
static void rust_strip_impl_type_args(char *type_text) {
316+
if (!type_text) {
317+
return;
318+
}
319+
char *lt = strchr(type_text, '<');
320+
if (lt && lt > type_text) {
321+
*lt = '\0';
322+
}
323+
}
324+
309325
/* Look up a `use` alias and return its fully-qualified module path,
310326
* or NULL if absent. The returned pointer aliases into the use map. */
311327
static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name) {
@@ -5274,6 +5290,7 @@ static void rust_process_impl(RustLSPContext *ctx, TSNode impl_node) {
52745290
char *type_text = rust_node_text(ctx, type_node);
52755291
if (!type_text)
52765292
return;
5293+
rust_strip_impl_type_args(type_text);
52775294

52785295
/* Detect blanket impl: `impl<T: Trait> ForeignTrait for T { ... }`
52795296
* where type_text is a name that appears in the impl's type
@@ -6028,6 +6045,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
60286045
if (ts_node_is_null(type_node) || ts_node_is_null(body))
60296046
continue;
60306047
char *type_name = cbm_node_text(arena, type_node, source);
6048+
rust_strip_impl_type_args(type_name);
60316049
if (!type_name || !type_name[0])
60326050
continue;
60336051
const char *type_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, type_name);

‎src/pipeline/pass_route_nodes.c‎

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,6 @@ static void finish_data_flow_props(char *props, size_t propsz, size_t pos,
677677
}
678678
}
679679

680-
typedef struct {
681-
int64_t source_id;
682-
const char *props;
683-
const char *edge_type;
684-
} caller_edge_ref_t;
685-
686680
static bool http_call_edge_has_valid_route(const cbm_gbuf_edge_t *edge) {
687681
char url_buf[CBM_SZ_512];
688682
const char *url = json_extract(edge->properties_json, "url_path", url_buf, sizeof(url_buf));
@@ -747,84 +741,82 @@ static int try_create_data_flow(cbm_gbuf_t *gb, int64_t caller_id, int64_t handl
747741
return SKIP_ONE;
748742
}
749743

750-
/* Collect extra handler IDs reachable via INFRA_MAPS chain from a route. */
751-
static int collect_infra_handlers(cbm_gbuf_t *gb, int64_t route_id, int64_t *out, int max_out) {
752-
const cbm_gbuf_edge_t **infra_edges = NULL;
753-
int infra_count = 0;
754-
cbm_gbuf_find_edges_by_source_type(gb, route_id, "INFRA_MAPS", &infra_edges, &infra_count);
744+
/* One route's DATA_FLOWS context: its direct handlers (handler -[HANDLES]-> route) and its
745+
* INFRA_MAPS hops (route -[INFRA_MAPS]-> endpoint, whose handlers are reached via the endpoint).
746+
* The edge arrays are the graph buffer's own index arrays. Inserting DATA_FLOWS never appends to
747+
* a HANDLES, INFRA_MAPS, HTTP_CALLS or ASYNC_CALLS array, so they stay valid while flows are added
748+
* (only edge deletion rebuilds the indexes). */
749+
typedef struct {
750+
cbm_gbuf_t *gb;
751+
const cbm_gbuf_node_t *route;
752+
const cbm_gbuf_edge_t **handles;
753+
int handles_count;
754+
const cbm_gbuf_edge_t **infra;
755+
int infra_count;
756+
int flows;
757+
int skipped;
758+
} route_flows_t;
759+
760+
static void route_flow_add(route_flows_t *rf, const cbm_gbuf_edge_t *caller, int64_t handler_id,
761+
const char *edge_type, bool via_infra) {
762+
int rc = try_create_data_flow(rf->gb, caller->source_id, handler_id, rf->route, edge_type,
763+
caller->properties_json, via_infra);
764+
if (rc > 0) {
765+
rf->flows++;
766+
} else if (rc < 0) {
767+
rf->skipped++;
768+
}
769+
}
755770

756-
int n = 0;
757-
for (int ie = 0; ie < infra_count; ie++) {
771+
/* DATA_FLOWS from one caller edge to every handler of the route, direct and via infra. */
772+
static void caller_data_flows(route_flows_t *rf, const cbm_gbuf_edge_t *caller,
773+
const char *edge_type) {
774+
for (int hi = 0; hi < rf->handles_count; hi++) {
775+
route_flow_add(rf, caller, rf->handles[hi]->source_id, edge_type, false);
776+
}
777+
for (int ie = 0; ie < rf->infra_count; ie++) {
758778
const cbm_gbuf_edge_t **ep_handles = NULL;
759779
int ep_hcount = 0;
760-
cbm_gbuf_find_edges_by_target_type(gb, infra_edges[ie]->target_id, "HANDLES", &ep_handles,
780+
cbm_gbuf_find_edges_by_target_type(rf->gb, rf->infra[ie]->target_id, "HANDLES", &ep_handles,
761781
&ep_hcount);
762-
for (int eh = 0; eh < ep_hcount && n < max_out; eh++) {
763-
out[n++] = ep_handles[eh]->source_id;
782+
for (int eh = 0; eh < ep_hcount; eh++) {
783+
route_flow_add(rf, caller, ep_handles[eh]->source_id, edge_type, true);
764784
}
765785
}
766-
return n;
767786
}
768787

769-
/* Collect caller edges (HTTP_CALLS + ASYNC_CALLS) targeting a route. */
770-
static int collect_caller_edges(cbm_gbuf_t *gb, int64_t route_id, caller_edge_ref_t *out,
771-
int max_out) {
772-
int n = 0;
788+
/* Create DATA_FLOWS between EVERY caller (HTTP_CALLS + ASYNC_CALLS) and EVERY handler of one
789+
* route node.
790+
*
791+
* Callers were copied into a 64-slot buffer and infra handlers into a 32-slot one, each keeping the
792+
* first entries in edge-insertion order -- i.e. in parallel worker interleaving. Which DATA_FLOWS
793+
* existed therefore changed between two indexes of the same tree with the same binary
794+
* (thingsboard, 2026-10-17), and every caller past the 64th lost its flow outright. The flow set
795+
* is now the full caller x handler product. A (caller, handler) pair reached twice -- through two
796+
* routes, or directly and via infra -- keeps the order-free property winner of
797+
* cbm_gbuf_insert_edge. */
798+
static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route, int *flows,
799+
int *skipped) {
800+
route_flows_t rf = {.gb = gb, .route = route};
801+
cbm_gbuf_find_edges_by_target_type(gb, route->id, "HANDLES", &rf.handles, &rf.handles_count);
802+
cbm_gbuf_find_edges_by_source_type(gb, route->id, "INFRA_MAPS", &rf.infra, &rf.infra_count);
803+
773804
const cbm_gbuf_edge_t **http_edges = NULL;
774805
int http_count = 0;
775-
cbm_gbuf_find_edges_by_target_type(gb, route_id, "HTTP_CALLS", &http_edges, &http_count);
776-
for (int i = 0; i < http_count && n < max_out; i++) {
777-
if (!http_call_edge_has_valid_route(http_edges[i])) {
778-
continue;
806+
cbm_gbuf_find_edges_by_target_type(gb, route->id, "HTTP_CALLS", &http_edges, &http_count);
807+
for (int i = 0; i < http_count; i++) {
808+
if (http_call_edge_has_valid_route(http_edges[i])) {
809+
caller_data_flows(&rf, http_edges[i], "HTTP_CALLS");
779810
}
780-
out[n].source_id = http_edges[i]->source_id;
781-
out[n].props = http_edges[i]->properties_json;
782-
out[n].edge_type = "HTTP_CALLS";
783-
n++;
784811
}
785812
const cbm_gbuf_edge_t **async_edges = NULL;
786813
int async_count = 0;
787-
cbm_gbuf_find_edges_by_target_type(gb, route_id, "ASYNC_CALLS", &async_edges, &async_count);
788-
for (int i = 0; i < async_count && n < max_out; i++) {
789-
out[n].source_id = async_edges[i]->source_id;
790-
out[n].props = async_edges[i]->properties_json;
791-
out[n].edge_type = "ASYNC_CALLS";
792-
n++;
793-
}
794-
return n;
795-
}
796-
797-
/* Create DATA_FLOWS between callers and handlers for one route node. */
798-
static void create_route_data_flows(cbm_gbuf_t *gb, const cbm_gbuf_node_t *route,
799-
const caller_edge_ref_t *callers, int n_callers, int *flows,
800-
int *skipped) {
801-
const cbm_gbuf_edge_t **handles_edges = NULL;
802-
int handles_count = 0;
803-
cbm_gbuf_find_edges_by_target_type(gb, route->id, "HANDLES", &handles_edges, &handles_count);
804-
805-
int64_t extra_handlers[CBM_SZ_32];
806-
int n_extra = collect_infra_handlers(gb, route->id, extra_handlers, CBM_SZ_32);
807-
808-
for (int ci = 0; ci < n_callers; ci++) {
809-
for (int hi = 0; hi < handles_count; hi++) {
810-
int rc = try_create_data_flow(gb, callers[ci].source_id, handles_edges[hi]->source_id,
811-
route, callers[ci].edge_type, callers[ci].props, false);
812-
if (rc > 0) {
813-
(*flows)++;
814-
} else if (rc < 0) {
815-
(*skipped)++;
816-
}
817-
}
818-
for (int xi = 0; xi < n_extra; xi++) {
819-
int rc = try_create_data_flow(gb, callers[ci].source_id, extra_handlers[xi], route,
820-
callers[ci].edge_type, callers[ci].props, true);
821-
if (rc > 0) {
822-
(*flows)++;
823-
} else if (rc < 0) {
824-
(*skipped)++;
825-
}
826-
}
814+
cbm_gbuf_find_edges_by_target_type(gb, route->id, "ASYNC_CALLS", &async_edges, &async_count);
815+
for (int i = 0; i < async_count; i++) {
816+
caller_data_flows(&rf, async_edges[i], "ASYNC_CALLS");
827817
}
818+
*flows += rf.flows;
819+
*skipped += rf.skipped;
828820
}
829821

830822
/* Find the enclosing proto service for a function by file + line range. */
@@ -931,9 +923,7 @@ static void create_data_flows(cbm_gbuf_t *gb) {
931923
int skipped = 0;
932924

933925
for (int ri = 0; ri < route_count; ri++) {
934-
caller_edge_ref_t caller_edges[CBM_SZ_64];
935-
int n_callers = collect_caller_edges(gb, routes[ri]->id, caller_edges, CBM_SZ_64);
936-
create_route_data_flows(gb, routes[ri], caller_edges, n_callers, &flows, &skipped);
926+
create_route_data_flows(gb, routes[ri], &flows, &skipped);
937927
}
938928

939929
if (flows > 0 || skipped > 0) {

‎tests/test_extraction.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7178,6 +7178,63 @@ TEST(extract_perl_method_call_flags_is_method) {
71787178
PASS();
71797179
}
71807180

7181+
/* The def QN of `name` and the enclosing-function QN of the call `callee` inside
7182+
* it, from one extraction result. */
7183+
static void rust_def_and_scope(const CBMFileResult *r, const char *name, const char *callee,
7184+
const char **def_qn, const char **scope_qn) {
7185+
*def_qn = NULL;
7186+
*scope_qn = NULL;
7187+
for (int i = 0; i < r->defs.count; i++) {
7188+
const CBMDefinition *d = &r->defs.items[i];
7189+
if (d->name && d->qualified_name && strcmp(d->name, name) == 0) {
7190+
*def_qn = d->qualified_name;
7191+
}
7192+
}
7193+
for (int i = 0; i < r->calls.count; i++) {
7194+
const CBMCall *c = &r->calls.items[i];
7195+
if (c->callee_name && strcmp(c->callee_name, callee) == 0 && c->enclosing_func_qn &&
7196+
strstr(c->enclosing_func_qn, name)) {
7197+
*scope_qn = c->enclosing_func_qn;
7198+
}
7199+
}
7200+
}
7201+
7202+
/* F-R4: a method of a generic impl (`impl<T> Gen<T>`, `impl<'a> Life<'a>`) got a
7203+
* call scope that kept the generic arguments ("...Gen<T>.in_generic") while the
7204+
* def side strips them ("...Gen.in_generic"), so no node carried the scope and
7205+
* every call in the body fell back to the File node (base census: 84,331 Rust
7206+
* CALLS sourced from a File although inside a function, 96% of all File-sourced
7207+
* Rust CALLS). The def QN and the call scope must agree. */
7208+
TEST(extract_rust_generic_impl_call_scope_matches_def) {
7209+
CBMFileResult *r = extract("pub struct Plain;\n"
7210+
"impl Plain {\n"
7211+
" pub fn in_plain(&self) -> u32 { helper() }\n"
7212+
"}\n"
7213+
"pub struct Gen<T> { v: T }\n"
7214+
"impl<T> Gen<T> {\n"
7215+
" pub fn in_generic(&self) -> u32 { helper() }\n"
7216+
"}\n"
7217+
"pub struct Life<'a> { s: &'a str }\n"
7218+
"impl<'a> Life<'a> {\n"
7219+
" pub fn in_lifetime(&self) -> u32 { helper() }\n"
7220+
"}\n"
7221+
"pub fn helper() -> u32 { 1 }\n",
7222+
CBM_LANG_RUST, "t", "src/lib.rs");
7223+
ASSERT_NOT_NULL(r);
7224+
ASSERT_FALSE(r->has_error);
7225+
static const char *const methods[] = {"in_plain", "in_generic", "in_lifetime", NULL};
7226+
for (int m = 0; methods[m]; m++) {
7227+
const char *def_qn = NULL;
7228+
const char *scope_qn = NULL;
7229+
rust_def_and_scope(r, methods[m], "helper", &def_qn, &scope_qn);
7230+
ASSERT_NOT_NULL(def_qn);
7231+
ASSERT_NOT_NULL(scope_qn);
7232+
ASSERT_STR_EQ(scope_qn, def_qn);
7233+
}
7234+
cbm_free_result(r);
7235+
PASS();
7236+
}
7237+
71817238
/* Languages OUTSIDE the is_method flag set (only Perl and TS/JS/TSX set it) must
71827239
* be unaffected: a Go method call never sets is_method. */
71837240
TEST(extract_flag_exempt_method_call_not_flagged_is_method) {
@@ -12512,6 +12569,7 @@ SUITE(extraction) {
1251212569
RUN_TEST(extract_perl_config_string_not_a_callee);
1251312570
RUN_TEST(extract_perl_builtin_call_is_function_not_method);
1251412571
RUN_TEST(extract_perl_method_call_flags_is_method);
12572+
RUN_TEST(extract_rust_generic_impl_call_scope_matches_def);
1251512573
RUN_TEST(extract_flag_exempt_method_call_not_flagged_is_method);
1251612574
RUN_TEST(extract_python_member_call_flags_is_method);
1251712575
RUN_TEST(extract_python_bare_call_flags_locally_bound_callee);

0 commit comments

Comments
 (0)