Skip to content

Commit f33e277

Browse files
committed
fix(cpp): synthesize a textual call for overloaded binary operators (lsp_operator)
An overloaded C++ binary operator (return a + b; where Vec defines operator+) invokes the operator method implicitly, so the call walk never produced a call node — the c-LSP resolved Vec::operator+ (lsp_operator) but had no call site to attach to (callable=0, no CALLS edge). Synthesize a textual call to the bare operator name (operator<token>) at each binary_expression for C++/CUDA, mirroring c_lsp.c's binary handling (the operator token is the first unnamed child). The join binds it to the c-LSP resolution. Builtin-operand expressions synthesize operator+ too but resolve no member and are dropped, so no spurious edges. Mirrors the existing Kotlin operator-call synthesis. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 147ad87 commit f33e277

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,41 @@ static void kt_push_implicit_call(CBMExtractCtx *ctx, TSNode node, const char *c
17041704
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
17051705
}
17061706

1707+
// C++ overloaded binary operator `a + b`: the operator method (`operator+`) is
1708+
// invoked implicitly, so the call walk never sees a call node. Synthesize a
1709+
// textual call to the bare operator name so the c-LSP's lsp_operator resolution
1710+
// (which keys the same `operator<tok>` member on the lhs type) has a call site to
1711+
// join. The operator token is the first unnamed child, mirroring c_lsp.c's binary
1712+
// handling. Builtin-operand expressions (int + int) synthesize an `operator+`
1713+
// callee too, but no such member exists so the call resolves to nothing and is
1714+
// dropped — no spurious edge.
1715+
static void extract_cpp_operator_call(CBMExtractCtx *ctx, TSNode node, const char *kind,
1716+
const char *enclosing_func_qn) {
1717+
if (strcmp(kind, "binary_expression") != 0) {
1718+
return;
1719+
}
1720+
TSNode lhs = ts_node_child_by_field_name(node, TS_FIELD("left"));
1721+
TSNode rhs = ts_node_child_by_field_name(node, TS_FIELD("right"));
1722+
if (ts_node_is_null(lhs) || ts_node_is_null(rhs)) {
1723+
return;
1724+
}
1725+
for (uint32_t i = 0; i < ts_node_child_count(node); i++) {
1726+
TSNode child = ts_node_child(node, i);
1727+
if (ts_node_is_named(child)) {
1728+
continue;
1729+
}
1730+
char *op = cbm_node_text(ctx->arena, child, ctx->source);
1731+
if (op && op[0]) {
1732+
CBMCall call = {0};
1733+
call.callee_name = cbm_arena_sprintf(ctx->arena, "operator%s", op);
1734+
call.enclosing_func_qn = enclosing_func_qn;
1735+
call.start_line = (int)ts_node_start_point(node).row + TS_LINE_OFFSET;
1736+
cbm_calls_push(&ctx->result->calls, ctx->arena, call);
1737+
}
1738+
break;
1739+
}
1740+
}
1741+
17071742
static void extract_kotlin_desugared_calls(CBMExtractCtx *ctx, TSNode node, const char *kind,
17081743
const char *enclosing_func_qn) {
17091744
if (strcmp(kind, "property_declaration") == 0) {
@@ -1833,4 +1868,8 @@ void handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Walk
18331868
extract_kotlin_operator_call(ctx, node, ts_node_type(node), state->enclosing_func_qn);
18341869
extract_kotlin_desugared_calls(ctx, node, ts_node_type(node), state->enclosing_func_qn);
18351870
}
1871+
1872+
if (ctx->language == CBM_LANG_CPP || ctx->language == CBM_LANG_CUDA) {
1873+
extract_cpp_operator_call(ctx, node, ts_node_type(node), state->enclosing_func_qn);
1874+
}
18361875
}

0 commit comments

Comments
 (0)