Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(lsp): join type-aware method calls (C++ ::/->, Go receiver QN, templates, ctor)

Martin Vogelfc1b6cc72d89
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 7fb7ad3e0..369453898 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -258,6 +258,18 @@ static char *extract_callee_from_fields(CBMArena *a, TSNode node, const char *so
strcmp(fk, "value_identifier") == 0 || strcmp(fk, "value_identifier_path") == 0) {
return cbm_node_text(a, func_node, source);
}
+ // C++ explicit template call f<T>(args): the `function` field is a
+ // template_function whose `name` child is the bare callee (identifier
+ // "identity" or qualified_identifier "ns::f"). Without this the whole
+ // "identity<int>" text would never be produced as a textual callee, so
+ // no CALLS edge — and the LSP's lsp_template resolution has nothing to
+ // attach to. Return the name child so the join recovers the bare method.
+ if (strcmp(fk, "template_function") == 0) {
+ TSNode tname = ts_node_child_by_field_name(func_node, TS_FIELD("name"));
+ if (!ts_node_is_null(tname)) {
+ return cbm_node_text(a, tname, source);
+ }
+ }
// R member call: module$fn() — function node is an extract_operator
// with lhs (object) and rhs (method). Emit "module.fn" so it resolves
// like other member calls (#219). Previously dropped → no CALLS edge.
diff --git a/internal/cbm/lsp/c_lsp.c b/internal/cbm/lsp/c_lsp.c
index 41dcdff4b..e2c1c4d53 100644
--- a/internal/cbm/lsp/c_lsp.c
+++ b/internal/cbm/lsp/c_lsp.c
@@ -3741,8 +3741,22 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
if (type_qn) {
const char *short_name = strrchr(type_qn, '.');
short_name = short_name ? short_name + 1 : type_qn;
+ const char *last_colon = strrchr(type_qn, ':');
+ if (last_colon)
+ short_name = last_colon + 1;
const char *ctor_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", type_qn, short_name);
- c_emit_resolved_call(ctx, ctor_qn, "lsp_constructor", 0.95f);
+ /* Point at the synthesized `Type.Type` ctor node only if it
+ * actually exists in the registry (explicit constructor). When
+ * the class has no user-declared constructor there is no such
+ * node, so resolve `new Foo()` to the Foo CLASS node (type_qn)
+ * instead — its short name equals the textual callee ("Foo"),
+ * so the pipeline join matches and a CALLS edge forms. Mirrors
+ * java_lsp.c object_creation_expression. */
+ if (cbm_registry_lookup_method(ctx->registry, type_qn, short_name)) {
+ c_emit_resolved_call(ctx, ctor_qn, "lsp_constructor", 0.95f);
+ } else {
+ c_emit_resolved_call(ctx, type_qn, "lsp_constructor", 0.95f);
+ }
}
}
}
diff --git a/internal/cbm/lsp/go_lsp.c b/internal/cbm/lsp/go_lsp.c
index af3e1c61a..c5ca2513a 100644
--- a/internal/cbm/lsp/go_lsp.c
+++ b/internal/cbm/lsp/go_lsp.c
@@ -1481,7 +1481,42 @@ static void process_function(GoLSPContext* ctx, TSNode func_node) {
char* func_name = lsp_node_text(ctx, name_node);
if (!func_name || !func_name[0]) return;
- ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->package_qn, func_name);
+ // For methods, the enclosing-function QN must include the receiver type
+ // (package.Type.Method), matching how the textual extractor and the
+ // registry qualify the method. Building it as package.Method (no receiver)
+ // here made the LSP-resolved call's caller_qn disagree with the textual
+ // call's enclosing_func_qn, so cbm_pipeline_find_lsp_resolution never
+ // joined them — every call inside a method body silently lost its
+ // type-aware LSP strategy. Derive the bare receiver type name the same way
+ // the receiver binding below does.
+ char* recv_type_name = NULL;
+ {
+ TSNode recv0 = ts_node_child_by_field_name(func_node, "receiver", 8);
+ if (!ts_node_is_null(recv0)) {
+ uint32_t rnc0 = ts_node_child_count(recv0);
+ for (uint32_t i = 0; i < rnc0 && !recv_type_name; i++) {
+ TSNode rp = ts_node_child(recv0, i);
+ if (ts_node_is_null(rp) || !ts_node_is_named(rp)) continue;
+ if (strcmp(ts_node_type(rp), "parameter_declaration") != 0) continue;
+ TSNode rtype = ts_node_child_by_field_name(rp, "type", 4);
+ if (ts_node_is_null(rtype)) continue;
+ // Unwrap a pointer receiver (*Type) to the bare type identifier.
+ const char* rtk = ts_node_type(rtype);
+ if (strcmp(rtk, "pointer_type") == 0 && ts_node_named_child_count(rtype) > 0) {
+ rtype = ts_node_named_child(rtype, 0);
+ }
+ char* tn = lsp_node_text(ctx, rtype);
+ if (tn && tn[0]) recv_type_name = tn;
+ }
+ }
+ }
+
+ if (recv_type_name) {
+ ctx->enclosing_func_qn =
+ cbm_arena_sprintf(ctx->arena, "%s.%s.%s", ctx->package_qn, recv_type_name, func_name);
+ } else {
+ ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->package_qn, func_name);
+ }
// Push function scope
CBMScope* saved_scope = ctx->current_scope;
diff --git a/src/pipeline/lsp_resolve.h b/src/pipeline/lsp_resolve.h
index 6eba980d0..28dcb9474 100644
--- a/src/pipeline/lsp_resolve.h
+++ b/src/pipeline/lsp_resolve.h
@@ -35,6 +35,35 @@
* (Go, C/C++, Python, PHP). */
#define CBM_LSP_CONFIDENCE_FLOOR 0.6f
+/* Bare last segment of a (possibly qualified) name, splitting on the LAST
+ * member/scope separator. C++ textual callees carry `::` (Class::method,
+ * Ns::f) and `->` (p->run), while the LSP records dotted internal QNs
+ * (Class.method). Splitting only on '.' (strrchr) leaves `Math::square`
+ * and `p->run` intact, so they never match the LSP's `square`/`run` short
+ * name and the type-aware strategy is silently dropped to the textual
+ * registry. Treat '.', ':' and '>' as terminal separators so the bare
+ * method name is recovered on BOTH the QN side (dotted, occasionally `::`
+ * for template/alias scopes) and the textual side (`.`/`::`/`->`). Other
+ * languages' callee names contain none of `::`/`->`, so this is a no-op
+ * for them. */
+static inline const char *cbm_lsp_bare_segment(const char *name) {
+ if (!name) {
+ return name;
+ }
+ const char *seg = name;
+ for (const char *p = name; *p; p++) {
+ /* '.' (dotted QN / Java-style member) and ':' (C++ `::`, last colon
+ * wins) are member/scope separators. '>' is only a separator when it
+ * closes the `->` arrow (preceded by '-'); a bare '>' closes a template
+ * argument list ("identity<int>") and must NOT split, else the segment
+ * would be the empty string after the trailing '>'. */
+ if (*p == '.' || *p == ':' || (*p == '>' && p != name && p[-1] == '-')) {
+ seg = p + SKIP_ONE;
+ }
+ }
+ return seg;
+}
+
/* Look up the highest-confidence LSP-resolved call entry whose caller QN
* matches the textual call's enclosing function and whose callee QN
* short-name matches the textual callee. Returns a pointer into `arr`
@@ -65,18 +94,16 @@ static inline const CBMResolvedCall *cbm_pipeline_find_lsp_resolution(
if (strcmp(rc->caller_qn, call->enclosing_func_qn) != 0) {
continue;
}
- const char *short_name = strrchr(rc->callee_qn, '.');
- short_name = short_name ? short_name + SKIP_ONE : rc->callee_qn;
+ const char *short_name = cbm_lsp_bare_segment(rc->callee_qn);
/* The call's callee_name is receiver-qualified for method/qualified
- * calls ("c.inc", "A.Helper"); the LSP records the resolved
- * class-qualified callee_qn ("Class.inc"). Compare the bare last
- * segment on BOTH sides so method-dispatch resolutions join — the LSP
- * already did the receiver->type resolution, and matching the full
- * "c.inc" against "inc" would always miss, silently dropping the
+ * calls ("c.inc", "A.Helper", "Math::square", "p->run"); the LSP
+ * records the resolved class-qualified callee_qn ("Class.inc"). Compare
+ * the bare last segment on BOTH sides so method-dispatch resolutions
+ * join — the LSP already did the receiver->type resolution, and matching
+ * the full "c.inc" against "inc" would always miss, silently dropping the
* type-aware LSP strategy to the weaker textual registry. Free-function
* calls (bare callee_name) are unaffected. */
- const char *call_short = strrchr(call->callee_name, '.');
- call_short = call_short ? call_short + SKIP_ONE : call->callee_name;
+ const char *call_short = cbm_lsp_bare_segment(call->callee_name);
if (strcmp(short_name, call_short) != 0) {
continue;
}