Skip to content

Commit cdd8c89

Browse files
committed
fix(cpp): resolve Class::member calls across files (DeusData#1153)
A method called on the object a static factory returns got no CALLS edge when the class lives in another file: `SecdManager::getInstance()->m()`, `Registry::instance().m()`, and `auto w = Widget::create(); w->m()` with std::unique_ptr or std::shared_ptr. The same code in a single file already resolved. Why: the cross-file C++ LSP resolved the scope of `Class::member` only as the bare QN or under the caller's own module. A class declared in a header and defined out of line in a .cpp is registered under that file's module (`<proj>.app.src.SecdManager.SecdManager`), so the static call missed and fell to a name heuristic (unique_name 0.38, qualified_suffix). Its return type stayed unknown, and every receiver built from it dropped the method call. A name heuristic on the same site could also bind a same-named function elsewhere: luabridge's `UserdataSharedHelper<C, false>::push(...)` was bound to the free function `luabridge::push`. Fix: once every existing lookup of `Class::member` has missed, resolve the scope to exactly one registered class (bare, caller module, then a type whose QN ends in `.<scope>` at a segment boundary) and look the member up on that class: direct, aliases, bases, never re-anchored onto a same-named class elsewhere. Two classes with that name name no single target, so the call stays unresolved. The same resolution types the call, so the declared return type (pointer, reference, unique_ptr, shared_ptr) reaches chained and auto receivers. C/C++ LSP only; no call that resolved before changes. Proof, qBittorrent e8f7c18b (628 C/C++ files), same machine, fresh index with a binary built from origin/main and one with this change: - CALLS 17,401 -> 17,889: +489 edges (487 lsp_type_dispatch, 2 lsp_scoped) and -1 edge, the suffix_match `luabridge::push` false positive above. - 295 existing edges now come from the LSP with the same target (268 qualified_suffix, 27 unique_name -> lsp_scoped). - All 489 added edges checked against their source line: 155 are `Class::f()->m()` or `Class::m()` on that line, 334 go through a receiver initialised from `Class::instance()` in the same function (Preferences, SettingsStorage, UIThemeManager, Logger). None binds a different class. - `X::instance()->m()` sites whose enclosing function has the edge: 4/461 -> 304/461. Namespaced scopes (`RSS::Session::instance()`) stay unresolved: forward declarations mint extra Class nodes, so the scope does not name one class. Tests (pipeline suite; each runs the sequential and the parallel path): one per shape (pointer, reference, unique_ptr, shared_ptr), plus controls: a class with the same method names is never bound, and an undefined receiver type and a class name two libraries declare both stay unresolved. The fixture asserts that every node it names exists, so a missing node cannot pass as a missing edge. RED 3/3 on main, green with the fix, RED again on revert; dropping the uniqueness rule turns the ambiguity control RED. The Python half of DeusData#1153, a method call on a module-level instance (`manager = StackManager(); manager.status()`), is fixed by the held Python LSP change 53653c15, not here. Refs DeusData#1153 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 2278498 commit cdd8c89

2 files changed

Lines changed: 485 additions & 0 deletions

File tree

‎internal/cbm/lsp/c_lsp.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,53 @@ static const CBMType *c_resolve_name_to_type(CLSPContext *ctx, const char *name)
10251025
return cbm_type_named(ctx->arena, name);
10261026
}
10271027

