Skip to content

Commit 26e6e9d

Browse files
committed
fix(java): bind a call to the copy of a class in the caller's build module
A repository with several Gradle or Maven modules often declares the same package and class in more than one module. Every resolver strategy (the LSP tail match, field_type_hint, suffix_match, import_map) then picked one copy, and callers in every module were bound to it. A build root is the nearest ancestor folder that holds settings.gradle(.kts), build.gradle(.kts) or pom.xml. When a Java caller resolves to a node in another build root, and the caller's own build root holds the same file path with the same qualified-name tail, name and label, the call binds to that node. In all other cases the result is unchanged. Both call resolvers apply the step at the point where the target node is final (pass_calls.c, which the incremental path also uses, and pass_parallel.c). Build-root lookups are cached per thread and per directory, and the cache is cleared at the start and end of each resolver run. No QN or schema change. Tests: pipeline_java_build_modules_bind_own_copy_{sequential,parallel}. Two modules each declare com.acme.Util. Before this change, both modules' callers bound to one module's copy. After it, each caller binds to its own copy. Signed-off-by: chidev <[email protected]>
1 parent cf70a88 commit 26e6e9d

4 files changed

Lines changed: 368 additions & 0 deletions

File tree

‎src/pipeline/pass_calls.c‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,168 @@ static const cbm_gbuf_node_t *calls_field_target(cbm_pipeline_ctx_t *ctx, const
10351035
return target_node;
10361036
}
10371037

