Skip to content

Commit 89f0cd4

Browse files
authored
Merge pull request #1647 from rudi193-cmd/fix/725-cross-language-suffix-match
fix(registry): drop suffix_match CALLS across language boundaries
2 parents 22d93dd + 416ba99 commit 89f0cd4

6 files changed

Lines changed: 178 additions & 0 deletions

File tree

‎src/pipeline/pass_calls.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
635635
if (!target_node || source_node->id == target_node->id) {
636636
return 0;
637637
}
638+
/* #725: suffix_match is language-agnostic and will attach a Python
639+
* Store.commit() call to a JS function named commit (or a Bash main
640+
* to a Python main). Drop that weak cross-language edge. */
641+
if (cbm_suppress_cross_language_suffix_match(lang, target_node->file_path, res.strategy)) {
642+
return 0;
643+
}
638644
emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys, imp_vals,
639645
imp_count, tsjs_drop_plain_call);
640646
return SKIP_ONE;

‎src/pipeline/pass_parallel.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,6 +2539,12 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
25392539
}
25402540
atomic_fetch_add_explicit(&rc->time_ns_rc_target, extract_now_ns() - _rc_t0,
25412541
memory_order_relaxed);
2542+
if (target_node && source_node->id != target_node->id &&
2543+
cbm_suppress_cross_language_suffix_match(lang, target_node->file_path, res.strategy)) {
2544+
/* #725: same guard as pass_calls.c — do not emit a suffix_match
2545+
* CALLS edge across a language boundary. */
2546+
continue;
2547+
}
25422548
if (!target_node || source_node->id == target_node->id) {
25432549
/* HTTP/ASYNC calls to an EXTERNAL client library (`requests.get(url)`)
25442550
* resolve to an unindexed QN (target_node == NULL), but their edge

‎src/pipeline/pipeline.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ bool cbm_perl_suppress_generic_match(bool is_perl, bool is_method, const char *c
264264
* Pure; unit-tested in test_registry.c. */
265265
bool cbm_tsjs_suppress_weak_method_match(bool is_tsjs, bool is_method, const char *strategy);
266266

267+
/* #725: drop a suffix_match CALLS edge when the caller language and the
268+
* target file's language disagree. unique_name (candidates == 1) is #1572
269+
* and is left alone; same_module / import_map / lsp_* are kept. JS/TS/TSX
270+
* are one family so a .ts helper calling a .tsx function is not dropped.
271+
* Pure; unit-tested in test_registry.c. */
272+
bool cbm_suppress_cross_language_suffix_match(CBMLanguage caller_lang, const char *target_file_path,
273+
const char *strategy);
274+
267275
/* Get the label of a qualified name, or NULL if not found. */
268276
const char *cbm_registry_label_of(const cbm_registry_t *r, const char *qn);
269277

‎src/pipeline/registry.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,49 @@ bool cbm_tsjs_suppress_weak_method_match(bool is_tsjs, bool is_method, const cha
455455
strcmp(strategy, "field_type_hint") == 0 || strcmp(strategy, "fuzzy") == 0;
456456
}
457457

458+
static bool js_ts_family(CBMLanguage lang) {
459+
return lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT || lang == CBM_LANG_TSX;
460+
}
461+
462+
static const char *path_basename(const char *path) {
463+
if (!path || !path[0]) {
464+
return path;
465+
}
466+
const char *slash = strrchr(path, '/');
467+
#ifdef _WIN32
468+
const char *bslash = strrchr(path, '\\');
469+
if (bslash && (!slash || bslash > slash)) {
470+
slash = bslash;
471+
}
472+
#endif
473+
return slash ? slash + 1 : path;
474+
}
475+
476+
bool cbm_suppress_cross_language_suffix_match(CBMLanguage caller_lang, const char *target_file_path,
477+
const char *strategy) {
478+
/* Two same-named symbols in different languages: suffix_match picks one
479+
* winner by import-distance and attaches every bare-name call to it
480+
* (#725, Bash/Python main, JS/Python commit). unique_name is the
481+
* candidates==1 case (#1572) and is not this guard. */
482+
if (!strategy || strcmp(strategy, "suffix_match") != 0) {
483+
return false;
484+
}
485+
if (caller_lang == CBM_LANG_COUNT || !target_file_path || !target_file_path[0]) {
486+
return false;
487+
}
488+
CBMLanguage target_lang = cbm_language_for_filename(path_basename(target_file_path));
489+
if (target_lang == CBM_LANG_COUNT) {
490+
return false;
491+
}
492+
if (caller_lang == target_lang) {
493+
return false;
494+
}
495+
if (js_ts_family(caller_lang) && js_ts_family(target_lang)) {
496+
return false;
497+
}
498+
return true;
499+
}
500+
458501
/* ── Lifecycle ──────────────────────────────────────────────────── */
459502

460503
cbm_registry_t *cbm_registry_new(void) {

‎tests/test_pipeline.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5893,6 +5893,91 @@ TEST(pipeline_python_cross_module_call) {
58935893
PASS();
58945894
}
58955895

5896+
/* #725: two same-named symbols across languages must not share CALLS edges.
5897+
* Python Store.commit is the real callee of save(); the JS Editor.commit
5898+
* function is a distinct binding and must have no inbound CALLS from Python.
5899+
* unique_name (candidates==1) is #1572 and is not this claim. */
5900+
TEST(pipeline_cross_language_same_name_does_not_share_calls_issue725) {
5901+
const char *files[] = {"store.py", "app.py", "web/src/pages/Editor.js"};
5902+
const char *contents[] = {
5903+
"class Store:\n"
5904+
" def commit(self):\n"
5905+
" return True\n",
5906+
5907+
"from store import Store\n"
5908+
"\n"
5909+
"def save():\n"
5910+
" return Store().commit()\n",
5911+
5912+
"export function commit() {\n"
5913+
" return 1;\n"
5914+
"}\n"};
5915+
5916+
if (setup_lang_repo(files, contents, 3) != 0)
5917+
FAIL("tmpdir");
5918+
char db[512];
5919+
snprintf(db, sizeof(db), "%s/test.db", g_lang_tmpdir);
5920+
5921+
cbm_pipeline_t *p = cbm_pipeline_new(g_lang_tmpdir, db, CBM_MODE_FULL);
5922+
ASSERT_NOT_NULL(p);
5923+
ASSERT_EQ(cbm_pipeline_run(p), 0);
5924+
5925+
cbm_store_t *s = cbm_store_open_path(db);
5926+
ASSERT_NOT_NULL(s);
5927+
const char *proj = cbm_pipeline_project_name(p);
5928+
5929+
cbm_node_t *commits = NULL;
5930+
int ncommit = 0;
5931+
cbm_store_find_nodes_by_name(s, proj, "commit", &commits, &ncommit);
5932+
ASSERT_GTE(ncommit, 2);
5933+
5934+
int64_t js_id = 0;
5935+
int64_t py_id = 0;
5936+
for (int i = 0; i < ncommit; i++) {
5937+
if (commits[i].file_path && strstr(commits[i].file_path, "Editor.js"))
5938+
js_id = commits[i].id;
5939+
if (commits[i].file_path && strstr(commits[i].file_path, "store.py"))
5940+
py_id = commits[i].id;
5941+
}
5942+
ASSERT_TRUE(js_id != 0);
5943+
ASSERT_TRUE(py_id != 0);
5944+
5945+
cbm_node_t *saves = NULL;
5946+
int nsave = 0;
5947+
cbm_store_find_nodes_by_name(s, proj, "save", &saves, &nsave);
5948+
ASSERT_GT(nsave, 0);
5949+
5950+
cbm_edge_t *from_save = NULL;
5951+
int nfrom = 0;
5952+
cbm_store_find_edges_by_source_type(s, saves[0].id, "CALLS", &from_save, &nfrom);
5953+
bool save_calls_py = false;
5954+
bool save_calls_js = false;
5955+
for (int i = 0; i < nfrom; i++) {
5956+
if (from_save[i].target_id == py_id)
5957+
save_calls_py = true;
5958+
if (from_save[i].target_id == js_id)
5959+
save_calls_js = true;
5960+
}
5961+
ASSERT_TRUE(save_calls_py);
5962+
ASSERT_FALSE(save_calls_js);
5963+
5964+
cbm_edge_t *into_js = NULL;
5965+
int njs = 0;
5966+
cbm_store_find_edges_by_target_type(s, js_id, "CALLS", &into_js, &njs);
5967+
ASSERT_EQ(njs, 0);
5968+
5969+
if (from_save)
5970+
cbm_store_free_edges(from_save, nfrom);
5971+
if (into_js)
5972+
cbm_store_free_edges(into_js, njs);
5973+
cbm_store_free_nodes(commits, ncommit);
5974+
cbm_store_free_nodes(saves, nsave);
5975+
cbm_store_close(s);
5976+
cbm_pipeline_free(p);
5977+
teardown_lang_repo();
5978+
PASS();
5979+
}
5980+
58965981
TEST(pipeline_go_type_classification) {
58975982
/* Port of TestGoTypeClassification */
58985983
const char *files[] = {"types.go"};
@@ -11668,6 +11753,7 @@ SUITE(pipeline) {
1166811753
RUN_TEST(pipeline_go_cross_package_call);
1166911754
RUN_TEST(pipeline_swift_cross_package_import);
1167011755
RUN_TEST(pipeline_python_cross_module_call);
11756+
RUN_TEST(pipeline_cross_language_same_name_does_not_share_calls_issue725);
1167111757
RUN_TEST(pipeline_go_type_classification);
1167211758
RUN_TEST(pipeline_go_grouped_types);
1167311759
RUN_TEST(pipeline_kotlin_project);

‎tests/test_registry.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,34 @@ TEST(perl_suppress_keeps_high_confidence_and_genuine_calls) {
761761
PASS();
762762
}
763763

764+
TEST(cross_language_suffix_match_drops_py_vs_js) {
765+
/* #725: two same-named symbols in different languages. suffix_match is the
766+
* strategy that collapses them; unique_name is #1572 and must stay. */
767+
ASSERT_TRUE(cbm_suppress_cross_language_suffix_match(CBM_LANG_PYTHON, "web/src/pages/Editor.js",
768+
"suffix_match"));
769+
ASSERT_TRUE(cbm_suppress_cross_language_suffix_match(CBM_LANG_JAVASCRIPT, "store.py",
770+
"suffix_match"));
771+
ASSERT_TRUE(cbm_suppress_cross_language_suffix_match(CBM_LANG_BASH, "cli/main.py",
772+
"suffix_match"));
773+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_PYTHON, "store.py",
774+
"suffix_match"));
775+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_PYTHON, "web/src/pages/Editor.js",
776+
"unique_name"));
777+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_PYTHON, "web/src/pages/Editor.js",
778+
"same_module"));
779+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_PYTHON, "web/src/pages/Editor.js",
780+
"import_map"));
781+
/* JS/TS/TSX are one family. */
782+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_JAVASCRIPT, "lib/util.ts",
783+
"suffix_match"));
784+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_TYPESCRIPT, "ui/Panel.tsx",
785+
"suffix_match"));
786+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_PYTHON, NULL, "suffix_match"));
787+
ASSERT_FALSE(cbm_suppress_cross_language_suffix_match(CBM_LANG_COUNT, "store.py",
788+
"suffix_match"));
789+
PASS();
790+
}
791+
764792
TEST(tsjs_suppress_drops_weak_method_matches) {
765793
/* #592/#606: a TS/JS member call whose receiver the LSP could not type, that
766794
* landed via a WEAK short-name strategy, is generic-resolver noise → drop.
@@ -893,6 +921,7 @@ SUITE(registry) {
893921
RUN_TEST(perl_builtin_set_rejects_project_subs);
894922
RUN_TEST(perl_suppress_drops_weak_builtin_and_method_matches);
895923
RUN_TEST(perl_suppress_keeps_high_confidence_and_genuine_calls);
924+
RUN_TEST(cross_language_suffix_match_drops_py_vs_js);
896925
RUN_TEST(tsjs_suppress_drops_weak_method_matches);
897926
RUN_TEST(tsjs_suppress_keeps_high_confidence_and_non_methods);
898927
}

0 commit comments

Comments
 (0)