Skip to content

Commit 9faa3e5

Browse files
authored
Merge pull request #1915 from ilyabrykau-orca/fix/go-init-qn
fix(extract): give each Go init() its own QN
2 parents 9f0fdcb + 8c62cb3 commit 9faa3e5

14 files changed

Lines changed: 646 additions & 41 deletions

‎internal/cbm/cbm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3924,7 +3924,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
39243924
uint64_t lsp_start = now_ns();
39253925
if (!result->lsp_skipped) {
39263926
if (language == CBM_LANG_GO) {
3927-
cbm_run_go_lsp(a, result, source, source_len, root);
3927+
cbm_run_go_lsp(a, result, source, source_len, root, rel_path);
39283928
}
39293929
if (language == CBM_LANG_C || language == CBM_LANG_CPP || language == CBM_LANG_CUDA) {
39303930
if (!cbm_run_c_lsp_with_test_owners(a, result, source, source_len, root,

‎internal/cbm/extract_defs.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7061,6 +7061,24 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
70617061
}
70627062
}
70637063

7064+
/* Go allows any number of init() functions per package — even several in
7065+
* one file — and they all run at start-up. On the flat QN they all
7066+
* collided and the graph upsert kept ONE node per package, silently
7067+
* dropping the rest (#1910). Disambiguate with the #495 cfg-twin pattern:
7068+
* fold the file basename and the init's per-file ordinal into the QN.
7069+
* The ordinal (not the line) keeps the QN stable when code is edited
7070+
* above the function — the QN is node identity, and a line suffix would
7071+
* churn nodes on every unrelated insertion. Nothing ever joins on the
7072+
* plain QN — calling init explicitly is illegal in Go — and the
7073+
* call-scope side (compute_func_qn in extract_unified.c) and the Go LSP
7074+
* (process_function in lsp/go_lsp.c) name it through the same
7075+
* cbm_go_init_qn, so init-body calls keep their attribution. */
7076+
if (ctx->language == CBM_LANG_GO && strcmp(def.label, "Function") == 0 &&
7077+
strcmp(name, "init") == 0) {
7078+
def.qualified_name =
7079+
cbm_go_init_qn(a, def.qualified_name, ctx->rel_path, node, ctx->source);
7080+
}
7081+
70647082
// C++/CUDA: out-of-line method definition (`Foo::bar` in a .cc/.cpp). The
70657083
// class body in the header is declaration-only, so without this the
70667084
// definition is recorded as a free Function. Promote it to a Method whose QN

‎internal/cbm/extract_unified.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,23 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
11331133
}
11341134
}
11351135

1136+
/* Go init(): the def extractor folds the file basename and the init's
1137+
* per-file ordinal into the QN so every init in a package survives the
1138+
* upsert (#1910, the #495 cfg-twin pattern; ordinal, not line, so the QN
1139+
* is stable under edits above the function). Name it through the same
1140+
* cbm_go_init_qn, or init-body calls fall back to File-node attribution
1141+
* (calls_find_source). */
1142+
if (ctx->language == CBM_LANG_GO && strcmp(name, "init") == 0 &&
1143+
strcmp(ts_node_type(node), "function_declaration") == 0) {
1144+
const char *base_qn = cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path,
1145+
name, ctx->language);
1146+
/* NULL only on allocation failure: fall through to the flat QN, as
1147+
* the receiver branch above does. */
1148+
if (base_qn) {
1149+
return cbm_go_init_qn(ctx->arena, base_qn, ctx->rel_path, node, ctx->source);
1150+
}
1151+
}
1152+
11361153
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def
11371154
* extractor bakes it into the def QN. Compose it identically here — otherwise
11381155
* an in-body call sources to a QN one or more segments short of the def, and

‎internal/cbm/helpers.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,6 +2330,65 @@ int cbm_classify_string(const char *str, int len) {
23302330
return NOT_FOUND;
23312331
}
23322332

