Skip to content

Commit 87a0e3f

Browse files
committed
fix: preserve semantic graph relationships
Signed-off-by: Martin Vogel <[email protected]>
1 parent 7d6cdb2 commit 87a0e3f

122 files changed

Lines changed: 39513 additions & 3254 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Makefile.cbm‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,19 @@ SANITIZE = -fsanitize=address,undefined -fno-omit-frame-pointer
6666
EDITOR_TEST_DEFINES = -DCBM_JSON_LIKE_ENABLE_TEST_API=1 \
6767
-DCBM_TOML_EDIT_ENABLE_TEST_API=1 -DCBM_YAML_ENABLE_TEST_API=1 \
6868
-DCBM_TEXT_EDIT_ENABLE_TEST_API=1 -DCBM_CLI_ENABLE_TEST_API=1
69-
CFLAGS_TEST = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) -g -O1 $(SANITIZE)
69+
KOTLIN_DEDUP_TEST_DEFINE = -DCBM_KOTLIN_DEDUP_TEST_API=1
70+
CALL_REFERENCE_LOOKUP_TEST_DEFINE = -DCBM_CALL_REFERENCE_LOOKUP_TEST_API=1
71+
INCREMENTAL_TEST_DEFINE = -DCBM_INCREMENTAL_TEST_API=1
72+
CFLAGS_TEST = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) $(KOTLIN_DEDUP_TEST_DEFINE) \
73+
$(CALL_REFERENCE_LOOKUP_TEST_DEFINE) $(INCREMENTAL_TEST_DEFINE) \
74+
-g -O1 $(SANITIZE)
7075
CXXFLAGS_TEST = $(CXXFLAGS_COMMON) -g -O1 $(SANITIZE)
7176

7277
# TSan (can't combine with ASan)
7378
TSAN_SANITIZE = -fsanitize=thread -fno-omit-frame-pointer
74-
CFLAGS_TSAN = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) -g -O1 \
75-
$(TSAN_SANITIZE)
79+
CFLAGS_TSAN = $(CFLAGS_COMMON) $(EDITOR_TEST_DEFINES) $(KOTLIN_DEDUP_TEST_DEFINE) \
80+
$(CALL_REFERENCE_LOOKUP_TEST_DEFINE) $(INCREMENTAL_TEST_DEFINE) \
81+
-g -O1 $(TSAN_SANITIZE)
7682
CXXFLAGS_TSAN = $(CXXFLAGS_COMMON) -g -O1 \
7783
$(TSAN_SANITIZE)
7884

@@ -156,6 +162,14 @@ LSP_UNITY_DEPS = $(CBM_DIR)/lsp_all.c \
156162

