Skip to content

Commit 5b60045

Browse files
committed
fix(cpp): resolve implicit destructor, copy-ctor, and conversion-operator calls
Three C++ constructs invoke a member implicitly with no textual call node, so the call walk produced nothing for the c-LSP to attach its resolution to (callable=0, no CALLS edge): - delete p; -> destructor T::~T - T a = b; -> copy/move ctor T::T - if (obj) { ... } -> conversion T::operator bool Synthesize a textual call sourced to the enclosing function at each trigger so the lsp_{destructor,copy_constructor,conversion} resolution binds. The destructor 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); the other two join by callee short-name (the declared type / the type-independent operator bool). Two underlying defects surfaced and are fixed: - The conversion check evaluated the type of the `condition_clause` node (the `( expr )` wrapper, which has no type) instead of the inner expression, so `if (obj)` never resolved obj's type. Unwrap the clause. - A conversion operator's `operator_cast` node spans the full "operator bool() const" in this grammar (params + cv-qualifiers folded in), so the method was indexed under that malformed name and member lookups for "operator bool" missed. Add cbm_func_name_node_text to normalize it to "operator bool", shared by the defs and unified extractors so the def name and call-scope QN agree. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent f33e277 commit 5b60045

7 files changed

Lines changed: 120 additions & 9 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,73 @@ static void extract_cpp_operator_call(CBMExtractCtx *ctx, TSNode node, const cha
17391739
}
17401740
}
17411741

1742+
// C++ implicit calls that produce no textual call node: the destructor
1743+
// (`delete p`), the copy/move constructor (`T a = b;` copy-init), and the
1744+
// conversion operator (`if (obj)` where obj has `operator bool`). The c-LSP
1745+
// resolves each to the corresponding member but there is no call site to join
1746+
// to (callable=0). Synthesize a textual call sourced to the enclosing function
1747+
// so the lsp_{destructor,copy_constructor,conversion} resolution binds.
1748+
//
1749+
// - destructor: the callee QN embeds the type (`T.~T`), which is not textually
1750+
// available from `delete p`, so it joins via the reason gate — c_lsp stashes
1751+
// the operand text in `reason` and the synthesized callee is that same text.
1752+
// - copy constructor: the callee short-name is the constructed type (`T`),
1753+
// which IS textually present as the declaration's type — join by short-name.
1754+
// - conversion: the callee short-name is the type-independent `operator bool`.
1755+
//
1756+
// Spurious synthesis (a condition/operand that has no such member) resolves to
1757+
// nothing and is dropped, so no extra edge is produced.
1758+
static void extract_cpp_implicit_calls(CBMExtractCtx *ctx, TSNode node, const char *kind,
1759+
const char *enclosing_func_qn) {
1760+
const char *callee = NULL;
1761+
if (strcmp(kind, "delete_expression") == 0) {
1762+
TSNode operand = ts_node_child_by_field_name(node, TS_FIELD("argument"));
1763+
if (ts_node_is_null(operand) && ts_node_named_child_count(node) > 0) {
1764+
operand = ts_node_named_child(node, 0);
1765+
}
1766+
if (!ts_node_is_null(operand)) {
1767+
callee = cbm_node_text(ctx->arena, operand, ctx->source);
1768+
}
1769+
} else if (strcmp(kind, "if_statement") == 0 || strcmp(kind, "while_statement") == 0 ||
1770+
strcmp(kind, "do_statement") == 0) {
1771+
// `if (obj)` invokes obj's `operator bool`. Only a lone-identifier
1772+
// condition triggers it; comparisons/logical exprs evaluate to bool.
1773+
TSNode cond = ts_node_child_by_field_name(node, TS_FIELD("condition"));
1774+
if (!ts_node_is_null(cond)) {
1775+
TSNode inner = cond;
1776+
if (strcmp(ts_node_type(cond), "condition_clause") == 0 &&
1777+
ts_node_named_child_count(cond) == 1) {
1778+
inner = ts_node_named_child(cond, 0);
1779+
}
1780+
if (strcmp(ts_node_type(inner), "identifier") == 0) {
1781+
callee = "operator bool";
1782+
}
1783+
}
1784+
} else if (strcmp(kind, "declaration") == 0) {
1785+
// `T a = b;` — copy-init from an identifier invokes T's copy constructor.
1786+
TSNode type = ts_node_child_by_field_name(node, TS_FIELD("type"));
1787+
TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));
1788+
if (!ts_node_is_null(type) && !ts_node_is_null(decl) &&
1789+
strcmp(ts_node_type(decl), "init_declarator") == 0) {
1790+
TSNode value = ts_node_child_by_field_name(decl, TS_FIELD("value"));
1791+
if (!ts_node_is_null(value) && strcmp(ts_node_type(value), "identifier") == 0) {
1792+
char *tn = cbm_node_text(ctx->arena, type, ctx->source);
1793+
if (tn) {
1794+
const char *colon = strrchr(tn, ':');
1795+
callee = colon ? colon + 1 : tn;
1796+
}
1797+
}
1798+
}
1799+
}
1800+
if (callee && callee[0]) {
1801+
CBMCall call = {0};
1802+
call.callee_name = callee;
1803+
call.enclosing_func_qn = enclosing_func_qn;
1804+
call.start_line = (int)ts_node_start_point(node).row + TS_LINE_OFFSET;
1805+
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
1806+
}
1807+
}
1808+
17421809
static void extract_kotlin_desugared_calls(CBMExtractCtx *ctx, TSNode node, const char *kind,
17431810
const char *enclosing_func_qn) {
17441811
if (strcmp(kind, "property_declaration") == 0) {
@@ -1871,5 +1938,6 @@ void handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Walk
18711938

18721939
if (ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA) {
18731940
extract_cpp_operator_call(ctx, node, ts_node_type(node), state->enclosing_func_qn);
1941+
extract_cpp_implicit_calls(ctx, node, ts_node_type(node), state->enclosing_func_qn);
18741942
}
18751943
}

‎internal/cbm/extract_defs.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,7 +2875,7 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
28752875
return;
28762876
}
28772877