2333+
/* 1-based source-order ordinal of a top-level Go `init` function_declaration
2334+
* among the file's `init`s. Unlike a line number, the ordinal is stable under
2335+
* edits above the function — QNs are node identity, and a line-based suffix
2336+
* would churn nodes on every unrelated insertion (the #495 cfg suffix and the
2337+
* GoogleTest derivation are both edit-stable for the same reason).
2338+
* One cursor pass over the top-level declarations: ts_node_named_child(root, i)
2339+
* is O(i), so an indexed loop is O(n^2) per init on a wide file, and generated
2340+
* Go (.pb.go, tables) carries thousands of top-level declarations next to its
2341+
* init(). */
2342+
static int go_init_ordinal(TSNode fn_node, const char *source) {
2343+
TSNode root = fn_node;
2344+
for (TSNode p = ts_node_parent(root); !ts_node_is_null(p); p = ts_node_parent(p)) {
2345+
root = p;
2346+
}
2347+
int ordinal = 0;
2348+
TSTreeCursor cur = ts_tree_cursor_new(root);
2349+
if (ts_tree_cursor_goto_first_child(&cur)) {
2350+
do {
2351+
TSNode child = ts_tree_cursor_current_node(&cur);
2352+
if (strcmp(ts_node_type(child), "function_declaration") != 0) {
2353+
continue;
2354+
}
2355+
TSNode name = ts_node_child_by_field_name(child, TS_FIELD("name"));
2356+
if (ts_node_is_null(name)) {
2357+
continue;
2358+
}
2359+
uint32_t start = ts_node_start_byte(name);
2360+
if (ts_node_end_byte(name) - start != 4 || memcmp(source + start, "init", 4) != 0) {
2361+
continue;
2362+
}
2363+
ordinal++;
2364+
if (ts_node_eq(child, fn_node)) {
2365+
break;
2366+
}
2367+
} while (ts_tree_cursor_goto_next_sibling(&cur));
2368+
}
2369+
ts_tree_cursor_delete(&cur);
2370+
return ordinal;
2371+
}
2372+
2373+
/* Qualified name of a Go `init` function (#1910). Go allows any number of
2374+
* init() per package — even several in one file — so the flat `pkg.init`
2375+
* collides and the graph upsert keeps one node. The QN folds in the file
2376+
* basename and the init's per-file ordinal: `pkg.init#file.go:N`. Every side
2377+
* that names an init calls this one function — the definition extractor, the
2378+
* call-scope side (compute_func_qn) and the Go LSP's enclosing-function QN —
2379+
* so the three cannot drift. A NULL base QN or path keeps base_qn as is. */
2380+
const char *cbm_go_init_qn(CBMArena *a, const char *base_qn, const char *rel_path, TSNode fn_node,
2381+
const char *source) {
2382+
if (!base_qn || !rel_path) {
2383+
return base_qn;
2384+
}
2385+
const char *base = strrchr(rel_path, '/');
2386+
base = base ? base + 1 : rel_path;
2387+
const char *qn =
2388+
cbm_arena_sprintf(a, "%s#%s:%d", base_qn, base, go_init_ordinal(fn_node, source));
2389+
return qn ? qn : base_qn;
2390+
}
2391+
23332392
/* Flatten a JS/TS `template_string` node into plain text (issue #1006).
23342393
* String fragments are kept verbatim; each ${...} substitution becomes the
23352394
* "{}" placeholder so client URLs built from template literals share the

‎internal/cbm/helpers.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ bool cbm_is_module_level(TSNode node, CBMLanguage lang);
287287
// node being classified.
288288
bool cbm_is_module_level_p(TSNode parent, CBMLanguage lang);
289289

290+
/* QN of a Go `init` function_declaration (#1910): base_qn + "#<file basename>:<N>",
291+
* N = its 1-based ordinal among the file's top-level `init`s. The ONE formula
292+
* for the def extractor, compute_func_qn and the Go LSP's enclosing-function
293+
* QN. A NULL base_qn or rel_path returns base_qn unchanged. */
294+
const char *cbm_go_init_qn(CBMArena *a, const char *base_qn, const char *rel_path, TSNode fn_node,
295+
const char *source);
296+
290297
// --- FQN computation ---
291298

292299
// Compute qualified name: project.rel_path_parts.name