157163
# Tree-sitter runtime
158164
TS_RUNTIME_SRC = $(CBM_DIR)/ts_runtime.c
165+
# ts_runtime.c is a unity wrapper that textually includes the vendored runtime.
166+
# Track those included sources and headers explicitly: without compiler-generated
167+
# dependencies, changing stack.c (including its GLR recursion cap) otherwise
168+
# leaves every runtime object stale while the final binary is merely relinked.
169+
TS_RUNTIME_DEPS = $(TS_RUNTIME_SRC) \
170+
$(wildcard $(TS_SRC)/*.c $(TS_SRC)/*.h \
171+
$(TS_SRC)/portable/*.h $(TS_SRC)/unicode/*.h \
172+
$(TS_INCLUDE)/tree_sitter/*.h)
159173

160174
# All 64 grammar shim files
161175
GRAMMAR_SRCS = $(wildcard $(CBM_DIR)/grammar_*.c)
@@ -357,7 +371,7 @@ TEST_DISCOVER_SRCS = \
357371

358372
TEST_GRAPH_BUFFER_SRCS = tests/test_graph_buffer.c
359373

360-
TEST_PIPELINE_SRCS = tests/test_registry.c tests/test_pipeline.c tests/test_fqn.c tests/test_route_canon.c tests/test_path_alias.c tests/test_configlink.c tests/test_infrascan.c tests/test_worker_pool.c tests/test_parallel.c tests/test_index_resilience.c
374+
TEST_PIPELINE_SRCS = tests/test_registry.c tests/test_pipeline.c tests/test_fqn.c tests/test_route_canon.c tests/test_path_alias.c tests/test_configlink.c tests/test_infrascan.c tests/test_worker_pool.c tests/test_parallel.c tests/test_index_resilience.c tests/test_call_reference_contract.c tests/repro/repro_call_scope_usages.c tests/repro/repro_call_argument_usages.c tests/repro/repro_reference_precision.c tests/repro/repro_lexical_binding_precision.c tests/repro/repro_call_argument_matrix_a.c tests/repro/repro_call_argument_matrix_b.c tests/repro/repro_call_node_behaviors.c tests/repro/repro_language_registry.c tests/repro/repro_call_node_manifest.c tests/repro/repro_lsp_ordered_signatures.c tests/repro/repro_lsp_ordered_local.c tests/repro/repro_ts_overload_return_chains.c tests/repro/repro_harness_cleanup.c tests/repro/repro_runner_filter.c
361375

362376
TEST_WATCHER_SRCS = tests/test_watcher.c
363377

@@ -423,12 +437,26 @@ TEST_SIMHASH_SRCS = tests/test_simhash.c
423437
TEST_STACK_OVERFLOW_SRCS = tests/test_stack_overflow.c
424438

425439
# Cumulative BUG-REPRODUCTION suite (separate runner, NOT in ALL_TEST_SRCS).
426-
# These cases are RED by design (one open bug each) — see tests/repro/repro_main.c.
440+
# Contains RED reproductions plus GREEN controls — see tests/repro/repro_main.c.
427441
# Kept out of the gating `make test` so `ci-ok` stays green; run via `make test-repro`.
428442
TEST_REPRO_SRCS = \
429443
tests/repro/repro_main.c \
430444
tests/repro/repro_parallel_determinism.c \
431445
tests/repro/repro_extraction.c \
446+
tests/repro/repro_runner_filter.c \
447+
tests/repro/repro_harness_cleanup.c \
448+
tests/repro/repro_language_registry.c \
449+
tests/repro/repro_call_node_manifest.c \
450+
tests/repro/repro_call_scope_usages.c \
451+
tests/repro/repro_call_argument_usages.c \
452+
tests/repro/repro_lsp_ordered_signatures.c \
453+
tests/repro/repro_lsp_ordered_local.c \
454+
tests/repro/repro_ts_overload_return_chains.c \
455+
tests/repro/repro_reference_precision.c \
456+
tests/repro/repro_lexical_binding_precision.c \
457+
tests/repro/repro_call_argument_matrix_a.c \
458+
tests/repro/repro_call_argument_matrix_b.c \
459+
tests/repro/repro_call_node_behaviors.c \
432460
tests/repro/repro_issue495.c \
433461
tests/repro/repro_issue521.c \
434462
tests/repro/repro_issue382.c \
@@ -485,6 +513,11 @@ TEST_REPRO_SRCS = \
485513
tests/repro/repro_lsp_java_cs.c \
486514
tests/repro/repro_lsp_kt_php_rust.c
487515

516+
TEST_SHARED_HEADERS = tests/test_framework.h tests/test_helpers.h tests/grammar_cases.h \
517+
tests/repro/repro_harness.h tests/repro/repro_runner.h
518+
TEST_REPRO_HEADERS = $(wildcard tests/repro/*.h) tests/test_framework.h tests/test_helpers.h
519+
PROD_HEADERS = $(wildcard src/*.h src/*/*.h $(CBM_DIR)/*.h $(CBM_DIR)/lsp/*.h)
520+
488521
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_PERL_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SEMANTIC_SRCS) $(TEST_AST_PROFILE_SRCS) $(TEST_SLAB_ALLOC_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
489522

490523

@@ -531,7 +564,7 @@ test-foundation: $(BUILD_DIR)/test-foundation
531564
$(BUILD_DIR)/%.o: $(CBM_DIR)/%.c | $(BUILD_DIR)
532565
$(CC) $(GRAMMAR_CFLAGS_TEST) -c -o $@ $<
533566

534-
$(BUILD_DIR)/ts_runtime.o: $(CBM_DIR)/ts_runtime.c | $(BUILD_DIR)
567+
$(BUILD_DIR)/ts_runtime.o: $(TS_RUNTIME_DEPS) | $(BUILD_DIR)
535568
$(CC) $(GRAMMAR_CFLAGS_TEST) -c -o $@ $<
536569

537570
$(BUILD_DIR)/lsp_all.o: $(LSP_UNITY_DEPS) | $(BUILD_DIR)
@@ -543,7 +576,7 @@ $(BUILD_DIR)/preprocessor.o: $(CBM_DIR)/preprocessor.cpp | $(BUILD_DIR)
543576
$(BUILD_DIR)/tsan_%.o: $(CBM_DIR)/%.c | $(BUILD_DIR)
544577
$(CC) $(GRAMMAR_CFLAGS_TSAN) -c -o $@ $<
545578

546-
$(BUILD_DIR)/tsan_ts_runtime.o: $(CBM_DIR)/ts_runtime.c | $(BUILD_DIR)
579+
$(BUILD_DIR)/tsan_ts_runtime.o: $(TS_RUNTIME_DEPS) | $(BUILD_DIR)
547580
$(CC) $(GRAMMAR_CFLAGS_TSAN) -c -o $@ $<
548581

549582
$(BUILD_DIR)/tsan_lsp_all.o: $(LSP_UNITY_DEPS) | $(BUILD_DIR)
@@ -629,7 +662,7 @@ $(UNIXCODER_OBJ): $(UNIXCODER_BLOB_SRC) vendored/nomic/code_vectors.bin | $(BUIL
629662
OBJS_VENDORED_TEST = $(MIMALLOC_OBJ_TEST) $(SQLITE3_OBJ_TEST) $(TRE_OBJ_TEST) $(GRAMMAR_OBJS_TEST) $(TS_RUNTIME_OBJ_TEST) $(LSP_OBJ_TEST) $(PP_OBJ_TEST) $(LZ4_OBJ_TEST) $(ZSTD_OBJ_TEST) $(UNIXCODER_OBJ)
630663
OBJS_VENDORED_TSAN = $(MIMALLOC_OBJ_TSAN) $(SQLITE3_OBJ_TSAN) $(TRE_OBJ_TSAN) $(GRAMMAR_OBJS_TSAN) $(TS_RUNTIME_OBJ_TSAN) $(LSP_OBJ_TSAN) $(PP_OBJ_TSAN) $(LZ4_OBJ_TSAN) $(ZSTD_OBJ_TSAN) $(UNIXCODER_OBJ)
631664

632-
$(BUILD_DIR)/test-runner: $(ALL_TEST_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) | $(BUILD_DIR)
665+
$(BUILD_DIR)/test-runner: $(ALL_TEST_SRCS) $(TEST_SHARED_HEADERS) $(PROD_HEADERS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) | $(BUILD_DIR)
633666
$(CC) $(CFLAGS_TEST) -Itests -Itests/repro -o $@ \
634667
$(ALL_TEST_SRCS) $(PROD_SRCS) \
635668
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \
@@ -653,26 +686,35 @@ test-focused: $(BUILD_DIR)/test-runner
653686
(echo "TEST_SUITES is required for test-focused"; exit 2)
654687
cd $(CURDIR) && $(BUILD_DIR)/test-runner $(TEST_SUITES)
655688

656-
# ── Cumulative bug-reproduction runner (RED by design, non-gating) ──
689+
# ── Cumulative bug-reproduction runner (RED board by design, non-gating) ──
657690
# Mirrors test-runner's link line but uses repro_main.c (own main + counters)
658691
# and TEST_REPRO_SRCS instead of ALL_TEST_SRCS. Exits non-zero while any bug is
659692
# still reproduced (the expected state); bug-repro.yml surfaces it as a board.
660-
$(BUILD_DIR)/test-repro-runner: $(TEST_REPRO_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) | $(BUILD_DIR)
693+
$(BUILD_DIR)/test-repro-runner: $(TEST_REPRO_SRCS) $(TEST_REPRO_HEADERS) $(PROD_HEADERS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TEST) | $(BUILD_DIR)
661694
$(CC) $(CFLAGS_TEST) -Itests -o $@ \
662695
$(TEST_REPRO_SRCS) $(PROD_SRCS) \
663696
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \
664697
$(OBJS_VENDORED_TEST) \
665698
$(LDFLAGS_TEST)
666699

667700
test-repro: $(BUILD_DIR)/test-repro-runner
701+
cd $(CURDIR) && bash tests/repro/repro_script_summary.sh
702+
cd $(CURDIR) && bash tests/repro/repro_script_zero_selection.sh
703+
cd $(CURDIR) && bash tests/repro/repro_script_missing_summary.sh
704+
cd $(CURDIR) && bash tests/repro/repro_script_skipped_summary.sh
705+
cd $(CURDIR) && bash tests/repro/repro_runner_zero_selection.sh
706+
cd $(CURDIR) && bash tests/repro/repro_runner_passing_semantics.sh
707+
cd $(CURDIR) && bash tests/repro/repro_script_no_workspace_artifact.sh
708+
cd $(CURDIR) && bash tests/repro/repro_script_stderr_summary.sh
709+
cd $(CURDIR) && bash tests/repro/repro_make_tracks_headers.sh
668710
cd $(CURDIR) && $(BUILD_DIR)/test-repro-runner
669711

670712
# ── TSan full test ───────────────────────────────────────────────
671713

672714
TEST_TSAN_SUITES ?= mem slab_alloc parallel
673715
TSAN_OPTIONS ?= halt_on_error=1
674716

675-
$(BUILD_DIR)/test-runner-tsan: $(ALL_TEST_SRCS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TSAN) | $(BUILD_DIR)
717+
$(BUILD_DIR)/test-runner-tsan: $(ALL_TEST_SRCS) $(TEST_SHARED_HEADERS) $(PROD_HEADERS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_TSAN) | $(BUILD_DIR)
676718
$(CC) $(CFLAGS_TSAN) -Itests -Itests/repro -o $@ \
677719
$(ALL_TEST_SRCS) $(PROD_SRCS) \
678720
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \
@@ -693,7 +735,7 @@ PP_OBJ_PROD = $(BUILD_DIR)/prod_preprocessor.o
693735
$(BUILD_DIR)/prod_%.o: $(CBM_DIR)/%.c | $(BUILD_DIR)
694736
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
695737

696-
$(BUILD_DIR)/prod_ts_runtime.o: $(CBM_DIR)/ts_runtime.c | $(BUILD_DIR)
738+
$(BUILD_DIR)/prod_ts_runtime.o: $(TS_RUNTIME_DEPS) | $(BUILD_DIR)
697739
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
698740

699741
$(BUILD_DIR)/prod_lsp_all.o: $(LSP_UNITY_DEPS) | $(BUILD_DIR)
@@ -718,7 +760,7 @@ OBJS_VENDORED_PROD = $(MIMALLOC_OBJ_PROD) $(SQLITE3_OBJ_PROD) $(TRE_OBJ_PROD) $(
718760

719761
MAIN_SRC = src/main.c
720762

721-
$(BUILD_DIR)/codebase-memory-mcp: $(MAIN_SRC) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_PROD) | $(BUILD_DIR)
763+
$(BUILD_DIR)/codebase-memory-mcp: $(MAIN_SRC) $(PROD_HEADERS) $(PROD_SRCS) $(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) $(OBJS_VENDORED_PROD) | $(BUILD_DIR)
722764
$(CC) $(CFLAGS_PROD) -o $@ \
723765
$(MAIN_SRC) $(PROD_SRCS) \
724766
$(EXTRACTION_SRCS) $(AC_LZ4_SRCS) $(ZSTD_SRCS) $(SQLITE_WRITER_SRC) \

‎README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ Removes owned agent config entries, skills, hooks, instructions, and the install
175175
- **Cross-repo architecture summary** combining services, routes, and dependencies across the indexed fleet
176176

177177
### Edge types (selected)
178-
- `CALLS`, `IMPORTS`, `DEFINES`, `IMPLEMENTS`, `INHERITS`
178+
- `CALLS` — a callable is invoked at the source site
179+
- `CALL_REFERENCE` — a callable is used at a supported reference site (for example, a direct value argument) and resolves to one exact target
180+
- `USAGE` — an identifier is used, but a unique callable target is not proven (including ambiguous or complex expressions)
181+
- `IMPORTS`, `DEFINES`, `IMPLEMENTS`, `INHERITS`
179182
- `HTTP_CALLS`, `ASYNC_CALLS` (cross-service)
180183
- `EMITS`, `LISTENS_ON` (channels)
181184
- `DATA_FLOWS` with arg-to-param mapping + field access chains
@@ -568,7 +571,7 @@ codebase-memory-mcp cli --raw search_graph '{"project": "my-project", "label": "
568571

569572
### Edge Types
570573

571-
`CONTAINS_PACKAGE`, `CONTAINS_FOLDER`, `CONTAINS_FILE`, `DEFINES`, `DEFINES_METHOD`, `IMPORTS`, `CALLS`, `HTTP_CALLS`, `ASYNC_CALLS`, `IMPLEMENTS`, `HANDLES`, `USAGE`, `CONFIGURES`, `WRITES`, `MEMBER_OF`, `TESTS`, `USES_TYPE`, `FILE_CHANGES_WITH`
574+
`CONTAINS_PACKAGE`, `CONTAINS_FOLDER`, `CONTAINS_FILE`, `DEFINES`, `DEFINES_METHOD`, `IMPORTS`, `CALLS`, `CALL_REFERENCE`, `HTTP_CALLS`, `ASYNC_CALLS`, `IMPLEMENTS`, `HANDLES`, `USAGE`, `CONFIGURES`, `WRITES`, `MEMBER_OF`, `TESTS`, `USES_TYPE`, `FILE_CHANGES_WITH`
572575

573576
### Qualified Names
574577

@@ -665,7 +668,7 @@ SQLite databases stored at `~/.cache/codebase-memory-mcp/`. Persists across rest
665668

666669
Tree-sitter alone gives a syntactic AST. That handles naming, structure, and call sites well, but it can't tell you that `user.profile.display_name()` resolves to `Profile.display_name` declared three modules away — tree-sitter doesn't track imports, generics, inheritance, or stdlib types.
667670

668-
codebase-memory-mcp ships a **lightweight C implementation of language type-resolution algorithms, structurally inspired by and compatible with major language servers** (tsserver / typescript-go, pyright, gopls, Roslyn, Eclipse JDT, rust-analyzer), embedded directly into the static binary. No language server process, no per-project setup, no API key. We call this layer **Hybrid LSP**: it runs alongside tree-sitter on every parse and refines `CALLS`, `USAGE`, and `RESOLVED_CALLS` edges with type information, so the resulting graph mirrors what an IDE "Go to Definition" would resolve.
671+
codebase-memory-mcp ships a **lightweight C implementation of language type-resolution algorithms, structurally inspired by and compatible with major language servers** (tsserver / typescript-go, pyright, gopls, Roslyn, Eclipse JDT, rust-analyzer), embedded directly into the static binary. No language server process, no per-project setup, no API key. We call this layer **Hybrid LSP**: it runs alongside tree-sitter on every parse and refines invocation resolution (`CALLS` / `RESOLVED_CALLS`) and callable-value resolution (`CALL_REFERENCE`, with ambiguous values retained as `USAGE`) using type information, so the resulting graph mirrors what an IDE "Go to Definition" would resolve.
669672

670673
**Languages with full Hybrid LSP:**
671674

‎internal/cbm/cbm.c‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,8 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
12291229
cbm_run_go_lsp(a, result, source, source_len, root);
12301230
}
12311231
if (language == CBM_LANG_C || language == CBM_LANG_CPP || language == CBM_LANG_CUDA) {
1232-
cbm_run_c_lsp(a, result, source, source_len, root, language != CBM_LANG_C);
1232+
cbm_run_c_lsp(a, result, source, source_len, root, language != CBM_LANG_C,
1233+
CBM_SOURCE_ORIGIN_RAW);
12331234
}
12341235
if (language == CBM_LANG_PHP) {
12351236
cbm_run_php_lsp(a, result, source, source_len, root);
@@ -1289,8 +1290,11 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
12891290
if (preprocessed && preprocessed->source) {
12901291
char *expanded = preprocessed->source;
12911292
int expanded_len = (int)strlen(expanded);
1292-
// Record calls count before second pass
1293+
// Record every site-bearing array boundary before the second pass.
1294+
// Numeric byte spans in `expanded` are not raw-source coordinates.
12931295
int calls_before = result->calls.count;
1296+
int usages_before = result->usages.count;
1297+
int resolved_before = result->resolved_calls.count;
12941298

12951299
// Parse expanded source with fresh tree
12961300
TSParser *pp_parser = get_thread_parser(ts_lang, language);
@@ -1326,11 +1330,30 @@ CBMFileResult *cbm_extract_file_ex(const char *source, int source_len, CBMLangua
13261330
// harmless (pipeline deduplicates by caller+callee).
13271331
cbm_extract_unified(&pp_ctx);
13281332

1333+
/* Stamp parser carriers before C-LSP performs any
1334+
* origin-sensitive rewrite. Numeric spans in the expanded
1335+
* buffer may collide with unrelated raw-source spans. */
1336+
for (int i = calls_before; i < result->calls.count; i++) {
1337+
result->calls.items[i].source_origin = CBM_SOURCE_ORIGIN_PREPROCESSED;
1338+
}
1339+
for (int i = usages_before; i < result->usages.count; i++) {
1340+
result->usages.items[i].source_origin = CBM_SOURCE_ORIGIN_PREPROCESSED;
1341+
}
1342+
13291343
// Also run LSP on expanded source for additional type-resolved
13301344
// calls (language is already C/C++/CUDA — checked in enclosing
13311345
// block). Runs in every mode.
13321346
cbm_run_c_lsp(a, result, expanded, expanded_len, pp_root,
1333-
language != CBM_LANG_C);
1347+
language != CBM_LANG_C, CBM_SOURCE_ORIGIN_PREPROCESSED);
1348+
1349+
/* All C-LSP emitters stamp origin directly so rewrite-time
1350+
* comparisons are already safe. Keep this boundary sweep as
1351+
* a defensive invariant for any future emitter added to the
1352+
* C resolver. */
1353+
for (int i = resolved_before; i < result->resolved_calls.count; i++) {
1354+
result->resolved_calls.items[i].source_origin =
1355+
CBM_SOURCE_ORIGIN_PREPROCESSED;
1356+
}
13341357

13351358
/* #961: a def whose body braces are split across
13361359
* #ifdef/#else branches parses as an ERROR region on the
@@ -1537,6 +1560,12 @@ void cbm_free_result(CBMFileResult *result) {
15371560
ts_tree_delete(result->cached_tree);
15381561
result->cached_tree = NULL;
15391562
}
1563+
for (int i = 0; i < result->owned_result_count; i++) {
1564+
cbm_free_result(result->owned_results[i]);
1565+
}
1566+
free(result->owned_results);
1567+
result->owned_results = NULL;
1568+
result->owned_result_count = 0;
15401569
cbm_arena_destroy(&result->arena);
15411570
free(result);
15421571
}

0 commit comments

Comments
 (0)