1028+
/* Resolve the class of a qualified member `Scope::member` -- scope_qn is the
1029+
* dotted scope ("SecdManager", "ns.Widget") -- to the QN of ONE registered
1030+
* class, or NULL. The bare and caller-module spellings come first. A class
1031+
* declared in another file is registered under that file's module
1032+
* ("<proj>.app.src.SecdManager.SecdManager"), which neither spelling reaches,
1033+
* so then every registered type whose QN ends in ".<scope_qn>" at a segment
1034+
* boundary is a candidate. More than one distinct candidate (two libraries
1035+
* each declaring `Twin`) names no single class: the call stays unresolved
1036+
* rather than bind one of them by guess. */
1037+
static const char *c_resolve_scope_class_qn(CLSPContext *ctx, const char *scope_qn) {
1038+
if (!ctx->registry || !scope_qn || !scope_qn[0]) {
1039+
return NULL;
1040+
}
1041+
if (cbm_registry_lookup_type(ctx->registry, scope_qn)) {
1042+
return scope_qn;
1043+
}
1044+
if (ctx->module_qn) {
1045+
const char *mod_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, scope_qn);
1046+
if (cbm_registry_lookup_type(ctx->registry, mod_qn)) {
1047+
return mod_qn;
1048+
}
1049+
}
1050+
const char *last_dot = strrchr(scope_qn, '.');
1051+
const char *short_name = last_dot ? last_dot + 1 : scope_qn;
1052+
size_t scope_len = strlen(scope_qn);
1053+
const char *found = NULL;
1054+
CBMTypeShortIter it;
1055+
cbm_registry_types_by_short_name_chain(ctx->registry, short_name, &it);
1056+
int i;
1057+
while ((i = cbm_type_short_iter_next(&it)) >= 0) {
1058+
const char *q = it.reg->types[i].qualified_name;
1059+
size_t qlen = q ? strlen(q) : 0;
1060+
if (qlen <= scope_len || q[qlen - scope_len - 1] != '.' ||
1061+
strcmp(q + qlen - scope_len, scope_qn) != 0) {
1062+
continue;
1063+
}
1064+
if (found && strcmp(found, q) != 0) {
1065+
return NULL;
1066+
}
1067+
found = q;
1068+
}
1069+
return found;
1070+
}
1071+
1072+
static const CBMRegisteredFunc *c_lookup_scope_member(CLSPContext *ctx, const char *class_qn,
1073+
const char *member_name);
1074+
10281075
// ============================================================================
10291076
// c_parse_type_node: AST type node -> CBMType
10301077
// ============================================================================
@@ -1821,6 +1868,16 @@ static const CBMType *c_eval_expr_type_inner(CLSPContext *ctx, TSNode node) {
18211868
}
18221869
}
18231870
}
1871+
1872+
// Class::member of a class declared in another file: its signature
1873+
// carries the return type a chained or auto receiver dispatches on.
1874+
const char *scope_class =
1875+
c_resolve_scope_class_qn(ctx, cbm_arena_strndup(ctx->arena, qn, prefix_len));
1876+
const CBMRegisteredFunc *sm =
1877+
scope_class ? c_lookup_scope_member(ctx, scope_class, dot + 1) : NULL;
1878+
if (sm && sm->signature) {
1879+
return sm->signature;
1880+
}
18241881
}
18251882

18261883
return cbm_type_unknown();
@@ -2860,6 +2917,15 @@ const CBMRegisteredFunc *c_lookup_member(CLSPContext *ctx, const char *type_qn,
28602917
return c_lookup_member_depth(ctx, type_qn, member_name, 0);
28612918
}
28622919

2920+
/* A member of a class c_resolve_scope_class_qn already pinned down: direct,
2921+
* then through its aliases and base classes. Entering below depth 0 skips the
2922+
* short-name fallback, which would re-anchor the lookup onto a same-named
2923+
* class elsewhere -- the scope names exactly this class. */
2924+
static const CBMRegisteredFunc *c_lookup_scope_member(CLSPContext *ctx, const char *class_qn,
2925+
const char *member_name) {
2926+
return c_lookup_member_depth(ctx, class_qn, member_name, 1);
2927+
}
2928+
28632929
// True if any BASE class of type_qn (not type_qn itself) declares member_name —
28642930
// i.e. a method found directly on type_qn is an OVERRIDE of an inherited method.
28652931
// This mirrors the existing virtual-dispatch notion (a derived override of a base
@@ -4122,6 +4188,24 @@ static void c_resolve_calls_in_node_inner(CLSPContext *ctx, TSNode node) {
41224188
goto recurse;
41234189
}
41244190
}
4191+
// Class::method of a class declared in another file (#1153).
4192+
if (dot) {
4193+
const char *scope_class = c_resolve_scope_class_qn(
4194+
ctx, cbm_arena_strndup(ctx->arena, qn, (size_t)(dot - qn)));
4195+
const CBMRegisteredFunc *sm = NULL;
4196+
if (scope_class) {
4197+
sm = cbm_registry_lookup_method_by_types(ctx->registry, scope_class,
4198+
dot + 1, arg_types, arg_count);
4199+
if (!sm) {
4200+
sm = c_lookup_scope_member(ctx, scope_class, dot + 1);
4201+
}
4202+
}
4203+
if (sm) {
4204+
c_emit_resolved_call_at(ctx, sm->qualified_name, "lsp_scoped", 0.95f,
4205+
node);
4206+
goto recurse;
4207+
}
4208+
}
41254209
c_emit_unresolved_call(ctx, qn, "scoped_not_in_registry");
41264210
}
41274211
goto recurse;

0 commit comments

Comments
 (0)