2878-
char *name = cbm_node_text(a, name_node, ctx->source);
2878+
char *name = cbm_func_name_node_text(a, name_node, ctx->source);
28792879
if (!name || !name[0] || strcmp(name, "function") == 0) {
28802880
return;
28812881
}
@@ -3867,7 +3867,7 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, const char *class_
38673867
const CBMLangSpec *spec, TSNode name_node) {
38683868
CBMArena *a = ctx->arena;
38693869

3870-
char *name = cbm_node_text(a, name_node, ctx->source);
3870+
char *name = cbm_func_name_node_text(a, name_node, ctx->source);
38713871
if (!name || !name[0]) {
38723872
return;
38733873
}

‎internal/cbm/extract_unified.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
391391
return NULL;
392392
}
393393

394-
char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
394+
char *name = cbm_func_name_node_text(ctx->arena, name_node, ctx->source);
395395
if (!name || !name[0]) {
396396
return NULL;
397397
}

‎internal/cbm/helpers.c‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,28 @@ TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
801801
return null_node;
802802
}
803803

804+
// Convert a resolved function/method name node to its name string. Most nodes
805+
// map directly to their text, but a C++ conversion-operator's `operator_cast`
806+
// node spans the full "operator bool() const" — this grammar folds the parameter
807+
// list and cv-qualifiers into the node. The method's name is only the
808+
// "operator <type>" prefix, so truncate at the first '(' and trim trailing
809+
// space. Without this the conversion operator is indexed as "operator bool()
810+
// const", and a member lookup for "operator bool" (the implicit call in
811+
// `if (obj)`) misses.
812+
char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source) {
813+
char *text = cbm_node_text(a, name_node, source);
814+
if (text && strcmp(ts_node_type(name_node), "operator_cast") == 0) {
815+
char *paren = strchr(text, '(');
816+
if (paren) {
817+
while (paren > text && (paren[-1] == ' ' || paren[-1] == '\t')) {
818+
paren--;
819+
}
820+
*paren = '\0';
821+
}
822+
}
823+
return text;
824+
}
825+
804826
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
805827
CBMLanguage lang) {
806828
// 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
839861
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
840862
TSNode dn = cbm_resolve_c_declarator_name_node(func_node);
841863
if (!ts_node_is_null(dn)) {
842-
return cbm_node_text(a, dn, source);
864+
return cbm_func_name_node_text(a, dn, source);
843865
}
844866
}
845867
return NULL;

‎internal/cbm/helpers.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const char *cbm_enclosing_func_qn_cached(CBMExtractCtx *ctx, TSNode node);
5050
// enclosing-function attribution — drift between private copies caused #438.
5151
TSNode cbm_resolve_c_declarator_name_node(TSNode func_node);
5252

