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.

Merge origin/main into fix/py-namespace-reexports-v2 (pick up #2697)

Martin Vogel67e53d0ead6a
diff --cc internal/cbm/lsp/py_lsp.c
index 6e13f54ed,01a0905e4..7ebc378eb
--- a/internal/cbm/lsp/py_lsp.c
+++ b/internal/cbm/lsp/py_lsp.c
@@@ -1268,104 -1224,85 +1268,164 @@@ static const CBMRegisteredFunc *py_look
return NULL;
}
- static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const char *type_qn,
- const char *member_name) {
- return py_lookup_attribute_depth(ctx, type_qn, member_name, 0);
- }
-
+static PyModuleMember py_module_member_exact(PyLSPContext *ctx, const char *module_qn,
+ const char *name) {
+ PyModuleMember m = {0};
+ m.func = cbm_registry_lookup_symbol(ctx->registry, module_qn, name);
+ if (m.func)
+ return m;
+ const char *qn = cbm_arena_sprintf(ctx->arena, "%s.%s", module_qn, name);
+ m.type = qn ? cbm_registry_lookup_type(ctx->registry, qn) : NULL;
+ if (m.type)
+ return m;
+ /* Re-export: `module.name` is bound by the module's imports (the export
+ * table the cross registry materialized once, before its seal), keyed by
+ * the module symbol scope (a package without its `.__init__`). */
+ size_t scope_len = cbm_fqn_symbol_scope_len(module_qn);
+ const char *scope =
+ module_qn[scope_len] ? cbm_arena_strndup(ctx->arena, module_qn, scope_len) : module_qn;
+ const char *target = scope ? cbm_registry_lookup_export(ctx->registry, scope, name) : NULL;
+ if (target) {
+ m.func = cbm_registry_lookup_func(ctx->registry, target);
+ if (!m.func)
+ m.type = cbm_registry_lookup_type(ctx->registry, target);
+ }
+ return m;
+}
+
+/* `prefix.module_qn`, or NULL when module_qn already starts with prefix. */
+static const char *py_qualify_module(PyLSPContext *ctx, const char *prefix, size_t prefix_len,
+ const char *module_qn) {
+ if (prefix_len == 0 ||
+ (strncmp(module_qn, prefix, prefix_len) == 0 && module_qn[prefix_len] == '.'))
+ return NULL;
+ return cbm_arena_sprintf(ctx->arena, "%.*s.%s", (int)prefix_len, prefix, module_qn);
+}
+
+/* An in-project `import sibling` / `import pkg` binds the module by its
+ * source spelling ("pkg"), while its defs are registered project-qualified.
+ * Try the spelling as written, then qualified by the importing file's
+ * package, then by the project root (the file module's first segment). */
+static PyModuleMember py_module_member(PyLSPContext *ctx, const char *module_qn, const char *name) {
+ PyModuleMember none = {0};
+ if (!ctx || !ctx->registry || !module_qn || !name)
+ return none;
+ PyModuleMember m = py_module_member_exact(ctx, module_qn, name);
+ if (m.func || m.type || !ctx->file_module_qn)
+ return m;
+ const char *file_mod = ctx->file_module_qn;
+ const char *last_dot = strrchr(file_mod, '.');
+ const char *first_dot = strchr(file_mod, '.');
+ if (last_dot && last_dot > file_mod) {
+ const char *qual =
+ py_qualify_module(ctx, file_mod, (size_t)(last_dot - file_mod), module_qn);
+ m = qual ? py_module_member_exact(ctx, qual, name) : none;
+ if (m.func || m.type)
+ return m;
+ }
+ if (first_dot && first_dot != last_dot) {
+ const char *qual =
+ py_qualify_module(ctx, file_mod, (size_t)(first_dot - file_mod), module_qn);
+ m = qual ? py_module_member_exact(ctx, qual, name) : none;
+ }
+ return m;
+}
+
+/* Emit a call resolved through a module namespace. A re-export may bind the
+ * definition under another name (`from .x import Orig as Renamed`): that is
+ * an import alias, whose join reads the written name from `reason`. */
+static void py_emit_module_member_call(PyLSPContext *ctx, const char *callee_qn,
+ const char *strategy, float confidence,
+ const char *written_name, TSNode site) {
+ const char *dot = strrchr(callee_qn, '.');
+ const char *short_name = dot ? dot + 1 : callee_qn;
+ if (strcmp(short_name, written_name) != 0) {
+ py_emit_resolved_call_reason(ctx, callee_qn, "lsp_import_alias", confidence, written_name,
+ site);
+ } else {
+ py_emit_resolved_call(ctx, callee_qn, strategy, confidence, site);
+ }
+}
+
- /* ── super(): the C3 method resolution order ──────────────────────── */
+ /* ── The C3 method resolution order ──────────────────────────────── */
/* Longest linearization followed. A longer one (or more bases than this) keeps
- * the base-by-base walk of the caller. */
+ * the depth-first walk above. */
#define PY_C3_MAX 64
- /* The linearizations computed during one super() lookup: every class of a
- * diamond is linearized once, however many paths reach it. */
- typedef struct {
- const char *qn[PY_C3_MAX];
- const char **mro[PY_C3_MAX];
- int len[PY_C3_MAX];
- int count;
- } py_c3_cache_t;
+ enum { PY_MRO_CACHE_INITIAL_CAP = 64 }; /* power of two (index masking) */
+ enum { PY_MRO_CACHE_MAX_CAP = 1 << 20 }; /* hard stop for adversarial files */
+
+ static uint32_t py_mro_hash(const char *qn) {
+ uint32_t h = 2166136261u; /* FNV-1a */
+ for (const unsigned char *p = (const unsigned char *)qn; *p; p++) {
+ h = (h ^ *p) * 16777619u;
+ }
+ return h;
+ }
+
+ static const CBMPyMroCacheEntry *py_mro_cache_find(const PyLSPContext *ctx, const char *qn,
+ uint32_t h) {
+ if (!ctx->mro_cache || ctx->mro_cache_cap == 0)
+ return NULL;
+ uint32_t mask = (uint32_t)ctx->mro_cache_cap - 1;
+ uint32_t idx = h & mask;
+ for (int probed = 0; probed < ctx->mro_cache_cap; probed++) {
+ const CBMPyMroCacheEntry *e = &ctx->mro_cache[idx];
+ if (!e->qn)
+ return NULL;
+ if (e->hash == h && strcmp(e->qn, qn) == 0)
+ return e;
+ idx = (idx + 1) & mask;
+ }
+ return NULL;
+ }
+
+ /* Doubles the table (arena allocation: the old table is freed with the
+ * per-file arena). */
+ static bool py_mro_cache_grow(PyLSPContext *ctx) {
+ if (ctx->mro_cache_cap >= PY_MRO_CACHE_MAX_CAP)
+ return false;
+ int new_cap = ctx->mro_cache_cap ? ctx->mro_cache_cap * 2 : PY_MRO_CACHE_INITIAL_CAP;
+ CBMPyMroCacheEntry *entries = (CBMPyMroCacheEntry *)cbm_arena_alloc(
+ ctx->arena, (size_t)new_cap * sizeof(CBMPyMroCacheEntry));
+ if (!entries)
+ return false;
+ memset(entries, 0, (size_t)new_cap * sizeof(CBMPyMroCacheEntry));
+ uint32_t mask = (uint32_t)new_cap - 1;
+ for (int i = 0; i < ctx->mro_cache_cap; i++) {
+ const CBMPyMroCacheEntry *e = &ctx->mro_cache[i];
+ if (!e->qn)
+ continue;
+ uint32_t idx = e->hash & mask;
+ while (entries[idx].qn)
+ idx = (idx + 1) & mask;
+ entries[idx] = *e;
+ }
+ ctx->mro_cache = entries;
+ ctx->mro_cache_cap = new_cap;
+ return true;
+ }
+
+ /* Records the order of `qn` (len -1: none). A table that cannot grow only
+ * costs a recomputation later. */
+ static void py_mro_cache_put(PyLSPContext *ctx, const char *qn, uint32_t h, const char **mro,
+ int len) {
+ if ((ctx->mro_cache_count + 1) * 4 > ctx->mro_cache_cap * 3 && !py_mro_cache_grow(ctx))
+ return;
+ const char *key = cbm_arena_strdup(ctx->arena, qn);
+ if (!key)
+ return;
+ uint32_t mask = (uint32_t)ctx->mro_cache_cap - 1;
+ uint32_t idx = h & mask;
+ while (ctx->mro_cache[idx].qn)
+ idx = (idx + 1) & mask;
+ ctx->mro_cache[idx].qn = key;
+ ctx->mro_cache[idx].hash = h;
+ ctx->mro_cache[idx].len = len;
+ ctx->mro_cache[idx].mro = mro;
+ ctx->mro_cache_count++;
+ }
static bool py_c3_in_tail(const char *qn, const char *const *seq, int head, int len) {
for (int k = head + 1; k < len; k++) {