Skip to content

Commit 0ae936f

Browse files
committed
fix(python): a per-file def selection follows package re-exports
A small incremental closure resolves each changed file through a per-file selection of defs (its own and its imported modules') instead of the shared registry. A package that re-exports a name keeps only the record there; the definition lives in the module the record points at, possibly through more re-exports, so a call through the package found nothing after an incremental re-index of the caller alone. cbm_pxc_filter_defs_for_file now follows every selected re-export or star record of a Python module to the module it names (relative exactly, absolute under the longest matching prefix, a member as a submodule) and selects that module's defs too, once per record, so the per-file LSP folds the same chain the shared registry does. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9b2ad49 commit 0ae936f

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,123 @@ void cbm_pxc_free_module_def_index(CBMModuleDefIndex *idx) {
27742774
free(idx);
27752775
}
27762776

2777+
/* A Python namespace record that points at another module (a re-export or a
2778+
* star import; `__all__` points nowhere). */
2779+
static bool pxc_is_py_reexport_record(const CBMLSPDef *d) {
2780+
return d->lang == CBM_LANG_PYTHON && d->label && d->receiver_type &&
2781+
(strcmp(d->label, "PyReexport") == 0 || strcmp(d->label, "PyStarImport") == 0);
2782+
}
2783+
2784+
/* The index entry of the Python module `qn` (a module file, or a package via
2785+
* its `__init__`), or NULL. */
2786+
static const pxc_module_entry_t *pxc_py_module_entry(const CBMModuleDefIndex *idx, const char *qn,
2787+
size_t len) {
2788+
char key[CBM_SZ_1K];
2789+
if (len + sizeof(".__init__") > sizeof(key))
2790+
return NULL;
2791+
memcpy(key, qn, len);
2792+
key[len] = '\0';
2793+
const pxc_module_entry_t *e = (const pxc_module_entry_t *)cbm_ht_get(idx->ht, key);
2794+
if (e)
2795+
return e;
2796+
memcpy(key + len, ".__init__", sizeof(".__init__"));
2797+
return (const pxc_module_entry_t *)cbm_ht_get(idx->ht, key);
2798+
}
2799+
2800+
/* The entry of the module `spec` names from inside the module `module_qn`:
2801+
* a relative spec (leading dots) against that module's package, exactly; an
2802+
* absolute spec under the longest prefix of module_qn that has it (the
2803+
* project and any source root precede the import path). `member`, when set,
2804+
* also names a submodule (`from . import base`). NULL when nothing matches. */
2805+
static const pxc_module_entry_t *pxc_py_spec_entry(const CBMModuleDefIndex *idx,
2806+
const char *module_qn, const char *spec,
2807+
const char *member) {
2808+
char qn[CBM_SZ_1K];
2809+
size_t level = strspn(spec, ".");
2810+
const char *rest = spec + level;
2811+
if (level > 0) {
2812+
size_t base = strlen(module_qn); /* drop the module's own segment first */
2813+
for (size_t up = 0; up < level; up++) {
2814+
while (base > 0 && module_qn[base - 1] != '.')
2815+
base--;
2816+
if (base == 0)
2817+
return NULL;
2818+
base--;
2819+
}
2820+
int n = rest[0] ? snprintf(qn, sizeof(qn), "%.*s.%s", (int)base, module_qn, rest)
2821+
: snprintf(qn, sizeof(qn), "%.*s", (int)base, module_qn);
2822+
if (n < 0 || (size_t)n >= sizeof(qn))
2823+
return NULL;
2824+
const pxc_module_entry_t *e = pxc_py_module_entry(idx, qn, (size_t)n);
2825+
if (!e && member) {
2826+
int m = snprintf(qn + n, sizeof(qn) - (size_t)n, ".%s", member);
2827+
if (m > 0 && (size_t)(n + m) < sizeof(qn))
2828+
e = pxc_py_module_entry(idx, qn, (size_t)(n + m));
2829+
}
2830+
return e;
2831+
}
2832+
for (size_t cut = strlen(module_qn); cut > 0; cut--) {
2833+
if (module_qn[cut - 1] != '.')
2834+
continue;
2835+
int n = snprintf(qn, sizeof(qn), "%.*s%s", (int)cut, module_qn, rest);
2836+
if (n < 0 || (size_t)n >= sizeof(qn))
2837+
continue;
2838+
const pxc_module_entry_t *e = pxc_py_module_entry(idx, qn, (size_t)n);
2839+
if (e)
2840+
return e;
2841+
}
2842+
return NULL;
2843+
}
2844+
2845+
/* A per-file selection holds the defs of the caller's own and imported
2846+
* modules. A Python package that re-exports a name keeps only the record;
2847+
* the definition lives in the module the record points at, possibly through
2848+
* further re-exports. Select those modules' defs too, following every
2849+
* selected record once (a worklist over def indices: linear in the defs it
2850+
* reaches), so the per-file LSP can fold the chain as the shared registry
2851+
* does. */
2852+
static void pxc_mark_reexport_targets(const CBMModuleDefIndex *idx, bool *selected,
2853+
const CBMLSPDef *all_defs, int *total) {
2854+
int cap = idx->def_count;
2855+
int *work = (int *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)cap * sizeof(int));
2856+
bool *queued = (bool *)cbm_calloc(CBM_MEM_CLASS_EXTRACT, (size_t)cap * sizeof(bool));
2857+
if (!work || !queued) {
2858+
cbm_free(CBM_MEM_CLASS_EXTRACT, work);
2859+
cbm_free(CBM_MEM_CLASS_EXTRACT, queued);
2860+
return;
2861+
}
2862+
int n = 0;
2863+
for (int i = 0; i < cap; i++) {
2864+
if (selected[i] && pxc_is_py_reexport_record(&all_defs[i])) {
2865+
queued[i] = true;
2866+
work[n++] = i;
2867+
}
2868+
}
2869+
for (int w = 0; w < n; w++) {
2870+
const CBMLSPDef *r = &all_defs[work[w]];
2871+
bool star = strcmp(r->label, "PyStarImport") == 0;
2872+
const pxc_module_entry_t *e =
2873+
r->def_module_qn ? pxc_py_spec_entry(idx, r->def_module_qn, r->receiver_type,
2874+
star ? NULL : r->method_names_str)
2875+
: NULL;
2876+
for (int j = 0; e && j < e->count; j++) {
2877+
int di = e->indices[j];
2878+
if (di < 0 || di >= cap || all_defs[di].lang != CBM_LANG_PYTHON)
2879+
continue;
2880+
if (!selected[di]) {
2881+
selected[di] = true;
2882+
(*total)++;
2883+
}
2884+
if (!queued[di] && pxc_is_py_reexport_record(&all_defs[di])) {
2885+
queued[di] = true;
2886+
work[n++] = di;
2887+
}
2888+
}
2889+
}
2890+
cbm_free(CBM_MEM_CLASS_EXTRACT, work);
2891+
cbm_free(CBM_MEM_CLASS_EXTRACT, queued);
2892+
}
2893+
27772894
CBMLSPDef *cbm_pxc_filter_defs_for_file(const CBMModuleDefIndex *idx, CBMLSPDef *all_defs,
27782895
CBMLanguage caller_lang, const char *caller_namespace,
27792896
const char *own_module, const char *const *imp_qns,
@@ -2806,6 +2923,9 @@ CBMLSPDef *cbm_pxc_filter_defs_for_file(const CBMModuleDefIndex *idx, CBMLSPDef
28062923
pxc_module_entry_t *e = (pxc_module_entry_t *)cbm_ht_get(idx->namespace_ht, namespace_key);
28072924
total += pxc_mark_entry_defs(selected, e, all_defs, caller_lang);
28082925
}
2926+
if (caller_lang == CBM_LANG_PYTHON) {
2927+
pxc_mark_reexport_targets(idx, selected, all_defs, &total);
2928+
}
28092929

28102930
if (total == 0) {
28112931
cbm_free(CBM_MEM_CLASS_EXTRACT, selected);

0 commit comments

Comments
 (0)