‎internal/cbm/lsp/go_lsp.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,8 @@ static void process_function(GoLSPContext *ctx, TSNode func_node) {
19941994
// resolver. Since #1909 receiver-qualified Go method QNs, that shared
19951995
// form is package_qn.ReceiverType.method for methods (the ONE-formula
19961996
// contract: cbm_go_receiver_type_name) and package_qn.func for functions.
1997+
// A package may hold any number of init() functions, so each one is
1998+
// package_qn.init#<file>:<N> (#1910, cbm_go_init_qn — the same formula).
19971999
ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->package_qn, func_name);
19982000
if (strcmp(ts_node_type(func_node), "method_declaration") == 0) {
19992001
TSNode recv = ts_node_child_by_field_name(func_node, "receiver", 8);
@@ -2004,6 +2006,9 @@ static void process_function(GoLSPContext *ctx, TSNode func_node) {
20042006
recv_type, func_name);
20052007
}
20062008
}
2009+
} else if (strcmp(func_name, "init") == 0) {
2010+
ctx->enclosing_func_qn = cbm_go_init_qn(ctx->arena, ctx->enclosing_func_qn, ctx->rel_path,
2011+
func_node, ctx->source);
20072012
}
20082013

20092014
// Push function scope
@@ -2261,7 +2266,7 @@ static void go_registry_with_stdlib(CBMTypeRegistry *reg, CBMArena *arena) {
22612266
// --- Entry point: build registry from file defs + run LSP ---
22622267

22632268
void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
2264-
TSNode root) {
2269+
TSNode root, const char *rel_path) {
22652270

22662271
CBMTypeRegistry reg;
22672272
cbm_registry_init(&reg, arena);
@@ -2619,6 +2624,7 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
26192624
// Phase 2: Build LSP context and run
26202625
GoLSPContext lsp_ctx;
26212626
go_lsp_init(&lsp_ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
2627+
lsp_ctx.rel_path = rel_path;
26222628

26232629
// Add imports
26242630
for (int i = 0; i < result->imports.count; i++) {
@@ -3239,9 +3245,9 @@ static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg,
32393245
extern const TSLanguage *tree_sitter_go(void);
32403246

32413247
void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
3242-
const char *module_qn, CBMLSPDef *defs, int def_count,
3243-
const char **import_names, const char **import_qns, int import_count,
3244-
TSTree *cached_tree, CBMResolvedCallArray *out) {
3248+
const char *module_qn, const char *rel_path, CBMLSPDef *defs,
3249+
int def_count, const char **import_names, const char **import_qns,
3250+
int import_count, TSTree *cached_tree, CBMResolvedCallArray *out) {
32453251
if (!source || source_len <= 0)
32463252
return;
32473253

@@ -3505,6 +3511,7 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
35053511
// 4. Build LSP context and run
35063512
GoLSPContext ctx;
35073513
go_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, out);
3514+
ctx.rel_path = rel_path;
35083515

35093516
for (int i = 0; i < import_count; i++) {
35103517
if (import_names[i] && import_qns[i]) {
@@ -3647,10 +3654,10 @@ CBMTypeRegistry *cbm_go_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, i
36473654
}
36483655

36493656
void cbm_run_go_lsp_cross_with_registry(CBMArena *arena, const char *source, int source_len,
3650-
const char *module_qn, CBMTypeRegistry *reg,
3651-
const char **import_names, const char **import_qns,
3652-
int import_count, TSTree *cached_tree,
3653-
CBMResolvedCallArray *out) {
3657+
const char *module_qn, const char *rel_path,
3658+
CBMTypeRegistry *reg, const char **import_names,
3659+
const char **import_qns, int import_count,
3660+
TSTree *cached_tree, CBMResolvedCallArray *out) {
36543661
if (!source || source_len <= 0 || !reg)
36553662
return;
36563663

@@ -3676,6 +3683,7 @@ void cbm_run_go_lsp_cross_with_registry(CBMArena *arena, const char *source, int
36763683

36773684
GoLSPContext ctx;
36783685
go_lsp_init(&ctx, arena, source, source_len, reg, module_qn, out);
3686+
ctx.rel_path = rel_path;
36793687
for (int i = 0; i < import_count; i++) {
36803688
if (import_names[i] && import_qns[i]) {
36813689
go_lsp_add_import(&ctx, import_names[i], import_qns[i]);
@@ -3825,8 +3833,8 @@ void cbm_batch_go_lsp_cross(CBMArena *arena, CBMBatchGoLSPFile *files, int file_
38253833

38263834
// Delegate to existing per-file function
38273835
cbm_run_go_lsp_cross(&file_arena, file->source, file->source_len, file->module_qn,
3828-
file->defs, file->def_count, file->import_names, file->import_qns,
3829-
file->import_count, file->cached_tree, &file_out);
3836+
file->rel_path, file->defs, file->def_count, file->import_names,
3837+
file->import_qns, file->import_count, file->cached_tree, &file_out);
38303838

38313839
// Copy results to output arena (must outlive per-file arena)
38323840
if (file_out.count > 0) {

‎internal/cbm/lsp/go_lsp.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef struct {
2222
// Current function context
2323
const char* enclosing_func_qn;
2424
const char* package_qn; // module QN for this file
25+
const char* rel_path; // repo-relative file path: init() QN suffix (#1910); NULL = flat
2526

2627
// Output: resolved calls accumulate here
2728
CBMResolvedCallArray* resolved_calls;
@@ -63,8 +64,10 @@ const CBMRegisteredFunc* go_lookup_field_or_method(GoLSPContext* ctx,
6364

6465
// Entry point: build registry from file's own defs + run LSP resolution.
6566
// Called from cbm_extract_file() after definitions and imports are extracted.
67+
// rel_path is the file's repo-relative path: it names the file's init()
68+
// functions exactly as the def extractor does (cbm_go_init_qn, #1910).
6669
void cbm_run_go_lsp(CBMArena* arena, CBMFileResult* result,
67-
const char* source, int source_len, TSNode root);
70+
const char* source, int source_len, TSNode root, const char* rel_path);
6871

6972
// Register Go stdlib types and functions into a registry.
7073
// Auto-generated by scripts/gen-go-stdlib.go.
@@ -112,11 +115,14 @@ typedef struct {
112115
// Re-parses source with tree-sitter internally.
113116
// defs includes both file-local and cross-file definitions.
114117
// import_names/import_qns are parallel arrays of length import_count.
118+
// rel_path: the file's repo-relative path, for its init() QNs (#1910); NULL
119+
// leaves init callers flat (they then miss the caller join).
115120
// Results written to out (arena-allocated).
116121
void cbm_run_go_lsp_cross(
117122
CBMArena* arena,
118123
const char* source, int source_len,
119124
const char* module_qn,
125+
const char* rel_path,
120126
CBMLSPDef* defs, int def_count,
121127
const char** import_names, const char** import_qns, int import_count,
122128
TSTree* cached_tree, // NULL = parse internally
@@ -135,6 +141,7 @@ void cbm_run_go_lsp_cross_with_registry(
135141
CBMArena* arena,
136142
const char* source, int source_len,
137143
const char* module_qn,
144+
const char* rel_path, // see cbm_run_go_lsp_cross
138145
CBMTypeRegistry* reg, // pre-built, finalized, READ-ONLY
139146
const char** import_names, const char** import_qns, int import_count,
140147
TSTree* cached_tree, // NULL = parse internally
@@ -160,6 +167,7 @@ typedef struct {
160167
const char* source;
161168
int source_len;
162169
const char* module_qn;
170+
const char* rel_path; // repo-relative path (init() QNs, #1910); NULL = flat
163171
TSTree* cached_tree; // from TSTree caching (NULL = parse internally)
164172
CBMLSPDef* defs; // combined file-local + cross-file defs
165173
int def_count;

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,14 +2082,15 @@ static CBMRustLSPDef *pxc_lspdefs_to_rust(CBMArena *arena, const CBMLSPDef *defs
20822082
void cbm_pxc_run_one(CBMLanguage lang, CBMFileResult *r, const char *source, int source_len,
20832083
const char *module_qn, CBMLSPDef *defs, int def_count, const char **imp_names,
20842084
const char **imp_qns, int imp_count) {
2085-
cbm_pxc_run_one_with_php_bindings(lang, r, source, source_len, module_qn, defs, def_count,
2085+
cbm_pxc_run_one_with_php_bindings(lang, r, source, source_len, module_qn, NULL, defs, def_count,
20862086
imp_names, imp_qns, imp_count, NULL);
20872087
}
20882088

20892089
void cbm_pxc_run_one_with_php_bindings(CBMLanguage lang, CBMFileResult *r, const char *source,
2090-
int source_len, const char *module_qn, CBMLSPDef *defs,
2091-
int def_count, const char **imp_names, const char **imp_qns,
2092-
int imp_count, const CBMPHPImportBindings *bindings) {
2090+
int source_len, const char *module_qn, const char *rel_path,
2091+
CBMLSPDef *defs, int def_count, const char **imp_names,
2092+
const char **imp_qns, int imp_count,
2093+
const CBMPHPImportBindings *bindings) {
20932094
TSTree *tree = r->cached_tree; /* may be NULL — LSP re-parses then */
20942095

20952096
CBMArena scratch;
@@ -2101,8 +2102,8 @@ void cbm_pxc_run_one_with_php_bindings(CBMLanguage lang, CBMFileResult *r, const
21012102

21022103
switch (lang) {
21032104
case CBM_LANG_GO:
2104-
cbm_run_go_lsp_cross(&scratch, source, source_len, module_qn, defs, def_count, imp_names,
2105-
imp_qns, imp_count, tree, &out);
2105+
cbm_run_go_lsp_cross(&scratch, source, source_len, module_qn, rel_path, defs, def_count,
2106+
imp_names, imp_qns, imp_count, tree, &out);
21062107
break;
21072108
case CBM_LANG_C:
21082109
case CBM_LANG_CPP:
@@ -2296,9 +2297,9 @@ void cbm_pxc_dispatch_file_with_php_bindings(
22962297
* lsp_unresolved entries -- no parse, no AST walk -- then the
22972298
* AST walk against the overlay (chained to the sealed base). */
22982299
cbm_go_fast_resolve_qualified_calls(result, prebuilt, imp_keys, imp_vals, imp_count);
2299-
cbm_run_go_lsp_cross_with_registry(&scratch, source, source_len, def_module, &overlay,
2300-
imp_keys, imp_vals, imp_count, result->cached_tree,
2301-
&out);
2300+
cbm_run_go_lsp_cross_with_registry(&scratch, source, source_len, def_module, rel,
2301+
&overlay, imp_keys, imp_vals, imp_count,
2302+
result->cached_tree, &out);
23022303
used_prebuilt = true;
23032304
break;
23042305
case CBM_LANG_PYTHON: {
@@ -2432,7 +2433,7 @@ void cbm_pxc_dispatch_file_with_php_bindings(
24322433
pxc_append_synthetic_calls(&result->arena, &result->calls, &synthetic_calls);
24332434
pxc_scratch_give(PXC_SCRATCH_DISPATCH, &scratch);
24342435
} else {
2435-
cbm_pxc_run_one_with_php_bindings(lang, result, source, source_len, def_module,
2436+
cbm_pxc_run_one_with_php_bindings(lang, result, source, source_len, def_module, rel,
24362437
file_defs, file_def_count, imp_keys, imp_vals,
24372438
imp_count, bindings);
24382439
}
@@ -2444,8 +2445,9 @@ void cbm_pxc_dispatch_file_with_php_bindings(
24442445
cbm_pxc_run_one_ts(result, source, source_len, def_module, file_defs, file_def_count,
24452446
imp_keys, imp_vals, imp_count, js, jsx, dts);
24462447
} else {
2447-
cbm_pxc_run_one_with_php_bindings(lang, result, source, source_len, def_module, file_defs,
2448-
file_def_count, imp_keys, imp_vals, imp_count, bindings);
2448+
cbm_pxc_run_one_with_php_bindings(lang, result, source, source_len, def_module, rel,
2449+
file_defs, file_def_count, imp_keys, imp_vals, imp_count,
2450+
bindings);
24492451
}
24502452
free(filtered);
24512453
}

0 commit comments

Comments
 (0)