53+
// Convert a resolved function/method name node to its name string, normalizing a
54+
// C++ conversion-operator's `operator_cast` node (which spans the full
55+
// "operator bool() const") down to "operator bool". Shared by the defs and
56+
// unified extractors so the def name and call-scope QN agree.
57+
char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source);
58+
5359
// Resolve a function/method definition node's NAME node across all ~130 grammars
5460
// (generic `name` field, arrow→declarator, C/C++ declarator chain, plus the many
5561
// per-language quirks: Fortran subroutine, SCSS mixin, SQL create_function, R,

‎internal/cbm/lsp/c_lsp.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3801,7 +3801,12 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
38013801
const char *short_name = strrchr(type_qn, '.');
38023802
short_name = short_name ? short_name + 1 : type_qn;
38033803
const char *dtor_qn = cbm_arena_sprintf(ctx->arena, "%s.~%s", type_qn, short_name);
3804-
c_emit_resolved_call(ctx, dtor_qn, "lsp_destructor", 0.90f);
3804+
// The destructor callee QN (`T.~T`) is not textually available
3805+
// from `delete p` — the call walk can only synthesize a call to
3806+
// the operand text. Stash that operand text in `reason` so the
3807+
// pipeline join binds the synthesized call via the reason gate.
3808+
c_emit_resolved_call_orig(ctx, dtor_qn, c_node_text(ctx, operand), "lsp_destructor",
3809+
0.90f);
38053810
}
38063811
}
38073812
}
@@ -3973,6 +3978,13 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
39733978
strcmp(kind, "do_statement") == 0)) {
39743979
TSNode cond = ts_node_child_by_field_name(node, "condition", 9);
39753980
if (!ts_node_is_null(cond)) {
3981+
// The `condition` field is a `condition_clause` wrapping the `( expr )`;
3982+
// unwrap it to the inner expression so its type evaluates (a clause
3983+
// node has no type, so `if (obj)` would never resolve obj's type).
3984+
if (strcmp(ts_node_type(cond), "condition_clause") == 0 &&
3985+
ts_node_named_child_count(cond) == 1) {
3986+
cond = ts_node_named_child(cond, 0);
3987+
}
39763988
// If condition is a single expression of a custom type with operator bool
39773989
const CBMType *cond_type = c_eval_expr_type(ctx, cond);
39783990
const CBMType *base = c_simplify_type(ctx, cond_type, false);

‎src/pipeline/lsp_resolve.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,20 @@ static inline const CBMResolvedCall *cbm_pipeline_find_lsp_resolution(
105105
* calls (bare callee_name) are unaffected. */
106106
const char *call_short = cbm_lsp_bare_segment(call->callee_name);
107107
if (strcmp(short_name, call_short) != 0) {
108-
/* Data-flow resolution: a function-pointer / DLL call's textual
109-
* callee is the pointer name (`fp`), which the LSP resolved to a
110-
* differently-named target and stashed in `reason`. Match the call
111-
* site on that original name, gated to those strategies so `reason`
108+
/* Indirect/implicit resolution: the textual callee differs from the
109+
* resolved callee_qn's short name. A function-pointer / DLL call's
110+
* callee is the pointer name (`fp`); a C++ destructor's only textual
111+
* anchor is the deleted operand (`p`, vs. the `T.~T` callee QN). In
112+
* both the LSP stashed the original textual name in `reason`. Match
113+
* the call site on that name, gated to those strategies so `reason`
112114
* is never misread as an unresolved-call diagnostic. */
113115
if (!(rc->reason && rc->strategy &&
114116
(strcmp(rc->strategy, "lsp_func_ptr") == 0 ||
115117
strcmp(rc->strategy, "lsp_dll_resolve") == 0 ||
116118
strcmp(rc->strategy, "lsp_method_ref_ctor") == 0 ||
117119
strcmp(rc->strategy, "lsp_method_ref_ctor_synth") == 0 ||
118120
strcmp(rc->strategy, "lsp_dict_dispatch") == 0 ||
121+
strcmp(rc->strategy, "lsp_destructor") == 0 ||
119122
strcmp(rc->strategy, "php_method_dynamic") == 0) &&
120123
strcmp(cbm_lsp_bare_segment(rc->reason), call_short) == 0)) {
121124
continue;

0 commit comments

Comments
 (0)