Skip to content

Commit 6b1e3a0

Browse files
committed
fix(calls): extract resolvable builtin calls so LSP builtin edges form
The call extractor dropped any callee matched by cbm_is_keyword(), and the Python keyword set lists builtins (len, str, range, int, list, dict, print) to suppress them as bare usages. That also dropped the textual CALL, so the LSP resolution to the injected builtins.<name> node (py_builtins.c) had no call to attach to and no CALLS edge formed (callable=0) for free-function and constructor builtins. Method builtins (s.upper) were unaffected because method names are not in the keyword set. Add cbm_is_resolvable_builtin() — the builtins we mint a real graph node for — and keep those callees in call extraction only; the keyword filter still applies to usage and semantic extraction (its four other call sites are unchanged). Every un-filtered builtin has a target node, so the new edge is always callable-sourced, never Module-sourced. Un-skips repro_lsp_py_builtin and repro_lsp_py_builtin_constructor. Repro suite 311/0, unit suite 5693/0. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 36d8328 commit 6b1e3a0

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,11 @@ void handle_calls(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Walk
18731873

18741874
if (cbm_kind_in_set(node, spec->call_node_types)) {
18751875
char *callee = extract_callee_name(ctx->arena, node, ctx->source, ctx->language);
1876-
if (callee && callee[0] && !cbm_is_keyword(callee, ctx->language)) {
1876+
// Keyword-filter callees, but keep builtins we mint a node for (len, str,
1877+
// ...) so the LSP-resolved builtin call still forms a CALLS edge.
1878+
if (callee && callee[0] &&
1879+
(!cbm_is_keyword(callee, ctx->language) ||
1880+
cbm_is_resolvable_builtin(callee, ctx->language))) {
18771881
CBMCall call = {0};
18781882
call.callee_name = callee;
18791883
call.enclosing_func_qn = state->enclosing_func_qn;

‎internal/cbm/helpers.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@ bool cbm_is_keyword(const char *name, CBMLanguage lang) {
210210
return false;
211211
}
212212

213+
// Builtins that appear in the keyword set above (so they are suppressed as bare
214+
// usages) but for which we mint a real graph node and an LSP resolution, so a
215+
// CALL to them must still be extracted. MUST stay in sync with kPyBuiltinNodes
216+
// in internal/cbm/lsp/py_builtins.c — every entry here has a "builtins.<name>"
217+
// node, so the resulting CALLS edge always has a target (never Module-sourced).
218+
static const char *python_resolvable_builtins[] = {"len", "print", "str", "int",
219+
"list", "dict", "range", NULL};
220+
221+
bool cbm_is_resolvable_builtin(const char *name, CBMLanguage lang) {
222+
if (!name || !name[0]) {
223+
return false;
224+
}
225+
if (lang != CBM_LANG_PYTHON) {
226+
return false;
227+
}
228+
for (const char **b = python_resolvable_builtins; *b; b++) {
229+
if (strcmp(name, *b) == 0) {
230+
return true;
231+
}
232+
}
233+
return false;
234+
}
235+
213236
// --- Export detection ---
214237

215238
bool cbm_is_exported(const char *name, CBMLanguage lang) {

‎internal/cbm/helpers.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ char *cbm_node_text(CBMArena *a, TSNode node, const char *source);
1515
// Check if a string is a language keyword (should be skipped as callee/usage).
1616
bool cbm_is_keyword(const char *name, CBMLanguage lang);
1717

18+
// Check if a name is a builtin we mint a real graph node for, so a CALL to it
19+
// must NOT be keyword-filtered out of call extraction (the LSP resolves it to
20+
// the injected builtin node and forms a CALLS edge). Narrower than
21+
// cbm_is_keyword: it only covers builtins with a target node, so un-filtering
22+
// them cannot produce a node-less / Module-sourced edge. The Python set MUST
23+
// stay in sync with kPyBuiltinNodes in internal/cbm/lsp/py_builtins.c.
24+
bool cbm_is_resolvable_builtin(const char *name, CBMLanguage lang);
25+
1826
// Classify a string literal as URL, config, or neither.
1927
// Returns CBM_STRREF_URL (0), CBM_STRREF_CONFIG (1), or -1 for neither.
2028
int cbm_classify_string(const char *str, int len);

‎tests/repro/repro_lsp_go_py.c‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -571,23 +571,14 @@ TEST(repro_lsp_py_operator_dunder) {
571571
}
572572

573573
TEST(repro_lsp_py_builtin) {
574-
/* PARKED for release: lsp_builtin (len(v)) needs a typeshed/builtins registry
575-
* so builtin functions have target nodes; without it the resolution has no
576-
* node to form a CALLS edge to (callable=0). Tracked for a future builtins
577-
* registry. */
578-
printf(" %sSKIP%s parked: needs builtins/typeshed registry (len has no node)\n", tf_dim(),
579-
tf_reset());
580-
return -1; /* skip — not counted as pass or fail */
574+
/* len(v) resolves to the injected builtins.len node (py_builtins.c) and
575+
* emits lsp_builtin with a real CALLS edge. */
581576
return assert_lsp_strategy("main.py", kPyBuiltin, "lsp_builtin");
582577
}
583578

584579
TEST(repro_lsp_py_builtin_constructor) {
585-
/* PARKED for release: lsp_builtin_constructor (str(v)) needs a builtins/
586-
* typeshed registry so the builtin type str has a node to target. Tracked
587-
* for a future builtins registry. */
588-
printf(" %sSKIP%s parked: needs builtins/typeshed registry (str type has no node)\n", tf_dim(),
589-
tf_reset());
590-
return -1; /* skip — not counted as pass or fail */
580+
/* str(v) resolves to the injected builtins.str type node (py_builtins.c)
581+
* and emits lsp_builtin_constructor with a real CALLS edge. */
591582
return assert_lsp_strategy("main.py", kPyBuiltinConstructor,
592583
"lsp_builtin_constructor");
593584
}

0 commit comments

Comments
 (0)