1038+
/* ── Java build roots (see pipeline_internal.h) ─────────────────────
1039+
*
1040+
* This thread's answers, keyed by the repository-relative directory and
1041+
* valued by its build root ("" = the repository root, or no build file
1042+
* anywhere above). tl_br_repo names the repository they belong to. */
1043+
static CBM_TLS CBMHashTable *tl_br_cache;
1044+
static CBM_TLS char *tl_br_repo;
1045+
1046+
static void br_free_entry(const char *key, void *value, void *ud) {
1047+
(void)ud;
1048+
free((void *)key);
1049+
free(value);
1050+
}
1051+
1052+
void cbm_pipeline_build_root_cache_clear(void) {
1053+
if (tl_br_cache) {
1054+
cbm_ht_foreach(tl_br_cache, br_free_entry, NULL);
1055+
cbm_ht_free(tl_br_cache);
1056+
tl_br_cache = NULL;
1057+
}
1058+
free(tl_br_repo);
1059+
tl_br_repo = NULL;
1060+
}
1061+
1062+
static bool br_dir_has_build_file(const char *repo_path, const char *dir) {
1063+
static const char *const files[] = {"settings.gradle", "settings.gradle.kts", "build.gradle",
1064+
"build.gradle.kts", "pom.xml"};
1065+
char path[CBM_SZ_4K];
1066+
for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
1067+
int n = snprintf(path, sizeof(path), "%s%s%s/%s", repo_path, dir[0] ? "/" : "", dir,
1068+
files[i]);
1069+
if (n <= 0 || (size_t)n >= sizeof(path)) {
1070+
continue;
1071+
}
1072+
cbm_path_info_t info;
1073+
if (cbm_path_info_utf8(path, &info) == CBM_PATH_INFO_OK && info.is_regular) {
1074+
return true;
1075+
}
1076+
}
1077+
return false;
1078+
}
1079+
1080+
/* The build root of repository-relative directory `dir` (cached). NULL only
1081+
* when memory runs out. */
1082+
static const char *br_root_of_dir(const char *repo_path, const char *dir) {
1083+
if (!tl_br_repo || strcmp(tl_br_repo, repo_path) != 0) {
1084+
cbm_pipeline_build_root_cache_clear();
1085+
tl_br_repo = strdup(repo_path);
1086+
tl_br_cache = cbm_ht_create(CBM_SZ_256);
1087+
if (!tl_br_repo || !tl_br_cache) {
1088+
cbm_pipeline_build_root_cache_clear();
1089+
return NULL;
1090+
}
1091+
}
1092+
const char *hit = cbm_ht_get(tl_br_cache, dir);
1093+
if (hit) {
1094+
return hit;
1095+
}
1096+
const char *root = NULL;
1097+
char *owned = NULL;
1098+
if (br_dir_has_build_file(repo_path, dir)) {
1099+
owned = strdup(dir);
1100+
} else if (dir[0]) {
1101+
char parent[CBM_SZ_4K];
1102+
snprintf(parent, sizeof(parent), "%s", dir);
1103+
char *slash = strrchr(parent, '/');
1104+
if (slash) {
1105+
*slash = '\0';
1106+
} else {
1107+
parent[0] = '\0';
1108+
}
1109+
root = br_root_of_dir(repo_path, parent);
1110+
owned = root ? strdup(root) : NULL;
1111+
} else {
1112+
owned = strdup("");
1113+
}
1114+
char *key = strdup(dir);
1115+
if (!owned || !key) {
1116+
free(owned);
1117+
free(key);
1118+
return NULL;
1119+
}
1120+
cbm_ht_set(tl_br_cache, key, owned);
1121+
return owned;
1122+
}
1123+
1124+
/* Build root of a repository-relative file path, copied into out. */
1125+
static bool br_root_of_file(const char *repo_path, const char *rel, char *out, size_t out_size) {
1126+
char dir[CBM_SZ_4K];
1127+
int n = snprintf(dir, sizeof(dir), "%s", rel);
1128+
if (n < 0 || (size_t)n >= sizeof(dir)) {
1129+
return false;
1130+
}
1131+
char *slash = strrchr(dir, '/');
1132+
if (slash) {
1133+
*slash = '\0';
1134+
} else {
1135+
dir[0] = '\0';
1136+
}
1137+
const char *root = br_root_of_dir(repo_path, dir);
1138+
if (!root) {
1139+
return false;
1140+
}
1141+
n = snprintf(out, out_size, "%s", root);
1142+
return n >= 0 && (size_t)n < out_size;
1143+
}
1144+
1145+
const cbm_gbuf_node_t *cbm_pipeline_java_same_build_root(const cbm_gbuf_t *gbuf,
1146+
const char *project,
1147+
const char *repo_path, CBMLanguage lang,
1148+
const char *caller_rel,
1149+
const cbm_gbuf_node_t *target) {
1150+
if (lang != CBM_LANG_JAVA || !gbuf || !project || !repo_path || !repo_path[0] ||
1151+
!caller_rel || !target || !target->file_path || !target->qualified_name ||
1152+
!target->name || !target->label) {
1153+
return target;
1154+
}
1155+
char croot[CBM_SZ_4K];
1156+
char troot[CBM_SZ_4K];
1157+
if (!br_root_of_file(repo_path, caller_rel, croot, sizeof(croot)) ||
1158+
!br_root_of_file(repo_path, target->file_path, troot, sizeof(troot)) ||
1159+
strcmp(croot, troot) == 0) {
1160+
return target;
1161+
}
1162+
/* The target's path below its build root, and its QN below the root's
1163+
* folder QN: both must be moved together. */
1164+
size_t tlen = strlen(troot);
1165+
const char *rest = target->file_path + tlen;
1166+
if (tlen > 0) {
1167+
if (strncmp(target->file_path, troot, tlen) != 0 || *rest != '/') {
1168+
return target;
1169+
}
1170+
rest++;
1171+
}
1172+
char *tprefix = cbm_pipeline_fqn_folder(project, troot);
1173+
char *cprefix = cbm_pipeline_fqn_folder(project, croot);
1174+
const cbm_gbuf_node_t *better = target;
1175+
size_t plen = tprefix ? strlen(tprefix) : 0;
1176+
if (tprefix && cprefix && plen > 0 && strncmp(target->qualified_name, tprefix, plen) == 0 &&
1177+
target->qualified_name[plen] == '.') {
1178+
const char *tail = target->qualified_name + plen;
1179+
size_t need = strlen(cprefix) + strlen(tail) + SKIP_ONE;
1180+
char *qn = malloc(need);
1181+
if (qn) {
1182+
snprintf(qn, need, "%s%s", cprefix, tail);
1183+
const cbm_gbuf_node_t *cand = cbm_gbuf_find_by_qn(gbuf, qn);
1184+
char want[CBM_SZ_4K];
1185+
int n = snprintf(want, sizeof(want), "%s%s%s", croot, croot[0] ? "/" : "", rest);
1186+
if (cand && cand != target && n > 0 && (size_t)n < sizeof(want) && cand->file_path &&
1187+
strcmp(cand->file_path, want) == 0 && cand->name &&
1188+
strcmp(cand->name, target->name) == 0 && cand->label &&
1189+
strcmp(cand->label, target->label) == 0) {
1190+
better = cand;
1191+
}
1192+
free(qn);
1193+
}
1194+
}
1195+
free(tprefix);
1196+
free(cprefix);
1197+
return better;
1198+
}
1199+
10381200
static bool calls_emit_lsp_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
10391201
const CBMResolvedCall *lsp, bool allow_tail, CBMLanguage lang,
10401202
const CBMImportArray *imports, const cbm_gbuf_node_t *source_node,
@@ -1051,6 +1213,9 @@ static bool calls_emit_lsp_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
10511213
lsp->callee_qn, allow_tail)
10521214
: cbm_pipeline_lsp_target_node(ctx->gbuf, ctx->project_name,
10531215
lsp->callee_qn, allow_tail);
1216+
/* The caller's own build module first. MUST match pass_parallel.c. */
1217+
target_node = cbm_pipeline_java_same_build_root(ctx->gbuf, ctx->project_name, ctx->repo_path,
1218+
lang, source_node->file_path, target_node);
10541219
if (!target_node || source_node->id == target_node->id) {
10551220
return false;
10561221
}
@@ -1370,6 +1535,12 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
13701535
}
13711536

