mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-12-03 04:27:30 +08:00
Merge pull request #1712 from benrfairless/feat/ruby-lsp-cross-file
feat(pipeline): wire Ruby cross-file LSP resolution
This commit is contained in:
@@ -288,6 +288,14 @@ typedef struct {
|
||||
* every other definition, and in every language outside
|
||||
* cbm_is_c_preprocessor_lang. */
|
||||
const char *variants;
|
||||
/* Ruby only: a `def self.m` / `class << self` method, which lives on the
|
||||
* singleton side of its class. False for every instance method and every
|
||||
* other language. */
|
||||
bool is_singleton;
|
||||
/* Ruby only, on a class/module def: the `include`/`prepend`/`extend`
|
||||
* macros of its body as "|"-separated "<k>:<Const>" pairs, k one of i, p,
|
||||
* e, in source order ("i:Auditable|e:Finders"). NULL when none. */
|
||||
const char *mixin_refs;
|
||||
/* Callable identity (#2061): offset of the signature suffix inside
|
||||
* qualified_name (base QN = the first qn_sig_off bytes); 0 = no suffix.
|
||||
* Always 0 until a language enables its callable_identity mode. */
|
||||
|
||||
@@ -7774,6 +7774,24 @@ static void push_make_rule_targets(CBMExtractCtx *ctx, TSNode node, const CBMDef
|
||||
}
|
||||
}
|
||||
|
||||
/* A Ruby `def self.m` (a singleton_method node) or a `def m` inside
|
||||
* `class << self` lives on the singleton side of its class. */
|
||||
static bool ruby_def_is_singleton(TSNode node) {
|
||||
if (strcmp(ts_node_type(node), "singleton_method") == 0) {
|
||||
return true;
|
||||
}
|
||||
for (TSNode p = ts_node_parent(node); !ts_node_is_null(p); p = ts_node_parent(p)) {
|
||||
const char *t = ts_node_type(p);
|
||||
if (strcmp(t, "singleton_class") == 0) {
|
||||
return true;
|
||||
}
|
||||
if (strcmp(t, "class") == 0 || strcmp(t, "module") == 0 || strcmp(t, "program") == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec) {
|
||||
CBMArena *a = ctx->arena;
|
||||
|
||||
@@ -7913,6 +7931,9 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
|
||||
strcmp(ts_node_type(node), "function_signature_item") == 0) {
|
||||
def.is_abstract = true;
|
||||
}
|
||||
if (ctx->language == CBM_LANG_RUBY) {
|
||||
def.is_singleton = ruby_def_is_singleton(node);
|
||||
}
|
||||
|
||||
// Parameters — use func_node (inner function for templates)
|
||||
TSNode params = configured && ctx->language == CBM_LANG_C
|
||||
@@ -9727,6 +9748,59 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
|
||||
emit_class_def(ctx, node, spec, kind, name);
|
||||
}
|
||||
|
||||
/* The `include`/`prepend`/`extend` macros of a Ruby class/module body as
|
||||
* "<k>:<Const>" pairs joined by '|' (k = i, p, e), in source order; NULL when
|
||||
* there are none. Only constant arguments count (`extend self` is not one),
|
||||
* and only bare or `self.` calls, as the per-file resolver reads them. */
|
||||
static const char *ruby_class_mixin_refs(CBMArena *a, TSNode node, const char *source) {
|
||||
TSNode body = ts_node_child_by_field_name(node, TS_FIELD("body"));
|
||||
TSNode scope = ts_node_is_null(body) ? node : body;
|
||||
const char *out = NULL;
|
||||
uint32_t n = ts_node_named_child_count(scope);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
TSNode call = ts_node_named_child(scope, i);
|
||||
if (strcmp(ts_node_type(call), "call") != 0) {
|
||||
continue;
|
||||
}
|
||||
TSNode meth = ts_node_child_by_field_name(call, TS_FIELD("method"));
|
||||
TSNode recv = ts_node_child_by_field_name(call, TS_FIELD("receiver"));
|
||||
TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
|
||||
if (ts_node_is_null(meth) || ts_node_is_null(args) ||
|
||||
(!ts_node_is_null(recv) && strcmp(ts_node_type(recv), "self") != 0)) {
|
||||
continue;
|
||||
}
|
||||
char *mname = cbm_node_text(a, meth, source);
|
||||
char kind;
|
||||
if (mname && strcmp(mname, "include") == 0) {
|
||||
kind = 'i';
|
||||
} else if (mname && strcmp(mname, "prepend") == 0) {
|
||||
kind = 'p';
|
||||
} else if (mname && strcmp(mname, "extend") == 0) {
|
||||
kind = 'e';
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
uint32_t ac = ts_node_named_child_count(args);
|
||||
for (uint32_t j = 0; j < ac; j++) {
|
||||
TSNode arg = ts_node_named_child(args, j);
|
||||
const char *at = ts_node_type(arg);
|
||||
if (strcmp(at, "constant") != 0 && strcmp(at, "scope_resolution") != 0) {
|
||||
continue;
|
||||
}
|
||||
char *ref = cbm_node_text(a, arg, source);
|
||||
if (!ref || !ref[0]) {
|
||||
continue;
|
||||
}
|
||||
while (ref[0] == ':') {
|
||||
ref++;
|
||||
}
|
||||
out = out ? cbm_arena_sprintf(a, "%s|%c:%s", out, kind, ref)
|
||||
: cbm_arena_sprintf(a, "%c:%s", kind, ref);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/* Emit the class-like def for `node` under `name`, plus its members (enum
|
||||
* members, methods, fields, class variables). Split from extract_class_def so a
|
||||
* C `typedef struct { ... } Name;` can name its anonymous struct after the
|
||||
@@ -9847,6 +9921,9 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
|
||||
}
|
||||
}
|
||||
def.base_classes = extract_base_classes(a, node, ctx->source, ctx->language);
|
||||
if (ctx->language == CBM_LANG_RUBY) {
|
||||
def.mixin_refs = ruby_class_mixin_refs(a, node, ctx->source);
|
||||
}
|
||||
// Go identity alias `type A = B` (#2033): keep the aliased type text so the
|
||||
// cross-file registries can set alias_of. A defined type `type A B` is a
|
||||
// type_spec and never gets here with this kind.
|
||||
@@ -10354,6 +10431,9 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
|
||||
strcmp(ts_node_type(child), "function_signature_item") == 0) {
|
||||
def.is_abstract = true;
|
||||
}
|
||||
if (ctx->language == CBM_LANG_RUBY) {
|
||||
def.is_singleton = ruby_def_is_singleton(child);
|
||||
}
|
||||
|
||||
TSNode params = find_function_params(child, ctx->language);
|
||||
if (!ts_node_is_null(params)) {
|
||||
|
||||
@@ -114,6 +114,11 @@ typedef struct {
|
||||
* by the registrar against def_module_qn like return_types. NULL = not an
|
||||
* identity alias. */
|
||||
const char *alias_target;
|
||||
/* Ruby only: singleton-side method (`def self.m`, `class << self`), and a
|
||||
* class/module's mixin macros as "|"-separated "<k>:<Const>" pairs (k = i,
|
||||
* p or e). See CBMDefinition. */
|
||||
bool is_singleton;
|
||||
const char *mixin_refs;
|
||||
/* Identities of the type names this def's declared types write, resolved
|
||||
* once in the scope of the file that wrote them (src/pipeline/type_scope.h):
|
||||
* "spelling=QN|spelling=QN". Read with cbm_type_scope_find (type_rep.h).
|
||||
|
||||
+703
-23
@@ -417,14 +417,130 @@ static const char *ruby_ivar_type(RubyLSPContext *ctx, const char *class_qn,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static RubyClassInfo *ruby_class_by_qn(RubyLSPContext *ctx, const char *qn) {
|
||||
/* ── shared (project-wide) class table ──────────────────────────── */
|
||||
|
||||
static unsigned ruby_hash(const char *s) {
|
||||
unsigned h = 2166136261u;
|
||||
for (; *s; s++)
|
||||
h = (h ^ (unsigned char)*s) * 16777619u;
|
||||
return h;
|
||||
}
|
||||
|
||||
static unsigned ruby_hash_root_path(const char *root, const char *path) {
|
||||
unsigned h = 2166136261u;
|
||||
for (const char *s = root; *s; s++)
|
||||
h = (h ^ (unsigned char)*s) * 16777619u;
|
||||
h = (h ^ 0x1fu) * 16777619u;
|
||||
for (const char *s = path; *s; s++)
|
||||
h = (h ^ (unsigned char)*s) * 16777619u;
|
||||
return h;
|
||||
}
|
||||
|
||||
/* Package root of a file, from its module QN ("proj.activemodel.test.models.
|
||||
* topic"): the QN prefix before the first directory named lib, app, test or
|
||||
* spec ("proj.activemodel"). "" when no such directory exists or it sits
|
||||
* directly under the project, so a single-package repository has one root.
|
||||
* Derived from the QN alone, so a full build and the incremental route (which
|
||||
* has only persisted defs) agree, with no file system access. */
|
||||
static const char *ruby_package_root(CBMArena *a, const char *module_qn) {
|
||||
if (!module_qn)
|
||||
return "";
|
||||
const char *p = strchr(module_qn, '.'); /* skip the project segment */
|
||||
while (p) {
|
||||
const char *seg = p + 1;
|
||||
const char *end = strchr(seg, '.');
|
||||
if (!end)
|
||||
break; /* the last segment is the file, not a directory */
|
||||
size_t n = (size_t)(end - seg);
|
||||
bool marker =
|
||||
(n == 3 && strncmp(seg, "lib", 3) == 0) || (n == 3 && strncmp(seg, "app", 3) == 0) ||
|
||||
(n == 4 && strncmp(seg, "test", 4) == 0) || (n == 4 && strncmp(seg, "spec", 4) == 0);
|
||||
if (marker)
|
||||
return p == strchr(module_qn, '.')
|
||||
? ""
|
||||
: cbm_arena_strndup(a, module_qn, (size_t)(p - module_qn));
|
||||
p = end;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/* Probe the QN index. */
|
||||
static const RubyClassInfo *ruby_shared_find(const CBMRubyCrossRegistry *sh, const int *slots,
|
||||
const char *qn) {
|
||||
if (!sh || !slots || !qn)
|
||||
return NULL;
|
||||
unsigned i = ruby_hash(qn) & sh->slot_mask;
|
||||
while (slots[i]) {
|
||||
CBM_LSP_WORK(1);
|
||||
const RubyClassInfo *c = &sh->classes[slots[i] - 1];
|
||||
if (strcmp(c->qn, qn) == 0)
|
||||
return c;
|
||||
i = (i + 1) & sh->slot_mask;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The group head declared in `root` for `path`, or NULL. */
|
||||
static const RubyClassInfo *ruby_shared_find_root_path(const CBMRubyCrossRegistry *sh,
|
||||
const char *root, const char *path) {
|
||||
if (!sh || !sh->by_path)
|
||||
return NULL;
|
||||
unsigned i = ruby_hash_root_path(root, path) & sh->slot_mask;
|
||||
while (sh->by_path[i]) {
|
||||
CBM_LSP_WORK(1);
|
||||
const RubyClassInfo *c = &sh->classes[sh->by_path[i] - 1];
|
||||
if (strcmp(c->root, root) == 0 && strcmp(c->path, path) == 0)
|
||||
return c;
|
||||
i = (i + 1) & sh->slot_mask;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The first group head with constant path `path` in any root; further roots
|
||||
* chain through path_next. */
|
||||
static const RubyClassInfo *ruby_shared_find_path(const CBMRubyCrossRegistry *sh,
|
||||
const char *path) {
|
||||
if (!sh || !sh->by_pp)
|
||||
return NULL;
|
||||
unsigned i = ruby_hash(path) & sh->slot_mask;
|
||||
while (sh->by_pp[i]) {
|
||||
CBM_LSP_WORK(1);
|
||||
const RubyClassInfo *c = &sh->classes[sh->by_pp[i] - 1];
|
||||
if (strcmp(c->path, path) == 0)
|
||||
return c;
|
||||
i = (i + 1) & sh->slot_mask;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* This file's own declaration of a class wins; otherwise the project table.
|
||||
* The own-file scan is bounded by the file, the shared probe is O(1). */
|
||||
static const RubyClassInfo *ruby_class_by_qn(RubyLSPContext *ctx, const char *qn) {
|
||||
if (!qn)
|
||||
return NULL;
|
||||
for (int i = 0; i < ctx->class_count; i++) {
|
||||
if (strcmp(ctx->classes[i].qn, qn) == 0)
|
||||
return &ctx->classes[i];
|
||||
}
|
||||
return NULL;
|
||||
return ruby_shared_find(ctx->shared, ctx->shared ? ctx->shared->by_qn : NULL, qn);
|
||||
}
|
||||
|
||||
/* The merged record for a class of the project table: the group head holding
|
||||
* every reopening's methods and the superclass. NULL when `qn` is not in it. */
|
||||
static const RubyClassInfo *ruby_shared_head(const CBMRubyCrossRegistry *sh, const char *qn) {
|
||||
const RubyClassInfo *c = ruby_shared_find(sh, sh ? sh->by_qn : NULL, qn);
|
||||
return c && c->group_head ? &sh->classes[c->group_head - 1] : NULL;
|
||||
}
|
||||
|
||||
/* The entry whose superclass applies to `qn`. A file that reopens a class
|
||||
* without `< Base` has an own entry with no superclass, so the merged record
|
||||
* backs it up: inherited methods and the model checks keep working there. */
|
||||
static const RubyClassInfo *ruby_effective_class(RubyLSPContext *ctx, const char *qn) {
|
||||
const RubyClassInfo *ci = ruby_class_by_qn(ctx, qn);
|
||||
if (ci && ci->superclass_ref)
|
||||
return ci;
|
||||
const RubyClassInfo *head = ruby_shared_head(ctx->shared, qn);
|
||||
return head ? head : ci;
|
||||
}
|
||||
|
||||
/* Conventional Rails superclasses for stdlib-seeded types the class table
|
||||
@@ -447,7 +563,7 @@ static const char *ruby_stdlib_superclass(const char *qn) {
|
||||
/* Superclass QN of a class: table entry first, then the conventional
|
||||
* stdlib chain above. */
|
||||
static const char *ruby_superclass_qn(RubyLSPContext *ctx, const char *class_qn) {
|
||||
RubyClassInfo *ci = ruby_class_by_qn(ctx, class_qn);
|
||||
const RubyClassInfo *ci = ruby_effective_class(ctx, class_qn);
|
||||
if (ci)
|
||||
return ci->superclass_qn;
|
||||
return ruby_stdlib_superclass(class_qn);
|
||||
@@ -455,13 +571,31 @@ static const char *ruby_superclass_qn(RubyLSPContext *ctx, const char *class_qn)
|
||||
|
||||
/* ── constant resolution ────────────────────────────────────────── */
|
||||
|
||||
/* Class-table probe by dotted path. */
|
||||
static const char *ruby_class_qn_by_path(RubyLSPContext *ctx, const char *path) {
|
||||
/* Class-table probe by dotted path: this file first, then the project.
|
||||
* *ambiguous is set when the project declares `path` under several package
|
||||
* roots, none of them the caller's: the constant exists, so Ruby would bind
|
||||
* it here and the caller must not fall through to an outer one. */
|
||||
static const char *ruby_class_qn_by_path(RubyLSPContext *ctx, const char *path, bool *ambiguous) {
|
||||
*ambiguous = false;
|
||||
for (int i = 0; i < ctx->class_count; i++) {
|
||||
if (strcmp(ctx->classes[i].path, path) == 0)
|
||||
return ctx->classes[i].qn;
|
||||
}
|
||||
return NULL;
|
||||
/* The caller's own package root wins. Failing that, a constant declared
|
||||
* in exactly one root resolves there; one declared under several roots,
|
||||
* none of them the caller's, stays unresolved rather than landing on
|
||||
* another package's same-named class. */
|
||||
const CBMRubyCrossRegistry *sh = ctx->shared;
|
||||
const RubyClassInfo *c =
|
||||
ruby_shared_find_root_path(sh, ctx->caller_root ? ctx->caller_root : "", path);
|
||||
if (c)
|
||||
return c->qn;
|
||||
c = ruby_shared_find_path(sh, path);
|
||||
if (c && c->path_next) {
|
||||
*ambiguous = true;
|
||||
return NULL;
|
||||
}
|
||||
return c ? c->qn : NULL;
|
||||
}
|
||||
|
||||
const char *ruby_resolve_constant(RubyLSPContext *ctx, const char *path) {
|
||||
@@ -481,9 +615,12 @@ const char *ruby_resolve_constant(RubyLSPContext *ctx, const char *path) {
|
||||
while (prefix[0]) {
|
||||
const char *cand =
|
||||
cbm_arena_sprintf(ctx->arena, "%s.%s", ruby_dotted(ctx->arena, prefix), path);
|
||||
const char *qn = ruby_class_qn_by_path(ctx, cand);
|
||||
bool ambiguous = false;
|
||||
const char *qn = ruby_class_qn_by_path(ctx, cand, &ambiguous);
|
||||
if (qn)
|
||||
return qn;
|
||||
if (ambiguous)
|
||||
return NULL; /* declared here in several other roots: fail closed */
|
||||
char *last_dot = strrchr(prefix, '.');
|
||||
if (last_dot)
|
||||
*last_dot = '\0';
|
||||
@@ -491,10 +628,15 @@ const char *ruby_resolve_constant(RubyLSPContext *ctx, const char *path) {
|
||||
prefix[0] = '\0';
|
||||
}
|
||||
}
|
||||
const char *qn = ruby_class_qn_by_path(ctx, path);
|
||||
bool ambiguous = false;
|
||||
const char *qn = ruby_class_qn_by_path(ctx, path, &ambiguous);
|
||||
if (qn)
|
||||
return qn;
|
||||
/* Stdlib types are keyed by their dotted bare path. */
|
||||
/* Stdlib types are keyed by their dotted bare path. A top-level constant
|
||||
* several roots declare and stdlib also has is a reopening of that one
|
||||
* global class (`class String` patched in two gems), so stdlib still
|
||||
* answers; without a stdlib type an ambiguous constant stays unresolved. */
|
||||
(void)ambiguous;
|
||||
const CBMRegisteredType *t = cbm_registry_lookup_type(ctx->registry, path);
|
||||
if (t)
|
||||
return t->qualified_name;
|
||||
@@ -506,17 +648,26 @@ const char *ruby_resolve_constant(RubyLSPContext *ctx, const char *path) {
|
||||
/* Depth cap shared by the instance/singleton lookup walks. */
|
||||
#define RUBY_LOOKUP_MAX_DEPTH CBM_LSP_MAX_LOOKUP_DEPTH
|
||||
|
||||
/* Own instance method of one exact class (no ancestry walk). */
|
||||
/* Own method of one exact class (no ancestry walk), instance or singleton
|
||||
* ("<qn>.self" receiver key). Against the project table every reopening's
|
||||
* methods are registered under the merged record's QN, so a class reopened in
|
||||
* any number of files is still one probe. */
|
||||
static const CBMRegisteredFunc *ruby_own_method(RubyLSPContext *ctx, const char *class_qn,
|
||||
const char *method_name, bool singleton) {
|
||||
const RubyClassInfo *head = ruby_shared_head(ctx->shared, class_qn);
|
||||
const char *owner = head ? head->qn : class_qn;
|
||||
const char *key = singleton ? cbm_arena_sprintf(ctx->arena, "%s.self", owner) : owner;
|
||||
return cbm_registry_lookup_method(ctx->registry, key, method_name);
|
||||
}
|
||||
|
||||
static const CBMRegisteredFunc *ruby_own_instance_method(RubyLSPContext *ctx, const char *class_qn,
|
||||
const char *method_name) {
|
||||
return cbm_registry_lookup_method(ctx->registry, class_qn, method_name);
|
||||
return ruby_own_method(ctx, class_qn, method_name, false);
|
||||
}
|
||||
|
||||
/* Own singleton method of one exact class ("<qn>.self" receiver key). */
|
||||
static const CBMRegisteredFunc *ruby_own_singleton_method(RubyLSPContext *ctx, const char *class_qn,
|
||||
const char *method_name) {
|
||||
const char *key = cbm_arena_sprintf(ctx->arena, "%s.self", class_qn);
|
||||
return cbm_registry_lookup_method(ctx->registry, key, method_name);
|
||||
return ruby_own_method(ctx, class_qn, method_name, true);
|
||||
}
|
||||
|
||||
/* Fail-closed ancestry. Every lookup below carries a `blocked` flag that is
|
||||
@@ -540,6 +691,12 @@ typedef struct {
|
||||
unsigned int stamp;
|
||||
} RubyLookupWalk;
|
||||
|
||||
typedef struct RubySharedMark {
|
||||
int index; /* shared class index + 1; 0 = empty slot */
|
||||
unsigned int seen_instance;
|
||||
unsigned int seen_singleton;
|
||||
} RubySharedMark;
|
||||
|
||||
static RubyLookupWalk ruby_walk_begin(RubyLSPContext *ctx) {
|
||||
if (++ctx->lookup_stamp == 0) {
|
||||
/* Wrapped: clear every mark so no class looks visited. */
|
||||
@@ -547,22 +704,74 @@ static RubyLookupWalk ruby_walk_begin(RubyLSPContext *ctx) {
|
||||
ctx->classes[i].seen_instance = 0;
|
||||
ctx->classes[i].seen_singleton = 0;
|
||||
}
|
||||
for (int i = 0; i < ctx->shared_mark_cap; i++) {
|
||||
ctx->shared_marks[i].seen_instance = 0;
|
||||
ctx->shared_marks[i].seen_singleton = 0;
|
||||
}
|
||||
ctx->lookup_stamp = 1;
|
||||
}
|
||||
RubyLookupWalk w = {false, ctx->lookup_stamp};
|
||||
return w;
|
||||
}
|
||||
|
||||
static unsigned ruby_mark_slot(int index, int cap) {
|
||||
return ((unsigned)index * 2654435761u) & (unsigned)(cap - 1);
|
||||
}
|
||||
|
||||
/* The mark pair for shared entry `index` (0-based), inserting it on first
|
||||
* touch. NULL only when the arena is exhausted. */
|
||||
static RubySharedMark *ruby_shared_mark(RubyLSPContext *ctx, int index) {
|
||||
if ((ctx->shared_mark_count + 1) * 2 > ctx->shared_mark_cap) {
|
||||
int newcap = ctx->shared_mark_cap ? ctx->shared_mark_cap * 2 : 16;
|
||||
RubySharedMark *nm =
|
||||
(RubySharedMark *)cbm_arena_alloc(ctx->arena, (size_t)newcap * sizeof(RubySharedMark));
|
||||
if (!nm)
|
||||
return NULL;
|
||||
memset(nm, 0, (size_t)newcap * sizeof(RubySharedMark));
|
||||
for (int i = 0; i < ctx->shared_mark_cap; i++) {
|
||||
if (!ctx->shared_marks[i].index)
|
||||
continue;
|
||||
unsigned j = ruby_mark_slot(ctx->shared_marks[i].index, newcap);
|
||||
while (nm[j].index)
|
||||
j = (j + 1) & (unsigned)(newcap - 1);
|
||||
nm[j] = ctx->shared_marks[i];
|
||||
}
|
||||
ctx->shared_marks = nm;
|
||||
ctx->shared_mark_cap = newcap;
|
||||
}
|
||||
unsigned j = ruby_mark_slot(index + 1, ctx->shared_mark_cap);
|
||||
while (ctx->shared_marks[j].index) {
|
||||
if (ctx->shared_marks[j].index == index + 1)
|
||||
return &ctx->shared_marks[j];
|
||||
j = (j + 1) & (unsigned)(ctx->shared_mark_cap - 1);
|
||||
}
|
||||
ctx->shared_marks[j].index = index + 1;
|
||||
ctx->shared_mark_count++;
|
||||
return &ctx->shared_marks[j];
|
||||
}
|
||||
|
||||
/* Marks `class_qn` searched on one side (instance or singleton) in this walk;
|
||||
* true when it already was. Only the file's class table is marked: mixins
|
||||
* and superclass refs are recorded for those classes alone, so a type outside
|
||||
* the table (a stdlib type) has nothing to expand and is never a repeat. */
|
||||
* true when it already was. This file's own entries carry their marks inline;
|
||||
* entries of the shared table are read-only, so theirs live in the per-file
|
||||
* hash. A type in neither (a stdlib type) has nothing to expand and is never
|
||||
* a repeat. */
|
||||
static bool ruby_walk_seen(RubyLSPContext *ctx, RubyLookupWalk *w, const char *class_qn,
|
||||
bool singleton) {
|
||||
RubyClassInfo *ci = ruby_class_by_qn(ctx, class_qn);
|
||||
if (!ci)
|
||||
return false;
|
||||
unsigned int *mark = singleton ? &ci->seen_singleton : &ci->seen_instance;
|
||||
unsigned int *mark = NULL;
|
||||
for (int i = 0; i < ctx->class_count && !mark; i++) {
|
||||
if (strcmp(ctx->classes[i].qn, class_qn) == 0)
|
||||
mark = singleton ? &ctx->classes[i].seen_singleton : &ctx->classes[i].seen_instance;
|
||||
}
|
||||
if (!mark) {
|
||||
const CBMRubyCrossRegistry *sh = ctx->shared;
|
||||
const RubyClassInfo *c = ruby_shared_find(sh, sh ? sh->by_qn : NULL, class_qn);
|
||||
if (!c)
|
||||
return false;
|
||||
RubySharedMark *m = ruby_shared_mark(ctx, (int)(c - sh->classes));
|
||||
if (!m)
|
||||
return false;
|
||||
mark = singleton ? &m->seen_singleton : &m->seen_instance;
|
||||
}
|
||||
if (*mark == w->stamp)
|
||||
return true;
|
||||
*mark = w->stamp;
|
||||
@@ -577,7 +786,7 @@ static const CBMRegisteredFunc *ruby_lookup_instance_method_depth(RubyLSPContext
|
||||
/* True when the class declares a superclass the finalize step could not
|
||||
* resolve (stdlib-seeded conventions like ApplicationRecord DO resolve). */
|
||||
static bool ruby_superclass_unresolved(RubyLSPContext *ctx, const char *class_qn) {
|
||||
RubyClassInfo *ci = ruby_class_by_qn(ctx, class_qn);
|
||||
const RubyClassInfo *ci = ruby_effective_class(ctx, class_qn);
|
||||
return ci && ci->superclass_ref && !ci->superclass_qn;
|
||||
}
|
||||
|
||||
@@ -600,6 +809,23 @@ static const CBMRegisteredFunc *ruby_lookup_in_mixins(RubyLSPContext *ctx, const
|
||||
if (f || w->blocked)
|
||||
return f;
|
||||
}
|
||||
/* A class reopened in other files: those files' mixins are in the merged
|
||||
* record, so a module one of them includes still shadows the superclass. */
|
||||
const CBMRubyCrossRegistry *sh = ctx->shared;
|
||||
const RubyClassInfo *head = ruby_shared_head(sh, owner_qn);
|
||||
for (int i = head ? head->mixin_n - 1 : -1; i >= 0; i--) {
|
||||
const RubyMixinInfo *mi = &sh->mixins[head->mixin_start + i];
|
||||
if (mi->kind != kind)
|
||||
continue;
|
||||
if (!mi->module_qn) {
|
||||
w->blocked = true;
|
||||
return NULL;
|
||||
}
|
||||
const CBMRegisteredFunc *f =
|
||||
ruby_lookup_instance_method_depth(ctx, mi->module_qn, method_name, depth + 1, w);
|
||||
if (f || w->blocked)
|
||||
return f;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -696,6 +922,32 @@ const CBMRegisteredFunc *ruby_lookup_singleton_method(RubyLSPContext *ctx, const
|
||||
|
||||
/* ── ActiveRecord model detection (typing special case) ─────────── */
|
||||
|
||||
/* Whether the finder rule (`User.find` -> User, relation chains keep the
|
||||
* model) also fires for a model declared in ANOTHER file, which is the usual
|
||||
* Rails layout. Off for now, so the rule keeps to models declared in the file
|
||||
* being resolved and nothing else changes. Across files it mostly meets
|
||||
* relation chains, and `Model.where(...).m` reaches the relation or the
|
||||
* model's class methods in Ruby, never an instance method `m`; a judged sample
|
||||
* of the cross-file edges came in below the 95% precision bar. Turn it on once
|
||||
* relations are typed apart from records. */
|
||||
static bool ruby_ar_finder_cross_file = false;
|
||||
|
||||
#ifdef CBM_ENABLE_TEST_SEAMS
|
||||
bool cbm_ruby_set_ar_finder_cross_file(bool on) {
|
||||
bool prev = ruby_ar_finder_cross_file;
|
||||
ruby_ar_finder_cross_file = on;
|
||||
return prev;
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool ruby_class_declared_here(const RubyLSPContext *ctx, const char *qn) {
|
||||
for (int i = 0; i < ctx->class_count; i++) {
|
||||
if (strcmp(ctx->classes[i].qn, qn) == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* True when class_qn's superclass chain reaches ActiveRecord::Base (or the
|
||||
* conventional ApplicationRecord intermediary, which per-file extraction
|
||||
* usually cannot see through). */
|
||||
@@ -704,7 +956,9 @@ static bool ruby_is_ar_model(RubyLSPContext *ctx, const char *class_qn, int dept
|
||||
return false;
|
||||
if (strcmp(class_qn, "ActiveRecord.Base") == 0 || strcmp(class_qn, "ApplicationRecord") == 0)
|
||||
return true;
|
||||
RubyClassInfo *ci = ruby_class_by_qn(ctx, class_qn);
|
||||
if (!ruby_ar_finder_cross_file && !ruby_class_declared_here(ctx, class_qn))
|
||||
return false;
|
||||
const RubyClassInfo *ci = ruby_effective_class(ctx, class_qn);
|
||||
if (ci && ci->superclass_ref &&
|
||||
(strcmp(ci->superclass_ref, "ApplicationRecord") == 0 ||
|
||||
strcmp(ci->superclass_ref, "ActiveRecord.Base") == 0))
|
||||
@@ -1822,3 +2076,429 @@ void cbm_run_ruby_lsp(CBMArena *arena, CBMFileResult *result, const char *source
|
||||
|
||||
cbm_arena_destroy(&idx_arena);
|
||||
}
|
||||
|
||||
/* ── cross-file: shared registry ────────────────────────────────── */
|
||||
|
||||
extern const TSLanguage *tree_sitter_ruby(void);
|
||||
|
||||
/* Raw spelling of a cross-file class def below its module: the part of its QN
|
||||
* after the module QN ("proj.app.models.user.Admin.Sub::User" with module
|
||||
* "proj.app.models.user" → "Admin.Sub::User"), i.e. the extractor's frames
|
||||
* joined by '.', each frame as written. NULL when the shape is odd. */
|
||||
static const char *ruby_cross_class_suffix(const CBMLSPDef *d) {
|
||||
if (!d->qualified_name)
|
||||
return NULL;
|
||||
if (d->def_module_qn && d->def_module_qn[0]) {
|
||||
size_t mlen = strlen(d->def_module_qn);
|
||||
if (strncmp(d->qualified_name, d->def_module_qn, mlen) == 0 &&
|
||||
d->qualified_name[mlen] == '.') {
|
||||
return d->qualified_name + mlen + 1;
|
||||
}
|
||||
}
|
||||
return d->short_name;
|
||||
}
|
||||
|
||||
/* The superclass a class def carries (first "|"-separated base, since Ruby
|
||||
* has single inheritance; a legacy "< Base" spelling is tolerated), in the
|
||||
* dotted form constant lookup uses. NULL when none. */
|
||||
static const char *ruby_def_superclass_ref(CBMArena *arena, const CBMLSPDef *d) {
|
||||
if (!d->embedded_types || !d->embedded_types[0])
|
||||
return NULL;
|
||||
const char *start = d->embedded_types;
|
||||
while (*start == '<' || *start == ' ' || *start == '\t')
|
||||
start++;
|
||||
const char *bar = strchr(start, '|');
|
||||
char *raw = bar ? cbm_arena_strndup(arena, start, (size_t)(bar - start))
|
||||
: cbm_arena_strdup(arena, start);
|
||||
if (!raw || !raw[0])
|
||||
return NULL;
|
||||
return ruby_dotted(arena, raw);
|
||||
}
|
||||
|
||||
/* Insert entry `idx` (already stored in sh->classes) into one hash index. */
|
||||
/* Insert entry `idx` into the QN index. */
|
||||
static void ruby_shared_insert_qn(CBMRubyCrossRegistry *sh, int idx) {
|
||||
unsigned i = ruby_hash(sh->classes[idx].qn) & sh->slot_mask;
|
||||
while (sh->by_qn[i])
|
||||
i = (i + 1) & sh->slot_mask;
|
||||
sh->by_qn[i] = idx + 1;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int head; /* owning merged record (table index) */
|
||||
int cls; /* declaring entry (table index) */
|
||||
const char *decl_qn; /* its QN, the canonical order key */
|
||||
int seq; /* position among that declaration's macros */
|
||||
RubyMixinKind kind;
|
||||
const char *ref; /* dotted constant */
|
||||
} RubyMixinTmp;
|
||||
|
||||
static int ruby_mixin_tmp_cmp(const void *a, const void *b) {
|
||||
const RubyMixinTmp *x = (const RubyMixinTmp *)a;
|
||||
const RubyMixinTmp *y = (const RubyMixinTmp *)b;
|
||||
if (x->head != y->head)
|
||||
return x->head < y->head ? -1 : 1;
|
||||
int c = strcmp(x->decl_qn, y->decl_qn);
|
||||
return c ? c : x->seq - y->seq;
|
||||
}
|
||||
|
||||
/* Resolve every declaration's include/prepend/extend macros once, from the
|
||||
* scope that declared them, into the merged record of their constant, so a
|
||||
* class's mixins are visible from every file. Order is canonical (declaring QN,
|
||||
* then source order), so it does not depend on def order. An unresolvable
|
||||
* module keeps module_qn NULL: lookups then fail closed. */
|
||||
static bool ruby_build_shared_mixins(CBMRubyCrossRegistry *sh, CBMArena *arena, RubyLSPContext *ctx,
|
||||
const char **decl_mixins, const char **body_nesting) {
|
||||
int total = 0;
|
||||
for (int i = 0; i < sh->class_count; i++) {
|
||||
if (!decl_mixins[i])
|
||||
continue;
|
||||
total++;
|
||||
for (const char *p = decl_mixins[i]; *p; p++)
|
||||
if (*p == '|')
|
||||
total++;
|
||||
}
|
||||
if (!total)
|
||||
return true;
|
||||
RubyMixinTmp *tmp = (RubyMixinTmp *)cbm_arena_alloc(arena, (size_t)total * sizeof(*tmp));
|
||||
if (!tmp)
|
||||
return false;
|
||||
int n = 0;
|
||||
for (int i = 0; i < sh->class_count; i++) {
|
||||
const char *p = decl_mixins[i];
|
||||
int seq = 0;
|
||||
while (p && *p) {
|
||||
const char *end = strchr(p, '|');
|
||||
size_t len = end ? (size_t)(end - p) : strlen(p);
|
||||
if (len > 2 && p[1] == ':' && (p[0] == 'i' || p[0] == 'p' || p[0] == 'e') &&
|
||||
n < total) {
|
||||
tmp[n].head = sh->classes[i].group_head - 1;
|
||||
tmp[n].cls = i;
|
||||
tmp[n].decl_qn = sh->classes[i].qn;
|
||||
tmp[n].seq = seq++;
|
||||
tmp[n].kind = p[0] == 'i' ? RUBY_MIXIN_INCLUDE
|
||||
: p[0] == 'p' ? RUBY_MIXIN_PREPEND
|
||||
: RUBY_MIXIN_EXTEND;
|
||||
tmp[n].ref = ruby_dotted(arena, cbm_arena_strndup(arena, p + 2, len - 2));
|
||||
n++;
|
||||
}
|
||||
p = end ? end + 1 : NULL;
|
||||
}
|
||||
}
|
||||
if (!n)
|
||||
return true;
|
||||
qsort(tmp, (size_t)n, sizeof(*tmp), ruby_mixin_tmp_cmp);
|
||||
sh->mixins = (RubyMixinInfo *)cbm_arena_alloc(arena, (size_t)n * sizeof(RubyMixinInfo));
|
||||
if (!sh->mixins)
|
||||
return false;
|
||||
for (int i = 0; i < n; i++) {
|
||||
RubyClassInfo *head = &sh->classes[tmp[i].head];
|
||||
if (head->mixin_n == 0)
|
||||
head->mixin_start = i;
|
||||
head->mixin_n++;
|
||||
RubyMixinInfo *mi = &sh->mixins[i];
|
||||
memset(mi, 0, sizeof(*mi));
|
||||
mi->owner_qn = head->qn;
|
||||
mi->module_ref = tmp[i].ref;
|
||||
mi->kind = tmp[i].kind;
|
||||
ctx->nesting = body_nesting[tmp[i].cls];
|
||||
ctx->caller_root = sh->classes[tmp[i].cls].root;
|
||||
mi->module_qn = ruby_resolve_constant(ctx, tmp[i].ref);
|
||||
}
|
||||
sh->mixin_count = n;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Slot of (root, path) in the by-path index, or -1 when absent. */
|
||||
static int ruby_shared_path_slot(const CBMRubyCrossRegistry *sh, const char *root,
|
||||
const char *path) {
|
||||
unsigned i = ruby_hash_root_path(root, path) & sh->slot_mask;
|
||||
while (sh->by_path[i]) {
|
||||
CBM_LSP_WORK(1);
|
||||
const RubyClassInfo *c = &sh->classes[sh->by_path[i] - 1];
|
||||
if (strcmp(c->root, root) == 0 && strcmp(c->path, path) == 0)
|
||||
return (int)i;
|
||||
i = (i + 1) & sh->slot_mask;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Descending QN order: the registry answers a repeated name with the entry
|
||||
* registered last, so registering the largest QN first makes the smallest win. */
|
||||
static int ruby_def_qn_cmp(const void *a, const void *b) {
|
||||
const CBMLSPDef *x = *(const CBMLSPDef *const *)a;
|
||||
const CBMLSPDef *y = *(const CBMLSPDef *const *)b;
|
||||
return strcmp(y->qualified_name, x->qualified_name);
|
||||
}
|
||||
|
||||
CBMRubyCrossRegistry *cbm_ruby_build_cross_registry(CBMArena *arena, CBMLSPDef *defs,
|
||||
int def_count) {
|
||||
if (!arena)
|
||||
return NULL;
|
||||
CBMRubyCrossRegistry *sh = (CBMRubyCrossRegistry *)cbm_arena_alloc(arena, sizeof(*sh));
|
||||
if (!sh)
|
||||
return NULL;
|
||||
memset(sh, 0, sizeof(*sh));
|
||||
cbm_registry_init(&sh->reg, arena);
|
||||
cbm_ruby_stdlib_register(&sh->reg, arena);
|
||||
|
||||
int max_classes = 0;
|
||||
for (int i = 0; i < def_count; i++) {
|
||||
const CBMLSPDef *d = &defs[i];
|
||||
if (d->lang == CBM_LANG_RUBY && d->label &&
|
||||
(strcmp(d->label, "Class") == 0 || strcmp(d->label, "Module") == 0))
|
||||
max_classes++;
|
||||
}
|
||||
unsigned slots = 16;
|
||||
while (slots < (unsigned)max_classes * 2u)
|
||||
slots <<= 1;
|
||||
sh->slot_mask = slots - 1;
|
||||
sh->by_qn = (int *)cbm_arena_alloc(arena, slots * sizeof(int));
|
||||
sh->by_path = (int *)cbm_arena_alloc(arena, slots * sizeof(int));
|
||||
sh->by_pp = (int *)cbm_arena_alloc(arena, slots * sizeof(int));
|
||||
sh->classes = (RubyClassInfo *)cbm_arena_alloc(arena, (size_t)(max_classes ? max_classes : 1) *
|
||||
sizeof(RubyClassInfo));
|
||||
const char **decl_nesting = (const char **)cbm_arena_alloc(
|
||||
arena, (size_t)(max_classes ? max_classes : 1) * sizeof(const char *));
|
||||
/* Per entry: the raw mixin macros and the full nesting its body resolves
|
||||
* them in. */
|
||||
const char **decl_mixins = (const char **)cbm_arena_alloc(
|
||||
arena, (size_t)(max_classes ? max_classes : 1) * sizeof(const char *));
|
||||
const char **body_nesting = (const char **)cbm_arena_alloc(
|
||||
arena, (size_t)(max_classes ? max_classes : 1) * sizeof(const char *));
|
||||
if (!sh->by_qn || !sh->by_path || !sh->by_pp || !sh->classes || !decl_nesting || !decl_mixins ||
|
||||
!body_nesting)
|
||||
return NULL;
|
||||
memset(sh->by_qn, 0, slots * sizeof(int));
|
||||
memset(sh->by_path, 0, slots * sizeof(int));
|
||||
memset(sh->by_pp, 0, slots * sizeof(int));
|
||||
|
||||
/* Ruby constants live in one global namespace, so every project class is
|
||||
* a resolution candidate. Pass A: one entry per declaration, and per
|
||||
* constant path the entry with the smallest QN becomes the group head, so
|
||||
* the head does not depend on def order (a full build and the incremental
|
||||
* closure route order defs differently). */
|
||||
for (int i = 0; i < def_count; i++) {
|
||||
CBMLSPDef *d = &defs[i];
|
||||
if (!d->qualified_name || !d->short_name || !d->label || d->lang != CBM_LANG_RUBY)
|
||||
continue;
|
||||
if (strcmp(d->label, "Class") != 0 && strcmp(d->label, "Module") != 0)
|
||||
continue;
|
||||
const char *suffix = ruby_cross_class_suffix(d);
|
||||
if (!suffix)
|
||||
continue;
|
||||
if (ruby_shared_find(sh, sh->by_qn, d->qualified_name))
|
||||
continue; /* the same class body seen again — one entry per QN */
|
||||
const char *path = ruby_dotted(arena, suffix);
|
||||
int n = sh->class_count++;
|
||||
RubyClassInfo *ci = &sh->classes[n];
|
||||
memset(ci, 0, sizeof(*ci));
|
||||
ci->path = path;
|
||||
ci->root = ruby_package_root(arena, d->def_module_qn);
|
||||
ci->qn = d->qualified_name;
|
||||
ci->is_module = strcmp(d->label, "Module") == 0;
|
||||
if (!ci->is_module)
|
||||
ci->superclass_ref = ruby_def_superclass_ref(arena, d);
|
||||
/* Declaring scope = every frame but the last; the superclass is
|
||||
* resolved from there, as in the per-file pass. */
|
||||
const char *last = strrchr(suffix, '.');
|
||||
decl_nesting[n] = last ? cbm_arena_strndup(arena, suffix, (size_t)(last - suffix)) : "";
|
||||
decl_mixins[n] = d->mixin_refs;
|
||||
body_nesting[n] = suffix;
|
||||
ruby_shared_insert_qn(sh, n);
|
||||
int slot = ruby_shared_path_slot(sh, ci->root, path);
|
||||
if (slot < 0) {
|
||||
unsigned h = ruby_hash_root_path(ci->root, path) & sh->slot_mask;
|
||||
while (sh->by_path[h])
|
||||
h = (h + 1) & sh->slot_mask;
|
||||
sh->by_path[h] = n + 1;
|
||||
} else if (strcmp(ci->qn, sh->classes[sh->by_path[slot] - 1].qn) < 0)
|
||||
sh->by_path[slot] = n + 1;
|
||||
}
|
||||
for (int i = 0; i < sh->class_count; i++) {
|
||||
RubyClassInfo *ci = &sh->classes[i];
|
||||
ci->group_head = sh->by_path[ruby_shared_path_slot(sh, ci->root, ci->path)];
|
||||
}
|
||||
/* Heads sharing a constant path across package roots, chained from one
|
||||
* by_pp entry, so a lookup can tell "one root" from "several". A later
|
||||
* head is linked right after the first one, O(1) per head: the only
|
||||
* reader asks whether a second head exists, so the order behind the
|
||||
* first does not matter (no tail walk, per the LSP registry checklist). */
|
||||
for (int i = 0; i < sh->class_count; i++) {
|
||||
if (sh->classes[i].group_head != i + 1)
|
||||
continue;
|
||||
unsigned h = ruby_hash(sh->classes[i].path) & sh->slot_mask;
|
||||
bool placed = false;
|
||||
while (sh->by_pp[h]) {
|
||||
RubyClassInfo *first = &sh->classes[sh->by_pp[h] - 1];
|
||||
if (strcmp(first->path, sh->classes[i].path) == 0) {
|
||||
sh->classes[i].path_next = first->path_next;
|
||||
first->path_next = i + 1;
|
||||
placed = true;
|
||||
break;
|
||||
}
|
||||
h = (h + 1) & sh->slot_mask;
|
||||
}
|
||||
if (!placed)
|
||||
sh->by_pp[h] = i + 1;
|
||||
}
|
||||
|
||||
/* Pass B: methods, in a fixed QN order so a name defined by two reopenings
|
||||
* resolves to the smallest QN whatever the def order. Each registers under its
|
||||
* group head's QN (the merged record), so a lookup is one probe however
|
||||
* many files reopen the class; the def keeps its own file's QN. A method
|
||||
* registers on exactly one key: the instance key, or "<qn>.self" for a
|
||||
* `def self.m` / `class << self` method. Registering on both would let an
|
||||
* instance call hit a singleton method of the same name and stop there,
|
||||
* never reaching the inherited instance method Ruby would call. */
|
||||
CBMLSPDef **fdefs = (CBMLSPDef **)cbm_arena_alloc(arena, (size_t)(def_count ? def_count : 1) *
|
||||
sizeof(CBMLSPDef *));
|
||||
if (!fdefs)
|
||||
return NULL;
|
||||
int fcount = 0;
|
||||
for (int i = 0; i < def_count; i++) {
|
||||
CBMLSPDef *d = &defs[i];
|
||||
if (d->qualified_name && d->short_name && d->label && d->lang == CBM_LANG_RUBY &&
|
||||
(strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0))
|
||||
fdefs[fcount++] = d;
|
||||
}
|
||||
qsort(fdefs, (size_t)fcount, sizeof(fdefs[0]), ruby_def_qn_cmp);
|
||||
for (int i = 0; i < fcount; i++) {
|
||||
const CBMLSPDef *d = fdefs[i];
|
||||
const RubyClassInfo *owner = ruby_shared_head(sh, d->receiver_type);
|
||||
const char *receiver = owner ? owner->qn : d->receiver_type;
|
||||
CBMRegisteredFunc rf;
|
||||
memset(&rf, 0, sizeof(rf));
|
||||
rf.min_params = -1;
|
||||
rf.qualified_name = d->qualified_name;
|
||||
rf.short_name = d->short_name;
|
||||
rf.receiver_type =
|
||||
receiver && d->is_singleton ? cbm_arena_sprintf(arena, "%s.self", receiver) : receiver;
|
||||
const CBMType **rets =
|
||||
(const CBMType **)cbm_arena_alloc(arena, 2 * sizeof(const CBMType *));
|
||||
if (rets) {
|
||||
rets[0] = cbm_type_unknown();
|
||||
rets[1] = NULL;
|
||||
}
|
||||
rf.signature = cbm_type_func(arena, NULL, NULL, rets);
|
||||
cbm_registry_add_func(&sh->reg, rf);
|
||||
}
|
||||
|
||||
cbm_registry_finalize(&sh->reg);
|
||||
sh->reg.read_only = true; /* sealed: shared across resolve workers */
|
||||
|
||||
/* Resolve each superclass against the complete table, once, from the
|
||||
* scope that declared it. */
|
||||
RubyLSPContext ctx;
|
||||
ruby_lsp_init(&ctx, arena, "", 0, &sh->reg, NULL, NULL);
|
||||
ctx.shared = sh;
|
||||
for (int i = 0; i < sh->class_count; i++) {
|
||||
RubyClassInfo *ci = &sh->classes[i];
|
||||
if (!ci->superclass_ref)
|
||||
continue;
|
||||
ctx.nesting = decl_nesting[i];
|
||||
ctx.caller_root = ci->root;
|
||||
ci->superclass_qn = ruby_resolve_constant(&ctx, ci->superclass_ref);
|
||||
}
|
||||
/* The merged record's superclass comes from the smallest-QN declaration
|
||||
* that has one, when the head itself declares none. */
|
||||
int *provider = (int *)cbm_arena_alloc(arena, (size_t)(sh->class_count ? sh->class_count : 1) *
|
||||
sizeof(int));
|
||||
if (!provider)
|
||||
return NULL;
|
||||
for (int i = 0; i < sh->class_count; i++)
|
||||
provider[i] = -1;
|
||||
for (int i = 0; i < sh->class_count; i++) {
|
||||
const RubyClassInfo *ci = &sh->classes[i];
|
||||
int h = ci->group_head - 1;
|
||||
if (!ci->superclass_ref || sh->classes[h].superclass_ref)
|
||||
continue;
|
||||
if (provider[h] < 0 || strcmp(ci->qn, sh->classes[provider[h]].qn) < 0)
|
||||
provider[h] = i;
|
||||
}
|
||||
for (int h = 0; h < sh->class_count; h++) {
|
||||
if (provider[h] < 0)
|
||||
continue;
|
||||
sh->classes[h].superclass_ref = sh->classes[provider[h]].superclass_ref;
|
||||
sh->classes[h].superclass_qn = sh->classes[provider[h]].superclass_qn;
|
||||
}
|
||||
if (!ruby_build_shared_mixins(sh, arena, &ctx, decl_mixins, body_nesting))
|
||||
return NULL;
|
||||
return sh;
|
||||
}
|
||||
|
||||
/* ── cross-file: per-file entry ─────────────────────────────────── */
|
||||
|
||||
void cbm_run_ruby_lsp_cross_with_registry(CBMArena *arena, const char *source, int source_len,
|
||||
const char *module_qn, const CBMRubyCrossRegistry *shared,
|
||||
const char **import_names, const char **import_qns,
|
||||
int import_count, TSTree *cached_tree,
|
||||
CBMResolvedCallArray *out) {
|
||||
if (!arena || !source || !out || !shared)
|
||||
return;
|
||||
|
||||
RubyLSPContext ctx;
|
||||
ruby_lsp_init(&ctx, arena, source, source_len, &shared->reg, module_qn, out);
|
||||
ctx.shared = shared;
|
||||
ctx.caller_root = ruby_package_root(arena, module_qn);
|
||||
ctx.import_names = import_names;
|
||||
ctx.import_qns = import_qns;
|
||||
ctx.import_count = import_count;
|
||||
|
||||
/* Parse if the pipeline didn't hand us a cached tree. */
|
||||
TSTree *tree = cached_tree;
|
||||
bool owns_tree = false;
|
||||
if (!tree) {
|
||||
TSParser *parser = ts_parser_new();
|
||||
if (!parser)
|
||||
return;
|
||||
const TSLanguage *lang = tree_sitter_ruby();
|
||||
if (!ts_parser_set_language(parser, lang)) {
|
||||
ts_parser_delete(parser);
|
||||
return;
|
||||
}
|
||||
tree = ts_parser_parse_string(parser, NULL, source, (uint32_t)source_len);
|
||||
ts_parser_delete(parser);
|
||||
if (!tree)
|
||||
return;
|
||||
owns_tree = true;
|
||||
}
|
||||
TSNode root = ts_tree_root_node(tree);
|
||||
|
||||
/* PASS 1 over this file's AST only: its own classes, mixins and
|
||||
* nesting-packed superclass refs. build_reg stays NULL — methods and every
|
||||
* other file's classes come from the shared registry, which is never
|
||||
* written. */
|
||||
ruby_pass1_scan(&ctx, root);
|
||||
ruby_finalize_refs(&ctx);
|
||||
|
||||
/* PASS 1.5 + PASS 2. */
|
||||
ctx.nesting = "";
|
||||
ctx.enclosing_class_qn = NULL;
|
||||
ruby_ivar_scan(&ctx, root);
|
||||
|
||||
ctx.nesting = "";
|
||||
ctx.enclosing_class_qn = NULL;
|
||||
ctx.enclosing_func_qn = NULL;
|
||||
ctx.in_singleton_method = false;
|
||||
ruby_resolve_calls_in_node(&ctx, root);
|
||||
|
||||
if (owns_tree)
|
||||
ts_tree_delete(tree);
|
||||
}
|
||||
|
||||
void cbm_run_ruby_lsp_cross(CBMArena *arena, const char *source, int source_len,
|
||||
const char *module_qn, CBMLSPDef *defs, int def_count,
|
||||
const char **import_names, const char **import_qns, int import_count,
|
||||
TSTree *cached_tree, CBMResolvedCallArray *out) {
|
||||
if (!arena || !source || !out)
|
||||
return;
|
||||
CBMArena build;
|
||||
cbm_arena_init(&build);
|
||||
CBMRubyCrossRegistry *sh = cbm_ruby_build_cross_registry(&build, defs, def_count);
|
||||
if (sh) {
|
||||
cbm_run_ruby_lsp_cross_with_registry(arena, source, source_len, module_qn, sh, import_names,
|
||||
import_qns, import_count, cached_tree, out);
|
||||
}
|
||||
cbm_arena_destroy(&build);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "scope.h"
|
||||
#include "type_registry.h"
|
||||
#include "../cbm.h"
|
||||
#include "go_lsp.h" /* CBMLSPDef reused across languages */
|
||||
|
||||
/* Ruby mixin kinds. Lookup order for an instance method on class C:
|
||||
* prepended modules (last prepended first) → C's own methods →
|
||||
@@ -33,12 +34,34 @@ typedef struct {
|
||||
const char *superclass_ref; /* raw dotted superclass spelling, or NULL */
|
||||
const char *superclass_qn; /* resolved QN (table/stdlib), or NULL */
|
||||
bool is_module; /* `module` (mixin source) vs `class` */
|
||||
/* Shared (project-wide) table only. A class reopened in another file has
|
||||
* a different QN there (the QN carries the file's module QN) but the same
|
||||
* constant path. All declarations of one path share one merged record: the
|
||||
* entry with the smallest QN (the group head), which holds every
|
||||
* reopening's methods and the superclass. `group_head` is the head's table
|
||||
* index + 1 (the entry's own index + 1 for the head itself); 0 in a
|
||||
* per-file table. */
|
||||
int group_head;
|
||||
/* Shared table only: the package root this declaration sits in ("" when
|
||||
* it has none; see ruby_package_root), and, on a head, the next head
|
||||
* (table index + 1) with the same constant path in another root. Two
|
||||
* packages of one repository can each declare their own `Topic`; the
|
||||
* merged record is per (root, path) so they are never pooled. */
|
||||
const char *root;
|
||||
int path_next;
|
||||
/* Head entries only: this constant's include/prepend/extend records, a
|
||||
* range of the registry's `mixins` array (all reopenings, in declaring-QN
|
||||
* then source order). */
|
||||
int mixin_start;
|
||||
int mixin_n;
|
||||
/* Method-lookup visit marks: the stamp of the last walk that searched
|
||||
* this class on the instance / singleton side (ruby_walk_seen). */
|
||||
unsigned int seen_instance;
|
||||
unsigned int seen_singleton;
|
||||
} RubyClassInfo;
|
||||
|
||||
struct CBMRubyCrossRegistry;
|
||||
|
||||
/* One `include`/`prepend`/`extend` record: owner class QN → module ref. */
|
||||
typedef struct {
|
||||
const char *owner_qn; /* class/module whose body held the call */
|
||||
@@ -88,7 +111,15 @@ typedef struct {
|
||||
* [A::B] only, so A's constants are NOT probed. */
|
||||
const char *nesting;
|
||||
|
||||
/* Class/module table (pass 1). */
|
||||
/* Project-wide read-only class table + method registry (cross-file
|
||||
* resolution). NULL for per-file extraction. Consulted after this file's
|
||||
* own table, so a class declared here always wins. */
|
||||
const struct CBMRubyCrossRegistry *shared;
|
||||
/* Package root of the file being resolved ("" = none); picks which
|
||||
* root's classes a shared-table constant lookup prefers. */
|
||||
const char *caller_root;
|
||||
|
||||
/* Class/module table (pass 1) — this file's own declarations only. */
|
||||
RubyClassInfo *classes;
|
||||
int class_count;
|
||||
int class_cap;
|
||||
@@ -137,6 +168,14 @@ typedef struct {
|
||||
* so marks left by earlier walks never match. */
|
||||
unsigned int lookup_stamp;
|
||||
|
||||
/* Visit marks for entries of the shared (read-only) class table. The table
|
||||
* is read by every resolve worker at once, so a walk never marks it; its
|
||||
* marks live here, in an open-addressing hash keyed by the shared index and
|
||||
* sized by what this file touches. */
|
||||
struct RubySharedMark *shared_marks;
|
||||
int shared_mark_cap;
|
||||
int shared_mark_count;
|
||||
|
||||
/* Debug mode (CBM_LSP_DEBUG env). */
|
||||
bool debug;
|
||||
} RubyLSPContext;
|
||||
@@ -173,4 +212,59 @@ void cbm_run_ruby_lsp(CBMArena *arena, CBMFileResult *result, const char *source
|
||||
* (ActiveRecord/ActiveSupport/ActionController) into a registry. */
|
||||
void cbm_ruby_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena);
|
||||
|
||||
/* --- Cross-file LSP resolution ---
|
||||
*
|
||||
* Ruby constants live in one global namespace and Rails/Zeitwerk autoloads
|
||||
* them with no require, so a file may name any class in the project. The
|
||||
* project's definitions are therefore indexed ONCE (cbm_ruby_build_cross_
|
||||
* registry) into a sealed, read-only registry: a finalized method registry
|
||||
* plus a hashed class table. Each file then resolves against it with O(1)
|
||||
* lookups and builds nothing proportional to the project. */
|
||||
typedef struct CBMRubyCrossRegistry {
|
||||
CBMTypeRegistry reg; /* stdlib + every project method; finalized, sealed */
|
||||
RubyClassInfo *classes;
|
||||
int class_count;
|
||||
/* Open-addressing hash indexes over `classes` (values are index + 1, 0 =
|
||||
* empty slot): by QN, and by dotted constant path (group head only). */
|
||||
int *by_qn;
|
||||
int *by_path; /* (package root, path) -> group head */
|
||||
int *by_pp; /* path alone -> first head; the rest chain via path_next */
|
||||
unsigned slot_mask;
|
||||
RubyMixinInfo *mixins; /* owner_qn = the merged record's QN; grouped by head */
|
||||
int mixin_count;
|
||||
} CBMRubyCrossRegistry;
|
||||
|
||||
#ifdef CBM_ENABLE_TEST_SEAMS
|
||||
/* Test seam: turn the cross-file half of the ActiveRecord finder rule on or
|
||||
* off (see ruby_ar_finder_cross_file in ruby_lsp.c). Returns the previous
|
||||
* setting, so a test restores what it found rather than a fixed value. */
|
||||
bool cbm_ruby_set_ar_finder_cross_file(bool on);
|
||||
#endif
|
||||
|
||||
/* Build the shared registry from every project def. Strings are borrowed from
|
||||
* `defs` (as the other languages' cross registries do) or copied into `arena`.
|
||||
* Returns NULL on allocation failure; callers then fall back to the
|
||||
* per-file path. */
|
||||
CBMRubyCrossRegistry *cbm_ruby_build_cross_registry(CBMArena *arena, CBMLSPDef *defs,
|
||||
int def_count);
|
||||
|
||||
/* Resolve one file against a prebuilt shared registry. Reads `shared` only;
|
||||
* everything it allocates lives in `arena`. */
|
||||
void cbm_run_ruby_lsp_cross_with_registry(CBMArena *arena, const char *source, int source_len,
|
||||
const char *module_qn, const CBMRubyCrossRegistry *shared,
|
||||
const char **import_names, const char **import_qns,
|
||||
int import_count,
|
||||
TSTree *cached_tree, /* NULL = parse internally */
|
||||
CBMResolvedCallArray *out);
|
||||
|
||||
/* Convenience for callers with a def list but no prebuilt registry (the
|
||||
* per-file fallback path and tests): builds a registry in a scratch arena for
|
||||
* this one call. Cost is proportional to the def list, so the pipeline uses
|
||||
* the prebuilt path instead. */
|
||||
void cbm_run_ruby_lsp_cross(CBMArena *arena, const char *source, int source_len,
|
||||
const char *module_qn, CBMLSPDef *defs, int def_count,
|
||||
const char **import_names, const char **import_qns, int import_count,
|
||||
TSTree *cached_tree, /* NULL = parse internally */
|
||||
CBMResolvedCallArray *out);
|
||||
|
||||
#endif /* CBM_LSP_RUBY_LSP_H */
|
||||
|
||||
@@ -282,6 +282,7 @@ static void cr_walk_def(cr_ctx_t *c, CBMDefinition *d) {
|
||||
cr_str(c, &d->decl_namespace);
|
||||
cr_str(c, &d->variant_key);
|
||||
cr_str(c, &d->alias_target);
|
||||
cr_str(c, &d->mixin_refs);
|
||||
/* The type-facts record: one record, then its strings. */
|
||||
cr_array(c, (void **)&d->type_facts, d->type_facts ? 1 : 0, sizeof(CBMDefTypeFacts));
|
||||
CBMDefTypeFacts *facts = (CBMDefTypeFacts *)d->type_facts;
|
||||
|
||||
@@ -847,6 +847,8 @@ static char *surface_file_to_json(const CBMFileResult *result, CBMLanguage langu
|
||||
yyjson_mut_obj_add_bool(doc, o, "ab", d->is_abstract);
|
||||
add_str_array_or_null(doc, o, "dec", d->decorators, -1);
|
||||
add_str_or_null(doc, o, "al", d->alias_target);
|
||||
yyjson_mut_obj_add_bool(doc, o, "sg", d->is_singleton);
|
||||
add_str_or_null(doc, o, "mx", d->mixin_refs);
|
||||
/* Stored for the routes that resolve against this row without
|
||||
* re-extracting it; never hashed (see SURFACE_CODEC_VERSION). */
|
||||
if (identities && d->type_scope) {
|
||||
@@ -1262,6 +1264,8 @@ int cbm_lsp_surface_defs_from_json(CBMArena *arena, const char *defs_json, CBMLS
|
||||
int dec_count = 0;
|
||||
d->decorators = arena_str_array(arena, yyjson_obj_get(o, "dec"), true, &dec_count);
|
||||
d->alias_target = arena_str_or_null(arena, yyjson_obj_get(o, "al"));
|
||||
d->is_singleton = yyjson_get_bool(yyjson_obj_get(o, "sg"));
|
||||
d->mixin_refs = arena_str_or_null(arena, yyjson_obj_get(o, "mx"));
|
||||
d->type_scope = arena_str_or_null(arena, yyjson_obj_get(o, "ty"));
|
||||
if (!d->qualified_name || !d->short_name || !d->label) {
|
||||
/* A def the writer could not have produced: qn/sn/label are
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "lsp/php_lsp.h"
|
||||
#include "lsp/java_lsp.h"
|
||||
#include "lsp/kotlin_lsp.h"
|
||||
#include "lsp/ruby_lsp.h"
|
||||
#include "lsp/rust_lsp.h"
|
||||
#include "lsp/rust_cargo.h"
|
||||
#include "graph_buffer/graph_buffer.h"
|
||||
@@ -837,6 +838,10 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
|
||||
dst->trait_qn = src->impl_trait ? cbm_arena_strdup(arena, src->impl_trait) : NULL;
|
||||
dst->is_abstract = src->is_abstract;
|
||||
}
|
||||
if (lang == CBM_LANG_RUBY) {
|
||||
dst->is_singleton = src->is_singleton;
|
||||
dst->mixin_refs = src->mixin_refs ? cbm_arena_strdup(arena, src->mixin_refs) : NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2504,6 +2509,33 @@ static bool pxc_rust_run_defs(const pxc_file_t *f, pxc_walk_t *w) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ── Ruby ── */
|
||||
|
||||
/* The shared Ruby registry is a CBMRubyCrossRegistry (method registry plus a
|
||||
* hashed class table); its CBMTypeRegistry is the first member, so that is
|
||||
* what the engine slot holds and the walk converts it back. Sealed and
|
||||
* read-only: nothing is built per file, so the overlay the dispatcher chains
|
||||
* to it goes unused. */
|
||||
static CBMTypeRegistry *pxc_ruby_build_registry(CBMArena *arena, CBMLSPDef *defs, int def_count) {
|
||||
CBMRubyCrossRegistry *r = cbm_ruby_build_cross_registry(arena, defs, def_count);
|
||||
return r ? &r->reg : NULL;
|
||||
}
|
||||
|
||||
static bool pxc_ruby_run_registry(const pxc_file_t *f, pxc_walk_t *w) {
|
||||
_Static_assert(offsetof(CBMRubyCrossRegistry, reg) == 0, "engine slot is the registry");
|
||||
cbm_run_ruby_lsp_cross_with_registry(w->scratch, f->source, f->source_len, f->module_qn,
|
||||
(const CBMRubyCrossRegistry *)w->shared, f->imp_keys,
|
||||
f->imp_vals, f->imp_count, f->result->cached_tree, w->out);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool pxc_ruby_run_defs(const pxc_file_t *f, pxc_walk_t *w) {
|
||||
cbm_run_ruby_lsp_cross(w->scratch, f->source, f->source_len, f->module_qn, w->defs,
|
||||
w->def_count, f->imp_keys, f->imp_vals, f->imp_count,
|
||||
f->result->cached_tree, w->out);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The rows, in the order their shared registries are built. A row's index is
|
||||
* its slot in CBMCrossLspRegistries. */
|
||||
static const pxc_engine_t pxc_engines[] = {
|
||||
@@ -2559,6 +2591,16 @@ static const pxc_engine_t pxc_engines[] = {
|
||||
.build_registry = cbm_java_build_cross_registry,
|
||||
.run_with_registry = pxc_java_run_registry,
|
||||
.run_one = pxc_java_run_defs},
|
||||
/* Ruby constants live in one global namespace and Rails/Zeitwerk autoloads
|
||||
* them without require statements, so an own+imports filter would starve
|
||||
* almost all cross-file resolution: the walk always sees the whole project. */
|
||||
{.name = "ruby",
|
||||
.langs = {CBM_LANG_RUBY},
|
||||
.lang_count = 1,
|
||||
.build_registry = pxc_ruby_build_registry,
|
||||
.run_with_registry = pxc_ruby_run_registry,
|
||||
.run_one = pxc_ruby_run_defs,
|
||||
.unfiltered_defs = true},
|
||||
{.name = "php", .langs = {CBM_LANG_PHP}, .lang_count = 1, .run_one = pxc_php_run_defs},
|
||||
{.name = "kotlin", .langs = {CBM_LANG_KOTLIN}, .lang_count = 1, .run_one = pxc_kotlin_run_defs},
|
||||
{.name = "rust",
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
*
|
||||
* The languages covered are the rows of one engine table (pass_lsp_cross.c
|
||||
* pxc_engines): Go, C/C++/CUDA, Python, TypeScript/JavaScript/JSX/TSX, PHP,
|
||||
* C#, Java, Kotlin and Rust. Anything else short-circuits via
|
||||
* C#, Ruby, Java, Kotlin and Rust. Anything else short-circuits via
|
||||
* cbm_pxc_has_cross_lsp.
|
||||
*
|
||||
* Previously this work ran as a separate sequential pipeline pass
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "lsp/ts_lsp.h" /* cbm_ts_build_cross_registry / cbm_run_ts_lsp_cross_with_registry */
|
||||
#include "lsp/java_lsp.h" /* cbm_java_build_cross_registry / cbm_run_java_lsp_cross_with_registry */
|
||||
#include "lsp/rust_lsp.h" /* cbm_rust_build_cross_registry / cbm_run_rust_lsp_cross_with_registry */
|
||||
#include "lsp/ruby_lsp.h" /* cbm_ruby_build_cross_registry; _cross is the per-file fallback */
|
||||
#include "pipeline/pipeline_internal.h"
|
||||
#include "pipeline/type_scope.h" /* cbm_type_scope_file_t */
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -2630,7 +2630,7 @@ static void log_result_census(const char *tag, CBMFileResult **cache, int file_c
|
||||
census_len(def->client_route_path) + census_len(def->impl_trait) +
|
||||
census_len(def->http_base_url) + census_list(def->extra_props) +
|
||||
census_len(def->decl_namespace) + census_len(def->variant_key) +
|
||||
census_len(def->alias_target);
|
||||
census_len(def->alias_target) + census_len(def->mixin_refs);
|
||||
if (def->type_facts) {
|
||||
str_def_misc += sizeof(*def->type_facts) +
|
||||
census_len(def->type_facts->type_params) +
|
||||
|
||||
@@ -2093,7 +2093,11 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed
|
||||
* path, filtered through module_def_index, is the SAME
|
||||
* pre-Tier-2 resolution code the full pipeline still uses
|
||||
* for languages without a shared registry, so convergence
|
||||
* is unaffected; only the amortization strategy changes. */
|
||||
* is unaffected; only the amortization strategy changes.
|
||||
* Ruby's fallback (cbm_run_ruby_lsp_cross) is exempt from the
|
||||
* def filter and so rebuilds the whole project table for each
|
||||
* changed Ruby file: at most CLOSURE_BUDGET_FLOOR_FILES
|
||||
* builds here, which is bounded and left as is. */
|
||||
if (ci > CLOSURE_BUDGET_FLOOR_FILES) {
|
||||
cbm_pxc_build_cross_registries(&closure->arena, all_defs, all_def_count,
|
||||
&cross_registries);
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
module CxApp
|
||||
class CxBase
|
||||
def label
|
||||
"base"
|
||||
end
|
||||
end
|
||||
|
||||
class CxHelper
|
||||
def assist(text)
|
||||
widget = CxWidget.new
|
||||
widget.label
|
||||
widget.name + text
|
||||
end
|
||||
end
|
||||
end
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
# Rails-shaped pair: no require, the constant is found through the global
|
||||
# namespace, so every call below is a cross-file resolution.
|
||||
module CxApp
|
||||
class CxWidget < CxBase
|
||||
def render
|
||||
helper = CxHelper.new
|
||||
helper.assist(name)
|
||||
end
|
||||
|
||||
def name
|
||||
"widget"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1896,6 +1896,97 @@ TEST(lrp_python_subclass_constructor_own_init_only) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── Ruby: cross-file through the prebuilt project registry ──────────
|
||||
* Rails/Zeitwerk code names classes in other files with no require, and a
|
||||
* class may be reopened in a third file. Both edges must come out of the
|
||||
* Ruby resolver (ruby_* strategy), not the name-based one. */
|
||||
TEST(lrp_ruby_cross_file_zeitwerk) {
|
||||
const LRP_File files[] = {
|
||||
{"app/widget.rb", "class Widget\n"
|
||||
" def run\n"
|
||||
" h = Helper.new\n"
|
||||
" h.assist\n"
|
||||
" h.extra_step\n"
|
||||
" end\n"
|
||||
"end\n"},
|
||||
{"app/helper.rb", "class Helper\n"
|
||||
" def assist\n"
|
||||
" 1\n"
|
||||
" end\n"
|
||||
"end\n"},
|
||||
{"lib/helper_ext.rb", "class Helper\n"
|
||||
" def extra_step\n"
|
||||
" 2\n"
|
||||
" end\n"
|
||||
"end\n"},
|
||||
};
|
||||
LRP_Proj lp;
|
||||
cbm_store_t *store = lrp_index(&lp, files, 3);
|
||||
int assist = lrp_exact_calls_by_name(store, lp.project, "run", "assist");
|
||||
int extra = lrp_exact_calls_by_name(store, lp.project, "run", "extra_step");
|
||||
int typed = lrp_count_calls_with_strategy(store, lp.project, "ruby_method_typed");
|
||||
if (assist < 1 || extra < 1 || typed < 2) {
|
||||
fprintf(stderr, " [LRP] ruby cross-file FAIL assist=%d extra_step=%d typed_edges=%d\n",
|
||||
assist, extra, typed);
|
||||
lrp_diag(store, lp.project, "ruby cross-file");
|
||||
cbm_edge_t *edges = NULL;
|
||||
int n = 0;
|
||||
if (store &&
|
||||
cbm_store_find_edges_by_type(store, lp.project, "CALLS", &edges, &n) == CBM_STORE_OK) {
|
||||
for (int i = 0; i < n; i++)
|
||||
fprintf(stderr, " [LRP] CALLS %s\n",
|
||||
edges[i].properties_json ? edges[i].properties_json : "(none)");
|
||||
cbm_store_free_edges(edges, n);
|
||||
}
|
||||
}
|
||||
lrp_cleanup(&lp, store);
|
||||
ASSERT_TRUE(assist >= 1);
|
||||
ASSERT_TRUE(extra >= 1);
|
||||
ASSERT_TRUE(typed >= 2);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── Ruby: singleton side and cross-file mixins, through real extraction ──
|
||||
* The hand-built-def rows pass the singleton flag and mixin macros in
|
||||
* themselves; this one has the extractor produce them. `Child.run` is a
|
||||
* `def self.run`, `Child.new.run` is Base's instance `run`, and `Widget`
|
||||
* includes a module that overrides Base#greet, all from a caller in a fifth
|
||||
* file. */
|
||||
TEST(lrp_ruby_cross_file_singleton_and_mixin) {
|
||||
const LRP_File files[] = {
|
||||
{"app/base.rb", "class Base\n def run\n 1\n end\n def greet\n 2\n end\nend\n"},
|
||||
{"app/overrides.rb", "module Overrides\n def greet\n 3\n end\nend\n"},
|
||||
{"app/child.rb", "class Child < Base\n def self.run\n 4\n end\nend\n"},
|
||||
{"app/widget.rb", "class Widget < Base\n include Overrides\nend\n"},
|
||||
{"app/caller.rb", "class Caller\n"
|
||||
" def go\n"
|
||||
" Child.new.run\n"
|
||||
" Child.run\n"
|
||||
" Widget.new.greet\n"
|
||||
" end\n"
|
||||
"end\n"},
|
||||
};
|
||||
LRP_Proj lp;
|
||||
cbm_store_t *store = lrp_index(&lp, files, 5);
|
||||
const char *p = store ? lp.project : NULL;
|
||||
int inst = lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "caller.Caller.go", "base.Base.run");
|
||||
int klass =
|
||||
lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "caller.Caller.go", "child.Child.run");
|
||||
int mixin = lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "caller.Caller.go",
|
||||
"overrides.Overrides.greet");
|
||||
int base_greet =
|
||||
lrp_exact_edge_by_qn_suffix(store, p, "CALLS", "caller.Caller.go", "base.Base.greet");
|
||||
if (inst != 1 || klass != 1 || mixin != 1 || base_greet != 0)
|
||||
fprintf(stderr, " [LRP] ruby singleton/mixin FAIL instance=%d class=%d mixin=%d base=%d\n",
|
||||
inst, klass, mixin, base_greet);
|
||||
lrp_cleanup(&lp, store);
|
||||
ASSERT_EQ(inst, 1);
|
||||
ASSERT_EQ(klass, 1);
|
||||
ASSERT_EQ(mixin, 1);
|
||||
ASSERT_EQ(base_greet, 0);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════
|
||||
* SUITE registration
|
||||
* ══════════════════════════════════════════════════════════════════════ */
|
||||
@@ -2014,4 +2105,8 @@ SUITE(lsp_resolution_probe) {
|
||||
RUN_TEST(lrp_tsx_usage_import_type_with_decoy);
|
||||
RUN_TEST(lrp_python_usage_instantiation);
|
||||
RUN_TEST(lrp_python_subclass_constructor_own_init_only);
|
||||
|
||||
/* ── Ruby (project registry built once) ── */
|
||||
RUN_TEST(lrp_ruby_cross_file_zeitwerk);
|
||||
RUN_TEST(lrp_ruby_cross_file_singleton_and_mixin);
|
||||
}
|
||||
|
||||
+823
-4
@@ -23,12 +23,18 @@
|
||||
* 15b. Scope gates (def, params, blocks, reassignment)
|
||||
* 16. send() / the whole dynamic-dispatch family emits NO edge (negative)
|
||||
* 16c. Blocks that rebind self emit NO self edge (negative)
|
||||
* 17. Cross-file: defs from another file resolve
|
||||
* 18. Cross-file: ActiveRecord repro mirroring the Rails-shaped e2e fixture
|
||||
* 19. Cross-file: a class reopened in a second file
|
||||
* 20. Cross-file: compact `class A::B` in another file
|
||||
* 21. Cross-file: one prebuilt registry serves many files unchanged
|
||||
* Fail-closed ancestry (7c) and block-nested defs (7d) sit with mixins.
|
||||
*
|
||||
* Scope: every row here is SINGLE-FILE, driven through cbm_extract_file so it
|
||||
* exercises the per-file resolver exactly as the extraction pipeline calls it.
|
||||
* Cross-file resolution (cbm_run_ruby_lsp_cross, wired through
|
||||
* pass_lsp_cross.c) is a separate tier and is covered by its own rows.
|
||||
* Scope: rows 1-16 are SINGLE-FILE, driven through cbm_extract_file so they
|
||||
* exercise the per-file resolver exactly as the extraction pipeline calls it.
|
||||
* Rows 17-18 drive cbm_run_ruby_lsp_cross directly with a hand-built
|
||||
* CBMLSPDef[], which is the shape pass_lsp_cross.c hands it on the
|
||||
* cbm_pxc_run_one fallback path.
|
||||
*
|
||||
* The resolver populates result->resolved_calls with CBMResolvedCall rows.
|
||||
* The pipeline joins a row to its caller def by EXACT QN equality, so every
|
||||
@@ -1524,6 +1530,803 @@ TEST(rubylsp_stdlib_seed_survives_alloc_failure) {
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── 17. Cross-file: defs from another file resolve ─────────────── */
|
||||
|
||||
TEST(rubylsp_cross_file_defs) {
|
||||
/* models/user.rb (other file) provides the class; this file calls it.
|
||||
* Simulates the fallback cbm_pxc_run_one path with a combined def
|
||||
* list. */
|
||||
CBMLSPDef defs[3];
|
||||
memset(defs, 0, sizeof(defs));
|
||||
defs[0].qualified_name = "test.models.user.User";
|
||||
defs[0].short_name = "User";
|
||||
defs[0].label = "Class";
|
||||
defs[0].def_module_qn = "test.models.user";
|
||||
defs[0].embedded_types = "ApplicationRecord";
|
||||
defs[0].lang = CBM_LANG_RUBY;
|
||||
defs[1].qualified_name = "test.models.user.User.full_name";
|
||||
defs[1].short_name = "full_name";
|
||||
defs[1].label = "Method";
|
||||
defs[1].receiver_type = "test.models.user.User";
|
||||
defs[1].def_module_qn = "test.models.user";
|
||||
defs[1].lang = CBM_LANG_RUBY;
|
||||
defs[2].qualified_name = "test.models.user.User.admin?";
|
||||
defs[2].short_name = "admin?";
|
||||
defs[2].label = "Method";
|
||||
defs[2].receiver_type = "test.models.user.User";
|
||||
defs[2].def_module_qn = "test.models.user";
|
||||
defs[2].lang = CBM_LANG_RUBY;
|
||||
|
||||
const char *src = "class UsersController\n"
|
||||
" def show\n"
|
||||
" u = User.new\n"
|
||||
" u.full_name\n"
|
||||
" f = User.find(3)\n"
|
||||
" f.admin?\n"
|
||||
" end\n"
|
||||
"end\n";
|
||||
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
/* `f = User.find(3)` with User in another file needs the cross-file half
|
||||
* of the finder rule, which is off by default (ruby_ar_finder_cross_file). */
|
||||
bool prev = cbm_ruby_set_ar_finder_cross_file(true);
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.controller", defs, 3, NULL,
|
||||
NULL, 0, NULL, &out);
|
||||
(void)cbm_ruby_set_ar_finder_cross_file(prev);
|
||||
|
||||
bool ctor = false;
|
||||
bool full_name = false;
|
||||
bool admin = false;
|
||||
for (int i = 0; i < out.count; i++) {
|
||||
if (!out.items[i].callee_qn || !out.items[i].caller_qn)
|
||||
continue;
|
||||
if (strcmp(out.items[i].caller_qn, "test.app.controller.UsersController.show") != 0)
|
||||
continue;
|
||||
if (strcmp(out.items[i].callee_qn, "test.models.user.User") == 0)
|
||||
ctor = true;
|
||||
if (strcmp(out.items[i].callee_qn, "test.models.user.User.full_name") == 0)
|
||||
full_name = true;
|
||||
if (strcmp(out.items[i].callee_qn, "test.models.user.User.admin?") == 0)
|
||||
admin = true;
|
||||
}
|
||||
if (!(ctor && full_name && admin)) {
|
||||
printf(" cross-file rows (%d):\n", out.count);
|
||||
for (int i = 0; i < out.count; i++)
|
||||
printf(" %s -> %s [%s]\n", out.items[i].caller_qn, out.items[i].callee_qn,
|
||||
out.items[i].strategy);
|
||||
}
|
||||
cbm_arena_destroy(&arena);
|
||||
ASSERT(ctor);
|
||||
ASSERT(full_name);
|
||||
ASSERT(admin);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── 18. Cross-file: AR repro mirroring the e2e fixture exactly ── */
|
||||
|
||||
TEST(rubylsp_cross_file_ar_repro) {
|
||||
CBMLSPDef defs[6];
|
||||
memset(defs, 0, sizeof(defs));
|
||||
defs[0].qualified_name = "e2e.app.models.user.User";
|
||||
defs[0].short_name = "User";
|
||||
defs[0].label = "Class";
|
||||
defs[0].def_module_qn = "e2e.app.models.user";
|
||||
defs[0].embedded_types = "ApplicationRecord";
|
||||
defs[0].lang = CBM_LANG_RUBY;
|
||||
defs[1].qualified_name = "e2e.app.models.user.User.full_name";
|
||||
defs[1].short_name = "full_name";
|
||||
defs[1].label = "Method";
|
||||
defs[1].receiver_type = "e2e.app.models.user.User";
|
||||
defs[1].def_module_qn = "e2e.app.models.user";
|
||||
defs[1].lang = CBM_LANG_RUBY;
|
||||
defs[2].qualified_name = "e2e.app.models.user.User.deactivate!";
|
||||
defs[2].short_name = "deactivate!";
|
||||
defs[2].label = "Method";
|
||||
defs[2].receiver_type = "e2e.app.models.user.User";
|
||||
defs[2].def_module_qn = "e2e.app.models.user";
|
||||
defs[2].lang = CBM_LANG_RUBY;
|
||||
defs[3].qualified_name = "e2e.app.models.application_record.ApplicationRecord";
|
||||
defs[3].short_name = "ApplicationRecord";
|
||||
defs[3].label = "Class";
|
||||
defs[3].def_module_qn = "e2e.app.models.application_record";
|
||||
defs[3].embedded_types = "ActiveRecord::Base";
|
||||
defs[3].lang = CBM_LANG_RUBY;
|
||||
defs[4].qualified_name = "e2e.app.services.greeter.Greeter";
|
||||
defs[4].short_name = "Greeter";
|
||||
defs[4].label = "Class";
|
||||
defs[4].def_module_qn = "e2e.app.services.greeter";
|
||||
defs[4].lang = CBM_LANG_RUBY;
|
||||
defs[5].qualified_name = "e2e.app.services.greeter.Greeter.greet";
|
||||
defs[5].short_name = "greet";
|
||||
defs[5].label = "Method";
|
||||
defs[5].receiver_type = "e2e.app.services.greeter.Greeter";
|
||||
defs[5].def_module_qn = "e2e.app.services.greeter";
|
||||
defs[5].lang = CBM_LANG_RUBY;
|
||||
|
||||
const char *src = "class Report\n"
|
||||
" def generate(id)\n"
|
||||
" user = User.find(id)\n"
|
||||
" name = user.full_name\n"
|
||||
" greeter = Greeter.new(user)\n"
|
||||
" greeter.greet\n"
|
||||
" user.deactivate!\n"
|
||||
" name\n"
|
||||
" end\n"
|
||||
"end\n";
|
||||
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
/* `user = User.find(id)` with User in another file: cross-file finder rule. */
|
||||
bool prev = cbm_ruby_set_ar_finder_cross_file(true);
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "e2e.app.services.report", defs, 6, NULL,
|
||||
NULL, 0, NULL, &out);
|
||||
(void)cbm_ruby_set_ar_finder_cross_file(prev);
|
||||
|
||||
bool full_name = false;
|
||||
bool deact = false;
|
||||
bool greet = false;
|
||||
for (int i = 0; i < out.count; i++) {
|
||||
if (!out.items[i].callee_qn)
|
||||
continue;
|
||||
if (strcmp(out.items[i].callee_qn, "e2e.app.models.user.User.full_name") == 0)
|
||||
full_name = true;
|
||||
if (strcmp(out.items[i].callee_qn, "e2e.app.models.user.User.deactivate!") == 0)
|
||||
deact = true;
|
||||
if (strcmp(out.items[i].callee_qn, "e2e.app.services.greeter.Greeter.greet") == 0)
|
||||
greet = true;
|
||||
}
|
||||
if (!(full_name && deact && greet)) {
|
||||
printf(" AR-repro rows (%d):\n", out.count);
|
||||
for (int i = 0; i < out.count; i++)
|
||||
printf(" %s -> %s [%s]\n", out.items[i].caller_qn, out.items[i].callee_qn,
|
||||
out.items[i].strategy);
|
||||
}
|
||||
cbm_arena_destroy(&arena);
|
||||
ASSERT(greet);
|
||||
ASSERT(full_name);
|
||||
ASSERT(deact);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── 19-21. Cross-file: the shared project registry ─────────────── */
|
||||
|
||||
static void cross_def(CBMLSPDef *d, const char *qn, const char *short_name, const char *label,
|
||||
const char *module_qn, const char *receiver, const char *embedded) {
|
||||
memset(d, 0, sizeof(*d));
|
||||
d->qualified_name = qn;
|
||||
d->short_name = short_name;
|
||||
d->label = label;
|
||||
d->def_module_qn = module_qn;
|
||||
d->receiver_type = receiver;
|
||||
d->embedded_types = embedded;
|
||||
d->lang = CBM_LANG_RUBY;
|
||||
}
|
||||
|
||||
/* Caller and callee both match exactly, as find_resolved_exact does for the
|
||||
* single-file rows: a substring match would let a row pass on the wrong caller. */
|
||||
static bool out_has(const CBMResolvedCallArray *out, const char *caller, const char *callee) {
|
||||
for (int i = 0; i < out->count; i++) {
|
||||
if (out->items[i].caller_qn && out->items[i].callee_qn &&
|
||||
strcmp(out->items[i].caller_qn, caller) == 0 &&
|
||||
strcmp(out->items[i].callee_qn, callee) == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void out_dump(const CBMResolvedCallArray *out) {
|
||||
for (int i = 0; i < out->count; i++)
|
||||
printf(" %s -> %s [%s]\n", out->items[i].caller_qn, out->items[i].callee_qn,
|
||||
out->items[i].strategy);
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_reopened_class_across_files) {
|
||||
/* `class User` is reopened in a second file. Each file's class has its
|
||||
* own QN (it carries the file's module QN) but one constant path, so a
|
||||
* method defined in either reopening must be found, and the constructor
|
||||
* edge lands on the first declaration. */
|
||||
CBMLSPDef defs[4];
|
||||
cross_def(&defs[0], "test.a.User", "User", "Class", "test.a", NULL, NULL);
|
||||
cross_def(&defs[1], "test.a.User.name", "name", "Method", "test.a", "test.a.User", NULL);
|
||||
cross_def(&defs[2], "test.b.User", "User", "Class", "test.b", NULL, NULL);
|
||||
cross_def(&defs[3], "test.b.User.email", "email", "Method", "test.b", "test.b.User", NULL);
|
||||
|
||||
const char *src = "class Report\n"
|
||||
" def run\n"
|
||||
" u = User.new\n"
|
||||
" u.name\n"
|
||||
" u.email\n"
|
||||
" end\n"
|
||||
"end\n";
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.report", defs, 4, NULL, NULL, 0,
|
||||
NULL, &out);
|
||||
bool ctor = out_has(&out, "test.app.report.Report.run", "test.a.User");
|
||||
bool name = out_has(&out, "test.app.report.Report.run", "test.a.User.name");
|
||||
bool email = out_has(&out, "test.app.report.Report.run", "test.b.User.email");
|
||||
if (!(ctor && name && email))
|
||||
out_dump(&out);
|
||||
cbm_arena_destroy(&arena);
|
||||
ASSERT(ctor);
|
||||
ASSERT(name);
|
||||
ASSERT(email);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_compact_class_name) {
|
||||
/* A compact `class Admin::User` in another file has QN
|
||||
* "<module>.Admin::User" but is found through the dotted path
|
||||
* "Admin.User". */
|
||||
CBMLSPDef defs[2];
|
||||
cross_def(&defs[0], "test.admin.Admin::User", "User", "Class", "test.admin", NULL, NULL);
|
||||
cross_def(&defs[1], "test.admin.Admin::User.greet", "greet", "Method", "test.admin",
|
||||
"test.admin.Admin::User", NULL);
|
||||
|
||||
const char *src = "class Welcome\n"
|
||||
" def run\n"
|
||||
" Admin::User.new.greet\n"
|
||||
" end\n"
|
||||
"end\n";
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.welcome", defs, 2, NULL, NULL,
|
||||
0, NULL, &out);
|
||||
bool ctor = out_has(&out, "test.app.welcome.Welcome.run", "test.admin.Admin::User");
|
||||
bool greet = out_has(&out, "test.app.welcome.Welcome.run", "test.admin.Admin::User.greet");
|
||||
if (!(ctor && greet))
|
||||
out_dump(&out);
|
||||
cbm_arena_destroy(&arena);
|
||||
ASSERT(ctor);
|
||||
ASSERT(greet);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_registry_built_once_serves_many_files) {
|
||||
/* One registry, several files, superclass chain across files: the shared
|
||||
* registry is read-only, so later files see exactly what earlier ones did
|
||||
* and nothing a file declares leaks into it. */
|
||||
CBMLSPDef defs[4];
|
||||
cross_def(&defs[0], "test.base.Base", "Base", "Class", "test.base", NULL, NULL);
|
||||
cross_def(&defs[1], "test.base.Base.shared_m", "shared_m", "Method", "test.base",
|
||||
"test.base.Base", NULL);
|
||||
cross_def(&defs[2], "test.child.Child", "Child", "Class", "test.child", NULL, "Base");
|
||||
cross_def(&defs[3], "test.child.Child.own_m", "own_m", "Method", "test.child",
|
||||
"test.child.Child", NULL);
|
||||
|
||||
CBMArena build;
|
||||
cbm_arena_init(&build);
|
||||
CBMRubyCrossRegistry *sh = cbm_ruby_build_cross_registry(&build, defs, 4);
|
||||
ASSERT(sh != NULL);
|
||||
int classes_before = sh->class_count;
|
||||
int funcs_before = sh->reg.func_count;
|
||||
/* The counts cannot see a written field (a visit mark on a shared entry,
|
||||
* say), so keep a byte copy of the class array and both hash indexes and
|
||||
* compare them after the resolves. */
|
||||
size_t classes_bytes = (size_t)sh->class_count * sizeof(RubyClassInfo);
|
||||
size_t slots_bytes = ((size_t)sh->slot_mask + 1) * sizeof(int);
|
||||
char *snap_classes = (char *)malloc(classes_bytes);
|
||||
char *snap_by_qn = (char *)malloc(slots_bytes);
|
||||
char *snap_by_path = (char *)malloc(slots_bytes);
|
||||
char *snap_by_pp = (char *)malloc(slots_bytes);
|
||||
ASSERT(snap_classes && snap_by_qn && snap_by_path && snap_by_pp);
|
||||
memcpy(snap_classes, sh->classes, classes_bytes);
|
||||
memcpy(snap_by_qn, sh->by_qn, slots_bytes);
|
||||
memcpy(snap_by_path, sh->by_path, slots_bytes);
|
||||
memcpy(snap_by_pp, sh->by_pp, slots_bytes);
|
||||
|
||||
const char *src1 =
|
||||
"class One\n def run\n c = Child.new\n c.shared_m\n c.own_m\n end\nend\n";
|
||||
const char *src2 = "class Two\n def run\n Child.new.shared_m\n end\nend\n";
|
||||
const char *src3 = "class Three\n def run\n Local.new.shared_m\n end\nend\n";
|
||||
CBMArena a1;
|
||||
CBMArena a2;
|
||||
CBMArena a3;
|
||||
cbm_arena_init(&a1);
|
||||
cbm_arena_init(&a2);
|
||||
cbm_arena_init(&a3);
|
||||
CBMResolvedCallArray o1;
|
||||
CBMResolvedCallArray o2;
|
||||
CBMResolvedCallArray o3;
|
||||
memset(&o1, 0, sizeof(o1));
|
||||
memset(&o2, 0, sizeof(o2));
|
||||
memset(&o3, 0, sizeof(o3));
|
||||
cbm_run_ruby_lsp_cross_with_registry(&a1, src1, (int)strlen(src1), "test.app.one", sh, NULL,
|
||||
NULL, 0, NULL, &o1);
|
||||
cbm_run_ruby_lsp_cross_with_registry(&a2, src2, (int)strlen(src2), "test.app.two", sh, NULL,
|
||||
NULL, 0, NULL, &o2);
|
||||
cbm_run_ruby_lsp_cross_with_registry(&a3, src3, (int)strlen(src3), "test.app.three", sh, NULL,
|
||||
NULL, 0, NULL, &o3);
|
||||
bool one_inherited = out_has(&o1, "test.app.one.One.run", "test.base.Base.shared_m");
|
||||
bool one_own = out_has(&o1, "test.app.one.One.run", "test.child.Child.own_m");
|
||||
bool two_inherited = out_has(&o2, "test.app.two.Two.run", "test.base.Base.shared_m");
|
||||
bool three_none = !out_has(&o3, "test.app.three.Three.run", "test.base.Base.shared_m");
|
||||
bool untouched = sh->class_count == classes_before && sh->reg.func_count == funcs_before &&
|
||||
memcmp(snap_classes, sh->classes, classes_bytes) == 0 &&
|
||||
memcmp(snap_by_qn, sh->by_qn, slots_bytes) == 0 &&
|
||||
memcmp(snap_by_path, sh->by_path, slots_bytes) == 0 &&
|
||||
memcmp(snap_by_pp, sh->by_pp, slots_bytes) == 0;
|
||||
free(snap_by_pp);
|
||||
free(snap_classes);
|
||||
free(snap_by_qn);
|
||||
free(snap_by_path);
|
||||
if (!(one_inherited && one_own && two_inherited && three_none && untouched)) {
|
||||
out_dump(&o1);
|
||||
out_dump(&o2);
|
||||
out_dump(&o3);
|
||||
}
|
||||
cbm_arena_destroy(&a1);
|
||||
cbm_arena_destroy(&a2);
|
||||
cbm_arena_destroy(&a3);
|
||||
cbm_arena_destroy(&build);
|
||||
ASSERT(one_inherited);
|
||||
ASSERT(one_own);
|
||||
ASSERT(two_inherited);
|
||||
ASSERT(three_none);
|
||||
ASSERT(untouched);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* A class reopened in `reopenings` files (`Widget`, one module each, the first
|
||||
* declaring `< CxBase`), plus the base. Work is counted for the registry build
|
||||
* and for resolving one fixed file whose call sites all miss on the head's own
|
||||
* methods (`label` is inherited from CxBase). */
|
||||
static bool rubylsp_reopen_work(int reopenings, uint64_t *build_work, uint64_t *resolve_work) {
|
||||
int n = reopenings * 2 + 2;
|
||||
CBMLSPDef *defs = (CBMLSPDef *)calloc((size_t)n, sizeof(CBMLSPDef));
|
||||
char (*names)[64] = (char (*)[64])calloc((size_t)n * 3, sizeof(*names));
|
||||
if (!defs || !names) {
|
||||
free(defs);
|
||||
free(names);
|
||||
return false;
|
||||
}
|
||||
int d = 0;
|
||||
int nm = 0;
|
||||
cross_def(&defs[d++], "test.base.CxBase", "CxBase", "Class", "test.base", NULL, NULL);
|
||||
cross_def(&defs[d++], "test.base.CxBase.label", "label", "Method", "test.base",
|
||||
"test.base.CxBase", NULL);
|
||||
for (int i = 0; i < reopenings; i++) {
|
||||
char *mod = names[nm++];
|
||||
char *cls = names[nm++];
|
||||
char *meth = names[nm++];
|
||||
snprintf(mod, 64, "test.m%d", i);
|
||||
snprintf(cls, 64, "test.m%d.CxWidget", i);
|
||||
snprintf(meth, 64, "test.m%d.CxWidget.own%d", i, i);
|
||||
cross_def(&defs[d++], cls, "CxWidget", "Class", mod, NULL, i == 0 ? "CxBase" : NULL);
|
||||
cross_def(&defs[d++], meth, "own", "Method", mod, cls, NULL);
|
||||
}
|
||||
CBMArena build;
|
||||
cbm_arena_init(&build);
|
||||
(void)cbm_lsp_work_take();
|
||||
CBMRubyCrossRegistry *sh = cbm_ruby_build_cross_registry(&build, defs, d);
|
||||
*build_work = cbm_lsp_work_take();
|
||||
|
||||
const char *src = "class CxCaller\n def run\n w = CxWidget.new\n w.label\n"
|
||||
" w.label\n w.label\n w.label\n w.label\n w.label\n"
|
||||
" w.label\n w.label\n end\nend\n";
|
||||
CBMArena a;
|
||||
cbm_arena_init(&a);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
(void)cbm_lsp_work_take();
|
||||
if (sh)
|
||||
cbm_run_ruby_lsp_cross_with_registry(&a, src, (int)strlen(src), "test.app.caller", sh, NULL,
|
||||
NULL, 0, NULL, &out);
|
||||
*resolve_work = cbm_lsp_work_take();
|
||||
bool resolved = out_has(&out, "test.app.caller.CxCaller.run", "test.base.CxBase.label");
|
||||
cbm_arena_destroy(&a);
|
||||
cbm_arena_destroy(&build);
|
||||
free(defs);
|
||||
free(names);
|
||||
return sh && resolved;
|
||||
}
|
||||
|
||||
/* Review items 2b / 2c / 3. Constants are global, so a namespace module or a
|
||||
* model is reopened in many files. Doubling the reopenings must at most double
|
||||
* the registry build (a per-reopening tail walk makes it R^2/2, ratio 4), and
|
||||
* must not change what resolving one fixed file costs at all: a lookup is one
|
||||
* probe however many files reopen the class (a group walk per miss makes it
|
||||
* grow with R). The resolve gate is therefore 1.5, not the build gate of 3. */
|
||||
TEST(rubylsp_cross_reopened_class_work_is_bounded) {
|
||||
uint64_t build_r = 0;
|
||||
uint64_t resolve_r = 0;
|
||||
uint64_t build_2r = 0;
|
||||
uint64_t resolve_2r = 0;
|
||||
ASSERT(rubylsp_reopen_work(64, &build_r, &resolve_r));
|
||||
ASSERT(rubylsp_reopen_work(128, &build_2r, &resolve_2r));
|
||||
printf(" reopened-class work: build R=64 %llu, 2R %llu; resolve R=64 %llu, 2R %llu\n",
|
||||
(unsigned long long)build_r, (unsigned long long)build_2r, (unsigned long long)resolve_r,
|
||||
(unsigned long long)resolve_2r);
|
||||
ASSERT(build_2r < 3 * (build_r ? build_r : 1));
|
||||
ASSERT(2 * resolve_2r < 3 * (resolve_r ? resolve_r : 1));
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Review item 4: the merged record must not depend on def order. A full build
|
||||
* orders defs by file index; the incremental closure route appends fresh defs
|
||||
* to the persisted ones, so the same project can arrive in any order. The head
|
||||
* is the smallest QN, the superclass comes from the smallest-QN declaration
|
||||
* that has one, and a method defined by two reopenings resolves to the smaller
|
||||
* QN. Built forwards and reversed, the resolved edges must be identical. */
|
||||
static void rubylsp_order_defs(CBMLSPDef *defs, bool reversed) {
|
||||
CBMLSPDef t[10];
|
||||
cross_def(&t[0], "test.m2.CxWidget", "CxWidget", "Class", "test.m2", NULL, "CxOther");
|
||||
cross_def(&t[1], "test.m2.CxWidget.dup_m", "dup_m", "Method", "test.m2", "test.m2.CxWidget",
|
||||
NULL);
|
||||
cross_def(&t[2], "test.m1.CxWidget", "CxWidget", "Class", "test.m1", NULL, "CxBase");
|
||||
cross_def(&t[3], "test.m1.CxWidget.dup_m", "dup_m", "Method", "test.m1", "test.m1.CxWidget",
|
||||
NULL);
|
||||
cross_def(&t[4], "test.m0.CxWidget", "CxWidget", "Class", "test.m0", NULL, NULL);
|
||||
cross_def(&t[5], "test.m0.CxWidget.own", "own", "Method", "test.m0", "test.m0.CxWidget", NULL);
|
||||
cross_def(&t[6], "test.base.CxBase", "CxBase", "Class", "test.base", NULL, NULL);
|
||||
cross_def(&t[7], "test.base.CxBase.label", "label", "Method", "test.base", "test.base.CxBase",
|
||||
NULL);
|
||||
cross_def(&t[8], "test.base.CxOther", "CxOther", "Class", "test.base", NULL, NULL);
|
||||
cross_def(&t[9], "test.base.CxOther.label", "label", "Method", "test.base", "test.base.CxOther",
|
||||
NULL);
|
||||
for (int i = 0; i < 10; i++)
|
||||
defs[i] = reversed ? t[9 - i] : t[i];
|
||||
}
|
||||
|
||||
static bool rubylsp_order_edges(bool reversed, bool *ctor, bool *label, bool *dup, bool *own) {
|
||||
CBMLSPDef defs[10];
|
||||
rubylsp_order_defs(defs, reversed);
|
||||
const char *src = "class CxCaller\n def run\n w = CxWidget.new\n w.label\n"
|
||||
" w.dup_m\n w.own\n end\nend\n";
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.caller", defs, 10, NULL, NULL,
|
||||
0, NULL, &out);
|
||||
*ctor = out_has(&out, "test.app.caller.CxCaller.run", "test.m0.CxWidget");
|
||||
*label = out_has(&out, "test.app.caller.CxCaller.run", "test.base.CxBase.label");
|
||||
*dup = out_has(&out, "test.app.caller.CxCaller.run", "test.m1.CxWidget.dup_m");
|
||||
*own = out_has(&out, "test.app.caller.CxCaller.run", "test.m0.CxWidget.own");
|
||||
if (!(*ctor && *label && *dup && *own))
|
||||
out_dump(&out);
|
||||
cbm_arena_destroy(&arena);
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_same_named_classes_stay_in_their_package) {
|
||||
/* Two packages of one repository each declare their own Topic. A call
|
||||
* from inside a package reaches that package's Topic, never the other's;
|
||||
* a caller in a third package, where the constant is declared under
|
||||
* several roots and none is its own, resolves nothing; a constant
|
||||
* declared under one root only still resolves from anywhere. */
|
||||
CBMLSPDef defs[7];
|
||||
cross_def(&defs[0], "proj.alpha.test.models.topic.Topic", "Topic", "Class",
|
||||
"proj.alpha.test.models.topic", NULL, NULL);
|
||||
cross_def(&defs[1], "proj.alpha.test.models.topic.Topic.hello", "hello", "Method",
|
||||
"proj.alpha.test.models.topic", "proj.alpha.test.models.topic.Topic", NULL);
|
||||
cross_def(&defs[2], "proj.beta.test.models.topic.Topic", "Topic", "Class",
|
||||
"proj.beta.test.models.topic", NULL, NULL);
|
||||
cross_def(&defs[3], "proj.beta.test.models.topic.Topic.hello", "hello", "Method",
|
||||
"proj.beta.test.models.topic", "proj.beta.test.models.topic.Topic", NULL);
|
||||
cross_def(&defs[4], "proj.beta.lib.only.Only", "Only", "Class", "proj.beta.lib.only", NULL,
|
||||
NULL);
|
||||
cross_def(&defs[5], "proj.beta.lib.only.Only.ping", "ping", "Method", "proj.beta.lib.only",
|
||||
"proj.beta.lib.only.Only", NULL);
|
||||
cross_def(&defs[6], "proj.alpha.lib.x.X", "X", "Class", "proj.alpha.lib.x", NULL, NULL);
|
||||
|
||||
CBMArena build;
|
||||
cbm_arena_init(&build);
|
||||
CBMRubyCrossRegistry *sh = cbm_ruby_build_cross_registry(&build, defs, 7);
|
||||
ASSERT(sh != NULL);
|
||||
|
||||
const char *src =
|
||||
"class Caller\n def run\n Topic.new.hello\n Only.new.ping\n end\nend\n";
|
||||
const char *mods[3] = {"proj.alpha.test.cases.caller", "proj.beta.test.cases.caller",
|
||||
"proj.gamma.test.cases.caller"};
|
||||
const char *callers[3] = {"proj.alpha.test.cases.caller.Caller.run",
|
||||
"proj.beta.test.cases.caller.Caller.run",
|
||||
"proj.gamma.test.cases.caller.Caller.run"};
|
||||
bool alpha_ok = false;
|
||||
bool beta_ok = false;
|
||||
bool gamma_none = false;
|
||||
bool only_all = true;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
CBMArena a;
|
||||
cbm_arena_init(&a);
|
||||
CBMResolvedCallArray o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
cbm_run_ruby_lsp_cross_with_registry(&a, src, (int)strlen(src), mods[i], sh, NULL, NULL, 0,
|
||||
NULL, &o);
|
||||
bool alpha = out_has(&o, callers[i], "proj.alpha.test.models.topic.Topic.hello");
|
||||
bool beta = out_has(&o, callers[i], "proj.beta.test.models.topic.Topic.hello");
|
||||
if (i == 0)
|
||||
alpha_ok = alpha && !beta;
|
||||
else if (i == 1)
|
||||
beta_ok = beta && !alpha;
|
||||
else
|
||||
gamma_none = !alpha && !beta;
|
||||
only_all = only_all && out_has(&o, callers[i], "proj.beta.lib.only.Only.ping");
|
||||
if (!(only_all && (i != 0 || alpha_ok)))
|
||||
out_dump(&o);
|
||||
cbm_arena_destroy(&a);
|
||||
}
|
||||
cbm_arena_destroy(&build);
|
||||
ASSERT(alpha_ok);
|
||||
ASSERT(beta_ok);
|
||||
ASSERT(gamma_none);
|
||||
ASSERT(only_all);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_ambiguous_nested_constant_does_not_fall_through) {
|
||||
/* Inside `module M`, `X` names M::X when M::X exists. Two packages
|
||||
* declare M::X and a third declares a top-level X. A caller in a fourth
|
||||
* package, inside module M, must not land on the top-level X (Ruby binds
|
||||
* M::X there; which package's is unknown, so nothing). A caller in
|
||||
* alpha binds alpha's M::X. Three packages share M::X so the per-path
|
||||
* chain holds more than two heads. */
|
||||
CBMLSPDef defs[8];
|
||||
cross_def(&defs[0], "proj.alpha.lib.mx.M.X", "X", "Class", "proj.alpha.lib.mx", NULL, NULL);
|
||||
cross_def(&defs[1], "proj.alpha.lib.mx.M.X.hello", "hello", "Method", "proj.alpha.lib.mx",
|
||||
"proj.alpha.lib.mx.M.X", NULL);
|
||||
cross_def(&defs[2], "proj.beta.lib.mx.M.X", "X", "Class", "proj.beta.lib.mx", NULL, NULL);
|
||||
cross_def(&defs[3], "proj.beta.lib.mx.M.X.hello", "hello", "Method", "proj.beta.lib.mx",
|
||||
"proj.beta.lib.mx.M.X", NULL);
|
||||
cross_def(&defs[4], "proj.eta.lib.mx.M.X", "X", "Class", "proj.eta.lib.mx", NULL, NULL);
|
||||
cross_def(&defs[5], "proj.eta.lib.mx.M.X.hello", "hello", "Method", "proj.eta.lib.mx",
|
||||
"proj.eta.lib.mx.M.X", NULL);
|
||||
cross_def(&defs[6], "proj.gamma.lib.x.X", "X", "Class", "proj.gamma.lib.x", NULL, NULL);
|
||||
cross_def(&defs[7], "proj.gamma.lib.x.X.hello", "hello", "Method", "proj.gamma.lib.x",
|
||||
"proj.gamma.lib.x.X", NULL);
|
||||
|
||||
CBMArena build;
|
||||
cbm_arena_init(&build);
|
||||
CBMRubyCrossRegistry *sh = cbm_ruby_build_cross_registry(&build, defs, 8);
|
||||
ASSERT(sh != NULL);
|
||||
|
||||
const char *src =
|
||||
"module M\n class Caller\n def run\n X.new.hello\n end\n end\nend\n";
|
||||
const char *mods[2] = {"proj.delta.lib.caller", "proj.alpha.lib.caller"};
|
||||
const char *callers[2] = {"proj.delta.lib.caller.M.Caller.run",
|
||||
"proj.alpha.lib.caller.M.Caller.run"};
|
||||
bool delta_none = false;
|
||||
bool alpha_own = false;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
CBMArena a;
|
||||
cbm_arena_init(&a);
|
||||
CBMResolvedCallArray o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
cbm_run_ruby_lsp_cross_with_registry(&a, src, (int)strlen(src), mods[i], sh, NULL, NULL, 0,
|
||||
NULL, &o);
|
||||
bool top = out_has(&o, callers[i], "proj.gamma.lib.x.X.hello");
|
||||
bool alpha = out_has(&o, callers[i], "proj.alpha.lib.mx.M.X.hello");
|
||||
bool beta = out_has(&o, callers[i], "proj.beta.lib.mx.M.X.hello");
|
||||
bool eta = out_has(&o, callers[i], "proj.eta.lib.mx.M.X.hello");
|
||||
if (i == 0)
|
||||
delta_none = !top && !alpha && !beta && !eta;
|
||||
else
|
||||
alpha_own = alpha && !top && !beta && !eta;
|
||||
if (!(i == 0 ? delta_none : alpha_own))
|
||||
out_dump(&o);
|
||||
cbm_arena_destroy(&a);
|
||||
}
|
||||
cbm_arena_destroy(&build);
|
||||
ASSERT(delta_none);
|
||||
ASSERT(alpha_own);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_registry_is_def_order_independent) {
|
||||
bool f[4];
|
||||
bool r[4];
|
||||
ASSERT(rubylsp_order_edges(false, &f[0], &f[1], &f[2], &f[3]));
|
||||
ASSERT(rubylsp_order_edges(true, &r[0], &r[1], &r[2], &r[3]));
|
||||
/* head = smallest QN (m0), superclass from the smallest-QN declaration
|
||||
* that has one (m1's CxBase, not m2's CxOther), duplicate method from the
|
||||
* smaller QN (m1) — in both orders. */
|
||||
for (int i = 0; i < 4; i++) {
|
||||
ASSERT(f[i]);
|
||||
ASSERT(r[i]);
|
||||
}
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── cross-file: singleton side and mixins (review items 5, 6) ───── */
|
||||
|
||||
static const CBMDefinition *rubylsp_find_def(const CBMFileResult *r, const char *name) {
|
||||
for (int i = 0; i < r->defs.count; i++) {
|
||||
if (r->defs.items[i].name && strcmp(r->defs.items[i].name, name) == 0)
|
||||
return &r->defs.items[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The extractor knows what CBMLSPDef used to lose: which methods sit on the
|
||||
* singleton side (`def self.m`, `class << self`) and which mixin macros a
|
||||
* class body holds. Only constants count; `extend self` is not one. */
|
||||
TEST(rubylsp_extractor_flags_singleton_methods_and_mixins) {
|
||||
const char *src = "class Widget < Base\n"
|
||||
" include Auditable\n"
|
||||
" prepend Admin::Guard\n"
|
||||
" extend Finders\n"
|
||||
" extend self\n"
|
||||
" def inst\n end\n"
|
||||
" def self.make\n end\n"
|
||||
" class << self\n"
|
||||
" def build\n end\n"
|
||||
" end\n"
|
||||
"end\n";
|
||||
CBMFileResult *r = extract_ruby(src);
|
||||
ASSERT(r != NULL);
|
||||
const CBMDefinition *w = rubylsp_find_def(r, "Widget");
|
||||
const CBMDefinition *inst = rubylsp_find_def(r, "inst");
|
||||
const CBMDefinition *make = rubylsp_find_def(r, "make");
|
||||
const CBMDefinition *build = rubylsp_find_def(r, "build");
|
||||
bool ok = w && inst && make;
|
||||
bool mixins =
|
||||
ok && w->mixin_refs && strcmp(w->mixin_refs, "i:Auditable|p:Admin::Guard|e:Finders") == 0;
|
||||
/* A method inside `class << self`, when the extractor emits one at all,
|
||||
* must be on the singleton side. */
|
||||
bool flags = ok && !inst->is_singleton && make->is_singleton && (!build || build->is_singleton);
|
||||
printf(" defs: Widget=%d inst=%d make=%d build=%d (class << self def emitted: %s)\n",
|
||||
w != NULL, inst != NULL, make != NULL, build != NULL, build ? "yes" : "no");
|
||||
if (!(mixins && flags))
|
||||
printf(" mixin_refs=%s\n", ok && w->mixin_refs ? w->mixin_refs : "(null)");
|
||||
cbm_free_result(r);
|
||||
ASSERT(ok);
|
||||
ASSERT(mixins);
|
||||
ASSERT(flags);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* Review item 5. `Child` defines `def self.run` and inherits instance `run`
|
||||
* from Base. An instance call must reach Base#run, the one Ruby would call; a
|
||||
* class-side call must reach Child.run. Registering each method on both keys
|
||||
* made the instance call stop at the singleton. */
|
||||
TEST(rubylsp_cross_singleton_and_instance_do_not_shadow) {
|
||||
CBMLSPDef defs[4];
|
||||
cross_def(&defs[0], "test.base.Base", "Base", "Class", "test.base", NULL, NULL);
|
||||
cross_def(&defs[1], "test.base.Base.run", "run", "Method", "test.base", "test.base.Base", NULL);
|
||||
cross_def(&defs[2], "test.child.Child", "Child", "Class", "test.child", NULL, "Base");
|
||||
cross_def(&defs[3], "test.child.Child.run", "run", "Method", "test.child", "test.child.Child",
|
||||
NULL);
|
||||
defs[3].is_singleton = true;
|
||||
|
||||
const char *src = "class Caller\n def go\n Child.new.run\n Child.run\n end\nend\n";
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.caller", defs, 4, NULL, NULL, 0,
|
||||
NULL, &out);
|
||||
bool instance = out_has(&out, "test.app.caller.Caller.go", "test.base.Base.run");
|
||||
bool klass = out_has(&out, "test.app.caller.Caller.go", "test.child.Child.run");
|
||||
if (!(instance && klass))
|
||||
out_dump(&out);
|
||||
cbm_arena_destroy(&arena);
|
||||
ASSERT(instance);
|
||||
ASSERT(klass);
|
||||
PASS();
|
||||
}
|
||||
|
||||
static bool rubylsp_mixin_call(const char *decl_mixin, bool reopen_elsewhere, bool *module_hit,
|
||||
bool *base_hit) {
|
||||
CBMLSPDef defs[8];
|
||||
int n = 0;
|
||||
cross_def(&defs[n++], "test.base.Base", "Base", "Class", "test.base", NULL, NULL);
|
||||
cross_def(&defs[n++], "test.base.Base.greet", "greet", "Method", "test.base", "test.base.Base",
|
||||
NULL);
|
||||
cross_def(&defs[n++], "test.mod.Overrides", "Overrides", "Module", "test.mod", NULL, NULL);
|
||||
cross_def(&defs[n++], "test.mod.Overrides.greet", "greet", "Method", "test.mod",
|
||||
"test.mod.Overrides", NULL);
|
||||
/* The class is declared in file A (the head); the include is in A, or in
|
||||
* a reopening in a later file Z. */
|
||||
cross_def(&defs[n++], "test.a.Widget", "Widget", "Class", "test.a", NULL, "Base");
|
||||
if (!reopen_elsewhere)
|
||||
defs[n - 1].mixin_refs = decl_mixin;
|
||||
if (reopen_elsewhere) {
|
||||
cross_def(&defs[n++], "test.z.Widget", "Widget", "Class", "test.z", NULL, NULL);
|
||||
defs[n - 1].mixin_refs = decl_mixin;
|
||||
}
|
||||
const char *src = "class Caller\n def go\n Widget.new.greet\n end\nend\n";
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.caller", defs, n, NULL, NULL, 0,
|
||||
NULL, &out);
|
||||
*module_hit = out_has(&out, "test.app.caller.Caller.go", "test.mod.Overrides.greet");
|
||||
*base_hit = out_has(&out, "test.app.caller.Caller.go", "test.base.Base.greet");
|
||||
if (!*module_hit && !*base_hit && out.count > 1)
|
||||
out_dump(&out);
|
||||
cbm_arena_destroy(&arena);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Review item 6. A module the class includes shadows the superclass's method,
|
||||
* whichever file the class and the include are in. A call from file B must not
|
||||
* resolve to Base#greet, and an include of a module the table cannot see must
|
||||
* block the walk rather than fall through to the superclass. */
|
||||
TEST(rubylsp_cross_mixin_shadows_superclass_across_files) {
|
||||
bool module_hit;
|
||||
bool base_hit;
|
||||
ASSERT(rubylsp_mixin_call("i:Overrides", false, &module_hit, &base_hit));
|
||||
ASSERT(module_hit);
|
||||
ASSERT(!base_hit);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_mixin_in_reopening_shadows_superclass) {
|
||||
bool module_hit;
|
||||
bool base_hit;
|
||||
ASSERT(rubylsp_mixin_call("i:Overrides", true, &module_hit, &base_hit));
|
||||
ASSERT(module_hit);
|
||||
ASSERT(!base_hit);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_unseen_mixin_blocks_superclass) {
|
||||
bool module_hit;
|
||||
bool base_hit;
|
||||
ASSERT(rubylsp_mixin_call("i:Missing", false, &module_hit, &base_hit));
|
||||
ASSERT(!module_hit);
|
||||
ASSERT(!base_hit);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* The finder rule (`User.find` -> User, relation chains keep the model) for a
|
||||
* model declared in ANOTHER file, the usual Rails layout. It is one switch
|
||||
* (ruby_ar_finder_cross_file): off by default, which keeps the rule to models
|
||||
* declared in the file being resolved, with nothing else changed; on, it also
|
||||
* types models from other files. */
|
||||
static bool rubylsp_ar_finder_cross_edge(void) {
|
||||
CBMLSPDef defs[2];
|
||||
cross_def(&defs[0], "test.models.user.User", "User", "Class", "test.models.user", NULL,
|
||||
"ApplicationRecord");
|
||||
cross_def(&defs[1], "test.models.user.User.full_name", "full_name", "Method",
|
||||
"test.models.user", "test.models.user.User", NULL);
|
||||
const char *src = "class Svc\n def go\n User.find(1).full_name\n "
|
||||
"User.where(active: true).first.full_name\n end\nend\n";
|
||||
CBMArena arena;
|
||||
cbm_arena_init(&arena);
|
||||
CBMResolvedCallArray out;
|
||||
memset(&out, 0, sizeof(out));
|
||||
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.svc", defs, 2, NULL, NULL, 0,
|
||||
NULL, &out);
|
||||
bool edge = out_has(&out, "test.app.svc.Svc.go", "test.models.user.User.full_name");
|
||||
cbm_arena_destroy(&arena);
|
||||
return edge;
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_ar_finder_same_file_only_by_default) {
|
||||
/* Default: a model from another file gets no finder typing, while a model
|
||||
* declared in the file being resolved still does. */
|
||||
bool cross = rubylsp_ar_finder_cross_edge();
|
||||
const char *src = "class User < ApplicationRecord\n def full_name\n 'x'\n end\nend\n"
|
||||
"class UsersController < ApplicationController\n def show\n"
|
||||
" User.find(1).full_name\n end\nend\n";
|
||||
CBMFileResult *r = extract_ruby(src);
|
||||
bool same = r && find_resolved(r, "UsersController.show", "User.full_name") >= 0;
|
||||
if (r)
|
||||
cbm_free_result(r);
|
||||
ASSERT(!cross);
|
||||
ASSERT(same);
|
||||
PASS();
|
||||
}
|
||||
|
||||
TEST(rubylsp_cross_ar_finder_crosses_files_when_switched_on) {
|
||||
bool prev = cbm_ruby_set_ar_finder_cross_file(true);
|
||||
bool cross = rubylsp_ar_finder_cross_edge();
|
||||
(void)cbm_ruby_set_ar_finder_cross_file(prev);
|
||||
ASSERT(cross);
|
||||
PASS();
|
||||
}
|
||||
|
||||
/* ── suite ──────────────────────────────────────────────────────── */
|
||||
|
||||
void suite_ruby_lsp(void) {
|
||||
@@ -1563,4 +2366,20 @@ void suite_ruby_lsp(void) {
|
||||
RUN_TEST(rubylsp_mixin_walk_visits_each_module_once);
|
||||
RUN_TEST(rubylsp_const_path_is_linear);
|
||||
RUN_TEST(rubylsp_stdlib_seed_survives_alloc_failure);
|
||||
RUN_TEST(rubylsp_cross_file_defs);
|
||||
RUN_TEST(rubylsp_cross_file_ar_repro);
|
||||
RUN_TEST(rubylsp_cross_reopened_class_across_files);
|
||||
RUN_TEST(rubylsp_cross_compact_class_name);
|
||||
RUN_TEST(rubylsp_cross_registry_built_once_serves_many_files);
|
||||
RUN_TEST(rubylsp_cross_reopened_class_work_is_bounded);
|
||||
RUN_TEST(rubylsp_cross_same_named_classes_stay_in_their_package);
|
||||
RUN_TEST(rubylsp_cross_ambiguous_nested_constant_does_not_fall_through);
|
||||
RUN_TEST(rubylsp_cross_registry_is_def_order_independent);
|
||||
RUN_TEST(rubylsp_extractor_flags_singleton_methods_and_mixins);
|
||||
RUN_TEST(rubylsp_cross_singleton_and_instance_do_not_shadow);
|
||||
RUN_TEST(rubylsp_cross_mixin_shadows_superclass_across_files);
|
||||
RUN_TEST(rubylsp_cross_mixin_in_reopening_shadows_superclass);
|
||||
RUN_TEST(rubylsp_cross_unseen_mixin_blocks_superclass);
|
||||
RUN_TEST(rubylsp_cross_ar_finder_same_file_only_by_default);
|
||||
RUN_TEST(rubylsp_cross_ar_finder_crosses_files_when_switched_on);
|
||||
}
|
||||
|
||||
@@ -402,7 +402,7 @@ TEST(type_scope_engine_table_rows) {
|
||||
static const CBMLanguage served[] = {
|
||||
CBM_LANG_GO, CBM_LANG_C, CBM_LANG_CPP, CBM_LANG_CUDA, CBM_LANG_PYTHON,
|
||||
CBM_LANG_JAVASCRIPT, CBM_LANG_TYPESCRIPT, CBM_LANG_TSX, CBM_LANG_PHP, CBM_LANG_CSHARP,
|
||||
CBM_LANG_JAVA, CBM_LANG_KOTLIN, CBM_LANG_RUST};
|
||||
CBM_LANG_JAVA, CBM_LANG_KOTLIN, CBM_LANG_RUST, CBM_LANG_RUBY};
|
||||
int served_count = 0;
|
||||
for (int lang = 0; lang < CBM_LANG_COUNT; lang++) {
|
||||
bool expect = false;
|
||||
@@ -415,7 +415,7 @@ TEST(type_scope_engine_table_rows) {
|
||||
ASSERT_EQ(cbm_pxc_wants_identities((CBMLanguage)lang),
|
||||
cbm_type_scope_lang((CBMLanguage)lang) != NULL);
|
||||
}
|
||||
ASSERT_EQ(served_count, 13);
|
||||
ASSERT_EQ(served_count, 14);
|
||||
ASSERT(cbm_pxc_wants_identities(CBM_LANG_GO));
|
||||
ASSERT(cbm_pxc_wants_identities(CBM_LANG_PYTHON));
|
||||
/* One row, one registry: C, C++ and CUDA share one, as do JavaScript,
|
||||
@@ -443,13 +443,15 @@ TEST(type_scope_engine_table_rows) {
|
||||
ASSERT_NULL(regs.by_engine[e]);
|
||||
}
|
||||
}
|
||||
ASSERT_STR_EQ(built, "go python c cs ts java");
|
||||
ASSERT_STR_EQ(built, "go python c cs ts java ruby");
|
||||
ASSERT(cbm_pxc_registry_for_lang(®s, CBM_LANG_CUDA) == regs.by_engine[c_row]);
|
||||
ASSERT(cbm_pxc_registry_for_lang(®s, CBM_LANG_TSX) == regs.by_engine[js_row]);
|
||||
ASSERT_NULL(cbm_pxc_registry_for_lang(®s, CBM_LANG_RUST)); /* lazy */
|
||||
ASSERT_NULL(cbm_pxc_registry_for_lang(®s, CBM_LANG_KOTLIN));
|
||||
ASSERT_NULL(cbm_pxc_registry_for_lang(®s, CBM_LANG_PHP));
|
||||
ASSERT_NULL(cbm_pxc_registry_for_lang(®s, CBM_LANG_RUBY));
|
||||
ASSERT(cbm_pxc_registry_for_lang(®s, CBM_LANG_RUBY) ==
|
||||
regs.by_engine[cbm_pxc_engine_of_lang(CBM_LANG_RUBY)]);
|
||||
ASSERT_NOT_NULL(cbm_pxc_registry_for_lang(®s, CBM_LANG_RUBY));
|
||||
ASSERT_NULL(cbm_pxc_registry_for_lang(NULL, CBM_LANG_GO));
|
||||
cbm_arena_destroy(&arena);
|
||||
PASS();
|
||||
|
||||
Reference in New Issue
Block a user