Skip to content

Commit 8fa5eef

Browse files
authored
Merge pull request DeusData#2370 from BobbieBarker/fix/registry-cfg-fence-bare-name
fix(registry): reach a cfg-fenced function from its bare name
2 parents 2278498 + e9058e1 commit 8fa5eef

3 files changed

Lines changed: 168 additions & 25 deletions

File tree

‎src/pipeline/registry.c‎

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,38 @@ void cbm_registry_free(cbm_registry_t *r) {
858858

859859
/* ── Registration ────────────────────────────────────────────────── */
860860

861+
/* Record `owned_qn` in the by-name bucket for `key`, keeping the bucket's
862+
* is_test flags in step with its entries.
863+
*
864+
* No array dedup needed: cbm_registry_add's exact-map check guarantees the QN
865+
* is new, and it calls this at most once per distinct key. */
866+
static void index_under_name(cbm_registry_t *r, const char *key, const char *owned_qn) {
867+
qn_array_t *arr = cbm_ht_get(r->by_name, key);
868+
if (!arr) {
869+
arr = calloc(CBM_ALLOC_ONE, sizeof(qn_array_t));
870+
cbm_ht_set(r->by_name, strdup(key), arr);
871+
}
872+
int before = arr->count;
873+
cbm_da_push(arr, (char *)owned_qn);
874+
if (arr->count == before) {
875+
return; /* the name could not be recorded: no verdict to cache */
876+
}
877+
if (arr->count > arr->is_test_cap) {
878+
int want = arr->cap > 0 ? arr->cap : arr->count;
879+
uint8_t *grown =
880+
cbm_realloc(CBM_MEM_CLASS_DYN_ARRAY, arr->is_test, (size_t)want * sizeof(uint8_t));
881+
if (grown) {
882+
arr->is_test = grown;
883+
arr->is_test_cap = want;
884+
}
885+
}
886+
if (arr->count <= arr->is_test_cap) {
887+
arr->is_test[arr->count - SKIP_ONE] = is_test_qn(owned_qn) ? 1 : 0;
888+
}
889+
}
890+
861891
void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified_name,
862892
const char *label) {
863-
(void)name;
864893
if (!r || !qualified_name || !label) {
865894
return;
866895
}
@@ -893,30 +922,37 @@ void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified
893922
cbm_ht_set(r->exact, strdup(qualified_name), (void *)interned);
894923
const char *owned_qn = cbm_ht_get_key(r->exact, qualified_name);
895924

896-
/* Index by simple name.
897-
* No array dedup needed: exact-map check above guarantees uniqueness. */
898-
const char *simple = simple_name(qualified_name);
899-
qn_array_t *arr = cbm_ht_get(r->by_name, simple);
900-
if (!arr) {
901-
arr = calloc(CBM_ALLOC_ONE, sizeof(qn_array_t));
902-
cbm_ht_set(r->by_name, strdup(simple), arr);
903-
}
904-
int before = arr->count;
905-
cbm_da_push(arr, (char *)owned_qn);
906-
if (arr->count == before) {
907-
return; /* the name could not be recorded: no verdict to cache */
908-
}
909-
if (arr->count > arr->is_test_cap) {
910-
int want = arr->cap > 0 ? arr->cap : arr->count;
911-
uint8_t *grown =
912-
cbm_realloc(CBM_MEM_CLASS_DYN_ARRAY, arr->is_test, (size_t)want * sizeof(uint8_t));
913-
if (grown) {
914-
arr->is_test = grown;
915-
arr->is_test_cap = want;
916-
}
917-
}
918-
if (arr->count <= arr->is_test_cap) {
919-
arr->is_test[arr->count - SKIP_ONE] = is_test_qn(owned_qn) ? 1 : 0;
925+
/* Index the symbol under the QN's last dot segment, and additionally under
926+
* the name its caller passed when that segment carries a '#' fence.
927+
*
928+
* The derived key is the one every language has always used and it stays
929+
* unconditional, so a grammar whose QNs carry no '#' is byte-identical to
930+
* before this commit. The second key exists for one shape: a fenced tail is
931+
* a literal string no bare callee is ever written as. rust_cfg_qualified_name
932+
* mints a `#[cfg(test)]` twin as "proj.lib.add#cfg(test)", simple_name() has
933+
* no '#' handling, so the derived key filed that function under the whole
934+
* string "add#cfg(test)" where no bare `add` callee could reach it.
935+
*
936+
* Gating on the fence rather than on the file's language is what makes
937+
* "unchanged for every other grammar" checkable instead of asserted. Every
938+
* by-name lookup keys through simple_name(), which splits on '.' and "::"
939+
* only, so a passed name that differs from the derived key by anything
940+
* OTHER than a fence is a key no lookup can reach: an HCL block's
941+
* "resource.aws_instance.web" and a TOML table's "tool.poetry.dependencies"
942+
* are both already indexed under the tail a reference spells, and adding
943+
* their dotted form would only widen the bucket the scorer walks.
944+
*
945+
* `name` is NULL or empty only for callers that have no symbol name to
946+
* give; those have the derived key and nothing else. */
947+
const char *derived = simple_name(qualified_name);
948+
index_under_name(r, derived, owned_qn);
949+
/* '#' is a QN fence, and extract_defs.c's rust_cfg_qualified_name is the
950+
* only thing in the tree that mints one today. A grammar that starts
951+
* minting a '#' opts into this second key by doing so, whatever it means by
952+
* the fence: its symbols become reachable under the passed name as well,
953+
* and they share that name's bucket with everything else filed under it. */
954+
if (name && name[0] && strchr(derived, '#') && strcmp(name, derived) != 0) {
955+
index_under_name(r, name, owned_qn);
920956
}
921957
}
922958

‎tests/test_pipeline.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,60 @@ TEST(pipeline_nix_scoped_binding_calls_resolve) {
13071307
PASS();
13081308
}
13091309

1310+
/* Terraform reference resolution, end to end.
1311+
*
1312+
* An HCL block names itself with its labels appended -- find_hcl_block_name
1313+
* mints "resource.aws_instance.web" -- while its QN's last dot segment is
1314+
* bare "web", and a reference written `aws_instance.web.id` reaches the
1315+
* by-name index through that tail. HCL is therefore a language where the
1316+
* definition name and the QN tail are different strings, and an index keyed on
1317+
* either one alone drops every cross-resource reference in the file.
1318+
*
1319+
* This has to be a pipeline test: the registry is shared by every language
1320+
* and nothing in the HCL extractor mentions the index key, so the two halves
1321+
* can disagree with every extraction-level assertion still green.
1322+
*/
1323+
TEST(pipeline_hcl_block_reference_resolves_to_its_block) {
1324+
if (setup_test_repo() != 0) {
1325+
FAIL("failed to create temp dir");
1326+
}
1327+
1328+
char tf_path[512];
1329+
snprintf(tf_path, sizeof(tf_path), "%s/main.tf", g_tmpdir);
1330+
FILE *tf = fopen(tf_path, "w");
1331+
if (!tf) {
1332+
teardown_test_repo();
1333+
FAIL("failed to write terraform fixture");
1334+
}
1335+
fprintf(tf, "resource \"aws_instance\" \"web\" {\n"
1336+
" ami = \"ami-0c55b159cbfafe1f0\"\n"
1337+
" instance_type = \"t2.micro\"\n"
1338+
"}\n"
1339+
"\n"
1340+
"resource \"aws_eip\" \"ip\" {\n"
1341+
" instance = aws_instance.web.id\n"
1342+
"}\n");
1343+
fclose(tf);
1344+
1345+
char tf_db[512];
1346+
snprintf(tf_db, sizeof(tf_db), "%s/test_hcl_refs.db", g_tmpdir);
1347+
1348+
cbm_pipeline_t *tp = cbm_pipeline_new(g_tmpdir, tf_db, CBM_MODE_FULL);
1349+
ASSERT_NOT_NULL(tp);
1350+
ASSERT_EQ(cbm_pipeline_run(tp), 0);
1351+
1352+
cbm_store_t *ts = cbm_store_open_path(tf_db);
1353+
ASSERT_NOT_NULL(ts);
1354+
const char *tf_project = cbm_pipeline_project_name(tp);
1355+
1356+
ASSERT(cross_file_edge_exists(ts, tf_project, "main", "resource.aws_instance.web", "USAGE"));
1357+
1358+
cbm_store_close(ts);
1359+
cbm_pipeline_free(tp);
1360+
teardown_test_repo();
1361+
PASS();
1362+
}
1363+
13101364
/* Regression: incremental re-index of an edited file must NOT drop inbound
13111365
* cross-file CALLS edges whose source lives in an UNCHANGED file.
13121366
*
@@ -15483,6 +15537,7 @@ SUITE(pipeline) {
1548315537
/* Calls pass */
1548415538
RUN_TEST(pipeline_calls_resolution);
1548515539
RUN_TEST(pipeline_nix_scoped_binding_calls_resolve);
15540+
RUN_TEST(pipeline_hcl_block_reference_resolves_to_its_block);
1548615541
RUN_TEST(pipeline_incremental_preserves_cross_file_calls);
1548715542
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
1548815543
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);