13721537
const cbm_gbuf_node_t *target_node = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
1538+
/* The caller's own build module first. MUST match pass_parallel.c. */
1539+
target_node = cbm_pipeline_java_same_build_root(ctx->gbuf, ctx->project_name, ctx->repo_path,
1540+
lang, source_node->file_path, target_node);
1541+
if (target_node) {
1542+
res.qualified_name = target_node->qualified_name;
1543+
}
13731544
if (!target_node || source_node->id == target_node->id) {
13741545
return 0;
13751546
}
@@ -1523,6 +1694,8 @@ static CBMFileResult *calls_get_or_extract(cbm_pipeline_ctx_t *ctx, int idx,
15231694

15241695
int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count) {
15251696
cbm_log_info("pass.start", "pass", "calls", "files", itoa_log(file_count));
1697+
/* Build files may have changed since the last run on this thread. */
1698+
cbm_pipeline_build_root_cache_clear();
15261699

15271700
/* ObjectScript: build the method-return-type table from the definitions
15281701
* already in the graph buffer so `Set x = obj.Method()` can resolve x's

‎src/pipeline/pass_parallel.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,6 +3671,16 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
36713671
} else {
36723672
target_node = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
36733673
}
3674+
/* The caller's own build module first. MUST match pass_calls.c. */
3675+
if (target_node) {
3676+
const cbm_gbuf_node_t *own = cbm_pipeline_java_same_build_root(
3677+
rc->main_gbuf, rc->project_name, rc->repo_path, lang, source_node->file_path,
3678+
target_node);
3679+
if (own != target_node) {
3680+
target_node = own;
3681+
res.qualified_name = own->qualified_name;
3682+
}
3683+
}
36743684
atomic_fetch_add_explicit(&rc->time_ns_rc_target, extract_now_ns() - _rc_t0,
36753685
memory_order_relaxed);
36763686
/* A call that resolved onto a struct Field: refused across languages
@@ -4376,6 +4386,7 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
43764386
cbm_service_pattern_cache_begin();
43774387
cbm_pxc_thread_scratch_begin();
43784388
file_node_cache_clear();
4389+
cbm_pipeline_build_root_cache_clear();
43794390

43804391
while (SKIP_ONE) {
43814392
int file_idx =
@@ -4693,6 +4704,7 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
46934704
cbm_service_pattern_cache_end();
46944705
cbm_pxc_thread_scratch_end();
46954706
file_node_cache_clear();
4707+
cbm_pipeline_build_root_cache_clear();
46964708
}
46974709

46984710
int cbm_parallel_resolve(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count,

‎src/pipeline/pipeline_internal.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,27 @@ void cbm_pipeline_spill_close(cbm_pipeline_ctx_t *ctx);
661661
const cbm_gbuf_node_t *cbm_pipeline_file_node(const cbm_gbuf_t *gbuf, const char *project,
662662
const char *rel);
663663

664+
/* Java multi-module builds (Gradle/Maven): the same package and class can be
665+
* declared in several build modules of one repository, and every resolver
666+
* strategy (LSP tail match, field_type_hint, suffix_match, ...) then picks one
667+
* copy for callers in every module. A build root is the nearest ancestor
668+
* folder of a file that holds settings.gradle(.kts), build.gradle(.kts) or
669+
* pom.xml. When a Java caller in `caller_rel` resolved to `target` in another
670+
* build root, and the caller's own build root holds the same file path and the
671+
* same qualified-name tail (same name and label), return that node; otherwise
672+
* return `target` unchanged. Both call resolvers apply it at the same point,
673+
* so the sequential and parallel paths stay in parity. */
674+
const cbm_gbuf_node_t *cbm_pipeline_java_same_build_root(const cbm_gbuf_t *gbuf,
675+
const char *project,
676+
const char *repo_path, CBMLanguage lang,
677+
const char *caller_rel,
678+
const cbm_gbuf_node_t *target);
679+
680+
/* Drop this thread's build-root cache (directory -> build root). Each call
681+
* resolver clears it at its start and end, so a build file added between runs
682+
* is seen by the next run. */
683+
void cbm_pipeline_build_root_cache_clear(void);
684+
664685
/* Transcode an ObjectScript Studio Export XML file and compose every generated
665686
* UDL class into one cacheable result. The returned result owns all child
666687
* extraction arenas and is released with the ordinary cbm_free_result(). */

0 commit comments

Comments
 (0)