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(cpp): resolve implicit destructor, copy-ctor, and conversion-operator calls

Martin Vogel5b60045c5510
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 278479f65..6d841960b 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -1739,6 +1739,73 @@ static void extract_cpp_operator_call(CBMExtractCtx *ctx, TSNode node, const cha
}
}
+// C++ implicit calls that produce no textual call node: the destructor
+// (`delete p`), the copy/move constructor (`T a = b;` copy-init), and the
+// conversion operator (`if (obj)` where obj has `operator bool`). The c-LSP
+// resolves each to the corresponding member but there is no call site to join
+// to (callable=0). Synthesize a textual call sourced to the enclosing function
+// so the lsp_{destructor,copy_constructor,conversion} resolution binds.
+//
+// - destructor: the callee QN embeds the type (`T.~T`), which is not textually
+// available from `delete p`, so it joins via the reason gate — c_lsp stashes
+// the operand text in `reason` and the synthesized callee is that same text.
+// - copy constructor: the callee short-name is the constructed type (`T`),
+// which IS textually present as the declaration's type — join by short-name.
+// - conversion: the callee short-name is the type-independent `operator bool`.
+//
+// Spurious synthesis (a condition/operand that has no such member) resolves to
+// nothing and is dropped, so no extra edge is produced.
+static void extract_cpp_implicit_calls(CBMExtractCtx *ctx, TSNode node, const char *kind,
+ const char *enclosing_func_qn) {
+ const char *callee = NULL;
+ if (strcmp(kind, "delete_expression") == 0) {
+ TSNode operand = ts_node_child_by_field_name(node, TS_FIELD("argument"));
+ if (ts_node_is_null(operand) && ts_node_named_child_count(node) > 0) {
+ operand = ts_node_named_child(node, 0);
+ }
+ if (!ts_node_is_null(operand)) {
+ callee = cbm_node_text(ctx->arena, operand, ctx->source);
+ }
+ } else if (strcmp(kind, "if_statement") == 0 || strcmp(kind, "while_statement") == 0 ||
+ strcmp(kind, "do_statement") == 0) {
+ // `if (obj)` invokes obj's `operator bool`. Only a lone-identifier
+ // condition triggers it; comparisons/logical exprs evaluate to bool.
+ TSNode cond = ts_node_child_by_field_name(node, TS_FIELD("condition"));
+ if (!ts_node_is_null(cond)) {
+ TSNode inner = cond;
+ if (strcmp(ts_node_type(cond), "condition_clause") == 0 &&
+ ts_node_named_child_count(cond) == 1) {
+ inner = ts_node_named_child(cond, 0);
+ }
+ if (strcmp(ts_node_type(inner), "identifier") == 0) {
+ callee = "operator bool";
+ }
+ }
+ } else if (strcmp(kind, "declaration") == 0) {
+ // `T a = b;` — copy-init from an identifier invokes T's copy constructor.
+ TSNode type = ts_node_child_by_field_name(node, TS_FIELD("type"));
+ TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));
+ if (!ts_node_is_null(type) && !ts_node_is_null(decl) &&
+ strcmp(ts_node_type(decl), "init_declarator") == 0) {
+ TSNode value = ts_node_child_by_field_name(decl, TS_FIELD("value"));
+ if (!ts_node_is_null(value) && strcmp(ts_node_type(value), "identifier") == 0) {
+ char *tn = cbm_node_text(ctx->arena, type, ctx->source);
+ if (tn) {
+ const char *colon = strrchr(tn, ':');
+ callee = colon ? colon + 1 : tn;
+ }
+ }
+ }
+ }
+ if (callee && callee[0]) {
+ CBMCall call = {0};
+ call.callee_name = callee;
+ call.enclosing_func_qn = enclosing_func_qn;
+ call.start_line = (int)ts_node_start_point(node).row + TS_LINE_OFFSET;
+ cbm_calls_push(&ctx->result->calls, ctx->arena, call);
+ }
+}
+
static void extract_kotlin_desugared_calls(CBMExtractCtx *ctx, TSNode node, const char *kind,
const char *enclosing_func_qn) {
if (strcmp(kind, "property_declaration") == 0) {
@@ -1871,5 +1938,6 @@ void handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Walk
if (ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA) {
extract_cpp_operator_call(ctx, node, ts_node_type(node), state->enclosing_func_qn);
+ extract_cpp_implicit_calls(ctx, node, ts_node_type(node), state->enclosing_func_qn);
}
}
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 51dec0ba9..47b249498 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -2875,7 +2875,7 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
return;
}
- char *name = cbm_node_text(a, name_node, ctx->source);
+ char *name = cbm_func_name_node_text(a, name_node, ctx->source);
if (!name || !name[0] || strcmp(name, "function") == 0) {
return;
}
@@ -3867,7 +3867,7 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, const char *class_
const CBMLangSpec *spec, TSNode name_node) {
CBMArena *a = ctx->arena;
- char *name = cbm_node_text(a, name_node, ctx->source);
+ char *name = cbm_func_name_node_text(a, name_node, ctx->source);
if (!name || !name[0]) {
return;
}
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index 5dd89313f..166f2856b 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -391,7 +391,7 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
return NULL;
}
- char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
+ char *name = cbm_func_name_node_text(ctx->arena, name_node, ctx->source);
if (!name || !name[0]) {
return NULL;
}
diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.c
index c079c5b96..5821eefbc 100644
--- a/internal/cbm/helpers.c
+++ b/internal/cbm/helpers.c
@@ -801,6 +801,28 @@ TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
return null_node;
}
+// Convert a resolved function/method name node to its name string. Most nodes
+// map directly to their text, but a C++ conversion-operator's `operator_cast`
+// node spans the full "operator bool() const" — this grammar folds the parameter
+// list and cv-qualifiers into the node. The method's name is only the
+// "operator <type>" prefix, so truncate at the first '(' and trim trailing
+// space. Without this the conversion operator is indexed as "operator bool()
+// const", and a member lookup for "operator bool" (the implicit call in
+// `if (obj)`) misses.
+char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source) {
+ char *text = cbm_node_text(a, name_node, source);
+ if (text && strcmp(ts_node_type(name_node), "operator_cast") == 0) {
+ char *paren = strchr(text, '(');
+ if (paren) {
+ while (paren > text && (paren[-1] == ' ' || paren[-1] == '\t')) {
+ paren--;
+ }
+ *paren = '\0';
+ }
+ }
+ return text;
+}
+
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
CBMLanguage lang) {
// Wolfram: set_delayed_top/set_top/set_delayed/set — LHS is apply(user_symbol("f"), ...)
@@ -839,7 +861,7 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
TSNode dn = cbm_resolve_c_declarator_name_node(func_node);
if (!ts_node_is_null(dn)) {
- return cbm_node_text(a, dn, source);
+ return cbm_func_name_node_text(a, dn, source);
}
}
return NULL;
diff --git a/internal/cbm/helpers.h b/internal/cbm/helpers.h
index 99dc140c0..232db84d1 100644
--- a/internal/cbm/helpers.h
+++ b/internal/cbm/helpers.h
@@ -50,6 +50,12 @@ const char *cbm_enclosing_func_qn_cached(CBMExtractCtx *ctx, TSNode node);
// enclosing-function attribution — drift between private copies caused #438.
TSNode cbm_resolve_c_declarator_name_node(TSNode func_node);
+// Convert a resolved function/method name node to its name string, normalizing a
+// C++ conversion-operator's `operator_cast` node (which spans the full
+// "operator bool() const") down to "operator bool". Shared by the defs and
+// unified extractors so the def name and call-scope QN agree.
+char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source);
+
// Resolve a function/method definition node's NAME node across all ~130 grammars
// (generic `name` field, arrow→declarator, C/C++ declarator chain, plus the many
// per-language quirks: Fortran subroutine, SCSS mixin, SQL create_function, R,
diff --git a/internal/cbm/lsp/c_lsp.c b/internal/cbm/lsp/c_lsp.c
index 3f130e909..3ab5cf951 100644
--- a/internal/cbm/lsp/c_lsp.c
+++ b/internal/cbm/lsp/c_lsp.c
@@ -3801,7 +3801,12 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
const char *short_name = strrchr(type_qn, '.');
short_name = short_name ? short_name + 1 : type_qn;
const char *dtor_qn = cbm_arena_sprintf(ctx->arena, "%s.~%s", type_qn, short_name);
- c_emit_resolved_call(ctx, dtor_qn, "lsp_destructor", 0.90f);
+ // The destructor callee QN (`T.~T`) is not textually available
+ // from `delete p` — the call walk can only synthesize a call to
+ // the operand text. Stash that operand text in `reason` so the
+ // pipeline join binds the synthesized call via the reason gate.
+ c_emit_resolved_call_orig(ctx, dtor_qn, c_node_text(ctx, operand), "lsp_destructor",
+ 0.90f);
}
}
}
@@ -3973,6 +3978,13 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
strcmp(kind, "do_statement") == 0)) {
TSNode cond = ts_node_child_by_field_name(node, "condition", 9);
if (!ts_node_is_null(cond)) {
+ // The `condition` field is a `condition_clause` wrapping the `( expr )`;
+ // unwrap it to the inner expression so its type evaluates (a clause
+ // node has no type, so `if (obj)` would never resolve obj's type).
+ if (strcmp(ts_node_type(cond), "condition_clause") == 0 &&
+ ts_node_named_child_count(cond) == 1) {
+ cond = ts_node_named_child(cond, 0);
+ }
// If condition is a single expression of a custom type with operator bool
const CBMType *cond_type = c_eval_expr_type(ctx, cond);
const CBMType *base = c_simplify_type(ctx, cond_type, false);
diff --git a/src/pipeline/lsp_resolve.h b/src/pipeline/lsp_resolve.h
index ad4e43751..5c66863df 100644
--- a/src/pipeline/lsp_resolve.h
+++ b/src/pipeline/lsp_resolve.h
@@ -105,10 +105,12 @@ static inline const CBMResolvedCall *cbm_pipeline_find_lsp_resolution(
* calls (bare callee_name) are unaffected. */
const char *call_short = cbm_lsp_bare_segment(call->callee_name);
if (strcmp(short_name, call_short) != 0) {
- /* Data-flow resolution: a function-pointer / DLL call's textual
- * callee is the pointer name (`fp`), which the LSP resolved to a
- * differently-named target and stashed in `reason`. Match the call
- * site on that original name, gated to those strategies so `reason`
+ /* Indirect/implicit resolution: the textual callee differs from the
+ * resolved callee_qn's short name. A function-pointer / DLL call's
+ * callee is the pointer name (`fp`); a C++ destructor's only textual
+ * anchor is the deleted operand (`p`, vs. the `T.~T` callee QN). In
+ * both the LSP stashed the original textual name in `reason`. Match
+ * the call site on that name, gated to those strategies so `reason`
* is never misread as an unresolved-call diagnostic. */
if (!(rc->reason && rc->strategy &&
(strcmp(rc->strategy, "lsp_func_ptr") == 0 ||
@@ -116,6 +118,7 @@ static inline const CBMResolvedCall *cbm_pipeline_find_lsp_resolution(
strcmp(rc->strategy, "lsp_method_ref_ctor") == 0 ||
strcmp(rc->strategy, "lsp_method_ref_ctor_synth") == 0 ||
strcmp(rc->strategy, "lsp_dict_dispatch") == 0 ||
+ strcmp(rc->strategy, "lsp_destructor") == 0 ||
strcmp(rc->strategy, "php_method_dynamic") == 0) &&
strcmp(cbm_lsp_bare_segment(rc->reason), call_short) == 0)) {
continue;