‎tests/test_registry.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,56 @@ TEST(resolve_qualified_ambiguous_tail_falls_through) {
325325
PASS();
326326
}
327327

328+
/* cbm_registry_add used to discard its `name` argument and re-derive the
329+
* lookup key from the QN's last dot segment. That made the QN's tail load
330+
* bearing for the bare-name index every language shares: a Rust cfg twin,
331+
* minted as "add#cfg(test)", was indexed under that literal string and no bare
332+
* `add` callee could ever reach it. */
333+
TEST(registry_indexes_by_passed_name_not_qn_tail) {
334+
cbm_registry_t *r = cbm_registry_new();
335+
cbm_registry_add(r, "add", "proj.lib.add#cfg(test)", "Function");
336+
337+
cbm_resolution_t res = cbm_registry_resolve(r, "add", "proj.lib.caller", NULL, NULL, 0);
338+
ASSERT_STR_EQ(res.qualified_name, "proj.lib.add#cfg(test)");
339+
340+
cbm_registry_free(r);
341+
PASS();
342+
}
343+
344+
/* The derived key stays unconditional, and this pins that.
345+
*
346+
* A name may carry segments the QN's tail drops, and then the tail is the key
347+
* callers actually spell. An HCL block is named "resource.aws_instance.web" by
348+
* find_hcl_block_name while its QN tail is bare "web", which is how an
349+
* `aws_instance.web.id` reference reaches it. Re-keying the index on the passed
350+
* name would lose that lookup outright, so the fence gate above leaves these
351+
* shapes exactly as they were.
352+
*
353+
* The Module row registers a shape production cannot produce: every caller of
354+
* cbm_registry_add gates on cbm_label_is_registry_symbol, which does not admit
355+
* "Module". It is here because the registry's own contract is per-label-string,
356+
* not per-caller, and a future label change should not silently drop the stem
357+
* lookup a bare module reference needs. */
358+
TEST(registry_indexes_a_dotted_name_under_its_tail_too) {
359+
cbm_registry_t *r = cbm_registry_new();
360+
cbm_registry_add(r, "resource.aws_instance.web", "proj.main.resource.aws_instance.web",
361+
"Class");
362+
cbm_registry_add(r, "helper.py", "proj.pkg.helper", "Module");
363+
364+
cbm_resolution_t tail = cbm_registry_resolve(r, "web", "proj.main", NULL, NULL, 0);
365+
ASSERT_STR_EQ(tail.qualified_name, "proj.main.resource.aws_instance.web");
366+
367+
cbm_resolution_t whole =
368+
cbm_registry_resolve(r, "resource.aws_instance.web", "proj.main", NULL, NULL, 0);
369+
ASSERT_STR_EQ(whole.qualified_name, "proj.main.resource.aws_instance.web");
370+
371+
cbm_resolution_t stem = cbm_registry_resolve(r, "helper", "proj.pkg.caller", NULL, NULL, 0);
372+
ASSERT_STR_EQ(stem.qualified_name, "proj.pkg.helper");
373+
374+
cbm_registry_free(r);
375+
PASS();
376+
}
377+
328378
TEST(resolve_import_map) {
329379
cbm_registry_t *r = cbm_registry_new();
330380
cbm_registry_add(r, "Process", "proj.pkg.worker.Process", "Function");
@@ -1201,6 +1251,8 @@ SUITE(registry) {
12011251
RUN_TEST(resolve_same_module);
12021252
RUN_TEST(resolve_qualified_disambiguates_same_name);
12031253
RUN_TEST(resolve_qualified_ambiguous_tail_falls_through);
1254+
RUN_TEST(registry_indexes_by_passed_name_not_qn_tail);
1255+
RUN_TEST(registry_indexes_a_dotted_name_under_its_tail_too);
12041256
RUN_TEST(resolve_import_map);
12051257
RUN_TEST(resolve_import_map_bare_function);
12061258
RUN_TEST(resolve_import_map_bare_alias);

0 commit comments

Comments
 (0)