Merge origin/main into the ObjectScript v1.11.0 refresh

Main brought in #2969 (vendored VB6/VBA grammar), #2981 (linear-growth
rewrites in extraction and eight LSP passes, a node for C# operators, C
increments recorded as writes), #2977 (receiver gate in the registry),
#2978 (possible override targets in trace_path), #2966 (CONTRIBUTING test
reference) and #2985 (freebsd-vm action bump).

Two files conflicted:

- internal/cbm/vendored/grammars/MANIFEST.md: the summary header. Took
  main's counts (164 grammars, 20 first-party, ABI 9x/78x/77x), which a
  recount of the merged tree confirms (164 grammar directories, LANGUAGE_VERSION
  13/14/15 = 9/78/77). Kept this branch's 2026-11-13 ObjectScript recount
  line above main's 2026-11-12 vba line; main had folded the 2026-11-11 sql
  line into its vba line, so that line goes. Main's vba prose and table rows
  and this branch's ObjectScript v1.11.0 rows are both present.
- scripts/vendored-checksums.txt: regenerated with
  scripts/security-vendored.sh --update. Only the MANIFEST.md hash moved;
  the result differs from this branch only by main's vba entries and from
  main only by the ObjectScript entries.

Every other file merged cleanly. For each file both sides changed
(THIRD_PARTY.md, extract_calls.c, extract_defs.c, extract_usages.c,
helpers.c, lang_specs.c, tests/test_parse_coverage.c) the merged file
differs from each parent by exactly the other parent's change.
#2981 rewrote objectscript_routine_preceding_tag onto a memoized sibling
search with the same probe; this branch does not touch that function.
ObjectScript stays outside #2977's receiver gate allowlist.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
Martin Vogel
2026-11-14 12:01:28 +01:00
73 changed files with 2270496 additions and 471 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ jobs:
- name: Build and run inside a FreeBSD ${{ matrix.release }} (${{ matrix.arch }}) VM
# https://github.com/vmactions/freebsd-vm
uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9
uses: vmactions/freebsd-vm@811efaaac0668df8bfb8308c3ec997b5ab50037b # v1.6.0
with:
release: ${{ matrix.release }}
arch: ${{ matrix.arch }}
+1 -1
View File
@@ -30,7 +30,7 @@ scripts/test.sh
This builds with ASan + UBSan and runs the full C test suite. Key test files:
- `tests/test_pipeline.c` — pipeline integration tests
- `tests/test_httplink.c` — HTTP route extraction and linking
- `tests/test_route_canon.c` — route-path canonicalization used to link HTTP clients and handlers
- `tests/test_mcp.c` — MCP protocol and tool handler tests
- `tests/test_store_*.c` — SQLite graph store tests
+2 -1
View File
@@ -428,7 +428,8 @@ EXTRACTION_SRCS = \
$(CBM_DIR)/macro_table.c \
$(CBM_DIR)/iris_export_xml.c \
$(CBM_DIR)/service_patterns.c \
$(CBM_DIR)/spawn_patterns.c
$(CBM_DIR)/spawn_patterns.c \
$(CBM_DIR)/resolve_stats.c
# LSP resolvers (compiled as one unit via lsp_all.c)
LSP_SRCS = $(CBM_DIR)/lsp_all.c
+5 -5
View File
@@ -5,7 +5,7 @@
[![CI](https://img.shields.io/github/actions/workflow/status/DeusData/codebase-memory-mcp/release.yml?branch=main&label=main%20CI)](https://github.com/DeusData/codebase-memory-mcp/actions/workflows/release.yml)
[![Native and frontend tests](https://img.shields.io/badge/tests-native_%2B_frontend-blue)](#tests)
[![MCP tools](https://img.shields.io/badge/MCP_tools-17-blue)](#mcp-tools)
[![Languages](https://img.shields.io/badge/languages-163-orange)](https://github.com/DeusData/codebase-memory-mcp)
[![Languages](https://img.shields.io/badge/languages-164-orange)](https://github.com/DeusData/codebase-memory-mcp)
[![Hybrid LSP](https://img.shields.io/badge/Hybrid_LSP-type_resolution-blue)](#hybrid-lsp)
[![Agents](https://img.shields.io/badge/agent_surfaces-46-purple)](https://github.com/DeusData/codebase-memory-mcp)
[![Pure C](https://img.shields.io/badge/pure_C-no_language_runtime-blue)](https://github.com/DeusData/codebase-memory-mcp)
@@ -398,7 +398,7 @@ In this repository, test narrowing is on for every PR unless `TEST_IMPACT_MODE`
- `SEMANTICALLY_RELATED` (vocabulary-mismatch, same-language, score ≥ 0.80)
### Indexing pipeline
- **163 vendored tree-sitter grammars** compiled into the binary
- **164 vendored tree-sitter grammars** compiled into the binary
- **Generic package / module resolution**: bare specifiers like `@myorg/pkg`, `github.com/foo/bar`, `use my_crate::foo` resolved via manifest scanning (`package.json`, `go.mod`, `Cargo.toml`, `pyproject.toml`, `composer.json`, `pubspec.yaml`, `pom.xml`, `build.gradle`, `mix.exs`, `*.gemspec`)
- **Infrastructure-as-code indexing**: Dockerfiles (`Dockerfile.*`, `*.Dockerfile`, `Containerfile`: bases, what the container runs, `ENV`), container images (`Image`: the normalised repository, tag and digest on the edges; a repository built from several Dockerfiles, one per component tag, gets an `Image` per component), docker compose services (`Resource`), Kubernetes manifests (`Resource`, with `group`, `version`, `kind`, `namespace`, `labels` as `k=v,k2=v2`), Kustomize overlays (`IMPORTS` to their entries and patch files, `OVERRIDE` from their patches), Helm charts (`Chart`, `DEPENDS_ON`), Terraform / OpenTofu (`.tf`, `.tofu`, `.tfvars`) as graph nodes. A Service walks to the code it runs: Service → workload → `Image` → Dockerfile → `main`
- **[Hybrid LSP semantic type resolution](#hybrid-lsp)** for Python, TypeScript / JavaScript / JSX / TSX, PHP, C#, Go, C, C++, Java, Kotlin, Rust, and Perl; a lightweight C implementation of language type-resolution algorithms, structurally inspired by and compatible with major language servers including tsserver / typescript-go, pyright, gopls, Roslyn, Eclipse JDT, and rust-analyzer (parameter binding, return-type inference, generic substitution, JSX component dispatch, JSDoc inference for plain JS files, namespace + trait + late-static-binding resolution for PHP, file-scoped namespaces + records + LINQ method syntax for C#, class-hierarchy + overload + lambda resolution for Java, extension-function + scope-function resolution for Kotlin, trait-method + UFCS resolution for Rust)
@@ -1124,14 +1124,14 @@ codebase-memory-mcp ships a **lightweight C implementation of language type-reso
**Two-layer architecture:**
1. **Tree-sitter pass**: fast, syntactic, runs for every one of the 163 languages. Extracts definitions, calls, imports.
1. **Tree-sitter pass**: fast, syntactic, runs for every one of the 164 languages. Extracts definitions, calls, imports.
2. **Hybrid LSP pass**: type-aware, runs above the tree-sitter pass per-language. Refines call edges using the import graph plus a per-file or pre-built cross-file definition registry. Languages without a Hybrid LSP pass yet fall back to textual resolution, with coverage varying by language and construct.
The result is a knowledge graph accurate enough to drive `trace_path` across packages, inheritance hierarchies, and stdlib calls; without paying for a language server process per project.
## Language Support
163 languages, counted by the vendored grammar directories in [the parser manifest](internal/cbm/vendored/grammars/MANIFEST.md). Benchmarked against 64 real open-source repositories (78 to 49K nodes):
164 languages, counted by the vendored grammar directories in [the parser manifest](internal/cbm/vendored/grammars/MANIFEST.md). Benchmarked against 64 real open-source repositories (78 to 49K nodes):
| Tier | Score | Languages |
|------|-------|-----------|
@@ -1158,7 +1158,7 @@ src/
ui/ Local HTTP server + embedded CBM Lens frontend
foundation/ Platform abstractions (threads, filesystem, logging, memory)
graph-ui/ CodeAtlasWeb: React, Monaco, and Three.js browser frontend
internal/cbm/ Vendored tree-sitter grammars (163 languages) + AST extraction engine
internal/cbm/ Vendored tree-sitter grammars (164 languages) + AST extraction engine
```
## Security
+23 -5
View File
@@ -37,7 +37,7 @@ The core runtime headers in `internal/cbm/vendored/common/tree_sitter/`
## Tree-sitter Grammars
163 pre-generated parsers are vendored in `internal/cbm/vendored/grammars/<lang>/`
164 pre-generated parsers are vendored in `internal/cbm/vendored/grammars/<lang>/`
(generated `parser.c` plus `scanner.c` where applicable, compiled statically).
Each grammar is the work of its upstream authors and each grammar directory
contains the upstream `LICENSE` file.
@@ -77,8 +77,8 @@ License summary:
smart-coin language, written for this project because no usable public
grammar exists; its source and corpus tests live in
`tools/tree-sitter-chialisp/`.
- Twelve further grammars (`arkts`, `assembly`, `cfml`,
`cfscript`, `dotenv`, `javascript`, `pascal`, `pine`, `qml`, `sql`, `tsx`, `vbnet`) are
- Thirteen further grammars (`arkts`, `assembly`, `cfml`, `cfscript`, `dotenv`,
`javascript`, `pascal`, `pine`, `qml`, `sql`, `tsx`, `vba`, `vbnet`) are
self-maintained forks that retain their original upstream authors' licenses —
see the manifest for per-grammar provenance. `arkts` is a first-party derivative of
[tree-sitter/tree-sitter-typescript](https://github.com/tree-sitter/tree-sitter-typescript)
@@ -101,8 +101,26 @@ License summary:
MIT, (c) 2021 Derek Stride) is the upstream grammar with one (c) 2026
DeusData patch that accepts the SQLite syntax of real migration files
(PRAGMA, AUTOINCREMENT, WITHOUT ROWID, trigger bodies, virtual tables); its
grammar source lives in `tools/tree-sitter-sql/`. `vbnet` is described in its
own section below.
grammar source lives in `tools/tree-sitter-sql/`. `vba` and `vbnet` are described
in their own sections below.
### tree-sitter-vba
- **Project:** [harumiWeb/tree-sitter-vba](https://github.com/harumiWeb/tree-sitter-vba)
- **License:** MIT
- **Copyright:** (c) 2026 harumiWeb
- **Vendored at:** `internal/cbm/vendored/grammars/vba/`
- **Pinned commit:** `83c5c3c3c721` (v0.14.5)
- **Notes:** Visual Basic for Applications and Visual Basic 6 grammar (its
`vba` dialect), maintained here as a self-maintained fork without local
changes: upstream does not track its generated parser, so its grammar source
(`common/define-grammar.js` and `vba/grammar.js`, byte-identical to
upstream) and the regeneration recipe live in `tools/tree-sitter-vba/`, and
the parser is generated in this repository with tree-sitter-cli 0.25.10.
Upstream's `THIRD_PARTY_LICENSE.md` covers only its test fixtures, none of
which is carried here. The grammar is vendored ahead of VB6/VBA extraction
(#2564); provenance hashes are in
`internal/cbm/vendored/grammars/MANIFEST.md`.
### tree-sitter-vbnet (with CodeGraph's real-world VB.NET patch)
+8
View File
@@ -1137,6 +1137,14 @@ typedef struct {
* The core never reads, interprets or frees it. It is the only place for
* such state: a static or thread-local cache is not. */
void *doclink_state;
/* C#: the first named file-scoped namespace among the children of the
* tree top cs_file_ns_top (extract_defs.c), found once per file. */
const void *cs_file_ns_top;
const char *cs_file_ns_name;
uint32_t cs_file_ns_start;
/* Go: what each block's statements bind (extract_calls.c), NULL until a route call asks;
* allocated in `scratch`. */
void *go_block_bindings;
} CBMExtractCtx;
/* Internal configured-definition seams. No declarations pointer enters a result. */
+227 -14
View File
@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "resolve_stats.h"
#if defined(CBM_KOTLIN_DEDUP_TEST_API) && CBM_KOTLIN_DEDUP_TEST_API
#include <stdatomic.h>
#endif
@@ -2163,30 +2164,238 @@ static bool go_is_function_boundary(const char *k) {
strcmp(k, "func_literal") == 0 || strcmp(k, "source_file") == 0;
}
/* Latest binding of `name` among the statements preceding `use` in its
* enclosing blocks, innermost block first; a null node when none. */
static TSNode go_find_binding(CBMArena *a, TSNode use, const char *name, const char *source) {
/* A function of route registrations asks what each receiver is bound to, so one block's bindings
* are read once: every name each statement binds, by name and then in source order. A statement
* that names a variable but gives it no value has no row, as go_stmt_binding has no answer. */
typedef struct {
const char *name;
uint32_t statement_start;
uint32_t order;
TSNode value;
} go_binding_t;
typedef struct {
const void *block;
go_binding_t *rows;
size_t count, capacity;
} go_block_bindings_t;
typedef struct {
go_block_bindings_t **slots;
size_t capacity, used;
} go_bindings_table_t;
static bool go_binding_add(CBMArena *a, go_block_bindings_t *block, TSNode statement,
const char *name, const char *source) {
TSNode value = go_stmt_binding(a, statement, name, source);
if (ts_node_is_null(value)) {
return true;
}
if (block->count == block->capacity) {
size_t capacity = block->capacity ? block->capacity * 2 : 16;
go_binding_t *rows = cbm_arena_alloc(a, capacity * sizeof(*rows));
if (!rows) {
return false;
}
if (block->count) {
memcpy(rows, block->rows, block->count * sizeof(*rows));
}
block->rows = rows;
block->capacity = capacity;
}
block->rows[block->count] = (go_binding_t){.name = name,
.statement_start = ts_node_start_byte(statement),
.order = (uint32_t)block->count,
.value = value};
block->count++;
return true;
}
/* Every name a `var` statement declares, through its specs and their lists. */
static bool go_var_names_add(CBMArena *a, go_block_bindings_t *block, TSNode statement, TSNode list,
const char *source) {
uint32_t count = ts_node_named_child_count(list);
for (uint32_t i = 0; i < count; i++) {
TSNode child = ts_node_named_child(list, i);
const char *kind = ts_node_type(child);
if (strcmp(kind, "var_spec") == 0) {
uint32_t parts = ts_node_named_child_count(child);
for (uint32_t p = 0; p < parts; p++) {
TSNode part = ts_node_named_child(child, p);
const char *name = strcmp(ts_node_type(part), "identifier") == 0
? cbm_node_text(a, part, source)
: NULL;
if (name && !go_binding_add(a, block, statement, name, source)) {
return false;
}
}
} else if (strcmp(kind, "var_spec_list") == 0 &&
!go_var_names_add(a, block, statement, child, source)) {
return false;
}
}
return true;
}
static bool go_statement_names_add(CBMArena *a, go_block_bindings_t *block, TSNode statement,
const char *source) {
const char *kind = ts_node_type(statement);
if (strcmp(kind, "var_declaration") == 0 || strcmp(kind, "var_spec_list") == 0) {
return go_var_names_add(a, block, statement, statement, source);
}
if (strcmp(kind, "short_var_declaration") != 0 && strcmp(kind, "assignment_statement") != 0) {
return true;
}
TSNode left = ts_node_child_by_field_name(statement, TS_FIELD("left"));
uint32_t count = ts_node_is_null(left) ? 0 : ts_node_named_child_count(left);
for (uint32_t i = 0; i < count; i++) {
const char *name = cbm_node_text(a, ts_node_named_child(left, i), source);
if (name && !go_binding_add(a, block, statement, name, source)) {
return false;
}
}
return true;
}
static int go_binding_order(const void *left, const void *right) {
const go_binding_t *a = left, *b = right;
int order = strcmp(a->name, b->name);
if (order) {
return order;
}
return a->order < b->order ? -1 : a->order > b->order;
}
static go_block_bindings_t *go_block_bindings_build(CBMArena *a, TSNode node, const char *source) {
go_block_bindings_t *block = cbm_arena_alloc(a, sizeof(*block));
if (!block) {
return NULL;
}
*block = (go_block_bindings_t){.block = node.id};
bool ok = true;
TSTreeCursor cursor = ts_tree_cursor_new(node);
if (ts_tree_cursor_goto_first_child(&cursor)) {
do {
TSNode statement = ts_tree_cursor_current_node(&cursor);
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
ok =
!ts_node_is_named(statement) || go_statement_names_add(a, block, statement, source);
} while (ok && ts_tree_cursor_goto_next_sibling(&cursor));
}
ts_tree_cursor_delete(&cursor);
if (!ok) {
return NULL;
}
if (block->count > 1) {
qsort(block->rows, block->count, sizeof(*block->rows), go_binding_order);
}
return block;
}
static size_t go_block_hash(const void *id, size_t capacity) {
uint64_t value = (uint64_t)(uintptr_t)id >> 3;
value *= UINT64_C(0x9E3779B97F4A7C15);
return (size_t)(value ^ (value >> 29)) & (capacity - 1);
}
/* This block's bindings, read the first time anything in it asks. */
static go_block_bindings_t *go_block_bindings_of(CBMExtractCtx *ctx, TSNode node) {
enum { GO_BLOCKS_FIRST = 16 };
CBMArena *a = ctx->scratch ? ctx->scratch : ctx->arena;
go_bindings_table_t *table = ctx->go_block_bindings;
if (!table) {
table = cbm_arena_calloc(a, sizeof(*table));
go_block_bindings_t **slots =
table ? cbm_arena_calloc(a, GO_BLOCKS_FIRST * sizeof(*slots)) : NULL;
if (!slots) {
return NULL;
}
table->slots = slots;
table->capacity = GO_BLOCKS_FIRST;
ctx->go_block_bindings = table;
}
size_t at = go_block_hash(node.id, table->capacity);
while (table->slots[at]) {
if (table->slots[at]->block == node.id) {
return table->slots[at];
}
at = (at + 1) & (table->capacity - 1);
}
go_block_bindings_t *block = go_block_bindings_build(a, node, ctx->source);
if (!block) {
return NULL;
}
table->slots[at] = block;
table->used++;
if (table->used * 2 > table->capacity) {
size_t capacity = table->capacity * 2;
go_block_bindings_t **slots = cbm_arena_calloc(a, capacity * sizeof(*slots));
if (!slots) {
return NULL;
}
for (size_t i = 0; i < table->capacity; i++) {
if (table->slots[i]) {
size_t place = go_block_hash(table->slots[i]->block, capacity);
while (slots[place]) {
place = (place + 1) & (capacity - 1);
}
slots[place] = table->slots[i];
}
}
table->slots = slots;
table->capacity = capacity;
}
return block;
}
/* The binding of `name` by the last statement in this block that starts before `before`. */
static TSNode go_block_binding(const go_block_bindings_t *block, const char *name,
uint32_t before) {
size_t low = 0, high = block->count;
while (low < high) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
size_t middle = low + (high - low) / 2;
const go_binding_t *row = &block->rows[middle];
int order = strcmp(row->name, name);
if (order < 0 || (order == 0 && row->statement_start < before)) {
low = middle + 1;
} else {
high = middle;
}
}
if (low == 0 || strcmp(block->rows[low - 1].name, name) != 0) {
return (TSNode){0};
}
return block->rows[low - 1].value;
}
/* Latest binding of `name` among the statements preceding `use` in its enclosing blocks, innermost
* block first; a null node when none. False when memory ran out and the answer is unknown. */
static bool go_find_binding(CBMExtractCtx *ctx, TSNode use, const char *name, TSNode *binding) {
*binding = (TSNode){0};
TSNode child = use;
TSNode cur = ts_node_parent(use);
while (!ts_node_is_null(cur) && !go_is_function_boundary(ts_node_type(cur))) {
const char *k = ts_node_type(cur);
if (strcmp(k, "block") == 0 || strcmp(k, "statement_list") == 0) {
for (TSNode s = ts_node_prev_named_sibling(child); !ts_node_is_null(s);
s = ts_node_prev_named_sibling(s)) {
TSNode v = go_stmt_binding(a, s, name, source);
if (!ts_node_is_null(v)) {
return v;
}
const go_block_bindings_t *block = go_block_bindings_of(ctx, cur);
if (!block) {
return false;
}
*binding = go_block_binding(block, name, ts_node_start_byte(child));
if (!ts_node_is_null(*binding)) {
return true;
}
}
child = cur;
cur = ts_node_parent(cur);
}
TSNode none = {0};
return none;
return true;
}
static const char *go_group_prefix_for_call(CBMArena *a, TSNode call, const char *source) {
static const char *go_group_prefix_for_call(CBMExtractCtx *ctx, TSNode call) {
CBMArena *a = ctx->arena;
const char *source = ctx->source;
TSNode fn = ts_node_child_by_field_name(call, TS_FIELD("function"));
if (ts_node_is_null(fn) || strcmp(ts_node_type(fn), "selector_expression") != 0) {
return NULL;
@@ -2205,7 +2414,11 @@ static const char *go_group_prefix_for_call(CBMArena *a, TSNode call, const char
}
if (strcmp(k, "identifier") == 0) {
const char *name = cbm_node_text(a, recv, source);
recv = name ? go_find_binding(a, recv, name, source) : (TSNode){0};
TSNode bound = {0};
if (name && !go_find_binding(ctx, recv, name, &bound)) {
return NULL; /* never a partial prefix */
}
recv = bound;
continue; /* unbound (parameter, package var): root reached */
}
TSNode inner = {0};
@@ -2238,7 +2451,7 @@ static const char *route_path_with_group_prefix(CBMExtractCtx *ctx, TSNode node,
if (ctx->language == CBM_LANG_PHP) {
gp = php_group_prefix_for_call(ctx->arena, node, ctx->source);
} else if (ctx->language == CBM_LANG_GO) {
gp = go_group_prefix_for_call(ctx->arena, node, ctx->source);
gp = go_group_prefix_for_call(ctx, node);
}
return (gp && gp[0]) ? route_path_with_prefix(ctx->arena, gp, path) : path;
}
+49 -18
View File
@@ -11,6 +11,7 @@
#include "foundation/sha256.h"
#include "foundation/mem_core.h" // cbm_realloc/cbm_free -- walk_defs stack
#include "extract_node_stack.h"
#include "resolve_stats.h"
#include "simhash/minhash.h"
#include "semantic/ast_profile.h"
#include "tree_sitter/api.h" // TSNode, ts_node_*
@@ -779,6 +780,11 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang) {
for (int tmpl_depth = 0; tmpl_depth < MAX_TEMPLATE_DEPTH; tmpl_depth++) {
const char *kind = ts_node_type(node);
// C#: an overloaded operator has no name field, its token names it.
if (lang == CBM_LANG_CSHARP && strcmp(kind, "operator_declaration") == 0) {
return ts_node_child_by_field_name(node, TS_FIELD("operator"));
}
if (lang == CBM_LANG_HASKELL && strcmp(kind, "signature") == 0) {
TSNode null_node = {0};
return null_node;
@@ -1787,6 +1793,7 @@ static bool doc_memo_fill(CBMArena *a, doc_memo_ent_t *e, TSNode parent) {
} while (k < n && ts_tree_cursor_goto_next_sibling(&cur));
}
ts_tree_cursor_delete(&cur);
cbm_resolve_stats_add(CBM_RESOLVE_STAT_SYNTAX_CHILD_STEPS, k);
e->node = parent;
e->n = k;
e->used = true;
@@ -1892,6 +1899,10 @@ static TSNode doc_parent(CBMExtractCtx *ctx, TSNode node) {
const doc_memo_ent_t *e = doc_memo_ancestor((const doc_memo_t *)ctx->doc_memo, node);
if (!e) {
e = doc_memo_get(ctx, ctx->root);
} else {
/* Use keeps it cached: a wide root evicted by its children's narrow lists is listed
* again every few definitions. */
((doc_memo_ent_t *)e)->stamp = ++((doc_memo_t *)ctx->doc_memo)->clock;
}
while (e) {
int k = doc_memo_containing(e, node);
@@ -8469,6 +8480,28 @@ static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *so
return text && text[0] ? text : NULL;
}
/* Note the first named file-scoped namespace among top's named children, once per
* file: asked per type, ts_node_named_child(top, i) steps over the i children
* before it, and a file of n types paid that for every type. */
static void cs_note_file_namespace(CBMExtractCtx *ctx, TSNode top) {
ctx->cs_file_ns_top = top.id;
ctx->cs_file_ns_name = NULL;
ctx->cs_file_ns_start = 0;
TSTreeCursor cur = ts_tree_cursor_new(top);
bool more = ts_tree_cursor_goto_first_child(&cur);
while (more && !ctx->cs_file_ns_name) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_SYNTAX_CHILD_STEPS, 1);
TSNode c = ts_tree_cursor_current_node(&cur);
if (ts_node_is_named(c) &&
strcmp(ts_node_type(c), "file_scoped_namespace_declaration") == 0) {
ctx->cs_file_ns_name = cs_namespace_decl_name(ctx->arena, c, ctx->source);
ctx->cs_file_ns_start = ts_node_start_byte(c);
}
more = ts_tree_cursor_goto_next_sibling(&cur);
}
ts_tree_cursor_delete(&cur);
}
/* Declared namespace of a C# type declaration: every enclosing namespace
* block (outer to inner) under an optional file-scoped namespace, which the
* grammar may attach either as an ancestor or as a preceding sibling of the
@@ -8481,7 +8514,9 @@ static const char *cs_namespace_decl_name(CBMArena *a, TSNode ns, const char *so
* grammar does not know (a `ref partial struct` with members does it), leaving
* the types after it parented straight under the compilation unit. Calling
* those global would let them hide every imported type of the same name. */
static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *source) {
static const char *cs_type_decl_namespace(CBMExtractCtx *ctx, TSNode node) {
CBMArena *a = ctx->arena;
const char *source = ctx->source;
const char *acc = NULL;
bool file_scoped = false;
TSNode top = node;
@@ -8500,19 +8535,14 @@ static const char *cs_type_decl_namespace(CBMArena *a, TSNode node, const char *
if (file_scoped) {
return acc;
}
uint32_t start = ts_node_start_byte(node);
uint32_t n = ts_node_named_child_count(top);
for (uint32_t i = 0; i < n; i++) {
TSNode c = ts_node_named_child(top, i);
if (ts_node_start_byte(c) >= start) {
break;
}
if (strcmp(ts_node_type(c), "file_scoped_namespace_declaration") == 0) {
const char *name = cs_namespace_decl_name(a, c, source);
if (name) {
return acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
}
}
if (ctx->cs_file_ns_top != top.id) {
cs_note_file_namespace(ctx, top);
}
/* Children come in source order: the first named one precedes this type exactly when
* some named one does. */
if (ctx->cs_file_ns_name && ctx->cs_file_ns_start < ts_node_start_byte(node)) {
const char *name = ctx->cs_file_ns_name;
return acc ? cbm_arena_sprintf(a, "%s.%s", name, acc) : name;
}
if (acc) {
return acc;
@@ -8921,7 +8951,7 @@ static void emit_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *s
def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);
def.is_exported = cbm_is_exported(name, ctx->language);
if (ctx->language == CBM_LANG_CSHARP && !ctx->enclosing_class_qn) {
def.decl_namespace = cs_type_decl_namespace(a, node, ctx->source);
def.decl_namespace = cs_type_decl_namespace(ctx, node);
}
if (ctx->language == CBM_LANG_SWIFT && ctx->module_qn) {
TSNode kind_node = ts_node_child_by_field_name(node, TS_FIELD("declaration_kind"));
@@ -9317,9 +9347,10 @@ static TSNode resolve_method_name(TSNode child, CBMLanguage lang) {
const char *ck = ts_node_type(child);
if ((lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
lang == CBM_LANG_GLSL) &&
strcmp(ck, "function_definition") == 0) {
if (((lang == CBM_LANG_C || lang == CBM_LANG_CPP || lang == CBM_LANG_CUDA ||
lang == CBM_LANG_GLSL) &&
strcmp(ck, "function_definition") == 0) ||
(lang == CBM_LANG_CSHARP && strcmp(ck, "operator_declaration") == 0)) {
return cbm_resolve_func_name(child, lang);
}
+49 -21
View File
@@ -15,6 +15,7 @@ enum { MAX_INFRA_BINDINGS = 8 };
#include <stdint.h> // uint32_t, uint8_t
#include <string.h>
#include <strings.h> // strcasecmp (ObjectScript type inference)
#include "resolve_stats.h"
// --- Scope stack management ---
@@ -2590,18 +2591,50 @@ static TSNode nasm_wrapped_label(TSNode node, int remaining_depth) {
return (TSNode){0};
}
static TSNode nasm_preceding_label(TSNode node) {
TSNode current = node;
for (int level = 0; level < 4 && !ts_node_is_null(current); level++) {
TSNode previous = ts_node_prev_named_sibling(current);
while (!ts_node_is_null(previous)) {
TSNode label = nasm_wrapped_label(previous, 2);
if (!ts_node_is_null(label)) {
return label;
}
previous = ts_node_prev_named_sibling(previous);
/* The nearest earlier sibling a probe finds a label in. The walk reaches siblings in order, so a
* search under the same parent stops at the sibling the last one started from, and what that one
* found holds for everything before it: each sibling is probed once however many ask. The caller
* passes node's parent, which it needs anyway. */
typedef TSNode (*flat_label_probe_fn)(TSNode sibling);
static TSNode flat_preceding_label(CBMFlatLookback *memo, TSNode parent, TSNode node,
flat_label_probe_fn probe) {
bool known = memo->parent && memo->parent == parent.id;
if (known && memo->asked == node.id) {
return memo->found;
}
TSNode found = {0};
for (TSNode previous = ts_node_prev_named_sibling(node); !ts_node_is_null(previous);
previous = ts_node_prev_named_sibling(previous)) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
found = probe(previous);
if (!ts_node_is_null(found)) {
break;
}
if (known && memo->asked == previous.id) {
found = memo->found;
break;
}
}
memo->parent = parent.id;
memo->asked = node.id;
memo->found = found;
return found;
}
static TSNode nasm_label_probe(TSNode sibling) {
return nasm_wrapped_label(sibling, 2);
}
static TSNode nasm_preceding_label(TSNode node, WalkState *state) {
TSNode current = node;
for (int level = 0; level < FLAT_LOOKBACK_LEVELS && !ts_node_is_null(current); level++) {
TSNode parent = ts_node_parent(current);
TSNode label =
flat_preceding_label(&state->flat_lookback[level], parent, current, nasm_label_probe);
if (!ts_node_is_null(label)) {
return label;
}
if (ts_node_is_null(parent) || strcmp(ts_node_type(current), "source_file") == 0) {
break;
}
@@ -2621,7 +2654,7 @@ static TSNode objectscript_routine_statement_tag(TSNode statement) {
: cbm_find_child_by_kind(tag_statement, "tag");
}
static TSNode objectscript_routine_preceding_tag(TSNode node) {
static TSNode objectscript_routine_preceding_tag(TSNode node, WalkState *state) {
if (strcmp(ts_node_type(node), "statement") != 0) {
return (TSNode){0};
}
@@ -2631,14 +2664,8 @@ static TSNode objectscript_routine_preceding_tag(TSNode node) {
!ts_node_is_null(cbm_find_child_by_kind(node, "procedure"))) {
return (TSNode){0};
}
for (TSNode previous = ts_node_prev_named_sibling(node); !ts_node_is_null(previous);
previous = ts_node_prev_named_sibling(previous)) {
TSNode tag = objectscript_routine_statement_tag(previous);
if (!ts_node_is_null(tag)) {
return tag;
}
}
return (TSNode){0};
return flat_preceding_label(&state->flat_lookback[0], parent, node,
objectscript_routine_statement_tag);
}
static bool push_pre_node_scope(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec,
@@ -2651,7 +2678,7 @@ static bool push_pre_node_scope(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
TSNode label = {0};
if (ctx->language == CBM_LANG_NASM && strcmp(ts_node_type(node), "actual_instruction") == 0) {
label = nasm_preceding_label(node);
label = nasm_preceding_label(node, state);
} else if (ctx->language == CBM_LANG_OBJECTSCRIPT_ROUTINE) {
if (strcmp(ts_node_type(node), "tag_statement") == 0) {
/* A parameterized flat tag owns its header as well as the following
@@ -2659,7 +2686,7 @@ static bool push_pre_node_scope(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
* parameter_list records lexical bindings under the same tag QN. */
label = cbm_find_child_by_kind(node, "tag");
} else {
label = objectscript_routine_preceding_tag(node);
label = objectscript_routine_preceding_tag(node, state);
}
}
if (ts_node_is_null(label)) {
@@ -3063,6 +3090,7 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
CBMInvocationDescriptor invocation =
tf_module ? (CBMInvocationDescriptor){0} : handle_calls(ctx, node, spec, &state);
if (!tf_module) {
state.walk_depth = depth;
handle_usages(ctx, node, spec, &state);
}
handle_throws(ctx, node, spec, &state);
+20
View File
@@ -20,6 +20,14 @@
#define INLINE_PYTHON_DIRECTIVES 16
#define INLINE_PY_PARAM_SLOTS 64
#define INLINE_PY_PARAM_STACK 64
#define FLAT_LOOKBACK_LEVELS 4
/* Under parent, the search from sibling `asked` found this label, or none. */
typedef struct {
const void *parent;
const void *asked;
TSNode found;
} CBMFlatLookback;
// ObjectScript type map: variable name → class name (for instance_method_call
// resolution). Stack-allocated, per-method scope. Overflow is silent (no crash).
@@ -129,6 +137,12 @@ typedef struct {
uint8_t kind;
} CBMPythonDirective;
// The lowest node a usage classifier has climbed from to the root undecided.
typedef struct {
TSNode node;
uint32_t depth;
} CBMOccurrenceClimb;
// WalkState tracks scope context during the unified cursor walk.
// Replaces parent-chain walks for enclosing_func_qn, import context, etc.
typedef struct {
@@ -137,6 +151,10 @@ typedef struct {
TSNode enclosing_class_node; // that class scope's node (null when not known)
const TSTreeCursor *current_cursor; // unified walk cursor at the current node
TSTreeCursor *occurrence_cursor; // reusable parent-preserving classifier cursor
uint32_t walk_depth; // depth of the current node in the unified walk
CBMOccurrenceClimb binding_climb; // settled by is_binding_occurrence
CBMOccurrenceClimb label_climb; // settled by is_call_argument_label_walk
CBMOccurrenceClimb write_climb; // settled by is_write_occurrence
CBMInvocationKind invocation_kind; // exact active invocation/reference role
TSNode callee_expr; // exact active callee expression, if any
TSNode callee_leaf; // exact active terminal callee, if any
@@ -163,6 +181,8 @@ typedef struct {
uint32_t flat_anchor_start_byte;
uint32_t flat_anchor_end_byte;
const char *flat_function_qn;
/* What the last flat label search found, one per ancestor level it looked at. */
CBMFlatLookback flat_lookback[FLAT_LOOKBACK_LEVELS];
CBMLexicalBinding *lexical_bindings;
CBMLexicalBinding inline_lexical_bindings[INLINE_LEXICAL_BINDINGS];
+52 -2
View File
@@ -295,6 +295,21 @@ static TSTreeCursor *reset_occurrence_cursor(WalkState *state, TSNode node) {
return state->occurrence_cursor;
}
/* Grammars with few fields let a classifier climb to the root from every identifier,
* so nesting d levels deep costs d per identifier. A climb that reaches the root
* undecided settles every node on its way for all nodes below them: a level's answer
* depends only on the path through it, and a node with any extent lies in one child of
* its parent. The next climb stops where it meets that path: a node no deeper than the
* settled one and covering it is the settled one or above it. */
static bool occurrence_climb_settled(const CBMOccurrenceClimb *climb, TSNode current,
uint32_t depth) {
return climb && depth <= climb->depth && node_contains(current, climb->node);
}
static CBMOccurrenceClimb *occurrence_climb_for(TSNode node, CBMOccurrenceClimb *climb) {
return ts_node_end_byte(node) > ts_node_start_byte(node) ? climb : NULL;
}
static bool occurrence_parent(TSTreeCursor *cursor, TSNode current, TSNode *parent,
const char **field) {
if (cursor) {
@@ -1154,12 +1169,22 @@ static bool is_binding_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLang
TSNode current = node;
TSTreeCursor *cursor = reset_occurrence_cursor(state, node);
CBMOccurrenceClimb *climb = cursor ? occurrence_climb_for(node, &state->binding_climb) : NULL;
uint32_t depth = cursor ? state->walk_depth : 0;
TSNode first_parent = {0};
while (!ts_node_is_null(current)) {
TSNode parent;
const char *field;
if (occurrence_climb_settled(climb, current, depth)) {
break;
}
if (!occurrence_parent(cursor, current, &parent, &field)) {
break;
}
depth--;
if (ts_node_is_null(first_parent)) {
first_parent = parent;
}
if (is_value_field(field)) {
return false;
}
@@ -1208,6 +1233,9 @@ static bool is_binding_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLang
}
current = parent;
}
if (climb && !ts_node_is_null(first_parent)) {
*climb = (CBMOccurrenceClimb){first_parent, state->walk_depth - 1};
}
return false;
}
@@ -1254,12 +1282,22 @@ static bool is_write_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
const CBMOccurrenceSpec *occurrence = &occurrence_specs[ctx->language];
TSNode current = node;
TSTreeCursor *cursor = reset_occurrence_cursor(state, node);
CBMOccurrenceClimb *climb = cursor ? occurrence_climb_for(node, &state->write_climb) : NULL;
uint32_t depth = cursor ? state->walk_depth : 0;
TSNode first_parent = {0};
while (!ts_node_is_null(current)) {
TSNode parent;
const char *field;
if (occurrence_climb_settled(climb, current, depth)) {
break;
}
if (!occurrence_parent(cursor, current, &parent, &field)) {
break;
}
depth--;
if (ts_node_is_null(first_parent)) {
first_parent = parent;
}
if (is_value_field(field)) {
return false;
}
@@ -1291,6 +1329,9 @@ static bool is_write_occurrence(CBMExtractCtx *ctx, TSNode node, const CBMLangSp
}
current = parent;
}
if (climb && !ts_node_is_null(first_parent)) {
*climb = (CBMOccurrenceClimb){first_parent, state->walk_depth - 1};
}
return false;
}
@@ -1332,12 +1373,19 @@ static bool is_call_argument_label_walk(TSNode node, WalkState *state) {
TSTreeCursor *cursor = reset_occurrence_cursor(state, node);
if (!cursor)
return is_call_argument_label(node);
CBMOccurrenceClimb *climb = occurrence_climb_for(node, &state->label_climb);
uint32_t depth = state->walk_depth;
TSNode first_parent = {0};
TSNode current = node;
while (!ts_node_is_null(current)) {
TSNode parent;
const char *field;
if (!occurrence_parent(cursor, current, &parent, &field))
return false;
if (occurrence_climb_settled(climb, current, depth) ||
!occurrence_parent(cursor, current, &parent, &field))
break;
depth--;
if (ts_node_is_null(first_parent))
first_parent = parent;
const char *parent_kind = ts_node_type(parent);
if (is_labeled_argument_kind(parent_kind)) {
return field && (strcmp(field, "name") == 0 || strcmp(field, "label") == 0 ||
@@ -1347,6 +1395,8 @@ static bool is_call_argument_label_walk(TSNode node, WalkState *state) {
return false;
current = parent;
}
if (climb && !ts_node_is_null(first_parent))
*climb = (CBMOccurrenceClimb){first_parent, state->walk_depth - 1};
return false;
}
+3
View File
@@ -0,0 +1,3 @@
// Vendored tree-sitter grammar: vba
// Each grammar compiled as separate unit (conflicting static symbols).
#include "vendored/grammars/vba/parser.c"
+14 -1
View File
@@ -892,7 +892,8 @@ static const char *func_kinds_ocaml[] = {"value_definition", "let_binding", NULL
static const char *func_kinds_zig[] = {"function_declaration", "test_declaration", NULL};
static const char *func_kinds_bash[] = {"function_definition", NULL};
static const char *func_kinds_erlang[] = {"function_clause", NULL};
static const char *func_kinds_csharp[] = {"method_declaration", "constructor_declaration", NULL};
static const char *func_kinds_csharp[] = {"method_declaration", "constructor_declaration",
"operator_declaration", NULL};
static const char *func_kinds_matlab[] = {"function_definition", NULL};
static const char *func_kinds_lean[] = {"def", "theorem", "instance", "abbrev", NULL};
static const char *func_kinds_form[] = {"procedure_definition", NULL};
@@ -1241,6 +1242,13 @@ char *cbm_func_name_node_text(CBMArena *a, TSNode name_node, const char *source,
*paren = '\0';
}
}
/* C# `operator +` is named by its token alone, an anonymous node; any other C# name
* is a named node. Spelled like C++'s `operator+` and `operator bool`, here so the
* def name and the call-scope QN cannot disagree. */
if (text && lang == CBM_LANG_CSHARP && !ts_node_is_named(name_node)) {
bool word = text[0] >= 'a' && text[0] <= 'z'; /* operator true, operator false */
return cbm_arena_sprintf(a, "operator%s%s", word ? " " : "", text);
}
/* Nix quoted attrpath segment: `"kebab-case" = a: a;` and `services."my.svc" = …`
* are ordinary names that merely need quoting in source. The node text carries
* the delimiters, so without this the def is named `"kebab-case"` — quotes and
@@ -1531,6 +1539,11 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
if (!ts_node_is_null(name_node)) {
return cbm_node_text(a, name_node, source);
}
// C#: an overloaded operator has no name field, its token names it.
if (lang == CBM_LANG_CSHARP && strcmp(ts_node_type(func_node), "operator_declaration") == 0) {
TSNode op = ts_node_child_by_field_name(func_node, TS_FIELD("operator"));
return ts_node_is_null(op) ? NULL : cbm_func_name_node_text(a, op, source, lang);
}
// Arrow functions: check parent variable_declarator
if (strcmp(ts_node_type(func_node), "arrow_function") == 0) {
TSNode parent = ts_node_parent(func_node);
+7 -5
View File
@@ -365,14 +365,16 @@ static const char *cpp_branch_types[] = {"if_statement", "for_statement",
"while_statement", "switch_statement", "case_statement",
"try_statement", "catch_clause", NULL};
static const char *cpp_var_types[] = {"declaration", NULL};
/* No update_expression here, unlike C: a bare C++ name can be an implicit-this
* member, so a local counter's `i++` would bind by name to an unrelated member. */
static const char *cpp_assign_types[] = {"assignment_expression", NULL};
static const char *cpp_throw_types[] = {"throw_statement", NULL};
// ==================== C# ====================
static const char *cs_func_types[] = {"destructor_declaration", "local_function_statement",
"function_pointer_type", "constructor_declaration",
"anonymous_method_expression", "lambda_expression",
"method_declaration", NULL};
static const char *cs_func_types[] = {
"destructor_declaration", "local_function_statement", "function_pointer_type",
"constructor_declaration", "anonymous_method_expression", "lambda_expression",
"method_declaration", "operator_declaration", NULL};
static const char *cs_class_types[] = {"class_declaration",
"struct_declaration",
"enum_declaration",
@@ -490,7 +492,7 @@ static const char *c_branch_types[] = {"if_statement",
"case_statement",
NULL};
static const char *c_var_types[] = {"declaration", NULL};
static const char *c_assign_types[] = {"assignment_expression", NULL};
static const char *c_assign_types[] = {"assignment_expression", "update_expression", NULL};
// ==================== BASH ====================
static const char *bash_func_types[] = {"function_definition", NULL};
+89 -8
View File
@@ -40,6 +40,8 @@
*/
#include "cs_lsp.h"
#include "lsp_name_rows.h"
#include "lsp_work.h"
#include "lsp_node_iter.h"
#include "../helpers.h"
#include <ctype.h>
@@ -3443,20 +3445,38 @@ typedef struct {
const CBMType *rt;
} cs_method_rt_entry_t;
enum { CS_SCRATCH_ARENA_BLOCK = 4096 };
typedef struct {
cs_method_rt_entry_t *items;
int count;
int cap;
/* The rows by QN (lsp_name_rows.h), when the caller gives a scratch arena;
* without one, or once a row could not be filed, the dedup scans. */
CBMNameRows by_qn;
CBMArena *rows_arena;
bool rows_failed;
} cs_method_rt_table_t;
static bool cs_method_rt_seen(const cs_method_rt_table_t *tab, const char *qn) {
if (tab->rows_arena && !tab->rows_failed) {
CBM_LSP_WORK(1);
return cbm_name_rows_lookup(&tab->by_qn, NULL, qn) >= 0;
}
for (int i = 0; i < tab->count; i++) {
CBM_LSP_WORK(1);
if (strcmp(tab->items[i].qn, qn) == 0)
return true;
}
return false;
}
static void cs_method_rt_add(CBMArena *arena, cs_method_rt_table_t *tab, const char *qn,
const CBMType *rt) {
if (!qn || !rt)
return;
for (int i = 0; i < tab->count; i++) {
if (strcmp(tab->items[i].qn, qn) == 0)
return;
}
if (cs_method_rt_seen(tab, qn))
return;
if (tab->count + 1 >= tab->cap) {
int new_cap = tab->cap ? tab->cap * 2 : 16;
cs_method_rt_entry_t *ne =
@@ -3470,9 +3490,49 @@ static void cs_method_rt_add(CBMArena *arena, cs_method_rt_table_t *tab, const c
}
tab->items[tab->count].qn = qn;
tab->items[tab->count].rt = rt;
if (tab->rows_arena && !tab->rows_failed &&
!cbm_name_rows_add(tab->rows_arena, &tab->by_qn, NULL, qn, tab->count))
tab->rows_failed = true;
tab->count++;
}
/* The first registration of `qn` among reg's functions or types. `qns` is a
* snapshot of their QNs, chained by name (lsp_name_rows.h); nothing registers
* while a caller uses it. Without a snapshot this is the scan it replaces. */
static int cs_first_func_row(const CBMTypeRegistry *reg, CBMNameRows *rows, CBMArena *arena,
const char **qns, const char *qn) {
if (qns)
return cbm_name_rows_first(rows, arena, qns, reg->func_count, qn);
for (int j = 0; j < reg->func_count; j++) {
CBM_LSP_WORK(1);
if (reg->funcs[j].qualified_name && strcmp(reg->funcs[j].qualified_name, qn) == 0)
return j;
}
return -1;
}
static int cs_first_type_row(const CBMTypeRegistry *reg, CBMNameRows *rows, CBMArena *arena,
const char **qns, const char *qn) {
if (qns)
return cbm_name_rows_first(rows, arena, qns, reg->type_count, qn);
for (int t = 0; t < reg->type_count; t++) {
CBM_LSP_WORK(1);
if (reg->types[t].qualified_name && strcmp(reg->types[t].qualified_name, qn) == 0)
return t;
}
return -1;
}
/* The QNs of reg's functions (types: false -> functions), in a scratch arena. */
static const char **cs_qn_snapshot(CBMArena *arena, const CBMTypeRegistry *reg, bool types) {
int count = types ? reg->type_count : reg->func_count;
const char **qns =
count > 0 ? (const char **)cbm_arena_alloc(arena, (size_t)count * sizeof(*qns)) : NULL;
for (int i = 0; qns && i < count; i++)
qns[i] = types ? reg->types[i].qualified_name : reg->funcs[i].qualified_name;
return qns;
}
/* Walk the tree, finding method_declaration / property_declaration nodes
* and recording (parent_class_qn + "." + method_name → return type). */
static void cs_collect_method_return_types(CSLSPContext *ctx, TSNode root,
@@ -3585,13 +3645,20 @@ void cbm_cs_refine_ast_return_types(CSLSPContext *ctx, CBMTypeRegistry *reg, TSN
if (!ctx || !reg || ts_node_is_null(root))
return;
/* Each method patches the first registration of its QN; the loop starts there. */
CBMArena scratch;
cbm_arena_init_lazy(&scratch, CS_SCRATCH_ARENA_BLOCK);
cs_method_rt_table_t mtab = {0};
mtab.rows_arena = &scratch;
cs_collect_method_return_types(ctx, root, &mtab);
const char **func_qns = mtab.count > 0 ? cs_qn_snapshot(&scratch, reg, false) : NULL;
CBMNameRows func_rows = {0};
for (int i = 0; i < mtab.count; i++) {
const char *qn = mtab.items[i].qn;
const CBMType *rt = mtab.items[i].rt;
bool patched = false;
for (int j = 0; j < reg->func_count; j++) {
int first = cs_first_func_row(reg, &func_rows, &scratch, func_qns, qn);
for (int j = first >= 0 ? first : reg->func_count; j < reg->func_count; j++) {
if (strcmp(reg->funcs[j].qualified_name, qn) == 0) {
const CBMType **rets =
(const CBMType **)cbm_arena_alloc(ctx->arena, 2 * sizeof(const CBMType *));
@@ -3607,6 +3674,7 @@ void cbm_cs_refine_ast_return_types(CSLSPContext *ctx, CBMTypeRegistry *reg, TSN
}
(void)patched;
}
cbm_arena_destroy(&scratch);
}
/* ── parse return type from CBMDefinition.return_type ──────────── */
@@ -3719,6 +3787,15 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
}
/* The registrations are complete: each patch below finds the first registration
* of its QN by name instead of scanning them all per definition. */
CBMArena scratch;
cbm_arena_init_lazy(&scratch, CS_SCRATCH_ARENA_BLOCK);
const char **func_qns = cs_qn_snapshot(&scratch, &reg, false);
const char **type_qns = cs_qn_snapshot(&scratch, &reg, true);
CBMNameRows func_rows = {0};
CBMNameRows type_rows = {0};
/* Phase C: build context for type resolution + run the resolver. */
CSLSPContext ctx;
cs_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
@@ -3741,7 +3818,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
const CBMType **param_types = cbm_type_materialize_signature_params(
arena, d->signature_param_types, d->signature_param_count,
cs_signature_param_type_adapter, &ctx);
for (int j = 0; j < reg.func_count; j++) {
int first = cs_first_func_row(&reg, &func_rows, &scratch, func_qns, d->qualified_name);
for (int j = first >= 0 ? first : reg.func_count; j < reg.func_count; j++) {
if (strcmp(reg.funcs[j].qualified_name, d->qualified_name) != 0)
continue;
const CBMType **return_types = NULL;
@@ -3763,7 +3841,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
continue;
const CBMType *rt = cs_parse_return_type_text(&ctx, d->return_type);
/* Find the registered func and patch its signature. */
for (int j = 0; j < reg.func_count; j++) {
int first = cs_first_func_row(&reg, &func_rows, &scratch, func_qns, d->qualified_name);
for (int j = first >= 0 ? first : reg.func_count; j < reg.func_count; j++) {
if (strcmp(reg.funcs[j].qualified_name, d->qualified_name) == 0) {
const CBMType **rets =
(const CBMType **)cbm_arena_alloc(arena, 2 * sizeof(const CBMType *));
@@ -3791,7 +3870,8 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
cs_fields_t *f = &tab.items[i];
if (f->count == 0)
continue;
for (int t = 0; t < reg.type_count; t++) {
int first = cs_first_type_row(&reg, &type_rows, &scratch, type_qns, f->class_qn);
for (int t = first >= 0 ? first : reg.type_count; t < reg.type_count; t++) {
if (strcmp(reg.types[t].qualified_name, f->class_qn) == 0) {
reg.types[t].field_names = f->field_names;
reg.types[t].field_types = f->field_types;
@@ -3800,6 +3880,7 @@ void cbm_run_cs_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
}
}
cbm_arena_destroy(&scratch);
/* Re-bind ctx state since collect_class_fields may have temporarily
* adjusted namespace stack (it doesn't, but be safe). */
+132 -51
View File
@@ -1,5 +1,7 @@
#include "go_lsp.h"
#include "lsp_node_iter.h"
#include "lsp_neg_memo.h"
#include "lsp_work.h"
#include "../../../src/foundation/hash_table.h"
#include "../helpers.h"
#include <string.h>
@@ -82,13 +84,14 @@ static char *lsp_node_text(GoLSPContext *ctx, TSNode node) {
// --- Helper: resolve import alias to package QN ---
/* The first import of this local name. Found by name (lsp_name_rows.h): a scan
* per use made a file of thousands of imports quadratic. */
static const char *resolve_import(GoLSPContext *ctx, const char *local_name) {
for (int i = 0; i < ctx->import_count; i++) {
if (strcmp(ctx->import_local_names[i], local_name) == 0) {
return ctx->import_package_qns[i];
}
}
return NULL;
if (!local_name)
return NULL;
int i = cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
ctx->import_count, local_name);
return i >= 0 ? ctx->import_package_qns[i] : NULL;
}
// --- Helper: check if name is a Go builtin ---
@@ -2263,6 +2266,47 @@ static void go_registry_with_stdlib(CBMTypeRegistry *reg, CBMArena *arena) {
}
}
/* The first row of each type QN in a filling registry, as a scan from the top
* finds it. Registering and refining a file's types asked a scan of every type
* per definition, and a generated file holds thousands. A row the memo cannot
* take (no memory) sends every later question back to the scan. */
enum { GO_MEMO_ARENA_BLOCK = 4096 };
typedef struct {
CBMIdxMemo first;
CBMArena *arena;
bool failed;
} GoTypeRows;
static void go_type_rows_note(GoTypeRows *rows, const CBMTypeRegistry *reg, int row) {
const char *qn = reg->types[row].qualified_name;
if (rows->failed || !qn)
return;
CBM_LSP_WORK(1);
cbm_idxmemo_put_if_absent(&rows->first, rows->arena, qn, row);
if (cbm_idxmemo_get(&rows->first, qn) < 0)
rows->failed = true;
}
static void go_type_rows_add(CBMTypeRegistry *reg, GoTypeRows *rows, CBMRegisteredType type) {
int before = reg->type_count;
cbm_registry_add_type(reg, type);
if (reg->type_count > before)
go_type_rows_note(rows, reg, reg->type_count - 1);
}
static int go_type_rows_first(const GoTypeRows *rows, const CBMTypeRegistry *reg, const char *qn) {
CBM_LSP_WORK(1);
if (!rows->failed)
return cbm_idxmemo_get(&rows->first, qn);
for (int ti = 0; ti < reg->type_count; ti++) {
CBM_LSP_WORK(1);
if (reg->types[ti].qualified_name && strcmp(reg->types[ti].qualified_name, qn) == 0)
return ti;
}
return -1;
}
// --- Entry point: build registry from file defs + run LSP ---
void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
@@ -2274,6 +2318,14 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
// Go stdlib types/functions: the shared sealed registry, chained
go_registry_with_stdlib(&reg, arena);
/* Rows of the file's own registry by QN, from whatever it holds already (the
* stdlib, when it could not be shared) to the types registered below. */
CBMArena memo_arena;
cbm_arena_init_lazy(&memo_arena, GO_MEMO_ARENA_BLOCK);
GoTypeRows own_types = {.arena = &memo_arena};
for (int ti = 0; ti < reg.type_count; ti++)
go_type_rows_note(&own_types, &reg, ti);
const char *module_qn = result->module_qn;
// Phase 1: Register all types and functions from the file's own definitions
@@ -2313,7 +2365,7 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
}
cbm_registry_add_type(&reg, rt);
go_type_rows_add(&reg, &own_types, rt);
}
// Register Function/Method nodes
@@ -2402,13 +2454,16 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
}
if (rf.receiver_type && !cbm_registry_lookup_type(&reg, rf.receiver_type)) {
/* reg's own types are all in own_types; the stdlib is behind. */
if (rf.receiver_type &&
go_type_rows_first(&own_types, &reg, rf.receiver_type) < 0 &&
!cbm_registry_lookup_type(reg.fallback, rf.receiver_type)) {
const char *dot = strrchr(rf.receiver_type, '.');
CBMRegisteredType auto_type;
memset(&auto_type, 0, sizeof(auto_type));
auto_type.qualified_name = rf.receiver_type;
auto_type.short_name = dot ? dot + 1 : rf.receiver_type;
cbm_registry_add_type(&reg, auto_type);
go_type_rows_add(&reg, &own_types, auto_type);
}
}
@@ -2446,22 +2501,16 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
cbm_arena_sprintf(arena, "%s.%s", module_qn, aname);
const char *alias_target_qn =
cbm_arena_sprintf(arena, "%s.%s", module_qn, atarget);
bool found_a = false;
for (int ti = 0; ti < reg.type_count; ti++) {
if (reg.types[ti].qualified_name &&
strcmp(reg.types[ti].qualified_name, alias_type_qn) == 0) {
reg.types[ti].alias_of = alias_target_qn;
found_a = true;
break;
}
}
if (!found_a) {
int alias_at = go_type_rows_first(&own_types, &reg, alias_type_qn);
if (alias_at >= 0) {
reg.types[alias_at].alias_of = alias_target_qn;
} else {
CBMRegisteredType alias_rt;
memset(&alias_rt, 0, sizeof(alias_rt));
alias_rt.qualified_name = alias_type_qn;
alias_rt.short_name = aname;
alias_rt.alias_of = alias_target_qn;
cbm_registry_add_type(&reg, alias_rt);
go_type_rows_add(&reg, &own_types, alias_rt);
}
}
}
@@ -2502,20 +2551,16 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
}
}
if (iface_method_count > 0) {
for (int ti = 0; ti < reg.type_count; ti++) {
if (!reg.types[ti].qualified_name ||
strcmp(reg.types[ti].qualified_name, type_qn) != 0)
continue;
const char **names = (const char **)cbm_arena_alloc(
arena, (iface_method_count + 1) * sizeof(const char *));
for (int mi = 0; mi < iface_method_count; mi++) {
names[mi] = iface_methods[mi];
}
names[iface_method_count] = NULL;
reg.types[ti].method_names = names;
break;
int iface_at =
iface_method_count > 0 ? go_type_rows_first(&own_types, &reg, type_qn) : -1;
if (iface_at >= 0) {
const char **names = (const char **)cbm_arena_alloc(
arena, (iface_method_count + 1) * sizeof(const char *));
for (int mi = 0; mi < iface_method_count; mi++) {
names[mi] = iface_methods[mi];
}
names[iface_method_count] = NULL;
reg.types[iface_at].method_names = names;
}
continue;
}
@@ -2585,11 +2630,8 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
// Find the registered type and update it
for (int ti = 0; ti < reg.type_count; ti++) {
if (!reg.types[ti].qualified_name ||
strcmp(reg.types[ti].qualified_name, type_qn) != 0)
continue;
int ti = go_type_rows_first(&own_types, &reg, type_qn);
if (ti >= 0) {
if (embed_count > 0) {
const char **arr = (const char **)cbm_arena_alloc(
arena, (embed_count + 1) * sizeof(const char *));
@@ -2612,11 +2654,11 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
reg.types[ti].field_names = names;
reg.types[ti].field_types = types;
}
break;
}
}
}
}
cbm_arena_destroy(&memo_arena);
// Phase 1c: Extract type parameters from generic function declarations
extract_type_params_from_ast(arena, &reg, root, source, module_qn);
@@ -3033,10 +3075,45 @@ static const CBMType *cbm_go_lsp_keep_known_refinement(const CBMType *candidate,
return candidate ? candidate : cbm_type_unknown();
}
/* The first registration of each function QN, listed when the first generic
* function asks: each one scanned every registered function for its own. The
* functions do not change while they are patched. */
typedef struct {
CBMIdxMemo first;
CBMArena arena;
bool listed;
bool failed; /* a row could not be remembered: scan instead */
} GoFuncRows;
static int go_func_rows_first(GoFuncRows *rows, const CBMTypeRegistry *reg, const char *qn) {
for (int fi = 0; !rows->listed && !rows->failed && fi < reg->func_count; fi++) {
const char *name = reg->funcs[fi].qualified_name;
if (!name)
continue;
CBM_LSP_WORK(1);
cbm_idxmemo_put_if_absent(&rows->first, &rows->arena, name, fi);
if (cbm_idxmemo_get(&rows->first, name) < 0)
rows->failed = true;
}
rows->listed = true;
CBM_LSP_WORK(1);
if (!rows->failed)
return cbm_idxmemo_get(&rows->first, qn);
for (int fi = 0; fi < reg->func_count; fi++) {
CBM_LSP_WORK(1);
if (reg->funcs[fi].qualified_name && strcmp(reg->funcs[fi].qualified_name, qn) == 0)
return fi;
}
return -1;
}
// Scan AST for generic function declarations and set type_param_names + re-parse
// return types on matching registered functions.
static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg, TSNode root,
const char *source, const char *module_qn) {
GoFuncRows func_rows;
memset(&func_rows, 0, sizeof(func_rows));
cbm_arena_init_lazy(&func_rows.arena, GO_MEMO_ARENA_BLOCK);
// Collect top-level children once (O(n)); ts_node_child(root,i) is O(i) → O(n²).
uint32_t rkn = 0;
@@ -3101,7 +3178,8 @@ static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg,
// Find the matching registered function and set type_param_names
const char *func_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, func_name);
for (int fi = 0; fi < reg->func_count; fi++) {
int first_fi = go_func_rows_first(&func_rows, reg, func_qn);
for (int fi = first_fi >= 0 ? first_fi : reg->func_count; fi < reg->func_count; fi++) {
if (reg->funcs[fi].qualified_name &&
strcmp(reg->funcs[fi].qualified_name, func_qn) == 0) {
reg->funcs[fi].type_param_names = tp_names;
@@ -3239,6 +3317,7 @@ static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg,
}
}
}
cbm_arena_destroy(&func_rows.arena);
}
// Forward: tree-sitter Go language (from lang_specs.c)
@@ -3345,6 +3424,19 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
// 3. Phase 1b: Scan AST for struct definitions to populate embedded_types
{
/* One context parses every struct's field types, so its import rows are
* chained once (resolve_import), not once per struct. */
GoLSPContext tmp_ctx;
memset(&tmp_ctx, 0, sizeof(tmp_ctx));
tmp_ctx.arena = arena;
tmp_ctx.source = source;
tmp_ctx.source_len = source_len;
tmp_ctx.registry = &reg;
tmp_ctx.package_qn = module_qn;
tmp_ctx.import_local_names = import_names;
tmp_ctx.import_package_qns = import_qns;
tmp_ctx.import_count = import_count;
// Collect top-level children once (O(n)); ts_node_child(root,i) is O(i) → O(n²).
uint32_t rkn = 0;
TSNode *rkids = cbm_lsp_collect_children(arena, root, &rkn);
@@ -3422,17 +3514,6 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
const CBMType *fld_types[64];
int fld_count = 0;
GoLSPContext tmp_ctx;
memset(&tmp_ctx, 0, sizeof(tmp_ctx));
tmp_ctx.arena = arena;
tmp_ctx.source = source;
tmp_ctx.source_len = source_len;
tmp_ctx.registry = &reg;
tmp_ctx.package_qn = module_qn;
tmp_ctx.import_local_names = import_names;
tmp_ctx.import_package_qns = import_qns;
tmp_ctx.import_count = import_count;
uint32_t fl_nc = ts_node_child_count(field_list);
for (uint32_t k = 0; k < fl_nc; k++) {
TSNode field = ts_node_child(field_list, k);
+2
View File
@@ -1,6 +1,7 @@
#ifndef CBM_LSP_GO_LSP_H
#define CBM_LSP_GO_LSP_H
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -18,6 +19,7 @@ typedef struct {
const char** import_local_names; // NULL-terminated
const char** import_package_qns; // NULL-terminated
int import_count;
CBMNameRows import_rows; // import_local_names by name, rows in table order
// Current function context
const char* enclosing_func_qn;
+74 -27
View File
@@ -32,6 +32,7 @@
*/
#include "java_lsp.h"
#include "lsp_work.h"
#include "../helpers.h"
#include <ctype.h>
@@ -318,6 +319,18 @@ void java_lsp_add_import(JavaLSPContext *ctx, const char *local_name, const char
ctx->import_count++;
}
/* The imports with this local name, in table order (lsp_name_rows.h): a scan per
* use made a file of thousands of imports quadratic. */
static int java_import_first(JavaLSPContext *ctx, const char *name) {
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
ctx->import_count, name);
}
static int java_import_next(const JavaLSPContext *ctx, const char *name, int row) {
return cbm_name_rows_next(&ctx->import_rows, ctx->import_local_names, ctx->import_count, name,
row);
}
static void push_enclosing_class(JavaLSPContext *ctx, const char *class_qn) {
if (ctx->enclosing_class_depth >= ctx->enclosing_class_cap) {
int new_cap = ctx->enclosing_class_cap == 0 ? 8 : ctx->enclosing_class_cap * 2;
@@ -502,7 +515,7 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
}
/* 2. Single-type imports. */
for (int i = 0; i < ctx->import_count; i++) {
for (int i = java_import_first(ctx, name); i >= 0; i = java_import_next(ctx, name, i)) {
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_TYPE)
continue;
if (strcmp(ctx->import_local_names[i], name) == 0) {
@@ -563,7 +576,12 @@ const char *java_resolve_type_name(JavaLSPContext *ctx, const char *name) {
if (ctx->registry && ctx->registry->types) {
const char *only_qn = NULL;
int matches = 0;
for (int i = 0; i < ctx->registry->type_count && matches < 2; i++) {
/* The registry's own types of this short name: by name once it is indexed
* (cbm_registry_index_types_into), else every type, as before. */
CBMTypeShortIter named;
cbm_registry_types_by_short_name(ctx->registry, name, &named);
for (int i = -1; matches < 2 && (i = cbm_type_short_iter_next(&named)) >= 0;) {
CBM_LSP_WORK(1);
const CBMRegisteredType *t = &ctx->registry->types[i];
if (!t->qualified_name || !t->short_name || t->alias_of) {
continue;
@@ -818,7 +836,7 @@ static const CBMType *resolve_identifier_type(JavaLSPContext *ctx, const char *n
}
/* Static imports — could be a static field. */
for (int i = 0; i < ctx->import_count; i++) {
for (int i = java_import_first(ctx, name); i >= 0; i = java_import_next(ctx, name, i)) {
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_STATIC)
continue;
if (strcmp(ctx->import_local_names[i], name) == 0) {
@@ -1121,7 +1139,7 @@ static const CBMType *eval_method_invocation(JavaLSPContext *ctx, TSNode node) {
if (f)
return java_return_type_of(f);
}
for (int i = 0; i < ctx->import_count; i++) {
for (int i = java_import_first(ctx, mname); i >= 0; i = java_import_next(ctx, mname, i)) {
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_STATIC)
continue;
if (strcmp(ctx->import_local_names[i], mname) != 0)
@@ -2148,7 +2166,7 @@ static void resolve_method_call(JavaLSPContext *ctx, TSNode call) {
}
}
/* Static import. */
for (int i = 0; i < ctx->import_count; i++) {
for (int i = java_import_first(ctx, mname); i >= 0; i = java_import_next(ctx, mname, i)) {
if (ctx->import_kinds[i] != CBM_JAVA_IMPORT_STATIC)
continue;
if (strcmp(ctx->import_local_names[i], mname) != 0)
@@ -3359,6 +3377,10 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
: d->qualified_name;
cbm_registry_add_type(reg, stub);
}
/* Every local type is in: from here on a type is found by name, not by a scan of
* every registered type per definition (first registrations, as the scan). */
if (reg->tail_arena)
cbm_registry_index_types_into(reg, reg->tail_arena);
/* Pass 1b: now that every local type is in the registry, walk again and
* fill in embedded_types using the parent_class chain. We mutate the
@@ -3383,15 +3405,7 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
do the in-place update directly. */
(void)rt;
/* Locate the actual entry in reg->types[] and write into it. */
CBMRegisteredType *slot = NULL;
for (int ti = 0; ti < reg->type_count; ti++) {
if (reg->types[ti].qualified_name == d->qualified_name ||
(reg->types[ti].qualified_name &&
strcmp(reg->types[ti].qualified_name, d->qualified_name) == 0)) {
slot = &reg->types[ti];
break;
}
}
CBMRegisteredType *slot = cbm_registry_first_own_type(reg, d->qualified_name);
if (!slot)
continue;
if (d->base_classes) {
@@ -3563,6 +3577,25 @@ static void register_local_func_or_type_from_file(JavaLSPContext *ctx, CBMTypeRe
static void patch_method_signatures_from_ast(JavaLSPContext *ctx, CBMTypeRegistry *reg,
TSNode class_node, const char *enclosing_class_qn);
/* The next registered function of this QN after `row` (-1: the first), in
* registration order. By name when the caller listed the functions in
* ctx->func_names and none was added since; otherwise by a scan. */
static int java_func_row_next(JavaLSPContext *ctx, const CBMTypeRegistry *reg, const char *qn,
int row) {
if (ctx->func_names && ctx->func_name_count == reg->func_count) {
return row < 0 ? cbm_name_rows_first(&ctx->func_rows, ctx->func_rows_arena, ctx->func_names,
ctx->func_name_count, qn)
: cbm_name_rows_next(&ctx->func_rows, ctx->func_names, ctx->func_name_count,
qn, row);
}
for (int fi = row + 1; fi < reg->func_count; fi++) {
CBM_LSP_WORK(1);
if (reg->funcs[fi].qualified_name && strcmp(reg->funcs[fi].qualified_name, qn) == 0)
return fi;
}
return -1;
}
static void patch_one_method(JavaLSPContext *ctx, CBMTypeRegistry *reg, TSNode method_node,
const char *class_qn, bool is_constructor) {
TSNode name_node = ts_node_child_by_field_name(method_node, "name", 4);
@@ -3608,7 +3641,8 @@ static void patch_one_method(JavaLSPContext *ctx, CBMTypeRegistry *reg, TSNode m
* first QN match keeps the legacy no-signature path best-effort. */
CBMRegisteredFunc *slot = NULL;
CBMRegisteredFunc *first_qn_match = NULL;
for (int fi = 0; fi < reg->func_count; fi++) {
for (int fi = java_func_row_next(ctx, reg, method_qn, -1); fi >= 0;
fi = java_func_row_next(ctx, reg, method_qn, fi)) {
CBMRegisteredFunc *candidate = &reg->funcs[fi];
if (!candidate->qualified_name || strcmp(candidate->qualified_name, method_qn) != 0)
continue;
@@ -3709,13 +3743,7 @@ static void patch_method_signatures_from_ast(JavaLSPContext *ctx, CBMTypeRegistr
* `obj.field.method()` for arbitrary receivers, not just `this`. */
static void append_field_to_class(CBMTypeRegistry *reg, CBMArena *a, const char *class_qn,
const char *field_name, const CBMType *ftype) {
CBMRegisteredType *slot = NULL;
for (int ti = 0; ti < reg->type_count; ti++) {
if (reg->types[ti].qualified_name && strcmp(reg->types[ti].qualified_name, class_qn) == 0) {
slot = &reg->types[ti];
break;
}
}
CBMRegisteredType *slot = cbm_registry_first_own_type(reg, class_qn);
if (!slot)
return;
@@ -3813,6 +3841,8 @@ static void populate_class_fields_from_ast(JavaLSPContext *ctx, CBMTypeRegistry
extern const TSLanguage *tree_sitter_java(void);
enum { JAVA_SCRATCH_ARENA_BLOCK = 4096 };
void cbm_run_java_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
TSNode root) {
if (!arena || !result || !source)
@@ -3846,9 +3876,23 @@ void cbm_run_java_lsp(CBMArena *arena, CBMFileResult *result, const char *source
java_lsp_add_import(&ctx, imp->local_name, imp->module_path, CBM_JAVA_IMPORT_TYPE);
}
/* Register file-local defs into the registry. */
/* Register file-local defs into the registry. Its lookups run while it
* fills, so it indexes its types as they come, in a scratch arena. */
CBMArena scratch;
cbm_arena_init_lazy(&scratch, JAVA_SCRATCH_ARENA_BLOCK);
reg.tail_arena = &scratch;
register_local_func_or_type_from_file(&ctx, &reg, result);
/* Overloads share a QN, so patching a method asks for all of its registrations. */
ctx.func_rows_arena = &scratch;
ctx.func_names =
reg.func_count > 0
? (const char **)cbm_arena_alloc(&scratch, (size_t)reg.func_count * sizeof(char *))
: NULL;
ctx.func_name_count = ctx.func_names ? reg.func_count : 0;
for (int fi = 0; fi < ctx.func_name_count; fi++)
ctx.func_names[fi] = reg.funcs[fi].qualified_name;
/* Populate field metadata from the AST so eval_field_access works on
* receivers other than `this`. Also patch method signatures with
* generics-preserving types (extract_defs strips generics from
@@ -3863,9 +3907,12 @@ void cbm_run_java_lsp(CBMArena *arena, CBMFileResult *result, const char *source
patch_method_signatures_from_ast(&ctx, &reg, c, NULL);
}
}
ctx.func_names = NULL;
ctx.func_name_count = 0;
/* Walk the file. */
java_lsp_process_file(&ctx, root);
cbm_arena_destroy(&scratch);
}
/* ── Cross-file LSP ───────────────────────────────────────────────── */
@@ -4065,14 +4112,14 @@ void cbm_run_java_lsp_cross_with_registry(CBMArena *arena, CBMFileResult *result
JavaLSPContext ctx;
java_lsp_init(&ctx, arena, source, source_len, &overlay, NULL, module_qn, out);
register_local_func_or_type_from_file(&ctx, &overlay, result);
cbm_pxc_count_perfile_defs((uint64_t)result->defs.count);
/* Index the overlay only — the base is already finalized. Scratch arena so
* per-file bucket allocations do not accumulate in the pipeline-lifetime
* arena across a large repo. */
* arena across a large repo. Registration already finds its own types there. */
CBMArena idx_arena;
cbm_arena_init(&idx_arena);
overlay.tail_arena = &idx_arena;
register_local_func_or_type_from_file(&ctx, &overlay, result);
cbm_pxc_count_perfile_defs((uint64_t)result->defs.count);
cbm_registry_finalize_into(&overlay, &idx_arena);
TSTree *tree = cached_tree;
+9
View File
@@ -29,6 +29,7 @@
* enforced by lsp_resolve.h).
*/
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -72,6 +73,14 @@ typedef struct {
int *import_kinds;
int import_count;
int import_cap;
CBMNameRows import_rows; /* import_local_names by name, rows in table order */
/* While signatures are patched: the registered functions' QNs, by name
* (overloads share a QN). NULL func_names: a scan answers instead. */
const char **func_names;
int func_name_count;
CBMNameRows func_rows;
CBMArena *func_rows_arena;
/* Current enclosing context. */
const char *enclosing_method_qn; /* QN of the nearest method/ctor */
+27 -3
View File
@@ -35,6 +35,7 @@
*/
#include "kotlin_lsp.h"
#include "lsp_work.h"
#include "../helpers.h"
/* CBMHashTable — cross-registry field bucketing. Relative path: the LSP
* amalgamation compiles with -Iinternal/cbm only, not -Isrc. */
@@ -63,6 +64,7 @@ typedef struct {
#define KT_EVAL_MAX_DEPTH 32
#define KT_IMPORT_INITIAL_CAP 16
#define KT_INDEX_ARENA_BLOCK 4096
#define KT_CONF_CONSTRUCTOR 0.95f
#define KT_CONF_METHOD 0.90f
@@ -1011,6 +1013,17 @@ void kotlin_lsp_add_import(KotlinLSPContext *ctx, const char *local_name, const
ctx->import_count++;
}
/* The imports with this local name, in table order (lsp_name_rows.h): a scan per
* use made a file of thousands of imports quadratic. */
static int kt_import_first(KotlinLSPContext *ctx, const char *name) {
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_locals, ctx->import_count,
name);
}
static int kt_import_next(const KotlinLSPContext *ctx, const char *name, int row) {
return cbm_name_rows_next(&ctx->import_rows, ctx->import_locals, ctx->import_count, name, row);
}
const char *kotlin_resolve_class_name(KotlinLSPContext *ctx, const char *name) {
if (!ctx || !name || !*name) {
return NULL;
@@ -1041,7 +1054,7 @@ const char *kotlin_resolve_class_name(KotlinLSPContext *ctx, const char *name) {
}
/* Explicit imports */
for (int i = 0; i < ctx->import_count; i++) {
for (int i = kt_import_first(ctx, name); i >= 0; i = kt_import_next(ctx, name, i)) {
const char *local = ctx->import_locals[i];
if (local && strcmp(local, name) == 0 &&
(ctx->import_kinds[i] == CBM_KT_USE_TYPE ||
@@ -1103,7 +1116,7 @@ const char *kotlin_resolve_function_name(KotlinLSPContext *ctx, const char *name
* symbol itself. Prove that the candidate is a registered top-level
* function before accepting it; otherwise `import values.handler` where
* handler is a property would fabricate a callable target. */
for (int i = 0; i < ctx->import_count; i++) {
for (int i = kt_import_first(ctx, name); i >= 0; i = kt_import_next(ctx, name, i)) {
const char *local = ctx->import_locals[i];
if (local && strcmp(local, name) == 0 &&
(ctx->import_kinds[i] == CBM_KT_USE_FUNCTION ||
@@ -5189,9 +5202,12 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
}
}
/* Build per-file registry. */
/* Build per-file registry. Indexed once the file's declarations are in, as the
* cross-file pass does: a scan finds the first registration, so the index
* answers with that one too. */
CBMTypeRegistry registry;
cbm_registry_init(&registry, arena);
registry.index_first_registered = true;
/* Curated stdlib */
cbm_kotlin_stdlib_register(&registry, arena);
@@ -5229,8 +5245,15 @@ void cbm_run_kotlin_lsp(CBMArena *arena, CBMFileResult *result, const char *sour
kotlin_lsp_init(&ctx, arena, use_source, use_source_len, &registry, module_qn, module_qn,
project_name, /*rel_path=*/NULL, &result->resolved_calls);
ctx.synthetic_calls = &result->calls;
/* The registry's lookups run while it fills and after: its types are found by
* name as they come (tail_arena), and all of it once it is indexed. */
CBMArena index_arena;
cbm_arena_init_lazy(&index_arena, KT_INDEX_ARENA_BLOCK);
ctx.overlay_index_arena = &index_arena;
registry.tail_arena = &index_arena;
kotlin_lsp_process_file(&ctx, use_root);
cbm_arena_destroy(&index_arena);
/* Inject kotlin.Any universal-method nodes (toString/equals/hashCode) so the
* lsp_kt_any fallback emitted above has a target node to form a CALLS edge. */
@@ -5584,6 +5607,7 @@ void cbm_run_kotlin_lsp_cross(CBMArena *arena, const char *source, int source_le
kotlin_lsp_init(&ctx, arena, source, source_len, &overlay, module_qn ? module_qn : "",
module_qn ? module_qn : "", project_name, /*rel_path=*/NULL, out);
ctx.overlay_index_arena = &idx_arena;
overlay.tail_arena = &idx_arena;
/* Unique short type name -> registered QN, for receiver annotations that
* name a type defined in another file without an import. Two types
+2
View File
@@ -48,6 +48,7 @@
#ifndef CBM_LSP_KOTLIN_LSP_H
#define CBM_LSP_KOTLIN_LSP_H
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -97,6 +98,7 @@ typedef struct KotlinLSPContext {
CBMKotlinUseKind *import_kinds;
int import_count;
int import_cap;
CBMNameRows import_rows; /* import_locals by name, rows in table order */
/* Current declaration context. */
const char *enclosing_func_qn; /* function we are resolving inside */
+159
View File
@@ -0,0 +1,159 @@
/*
* lsp_name_rows.h — rows of a per-file table, found by name.
*
* A file's import table is searched once per identifier, and a scan of it makes that
* imports x identifiers: a generated module with thousands of imports took seconds in
* each pass. These chains answer with the same rows in the same order.
*
* cbm_name_rows_first/next index a table of names on the first question, and only a
* table long enough for that to matter; a short one, which is nearly all of them, is
* still scanned and allocates nothing. Rows appended later join on the next question.
* A caller that shrinks or rewrites its table zeroes the CBMNameRows.
*
* cbm_name_rows_add/lookup are the same chains built by hand, for a key that also has a
* scope. Memory comes from the caller's arena.
*/
#ifndef CBM_LSP_NAME_ROWS_H
#define CBM_LSP_NAME_ROWS_H
#include "../arena.h"
#include "lsp_work.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
enum { CBM_NAME_ROWS_MIN = 16, CBM_NAME_ROWS_FIRST_SLOTS = 32 };
typedef struct {
uint64_t hash;
const void *scope;
const char *name;
int first, last; /* row + 1; 0 marks an unused slot */
} CBMNameRowSlot;
typedef struct {
CBMNameRowSlot *slots;
uint32_t capacity, used;
int *next; /* row -> the next row under its key, or -1 */
int next_capacity;
int synced; /* table rows already chained */
bool failed;
bool chained; /* cbm_name_rows_add callers: every row is in */
} CBMNameRows;
static inline uint64_t cbm_name_rows_hash(const void *scope, const char *name) {
uint64_t hash = ((uint64_t)(uintptr_t)scope >> 3) * UINT64_C(0x9E3779B97F4A7C15);
for (const char *at = name; at && *at; at++)
hash = (hash ^ (unsigned char)*at) * UINT64_C(0x100000001B3);
return hash ^ (hash >> 32);
}
static inline CBMNameRowSlot *cbm_name_rows_slot(const CBMNameRows *rows, uint64_t hash,
const void *scope, const char *name) {
uint32_t mask = rows->capacity - 1;
for (uint32_t at = (uint32_t)hash & mask;; at = (at + 1) & mask) {
CBMNameRowSlot *slot = &rows->slots[at];
if (!slot->first ||
(slot->hash == hash && slot->scope == scope &&
(slot->name == name || (slot->name && name && strcmp(slot->name, name) == 0)))) {
return slot;
}
}
}
/* Rows go in ascending order. false when memory ran out. */
static inline bool cbm_name_rows_add(CBMArena *arena, CBMNameRows *rows, const void *scope,
const char *name, int row) {
if (!arena)
return false;
if (row >= rows->next_capacity) {
int capacity = rows->next_capacity ? rows->next_capacity * 2 : CBM_NAME_ROWS_FIRST_SLOTS;
while (capacity <= row)
capacity *= 2;
int *next = (int *)cbm_arena_alloc(arena, (size_t)capacity * sizeof(*next));
if (!next)
return false;
if (rows->next_capacity)
memcpy(next, rows->next, (size_t)rows->next_capacity * sizeof(*next));
rows->next = next;
rows->next_capacity = capacity;
}
if (rows->used * 2 >= rows->capacity) {
uint32_t capacity = rows->capacity ? rows->capacity * 2 : CBM_NAME_ROWS_FIRST_SLOTS;
CBMNameRowSlot *slots =
(CBMNameRowSlot *)cbm_arena_alloc(arena, (size_t)capacity * sizeof(*slots));
if (!slots)
return false;
memset(slots, 0, (size_t)capacity * sizeof(*slots));
CBMNameRows grown = *rows;
grown.slots = slots;
grown.capacity = capacity;
for (uint32_t i = 0; i < rows->capacity; i++) {
const CBMNameRowSlot *old = &rows->slots[i];
if (old->first)
*cbm_name_rows_slot(&grown, old->hash, old->scope, old->name) = *old;
}
*rows = grown;
}
uint64_t hash = cbm_name_rows_hash(scope, name);
CBMNameRowSlot *slot = cbm_name_rows_slot(rows, hash, scope, name);
rows->next[row] = -1;
if (slot->first) {
rows->next[slot->last - 1] = row;
} else {
*slot = (CBMNameRowSlot){.hash = hash, .scope = scope, .name = name, .first = row + 1};
rows->used++;
}
slot->last = row + 1;
return true;
}
static inline int cbm_name_rows_lookup(const CBMNameRows *rows, const void *scope,
const char *name) {
if (!rows->capacity)
return -1;
return cbm_name_rows_slot(rows, cbm_name_rows_hash(scope, name), scope, name)->first - 1;
}
static inline bool cbm_name_rows_chained(const CBMNameRows *rows, int count) {
return !rows->failed && count >= CBM_NAME_ROWS_MIN && rows->synced == count;
}
static inline int cbm_name_rows_scan(const char *const *names, int from, int count,
const char *name) {
for (int i = from; i < count; i++) {
CBM_LSP_WORK(1);
if (names[i] && strcmp(names[i], name) == 0)
return i;
}
return -1;
}
/* The first row of names[0..count) equal to name, or -1. */
static inline int cbm_name_rows_first(CBMNameRows *rows, CBMArena *arena, const char *const *names,
int count, const char *name) {
if (count >= CBM_NAME_ROWS_MIN && !rows->failed) {
for (; rows->synced < count; rows->synced++) {
int row = rows->synced;
if (names[row] && !cbm_name_rows_add(arena, rows, NULL, names[row], row)) {
rows->failed = true;
break;
}
}
}
if (!cbm_name_rows_chained(rows, count))
return cbm_name_rows_scan(names, 0, count, name);
CBM_LSP_WORK(1);
return cbm_name_rows_lookup(rows, NULL, name);
}
/* The next such row after row, or -1. */
static inline int cbm_name_rows_next(const CBMNameRows *rows, const char *const *names, int count,
const char *name, int row) {
if (!cbm_name_rows_chained(rows, count))
return cbm_name_rows_scan(names, row + 1, count, name);
CBM_LSP_WORK(1);
return rows->next[row];
}
#endif /* CBM_LSP_NAME_ROWS_H */
+57 -6
View File
@@ -184,8 +184,24 @@ void php_lsp_add_use(PHPLSPContext *ctx, const char *local_name, const char *tar
ctx->use_count++;
}
/* The registry's free functions of this short name, in registration order, through
* its short-name index and the rows behind it. An indexed registry whose short-name
* index could not be built is walked in full, as the scan this replaces was (the
* caller re-checks each row). */
static void php_free_funcs_by_name(const CBMTypeRegistry *reg, const char *name,
CBMFreeFuncIter *it) {
cbm_registry_free_funcs_by_short_name(reg, name, it);
if (it->chain_idx < 0 && !reg->ffunc_short_buckets)
it->tail_i = 0;
}
/* The uses with this local name, in table order (lsp_name_rows.h): a scan per
* name made a file of thousands of `use` declarations quadratic. */
static const char *find_use(PHPLSPContext *ctx, const char *local_name, int kind) {
for (int i = 0; i < ctx->use_count; i++) {
for (int i = cbm_name_rows_first(&ctx->use_rows, ctx->arena, ctx->use_local_names,
ctx->use_count, local_name);
i >= 0; i = cbm_name_rows_next(&ctx->use_rows, ctx->use_local_names, ctx->use_count,
local_name, i)) {
if ((int)ctx->use_kinds[i] != kind)
continue;
if (strcmp(ctx->use_local_names[i], local_name) == 0)
@@ -325,7 +341,12 @@ static const CBMRegisteredType *lookup_type_with_project(PHPLSPContext *ctx, con
const CBMRegisteredType *best = NULL;
int best_score = -1;
for (int i = 0; ctx->registry && i < ctx->registry->type_count; i++) {
/* The registry's own types of this short name, in registration order. */
CBMTypeShortIter named = {0};
if (ctx->registry)
cbm_registry_types_by_short_name(ctx->registry, short_name, &named);
for (int i = -1; ctx->registry && (i = cbm_type_short_iter_next(&named)) >= 0;) {
CBM_LSP_WORK(1);
const CBMRegisteredType *cand = &ctx->registry->types[i];
if (!cand->short_name || strcmp(cand->short_name, short_name) != 0)
continue;
@@ -1202,7 +1223,10 @@ static const CBMType *eval_function_call_type(PHPLSPContext *ctx, TSNode call_no
}
if (!f && ctx->registry) {
/* Walk the symbol table by short_name as a last resort. */
for (int i = 0; i < ctx->registry->func_count; i++) {
CBMFreeFuncIter named;
php_free_funcs_by_name(ctx->registry, name, &named);
for (int i = -1; (i = cbm_free_func_iter_next(&named)) >= 0;) {
CBM_LSP_WORK(1);
const CBMRegisteredFunc *cand = &ctx->registry->funcs[i];
if (cand->receiver_type)
continue;
@@ -1696,7 +1720,11 @@ static void resolve_function_call(PHPLSPContext *ctx, TSNode call, CBMResolvedKi
* file's project (i.e. with the longest module-prefix overlap). */
const CBMRegisteredFunc *best = NULL;
int best_score = -1;
for (int i = 0; ctx->registry && i < ctx->registry->func_count; i++) {
CBMFreeFuncIter named = {0};
if (ctx->registry)
php_free_funcs_by_name(ctx->registry, name, &named);
for (int i = -1; ctx->registry && (i = cbm_free_func_iter_next(&named)) >= 0;) {
CBM_LSP_WORK(1);
const CBMRegisteredFunc *cand = &ctx->registry->funcs[i];
if (cand->receiver_type)
continue;
@@ -4034,6 +4062,22 @@ static void process_trait_use(PHPLSPContext *ctx, CBMTypeRegistry *reg, const ch
}
}
/* Where a scan for the first function of reg itself with this receiver and name
* can start: the lowest row the method iterator yields (reg's own rows only: its
* index chain, in whichever order that was built, then the rows behind it; every
* row when unindexed). reg->func_count when there is none. */
static int php_first_method_row(const CBMTypeRegistry *reg, const char *receiver,
const char *name) {
CBMMethodIter it;
cbm_registry_methods(reg, receiver, name, &it);
int lowest = reg->func_count;
for (int row = cbm_method_iter_next(&it); row >= 0; row = cbm_method_iter_next(&it)) {
if (row < lowest)
lowest = row;
}
return lowest;
}
static void process_class_for_fields(PHPLSPContext *ctx, CBMTypeRegistry *reg, TSNode class_node,
php_class_field_table_t *tab) {
const char *cqn = class_qn_for_node(ctx, class_node);
@@ -4215,7 +4259,10 @@ static void process_class_for_fields(PHPLSPContext *ctx, CBMTypeRegistry *reg, T
ctx->enclosing_class_qn = saved_class;
ctx->enclosing_parent_qn = saved_parent;
if (parsed && parsed->kind != CBM_TYPE_UNKNOWN) {
for (int fi = 0; fi < reg->func_count; fi++) {
/* The loop patches the first registration of (cqn, mn): its
* method index answers in registration order; start there. */
CBM_LSP_WORK(1);
for (int fi = php_first_method_row(reg, cqn, mn); fi < reg->func_count; fi++) {
CBMRegisteredFunc *rf = &reg->funcs[fi];
if (!rf->receiver_type || !rf->short_name)
continue;
@@ -4292,7 +4339,9 @@ static void process_class_for_fields(PHPLSPContext *ctx, CBMTypeRegistry *reg, T
if (ret_t && ret_t->kind != CBM_TYPE_UNKNOWN) {
/* Find the registered method by receiver_type=cqn,
* short_name=mn and patch its signature. */
for (int fi = 0; fi < reg->func_count; fi++) {
CBM_LSP_WORK(1);
for (int fi = php_first_method_row(reg, cqn, mn); fi < reg->func_count;
fi++) {
CBMRegisteredFunc *rf = &reg->funcs[fi];
if (!rf->receiver_type || !rf->short_name)
continue;
@@ -4540,6 +4589,7 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
/* Reset namespace/use state — process_file will re-collect. */
ctx.current_namespace_qn = "";
ctx.use_count = 0;
ctx.use_rows = (CBMNameRows){0};
}
/* Trait flattening registered methods after the index was built; lookups
@@ -4875,6 +4925,7 @@ void cbm_run_php_lsp_cross_with_bindings(CBMArena *arena, const char *source, in
ctx.current_namespace_qn = "";
/* Reset to caller-supplied uses only — process_file re-adds AST uses. */
ctx.use_count = supplied_use_count;
ctx.use_rows = (CBMNameRows){0};
}
php_lsp_process_file(&ctx, root);
+2
View File
@@ -1,6 +1,7 @@
#ifndef CBM_LSP_PHP_LSP_H
#define CBM_LSP_PHP_LSP_H
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -36,6 +37,7 @@ typedef struct {
enum { CBM_PHP_USE_CLASS = 0, CBM_PHP_USE_FUNCTION, CBM_PHP_USE_CONST } *use_kinds;
int use_count;
int use_cap;
CBMNameRows use_rows; /* use_local_names by name; zeroed whenever use_count is reset */
/* Cross-file mode: dotted targets of the file's class `use` declarations
* that the pipeline's import map left unresolved (vendor / unmapped
+253 -77
View File
@@ -394,37 +394,55 @@ void py_lsp_init(PyLSPContext *ctx, CBMArena *arena, const char *source, int sou
py_lsp_init_for_stage(ctx, arena, source, source_len, registry, module_qn, out, "context");
}
enum { PY_IMPORT_FIRST_CAPACITY = 16, PY_IMPORT_JOIN_BLOCK = 4096 };
void py_lsp_add_import(PyLSPContext *ctx, const char *local_name, const char *module_qn) {
if (!ctx || !local_name || !module_qn)
return;
/* The arrays double when full: copied on every import, a module of thousands
* of imports was quadratic. Each name and QN array keeps a slot for its NULL. */
int new_count = ctx->import_count + 1;
const char **names =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)(new_count + 1) * sizeof(const char *));
const char **qns =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)(new_count + 1) * sizeof(const char *));
unsigned char *kinds =
(unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(unsigned char));
if (!names || !qns || !kinds)
return;
for (int i = 0; i < ctx->import_count; i++) {
names[i] = ctx->import_local_names[i];
qns[i] = ctx->import_module_qns[i];
kinds[i] = ctx->import_kinds ? ctx->import_kinds[i] : 0;
if (new_count > ctx->import_capacity) {
int capacity = ctx->import_capacity ? ctx->import_capacity * 2 : PY_IMPORT_FIRST_CAPACITY;
const char **names = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(capacity + 1) *
sizeof(const char *));
const char **qns = (const char **)cbm_arena_alloc(ctx->arena, (size_t)(capacity + 1) *
sizeof(const char *));
unsigned char *kinds =
(unsigned char *)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(unsigned char));
if (!names || !qns || !kinds)
return;
for (int i = 0; i < ctx->import_count; i++) {
CBM_LSP_WORK(1);
names[i] = ctx->import_local_names[i];
qns[i] = ctx->import_module_qns[i];
kinds[i] = ctx->import_kinds ? ctx->import_kinds[i] : 0;
}
ctx->import_local_names = names;
ctx->import_module_qns = qns;
ctx->import_kinds = kinds;
ctx->import_capacity = capacity;
}
names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
kinds[ctx->import_count] = 0;
names[new_count] = NULL;
qns[new_count] = NULL;
ctx->import_local_names = names;
ctx->import_module_qns = qns;
ctx->import_kinds = kinds;
ctx->import_local_names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
ctx->import_module_qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
ctx->import_kinds[ctx->import_count] = 0;
ctx->import_local_names[new_count] = NULL;
ctx->import_module_qns[new_count] = NULL;
ctx->import_count = new_count;
}
/* The import rows with this local name, in table order (lsp_name_rows.h). */
static int py_import_row_first(PyLSPContext *ctx, const char *local) {
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
ctx->import_count, local);
}
static int py_import_row_next(const PyLSPContext *ctx, const char *local, int row) {
return cbm_name_rows_next(&ctx->import_rows, ctx->import_local_names, ctx->import_count, local,
row);
}
/* Determine whether this import is an `import X` style binding (binds the
* module itself) or a `from X import Y` style binding (binds Y, an
* attribute of module X). Heuristic: if module_qn ends in `.<local_name>`
@@ -575,64 +593,193 @@ static const char *py_canonical_from_import_qn(PyLSPContext *ctx, TSNode stateme
* binding for the local name. Python permits later imports to rebind the same
* local; selecting the first would turn conflicting runtime identities into
* false semantic proof. */
static void py_import_match_item(PyLSPContext *ctx, TSNode stmt, bool from_import, TSNode item,
const char *local, const char *qn, PyImportSyntaxMatch *match) {
const char *kind = ts_node_type(item);
if (from_import) {
if (strcmp(kind, "aliased_import") == 0) {
TSNode name = ts_node_child_by_field_name(item, "name", 4);
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
if (py_import_node_text_equals(ctx, alias, local)) {
const char *canonical = py_canonical_from_import_qn(ctx, stmt, name, qn);
py_import_syntax_match_add(match,
canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
canonical ? canonical : qn);
}
} else if ((strcmp(kind, "identifier") == 0 || strcmp(kind, "dotted_name") == 0) &&
py_import_node_text_equals(ctx, item, local)) {
const char *canonical = py_canonical_from_import_qn(ctx, stmt, item, qn);
py_import_syntax_match_add(match, canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
canonical ? canonical : qn);
}
return;
}
if (strcmp(kind, "aliased_import") == 0) {
TSNode name = ts_node_child_by_field_name(item, "name", 4);
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
if (py_import_node_text_equals(ctx, alias, local)) {
char *imported_path = py_import_node_text_dup(ctx, name);
PyDirectImportKind matched_kind =
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
? PY_DIRECT_IMPORT_ALIASED
: PY_IMPORT_UNCLASSIFIED;
py_import_syntax_match_add(match, matched_kind, qn);
}
} else if ((strcmp(kind, "dotted_name") == 0 || strcmp(kind, "identifier") == 0) &&
py_import_node_root_equals(ctx, item, local)) {
char *imported_path = py_import_node_text_dup(ctx, item);
PyDirectImportKind matched_kind =
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
? PY_DIRECT_IMPORT_UNALIASED
: PY_IMPORT_UNCLASSIFIED;
py_import_syntax_match_add(match, matched_kind, qn);
}
}
static void py_import_match_statement(PyLSPContext *ctx, TSNode stmt, const char *local,
const char *qn, PyImportSyntaxMatch *match) {
if (!ctx || ts_node_is_null(stmt) || !local || !qn || !match)
return;
const char *stmt_kind = ts_node_type(stmt);
if (strcmp(stmt_kind, "import_from_statement") == 0) {
TSNode module = py_from_import_module_node(stmt);
uint32_t import_count = ts_node_named_child_count(stmt);
for (uint32_t j = 0; j < import_count; j++) {
TSNode item = ts_node_named_child(stmt, j);
if (!ts_node_is_null(module) && ts_node_eq(item, module))
continue;
const char *kind = ts_node_type(item);
if (strcmp(kind, "aliased_import") == 0) {
TSNode name = ts_node_child_by_field_name(item, "name", 4);
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
if (py_import_node_text_equals(ctx, alias, local)) {
const char *canonical = py_canonical_from_import_qn(ctx, stmt, name, qn);
py_import_syntax_match_add(match,
canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
canonical ? canonical : qn);
}
} else if ((strcmp(kind, "identifier") == 0 || strcmp(kind, "dotted_name") == 0) &&
py_import_node_text_equals(ctx, item, local)) {
const char *canonical = py_canonical_from_import_qn(ctx, stmt, item, qn);
py_import_syntax_match_add(match,
canonical ? PY_FROM_IMPORT : PY_IMPORT_UNCLASSIFIED,
canonical ? canonical : qn);
}
}
return;
}
if (strcmp(stmt_kind, "import_statement") != 0)
bool from_import = strcmp(stmt_kind, "import_from_statement") == 0;
if (!from_import && strcmp(stmt_kind, "import_statement") != 0)
return;
TSNode module = from_import ? py_from_import_module_node(stmt) : (TSNode){0};
uint32_t import_count = ts_node_named_child_count(stmt);
for (uint32_t j = 0; j < import_count; j++) {
CBM_LSP_WORK(1);
TSNode item = ts_node_named_child(stmt, j);
const char *kind = ts_node_type(item);
if (strcmp(kind, "aliased_import") == 0) {
TSNode name = ts_node_child_by_field_name(item, "name", 4);
TSNode alias = ts_node_child_by_field_name(item, "alias", 5);
if (py_import_node_text_equals(ctx, alias, local)) {
char *imported_path = py_import_node_text_dup(ctx, name);
PyDirectImportKind matched_kind =
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
? PY_DIRECT_IMPORT_ALIASED
: PY_IMPORT_UNCLASSIFIED;
py_import_syntax_match_add(match, matched_kind, qn);
}
} else if ((strcmp(kind, "dotted_name") == 0 || strcmp(kind, "identifier") == 0) &&
py_import_node_root_equals(ctx, item, local)) {
char *imported_path = py_import_node_text_dup(ctx, item);
PyDirectImportKind matched_kind =
imported_path && py_qn_has_boundary_suffix(qn, imported_path)
? PY_DIRECT_IMPORT_UNALIASED
: PY_IMPORT_UNCLASSIFIED;
py_import_syntax_match_add(match, matched_kind, qn);
if (from_import && !ts_node_is_null(module) && ts_node_eq(item, module))
continue;
py_import_match_item(ctx, stmt, from_import, item, local, qn, match);
}
}
/* ── The import statements' items, by the names they bind ─────────────
* Each import asked every import statement of the module, and each item of a
* statement asked every import, which was imports x items. The items are
* filed once per walk under exactly the names py_import_match_item matches
* them on, in source order, so only those items are matched. Running out of
* memory sets `failed`, and every question is answered by the scan again. */
typedef struct {
TSNode statement;
TSNode item; /* null on a statement's own row */
bool from_import;
} PyImportItem;
struct PyImportJoin {
CBMArena *arena;
CBMNameRows by_name; /* (statement, name) and (NULL, name) -> the items binding name */
PyImportItem *items;
int item_count;
int item_capacity;
bool failed;
};
/* Files one name an item binds under its statement and, for a statement of
* the module's own list, under NULL. A NULL name files the statement's own row. */
static bool py_import_join_file(struct PyImportJoin *join, PyImportItem item, const char *name,
bool module_list) {
for (int under_statement = module_list ? 0 : 1; under_statement < 2; under_statement++) {
if (join->item_count == join->item_capacity) {
int capacity = join->item_capacity ? join->item_capacity * 2 : PY_IMPORT_FIRST_CAPACITY;
PyImportItem *items =
(PyImportItem *)cbm_arena_alloc(join->arena, (size_t)capacity * sizeof(*items));
if (!items)
return false;
if (join->item_count)
memcpy(items, join->items, (size_t)join->item_count * sizeof(*items));
join->items = items;
join->item_capacity = capacity;
}
int row = join->item_count;
join->items[row] = item;
const void *scope = under_statement ? item.statement.id : NULL;
if (!cbm_name_rows_add(join->arena, &join->by_name, scope, name, row))
return false;
join->item_count++;
}
return true;
}
/* The name an item binds, as py_import_match_item reads it; *name stays NULL
* when the item binds none. False only when memory ran out. */
static bool py_import_join_item_name(PyLSPContext *ctx, struct PyImportJoin *join, TSNode node,
char **name) {
*name = NULL;
if (ts_node_is_null(node))
return true;
uint32_t start = ts_node_start_byte(node);
uint32_t end = ts_node_end_byte(node);
if (end < start || (int)end > ctx->source_len)
return true;
*name = cbm_arena_strndup(join->arena, ctx->source + start, (size_t)(end - start));
return *name != NULL;
}
static bool py_import_join_items(PyLSPContext *ctx, struct PyImportJoin *join, TSNode stmt,
bool from_import, bool module_list) {
TSNode module = from_import ? py_from_import_module_node(stmt) : (TSNode){0};
PyKids kids = py_kids(join->arena, stmt);
for (uint32_t i = 0; i < kids.n; i++) {
TSNode item = py_kid(&kids, i);
if (from_import && !ts_node_is_null(module) && ts_node_eq(item, module))
continue;
const char *kind = ts_node_type(item);
bool aliased = strcmp(kind, "aliased_import") == 0;
if (!aliased && strcmp(kind, "identifier") != 0 && strcmp(kind, "dotted_name") != 0)
continue;
char *name = NULL;
TSNode named = aliased ? ts_node_child_by_field_name(item, "alias", 5) : item;
if (!py_import_join_item_name(ctx, join, named, &name))
return false;
if (!name)
continue;
PyImportItem row = {.statement = stmt, .item = item, .from_import = from_import};
/* `import a.b.c` binds a, and answers for a, a.b and a.b.c. */
if (!aliased && !from_import) {
for (char *dot = strchr(name, '.'); dot; dot = strchr(dot + 1, '.')) {
char *prefix = cbm_arena_strndup(join->arena, name, (size_t)(dot - name));
if (!prefix || !py_import_join_file(join, row, prefix, module_list))
return false;
}
}
if (!py_import_join_file(join, row, name, module_list))
return false;
}
PyImportItem own = {.statement = stmt, .from_import = from_import};
return py_import_join_file(join, own, NULL, false);
}
/* True when the join can answer for stmt's items: filed now or by an earlier question. */
static bool py_import_join_statement(PyLSPContext *ctx, TSNode stmt, bool module_list) {
struct PyImportJoin *join = ctx->import_join;
if (!join || join->failed)
return false;
if (cbm_name_rows_lookup(&join->by_name, stmt.id, NULL) >= 0)
return true;
const char *stmt_kind = ts_node_type(stmt);
bool from_import = strcmp(stmt_kind, "import_from_statement") == 0;
if (!from_import && strcmp(stmt_kind, "import_statement") != 0)
return false;
if (!py_import_join_items(ctx, join, stmt, from_import, module_list)) {
join->failed = true;
return false;
}
return true;
}
/* The items filed under (scope, local), in source order. The answer depends on
* the first match and on whether there is a second one, so the second ends it. */
static void py_import_join_match(PyLSPContext *ctx, const void *scope, const char *local,
const char *qn, PyImportSyntaxMatch *match) {
const struct PyImportJoin *join = ctx->import_join;
CBM_LSP_WORK(1);
for (int row = cbm_name_rows_lookup(&join->by_name, scope, local); row >= 0 && match->count < 2;
row = join->by_name.next[row]) {
CBM_LSP_WORK(1);
const PyImportItem *item = &join->items[row];
py_import_match_item(ctx, item->statement, item->from_import, item->item, local, qn, match);
}
}
@@ -654,14 +801,18 @@ static PyDirectImportKind py_import_kind_from_statement(PyLSPContext *ctx, TSNod
if (!ctx || !local || !qn || !qn_io || ts_node_is_null(stmt))
return PY_DIRECT_IMPORT_UNKNOWN;
PyImportSyntaxMatch match = {0};
py_import_match_statement(ctx, stmt, local, qn, &match);
if (py_import_join_statement(ctx, stmt, false))
py_import_join_match(ctx, stmt.id, local, qn, &match);
else
py_import_match_statement(ctx, stmt, local, qn, &match);
return py_import_match_result(&match, qn_io);
}
/* stmts: the root's import statements, collected once per root by the caller
* (#1527) -- only they can match, and rescanning every top-level statement
* once per import made classification O(imports x module size). */
static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, bool have_root,
* once per import made classification O(imports x module size). `joined`:
* their items are filed by name, so only the items binding `local` are asked. */
static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, bool have_root, bool joined,
const TSNode *stmts, uint32_t stmt_count,
const char *local, const char **qn_io) {
const char *qn = qn_io ? *qn_io : NULL;
@@ -669,6 +820,10 @@ static PyDirectImportKind py_import_kind_from_ast(PyLSPContext *ctx, bool have_r
return PY_DIRECT_IMPORT_UNKNOWN;
PyImportSyntaxMatch match = {0};
if (joined) {
py_import_join_match(ctx, NULL, local, qn, &match);
return py_import_match_result(&match, qn_io);
}
for (uint32_t i = 0; i < stmt_count; i++) {
py_import_match_statement(ctx, stmts[i], local, qn, &match);
}
@@ -736,13 +891,18 @@ static void py_classify_imports_for_root(PyLSPContext *ctx, TSNode root) {
stmts[stmt_count++] = c;
}
}
/* A statement the join could not file leaves every import to the scan. */
bool joined =
have_root && ctx->import_count > 0 && ctx->import_join && !ctx->import_join->failed;
for (uint32_t i = 0; joined && i < stmt_count; i++)
joined = py_import_join_statement(ctx, stmts[i], true);
for (int i = 0; i < ctx->import_count; i++) {
const char *local = ctx->import_local_names[i];
const char *qn = ctx->import_module_qns[i];
if (!local || !qn)
continue;
PyDirectImportKind direct_kind =
py_import_kind_from_ast(ctx, have_root, stmts, stmt_count, local, &qn);
py_import_kind_from_ast(ctx, have_root, joined, stmts, stmt_count, local, &qn);
ctx->import_module_qns[i] = qn;
if (ctx->import_kinds)
ctx->import_kinds[i] = (unsigned char)direct_kind;
@@ -1101,7 +1261,8 @@ static const char *py_exact_imported_reference_candidate(PyLSPContext *ctx,
if (!ctx || !local_name)
return NULL;
const char *candidate = NULL;
for (int i = 0; i < ctx->import_count; i++) {
for (int i = py_import_row_first(ctx, local_name); i >= 0;
i = py_import_row_next(ctx, local_name, i)) {
const char *local = ctx->import_local_names[i];
const char *qn = ctx->import_module_qns[i];
if (!local || !qn || strcmp(local, local_name) != 0 ||
@@ -3234,7 +3395,8 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
* reason-based alias join by import metadata so an unrelated
* local NAMED value cannot masquerade as an imported callable. */
if (qn && qn_short && strcmp(qn_short, fname) != 0) {
for (int i = 0; i < ctx->import_count; i++) {
for (int i = py_import_row_first(ctx, fname); i >= 0;
i = py_import_row_next(ctx, fname, i)) {
const char *local = ctx->import_local_names[i];
const char *import_qn = ctx->import_module_qns[i];
if (local && import_qn && strcmp(local, fname) == 0 &&
@@ -5687,7 +5849,7 @@ static void py_replay_import_local(PyLSPContext *ctx, TSNode stmt, const char *l
PyDirectImportKind chosen_kind = PY_IMPORT_UNCLASSIFIED;
const char *chosen_qn = NULL;
bool conflicting_target = false;
for (int i = 0; i < ctx->import_count; i++) {
for (int i = py_import_row_first(ctx, local); i >= 0; i = py_import_row_next(ctx, local, i)) {
if ((consumed && consumed[i]) || !ctx->import_local_names[i] ||
!ctx->import_module_qns[i] || strcmp(ctx->import_local_names[i], local) != 0) {
continue;
@@ -5772,9 +5934,23 @@ static bool py_expression_is_annotation_only_assignment(TSNode statement) {
return !ts_node_is_null(annotation) && ts_node_is_null(value);
}
static void py_lsp_process_tree(PyLSPContext *ctx, TSNode root);
void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {
if (!ctx || ctx->eval_failure || ts_node_is_null(root))
return;
/* The import join holds this tree's nodes: it lives for this walk only. */
CBMArena join_arena;
cbm_arena_init_lazy(&join_arena, PY_IMPORT_JOIN_BLOCK);
struct PyImportJoin join = {.arena = &join_arena};
struct PyImportJoin *outer = ctx->import_join;
ctx->import_join = &join;
py_lsp_process_tree(ctx, root);
ctx->import_join = outer;
cbm_arena_destroy(&join_arena);
}
static void py_lsp_process_tree(PyLSPContext *ctx, TSNode root) {
int resolved_start = ctx->resolved_calls ? ctx->resolved_calls->count : 0;
int synthetic_start = ctx->syn_calls ? ctx->syn_calls->count : 0;
/* Only invocation dedup mutates prior rows, within this fixed tail.
+5
View File
@@ -1,6 +1,7 @@
#ifndef CBM_LSP_PY_LSP_H
#define CBM_LSP_PY_LSP_H
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -71,6 +72,10 @@ typedef struct {
const char **import_module_qns;
unsigned char *import_kinds; // internal PyDirectImportKind, classified once from the AST
int import_count;
int import_capacity; // rows the three arrays hold (py_lsp_add_import)
CBMNameRows import_rows; // import_local_names by name, rows in table order
// During one file's walk: the items of its import statements by the names they bind.
struct PyImportJoin *import_join;
// Current function/class context for resolving `self`/`cls` and emitting
// caller QNs.
+90 -12
View File
@@ -26,6 +26,7 @@
*/
#include "rust_lsp.h"
#include "lsp_work.h"
#include "rust_cargo.h"
#include "../helpers.h"
#include <ctype.h>
@@ -328,12 +329,11 @@ static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name)
if (!ctx || !local_name) {
return NULL;
}
for (int i = 0; i < ctx->use_count; i++) {
if (strcmp(ctx->use_local_names[i], local_name) == 0) {
return ctx->use_module_paths[i];
}
}
return NULL;
/* The first use of this local name (lsp_name_rows.h): a scan per use made a
* file of thousands of `use` items quadratic. */
int i = cbm_name_rows_first(&ctx->use_rows, ctx->arena, ctx->use_local_names, ctx->use_count,
local_name);
return i >= 0 ? ctx->use_module_paths[i] : NULL;
}
/* The Rust prelude is auto-imported into every module. We map each name
@@ -2648,6 +2648,7 @@ static const CBMRegisteredFunc *rust_lookup_method_in_trait(RustLSPContext *ctx,
if (reg->fallback) {
RustLSPContext fallback_ctx = *ctx;
fallback_ctx.registry = reg->fallback;
fallback_ctx.use_rows = (CBMNameRows){0}; /* the copy must not grow ctx's chains */
return rust_lookup_method_in_trait(&fallback_ctx, trait_qn, method_name, allow_abstract);
}
return NULL;
@@ -5554,6 +5555,51 @@ static void rust_collect_uses(RustLSPContext *ctx, TSNode root) {
}
}
/* The first function row of each key, for a pass that patches registered
* functions while the function list does not change: the pass starts its old
* loop at that row instead of the top. A row the memo cannot take (no memory)
* sets `failed`, and every loop starts at the top again. Scratch memory. */
enum { RUST_FIRST_ROWS_BLOCK = 4096 };
typedef struct {
CBMIdxMemo first;
CBMArena arena;
bool failed;
} RustFirstRows;
static void rust_first_rows_init(RustFirstRows *rows) {
memset(rows, 0, sizeof(*rows));
cbm_arena_init_lazy(&rows->arena, RUST_FIRST_ROWS_BLOCK);
}
static void rust_first_rows_destroy(RustFirstRows *rows) {
cbm_arena_destroy(&rows->arena);
}
static void rust_first_rows_put(RustFirstRows *rows, const char *key, int row) {
if (rows->failed)
return;
CBM_LSP_WORK(1);
if (key)
cbm_idxmemo_put_if_absent(&rows->first, &rows->arena, key, row);
if (!key || cbm_idxmemo_get(&rows->first, key) < 0)
rows->failed = true;
}
/* Where a patch loop over `count` functions starts for this key. */
static int rust_first_rows_start(const RustFirstRows *rows, const char *key, int count) {
CBM_LSP_WORK(1);
if (rows->failed || !key)
return 0;
int row = cbm_idxmemo_get(&rows->first, key);
return row >= 0 ? row : count;
}
/* A method's key: receiver and name, joined by a byte no name holds. */
static const char *rust_method_key(RustFirstRows *rows, const char *receiver, const char *name) {
return cbm_arena_sprintf(&rows->arena, "%s\x1f%s", receiver, name);
}
/* Build the registry from the per-file `result->defs`, `result->impl_traits`,
* and a Rust prelude seed. */
void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFileResult *result,
@@ -5719,7 +5765,11 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
fld_count++;
}
if (fld_count > 0) {
for (int ti = 0; ti < reg->type_count; ti++) {
/* The type's first registration (type_idx); the loop starts there. */
CBM_LSP_WORK(1);
int first = cbm_idxmemo_get(&type_idx, type_qn);
for (int ti = first >= 0 ? first : reg->type_count; ti < reg->type_count;
ti++) {
if (reg->types[ti].qualified_name &&
strcmp(reg->types[ti].qualified_name, type_qn) == 0) {
const char **names = (const char **)cbm_arena_alloc(
@@ -5751,8 +5801,11 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
continue;
const char *trait_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, tn);
/* Mark as interface and collect method names. */
for (int ti = 0; ti < reg->type_count; ti++) {
/* Mark as interface and collect method names: the trait's first
* registration (type_idx); the loop starts there. */
CBM_LSP_WORK(1);
int first = cbm_idxmemo_get(&type_idx, trait_qn);
for (int ti = first >= 0 ? first : reg->type_count; ti < reg->type_count; ti++) {
if (!reg->types[ti].qualified_name)
continue;
if (strcmp(reg->types[ti].qualified_name, trait_qn) == 0) {
@@ -5804,6 +5857,13 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
tmp.registry = reg;
tmp.module_qn = module_qn;
/* The first free function of each QN; the patch loop starts there. */
RustFirstRows free_rows;
rust_first_rows_init(&free_rows);
for (int k = 0; k < reg->func_count; k++) {
if (reg->funcs[k].qualified_name && !reg->funcs[k].receiver_type)
rust_first_rows_put(&free_rows, reg->funcs[k].qualified_name, k);
}
uint32_t rnc = ts_node_child_count(root);
for (uint32_t i = 0; i < rnc; i++) {
TSNode top = ts_node_child(root, i);
@@ -5818,7 +5878,8 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
continue;
const CBMType *ret = rust_parse_type_node(&tmp, rtn);
const char *fn_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, fname);
for (int k = 0; k < reg->func_count; k++) {
for (int k = rust_first_rows_start(&free_rows, fn_qn, reg->func_count);
k < reg->func_count; k++) {
CBMRegisteredFunc *rf = &reg->funcs[k];
if (!rf->qualified_name)
continue;
@@ -5834,6 +5895,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
break;
}
}
rust_first_rows_destroy(&free_rows);
}
/* Phase A2: derive-macro synthesis.
@@ -5963,7 +6025,10 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
* trait QN as an embedded_type on the receiver
* AND synthesize the method entries. */
CBMRegisteredType *rt = NULL;
for (int ti = 0; ti < reg->type_count; ti++) {
CBM_LSP_WORK(1);
int first = cbm_idxmemo_get(&type_idx, d->qualified_name);
for (int ti = first >= 0 ? first : reg->type_count; ti < reg->type_count;
ti++) {
if (reg->types[ti].qualified_name &&
strcmp(reg->types[ti].qualified_name, d->qualified_name) == 0) {
rt = &reg->types[ti];
@@ -6035,6 +6100,16 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
* registered functions have no return-type signature and chained
* method calls (`File::open().read()`) break. */
if (!ts_node_is_null(root)) {
/* The first function of each receiver and name; the patch loop starts there. */
RustFirstRows method_rows;
rust_first_rows_init(&method_rows);
for (int k = 0; k < reg->func_count; k++) {
const CBMRegisteredFunc *rf = &reg->funcs[k];
if (rf->receiver_type && rf->short_name)
rust_first_rows_put(
&method_rows, rust_method_key(&method_rows, rf->receiver_type, rf->short_name),
k);
}
uint32_t rnc = ts_node_child_count(root);
for (uint32_t i = 0; i < rnc; i++) {
TSNode top = ts_node_child(root, i);
@@ -6080,7 +6155,9 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
ret = cbm_type_named(arena, type_qn);
}
/* Patch the registered function's signature. */
for (int k = 0; k < reg->func_count; k++) {
const char *key = type_qn ? rust_method_key(&method_rows, type_qn, mname) : NULL;
for (int k = rust_first_rows_start(&method_rows, key, reg->func_count);
k < reg->func_count; k++) {
CBMRegisteredFunc *rf = &reg->funcs[k];
if (!rf->receiver_type || !rf->short_name)
continue;
@@ -6097,6 +6174,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
}
}
}
rust_first_rows_destroy(&method_rows);
}
/* Phase C: encode `impl Trait for Type` as `embedded_types` on the
+2
View File
@@ -24,6 +24,7 @@
#ifndef CBM_LSP_RUST_LSP_H
#define CBM_LSP_RUST_LSP_H
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -75,6 +76,7 @@ typedef struct {
const char **use_local_names;
const char **use_module_paths;
int use_count;
CBMNameRows use_rows; /* use_local_names by name, rows in table order */
const char **glob_module_qns;
int glob_count;
+94 -62
View File
@@ -27,6 +27,7 @@
*/
#include "ts_lsp.h"
#include "lsp_work.h"
#include "../../../src/foundation/platform.h"
#include "../helpers.h"
#include "../../../src/foundation/log.h"
@@ -1499,7 +1500,7 @@ static const CBMRegisteredFunc *ts_exact_imported_function(TSLSPContext *ctx, co
return NULL;
}
const char *matched_module = NULL;
for (int i = 0; i < ctx->import_count; i++) {
for (int i = ts_import_first(ctx, name); i >= 0; i = ts_import_next(ctx, name, i)) {
const char *local = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
const char *module_qn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
if (!local || !module_qn || strcmp(local, name) != 0) {
@@ -1554,7 +1555,7 @@ static const CBMType *type_of_identifier(TSLSPContext *ctx, const char *name) {
// and look it up in the registry. If it's a registered func, return its signature;
// if it's a registered type, return NAMED. Falls back to NAMED(module_qn) for
// namespace-style imports where no specific symbol matches.
for (int i = 0; i < ctx->import_count; i++) {
for (int i = ts_import_first(ctx, name); i >= 0; i = ts_import_next(ctx, name, i)) {
const char *local = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
if (!local || !mqn || strcmp(local, name) != 0)
@@ -2080,14 +2081,14 @@ static const CBMRegisteredFunc *ts_lookup_symbol_for_call(TSLSPContext *ctx, con
/* Map one local import binding to its module. Duplicate bindings from
* different modules are ambiguous and deliberately fail closed. */
static const char *ts_import_module_for_local(const TSLSPContext *ctx, const char *local_name,
static const char *ts_import_module_for_local(TSLSPContext *ctx, const char *local_name,
bool *out_matched) {
if (out_matched)
*out_matched = false;
if (!ctx || !local_name)
return NULL;
const char *matched = NULL;
for (int i = 0; i < ctx->import_count; i++) {
for (int i = ts_import_first(ctx, local_name); i >= 0; i = ts_import_next(ctx, local_name, i)) {
const char *local = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
const char *module_qn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
if (!local || !module_qn || strcmp(local, local_name) != 0)
@@ -2591,7 +2592,7 @@ static const CBMType *resolve_type_with_imports(TSLSPContext *ctx, const CBMType
return cbm_type_named(ctx->arena, local_qn);
}
for (int i = 0; i < ctx->import_count; i++) {
for (int i = ts_import_first(ctx, bare); i >= 0; i = ts_import_next(ctx, bare, i)) {
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
if (!lname || !mqn)
@@ -3061,7 +3062,7 @@ static void resolve_call_at(TSLSPContext *ctx, TSNode call_node) {
}
}
// Imported default/named function.
for (int i = 0; i < ctx->import_count; i++) {
for (int i = ts_import_first(ctx, name); i >= 0; i = ts_import_next(ctx, name, i)) {
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
if (lname && mqn && strcmp(lname, name) == 0) {
@@ -3399,7 +3400,7 @@ static void resolve_jsx_element(TSLSPContext *ctx, TSNode element_node) {
}
}
// Try imports.
for (int i = 0; i < ctx->import_count; i++) {
for (int i = ts_import_first(ctx, tag_name); i >= 0; i = ts_import_next(ctx, tag_name, i)) {
const char *lname = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
const char *mqn = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
if (lname && mqn && strcmp(lname, tag_name) == 0) {
@@ -4036,25 +4037,33 @@ void ts_lsp_init(TSLSPContext *ctx, CBMArena *arena, const char *source, int sou
ctx->debug = (debug_env && debug_env[0]);
}
enum { TS_IMPORT_FIRST_CAPACITY = 8, TS_SCRATCH_ARENA_BLOCK = 4096 };
void ts_lsp_add_import(TSLSPContext *ctx, const char *local_name, const char *module_qn) {
if (!ctx || !ctx->arena || !local_name || !module_qn)
return;
int new_count = ctx->import_count + 1;
const char **names =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
const char **qns =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)new_count * sizeof(const char *));
if (!names || !qns)
return;
for (int i = 0; i < ctx->import_count; i++) {
names[i] = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
qns[i] = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
/* The arrays double when full: copied on every import, a file of thousands of
* imports was quadratic. */
if (ctx->import_count == ctx->import_capacity) {
int capacity = ctx->import_capacity ? ctx->import_capacity * 2 : TS_IMPORT_FIRST_CAPACITY;
const char **names =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(const char *));
const char **qns =
(const char **)cbm_arena_alloc(ctx->arena, (size_t)capacity * sizeof(const char *));
if (!names || !qns)
return;
for (int i = 0; i < ctx->import_count; i++) {
CBM_LSP_WORK(1);
names[i] = ctx->import_local_names ? ctx->import_local_names[i] : NULL;
qns[i] = ctx->import_module_qns ? ctx->import_module_qns[i] : NULL;
}
ctx->import_local_names = names;
ctx->import_module_qns = qns;
ctx->import_capacity = capacity;
}
names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
ctx->import_local_names = names;
ctx->import_module_qns = qns;
ctx->import_count = new_count;
ctx->import_local_names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);
ctx->import_module_qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);
ctx->import_count++;
}
static void ts_lsp_add_import_alias(TSLSPContext *ctx, const char *local, const char *exported) {
@@ -4742,6 +4751,23 @@ void cbm_ts_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena) {
// ── Single-file entry point ───────────────────────────────────────────────────
/* The next function after `row` (-1: the first) whose receiver is `owner`, in
* registration order: by name over `receivers` when it could be listed, else by
* a scan. */
static int ts_receiver_row_next(const CBMTypeRegistry *reg, CBMNameRows *rows, CBMArena *arena,
const char **receivers, const char *owner, int row) {
if (receivers) {
return row < 0 ? cbm_name_rows_first(rows, arena, receivers, reg->func_count, owner)
: cbm_name_rows_next(rows, receivers, reg->func_count, owner, row);
}
for (int fi = row + 1; fi < reg->func_count; fi++) {
CBM_LSP_WORK(1);
if (reg->funcs[fi].receiver_type && strcmp(reg->funcs[fi].receiver_type, owner) == 0)
return fi;
}
return -1;
}
// Build a registry from result->defs: register Class/Interface as types; Function/Method
// as funcs. Walks AST briefly for class fields/extends.
static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileResult *result,
@@ -4890,18 +4916,30 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes
// Second pass: attach method_names / method_qns to each registered type from
// the registered methods. Lets dispatch find them via lookup_member_type.
// A type's methods are found by receiver name (lsp_name_rows.h): a scan of every
// function per type made a file of classes quadratic. The functions do not
// change in this pass.
CBMArena rows_arena;
cbm_arena_init_lazy(&rows_arena, TS_SCRATCH_ARENA_BLOCK);
CBMNameRows receiver_rows = {0};
const char **receivers = reg->func_count > 0
? (const char **)cbm_arena_alloc(
&rows_arena, (size_t)reg->func_count * sizeof(*receivers))
: NULL;
for (int fi = 0; receivers && fi < reg->func_count; fi++)
receivers[fi] = reg->funcs[fi].receiver_type;
for (int ti = 0; ti < reg->type_count; ti++) {
CBMRegisteredType *rt = &reg->types[ti];
if (!rt->qualified_name)
continue;
const char *owner = rt->qualified_name;
// Count methods first.
int mcount = 0;
for (int fi = 0; fi < reg->func_count; fi++) {
const CBMRegisteredFunc *f = &reg->funcs[fi];
if (f->receiver_type && strcmp(f->receiver_type, rt->qualified_name) == 0) {
mcount++;
}
for (int fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, -1);
fi >= 0;
fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, fi)) {
mcount++;
}
if (mcount == 0)
continue;
@@ -4913,19 +4951,20 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes
if (!mnames || !mqns)
continue;
int idx = 0;
for (int fi = 0; fi < reg->func_count && idx < mcount; fi++) {
for (int fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, -1);
fi >= 0 && idx < mcount;
fi = ts_receiver_row_next(reg, &receiver_rows, &rows_arena, receivers, owner, fi)) {
const CBMRegisteredFunc *f = &reg->funcs[fi];
if (f->receiver_type && strcmp(f->receiver_type, rt->qualified_name) == 0) {
mnames[idx] = f->short_name;
mqns[idx] = f->qualified_name;
idx++;
}
mnames[idx] = f->short_name;
mqns[idx] = f->qualified_name;
idx++;
}
mnames[mcount] = NULL;
mqns[mcount] = NULL;
rt->method_names = mnames;
rt->method_qns = mqns;
}
cbm_arena_destroy(&rows_arena);
}
// AST walk stacks for the passes below. The first chunk lives on the C stack, the
@@ -5566,7 +5605,9 @@ static void apply_jsdoc_signatures(TSLSPContext *ctx, TSNode root, CBMTypeRegist
continue;
const char *fqn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, fname);
for (int fi = 0; fi < reg->func_count; fi++) {
/* The loop patches the first registration of fqn; start there. */
const CBMRegisteredFunc *own = cbm_registry_first_own_func(reg, fqn);
for (int fi = own ? (int)(own - reg->funcs) : reg->func_count; fi < reg->func_count; fi++) {
if (!reg->funcs[fi].qualified_name)
continue;
if (strcmp(reg->funcs[fi].qualified_name, fqn) != 0)
@@ -5667,15 +5708,10 @@ static void infer_implicit_returns(TSLSPContext *ctx, TSNode root, CBMTypeRegist
const CBMType *rets[2] = {inferred, NULL};
const CBMType *new_sig = cbm_type_func(ctx->arena, param_names, param_types, rets);
// Patch the registered func in place.
for (int fi = 0; fi < reg->func_count; fi++) {
if (reg->funcs[fi].qualified_name == fqn ||
(reg->funcs[fi].qualified_name &&
strcmp(reg->funcs[fi].qualified_name, fqn) == 0)) {
reg->funcs[fi].signature = new_sig;
break;
}
}
// Patch the registered func in place: its first registration.
CBMRegisteredFunc *patched = cbm_registry_first_own_func(reg, fqn);
if (patched)
patched->signature = new_sig;
}
}
@@ -5873,17 +5909,10 @@ static void ast_sweep_shapes(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *re
}
}
// Find or insert.
bool found = false;
for (int ti = 0; ti < reg->type_count; ti++) {
if (reg->types[ti].qualified_name &&
strcmp(reg->types[ti].qualified_name, qn) == 0) {
if (alias_qn)
reg->types[ti].alias_of = alias_qn;
found = true;
break;
}
}
if (!found) {
CBMRegisteredType *known = cbm_registry_first_own_type(reg, qn);
if (known && alias_qn)
known->alias_of = alias_qn;
if (!known) {
CBMRegisteredType rt;
memset(&rt, 0, sizeof(rt));
rt.qualified_name = qn;
@@ -5908,14 +5937,7 @@ static void ast_sweep_shapes(TSLSPContext *ctx, TSNode root, CBMTypeRegistry *re
const char *class_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->module_qn, cname);
// Find the registered type and augment with fields.
CBMRegisteredType *rt = NULL;
for (int ti = 0; ti < reg->type_count; ti++) {
if (reg->types[ti].qualified_name &&
strcmp(reg->types[ti].qualified_name, class_qn) == 0) {
rt = &reg->types[ti];
break;
}
}
CBMRegisteredType *rt = cbm_registry_first_own_type(reg, class_qn);
if (!rt)
continue;
@@ -6046,7 +6068,16 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
CBMTypeRegistry reg;
cbm_registry_init(&reg, arena);
cbm_ts_stdlib_register(&reg, arena);
/* The passes below ask about every definition of the file, while it fills and
* after. It is indexed once its defs are in, with the first registration
* answering as a scan does, and what the passes add after that is found by
* name behind the index. The index memory is scratch: nothing reads it later. */
reg.index_first_registered = true;
CBMArena idx_arena;
cbm_arena_init_lazy(&idx_arena, TS_SCRATCH_ARENA_BLOCK);
reg.tail_arena = &idx_arena;
register_file_defs(arena, &reg, result, result->module_qn);
cbm_registry_finalize_into(&reg, &idx_arena);
TSLSPContext ctx;
ts_lsp_init(&ctx, arena, source, source_len, &reg, result->module_qn, js_mode, jsx_mode,
@@ -6070,6 +6101,7 @@ void cbm_run_ts_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
infer_implicit_returns(&ctx, root, &reg);
ts_lsp_process_file(&ctx, root);
cbm_arena_destroy(&idx_arena);
}
// ── Cross-file entry point (Phase 3) ──────────────────────────────────────────
+13
View File
@@ -1,6 +1,7 @@
#ifndef CBM_LSP_TS_LSP_H
#define CBM_LSP_TS_LSP_H
#include "lsp_name_rows.h"
#include "type_rep.h"
#include "scope.h"
#include "type_registry.h"
@@ -32,6 +33,8 @@ typedef struct {
const char **import_local_names;
const char **import_module_qns;
int import_count;
int import_capacity; // rows the two arrays hold (ts_lsp_add_import)
CBMNameRows import_rows; // import_local_names by name, rows in table order
// Renamed named imports (`import { foo as bar }`): local binding -> the name
// the module exports. Parallel arrays of length import_alias_count; a
@@ -75,6 +78,16 @@ typedef struct {
struct TsEvalMemo *eval_memo;
} TSLSPContext;
/* The imports with this local name, in table order (lsp_name_rows.h). */
static inline int ts_import_first(TSLSPContext *ctx, const char *name) {
return cbm_name_rows_first(&ctx->import_rows, ctx->arena, ctx->import_local_names,
ctx->import_count, name);
}
static inline int ts_import_next(const TSLSPContext *ctx, const char *name, int row) {
return cbm_name_rows_next(&ctx->import_rows, ctx->import_local_names, ctx->import_count, name,
row);
}
#ifdef CBM_ENABLE_TEST_SEAMS
// Complexity-regression seam: remaining expression-eval budget / warned flag
// for the calling thread, valid after a cbm_run_ts_lsp on the same thread.
+155 -31
View File
@@ -351,6 +351,91 @@ static const CBMRegisteredType *lookup_type_self(const CBMTypeRegistry *reg,
static const CBMRegisteredFunc *lookup_func_self(const CBMTypeRegistry *reg,
const char *qualified_name);
/* A tail this short is scanned; it is not worth a memo. */
enum { CBM_REGISTRY_TAIL_SCAN = 64 };
/* The first row from `from` on that registered the name, or -1. The memo answers
* for the rows it holds, and the rows behind it are scanned. */
static int tail_first_func(const CBMTypeRegistry *reg, int from, const char *qualified_name) {
const CBMRegistryTail *tail = &reg->func_tail;
if (tail->start == from && tail->synced > from) {
CBM_LSP_WORK(1);
int row = cbm_idxmemo_get(&tail->first, qualified_name);
if (row >= 0)
return row;
from = tail->synced;
}
for (int i = from; i < reg->func_count; i++) {
CBM_LSP_WORK(1);
if (reg->funcs[i].qualified_name &&
strcmp(reg->funcs[i].qualified_name, qualified_name) == 0)
return i;
}
return -1;
}
static int tail_first_type(const CBMTypeRegistry *reg, int from, const char *qualified_name) {
const CBMRegistryTail *tail = &reg->type_tail;
if (tail->start == from && tail->synced > from) {
CBM_LSP_WORK(1);
int row = cbm_idxmemo_get(&tail->first, qualified_name);
if (row >= 0)
return row;
from = tail->synced;
}
for (int i = from; i < reg->type_count; i++) {
CBM_LSP_WORK(1);
if (reg->types[i].qualified_name &&
strcmp(reg->types[i].qualified_name, qualified_name) == 0)
return i;
}
return -1;
}
/* Brings a long tail's memo up to date. An index built since starts it over, and a
* row the memo could not take stays behind `synced`, where lookups scan for it. */
static void tail_sync_funcs(CBMTypeRegistry *reg) {
CBMRegistryTail *tail = &reg->func_tail;
int from = reg->func_qn_buckets && reg->func_qn_bucket_count > 0 ? reg->func_qn_entry_count : 0;
if (!reg->tail_arena || reg->func_count - from < CBM_REGISTRY_TAIL_SCAN)
return;
if (tail->start != from) {
memset(&tail->first, 0, sizeof(tail->first));
tail->start = from;
tail->synced = from;
}
for (; tail->synced < reg->func_count; tail->synced++) {
const char *qn = reg->funcs[tail->synced].qualified_name;
if (!qn)
continue;
CBM_LSP_WORK(1);
cbm_idxmemo_put_if_absent(&tail->first, reg->tail_arena, qn, tail->synced);
if (cbm_idxmemo_get(&tail->first, qn) < 0)
return;
}
}
static void tail_sync_types(CBMTypeRegistry *reg) {
CBMRegistryTail *tail = &reg->type_tail;
int from = reg->type_qn_buckets && reg->type_qn_bucket_count > 0 ? reg->type_qn_entry_count : 0;
if (!reg->tail_arena || reg->type_count - from < CBM_REGISTRY_TAIL_SCAN)
return;
if (tail->start != from) {
memset(&tail->first, 0, sizeof(tail->first));
tail->start = from;
tail->synced = from;
}
for (; tail->synced < reg->type_count; tail->synced++) {
const char *qn = reg->types[tail->synced].qualified_name;
if (!qn)
continue;
CBM_LSP_WORK(1);
cbm_idxmemo_put_if_absent(&tail->first, reg->tail_arena, qn, tail->synced);
if (cbm_idxmemo_get(&tail->first, qn) < 0)
return;
}
}
/* A base entry is shadowed when the head holds an entry with the same QN. */
static bool type_shadowed(const CBMTypeRegistry *shadow, const CBMTypeRegistry *reg, int i) {
if (!shadow || shadow == reg || !reg->types[i].qualified_name) {
@@ -727,6 +812,66 @@ void cbm_registry_finalize_into(CBMTypeRegistry *reg, CBMArena *idx_arena) {
build_type_short_index(reg, idx_arena);
}
void cbm_registry_index_types_into(CBMTypeRegistry *reg, CBMArena *idx_arena) {
if (!reg || !idx_arena)
return;
bool first = reg->index_first_registered;
reg->index_first_registered = true;
build_qn_index(reg, idx_arena, /*for_funcs=*/false);
build_type_short_index(reg, idx_arena);
reg->index_first_registered = first;
}
CBMRegisteredFunc *cbm_registry_first_own_func(CBMTypeRegistry *reg, const char *qualified_name) {
if (!reg || !qualified_name) {
return NULL;
}
int indexed = 0, lowest = -1;
if (reg->func_qn_buckets && reg->func_qn_bucket_count > 0) {
indexed = reg->func_qn_entry_count;
uint64_t h = fnv1a(qualified_name);
int slot = (int)(h & (uint64_t)(reg->func_qn_bucket_count - 1));
for (int idx = reg->func_qn_buckets[slot]; idx >= 0;
idx = reg->func_qn_entries[idx].next_index) {
CBM_LSP_WORK(1);
int p = reg->func_qn_entries[idx].payload_index;
if (reg->func_qn_entries[idx].hash == h && reg->funcs[p].qualified_name &&
strcmp(reg->funcs[p].qualified_name, qualified_name) == 0 &&
(lowest < 0 || p < lowest)) {
lowest = p;
}
}
}
if (lowest < 0)
lowest = tail_first_func(reg, indexed, qualified_name);
return lowest >= 0 ? &reg->funcs[lowest] : NULL;
}
CBMRegisteredType *cbm_registry_first_own_type(CBMTypeRegistry *reg, const char *qualified_name) {
if (!reg || !qualified_name) {
return NULL;
}
int indexed = 0, lowest = -1;
if (reg->type_qn_buckets && reg->type_qn_bucket_count > 0) {
indexed = reg->type_qn_entry_count;
uint64_t h = fnv1a(qualified_name);
int slot = (int)(h & (uint64_t)(reg->type_qn_bucket_count - 1));
for (int idx = reg->type_qn_buckets[slot]; idx >= 0;
idx = reg->type_qn_entries[idx].next_index) {
CBM_LSP_WORK(1);
int p = reg->type_qn_entries[idx].payload_index;
if (reg->type_qn_entries[idx].hash == h && reg->types[p].qualified_name &&
strcmp(reg->types[p].qualified_name, qualified_name) == 0 &&
(lowest < 0 || p < lowest)) {
lowest = p;
}
}
}
if (lowest < 0)
lowest = tail_first_type(reg, indexed, qualified_name);
return lowest >= 0 ? &reg->types[lowest] : NULL;
}
void cbm_registry_finalize(CBMTypeRegistry *reg) {
if (!reg)
return;
@@ -756,6 +901,7 @@ void cbm_registry_add_func(CBMTypeRegistry *reg, CBMRegisteredFunc func) {
reg->func_cap = new_cap;
}
reg->funcs[reg->func_count++] = func;
tail_sync_funcs(reg);
}
void cbm_registry_add_type(CBMTypeRegistry *reg, CBMRegisteredType type) {
@@ -776,6 +922,7 @@ void cbm_registry_add_type(CBMTypeRegistry *reg, CBMRegisteredType type) {
reg->type_cap = new_cap;
}
reg->types[reg->type_count++] = type;
tail_sync_types(reg);
}
static const CBMRegisteredFunc *lookup_method_self(const CBMTypeRegistry *reg,
@@ -850,23 +997,12 @@ static const CBMRegisteredType *lookup_type_self(const CBMTypeRegistry *reg,
}
}
/* Tail-scan types added after finalize (see lookup_method_self). */
for (int i = reg->type_qn_entry_count; i < reg->type_count; i++) {
CBM_LSP_WORK(1);
if (reg->types[i].qualified_name &&
strcmp(reg->types[i].qualified_name, qualified_name) == 0) {
return &reg->types[i];
}
}
return NULL;
int tail_row = tail_first_type(reg, reg->type_qn_entry_count, qualified_name);
return tail_row >= 0 ? &reg->types[tail_row] : NULL;
}
for (int i = 0; i < reg->type_count; i++) {
CBM_LSP_WORK(1);
if (strcmp(reg->types[i].qualified_name, qualified_name) == 0) {
return &reg->types[i];
}
}
return NULL;
int row = tail_first_type(reg, 0, qualified_name);
return row >= 0 ? &reg->types[row] : NULL;
}
const CBMRegisteredType *cbm_registry_lookup_type(const CBMTypeRegistry *reg,
@@ -897,24 +1033,12 @@ static const CBMRegisteredFunc *lookup_func_self(const CBMTypeRegistry *reg,
}
}
/* Tail-scan funcs added after finalize (see lookup_method_self). */
for (int i = reg->func_qn_entry_count; i < reg->func_count; i++) {
CBM_LSP_WORK(1);
if (reg->funcs[i].qualified_name &&
strcmp(reg->funcs[i].qualified_name, qualified_name) == 0) {
return &reg->funcs[i];
}
}
return NULL;
int tail_row = tail_first_func(reg, reg->func_qn_entry_count, qualified_name);
return tail_row >= 0 ? &reg->funcs[tail_row] : NULL;
}
for (int i = 0; i < reg->func_count; i++) {
CBM_LSP_WORK(1);
if (reg->funcs[i].qualified_name &&
strcmp(reg->funcs[i].qualified_name, qualified_name) == 0) {
return &reg->funcs[i];
}
}
return NULL;
int row = tail_first_func(reg, 0, qualified_name);
return row >= 0 ? &reg->funcs[row] : NULL;
}
const CBMRegisteredFunc *cbm_registry_lookup_func(const CBMTypeRegistry *reg,
+22
View File
@@ -3,6 +3,7 @@
#include "type_rep.h"
#include "../arena.h"
#include "lsp_neg_memo.h"
#include <stdbool.h>
// Language-specific function metadata. Added at struct tail so existing
@@ -95,6 +96,13 @@ typedef struct {
int type_index;
} CBMRegistryTypeQNEntry;
// The rows of funcs[] or types[] past their QN index, by name (see tail_arena).
typedef struct {
CBMIdxMemo first; // a QN's first row among [start, synced)
int start;
int synced;
} CBMRegistryTail;
// Cross-file type/function registry.
typedef struct CBMTypeRegistry {
CBMRegisteredFunc *funcs;
@@ -126,6 +134,13 @@ typedef struct CBMTypeRegistry {
int type_qn_bucket_count;
int type_qn_entry_count;
// Set by a caller whose lookups run while the registry fills: the rows past
// the QN indexes are then found by name instead of by a scan, with the same
// answers. The arena must outlive those lookups. NULL keeps the scan.
CBMArena *tail_arena;
CBMRegistryTail func_tail;
CBMRegistryTail type_tail;
// Methods indexed by (receiver_type, short_name) — chain holds overloads.
int *method_buckets;
CBMRegistryHashEntry *method_entries;
@@ -212,6 +227,9 @@ void cbm_registry_finalize(CBMTypeRegistry *reg);
// pipeline-lifetime result arena, and per-file index allocations accumulated
// there add GBs across a large repo (FastAPI incremental test: +1.1 GB RSS).
void cbm_registry_finalize_into(CBMTypeRegistry *reg, CBMArena *idx_arena);
// Indexes only the types, for a registry that is still filling. A scan finds the first
// registration, so these answer with that one; a later finalize replaces them.
void cbm_registry_index_types_into(CBMTypeRegistry *reg, CBMArena *idx_arena);
// Build the optional final-QN-segment type index. Call immediately after
// finalization. Consumers that do not opt in avoid its type_count-sized allocation
@@ -463,6 +481,10 @@ void cbm_registry_all_types_chain(const CBMTypeRegistry *head, CBMTypeShortIter
* sealed-registry no-op in cbm_registry_add_func does today. */
CBMRegisteredFunc *cbm_registry_func_for_update(CBMTypeRegistry *head, const char *qualified_name);
CBMRegisteredType *cbm_registry_type_for_update(CBMTypeRegistry *head, const char *qualified_name);
/* The first function or type registered under this name in reg itself, whichever order
* its index answers lookups in; a fallback registry is not consulted. */
CBMRegisteredFunc *cbm_registry_first_own_func(CBMTypeRegistry *reg, const char *qualified_name);
CBMRegisteredType *cbm_registry_first_own_type(CBMTypeRegistry *reg, const char *qualified_name);
// --- TS-specific helpers (return NULL for types without these signatures) ---
+47
View File
@@ -0,0 +1,47 @@
/*
* resolve_stats.c — deterministic work counters for growth tests (see the header).
*/
#include "resolve_stats.h"
#ifdef CBM_ENABLE_TEST_SEAMS
#include <stddef.h>
static _Thread_local cbm_resolve_stats_t *current_stats;
void cbm_resolve_stats_reset(cbm_resolve_stats_t *stats) {
if (stats) {
for (unsigned i = 0; i < CBM_RESOLVE_STAT_COUNT; i++) {
atomic_init(&stats->values[i], 0);
}
}
}
cbm_resolve_stats_t *cbm_resolve_stats_bind(cbm_resolve_stats_t *stats) {
cbm_resolve_stats_t *previous = current_stats;
current_stats = stats;
return previous;
}
void cbm_resolve_stats_add(cbm_resolve_stat_t event, uint64_t amount) {
if (current_stats && (unsigned)event < CBM_RESOLVE_STAT_COUNT) {
atomic_fetch_add_explicit(&current_stats->values[event], amount, memory_order_relaxed);
}
}
cbm_resolve_stats_snapshot_t cbm_resolve_stats_snapshot(const cbm_resolve_stats_t *stats) {
cbm_resolve_stats_snapshot_t result = {0};
if (stats) {
for (unsigned i = 0; i < CBM_RESOLVE_STAT_COUNT; i++) {
result.values[i] = atomic_load_explicit(&stats->values[i], memory_order_relaxed);
}
}
return result;
}
const char *cbm_resolve_stat_name(cbm_resolve_stat_t event) {
static const char *const names[] = {"lookup_probes", "syntax_child_steps"};
_Static_assert(sizeof(names) / sizeof(names[0]) == CBM_RESOLVE_STAT_COUNT,
"one name per counter");
return (unsigned)event < CBM_RESOLVE_STAT_COUNT ? names[event] : "unknown";
}
#endif
+49
View File
@@ -0,0 +1,49 @@
/*
* resolve_stats.h — deterministic work counters for growth tests.
*
* Semantic work, never elapsed time or address-dependent hash placement, so a
* test can double its input and assert the work about doubles (O9).
* LOOKUP_PROBES counts keyed probes and comparisons; SYNTAX_CHILD_STEPS counts
* the sibling steps taken to enumerate a node's children.
*
* Seam builds only (CBM_ENABLE_TEST_SEAMS), like lsp/lsp_work.h: nothing in a
* product build reads these, so there cbm_resolve_stats_add compiles to nothing.
*/
#ifndef CBM_RESOLVE_STATS_H
#define CBM_RESOLVE_STATS_H
#include <stdint.h>
typedef enum {
CBM_RESOLVE_STAT_LOOKUP_PROBES,
CBM_RESOLVE_STAT_SYNTAX_CHILD_STEPS,
CBM_RESOLVE_STAT_COUNT
} cbm_resolve_stat_t;
#ifdef CBM_ENABLE_TEST_SEAMS
#include <stdatomic.h>
typedef struct {
_Atomic uint64_t values[CBM_RESOLVE_STAT_COUNT];
} cbm_resolve_stats_t;
typedef struct {
uint64_t values[CBM_RESOLVE_STAT_COUNT];
} cbm_resolve_stats_snapshot_t;
/* A sink is owned by its caller. Reset it before binding; snapshot it after
* every thread bound to it has unbound. Bind returns the thread's previous
* sink, which the caller must restore before leaving its scope. Work done on a
* thread with no sink bound is not counted. No allocation is involved. */
void cbm_resolve_stats_reset(cbm_resolve_stats_t *stats);
cbm_resolve_stats_t *cbm_resolve_stats_bind(cbm_resolve_stats_t *stats);
void cbm_resolve_stats_add(cbm_resolve_stat_t event, uint64_t amount);
cbm_resolve_stats_snapshot_t cbm_resolve_stats_snapshot(const cbm_resolve_stats_t *stats);
const char *cbm_resolve_stat_name(cbm_resolve_stat_t event);
#else
static inline void cbm_resolve_stats_add(cbm_resolve_stat_t event, uint64_t amount) {
(void)event;
(void)amount;
}
#endif
#endif /* CBM_RESOLVE_STATS_H */
+105 -15
View File
@@ -6,7 +6,10 @@
* `Runtime.getRuntime().exec`, a constructor as its class name.
*/
#include "spawn_patterns.h"
#include "foundation/compat.h"
#include "foundation/constants.h"
#include "foundation/hash_table.h"
#include "resolve_stats.h"
#include <stdio.h>
#include <string.h>
@@ -415,6 +418,82 @@ static bool spawn_val_names(const char *val, const char *name) {
return sep == '.' || sep == ':' || sep == '/' || sep == '\\';
}
/* The rows of the file being walked, once a call has asked: every call asks about them. */
typedef struct {
const CBMImport *imports;
int count;
bool built;
CBMHashTable *first_row; /* local name -> its first row with a path */
CBMHashTable *modules; /* a spawn table's module -> spawn_module_yes or spawn_module_no */
} spawn_file_t;
static CBM_TLS spawn_file_t spawn_file;
static const char spawn_module_yes[] = "y";
static const char spawn_module_no[] = "n";
void cbm_spawn_file_end(void) {
cbm_ht_free(spawn_file.first_row);
cbm_ht_free(spawn_file.modules);
spawn_file = (spawn_file_t){0};
}
void cbm_spawn_file_begin(const CBMImport *imports, int count) {
cbm_spawn_file_end();
spawn_file.imports = imports;
spawn_file.count = count;
}
/* False: read the rows. These are not the file's, or memory ran out. */
static bool spawn_file_ready(const CBMImport *imports, int count) {
if (count <= 0 || spawn_file.imports != imports || spawn_file.count != count) {
return false;
}
if (spawn_file.built) {
return spawn_file.first_row != NULL;
}
spawn_file.built = true;
CBMHashTable *rows = cbm_ht_create((uint32_t)count * 2U);
CBMHashTable *modules = cbm_ht_create(16);
bool ok = rows && modules;
for (int i = 0; ok && i < count; i++) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
const char *key = imports[i].local_name;
if (!key || !imports[i].module_path || cbm_ht_get(rows, key)) {
continue;
}
cbm_ht_set(rows, key, (void *)&imports[i]);
ok = cbm_ht_get(rows, key) == &imports[i];
}
if (!ok) {
cbm_ht_free(rows);
cbm_ht_free(modules);
return false;
}
spawn_file.first_row = rows;
spawn_file.modules = modules;
return true;
}
/* The first row with a path that names the callee's first key_len bytes. */
static const CBMImport *spawn_row_named(const char *callee, size_t key_len,
const CBMImport *imports, int count) {
char name[CBM_SZ_512];
if (key_len < sizeof(name) && spawn_file_ready(imports, count)) {
memcpy(name, callee, key_len);
name[key_len] = '\0';
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
return cbm_ht_get(spawn_file.first_row, name);
}
for (int i = 0; i < count; i++) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
const char *key = imports[i].local_name;
if (key && imports[i].module_path && strlen(key) == key_len &&
strncmp(key, callee, key_len) == 0) {
return &imports[i];
}
}
return NULL;
}
/* The callee as its import makes it: `sp.run` -> subprocess.run (an alias),
* `run` -> subprocess.run (a member import), `spawn` -> child_process.spawn
* (a named import mapped to its module). Empty when no import applies. */
@@ -423,27 +502,24 @@ static void spawn_expand(const char *callee, const CBMImport *imports, int count
out[0] = '\0';
size_t head = spawn_head_len(callee);
size_t key_len = head ? head : strlen(callee);
for (int i = 0; i < count; i++) {
const char *key = imports[i].local_name;
if (!key || !imports[i].module_path || strlen(key) != key_len ||
strncmp(key, callee, key_len) != 0) {
continue;
}
const char *val = spawn_module(imports[i].module_path);
if (head) {
snprintf(out, n, "%s%s", val, callee + head);
} else if (spawn_val_names(val, callee)) {
snprintf(out, n, "%s", val);
} else {
snprintf(out, n, "%s.%s", val, callee);
}
const CBMImport *row = spawn_row_named(callee, key_len, imports, count);
if (!row) {
return;
}
const char *val = spawn_module(row->module_path);
if (head) {
snprintf(out, n, "%s%s", val, callee + head);
} else if (spawn_val_names(val, callee)) {
snprintf(out, n, "%s", val);
} else {
snprintf(out, n, "%s.%s", val, callee);
}
}
static bool spawn_imports_module(const CBMImport *imports, int count, const char *module) {
static bool spawn_rows_import_module(const CBMImport *imports, int count, const char *module) {
size_t ml = strlen(module);
for (int i = 0; i < count; i++) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
if (!imports[i].module_path) {
continue;
}
@@ -456,6 +532,20 @@ static bool spawn_imports_module(const CBMImport *imports, int count, const char
return false;
}
/* A spawn table names a fixed few modules, so one file reads its rows that many times at most. */
static bool spawn_imports_module(const CBMImport *imports, int count, const char *module) {
if (!spawn_file_ready(imports, count)) {
return spawn_rows_import_module(imports, count, module);
}
const void *known = cbm_ht_get(spawn_file.modules, module);
if (known) {
return known == spawn_module_yes;
}
bool found = spawn_rows_import_module(imports, count, module);
cbm_ht_set(spawn_file.modules, module, (void *)(found ? spawn_module_yes : spawn_module_no));
return found;
}
/* Groovy `"git status".execute`: a quoted receiver, then the api suffix. */
static bool spawn_string_receiver(const char *callee, const char *suffix) {
size_t cl = strlen(callee);
+5
View File
@@ -59,6 +59,11 @@ typedef struct {
const cbm_spawn_api_t *cbm_spawn_match(CBMLanguage lang, const char *callee,
const CBMImport *imports, int count);
/* Around one file's loop over its calls, on one thread: a match on exactly these rows asks them
* by name, the same answer as reading them all. Any other rows are read as usual. */
void cbm_spawn_file_begin(const CBMImport *imports, int count);
void cbm_spawn_file_end(void);
/* The program a matched spawn starts: the first literal in its program
* argument, that literal's first word, without directories
* ("/usr/bin/git status" -> "git"). CBM_SPAWN_DYNAMIC when the program is
File diff suppressed because one or more lines are too long
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 harumiWeb
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,54 @@
#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void *(*ts_current_malloc)(size_t size);
extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void *ptr);
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif
#else
#ifndef ts_malloc
#define ts_malloc malloc
#endif
#ifndef ts_calloc
#define ts_calloc calloc
#endif
#ifndef ts_realloc
#define ts_realloc realloc
#endif
#ifndef ts_free
#define ts_free free
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ALLOC_H_
@@ -0,0 +1,291 @@
#ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "./alloc.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#define Array(T) \
struct { \
T *contents; \
uint32_t size; \
uint32_t capacity; \
}
/// Initialize an array.
#define array_init(self) \
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
/// Create an empty array.
#define array_new() \
{ NULL, 0, 0 }
/// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0)
/// Get a pointer to the last element in the array.
#define array_back(self) array_get(self, (self)->size - 1)
/// Clear the array, setting its size to zero. Note that this does not free any
/// memory allocated for the array's contents.
#define array_clear(self) ((self)->size = 0)
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
/// less than the array's current capacity, this function has no effect.
#define array_reserve(self, new_capacity) \
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
/// Free any memory allocated for this array. Note that this does not free any
/// memory allocated for the array's contents.
#define array_delete(self) _array__delete((Array *)(self))
/// Push a new `element` onto the end of the array.
#define array_push(self, element) \
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
(self)->contents[(self)->size++] = (element))
/// Increase the array's size by `count` elements.
/// New elements are zero-initialized.
#define array_grow_by(self, count) \
do { \
if ((count) == 0) break; \
_array__grow((Array *)(self), count, array_elem_size(self)); \
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
(self)->size += (count); \
} while (0)
/// Append all elements from one array to the end of another.
#define array_push_all(self, other) \
array_extend((self), (other)->size, (other)->contents)
/// Append `count` elements to the end of the array, reading their values from the
/// `contents` pointer.
#define array_extend(self, count, contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), (self)->size, \
0, count, contents \
)
/// Remove `old_count` elements from the array starting at the given `index`. At
/// the same index, insert `new_count` new elements, reading their values from the
/// `new_contents` pointer.
#define array_splice(self, _index, old_count, new_count, new_contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), _index, \
old_count, new_count, new_contents \
)
/// Insert one `element` into the array at the given `index`.
#define array_insert(self, _index, element) \
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
/// Remove one element from the array at the given `index`.
#define array_erase(self, _index) \
_array__erase((Array *)(self), array_elem_size(self), _index)
/// Pop the last element off the array, returning the element by value.
#define array_pop(self) ((self)->contents[--(self)->size])
/// Assign the contents of one array to another, reallocating if necessary.
#define array_assign(self, other) \
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
/// Swap one array with another
#define array_swap(self, other) \
_array__swap((Array *)(self), (Array *)(other))
/// Get the size of the array contents
#define array_elem_size(self) (sizeof *(self)->contents)
/// Search a sorted array for a given `needle` value, using the given `compare`
/// callback to determine the order.
///
/// If an existing element is found to be equal to `needle`, then the `index`
/// out-parameter is set to the existing value's index, and the `exists`
/// out-parameter is set to true. Otherwise, `index` is set to an index where
/// `needle` should be inserted in order to preserve the sorting, and `exists`
/// is set to false.
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
/// Search a sorted array for a given `needle` value, using integer comparisons
/// of a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, _index, _exists) \
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
/// Insert a given `value` into a sorted array, using the given `compare`
/// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
/// Insert a given `value` into a sorted array, using integer comparisons of
/// a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
// Private
typedef Array(void) Array;
/// This is not what you're looking for, see `array_delete`.
static inline void _array__delete(Array *self) {
if (self->contents) {
ts_free(self->contents);
self->contents = NULL;
self->size = 0;
self->capacity = 0;
}
}
/// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array *self, size_t element_size,
uint32_t index) {
assert(index < self->size);
char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size,
(self->size - index - 1) * element_size);
self->size--;
}
/// This is not what you're looking for, see `array_reserve`.
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
if (new_capacity > self->capacity) {
if (self->contents) {
self->contents = ts_realloc(self->contents, new_capacity * element_size);
} else {
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
}
}
/// This is not what you're looking for, see `array_assign`.
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
_array__reserve(self, element_size, other->size);
self->size = other->size;
memcpy(self->contents, other->contents, self->size * element_size);
}
/// This is not what you're looking for, see `array_swap`.
static inline void _array__swap(Array *self, Array *other) {
Array swap = *other;
*other = *self;
*self = swap;
}
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
uint32_t new_size = self->size + count;
if (new_size > self->capacity) {
uint32_t new_capacity = self->capacity * 2;
if (new_capacity < 8) new_capacity = 8;
if (new_capacity < new_size) new_capacity = new_size;
_array__reserve(self, element_size, new_capacity);
}
}
/// This is not what you're looking for, see `array_splice`.
static inline void _array__splice(Array *self, size_t element_size,
uint32_t index, uint32_t old_count,
uint32_t new_count, const void *elements) {
uint32_t new_size = self->size + new_count - old_count;
uint32_t old_end = index + old_count;
uint32_t new_end = index + new_count;
assert(old_end <= self->size);
_array__reserve(self, element_size, new_size);
char *contents = (char *)self->contents;
if (self->size > old_end) {
memmove(
contents + new_end * element_size,
contents + old_end * element_size,
(self->size - old_end) * element_size
);
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
}
}
self->size += new_count - old_count;
}
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
do { \
*(_index) = start; \
*(_exists) = false; \
uint32_t size = (self)->size - *(_index); \
if (size == 0) break; \
int comparison; \
while (size > 1) { \
uint32_t half_size = size / 2; \
uint32_t mid_index = *(_index) + half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
if (comparison <= 0) *(_index) = mid_index; \
size -= half_size; \
} \
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
if (comparison == 0) *(_exists) = true; \
else if (comparison < 0) *(_index) += 1; \
} while (0)
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
/// parameter by reference in order to work with the generic sorting function above.
#define _compare_int(a, b) ((int)*(a) - (int)(b))
#ifdef _MSC_VER
#pragma warning(pop)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ARRAY_H_
@@ -0,0 +1,286 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata {
uint8_t major_version;
uint8_t minor_version;
uint8_t patch_version;
} TSLanguageMetadata;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
// Used to index the field and supertype maps.
typedef struct {
uint16_t index;
uint16_t length;
} TSMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
void (*log)(const TSLexer *, const char *, ...);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
uint16_t reserved_word_set_id;
} TSLexerMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage {
uint32_t abi_version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexerMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
const char *name;
const TSSymbol *reserved_words;
uint16_t max_reserved_word_set_size;
uint32_t supertype_count;
const TSSymbol *supertype_symbols;
const TSMapSlice *supertype_map_slices;
const TSSymbol *supertype_map_entries;
TSLanguageMetadata metadata;
};
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
uint32_t index = 0;
uint32_t size = len - index;
while (size > 1) {
uint32_t half_size = size / 2;
uint32_t mid_index = index + half_size;
const TSCharacterRange *range = &ranges[mid_index];
if (lookahead >= range->start && lookahead <= range->end) {
return true;
} else if (lookahead > range->end) {
index = mid_index;
}
size -= half_size;
}
const TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
}
/*
* Lexer Macros
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define ADVANCE_MAP(...) \
{ \
static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
if (map[i] == lookahead) { \
state = map[i + 1]; \
goto next_state; \
} \
} \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value) \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value), \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_name, children, precedence, prod_id) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_name, \
.child_count = children, \
.dynamic_precedence = precedence, \
.production_id = prod_id \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_
+1 -1
View File
@@ -18,7 +18,7 @@ release-owned runtime assets. Chocolatey retains the complete extracted runtime
set and exposes its executable through a PATH shim.
Full indexes an average repository in milliseconds, the Linux kernel (28M LOC) in
3 minutes. Answers structural queries in under 1ms. Supports 163 languages via
3 minutes. Answers structural queries in under 1ms. Supports 164 languages via
vendored tree-sitter grammars. No hosted service or API key. Plug and play across
11 coding agents including Claude Code, Codex CLI, Gemini CLI, and Zed.
+4
View File
@@ -47,6 +47,10 @@ FORKS = { # self-maintained forks: vendored LICENSE must match the original ups
# same routing rule as arkts -- upstream's MIT verbatim, byte-verified.
"sql": "DerekStride/tree-sitter-sql",
"tsx": "tree-sitter/tree-sitter-typescript",
# vba: tree-sitter-vba's vba dialect, generated here because upstream tracks
# no parser.c; no local patch. Same routing rule as arkts -- upstream's MIT
# verbatim, byte-verified.
"vba": "harumiWeb/tree-sitter-vba",
# vbnet: tree-sitter-vbnet + CodeGraph's real-world VB.NET patch; same
# routing rule as arkts -- `LICENSE` is upstream's MIT verbatim and is the
# file byte-verified here (local_license() takes it before the sibling
+1 -1
View File
@@ -73,7 +73,7 @@ src/pipeline/pass_pkgmap.c 80
src/pipeline/pass_semantic.c 3
src/pipeline/pass_tests.c 3
src/pipeline/pass_usages.c 4
src/pipeline/path_alias.c 40
src/pipeline/path_alias.c 1
src/pipeline/pipeline.c 129
src/pipeline/pipeline_delta.c 19
src/pipeline/pipeline_incremental.c 130
+6 -1
View File
@@ -5,7 +5,7 @@ c5cfb43042b6b72045f4ba997834d0a7786d2793d91680868b5815b39f14fc78 internal/cbm/v
b29c1c9fb7cc82f58c84b376df1297d6e2737a1d655fd356db0859e3c29c2fea internal/cbm/vendored/common/tree_sitter/alloc.h
31e60a1bff6f715afacce03b5b70efe42b58371b4f9595dd4af52a577ff9608c internal/cbm/vendored/common/tree_sitter/array.h
180b893c8734778fd32f372dfbc27bd6ad1cd2221f26150b31256ff6716320d2 internal/cbm/vendored/common/tree_sitter/parser.h
03ae993e12fba27d7e4a14ca11f287e4f9d26b22cafa3ba812f07bfade08c76c internal/cbm/vendored/grammars/MANIFEST.md
c22080d2defb55a7d54cf2925fed37ec81479676fff14bda968c387fae992916 internal/cbm/vendored/grammars/MANIFEST.md
ad8425038de519f8c4e3e9339feebf99dfad8a6002dcf79227d91402780a32cc internal/cbm/vendored/grammars/ada/LICENSE
02805ec13939b749c891567be36bf024b09034e04c80683a1ae667272458d549 internal/cbm/vendored/grammars/ada/parser.c
115a75d000bef9c70c4de7dfbb7f2a80b90cbb14ba265056dc7abb1ef3b9b6db internal/cbm/vendored/grammars/ada/tree_sitter/alloc.h
@@ -862,6 +862,11 @@ eeb1ce8085d748ea8ec44b337b179a676dfab4549593b834e9c510362e795b64 internal/cbm/v
02d139d44303b0b0b85a899839dbc1346c478e7318d1f2971fad2b38fd57cb73 internal/cbm/vendored/grammars/typst/tree_sitter/array.h
2db740ea1be6b71014d2be1385491f01bf2d15b607e61c5b30b4128535efe68a internal/cbm/vendored/grammars/typst/tree_sitter/parser.h
6d1a6e017b78433535460ceb3e386d92d8c5dca6e85e6919d5810f1f9aa665a1 internal/cbm/vendored/grammars/typst/unicode.h
f58a3dc0eafdfbb9615856ba74f340c96db45d0f7423cbb28bbdc12bef5c08a1 internal/cbm/vendored/grammars/vba/LICENSE
bd2a7ecde688f90e53e96b2f3d3e8e0c338363efd329e5b8b6f90abe9ac4beb3 internal/cbm/vendored/grammars/vba/parser.c
b29c1c9fb7cc82f58c84b376df1297d6e2737a1d655fd356db0859e3c29c2fea internal/cbm/vendored/grammars/vba/tree_sitter/alloc.h
5bdf6ed1a78e3409fd443e085ca967a64c188a5d082aaf7f819bccd53a471c94 internal/cbm/vendored/grammars/vba/tree_sitter/array.h
180b893c8734778fd32f372dfbc27bd6ad1cd2221f26150b31256ff6716320d2 internal/cbm/vendored/grammars/vba/tree_sitter/parser.h
fa7ab39bcf92c216d429e8aa8be8a5a40db85bfbe4ff14a7bb0ecbbcca937454 internal/cbm/vendored/grammars/vbnet/LICENSE
e6d98f98c666bebe065ac2492a0a19232cc318d4d67bac3ca42ffb77bacc8809 internal/cbm/vendored/grammars/vbnet/LICENSE-codegraph
d0c29e9a77416fb24e60c8e7f832d60b0cbbf7f40e375f179b119790b0ff0d78 internal/cbm/vendored/grammars/vbnet/parser.c
+11 -3
View File
@@ -7518,6 +7518,14 @@ int cbm_cli_remove_windows_user_path_for_test(const char *bin_dir, bool dry_run)
*/
#ifdef CBM_CLI_ENABLE_TEST_API
/* The most an archive may decompress to. A test lowers it to pin the refusal
* without building an archive of hundreds of megabytes. */
static size_t g_cli_decompress_max = DECOMPRESS_MAX_BYTES;
void cbm_cli_set_decompress_max_for_test(size_t max_bytes) {
g_cli_decompress_max = max_bytes ? max_bytes : DECOMPRESS_MAX_BYTES;
}
/* Decompress gzip data into a malloc'd buffer. Returns NULL on failure.
* *out_total receives the decompressed size. Caller must free the result. */
static unsigned char *gzip_decompress(const unsigned char *data, int data_len, size_t *out_total) {
@@ -7535,8 +7543,8 @@ static unsigned char *gzip_decompress(const unsigned char *data, int data_len, s
if (buf_cap < CLI_BUF_4K) {
buf_cap = CLI_BUF_4K;
}
if (buf_cap > DECOMPRESS_MAX_BYTES) {
buf_cap = DECOMPRESS_MAX_BYTES;
if (buf_cap > g_cli_decompress_max) {
buf_cap = g_cli_decompress_max;
}
unsigned char *decompressed = malloc(buf_cap);
if (!decompressed) {
@@ -7549,7 +7557,7 @@ static unsigned char *gzip_decompress(const unsigned char *data, int data_len, s
do {
if (total >= buf_cap) {
size_t new_cap = buf_cap * GROWTH_FACTOR;
if (new_cap > DECOMPRESS_MAX_BYTES) {
if (new_cap > g_cli_decompress_max) {
free(decompressed);
inflateEnd(&strm);
return NULL;
+4
View File
@@ -480,6 +480,10 @@ unsigned char *cbm_extract_binary_from_targz(const unsigned char *data, int data
* Returns NULL on error. Caller must free. */
unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_len, int *out_len);
/* Lower the limit a tar.gz archive may decompress to, so a test can pin the
* refusal with a small archive. 0 restores the 500 MiB limit. */
void cbm_cli_set_decompress_max_for_test(size_t max_bytes);
#endif /* CBM_CLI_ENABLE_TEST_API */
/* ── Index management ─────────────────────────────────────────── */
+339 -3
View File
@@ -77,6 +77,7 @@ enum {
#include "foundation/mem.h"
#include "foundation/mem_core.h"
#include "foundation/hash_table.h"
#include "foundation/dyn_array.h"
#include "foundation/json_writer.h"
#include "foundation/arena.h"
#include "foundation/diagnostics.h"
@@ -640,6 +641,9 @@ static const tool_def_t TOOLS[] = {
"topology: infra objects, routes, controllers (from a Resource/Route); deploy: what CI "
"jobs and scripts run and deploy; run: what a Service runs (image, Dockerfile, program, "
"env).\"},"
"\"include_overrides\":{\"type\":\"boolean\",\"default\":false,"
"\"description\":\"For outbound calls, list OVERRIDE implementations separately as possible "
"callees. Declared CALLS edges remain unchanged.\"},"
"\"parameter_name\":{\"type\":\"string\",\"description\":\"data_flow parameter filter.\"},"
"\"edge_types\":{\"type\":\"array\",\"items\":{"
"\"type\":\"string\"}},\"risk_labels\":{\"type\":\"boolean\",\"default\":false,"
@@ -9887,6 +9891,284 @@ static int bfs_union_same_name(cbm_store_t *store, const cbm_node_t *nodes, int
return CBM_STORE_OK;
}
/* include_overrides (#1271): the possible runtime callees of the outbound rows
* a page emits. A callee row is a contract when an implementation overrides
* it. The indexer links each override to its nearest declaring ancestor only
* (pass_semantic), so the walk follows inbound OVERRIDE edges transitively --
* one store query per node reached, with a visited set -- and lists C.m for a
* call to A.m when C : B : A. Only callables are contracts or implementations:
* an OVERRIDE edge between infra objects (tfvars values, kustomize overlays)
* is not a call. Nothing is written; the declared CALLS rows stay what they
* are. TRACE_OVERRIDE_WALK_CAP bounds the nodes one response may reach, and
* hitting it is reported, never silent. */
enum { TRACE_OVERRIDE_WALK_CAP = MCP_BFS_LIMIT_MAX };
typedef struct {
int contract; /* index of the contract row in the outbound traversal */
cbm_node_t impl; /* owned strings, released by trace_possibles_free */
} trace_possible_t;
typedef CBM_DYN_ARRAY(int64_t) trace_id_array_t;
typedef struct {
CBM_DYN_ARRAY(trace_possible_t) items;
bool truncated; /* TRACE_OVERRIDE_WALK_CAP stopped the walk */
bool failed; /* a store lookup failed; the list may be incomplete */
} trace_possibles_t;
static bool trace_label_is_callable(const char *label) {
return label && (strcmp(label, "Function") == 0 || strcmp(label, "Method") == 0);
}
static bool trace_id_seen(const trace_id_array_t *ids, int64_t id) {
for (int i = 0; i < ids->count; i++) {
if (ids->items[i] == id) {
return true;
}
}
return false;
}
static const char *trace_node_qn(const cbm_node_t *n) {
return n->qualified_name ? n->qualified_name : (n->name ? n->name : "");
}
/* Contract row order (the page's own order), then implementation QN. */
static int trace_possible_cmp(const void *pa, const void *pb) {
const trace_possible_t *a = pa;
const trace_possible_t *b = pb;
if (a->contract != b->contract) {
return a->contract < b->contract ? -1 : 1;
}
int c = strcmp(trace_node_qn(&a->impl), trace_node_qn(&b->impl));
if (c != 0) {
return c;
}
return (a->impl.id > b->impl.id) - (a->impl.id < b->impl.id);
}
static void trace_possibles_free(trace_possibles_t *p) {
for (int i = 0; i < p->items.count; i++) {
free_node_contents(&p->items.items[i].impl);
}
cbm_da_free(&p->items);
}
/* One reached node: recorded in `seen`; a callable is walked further and,
* unless it lives in a test file the trace hides, listed. */
static void trace_possibles_visit(cbm_store_t *store, int contract, int64_t id, bool include_tests,
trace_id_array_t *seen, trace_id_array_t *frontier,
trace_possibles_t *out) {
cbm_da_push(seen, id);
cbm_node_t impl = {0};
if (cbm_store_find_node_by_id(store, id, &impl) != CBM_STORE_OK) {
free_node_contents(&impl);
out->failed = true;
return;
}
if (!trace_label_is_callable(impl.label)) {
free_node_contents(&impl);
return;
}
cbm_da_push(frontier, id);
if (include_tests || !is_test_file(impl.file_path)) {
trace_possible_t item = {.contract = contract, .impl = impl};
int before = out->items.count;
cbm_da_push(&out->items, item);
if (out->items.count == before) { /* allocation failed: not listed */
free_node_contents(&impl);
out->failed = true;
}
} else {
free_node_contents(&impl);
}
}
/* Whether a traced call can run an override of its declared target: it
* goes through an instance receiver (`self.m`, `obj.m`, `$this->m`). A bare
* call (a function, or a constructor that runs its own class), a `::` path, a
* type or constructed-object root (`Cls.m`, `Factory().m`, `new X().m`), and a
* super/base call (bound to the parent) run the declared target itself.
* Callee text the edge does not carry counts as dispatching: the list says
* "possible". */
static bool trace_call_dispatches(const char *props_json) {
yyjson_doc *doc = props_json ? yyjson_read(props_json, strlen(props_json), 0) : NULL;
const char *callee =
doc ? yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), "callee")) : NULL;
bool dispatches = true;
if (callee && callee[0]) {
const char *dot = NULL;
int depth = 0;
for (const char *c = callee; *c && !dot; c++) {
if (*c == '(' || *c == '[' || *c == '<') {
depth++;
} else if ((*c == ')' || *c == ']' || *c == '>') && depth > 0) {
depth--;
} else if (*c == '.' && depth == 0) {
dot = c;
}
}
const char *colons = strstr(callee, "::");
const char *root = callee;
while (*root == '$') {
root++;
}
size_t root_len = 0;
while (root[root_len] && root[root_len] != '.' && root[root_len] != '(' &&
root[root_len] != '[' && root[root_len] != '<') {
root_len++;
}
bool super_root = (root_len == strlen("super") && strncmp(root, "super", root_len) == 0) ||
(root_len == strlen("base") && strncmp(root, "base", root_len) == 0);
dispatches = dot && !(colons && colons < dot) &&
strncmp(root, "new ", strlen("new ")) != 0 && !super_root &&
!(root[0] >= 'A' && root[0] <= 'Z');
}
if (doc) {
yyjson_doc_free(doc);
}
return dispatches;
}
static int trace_id_cmp(const void *pa, const void *pb) {
int64_t a = *(const int64_t *)pa;
int64_t b = *(const int64_t *)pb;
return (a > b) - (a < b);
}
static bool trace_ids_contain(const trace_id_array_t *sorted, int64_t id) {
return sorted->count > 0 && bsearch(&id, sorted->items, (size_t)sorted->count,
sizeof(sorted->items[0]), trace_id_cmp) != NULL;
}
/* A contract's implementations are listed only when some call that reaches it
* from the traced graph (`traced`: the roots and the outbound rows, sorted)
* dispatches. */
static bool trace_contract_dispatched(cbm_store_t *store, int64_t contract_id,
const trace_id_array_t *traced, trace_possibles_t *out) {
cbm_edge_t *edges = NULL;
int edge_count = 0;
if (cbm_store_find_edges_by_target_type(store, contract_id, "CALLS", &edges, &edge_count) !=
CBM_STORE_OK) {
cbm_store_free_edges(edges, edge_count);
out->failed = true;
return false;
}
bool dispatched = false;
for (int i = 0; i < edge_count && !dispatched; i++) {
dispatched = trace_ids_contain(traced, edges[i].source_id) &&
trace_call_dispatches(edges[i].properties_json);
}
cbm_store_free_edges(edges, edge_count);
return dispatched;
}
/* Every implementation below one contract, through inbound OVERRIDE edges. */
static void trace_possibles_walk(cbm_store_t *store, int contract, int64_t contract_id,
const trace_id_array_t *traced, bool include_tests,
int *walk_budget, trace_possibles_t *out) {
trace_id_array_t seen = {0};
trace_id_array_t frontier = {0};
cbm_da_push(&seen, contract_id);
cbm_da_push(&frontier, contract_id);
for (int head = 0; head < frontier.count && !out->truncated; head++) {
cbm_edge_t *edges = NULL;
int edge_count = 0;
if (cbm_store_find_edges_by_target_type(store, frontier.items[head], "OVERRIDE", &edges,
&edge_count) != CBM_STORE_OK) {
cbm_store_free_edges(edges, edge_count);
out->failed = true;
break;
}
if (head == 0 && edge_count > 0 &&
!trace_contract_dispatched(store, contract_id, traced, out)) {
cbm_store_free_edges(edges, edge_count);
break;
}
for (int e = 0; e < edge_count; e++) {
if (trace_id_seen(&seen, edges[e].source_id)) {
continue;
}
if (*walk_budget <= 0) {
out->truncated = true;
break;
}
(*walk_budget)--;
trace_possibles_visit(store, contract, edges[e].source_id, include_tests, &seen,
&frontier, out);
}
cbm_store_free_edges(edges, edge_count);
}
cbm_da_free(&seen);
cbm_da_free(&frontier);
}
/* The possible callees of rows [start, start + len) of the outbound leg. */
static void trace_possibles_for_page(cbm_store_t *store, const cbm_node_t *roots, int root_count,
const cbm_traverse_result_t *tr, int start, int len,
bool include_tests, trace_possibles_t *out) {
int walk_budget = TRACE_OVERRIDE_WALK_CAP;
trace_id_array_t traced = {0};
for (int i = 0; i < root_count; i++) {
cbm_da_push(&traced, roots[i].id);
}
for (int i = 0; i < tr->visited_count; i++) {
cbm_da_push(&traced, tr->visited[i].node.id);
}
if (traced.count > 1) {
qsort(traced.items, (size_t)traced.count, sizeof(traced.items[0]), trace_id_cmp);
}
for (int i = start; i < start + len && i < tr->visited_count && !out->truncated; i++) {
const cbm_node_t *contract = &tr->visited[i].node;
if (trace_label_is_callable(contract->label)) {
trace_possibles_walk(store, i, contract->id, &traced, include_tests, &walk_budget, out);
}
}
cbm_da_free(&traced);
if (out->items.count > 1) {
qsort(out->items.items, (size_t)out->items.count, sizeof(out->items.items[0]),
trace_possible_cmp);
}
}
/* JSON: [{contract, implementations: [qn, ...]}], one entry per contract. The
* strings are borrowed: the traversal and the list outlive serialization. */
static yyjson_mut_val *trace_possibles_json(yyjson_mut_doc *doc, const cbm_traverse_result_t *tr,
const trace_possibles_t *p) {
yyjson_mut_val *arr = yyjson_mut_arr(doc);
yyjson_mut_val *implementations = NULL;
int current = CBM_NOT_FOUND;
for (int i = 0; i < p->items.count; i++) {
const trace_possible_t *item = &p->items.items[i];
if (item->contract != current) {
current = item->contract;
yyjson_mut_val *entry = yyjson_mut_obj(doc);
yyjson_mut_obj_add_str(doc, entry, "contract",
trace_node_qn(&tr->visited[current].node));
implementations = yyjson_mut_arr(doc);
yyjson_mut_obj_add_val(doc, entry, "implementations", implementations);
yyjson_mut_arr_add_val(arr, entry);
}
yyjson_mut_arr_add_str(doc, implementations, trace_node_qn(&item->impl));
}
return arr;
}
/* Tree: one table row per (contract, implementation), like every section. */
static void trace_possibles_tree(cbm_sb_t *sb, const cbm_traverse_result_t *tr,
const trace_possibles_t *p) {
static const char *const cols[] = {"contract", "implementation"};
cbm_tree_table_header(sb, "possible_callees", p->items.count, cols,
(int)(sizeof(cols) / sizeof(cols[0])));
for (int i = 0; i < p->items.count; i++) {
const trace_possible_t *item = &p->items.items[i];
cbm_tree_row_begin(sb);
cbm_tree_cell_str(sb, trace_node_qn(&tr->visited[item->contract].node), true);
cbm_tree_cell_str(sb, trace_node_qn(&item->impl), false);
cbm_tree_row_end(sb);
}
}
/* ── Pagination cursors (stateless, exactly-once) ────────────────────
* Token: "c1.<leg>.<generation>.<qhash>.<hop>.<id>" — version, trace leg
* (o=callees, i=callers), the store generation (per-DB uid + mutation
@@ -9919,7 +10201,7 @@ typedef struct {
static uint64_t trace_params_hash(const char *project, const char *func_name, const char *direction,
const char *mode, const char *param_name, int depth,
bool include_tests, bool risk_labels, bool include_evidence,
int limit, const char *args) {
bool include_overrides, int limit, const char *args) {
uint64_t h = 0xcbf29ce484222325ULL;
h = cursor_fnv1a64(project ? project : "", h);
h = cursor_fnv1a64("|", h);
@@ -9938,6 +10220,12 @@ static uint64_t trace_params_hash(const char *project, const char *func_name, co
snprintf(nums, sizeof(nums), "|%d|%d|%d|%d|%d", depth, include_tests ? 1 : 0,
risk_labels ? 1 : 0, include_evidence ? 1 : 0, limit);
h = cursor_fnv1a64(nums, h);
/* include_overrides adds an optional section and never changes the rows.
* It joins the identity only when set, so a cursor minted without it is
* the token minted before the flag existed. */
if (include_overrides) {
h = cursor_fnv1a64("|include_overrides", h);
}
/* Explicit edge types define the traversed graph. Omitting them from the
* cursor identity allowed a token minted for CALLS to resume an IMPORTS
@@ -10628,6 +10916,7 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
(size_t)max_output_tokens * (size_t)MCP_OUTPUT_BYTES_PER_TOKEN_ESTIMATE;
bool risk_labels = cbm_mcp_get_bool_arg(args, "risk_labels");
bool include_tests = cbm_mcp_get_bool_arg(args, "include_tests");
bool include_overrides = cbm_mcp_get_bool_arg(args, "include_overrides");
/* Off by default: two extra columns on every row is exactly the kind of
* inflation the tree format exists to avoid. Opt in when you need to judge
* whether an edge is trustworthy. */
@@ -10691,7 +10980,7 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
if (cursor_arg && cursor_arg[0]) {
uint64_t qh = trace_params_hash(project, func_name, direction ? direction : "both", mode,
param_name, depth, include_tests, risk_labels,
include_evidence, trace_limit, args);
include_evidence, include_overrides, trace_limit, args);
const char *cerr = trace_cursor_decode(cursor_arg, generation, qh, &cur);
if (cerr) {
free(cursor_arg);
@@ -10940,6 +11229,16 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
break;
}
}
/* include_overrides: the possible callees of the outbound rows this page
* requests, computed once. They are optional output: under the byte
* ceiling they yield before every other field and every graph row. */
trace_possibles_t possibles = {0};
bool emit_possibles = include_overrides && do_outbound && trace_includes_calls;
bool possibles_omitted = false;
if (emit_possibles) {
trace_possibles_for_page(store, nodes, node_count, &tr_out, out_start, requested_out_len,
include_tests, &possibles);
}
bool unresolved_out = false;
bool unresolved_in = false;
trace_coverage_scope_t coverage_scope = {.roots = nodes,
@@ -10975,7 +11274,7 @@ render_trace_output:;
snprintf(nc.generation, sizeof(nc.generation), "%s", generation);
nc.qhash =
trace_params_hash(project, func_name, direction, mode, param_name, depth, include_tests,
risk_labels, include_evidence, trace_limit, args);
risk_labels, include_evidence, include_overrides, trace_limit, args);
/* The watermark is the last row ACTUALLY emitted, not the leg that
* happens to have additional rows. At an exact outbound page boundary
* inbound may be pending with zero emitted rows. */
@@ -11053,6 +11352,15 @@ render_trace_output:;
render_evidence, &out_edge_ctx);
}
}
if (emit_possibles) {
trace_possibles_tree(&sb, &tr_out, &possibles);
if (possibles.truncated) {
cbm_tree_scalar_bool(&sb, "possible_callees_truncated", true);
}
if (possibles.failed) {
cbm_tree_scalar_bool(&sb, "possible_callees_incomplete", true);
}
}
if (do_inbound) {
cbm_tree_scalar_int(&sb, "callers_total", in_total);
cbm_tree_scalar_str(&sb, "callers_total_relation", in_relation);
@@ -11081,6 +11389,9 @@ render_trace_output:;
trace_emit_omitted_optional_fields_tree(&sb, risk_labels, data_flow,
include_evidence);
}
if (possibles_omitted) {
cbm_tree_scalar_bool(&sb, "possible_callees_omitted", true);
}
if (engine_saturated) {
cbm_tree_scalar_bool(&sb, "engine_saturated", true);
}
@@ -11129,6 +11440,16 @@ render_trace_output:;
data_flow && emit_optional_fields,
include_evidence && emit_optional_fields, &out_edge_ctx));
}
if (emit_possibles) {
yyjson_mut_obj_add_val(doc, root, "possible_callees",
trace_possibles_json(doc, &tr_out, &possibles));
if (possibles.truncated) {
yyjson_mut_obj_add_bool(doc, root, "possible_callees_truncated", true);
}
if (possibles.failed) {
yyjson_mut_obj_add_bool(doc, root, "possible_callees_incomplete", true);
}
}
if (do_inbound) {
yyjson_mut_obj_add_int(doc, root, "callers_total", in_total);
yyjson_mut_obj_add_str(doc, root, "callers_total_relation", in_relation);
@@ -11155,6 +11476,9 @@ render_trace_output:;
trace_emit_omitted_optional_fields_json(doc, root, risk_labels, data_flow,
include_evidence);
}
if (possibles_omitted) {
yyjson_mut_obj_add_bool(doc, root, "possible_callees_omitted", true);
}
if (engine_saturated) {
yyjson_mut_obj_add_bool(doc, root, "engine_saturated", true);
}
@@ -11176,6 +11500,11 @@ render_trace_output:;
* render, so `next` always follows the last row actually emitted. */
if (json && strlen(json) > output_budget_bytes) {
output_budget_hit = true;
if (emit_possibles && possibles.items.count > 0) {
emit_possibles = false;
possibles_omitted = true;
goto render_trace_output;
}
if (emit_optional_fields) {
emit_optional_fields = false;
optional_fields_omitted = true;
@@ -11209,6 +11538,9 @@ render_trace_output:;
trace_emit_omitted_optional_fields_tree(&floor, risk_labels, data_flow,
include_evidence);
}
if (possibles_omitted) {
cbm_tree_scalar_bool(&floor, "possible_callees_omitted", true);
}
cbm_tree_scalar_str(&floor, "hint",
"raise max_output_tokens; no identifier was sliced");
json = cbm_sb_finish(&floor);
@@ -11240,6 +11572,9 @@ render_trace_output:;
trace_emit_omitted_optional_fields_json(floor_doc, floor, risk_labels,
data_flow, include_evidence);
}
if (possibles_omitted) {
yyjson_mut_obj_add_bool(floor_doc, floor, "possible_callees_omitted", true);
}
yyjson_mut_obj_add_str(floor_doc, floor, "hint",
"raise max_output_tokens; no identifier was sliced");
json = yy_doc_to_str(floor_doc);
@@ -11291,6 +11626,7 @@ trace_output_ready:
free(direction);
free(mode);
free(param_name);
trace_possibles_free(&possibles);
if (et_doc_keep) {
yyjson_doc_free(et_doc_keep);
}
+2
View File
@@ -1630,6 +1630,7 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
/* Resolve each call */
cbm_external_import_index_t ext_imports = {0};
cbm_spawn_file_begin(result->imports.items, result->imports.count);
for (int c = 0; c < result->calls.count; c++) {
CBMCall *call = &result->calls.items[c];
if (!call->callee_name) {
@@ -1646,6 +1647,7 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
unresolved++;
}
}
cbm_spawn_file_end();
cbm_pipeline_external_import_index_free(&ext_imports);
cbm_pipeline_record_unresolved_calls(ctx->pipeline, rel, result, evidence);
+27 -16
View File
@@ -18,6 +18,7 @@
#include "pipeline/lsp_surface.h"
#include "foundation/mem_core.h"
#include "result_spill.h"
#include "resolve_stats.h"
#include "pipeline/pipeline_internal.h"
#include "pipeline/lsp_resolve.h"
#include "callable_sig.h"
@@ -759,23 +760,36 @@ static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src, const ch
* "Class" for a C struct or union. */
static void pxc_fold_struct_fields(CBMArena *arena, const CBMFileResult *result, CBMLSPDef *defs,
int start, int end, const char *type_label) {
if (!arena || !result || !defs || start >= end) {
if (!arena || !result || !defs || start >= end || result->defs.count <= 0) {
return;
}
for (int si = start; si < end; si++) {
/* A generated file holds hundreds of types and thousands of fields: each type's
* fields are one chain, in definition order, instead of a scan of the file per type.
* next_field[di] is the 1-based index of the field after di of the same owner. */
int *next_field =
cbm_alloc(CBM_MEM_CLASS_EXTRACT, (size_t)result->defs.count * sizeof(*next_field));
CBMHashTable *fields_of = cbm_ht_create_in(CBM_MEM_CLASS_EXTRACT, 32);
bool joined = next_field && fields_of;
for (int di = result->defs.count - 1; joined && di >= 0; di--) {
const CBMDefinition *fd = &result->defs.items[di];
if (!fd->label || !fd->parent_class || !fd->name || !fd->name[0] || !fd->return_type ||
!fd->return_type[0] || strcmp(fd->label, "Field") != 0) {
continue;
}
next_field[di] =
(int)(intptr_t)cbm_ht_set(fields_of, fd->parent_class, (void *)(intptr_t)(di + 1));
}
for (int si = start; joined && si < end; si++) {
CBMLSPDef *dst = &defs[si];
if (!dst->label || strcmp(dst->label, type_label) != 0 || !dst->qualified_name) {
continue;
}
int count = 0;
size_t total = 0; /* "name:type" bytes; separators and NUL added below */
for (int di = 0; di < result->defs.count; di++) {
const CBMDefinition *fd = &result->defs.items[di];
if (!fd->label || !fd->parent_class || !fd->name || !fd->name[0] || !fd->return_type ||
!fd->return_type[0] || strcmp(fd->label, "Field") != 0 ||
strcmp(fd->parent_class, dst->qualified_name) != 0) {
continue;
}
int first_field = (int)(intptr_t)cbm_ht_get(fields_of, dst->qualified_name);
for (int link = first_field; link; link = next_field[link - 1]) {
cbm_resolve_stats_add(CBM_RESOLVE_STAT_LOOKUP_PROBES, 1);
const CBMDefinition *fd = &result->defs.items[link - 1];
total += strlen(fd->name) + 1 + strlen(fd->return_type);
count++;
}
@@ -790,13 +804,8 @@ static void pxc_fold_struct_fields(CBMArena *arena, const CBMFileResult *result,
}
char *p = buf;
int written = 0;
for (int di = 0; di < result->defs.count; di++) {
const CBMDefinition *fd = &result->defs.items[di];
if (!fd->label || !fd->parent_class || !fd->name || !fd->name[0] || !fd->return_type ||
!fd->return_type[0] || strcmp(fd->label, "Field") != 0 ||
strcmp(fd->parent_class, dst->qualified_name) != 0) {
continue;
}
for (int link = first_field; link; link = next_field[link - 1]) {
const CBMDefinition *fd = &result->defs.items[link - 1];
size_t n = strlen(fd->name);
memcpy(p, fd->name, n);
p += n;
@@ -812,6 +821,8 @@ static void pxc_fold_struct_fields(CBMArena *arena, const CBMFileResult *result,
*p = '\0';
dst->field_defs = buf;
}
cbm_ht_free(fields_of);
cbm_free(CBM_MEM_CLASS_EXTRACT, next_field);
}
/* Python annotation text -> the one type name it declares, or NULL. Strips a
+2
View File
@@ -3352,6 +3352,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
cbm_pipeline_lsp_field_index_t field_index = {0};
bool field_index_built = false;
bool field_index_ready = false;
cbm_spawn_file_begin(result->imports.items, result->imports.count);
for (int c = 0; c < result->calls.count; c++) {
const CBMCall *call = &result->calls.items[c];
CBMCallEvidence fallback = {0};
@@ -3831,6 +3832,7 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
cbm_ht_foreach(lsp_legacy_idx, lsp_idx_free_key, NULL);
cbm_ht_free(lsp_legacy_idx);
}
cbm_spawn_file_end();
cbm_pipeline_external_import_index_free(&ext_imports);
cbm_pipeline_lsp_field_index_free(&field_index);
}
+57 -41
View File
@@ -26,6 +26,7 @@
#include "foundation/compat_fs.h"
#include "foundation/constants.h"
#include "foundation/log.h"
#include "foundation/mem_core.h"
#include "foundation/platform.h"
#include <stdbool.h>
@@ -45,8 +46,22 @@ enum {
CBM_PATH_ALIAS_MAX_DEPTH = 32, /* directory recursion depth */
};
/* Everything a collection owns comes from the memory core in this class and
* goes back through cbm_path_alias_collection_free. */
#define ALIAS_MEM CBM_MEM_CLASS_OTHER
/* ── Helpers ───────────────────────────────────────────────────── */
/* Copy the first length bytes of text, which holds no NUL before them. */
static char *alias_strndup(const char *text, size_t length) {
char *copy = cbm_alloc(ALIAS_MEM, length + 1);
if (copy) {
memcpy(copy, text, length);
copy[length] = '\0';
}
return copy;
}
/* Strip .ts/.tsx/.js/.jsx in place. Returns its argument. */
static char *strip_resolved_ext(char *path) {
if (!path) {
@@ -82,7 +97,7 @@ static char *resolve_target_relative(const char *dir_prefix, const char *target)
}
size_t dp_len = (dir_prefix && dir_prefix[0] != '\0') ? strlen(dir_prefix) : 0;
size_t t_len = strlen(target);
char *buf = malloc(dp_len + t_len + 2);
char *buf = cbm_alloc(ALIAS_MEM, dp_len + t_len + 2);
if (!buf) {
return NULL;
}
@@ -183,7 +198,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
fclose(f);
return NULL;
}
char *buf = malloc((size_t)len + 1);
char *buf = cbm_alloc(ALIAS_MEM, (size_t)len + 1);
if (!buf) {
fclose(f);
return NULL;
@@ -194,7 +209,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
yyjson_read_flag flg = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
yyjson_doc *doc = yyjson_read(buf, nread, flg);
free(buf);
cbm_free(ALIAS_MEM, buf);
if (!doc) {
return NULL;
}
@@ -212,7 +227,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
return NULL;
}
cbm_path_alias_map_t *map = calloc(1, sizeof(*map));
cbm_path_alias_map_t *map = cbm_calloc(ALIAS_MEM, sizeof(*map));
if (!map) {
yyjson_doc_free(doc);
return NULL;
@@ -222,7 +237,7 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
map->base_url = resolve_target_relative(dir_prefix, base_url_str);
} else if (base_url_str && strcmp(base_url_str, ".") == 0 && dir_prefix &&
dir_prefix[0] != '\0') {
map->base_url = strdup(dir_prefix);
map->base_url = cbm_mem_strdup(ALIAS_MEM, dir_prefix);
}
if (paths_obj && yyjson_is_obj(paths_obj)) {
@@ -230,10 +245,10 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
bool capped = obj_size > CBM_PATH_ALIAS_MAX_ENTRIES;
int capacity = (int)(capped ? (size_t)CBM_PATH_ALIAS_MAX_ENTRIES : obj_size);
if (capacity > 0) {
map->entries = calloc((size_t)capacity, sizeof(cbm_path_alias_t));
map->entries = cbm_calloc(ALIAS_MEM, (size_t)capacity * sizeof(cbm_path_alias_t));
if (!map->entries) {
free(map->base_url);
free(map);
cbm_free(ALIAS_MEM, map->base_url);
cbm_free(ALIAS_MEM, map);
yyjson_doc_free(doc);
return NULL;
}
@@ -254,22 +269,22 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
if (star) {
entry->has_wildcard = true;
entry->alias_prefix =
cbm_strndup(alias_pattern, (size_t)(star - alias_pattern));
entry->alias_suffix = strdup(star + 1);
alias_strndup(alias_pattern, (size_t)(star - alias_pattern));
entry->alias_suffix = cbm_mem_strdup(ALIAS_MEM, star + 1);
} else {
entry->has_wildcard = false;
entry->alias_prefix = strdup(alias_pattern);
entry->alias_suffix = strdup("");
entry->alias_prefix = cbm_mem_strdup(ALIAS_MEM, alias_pattern);
entry->alias_suffix = cbm_mem_strdup(ALIAS_MEM, "");
}
const char *tstar = strchr(target_pattern, '*');
if (tstar) {
char *pre = cbm_strndup(target_pattern, (size_t)(tstar - target_pattern));
char *pre = alias_strndup(target_pattern, (size_t)(tstar - target_pattern));
entry->target_prefix = resolve_target_relative(dir_prefix, pre);
free(pre);
entry->target_suffix = strdup(tstar + 1);
cbm_free(ALIAS_MEM, pre);
entry->target_suffix = cbm_mem_strdup(ALIAS_MEM, tstar + 1);
} else {
entry->target_prefix = resolve_target_relative(dir_prefix, target_pattern);
entry->target_suffix = strdup("");
entry->target_suffix = cbm_mem_strdup(ALIAS_MEM, "");
}
map->count++;
}
@@ -294,14 +309,14 @@ static void free_alias_map(cbm_path_alias_map_t *map) {
return;
}
for (int j = 0; j < map->count; j++) {
free(map->entries[j].alias_prefix);
free(map->entries[j].alias_suffix);
free(map->entries[j].target_prefix);
free(map->entries[j].target_suffix);
cbm_free(ALIAS_MEM, map->entries[j].alias_prefix);
cbm_free(ALIAS_MEM, map->entries[j].alias_suffix);
cbm_free(ALIAS_MEM, map->entries[j].target_prefix);
cbm_free(ALIAS_MEM, map->entries[j].target_suffix);
}
free(map->entries);
free(map->base_url);
free(map);
cbm_free(ALIAS_MEM, map->entries);
cbm_free(ALIAS_MEM, map->base_url);
cbm_free(ALIAS_MEM, map);
}
void cbm_path_alias_collection_free(cbm_path_alias_collection_t *coll) {
@@ -309,12 +324,12 @@ void cbm_path_alias_collection_free(cbm_path_alias_collection_t *coll) {
return;
}
for (int i = 0; i < coll->count; i++) {
free(coll->scopes[i].dir_prefix);
free(coll->scopes[i].source_rel_path);
cbm_free(ALIAS_MEM, coll->scopes[i].dir_prefix);
cbm_free(ALIAS_MEM, coll->scopes[i].source_rel_path);
free_alias_map(coll->scopes[i].map);
}
free(coll->scopes);
free(coll);
cbm_free(ALIAS_MEM, coll->scopes);
cbm_free(ALIAS_MEM, coll);
}
bool cbm_path_alias_resolve_into(const cbm_path_alias_map_t *map, const char *module_path,
@@ -367,6 +382,7 @@ bool cbm_path_alias_resolve_into(const cbm_path_alias_map_t *map, const char *mo
return true;
}
/* The result stays on the C allocator: its callers release it with free(). */
char *cbm_path_alias_resolve(const cbm_path_alias_map_t *map, const char *module_path) {
char buf[CBM_PATH_ALIAS_RESULT_MAX];
return cbm_path_alias_resolve_into(map, module_path, buf, sizeof(buf)) ? strdup(buf) : NULL;
@@ -450,7 +466,7 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
if (!repo_path) {
return NULL;
}
alias_config_hit_t *hits = calloc(CBM_PATH_ALIAS_MAX_FILES, sizeof(*hits));
alias_config_hit_t *hits = cbm_calloc(ALIAS_MEM, CBM_PATH_ALIAS_MAX_FILES * sizeof(*hits));
if (!hits) {
return NULL;
}
@@ -461,19 +477,19 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
cbm_log_warn("path_alias.files.cap_hit", "repo", repo_path, "kept", "256_of_more");
}
if (count == 0) {
free(hits);
cbm_free(ALIAS_MEM, hits);
return NULL;
}
cbm_path_alias_collection_t *coll = calloc(1, sizeof(*coll));
cbm_path_alias_collection_t *coll = cbm_calloc(ALIAS_MEM, sizeof(*coll));
if (!coll) {
free(hits);
cbm_free(ALIAS_MEM, hits);
return NULL;
}
coll->scopes = calloc((size_t)count, sizeof(cbm_path_alias_scope_t));
coll->scopes = cbm_calloc(ALIAS_MEM, (size_t)count * sizeof(cbm_path_alias_scope_t));
if (!coll->scopes) {
free(coll);
free(hits);
cbm_free(ALIAS_MEM, coll);
cbm_free(ALIAS_MEM, hits);
return NULL;
}
@@ -490,11 +506,11 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
} else {
snprintf(selected_rel, sizeof(selected_rel), "%s", base);
}
char *dir_prefix = strdup(hits[i].rel);
char *source_rel_path = strdup(selected_rel);
char *dir_prefix = cbm_mem_strdup(ALIAS_MEM, hits[i].rel);
char *source_rel_path = cbm_mem_strdup(ALIAS_MEM, selected_rel);
if (!dir_prefix || !source_rel_path) {
free(dir_prefix);
free(source_rel_path);
cbm_free(ALIAS_MEM, dir_prefix);
cbm_free(ALIAS_MEM, source_rel_path);
free_alias_map(map);
continue;
}
@@ -503,11 +519,11 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
coll->scopes[coll->count].map = map;
coll->count++;
}
free(hits);
cbm_free(ALIAS_MEM, hits);
if (coll->count == 0) {
free(coll->scopes);
free(coll);
cbm_free(ALIAS_MEM, coll->scopes);
cbm_free(ALIAS_MEM, coll);
return NULL;
}
+532 -15
View File
@@ -472,6 +472,7 @@ typedef struct {
cbm_resolution_t res;
size_t scope_len; /* raw and Python-scoped requests may share one file cache */
bool skip_members; /* a C call and a C reference to the same name share it too */
bool calls_path; /* a call and a reference to the same name share it too */
bool cs_keywords; /* a C# call and a C# reference to the same name share it too */
bool caller_test; /* a test and a production function of one file share it too */
} resolve_cache_entry_t;
@@ -2405,10 +2406,11 @@ static bool cs_type_keyword_root(const char *dotted) {
* appears somewhere in the chain. Calendar.utcGregorian.startOfDayUTC resolving
* to AuthDTOs.Calendar.startOfDayUTC passes, because Calendar is in the chain.
*
* Only an upper-case first segment is guarded. A lower-case root names a value
* (vm.load, http.Get, os.path.join) whose declared type the chain does not
* show, so the chain proves nothing there and the call passes through
* unchanged. A callee with no separator passes through as well.
* Only an upper-case first segment is guarded here. A lower-case root names a
* value (vm.load, http.Get, os.path.join) whose declared type the chain does
* not show, so the chain proves nothing there and the call passes through
* unchanged; on the CALLS path value_receiver_blind judges it instead. A
* callee with no separator passes through as well.
*
* One exception, for C# CALLS only (#2121): C# spells its built-in types as
* lower-case keywords, so in `int.TryParse` or `string.Join` the root names a
@@ -2497,6 +2499,495 @@ static bool receiver_chain_admits(const char *callee_name, const char *candidate
return false;
}
/* A receiver ROOT that is the calling object or its class hierarchy: a call
* through it names a member the caller's own class defines or inherits, so
* its receiver is not unknown. `this_` and `_this` are the local aliases of
* `this` generated protobuf code and compiled JavaScript use. Lower-case
* spellings only; an upper-case root (Self, Me, MyBase) stays with
* receiver_chain_admits as before. */
static bool self_receiver_root(const char *root, size_t len) {
static const char *const roots[] = {"this", "self", "cls", "super", "base",
"$this", "$self", "this_", "_this"};
for (size_t i = 0; i < sizeof(roots) / sizeof(roots[0]); i++) {
if (strlen(roots[i]) == len && strncmp(root, roots[i], len) == 0) {
return true;
}
}
static const char labelled[] = "this@"; /* Kotlin's this@Outer */
return len > sizeof(labelled) - SKIP_ONE && strncmp(root, labelled, sizeof(labelled) - 1) == 0;
}
static bool ascii_is_upper(char c) {
return c >= 'A' && c <= 'Z';
}
static char ascii_upper(char c) {
return c >= 'a' && c <= 'z' ? (char)(c - ('a' - 'A')) : c;
}
static bool ascii_is_space(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
/* One receiver-chain segment, normalized for naming: surrounding whitespace,
* a constructor's `new `, a call's or an index's trailing `(...)` / `[...]`
* dropped, then a field prefix `_` / `m_` and a trailing number
* (`context2`). */
typedef struct {
const char *text;
size_t len;
} gate_seg_t;
static gate_seg_t gate_segment(const char *seg, size_t len) {
while (len > 0 && ascii_is_space(seg[0])) {
seg++;
len--;
}
static const char ctor[] = "new ";
if (len > sizeof(ctor) - SKIP_ONE && strncmp(seg, ctor, sizeof(ctor) - SKIP_ONE) == 0) {
seg += sizeof(ctor) - SKIP_ONE;
len -= sizeof(ctor) - SKIP_ONE;
}
for (size_t i = 0; i < len; i++) {
if (seg[i] == '(' || seg[i] == '[' || seg[i] == '<') {
len = i;
break;
}
}
while (len > 0 && ascii_is_space(seg[len - 1])) {
len--;
}
if (len > 0 && (seg[0] == '_' || seg[0] == '$')) {
seg++;
len--;
}
if (len > 2 && seg[0] == 'm' && seg[1] == '_') {
seg += 2;
len -= 2;
}
while (len > 1 && seg[len - 1] >= '0' && seg[len - 1] <= '9') {
len--;
}
return (gate_seg_t){seg, len};
}
/* A name split into words at `_` / `-` and at camel-case boundaries:
* fetchSize -> fetch|size, JSONParser -> JSON|parser, flagd_ui -> flagd|ui,
* otel_guard -> otel|guard. Compared case-blind, word by word. */
enum { GATE_WORDS_MAX = 16, GATE_WORDS_MIN_CHARS = 3 };
typedef struct {
const char *at[GATE_WORDS_MAX];
size_t len[GATE_WORDS_MAX];
int n;
size_t chars;
} gate_words_t;
static bool ascii_is_lower_or_digit(char c) {
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
}
static bool gate_word_sep(char c) {
return c == '_' || c == '-' || c == '$';
}
static void gate_words(const char *s, size_t len, gate_words_t *out) {
out->n = 0;
out->chars = 0;
size_t i = 0;
while (i < len && out->n < GATE_WORDS_MAX) {
while (i < len && gate_word_sep(s[i])) {
i++;
}
if (i >= len) {
break;
}
size_t start = i++;
while (i < len && !gate_word_sep(s[i])) {
bool acronym_end = ascii_is_upper(s[i - 1]) && i + SKIP_ONE < len &&
s[i + SKIP_ONE] >= 'a' && s[i + SKIP_ONE] <= 'z';
if (ascii_is_upper(s[i]) && (ascii_is_lower_or_digit(s[i - 1]) || acronym_end)) {
break;
}
i++;
}
out->at[out->n] = s + start;
out->len[out->n] = i - start;
out->chars += i - start;
out->n++;
}
}
static bool gate_word_eq(const gate_words_t *a, int i, const gate_words_t *b, int j) {
if (a->len[i] != b->len[j]) {
return false;
}
for (size_t k = 0; k < a->len[i]; k++) {
if (ascii_upper(a->at[i][k]) != ascii_upper(b->at[j][k])) {
return false;
}
}
return true;
}
static bool gate_words_equal(const gate_words_t *a, const gate_words_t *b) {
if (a->n == 0 || a->n != b->n) {
return false;
}
for (int i = 0; i < a->n; i++) {
if (!gate_word_eq(a, i, b, i)) {
return false;
}
}
return true;
}
/* `a`'s words appear in order among `b`'s, ending on `b`'s last word:
* client in Mock|Client, transaction|manager in Transaction|State|Manager. */
static bool gate_words_end_within(const gate_words_t *a, const gate_words_t *b) {
if (a->n == 0 || a->n > b->n || a->chars < GATE_WORDS_MIN_CHARS ||
!gate_word_eq(a, a->n - 1, b, b->n - 1)) {
return false;
}
int j = b->n - 2;
for (int i = a->n - 2; i >= 0; i--) {
while (j >= 0 && !gate_word_eq(a, i, b, j)) {
j--;
}
if (j < 0) {
return false;
}
j--;
}
return true;
}
/* A chain segment names an ancestry segment: the same words (`repo` / Repo,
* `store` / the store package, `otel_guard` / OtelGuard), or, for a type
* (upper-case) ancestry segment, the words of one end within the other's
* (`cleanerManager` / LogCleanerManager, `spyLogManager` / LogManager,
* `transactionManager` / TransactionStateManager, `client` / MockClient). */
static bool segment_names(gate_seg_t s, const char *anc, size_t anc_len) {
if (s.len == 0 || anc_len == 0) {
return false;
}
gate_words_t sw;
gate_words_t aw;
gate_words(s.text, s.len, &sw);
gate_words(anc, anc_len, &aw);
if (gate_words_equal(&sw, &aw)) {
return true;
}
return ascii_is_upper(anc[0]) &&
(gate_words_end_within(&sw, &aw) || gate_words_end_within(&aw, &sw));
}
/* The same words only: a type root (`Scheduler` for the scheduler module,
* `FlagdUi` for flagd_ui) names its type exactly. */
static bool segment_names_exactly(gate_seg_t s, const char *anc, size_t anc_len) {
gate_words_t sw;
gate_words_t aw;
gate_words(s.text, s.len, &sw);
gate_words(anc, anc_len, &aw);
return gate_words_equal(&sw, &aw);
}
/* Calls `visit` on each top-level segment of the receiver chain of
* `callee_name` (every segment before the final name; dots inside a call's
* arguments do not split), and on the receiver a mocking call hands in
* (`verify(consumer).commitAsync`, `when(rlm).fetch`, `doReturn(x).when(log)`:
* its first argument). Stops and returns true when `visit` does. */
typedef bool (*gate_visit_fn)(gate_seg_t seg, const char *candidate_qn);
static bool gate_visit_segment(const char *seg, size_t len, gate_visit_fn visit,
const char *candidate_qn) {
gate_seg_t s = gate_segment(seg, len);
if (s.len > 0 && visit(s, candidate_qn)) {
return true;
}
static const char *const mockers[] = {"verify", "when", "spy", "inOrder"};
for (size_t m = 0; m < sizeof(mockers) / sizeof(mockers[0]); m++) {
size_t mlen = strlen(mockers[m]);
if (s.len != mlen || strncmp(s.text, mockers[m], mlen) != 0) {
continue;
}
const char *open = memchr(seg, '(', len);
if (!open) {
return false;
}
const char *arg = open + SKIP_ONE;
const char *end = arg;
while (end < seg + len && *end != ',' && *end != ')' && *end != '(') {
end++;
}
gate_seg_t a = gate_segment(arg, (size_t)(end - arg));
return a.len > 0 && visit(a, candidate_qn);
}
return false;
}
static bool gate_chain_any(const char *callee_name, gate_visit_fn visit, const char *candidate_qn) {
const char *seg = callee_name;
int depth = 0;
const char *last_top_dot = NULL;
for (const char *p = callee_name; *p; p++) {
if (*p == '(' || *p == '[') {
depth++;
} else if ((*p == ')' || *p == ']') && depth > 0) {
depth--;
} else if (*p == '.' && depth == 0) {
last_top_dot = p;
}
}
if (!last_top_dot) {
return false;
}
depth = 0;
for (const char *p = callee_name; p <= last_top_dot; p++) {
if (*p == '(' || *p == '[') {
depth++;
} else if ((*p == ')' || *p == ']') && depth > 0) {
depth--;
} else if (*p == '.' && depth == 0) {
if (gate_visit_segment(seg, (size_t)(p - seg), visit, candidate_qn)) {
return true;
}
seg = p + SKIP_ONE;
}
}
return false;
}
/* Some ancestry segment of `candidate_qn` (everything before its final name)
* is named by `seg` (segment_names). */
static bool seg_names_ancestry(gate_seg_t seg, const char *candidate_qn) {
const char *cand_last = strrchr(candidate_qn, '.');
if (!cand_last || cand_last == candidate_qn) {
return false;
}
for (const char *anc = candidate_qn; anc < cand_last;) {
const char *anc_end = strchr(anc, '.');
if (segment_names(seg, anc, (size_t)(anc_end - anc))) {
return true;
}
anc = anc_end + SKIP_ONE;
}
return false;
}
/* As seg_names_ancestry, by segment_names_exactly. */
static bool seg_names_ancestry_exactly(gate_seg_t seg, const char *candidate_qn) {
const char *cand_last = strrchr(candidate_qn, '.');
if (!cand_last || cand_last == candidate_qn) {
return false;
}
for (const char *anc = candidate_qn; anc < cand_last;) {
const char *anc_end = strchr(anc, '.');
if (segment_names_exactly(seg, anc, (size_t)(anc_end - anc))) {
return true;
}
anc = anc_end + SKIP_ONE;
}
return false;
}
/* The field-type hint's own convention (try_field_type_hint, pass_parallel.c):
* the candidate's owner -- the segment before its final name -- is the
* receiver's name capitalized, or that name with an interface `I`. */
static bool seg_is_hint_owner(gate_seg_t seg, const char *candidate_qn) {
const char *last = strrchr(candidate_qn, '.');
if (!last || last == candidate_qn) {
return false;
}
const char *owner = last;
while (owner > candidate_qn && owner[-1] != '.') {
owner--;
}
size_t olen = (size_t)(last - owner);
if (olen == seg.len + SKIP_ONE && owner[0] == 'I') {
owner++;
olen--;
}
return olen == seg.len && seg.len > 0 && owner[0] == ascii_upper(seg.text[0]) &&
strncmp(owner + SKIP_ONE, seg.text + SKIP_ONE, seg.len - SKIP_ONE) == 0;
}
/* The first top-level dot of `callee_name` (not one inside a call's
* arguments), or NULL. */
static const char *gate_first_dot(const char *callee_name) {
int depth = 0;
for (const char *p = callee_name; *p; p++) {
if (*p == '(' || *p == '[') {
depth++;
} else if ((*p == ')' || *p == ']') && depth > 0) {
depth--;
} else if (*p == '.' && depth == 0) {
return p;
}
}
return NULL;
}
/* receiver_chain_admits judges a chain rooted in a TYPE and lets a lower-case
* root through: `tp.topic`, `vm.load`, `os.path.join` name a value or a module
* whose type the chain does not show. So on the CALLS path the name alone
* chose the target of every such member call the LSP had not typed: kafka's
* `tp.topic()` (a TopicPartition) bound ConsumerRecord.topic, the project's
* one `topic`, and `new DeleteRecordsPartitionResult().setErrorCode()` a
* builder of another type. This is the same gate for those calls, a value root
* or a constructor root (`new X(...)`, whose type the chain does name): the
* name-only exits (unique_name, suffix_match) bind such a call only on
* evidence that it means the chosen candidate --
* - a chain segment names a segment of its ancestry (segment_names: `repo`
* for Repo, `store` for the store package, `cleanerManager` for
* LogCleanerManager, `new RaftContext.Builder()` for RaftContext.Builder);
* - it lives in the caller's own module (a class beside the caller, a nested
* class of its file);
* - it is reachable through the file's imports;
* - or, in C and C++, the C member resolver decides a call onto a struct
* member from the object's type (cbm_call_onto_field_policy), and a
* candidate whose owner type the caller's own file declares
* (caller_declares_owner) is that type's member.
* When another candidate is the field-type hint's own pick (its owner is the
* receiver's name capitalized) the call is left as before, for the parallel
* resolver to re-pick. Otherwise the guess is refused: a veto, never a
* re-pick, so a refused call stays unresolved instead of falling to the next
* name-only candidate. A self receiver (this, self, cls, super, base, $this,
* $self, this_, _this) is not unknown, and a `::` path (a Rust, C++ or PHP
* namespace) is not a receiver; both, and a type root, stay with
* receiver_chain_admits. Every strategy before these exits -- import_map,
* same_module, qualified_suffix and lsp_* -- is untouched. `lang` is the
* calling file's language (CALLS path only); a C# keyword root
* (`int.TryParse`) is a type there. */
/* C and C++: the caller's own file declares the candidate's owner type -- a
* class re-declared or declared ahead of its use in the calling translation
* unit (`class Widget { public: int get_id() const; };` above `Widget w(n);
* w.get_id()`). The C++ resolver types the receiver against that declaration,
* whose methods have no definition to bind; the candidate is the type's
* definition elsewhere. */
static bool caller_declares_owner(const cbm_registry_t *r, const char *chosen,
const char *module_qn) {
const char *last = strrchr(chosen, '.');
if (!last || !module_qn || !module_qn[0]) {
return false;
}
const char *owner = last;
while (owner > chosen && owner[-1] != '.') {
owner--;
}
if (owner == chosen || owner == last) {
return false;
}
char qn[CBM_SZ_1K];
int n = snprintf(qn, sizeof(qn), "%s.%.*s", module_qn, (int)(last - owner), owner);
if (n < 0 || (size_t)n >= sizeof(qn)) {
return false;
}
return cbm_label_is_type_like(cbm_registry_label_of(r, qn));
}
static bool value_receiver_blind(const cbm_registry_t *r, const qn_array_t *arr,
const char *callee_name, const char *chosen, const char *module_qn,
const char **import_vals, int import_count, CBMLanguage lang) {
const char *dot = gate_first_dot(callee_name);
const char *colons = strstr(callee_name, "::");
if (!dot || (colons && colons < dot)) {
return false; /* bare name, or a :: path */
}
const char *start = callee_name;
while (ascii_is_space(*start)) {
start++;
}
bool ctor = strncmp(start, "new ", 4) == 0;
if (!ctor &&
(ascii_is_upper(start[0]) || (lang == CBM_LANG_CSHARP && cs_type_keyword_root(start)))) {
return false; /* a type root: receiver_chain_admits */
}
gate_seg_t root = gate_segment(start, (size_t)(dot - start));
if (!ctor && self_receiver_root(root.text, root.len)) {
return false;
}
if (import_count > 0 && is_import_reachable(chosen, import_vals, import_count)) {
return false;
}
size_t mlen = module_qn ? strlen(module_qn) : 0;
if (mlen > 0 && strncmp(chosen, module_qn, mlen) == 0 && chosen[mlen] == '.') {
return false;
}
if (lang == CBM_LANG_C || lang == CBM_LANG_CPP) {
const char *label = cbm_registry_label_of(r, chosen);
if (label && strcmp(label, "Field") == 0) {
return false; /* CBM_FIELD_CALL_BY_OWNER: the C member resolver decides */
}
if (caller_declares_owner(r, chosen, module_qn)) {
return false;
}
}
if (gate_chain_any(callee_name, seg_names_ancestry, chosen)) {
return false;
}
if (!ctor && lang != CBM_LANG_GO) { /* the hint never runs for Go */
for (int i = 0; i < arr->count; i++) {
if (arr->items[i] != chosen && seg_is_hint_owner(root, arr->items[i])) {
return false;
}
}
}
return true;
}
/* Where the receiver gate applies, by the calling file's language: an
* allowlist by measurement. A language is listed only where a hand-read
* seeded sample of the calls the gate refuses there, on a real corpus, was
* at least ~85% wrong. GATE_FULL: both its unique_name binds and its
* suffix_match picks cleared that. GATE_SUFFIX_ONLY: only the suffix_match
* picks (a choice among same-named candidates by path proximity alone) did,
* so the language keeps its unique_name binds. Every other language -- the
* ones measured below the bar (Java, Kotlin, Swift, Ruby, Objective-C), the
* ones where it refused nothing (C, Dart, and the languages with their own
* member-call guard after resolution: Python, the JS/TS family, Perl), and
* every language not measured -- keeps its name-only binds. */
typedef enum { GATE_OFF, GATE_SUFFIX_ONLY, GATE_FULL } gate_scope_t;
static gate_scope_t gate_scope(CBMLanguage lang) {
switch (lang) {
case CBM_LANG_GO:
case CBM_LANG_CPP:
case CBM_LANG_CSHARP:
case CBM_LANG_LUA:
case CBM_LANG_TCL:
case CBM_LANG_GROOVY:
case CBM_LANG_ELIXIR:
return GATE_FULL;
case CBM_LANG_SCALA:
case CBM_LANG_RUST:
case CBM_LANG_PHP:
return GATE_SUFFIX_ONLY;
default:
return GATE_OFF;
}
}
/* #1897's type check for the one name-only exit it did not cover: the
* fallback of resolve_multi_with_imports that ranks every candidate when the
* file's imports reach none (Scala `Future.exception` bound a Variable in a
* test, then MultiplexedFinagleService.exception). Refused when the chosen
* candidate's ancestry is not in the chain and no candidate's is by the same
* words (Elixir's `Scheduler` names the scheduler module) -- when one is, the
* pick is left for the parallel resolver's field-type hint. */
static bool type_receiver_blind(const qn_array_t *arr, const char *callee_name, const char *chosen,
CBMLanguage lang) {
if (receiver_chain_admits(callee_name, chosen, lang)) {
return false;
}
for (int i = 0; i < arr->count; i++) {
if (gate_chain_any(callee_name, seg_names_ancestry_exactly, arr->items[i])) {
return false;
}
}
return true;
}
/* The same-name candidates without the members -- Field and Method nodes --
* in `view` (backed by the caller's buffers, at most REG_MAX_CANDIDATES
* entries). Every entry of a non-empty bucket has its stored verdict and
@@ -2522,12 +3013,14 @@ static void candidates_without_members(const cbm_registry_t *r, const qn_array_t
}
/* Strategies 3.5, 3 and 4 over one same-name candidate set. `lang` reaches
* receiver_chain_admits only (see there); `caller_test` reaches
* best_by_import_distance only. */
* receiver_chain_admits and value_receiver_blind only (see there): it is the
* calling file's language on the CALLS path and CBM_LANG_COUNT on every other
* resolve. `caller_test` reaches best_by_import_distance only. */
static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const qn_array_t *arr,
const char *callee_name, const char *module_qn,
const char **import_vals, int import_count,
CBMLanguage lang, bool caller_test) {
gate_scope_t gate = gate_scope(lang);
/* Strategy 3.5: a qualified callee disambiguates among multiple same-name
* candidates by full qualified tail, before bare-name scoring collapses
* them onto a single winner. */
@@ -2538,9 +3031,13 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q
}
}
/* Strategy 3: unique name */
/* Strategy 3: unique name, judged by the receiver gate only where it is
* whole (gate_scope). */
if (arr->count == SKIP_ONE) {
if (!receiver_chain_admits(callee_name, arr->items[0], lang)) {
if (!receiver_chain_admits(callee_name, arr->items[0], lang) ||
(gate == GATE_FULL &&
value_receiver_blind(r, arr, callee_name, arr->items[0], module_qn, import_vals,
import_count, lang))) {
return empty_result();
}
double conf = CONF_UNIQUE_NAME;
@@ -2553,14 +3050,27 @@ static cbm_resolution_t resolve_name_candidates(const cbm_registry_t *r, const q
/* Strategy 4: multiple candidates */
if (import_vals && import_count > 0) {
return resolve_multi_with_imports(r, arr, module_qn, import_vals, import_count,
caller_test);
cbm_resolution_t res =
resolve_multi_with_imports(r, arr, module_qn, import_vals, import_count, caller_test);
/* An answer the file's imports do not reach came from the fallback
* that ranks every candidate: a name-only guess like the exits here,
* so on the CALLS path it passes the same receiver gates. */
if (gate != GATE_OFF && res.qualified_name && res.qualified_name[0] &&
!is_import_reachable(res.qualified_name, import_vals, import_count) &&
(type_receiver_blind(arr, callee_name, res.qualified_name, lang) ||
value_receiver_blind(r, arr, callee_name, res.qualified_name, module_qn, import_vals,
import_count, lang))) {
return empty_result();
}
return res;
}
const char *best =
best_by_import_distance(r, (const char **)arr->items, qn_test_flags(arr),
qn_lang_flags(arr), arr->count, module_qn, caller_test);
if (best) {
if (!receiver_chain_admits(callee_name, best, lang)) {
if (!receiver_chain_admits(callee_name, best, lang) ||
(gate != GATE_OFF &&
value_receiver_blind(r, arr, callee_name, best, module_qn, NULL, 0, lang))) {
return empty_result();
}
double conf = candidate_count_penalty(CONF_SUFFIX_MATCH, arr->count);
@@ -2711,14 +3221,19 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c
/* Per-file cache: same callee_name in N call sites → 1 chain walk
* + N-1 O(1) hash hits. module_qn is constant per file so the
* cache key only needs callee_name. The chain reads call_lang only as
* "is it C#", so that one bit is all the entry has to record; the
* caller's test verdict can differ between two functions of one file. */
* cache key only needs callee_name. Within one file call_lang is
* either the file's own language (a CALLS resolve, which the receiver
* gate and the C# keyword rule read) or CBM_LANG_COUNT, so "is it a
* CALLS resolve" pins it and is all the entry has to record of it
* beside the C# bit; the caller's test verdict can differ between two
* functions of one file. */
bool calls_path = call_lang < CBM_LANG_COUNT;
bool cs_keywords = call_lang == CBM_LANG_CSHARP;
resolve_cache_entry_t *cached =
_resolve_cache ? (resolve_cache_entry_t *)cbm_ht_get(_resolve_cache, callee_name) : NULL;
if (cached && cached->scope_len == scope_len && cached->skip_members == skip_members &&
cached->cs_keywords == cs_keywords && cached->caller_test == caller_test)
cached->calls_path == calls_path && cached->cs_keywords == cs_keywords &&
cached->caller_test == caller_test)
return cached->res;
cbm_resolution_t res =
@@ -2744,6 +3259,7 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c
cached->res = res;
cached->scope_len = scope_len;
cached->skip_members = skip_members;
cached->calls_path = calls_path;
cached->cs_keywords = cs_keywords;
cached->caller_test = caller_test;
} else if (_resolve_cache) {
@@ -2752,6 +3268,7 @@ static cbm_resolution_t registry_resolve_scoped(const cbm_registry_t *r, const c
e->res = res;
e->scope_len = scope_len;
e->skip_members = skip_members;
e->calls_path = calls_path;
e->cs_keywords = cs_keywords;
e->caller_test = caller_test;
char *kdup = strdup(callee_name);
+31
View File
@@ -7121,6 +7121,36 @@ TEST(cli_extract_binary_from_targz_invalid_data) {
PASS();
}
/* The decompression limit keeps a hostile archive from inflating without end.
* The same archive is refused over a lowered limit and accepted under the real
* one, so the limit is the only reason for the refusal. */
TEST(cli_update_refuses_an_archive_over_the_decompression_limit) {
enum { PAYLOAD = 1 << 20, LIMIT = 1 << 16 };
unsigned char *payload = calloc(1, PAYLOAD);
ASSERT_NOT_NULL(payload);
int archive_len = 0;
unsigned char *archive =
create_test_targz("codebase-memory-mcp", payload, PAYLOAD, &archive_len);
free(payload);
ASSERT_NOT_NULL(archive);
cbm_cli_set_decompress_max_for_test(LIMIT);
int over_len = 0;
unsigned char *over = cbm_extract_binary_from_targz(archive, archive_len, &over_len);
cbm_cli_set_decompress_max_for_test(0);
int under_len = 0;
unsigned char *under = cbm_extract_binary_from_targz(archive, archive_len, &under_len);
free(archive);
bool refused = over == NULL;
free(over);
bool accepted = under != NULL && under_len == PAYLOAD;
free(under);
ASSERT_TRUE(refused);
ASSERT_TRUE(accepted);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* Zip extraction tests
* ═══════════════════════════════════════════════════════════════════ */
@@ -21449,6 +21479,7 @@ SUITE(cli) {
RUN_TEST(cli_extract_binary_from_targz);
RUN_TEST(cli_extract_binary_from_targz_not_found);
RUN_TEST(cli_extract_binary_from_targz_invalid_data);
RUN_TEST(cli_update_refuses_an_archive_over_the_decompression_limit);
RUN_TEST(cli_extract_binary_from_zip);
RUN_TEST(cli_extract_binary_from_zip_not_found);
RUN_TEST(cli_extract_binary_from_zip_path_traversal);
File diff suppressed because it is too large Load Diff
+58
View File
@@ -624,6 +624,62 @@ TEST(handles_gin_group_prefix_issue686) {
PASS();
}
/* #686: which statement binds a group receiver. `var` lists and blocks, tuples, a name
* declared with no value (no prefix), a shadowing inner block, rebinding, an enclosing
* block reached from an inner one, a chain of groups, and a function literal (a boundary:
* nothing outside it binds). */
static const EtFile et_go_group_bindings[] = {{"main.go",
"package main\n\n"
"import \"github.com/gin-gonic/gin\"\n\n"
"func h(c *gin.Context) {}\n\n"
"func main() {\n"
" r := gin.Default()\n"
" var a, b = r.Group(\"/a\"), r.Group(\"/b\")\n"
" a.GET(\"/x\", h)\n"
" b.GET(\"/y\", h)\n"
" var (\n"
" c1 = r.Group(\"/c\")\n"
" d1 *gin.RouterGroup\n"
" )\n"
" c1.GET(\"/z\", h)\n"
" d1.GET(\"/w\", h)\n"
" e, f := r.Group(\"/e\"), r.Group(\"/f\")\n"
" f.GET(\"/q\", h)\n"
" e.GET(\"/p\", h)\n"
" g := r.Group(\"/g1\")\n"
" {\n"
" g := r.Group(\"/g2\")\n"
" g.GET(\"/in\", h)\n"
" }\n"
" g.GET(\"/out\", h)\n"
" g = r.Group(\"/g3\")\n"
" if true {\n"
" g.GET(\"/if\", h)\n"
" }\n"
" m := r.Group(\"/m\")\n"
" n := m.Group(\"/n\")\n"
" n.GET(\"/deep\", h)\n"
" go func() {\n"
" g.GET(\"/lit\", h)\n"
" }()\n"
"}\n"}};
static const char *et_go_group_bindings_routes[] = {"/a/x", "/b/y", "/c/z", "/w",
"/f/q", "/e/p", "/g2/in", "/g1/out",
"/g3/if", "/m/n/deep", "/lit", NULL};
TEST(handles_go_group_binding_forms_sequential_issue686) {
ASSERT_TRUE(
et_routes_exact_mode(et_go_group_bindings, 1, et_go_group_bindings_routes, false, NULL));
PASS();
}
TEST(handles_go_group_binding_forms_parallel_issue686) {
ASSERT_TRUE(
et_routes_exact_mode(et_go_group_bindings, 1, et_go_group_bindings_routes, true, NULL));
PASS();
}
/* Spring (Java) — class-level @RequestMapping must prefix method mappings.
* Reproduce-first: a HANDLES count alone can pass with partial routes
* ("/orders"), but callers/search_graph need the actual endpoint names
@@ -3003,6 +3059,8 @@ SUITE(edge_types_probe) {
RUN_TEST(handles_fiber_group_prefix_sequential_issue686);
RUN_TEST(handles_fiber_group_prefix_parallel_issue686);
RUN_TEST(handles_gin_group_prefix_issue686);
RUN_TEST(handles_go_group_binding_forms_sequential_issue686);
RUN_TEST(handles_go_group_binding_forms_parallel_issue686);
RUN_TEST(handles_spring_java);
RUN_TEST(data_flows_test_files_pair_within_their_file_sequential);
RUN_TEST(data_flows_test_files_pair_within_their_file_parallel);
+124 -9
View File
@@ -174,6 +174,62 @@ static int mkc_edge(const MKC_File *files, int nfiles, const char *edge_type, in
return got >= floor;
}
/* Nodes carrying `label` whose qualified name ends with `qn_suffix`. */
static int mkc_label_nodes_by_qn_suffix(cbm_store_t *store, const char *project, const char *label,
const char *qn_suffix) {
cbm_node_t *nodes = NULL;
int count = 0;
if (!store || cbm_store_find_nodes_by_qn_suffix(store, project, qn_suffix, &nodes, &count) !=
CBM_STORE_OK) {
return -1;
}
int matches = 0;
for (int i = 0; i < count; i++) {
if (nodes[i].label && strcmp(nodes[i].label, label) == 0) {
matches++;
}
}
cbm_store_free_nodes(nodes, count);
return matches;
}
/* Edges of `edge_type` from the one node ending in `source_suffix` to nodes named
* `target_name`. -1 when the suffix names no node or more than one, so a missing
* source node is told apart from a missing edge. A per-type total cannot see
* WHICH scope a call was attributed to. The target is matched by name so a
* constructor call counts whether it binds the type or its constructor. */
static int mkc_edges_from_suffix_to_name(cbm_store_t *store, const char *project,
const char *edge_type, const char *source_suffix,
const char *target_name) {
cbm_node_t *sources = NULL;
int source_count = 0;
if (!store ||
cbm_store_find_nodes_by_qn_suffix(store, project, source_suffix, &sources, &source_count) !=
CBM_STORE_OK ||
source_count != 1) {
cbm_store_free_nodes(sources, source_count);
return -1;
}
cbm_edge_t *edges = NULL;
int edge_count = 0;
int matches = -1;
if (cbm_store_find_edges_by_source_type(store, sources[0].id, edge_type, &edges, &edge_count) ==
CBM_STORE_OK) {
matches = 0;
for (int i = 0; i < edge_count; i++) {
cbm_node_t target = {0};
if (cbm_store_find_node_by_id(store, edges[i].target_id, &target) == CBM_STORE_OK &&
target.name && strcmp(target.name, target_name) == 0) {
matches++;
}
cbm_node_free_fields(&target);
}
}
cbm_store_free_edges(edges, edge_count);
cbm_store_free_nodes(sources, source_count);
return matches;
}
/* ══════════════════════════════════════════════════════════════════════════
* ═══ CLASS C1: CONSTRUCTOR / INSTANTIATION ═══════════════════════════════
*
@@ -475,9 +531,13 @@ TEST(mkc_c2_kotlin_operator_plus) {
}
/* C2-G: C# — operator + overload.
* red=bug: C# `a + b` for structs with operator+ is a binary_expression in
* the grammar. cs_call_types = {invocation_expression} only — no
* binary_expression, so the operator call is never extracted. */
* green=guard: `operator+` is a Method of Vec2 with a node of its own, and the
* constructor call in its body belongs to it. operator_declaration was no
* function kind, so the operator had no node and `new Vec2(...)` was attributed
* to the file. That misattributed edge plus Run->Dot made CALLS >= 2, so the
* count this case asserted before passed without the operator ever existing.
* Still open [KNOWN class 12]: the operator site `a + b` in Run is no CALLS
* edge to operator+ (cs_call_types has no binary_expression). */
TEST(mkc_c2_csharp_operator_plus) {
static const MKC_File f[] = {{"Vec.cs",
"namespace App {\n"
@@ -495,11 +555,65 @@ TEST(mkc_c2_csharp_operator_plus) {
" }\n"
" }\n"
"}\n"}};
/* REAL BUG: `(a + b).Dot(b)` should CALLS both Vec2::op_Addition (the `+`)
* AND Dot (>=2). Dot resolves (CALLS=1) but the operator `a + b` does not —
* C# binary_expression operator desugaring is not modeled (cs_call_types omits
* it), so the op_Addition call is missing → CALLS=1 < 2. [KNOWN class 12] */
ASSERT_TRUE(mkc_edge(f, 1, "CALLS", 2, "c2/csharp/operator_plus", 0));
MKC_Proj lp;
cbm_store_t *store = mkc_index(&lp, f, 1);
int op_nodes = mkc_label_nodes_by_qn_suffix(store, lp.project, "Method", "Vec2.operator+");
int op_ctor =
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator+", "Vec2");
int run_dot = mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Math.Run", "Dot");
if (op_nodes != 1 || op_ctor != 1 || run_dot != 1) {
fprintf(stderr,
" [MKC] c2/csharp/operator_plus FAIL operator_methods=%d operator_ctor_calls=%d "
"run_dot_calls=%d (GREEN regression)\n",
op_nodes, op_ctor, run_dot);
mkc_diag(store, lp.project, "c2/csharp/operator_plus");
}
mkc_cleanup(&lp, store);
ASSERT_EQ(op_nodes, 1);
ASSERT_EQ(op_ctor, 1);
ASSERT_EQ(run_dot, 1);
PASS();
}
/* C2-G2: C# — overloaded operators, and operators spelled with a space or a word.
* green=guard: the two `operator+` overloads merge into one Method node, as
* other C# overloads do; `operator -` is spelled `operator-`, and `operator true`
* keeps its space. Each operator owns the calls in its body. */
TEST(mkc_c2_csharp_operator_overloads) {
static const MKC_File f[] = {
{"Vec.cs",
"namespace App {\n"
" struct Vec2 {\n"
" public float X;\n"
" public Vec2(float x) { X = x; }\n"
" public static Vec2 operator+(Vec2 a, Vec2 b) { return new Vec2(a.X + b.X); }\n"
" public static Vec2 operator+(Vec2 a, float b) { return new Vec2(a.X + b); }\n"
" public static Vec2 operator -(Vec2 a, Vec2 b) { return new Vec2(a.X - b.X); }\n"
" public static bool operator true(Vec2 a) { return Check(a); }\n"
" static bool Check(Vec2 a) { return true; }\n"
" }\n"
"}\n"}};
MKC_Proj lp;
cbm_store_t *store = mkc_index(&lp, f, 1);
int plus_nodes = mkc_label_nodes_by_qn_suffix(store, lp.project, "Method", "Vec2.operator+");
int plus_ctor =
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator+", "Vec2");
int minus_ctor =
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator-", "Vec2");
int true_check =
mkc_edges_from_suffix_to_name(store, lp.project, "CALLS", "Vec2.operator true", "Check");
if (plus_nodes != 1 || plus_ctor != 1 || minus_ctor != 1 || true_check != 1) {
fprintf(stderr,
" [MKC] c2/csharp/operator_overloads FAIL plus_methods=%d plus_ctor=%d "
"minus_ctor=%d true_check=%d (GREEN regression)\n",
plus_nodes, plus_ctor, minus_ctor, true_check);
mkc_diag(store, lp.project, "c2/csharp/operator_overloads");
}
mkc_cleanup(&lp, store);
ASSERT_EQ(plus_nodes, 1);
ASSERT_EQ(plus_ctor, 1);
ASSERT_EQ(minus_ctor, 1);
ASSERT_EQ(true_check, 1);
PASS();
}
@@ -1251,8 +1365,9 @@ SUITE(matrix_known_classes) {
RUN_TEST(mkc_c2_rust_add_trait);
/* C2-F: Kotlin operator fun plus — red=bug */
RUN_TEST(mkc_c2_kotlin_operator_plus);
/* C2-G: C# operator+ — red=bug */
/* C2-G: C# operator+ — green=guard (the `a + b` site is still class 12) */
RUN_TEST(mkc_c2_csharp_operator_plus);
RUN_TEST(mkc_c2_csharp_operator_overloads);
/* C2-H: Scala symbolic method — uncertain/red=bug */
RUN_TEST(mkc_c2_scala_symbolic_method);
/* C2-I: Ruby def + — uncertain/red=bug */
+420
View File
@@ -8855,6 +8855,418 @@ TEST(tool_trace_union_records_min_hop_across_seeds) {
PASS();
}
/* include_overrides fixture (#1271): run_local calls two contracts.
* LocalProcessor.process is overridden by Alpha and Beta, and Alpha's by
* Gamma -- two levels, since an OVERRIDE edge points at the nearest declaring
* ancestor only. A test-file Mock overrides the contract too, and so does an
* infra Variable (a tfvars/kustomize-style OVERRIDE). Sink.write has one
* implementation. Other.process is never called, so its implementation never
* appears. */
static int64_t ov_node(cbm_store_t *store, const char *project, const char *label, const char *name,
const char *qn, const char *file) {
cbm_node_t n = {.project = project,
.label = label,
.name = name,
.qualified_name = qn,
.file_path = file,
.start_line = 1,
.end_line = 3};
return cbm_store_upsert_node(store, &n);
}
static bool ov_edge(cbm_store_t *store, const char *project, int64_t src, int64_t dst,
const char *type) {
cbm_edge_t e = {.project = project, .source_id = src, .target_id = dst, .type = type};
return src > 0 && dst > 0 && cbm_store_insert_edge(store, &e) > 0;
}
static cbm_mcp_server_t *ov_fixture(void) {
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
if (!srv) {
return NULL;
}
cbm_store_t *st = cbm_mcp_server_store(srv);
const char *p = "dispatch-fixture";
cbm_mcp_server_set_project(srv, p);
if (cbm_store_upsert_project(st, p, "/tmp/dispatch-fixture") != CBM_STORE_OK) {
cbm_mcp_server_free(srv);
return NULL;
}
const char *src = "src/fixture.py";
int64_t caller = ov_node(st, p, "Function", "run_local", "dispatch_fixture.run_local", src);
int64_t contract =
ov_node(st, p, "Method", "process", "dispatch_fixture.LocalProcessor.process", src);
int64_t sink = ov_node(st, p, "Method", "write", "dispatch_fixture.Sink.write", src);
int64_t alpha =
ov_node(st, p, "Method", "process", "dispatch_fixture.LocalAlphaProcessor.process", src);
int64_t beta =
ov_node(st, p, "Method", "process", "dispatch_fixture.LocalBetaProcessor.process", src);
int64_t gamma =
ov_node(st, p, "Method", "process", "dispatch_fixture.LocalGammaProcessor.process", src);
int64_t mock = ov_node(st, p, "Method", "process",
"dispatch_fixture.tests.MockProcessor.process", "tests/test_fixture.py");
int64_t var =
ov_node(st, p, "Variable", "process", "dispatch_fixture.env.process", "env/prod.tfvars");
int64_t file_sink = ov_node(st, p, "Method", "write", "dispatch_fixture.FileSink.write", src);
int64_t other = ov_node(st, p, "Method", "process", "dispatch_fixture.Other.process", src);
int64_t other_impl =
ov_node(st, p, "Method", "process", "dispatch_fixture.OtherImpl.process", src);
bool ok =
ov_edge(st, p, caller, contract, "CALLS") && ov_edge(st, p, caller, sink, "CALLS") &&
ov_edge(st, p, alpha, contract, "OVERRIDE") && ov_edge(st, p, beta, contract, "OVERRIDE") &&
ov_edge(st, p, gamma, alpha, "OVERRIDE") && ov_edge(st, p, mock, contract, "OVERRIDE") &&
ov_edge(st, p, var, contract, "OVERRIDE") && ov_edge(st, p, file_sink, sink, "OVERRIDE") &&
ov_edge(st, p, other_impl, other, "OVERRIDE");
if (!ok) {
cbm_mcp_server_free(srv);
return NULL;
}
return srv;
}
/* One trace_path call on the fixture's run_local; `extra` is appended to the
* arguments object. Returns the tool's text content. */
static char *ov_trace(cbm_mcp_server_t *srv, const char *extra) {
char args[640];
snprintf(args, sizeof(args),
"{\"project\":\"dispatch-fixture\",\"function_name\":\"run_local\","
"\"direction\":\"outbound\",\"depth\":1%s}",
extra);
char *response = cbm_mcp_handle_tool(srv, "trace_path", args);
char *inner = extract_text_content(response);
free(response);
return inner;
}
/* The implementations listed for one contract in a JSON possible_callees. */
static yyjson_val *ov_implementations(yyjson_val *possible, const char *contract) {
size_t i;
size_t max;
yyjson_val *entry;
yyjson_arr_foreach(possible, i, max, entry) {
const char *name = yyjson_get_str(yyjson_obj_get(entry, "contract"));
if (name && strcmp(name, contract) == 0) {
return yyjson_obj_get(entry, "implementations");
}
}
return NULL;
}
static bool ov_lists(yyjson_val *implementations, const char *qn) {
size_t i;
size_t max;
yyjson_val *impl;
yyjson_arr_foreach(implementations, i, max, impl) {
if (yyjson_is_str(impl) && strcmp(yyjson_get_str(impl), qn) == 0) {
return true;
}
}
return false;
}
/* include_overrides lists possible runtime implementations apart from the
* declared CALLS targets, which stay what they are; nothing is written to the
* index, and without the flag the response is the one before it existed. */
TEST(tool_trace_path_possible_callees_stay_apart_from_calls) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
cbm_store_t *st = cbm_mcp_server_store(srv);
char *inner = ov_trace(srv, ",\"include_overrides\":true,\"format\":\"json\"");
ASSERT_NOT_NULL(inner);
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *root = yyjson_doc_get_root(doc);
ASSERT_EQ(yyjson_get_int(yyjson_obj_get(root, "callees_total")), 2);
yyjson_val *possible = yyjson_obj_get(root, "possible_callees");
ASSERT_TRUE(yyjson_is_arr(possible));
ASSERT_EQ(yyjson_arr_size(possible), 2);
yyjson_val *processors =
ov_implementations(possible, "dispatch_fixture.LocalProcessor.process");
ASSERT_NOT_NULL(processors);
ASSERT_TRUE(ov_lists(processors, "dispatch_fixture.LocalAlphaProcessor.process"));
ASSERT_TRUE(ov_lists(processors, "dispatch_fixture.LocalBetaProcessor.process"));
yyjson_val *sinks = ov_implementations(possible, "dispatch_fixture.Sink.write");
ASSERT_NOT_NULL(sinks);
ASSERT_EQ(yyjson_arr_size(sinks), 1);
ASSERT_TRUE(ov_lists(sinks, "dispatch_fixture.FileSink.write"));
ASSERT_NULL(strstr(inner, "dispatch_fixture.OtherImpl.process"));
ASSERT_EQ(cbm_store_count_edges_by_type(st, "dispatch-fixture", "CALLS"), 2);
ASSERT_EQ(cbm_store_count_edges_by_type(st, "dispatch-fixture", "OVERRIDE"), 7);
yyjson_doc_free(doc);
free(inner);
char *by_default = ov_trace(srv, ",\"format\":\"json\"");
char *off = ov_trace(srv, ",\"include_overrides\":false,\"format\":\"json\"");
ASSERT_NOT_NULL(by_default);
ASSERT_NOT_NULL(off);
ASSERT_NULL(strstr(by_default, "possible_callees"));
ASSERT_STR_EQ(by_default, off);
free(by_default);
free(off);
char *tree_default = ov_trace(srv, "");
ASSERT_NOT_NULL(tree_default);
ASSERT_NULL(strstr(tree_default, "possible_callees"));
free(tree_default);
cbm_mcp_server_free(srv);
PASS();
}
/* An OVERRIDE edge names the nearest declaring ancestor only, so the walk is
* transitive: Gamma overrides Alpha, which overrides the contract, and Gamma
* is a possible callee of the contract too. */
TEST(tool_trace_path_possible_callees_follow_the_hierarchy) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
char *inner = ov_trace(srv, ",\"include_overrides\":true,\"format\":\"json\"");
ASSERT_NOT_NULL(inner);
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *possible = yyjson_obj_get(yyjson_doc_get_root(doc), "possible_callees");
yyjson_val *processors =
ov_implementations(possible, "dispatch_fixture.LocalProcessor.process");
ASSERT_NOT_NULL(processors);
ASSERT_TRUE(ov_lists(processors, "dispatch_fixture.LocalGammaProcessor.process"));
ASSERT_EQ(yyjson_arr_size(processors), 3);
yyjson_doc_free(doc);
free(inner);
cbm_mcp_server_free(srv);
PASS();
}
/* Test-file implementations follow include_tests like every trace row; an
* OVERRIDE edge from something that is not callable (a tfvars variable) is
* never a possible callee. */
TEST(tool_trace_path_possible_callees_filter_tests_and_non_callables) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
const char *mock = "dispatch_fixture.tests.MockProcessor.process";
const char *var = "dispatch_fixture.env.process";
char *inner = ov_trace(srv, ",\"include_overrides\":true,\"format\":\"json\"");
ASSERT_NOT_NULL(inner);
ASSERT_NULL(strstr(inner, mock));
ASSERT_NULL(strstr(inner, var));
free(inner);
inner = ov_trace(srv, ",\"include_overrides\":true,\"include_tests\":true,\"format\":\"json\"");
ASSERT_NOT_NULL(inner);
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *possible = yyjson_obj_get(yyjson_doc_get_root(doc), "possible_callees");
yyjson_val *processors =
ov_implementations(possible, "dispatch_fixture.LocalProcessor.process");
ASSERT_NOT_NULL(processors);
ASSERT_TRUE(ov_lists(processors, mock));
ASSERT_FALSE(ov_lists(processors, var));
ASSERT_EQ(yyjson_arr_size(processors), 4);
yyjson_doc_free(doc);
free(inner);
cbm_mcp_server_free(srv);
PASS();
}
/* The tree format lists possible callees as a tree table, one row per
* (contract, implementation), like every other section. */
TEST(tool_trace_path_possible_callees_render_as_tree_table) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
char *tree = ov_trace(srv, ",\"include_overrides\":true");
ASSERT_NOT_NULL(tree);
ASSERT_NOT_NULL(strstr(tree, "possible_callees: 4 (cols: contract implementation)\n"));
ASSERT_NULL(strstr(tree, "possible_callees: ["));
ASSERT_NOT_NULL(strstr(tree, "dispatch_fixture.LocalGammaProcessor.process"));
ASSERT_NOT_NULL(strstr(tree, "dispatch_fixture.FileSink.write"));
free(tree);
cbm_mcp_server_free(srv);
PASS();
}
/* Possible callees belong to the rows a page emits: with limit 1 each page
* lists the implementations of its own callee only, so every implementation
* is reported exactly once across the pages. */
TEST(tool_trace_path_possible_callees_follow_the_page) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
char *first = ov_trace(srv, ",\"include_overrides\":true,\"limit\":1,\"format\":\"json\"");
ASSERT_NOT_NULL(first);
yyjson_doc *doc = yyjson_read(first, strlen(first), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *possible = yyjson_obj_get(root, "possible_callees");
ASSERT_TRUE(yyjson_is_arr(possible));
ASSERT_EQ(yyjson_arr_size(possible), 1);
ASSERT_NOT_NULL(ov_implementations(possible, "dispatch_fixture.LocalProcessor.process"));
const char *cursor = yyjson_get_str(yyjson_obj_get(root, "next_cursor"));
ASSERT_NOT_NULL(cursor);
char extra[320];
snprintf(extra, sizeof(extra),
",\"include_overrides\":true,\"limit\":1,\"format\":\"json\",\"cursor\":\"%s\"",
cursor);
yyjson_doc_free(doc);
ASSERT_NULL(strstr(first, "dispatch_fixture.FileSink.write"));
free(first);
char *second = ov_trace(srv, extra);
ASSERT_NOT_NULL(second);
doc = yyjson_read(second, strlen(second), 0);
ASSERT_NOT_NULL(doc);
possible = yyjson_obj_get(yyjson_doc_get_root(doc), "possible_callees");
ASSERT_TRUE(yyjson_is_arr(possible));
ASSERT_EQ(yyjson_arr_size(possible), 1);
yyjson_val *sinks = ov_implementations(possible, "dispatch_fixture.Sink.write");
ASSERT_NOT_NULL(sinks);
ASSERT_TRUE(ov_lists(sinks, "dispatch_fixture.FileSink.write"));
ASSERT_NULL(strstr(second, "dispatch_fixture.LocalAlphaProcessor.process"));
yyjson_doc_free(doc);
free(second);
cbm_mcp_server_free(srv);
PASS();
}
/* Only a call through an instance receiver can run an override. A super()
* call is bound to the parent, and a call on a freshly constructed object
* runs that class's own method, so neither lists implementations; a self
* call does. */
TEST(tool_trace_path_possible_callees_only_for_dispatching_calls) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
cbm_store_t *st = cbm_mcp_server_store(srv);
const char *p = "dispatch-fixture";
const char *src = "src/fixture.py";
cbm_node_t contract = {0};
ASSERT_EQ(
cbm_store_find_node_by_qn(st, p, "dispatch_fixture.LocalProcessor.process", &contract),
CBM_STORE_OK);
static const struct {
const char *caller;
const char *callee;
bool lists;
} cases[] = {
{"reset_local", "super().process", false},
{"build_local", "ProcessorFactory().process", false},
{"step_local", "self.process", true},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
char qn[96];
snprintf(qn, sizeof(qn), "dispatch_fixture.%s", cases[i].caller);
int64_t caller = ov_node(st, p, "Function", cases[i].caller, qn, src);
ASSERT_GT(caller, 0);
char props[128];
snprintf(props, sizeof(props), "{\"callee\":\"%s\"}", cases[i].callee);
cbm_edge_t call = {.project = p,
.source_id = caller,
.target_id = contract.id,
.type = "CALLS",
.properties_json = props};
ASSERT_GT(cbm_store_insert_edge(st, &call), 0);
char args[320];
snprintf(args, sizeof(args),
"{\"project\":\"dispatch-fixture\",\"function_name\":\"%s\","
"\"direction\":\"outbound\",\"depth\":1,\"include_overrides\":true,"
"\"format\":\"json\"}",
cases[i].caller);
char *response = cbm_mcp_handle_tool(srv, "trace_path", args);
char *inner = extract_text_content(response);
free(response);
ASSERT_NOT_NULL(inner);
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *root = yyjson_doc_get_root(doc);
ASSERT_EQ(yyjson_get_int(yyjson_obj_get(root, "callees_total")), 1);
yyjson_val *possible = yyjson_obj_get(root, "possible_callees");
ASSERT_TRUE(yyjson_is_arr(possible));
ASSERT_EQ(yyjson_arr_size(possible), cases[i].lists ? 1U : 0U);
yyjson_doc_free(doc);
free(inner);
}
cbm_node_free_fields(&contract);
cbm_mcp_server_free(srv);
PASS();
}
/* include_overrides joins the cursor identity only when it is set. Without
* it the token is the one main minted before the flag existed (the qhash
* below is main's formula for this request), so default output and cursors
* already handed out survive the upgrade; with it, the token differs. */
TEST(tool_trace_path_include_overrides_keeps_cursor_identity) {
cbm_mcp_server_t *srv = ov_fixture();
ASSERT_NOT_NULL(srv);
static const char *const variants[] = {
",\"limit\":1,\"format\":\"json\"",
",\"limit\":1,\"include_overrides\":false,\"format\":\"json\"",
",\"limit\":1,\"include_overrides\":true,\"format\":\"json\"",
};
for (size_t i = 0; i < sizeof(variants) / sizeof(variants[0]); i++) {
char *inner = ov_trace(srv, variants[i]);
ASSERT_NOT_NULL(inner);
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
ASSERT_NOT_NULL(doc);
const char *cursor =
yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), "next_cursor"));
ASSERT_NOT_NULL(cursor);
bool mains_token = strstr(cursor, ".6965863f18fa7040.") != NULL;
ASSERT_TRUE(mains_token == (i < 2));
yyjson_doc_free(doc);
free(inner);
}
cbm_mcp_server_free(srv);
PASS();
}
/* possible_callees is optional output: under max_output_tokens it yields
* before any graph row and says so, in both formats. */
TEST(tool_trace_path_possible_callees_yield_first_under_budget) {
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
ASSERT_NOT_NULL(srv);
cbm_store_t *st = cbm_mcp_server_store(srv);
const char *p = "override-budget";
cbm_mcp_server_set_project(srv, p);
ASSERT_EQ(cbm_store_upsert_project(st, p, "/tmp/override-budget"), CBM_STORE_OK);
int64_t hub = ov_node(st, p, "Function", "hub", "override_budget.hub", "src/hub.py");
int64_t base = ov_node(st, p, "Method", "run", "override_budget.Base.run", "src/hub.py");
ASSERT_TRUE(ov_edge(st, p, hub, base, "CALLS"));
enum { IMPLEMENTATIONS = 60 };
for (int i = 0; i < IMPLEMENTATIONS; i++) {
char qn[160];
snprintf(qn, sizeof(qn),
"override_budget.implementations.with.a.rather.long.package.path.Impl%02d.run", i);
int64_t impl = ov_node(st, p, "Method", "run", qn, "src/impls.py");
ASSERT_TRUE(ov_edge(st, p, impl, base, "OVERRIDE"));
}
const char *formats[] = {"json", "tree"};
for (size_t f = 0; f < sizeof(formats) / sizeof(formats[0]); f++) {
char args[320];
snprintf(args, sizeof(args),
"{\"project\":\"override-budget\",\"function_name\":\"hub\","
"\"direction\":\"outbound\",\"depth\":1,\"include_overrides\":true,"
"\"max_output_tokens\":300,\"format\":\"%s\"}",
formats[f]);
char *response = cbm_mcp_handle_tool(srv, "trace_path", args);
char *inner = extract_text_content(response);
free(response);
ASSERT_NOT_NULL(inner);
ASSERT_NULL(strstr(inner, "Impl00.run"));
ASSERT_NULL(strstr(inner, "output_budget_floor_exceeded"));
if (strcmp(formats[f], "json") == 0) {
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
ASSERT_NOT_NULL(doc);
yyjson_val *root = yyjson_doc_get_root(doc);
ASSERT_NULL(yyjson_obj_get(root, "possible_callees"));
ASSERT_TRUE(yyjson_get_bool(yyjson_obj_get(root, "possible_callees_omitted")));
ASSERT_FALSE(yyjson_get_bool(yyjson_obj_get(root, "has_more")));
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(root, "truncation_reason")),
"output_budget");
ASSERT_NOT_NULL(trace_grouped_row_named(yyjson_obj_get(root, "callees"), "run", NULL));
yyjson_doc_free(doc);
} else {
ASSERT_NOT_NULL(strstr(inner, "possible_callees_omitted: true"));
ASSERT_NOT_NULL(strstr(inner, "has_more: false"));
ASSERT_NOT_NULL(strstr(inner, "override_budget.Base"));
}
free(inner);
}
cbm_mcp_server_free(srv);
PASS();
}
/* Exactly-once trace pagination: 12 callees paged at limit=5 must yield
* 5+5+2 rows with every callee appearing on exactly one page, exact totals
* on every page, and a final page without a cursor. Stale and mismatched
@@ -28298,6 +28710,14 @@ SUITE(mcp) {
RUN_TEST(tool_trace_call_path_ambiguous);
RUN_TEST(tool_trace_path_definition_beats_c_macro_c1);
RUN_TEST(tool_trace_union_records_min_hop_across_seeds);
RUN_TEST(tool_trace_path_possible_callees_stay_apart_from_calls);
RUN_TEST(tool_trace_path_possible_callees_follow_the_hierarchy);
RUN_TEST(tool_trace_path_possible_callees_filter_tests_and_non_callables);
RUN_TEST(tool_trace_path_possible_callees_render_as_tree_table);
RUN_TEST(tool_trace_path_possible_callees_follow_the_page);
RUN_TEST(tool_trace_path_possible_callees_yield_first_under_budget);
RUN_TEST(tool_trace_path_possible_callees_only_for_dispatching_calls);
RUN_TEST(tool_trace_path_include_overrides_keeps_cursor_identity);
RUN_TEST(tool_trace_pagination_exactly_once);
RUN_TEST(tool_trace_paging_filters_before_window_and_hashes_effective_args);
RUN_TEST(tool_trace_budget_never_slices_identifiers);
+240
View File
@@ -2250,6 +2250,243 @@ TEST(vbnet_vendored_grammar_still_flags_broken_source) {
PASS();
}
/* ── VB6 / VBA: the vendored grammar parses real VB6 and VBA (#2564, #721) ──
* tools/tree-sitter-vba (harumiWeb/tree-sitter-vba v0.14.5, vba dialect) is
* vendored ahead of VB6/VBA extraction: no CBMLanguage routes to it yet, so
* these cases parse through tree_sitter_vba() directly. The class module is
* dual-target VB6 the way real control libraries write it: the VB6 IDE class
* header and Attribute lines, a Declare kept in both #If VBA7 branches
* (PtrSafe, LongPtr) plus the LongPtr shim enum VB6 needs, Implements, an
* Event, Property Get/Let/Set, a label, and bare calls with plain and member
* callees. It is RED on the alternatives: upstream's vb6 dialect rejects
* PtrSafe, and the vba dialect at v0.13.0 (the pin PR #2564 carried) leaves
* an ERROR at `Case Target Is Nothing` and turns `Err.Raise a, b, c` after
* the label into an expression_statement, losing the callee. The form is CRLF,
* as the VB6 IDE writes it. */
extern const TSLanguage *tree_sitter_vba(void);
static const char VBA_CLASS_SOURCE[] =
"VERSION 1.0 CLASS\n"
"BEGIN\n"
" MultiUse = -1 'True\n"
" Persistable = 0 'NotPersistable\n"
" DataBindingBehavior = 0 'vbNone\n"
" DataSourceBehavior = 0 'vbNone\n"
" MTSTransactionMode = 0 'NotAnMTSObject\n"
"END\n"
"Attribute VB_Name = \"Account\"\n"
"Attribute VB_GlobalNameSpace = False\n"
"Attribute VB_Creatable = True\n"
"Attribute VB_PredeclaredId = False\n"
"Attribute VB_Exposed = True\n"
"Option Explicit\n"
"Implements IComparable\n"
"\n"
"#If VBA7 Then\n"
"Private Declare PtrSafe Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" "
"(ByRef Destination As Any, ByRef Source As Any, ByVal Length As LongPtr)\n"
"#Else\n"
"Private Declare Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" (ByRef "
"Destination As Any, ByRef Source As Any, ByVal Length As Long)\n"
"#End If\n"
"\n"
"#If (VBA7 = 0) Then\n"
"Private Enum LongPtr\n"
" [_]\n"
"End Enum\n"
"#End If\n"
"\n"
"Public Event Changed(ByVal OldValue As Currency)\n"
"\n"
"Private Type BalanceInfo\n"
" Amount As Currency\n"
" Stamp As Date\n"
"End Type\n"
"\n"
"Private mOwner As String\n"
"Private mInfo As BalanceInfo\n"
"Private mHistory As Collection\n"
"\n"
"Private Sub Class_Initialize()\n"
" Set mHistory = New Collection\n"
"End Sub\n"
"\n"
"Public Property Get Owner() As String\n"
" Owner = mOwner\n"
"End Property\n"
"\n"
"Public Property Let Owner(ByVal Value As String)\n"
" mOwner = Value\n"
"End Property\n"
"\n"
"Public Property Set History(ByVal Value As Collection)\n"
" Set mHistory = Value\n"
"End Property\n"
"\n"
"Public Function Deposit(ByVal Amount As Currency) As Boolean\n"
" On Error GoTo Fail\n"
" Dim Old As Currency\n"
" Old = mInfo.Amount\n"
" mInfo.Amount = mInfo.Amount + Amount\n"
" mHistory.Add Amount\n"
" ' audit trail\n"
" LogLine Format$(Amount, \"0.00\"), True\n"
" RaiseEvent Changed(Old)\n"
" Deposit = True\n"
" Exit Function\n"
"Fail:\n"
" Err.Raise vbObjectError + 513, \"Account.Deposit\", \"deposit failed\"\n"
"End Function\n"
"\n"
"Public Sub Apply(ByVal Target As Object)\n"
" Dim Entry As Variant\n"
" For Each Entry In mHistory\n"
" CallByName Target, \"Owner\", VbLet, mOwner\n"
" Next\n"
" Select Case True\n"
" Case Target Is Nothing\n"
" Exit Sub\n"
" Case Else\n"
" With Target\n"
" .Refresh\n"
" End With\n"
" End Select\n"
"End Sub\n"
"\n"
"Private Function IComparable_CompareTo(ByVal Other As Object) As Long\n"
" IComparable_CompareTo = StrComp(mOwner, Other.Owner, vbTextCompare)\n"
"End Function\n";
static const char VBA_FORM_SOURCE[] =
"VERSION 5.00\r\n"
"Begin VB.Form frmMain\r\n"
" Caption = \"Accounts\"\r\n"
" ClientHeight = 3090\r\n"
" ClientLeft = 60\r\n"
" ClientTop = 450\r\n"
" ClientWidth = 4680\r\n"
" LinkTopic = \"Form1\"\r\n"
" ScaleHeight = 3090\r\n"
" ScaleWidth = 4680\r\n"
" StartUpPosition = 3 'Windows Default\r\n"
" Begin VB.CommandButton cmdDeposit\r\n"
" Caption = \"Deposit\"\r\n"
" Height = 495\r\n"
" Left = 1680\r\n"
" TabIndex = 0\r\n"
" Top = 1200\r\n"
" Width = 1215\r\n"
" End\r\n"
"End\r\n"
"Attribute VB_Name = \"frmMain\"\r\n"
"Attribute VB_GlobalNameSpace = False\r\n"
"Attribute VB_Creatable = False\r\n"
"Attribute VB_PredeclaredId = True\r\n"
"Attribute VB_Exposed = False\r\n"
"Option Explicit\r\n"
"\r\n"
"Private WithEvents mAccount As Account\r\n"
"\r\n"
"Private Sub cmdDeposit_Click()\r\n"
" If mAccount.Deposit(100) Then MsgBox \"Done\", vbInformation, Me.Caption\r\n"
"End Sub\r\n"
"\r\n"
"Private Sub mAccount_Changed(ByVal OldValue As Currency)\r\n"
" Debug.Print \"was \"; OldValue\r\n"
"End Sub\r\n";
/* Counts the nodes of `type` under `node`. */
static int vba_type_count(TSNode node, const char *type) {
int hits = strcmp(ts_node_type(node), type) == 0 ? 1 : 0;
uint32_t count = ts_node_child_count(node);
for (uint32_t i = 0; i < count; i++) {
hits += vba_type_count(ts_node_child(node, i), type);
}
return hits;
}
/* Counts the call_statement nodes under `node` whose callee text is exactly
* `callee`. */
static int vba_callee_count(TSNode node, const char *src, const char *callee) {
int hits = 0;
if (strcmp(ts_node_type(node), "call_statement") == 0) {
TSNode target = ts_node_child_by_field_name(node, "callee", 6);
if (!ts_node_is_null(target)) {
uint32_t start = ts_node_start_byte(target);
uint32_t len = ts_node_end_byte(target) - start;
if (strlen(callee) == len && memcmp(src + start, callee, len) == 0) {
hits++;
}
}
}
uint32_t count = ts_node_child_count(node);
for (uint32_t i = 0; i < count; i++) {
hits += vba_callee_count(ts_node_child(node, i), src, callee);
}
return hits;
}
TEST(vba_vendored_grammar_parses_dual_target_vb6_class_clean) {
TSParser *parser = ts_parser_new();
ASSERT_NOT_NULL(parser);
/* The vendored runtime accepts the generated ABI (15). */
ASSERT_TRUE(ts_parser_set_language(parser, tree_sitter_vba()));
TSTree *tree = ts_parser_parse_string(parser, NULL, VBA_CLASS_SOURCE,
(uint32_t)strlen(VBA_CLASS_SOURCE));
ASSERT_NOT_NULL(tree);
TSNode root = ts_tree_root_node(tree);
ASSERT_STR_EQ(ts_node_type(root), "source_file");
ASSERT_FALSE(ts_node_has_error(root));
/* Every procedure is a declaration node an extractor can index. */
ASSERT_EQ(vba_type_count(root, "sub_declaration"), 2);
ASSERT_EQ(vba_type_count(root, "function_declaration"), 2);
ASSERT_EQ(vba_type_count(root, "property_get_declaration"), 1);
ASSERT_EQ(vba_type_count(root, "property_let_declaration"), 1);
ASSERT_EQ(vba_type_count(root, "property_set_declaration"), 1);
ASSERT_EQ(vba_type_count(root, "declare_sub_statement"), 2);
/* Bare calls keep their callee; none is split into an expression. */
ASSERT_EQ(vba_type_count(root, "expression_statement"), 0);
ASSERT_EQ(vba_callee_count(root, VBA_CLASS_SOURCE, "Err.Raise"), 1);
ASSERT_EQ(vba_callee_count(root, VBA_CLASS_SOURCE, "CallByName"), 1);
ASSERT_EQ(vba_callee_count(root, VBA_CLASS_SOURCE, "LogLine"), 1);
ASSERT_EQ(vba_callee_count(root, VBA_CLASS_SOURCE, "mHistory.Add"), 1);
ts_tree_delete(tree);
ts_parser_delete(parser);
PASS();
}
TEST(vba_vendored_grammar_parses_vb6_form_clean) {
TSParser *parser = ts_parser_new();
ASSERT_NOT_NULL(parser);
ASSERT_TRUE(ts_parser_set_language(parser, tree_sitter_vba()));
TSTree *tree = ts_parser_parse_string(parser, NULL, VBA_FORM_SOURCE,
(uint32_t)strlen(VBA_FORM_SOURCE));
ASSERT_NOT_NULL(tree);
TSNode root = ts_tree_root_node(tree);
ASSERT_FALSE(ts_node_has_error(root));
/* The designer header is structure, not code: the form and its button
* are two nested begin blocks ahead of the two event handlers. */
ASSERT_STR_EQ(ts_node_type(ts_node_named_child(root, 0)), "frm_version_statement");
ASSERT_EQ(vba_type_count(root, "frm_begin_block"), 2);
ASSERT_EQ(vba_type_count(root, "sub_declaration"), 2);
ts_tree_delete(tree);
ts_parser_delete(parser);
PASS();
}
TEST(vba_vendored_grammar_still_flags_broken_source) {
static const char broken[] = "Public Sub Broken(\n x = \nEnd Sub\n";
TSParser *parser = ts_parser_new();
ASSERT_NOT_NULL(parser);
ASSERT_TRUE(ts_parser_set_language(parser, tree_sitter_vba()));
TSTree *tree = ts_parser_parse_string(parser, NULL, broken, (uint32_t)strlen(broken));
ASSERT_NOT_NULL(tree);
ASSERT_TRUE(ts_node_has_error(ts_tree_root_node(tree)));
ts_tree_delete(tree);
ts_parser_delete(parser);
PASS();
}
/* ── SQLite migrations (#2246) ───────────────────────────────────────────────
* The vendored SQL grammar is a self-maintained fork of DerekStride/tree-sitter-sql
* (tools/tree-sitter-sql + patches/sqlite-dialect.patch). Upstream rejected
@@ -2580,6 +2817,9 @@ SUITE(parse_coverage) {
RUN_TEST(pascal_broken_source_still_flagged_issue2211);
RUN_TEST(vbnet_vendored_grammar_parses_real_vb_clean);
RUN_TEST(vbnet_vendored_grammar_still_flags_broken_source);
RUN_TEST(vba_vendored_grammar_parses_dual_target_vb6_class_clean);
RUN_TEST(vba_vendored_grammar_parses_vb6_form_clean);
RUN_TEST(vba_vendored_grammar_still_flags_broken_source);
RUN_TEST(sqlite_valid_migration_not_flagged);
RUN_TEST(sqlite_malformed_still_flagged);
+9 -4
View File
@@ -69,9 +69,9 @@ static cbm_path_alias_map_t *make_map(const char *base_url, int count, ...) {
return map;
}
/* The map produced by make_map() owns all heap memory the same way the
* loader does, so the public free routine on the wrapping collection
* works equivalently — but tests build naked maps, so use this helper. */
/* make_map() allocates with the C allocator, the loader through the memory
* core, so a map built here is freed with this helper and never handed to
* cbm_path_alias_collection_free. */
static void free_map(cbm_path_alias_map_t *map) {
if (!map) {
return;
@@ -212,7 +212,12 @@ TEST(path_alias_find_for_file_nearest_ancestor) {
ASSERT_EQ(m2->count, 1);
ASSERT_STR_EQ(m2->entries[0].alias_prefix, "@root/");
cbm_path_alias_collection_free(coll);
for (int i = 0; i < coll->count; i++) {
free(coll->scopes[i].dir_prefix);
free_map(coll->scopes[i].map);
}
free(coll->scopes);
free(coll);
PASS();
}
+199 -8
View File
@@ -2848,6 +2848,41 @@ TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes) {
PASS();
}
/* C `x++` and `--x` write x. The C assignment node set held only
* assignment_expression, so an increment or a decrement left no WRITES edge,
* although the write resolver already handles update_expression and `x = 0`
* in the same file did write. */
TEST(pipeline_c_increment_writes) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_incwrite_XXXXXX");
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
th_write_file(TH_PATH(tmp, "src/counter.c"),
"struct counter { int n; };\n"
"int hits;\n"
"void hits_reset(void) { hits = 0; }\n"
"void hits_bump(void) { hits++; }\n"
"void hits_drop(void) { --hits; }\n"
"void counter_tick(struct counter *c) { c->n++; }\n");
char db_path[512];
snprintf(db_path, sizeof(db_path), "%s/incwrite.db", tmp);
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
ASSERT_NOT_NULL(p);
ASSERT_EQ(cbm_pipeline_run(p), 0);
const char *project = cbm_pipeline_project_name(p);
cbm_store_t *store = cbm_store_open_path(db_path);
ASSERT_NOT_NULL(store);
/* The plain assignment shows the fixture can produce WRITES at all. */
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "hits_reset", "hits", "Variable"), 1);
/* Postfix and prefix on a file-level variable, postfix on a member. */
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "hits_bump", "hits", "Variable"), 1);
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "hits_drop", "hits", "Variable"), 1);
ASSERT_EQ(labeled_edge_count(store, project, "WRITES", "counter_tick", "n", "Field"), 1);
cbm_store_close(store);
cbm_pipeline_free(p);
th_cleanup(tmp);
PASS();
}
typedef struct {
int run_rc;
bool store_opened;
@@ -6907,6 +6942,157 @@ TEST(pipeline_registry_test_caller_ranks_test_helpers_full_and_incremental) {
PASS();
}
/* The receiver gate's fixture: member calls whose receivers are lambda
* parameters nothing types, so every one of them reaches the registry's
* name-only exits, a C call through a function-pointer member, and a Go call
* on a standard-library package. */
static const char GATE_CALLER_PATH[] = "streams/src/main/java/org/acme/streams/Assign.java";
static const char GATE_CALLER[] =
"package org.acme.streams;\n\nimport java.util.List;\n\npublic class Assign {\n"
" long count(List<Object> tps, String t) {\n"
" return tps.stream().filter(tp -> tp.topic().equals(t)).count();\n }\n\n"
" long found(List<Object> repos) {\n"
" return repos.stream().filter(repo -> repo.find() > 0).count();\n }\n\n"
" long cleaned(List<Object> ms) {\n"
" return ms.stream().filter(cleanerManager -> cleanerManager.cleaningState() > 0)"
".count();\n }\n\n"
" Object built() {\n return new RaftContext.Builder().withVoters();\n }\n\n"
" void named() {\n new External().setName(\"x\");\n }\n\n"
" long parts(List<Object> ts) {\n"
" return ts.stream().filter(topic -> topic.numPartitions() > 0).count();\n }\n"
"}\n";
static const char GATE_C_PATH[] = "csrc/alloc.c";
static const char GATE_C[] = "#include <stddef.h>\n\n"
"typedef struct allocFuncs {\n void *(*mallocFn)(size_t);\n} "
"allocFuncs;\n\n"
"static void *plain_malloc(size_t n) { return NULL; }\n\n"
"allocFuncs allocFns = {plain_malloc};\n\n"
"void *my_malloc(size_t n) {\n return allocFns.mallocFn(n);\n}\n";
static const char GATE_GO_PATH[] = "gosrc/cmd/main.go";
static const char GATE_GO[] = "package cmd\n\nimport \"strings\"\n\n"
"func Run(parts []string) string {\n"
"\treturn strings.Join(parts, \",\")\n}\n";
static const char GATE_RECORD[] =
"clients/src/main/java/org/acme/clients/consumer/ConsumerRecord.java";
static const char GATE_PRODUCER[] =
"clients/src/main/java/org/acme/clients/producer/ProducerRecord.java";
static const char GATE_REPO[] = "clients/src/main/java/org/acme/clients/store/Repo.java";
static const char GATE_INDEX[] = "core/src/main/java/org/acme/index/Index.java";
static const char GATE_LOGGER[] = "core/src/main/java/org/acme/log/Logger.java";
static const char GATE_GO_JOIN[] = "gosrc/util/join.go";
static const char GATE_SCALA_PATH[] = "streams/src/main/scala/org/acme/streams/Assigner.scala";
static const char GATE_SCALA[] =
"package org.acme.streams\n\nobject Assigner {\n"
" def count(tps: Seq[AnyRef], t: String): Int = tps.count(tp => tp.topicName() == t)\n\n"
" def named(rs: Seq[AnyRef], t: String): Int =\n"
" rs.count(consumerRecordS => consumerRecordS.topicName() == t)\n}\n";
static const char GATE_SCALA_CONSUMER[] =
"clients/src/main/scala/org/acme/clients/consumer/Record.scala";
static const char GATE_SCALA_PRODUCER[] =
"clients/src/main/scala/org/acme/clients/producer/Record.scala";
static void gate_fixture(const char *tmp) {
write_temp_file(tmp, GATE_RECORD,
"package org.acme.clients.consumer;\n\npublic class ConsumerRecord {\n"
" public String topic() { return \"t\"; }\n}\n");
write_temp_file(tmp, GATE_PRODUCER,
"package org.acme.clients.producer;\n\npublic class ProducerRecord {\n"
" public String topic() { return \"p\"; }\n}\n");
write_temp_file(tmp, GATE_REPO,
"package org.acme.clients.store;\n\npublic class Repo {\n"
" public int find() { return 1; }\n}\n");
write_temp_file(tmp, GATE_INDEX,
"package org.acme.index;\n\npublic class Index {\n"
" public int find() { return 2; }\n}\n");
write_temp_file(tmp, "core/src/main/java/org/acme/log/LogCleanerManager.java",
"package org.acme.log;\n\npublic class LogCleanerManager {\n"
" public int cleaningState() { return 1; }\n}\n");
write_temp_file(tmp, GATE_LOGGER,
"package org.acme.log;\n\npublic class Logger {\n"
" public void setName(String name) {}\n}\n");
write_temp_file(tmp, "clients/src/main/java/org/acme/clients/RaftContext.java",
"package org.acme.clients;\n\npublic class RaftContext {\n"
" public static class Builder {\n"
" public Builder withVoters() { return this; }\n }\n}\n");
write_temp_file(tmp, "streams/src/main/java/org/acme/streams/TopicInfo.java",
"package org.acme.streams;\n\npublic class TopicInfo {\n"
" public int numPartitions() { return 1; }\n}\n");
write_temp_file(tmp, "go.mod", "module example.com/gate\n\ngo 1.22\n");
write_temp_file(tmp, GATE_GO_JOIN,
"package util\n\nfunc Join(parts []string) string { return \"\" }\n");
write_temp_file(tmp, GATE_SCALA_CONSUMER,
"package org.acme.clients.consumer\n\nclass ConsumerRecordS {\n"
" def topicName(): String = \"t\"\n}\n");
write_temp_file(tmp, GATE_SCALA_PRODUCER,
"package org.acme.clients.producer\n\nclass ProducerRecordS {\n"
" def topicName(): String = \"p\"\n}\n");
write_temp_file(tmp, GATE_CALLER_PATH, GATE_CALLER);
write_temp_file(tmp, GATE_SCALA_PATH, GATE_SCALA);
write_temp_file(tmp, GATE_C_PATH, GATE_C);
write_temp_file(tmp, GATE_GO_PATH, GATE_GO);
}
/* A member call whose receiver type nothing resolved binds by name only on
* evidence that it means the candidate. Refused: Scala's proximity pick
* between two same-named methods for `tp.topicName()` (kafka's `tp.topic()`
* shape), and Go's `strings.Join` onto the project's one Join. Kept: the
* Scala receiver named after its owner, and a C call through a
* function-pointer member, which the C member resolver types (C is off the
* gate's allowlist). A veto, not a re-pick: a refused call stays unresolved.
* Full index (sequential and parallel) and both incremental routes. Java is
* off the allowlist too (the registry test pins every language's scope); the
* Java files only share the fixture. */
TEST(pipeline_registry_receiver_gate_full_and_incremental) {
static const struct {
const char *source;
const char *target;
const char *file;
const char *other;
const char *caller_path;
const char *caller;
int expect;
} cases[] = {
{"count", "topicName", GATE_SCALA_CONSUMER, GATE_SCALA_PRODUCER, GATE_SCALA_PATH,
GATE_SCALA, 0},
{"named", "topicName", GATE_SCALA_CONSUMER, GATE_SCALA_PRODUCER, GATE_SCALA_PATH,
GATE_SCALA, 1},
{"my_malloc", "mallocFn", GATE_C_PATH, GATE_INDEX, GATE_C_PATH, GATE_C, 1},
{"Run", "Join", GATE_GO_JOIN, GATE_INDEX, GATE_GO_PATH, GATE_GO, 0},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_gate_%zu_XXXXXX", i);
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
gate_fixture(tmp);
rank_probe_t r;
rank_probe(tmp, cases[i].caller_path, cases[i].caller, cases[i].source, cases[i].target,
cases[i].file, cases[i].other, &r);
int par = -1;
int par_other = -1;
rank_probe_parallel(tmp, cases[i].source, cases[i].target, cases[i].file, cases[i].other,
&par, &par_other);
th_rmtree(tmp);
int want = cases[i].expect;
bool routes_ok = r.route[1] == CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR;
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
routes_ok = routes_ok && r.route[2] == CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL;
#endif
bool others_zero = r.other[0] == 0 && par_other == 0 && r.other[1] == 0 && r.other[2] == 0;
if (r.want[0] != want || par != want || !routes_ok || r.want[1] != want ||
r.want[2] != want || !others_zero) {
char why[256];
snprintf(why, sizeof(why),
"%s -> %s: want %d, got full %d parallel %d closure %d legacy %d, other "
"%d %d %d %d (routes %d %d)",
cases[i].source, cases[i].target, want, r.want[0], par, r.want[1], r.want[2],
r.other[0], par_other, r.other[1], r.other[2], (int)r.route[1],
(int)r.route[2]);
FAIL(why);
}
}
PASS();
}
/* A semantic control file can change an unchanged source file's exact target.
* Both target modules deliberately export the same short name: without the
* tsconfig path mapping, project-wide unique-name fallback is ambiguous and
@@ -28534,15 +28720,17 @@ TEST(registry_receiver_chain_keyword_list_gated_to_csharp_issue2121) {
ASSERT_STR_EQ(r.strategy, "unique_name");
/* A Go call and the language-blind cbm_registry_resolve read a second
* keyword spelling as a value root as well. */
* keyword spelling as a value root as well. A Go value receiver binds a
* name-only candidate on evidence (the receiver gate): here the candidate
* lives in the caller's own module, which a type root would not admit. */
cbm_registry_t *reg2 = cbm_registry_new();
cbm_registry_add(reg2, "Join", "tmp.Decoys.Joiner.Join", "Method", false);
cbm_registry_add(reg2, "Join", "tmp.Demo.Joiner.Join", "Method", false);
cbm_resolution_t r2 =
cbm_registry_resolve_call_lang(reg2, "string.Join", "tmp.Demo", NULL, NULL, 0, CBM_LANG_GO);
ASSERT_STR_EQ(r2.qualified_name, "tmp.Decoys.Joiner.Join");
ASSERT_STR_EQ(r2.qualified_name, "tmp.Demo.Joiner.Join");
ASSERT_STR_EQ(r2.strategy, "unique_name");
r2 = cbm_registry_resolve(reg2, "string.Join", "tmp.Demo", NULL, NULL, 0);
ASSERT_STR_EQ(r2.qualified_name, "tmp.Decoys.Joiner.Join");
ASSERT_STR_EQ(r2.qualified_name, "tmp.Demo.Joiner.Join");
ASSERT_STR_EQ(r2.strategy, "unique_name");
cbm_registry_free(reg);
@@ -28554,15 +28742,16 @@ TEST(registry_receiver_chain_keyword_list_gated_to_csharp_issue2121) {
* `int`: in receiver position (`dynamic.Invoke(...)`) it is a variable that
* happens to be called `dynamic`, whose type the chain does not show. In a C#
* file it must stay an ordinary lower-case value root, like vm.load above, so
* the call still binds: the control that keeps the keyword list closed to
* words that can only name a type. */
* the call still binds on a value receiver's evidence (here: a candidate in
* the caller's own module, which a type root would not admit): the control
* that keeps the keyword list closed to words that can only name a type. */
TEST(registry_receiver_chain_csharp_dynamic_variable_still_binds_issue2121) {
cbm_registry_t *reg = cbm_registry_new();
cbm_registry_add(reg, "Invoke", "tmp.Handlers.Handler.Invoke", "Method", false);
cbm_registry_add(reg, "Invoke", "tmp.Demo.Handler.Invoke", "Method", false);
cbm_resolution_t r = cbm_registry_resolve_call_lang(reg, "dynamic.Invoke", "tmp.Demo", NULL,
NULL, 0, CBM_LANG_CSHARP);
ASSERT_STR_EQ(r.qualified_name, "tmp.Handlers.Handler.Invoke");
ASSERT_STR_EQ(r.qualified_name, "tmp.Demo.Handler.Invoke");
ASSERT_STR_EQ(r.strategy, "unique_name");
cbm_registry_free(reg);
@@ -34878,6 +35067,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_incremental_keeps_ansible_notifies_across_files);
RUN_TEST(pipeline_incremental_keeps_ansible_deploys_across_files);
RUN_TEST(pipeline_field_named_like_method_keeps_its_reads_and_writes);
RUN_TEST(pipeline_c_increment_writes);
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);
RUN_TEST(pipeline_js_route_not_bound_to_python_builtin_sequential);
@@ -34898,6 +35088,7 @@ SUITE(pipeline) {
RUN_TEST(pipeline_registry_ranks_test_file_variable_last_full_and_incremental);
RUN_TEST(pipeline_registry_ranks_test_file_class_last_full_and_incremental);
RUN_TEST(pipeline_registry_test_caller_ranks_test_helpers_full_and_incremental);
RUN_TEST(pipeline_registry_receiver_gate_full_and_incremental);
RUN_TEST(pipeline_incremental_changed_target_invalidates_stale_inbound_call_reference);
RUN_TEST(pipeline_incremental_parallel_registry_nodes_advance_shared_ids);
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+184
View File
@@ -1706,6 +1706,187 @@ TEST(c_bare_call_does_not_weigh_field_namesakes) {
PASS();
}
static bool gate_bound(const cbm_resolution_t *res, const char *qn) {
return res->qualified_name && strcmp(res->qualified_name, qn) == 0;
}
static bool gate_empty(const cbm_resolution_t *res) {
return !res->qualified_name || res->qualified_name[0] == '\0';
}
/* The receiver gate at the name-only CALLS exits judges a member call whose
* receiver is a value (`tp.topic`): it binds only on evidence that the call
* means the candidate, and is refused otherwise -- a veto, never a re-pick.
* The evidence rules are shown on C#, where unique_name binds are judged too. */
TEST(registry_receiver_gate_judges_value_receivers) {
cbm_registry_t *r = cbm_registry_new();
const char *mod = "proj.streams.src.main.java.org.acme.streams";
const char *topic = "proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord.topic";
const char *clean = "proj.core.src.main.java.org.acme.log.LogCleanerManager.cleaningState";
const char *with = "proj.clients.src.main.java.org.acme.clients.RaftContext.Builder.withVoters";
const char *set_name = "proj.core.src.main.java.org.acme.log.Logger.setName";
const char *parts = "proj.streams.src.main.java.org.acme.streams.TopicInfo.numPartitions";
const char *guard = "proj.src.shipping.src.telemetry_conf.OtelGuard.shutdown";
cbm_registry_add_lang(r, "topic", topic, "Method", CBM_LANG_CSHARP, false);
cbm_registry_add_lang(r, "cleaningState", clean, "Method", CBM_LANG_CSHARP, false);
cbm_registry_add_lang(r, "withVoters", with, "Method", CBM_LANG_CSHARP, false);
cbm_registry_add_lang(r, "setName", set_name, "Method", CBM_LANG_CSHARP, false);
cbm_registry_add_lang(r, "numPartitions", parts, "Method", CBM_LANG_CSHARP, false);
cbm_registry_add_lang(r, "shutdown", guard, "Method", CBM_LANG_CPP, false);
const CBMLanguage cs = CBM_LANG_CSHARP;
/* Nothing ties `tp` to ConsumerRecord: refused on the CALLS path, while
* every other resolve keeps its answer. */
cbm_resolution_t res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_empty(&res));
res = cbm_registry_resolve_lang(r, "tp.topic", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_bound(&res, topic));
/* The file imports the candidate's class: bound. */
const char *vals[] = {"proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord"};
const char *keys[] = {"ConsumerRecord"};
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, keys, vals, 1, cs);
ASSERT_TRUE(gate_bound(&res, topic));
/* Self receivers are not unknown, protobuf's `this_` included. */
res = cbm_registry_resolve_call_lang(r, "this.topic", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_bound(&res, topic));
res = cbm_registry_resolve_call_lang(r, "this_.topic", mod, NULL, NULL, 0, CBM_LANG_CPP);
ASSERT_TRUE(gate_bound(&res, topic));
res =
cbm_registry_resolve_call_lang(r, "self.items.topic", mod, NULL, NULL, 0, CBM_LANG_PYTHON);
ASSERT_TRUE(gate_bound(&res, topic));
/* A receiver named after the owner type, compared word by word: one name's
* words ending the other's, snake_case and a PHP sigil included. */
res = cbm_registry_resolve_call_lang(r, "cleanerManager.cleaningState", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_bound(&res, clean));
res = cbm_registry_resolve_call_lang(r, "spyLogCleanerManager2.cleaningState", mod, NULL, NULL,
0, cs);
ASSERT_TRUE(gate_bound(&res, clean));
res =
cbm_registry_resolve_call_lang(r, "anotherManagerX.cleaningState", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_empty(&res));
res = cbm_registry_resolve_call_lang(r, "verify(cleanerManager).cleaningState", mod, NULL, NULL,
0, cs);
ASSERT_TRUE(gate_bound(&res, clean));
res = cbm_registry_resolve_call_lang(r, "otel_guard.shutdown", "proj.src.shipping.src.main",
NULL, NULL, 0, CBM_LANG_CPP);
ASSERT_TRUE(gate_bound(&res, guard));
res = cbm_registry_resolve_call_lang(r, "$logger.setName", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_bound(&res, set_name));
/* Languages with their own member-call guard after resolution are left
* to it. */
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, CBM_LANG_TYPESCRIPT);
ASSERT_TRUE(gate_bound(&res, topic));
/* A constructor root names its type. */
res = cbm_registry_resolve_call_lang(r, "new RaftContext.Builder(voters, 1).withVoters", mod,
NULL, NULL, 0, cs);
ASSERT_TRUE(gate_bound(&res, with));
res = cbm_registry_resolve_call_lang(r, "new External().setName", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_empty(&res));
/* A candidate in the caller's own module. */
res = cbm_registry_resolve_call_lang(r, "topic.numPartitions", mod, NULL, NULL, 0, cs);
ASSERT_TRUE(gate_bound(&res, parts));
/* C++ (and C): a member call landing on a struct member is decided by the C
* member resolver from the object's type. */
const char *c_mod = "proj.src.redis";
cbm_registry_add_lang(r, "mallocFn", "proj.deps.hiredis.alloc.hiredisAllocFuncs.mallocFn",
"Field", CBM_LANG_CPP, false);
res = cbm_registry_resolve_call_lang(r, "hiredisAllocFns.mallocFn", c_mod, NULL, NULL, 0,
CBM_LANG_CPP);
ASSERT_TRUE(gate_bound(&res, "proj.deps.hiredis.alloc.hiredisAllocFuncs.mallocFn"));
/* C++: a candidate whose owner type the caller's own file declares (a class
* re-declared above `Widget w(n); w.get_id()`); refused from a file that
* declares no Widget. */
const char *get_id = "proj.src.widget.Widget.get_id";
cbm_registry_add_lang(r, "get_id", get_id, "Method", CBM_LANG_CPP, false);
cbm_registry_add_lang(r, "Widget", "proj.src.main.Widget", "Class", CBM_LANG_CPP, false);
res =
cbm_registry_resolve_call_lang(r, "w.get_id", "proj.src.main", NULL, NULL, 0, CBM_LANG_CPP);
ASSERT_TRUE(gate_bound(&res, get_id));
res = cbm_registry_resolve_call_lang(r, "w.get_id", "proj.src.other", NULL, NULL, 0,
CBM_LANG_CPP);
ASSERT_TRUE(gate_empty(&res));
cbm_registry_free(r);
PASS();
}
/* The gate runs by measured language (an allowlist). Whole for Go and Elixir:
* a unique_name bind is judged too. Suffix-only for Scala, Rust and PHP: they
* keep their unique_name binds and only a suffix_match pick is judged. Off
* for every other language -- measured below the bar (Java, Kotlin, Swift,
* Ruby, Objective-C), refusing nothing (C, Dart), or not measured (Zig). */
TEST(registry_receiver_gate_runs_by_measured_language) {
cbm_registry_t *r = cbm_registry_new();
const char *mod = "proj.streams.src.main.scala.org.acme.streams";
const char *consumer =
"proj.clients.src.main.java.org.acme.clients.consumer.ConsumerRecord.topic";
const char *producer =
"proj.clients.src.main.java.org.acme.clients.producer.ProducerRecord.topic";
static const CBMLanguage full[] = {CBM_LANG_GO, CBM_LANG_ELIXIR};
static const CBMLanguage suffix_only[] = {CBM_LANG_SCALA, CBM_LANG_RUST, CBM_LANG_PHP};
static const CBMLanguage off[] = {CBM_LANG_JAVA, CBM_LANG_KOTLIN, CBM_LANG_SWIFT, CBM_LANG_RUBY,
CBM_LANG_OBJC, CBM_LANG_C, CBM_LANG_DART, CBM_LANG_ZIG};
cbm_registry_add_lang(r, "topic", consumer, "Method", CBM_LANG_JAVA, false);
/* One definition of the name: refused only where the gate is whole. */
cbm_resolution_t res;
for (size_t i = 0; i < sizeof(full) / sizeof(full[0]); i++) {
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, full[i]);
ASSERT_TRUE(gate_empty(&res));
}
for (size_t i = 0; i < sizeof(suffix_only) / sizeof(suffix_only[0]); i++) {
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, suffix_only[i]);
ASSERT_TRUE(gate_bound(&res, consumer));
}
for (size_t i = 0; i < sizeof(off) / sizeof(off[0]); i++) {
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, off[i]);
ASSERT_TRUE(gate_bound(&res, consumer));
}
/* Two: the proximity pick between them is refused where the gate runs,
* unless the receiver names its owner; the other languages keep theirs. */
cbm_registry_add_lang(r, "topic", producer, "Method", CBM_LANG_JAVA, false);
for (size_t i = 0; i < sizeof(suffix_only) / sizeof(suffix_only[0]); i++) {
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, suffix_only[i]);
ASSERT_TRUE(gate_empty(&res));
res = cbm_registry_resolve_call_lang(r, "consumerRecord.topic", mod, NULL, NULL, 0,
suffix_only[i]);
ASSERT_TRUE(gate_bound(&res, consumer));
}
for (size_t i = 0; i < sizeof(off) / sizeof(off[0]); i++) {
res = cbm_registry_resolve_call_lang(r, "tp.topic", mod, NULL, NULL, 0, off[i]);
ASSERT_TRUE(gate_bound(&res, consumer));
}
cbm_registry_free(r);
PASS();
}
/* A chain rooted in a TYPE (`Future.exception`) in a file with imports: no
* candidate is import-reachable, so the fallback ranks every candidate -- a
* name-only guess that now passes the same type check as the other exits.
* When some candidate's owner IS named, the pick is left alone for the
* parallel resolver's field-type hint. */
TEST(registry_receiver_gate_judges_type_roots_at_import_fallback) {
cbm_registry_t *r = cbm_registry_new();
const char *mod = "proj.finagle-core.src.main.scala.com.twitter.finagle.service";
cbm_registry_add_lang(r, "exception",
"proj.finagle-thrift.src.main.scala.com.twitter.finagle.thrift."
"MultiplexedService.MultiplexedFinagleService.exception",
"Method", CBM_LANG_SCALA, false);
cbm_registry_add_lang(
r, "exception", "proj.finagle-mux.src.main.scala.com.twitter.finagle.mux.Message.exception",
"Method", CBM_LANG_SCALA, false);
const char *vals[] = {"proj.finagle-core.src.main.scala.com.twitter.finagle.Stack"};
const char *keys[] = {"Stack"};
cbm_resolution_t res =
cbm_registry_resolve_call_lang(r, "Future.exception", mod, keys, vals, 1, CBM_LANG_SCALA);
ASSERT_TRUE(gate_empty(&res));
/* The reference resolver keeps its answer. */
res = cbm_registry_resolve_lang(r, "Future.exception", mod, keys, vals, 1, CBM_LANG_SCALA);
ASSERT_FALSE(gate_empty(&res));
cbm_registry_free(r);
PASS();
}
TEST(call_onto_field_follows_language_and_shape) {
/* A call the registry resolved onto a Field node. */
/* Another language: only the spelling connects them. */
@@ -3518,6 +3699,9 @@ SUITE(registry) {
RUN_TEST(go_bare_ref_never_binds_field);
RUN_TEST(go_bare_init_never_binds_init_namesake);
RUN_TEST(c_bare_ref_never_binds_field);
RUN_TEST(registry_receiver_gate_judges_value_receivers);
RUN_TEST(registry_receiver_gate_runs_by_measured_language);
RUN_TEST(registry_receiver_gate_judges_type_roots_at_import_fallback);
RUN_TEST(call_onto_field_follows_language_and_shape);
RUN_TEST(c_bare_call_does_not_weigh_field_namesakes);
RUN_TEST(c_arrow_member_call_is_c_family_and_arrow_only);
File diff suppressed because it is too large Load Diff
+9
View File
@@ -0,0 +1,9 @@
{
"name": "tree-sitter-vba-cbm",
"version": "0.14.5-cbm.1",
"description": "cbm fork of tree-sitter-vba @ 83c5c3c (vba dialect, generated with tree-sitter-cli 0.25.10)",
"license": "MIT",
"devDependencies": {
"tree-sitter-cli": "0.25.10"
}
}
+16
View File
@@ -0,0 +1,16 @@
{
"grammars": [
{
"name": "vba",
"camelcase": "Vba",
"scope": "source.vba",
"path": "vba",
"file-types": ["bas", "cls", "frm"]
}
],
"metadata": {
"version": "0.14.5-cbm.1",
"license": "MIT",
"description": "cbm fork of tree-sitter-vba @ 83c5c3c (vba dialect, generated with tree-sitter-cli 0.25.10)"
}
}
+50
View File
@@ -0,0 +1,50 @@
/*
* cbm fork of tree-sitter-vba (Visual Basic for Applications and VB6),
* vba dialect
*
* Copyright (c) 2026 harumiWeb (tree-sitter-vba, MIT)
*
* The VENDORED grammar ships upstream's MIT notice byte-identical:
* internal/cbm/vendored/grammars/vba/LICENSE.
*
* Fork provenance (pinned):
* harumiWeb/tree-sitter-vba @ 83c5c3c3c7210468fb6745b585b5fb6532aa2376
* (tag v0.14.5, upstream main HEAD). Upstream checks in no generated
* parser.c (its docs/adr/0002), so the parser is generated here.
* common/define-grammar.js is upstream's file byte-identical
* (git blob 5e1f7d758ce1414dbe9ec01e5236bc1c29f62a9a).
* This file = this 40-line header + upstream's vba/grammar.js
* (`tail -n +41 vba/grammar.js | git hash-object --stdin` gives
* 9247dbbfe166d139a28b10e9c960829ad6d265fc). No local patch.
*
* Why the vba dialect: upstream also generates a vb6 dialect from the same
* core. On real VB6 code both expose the same procedures, but the vb6 dialect
* rejects the VBA7 forms (PtrSafe, LongPtr) that dual-target VB6 sources keep
* inside #If VBA7 blocks, and its error recovery then swallows whole files;
* it is also 75.8 MB of parser.c against 48.3 MB here. The numbers are in
* internal/cbm/vendored/grammars/MANIFEST.md.
*
* Regenerate with exactly tree-sitter-cli 0.25.10 (the generator every fork
* here uses; the vendored runtime's ceiling is ABI 15):
* cd tools/tree-sitter-vba
* npx tree-sitter-cli@0.25.10 generate --abi 15 --output vba/src vba/grammar.js
* cp vba/src/parser.c ../../internal/cbm/vendored/grammars/vba/
* cp vba/src/tree_sitter/alloc.h vba/src/tree_sitter/array.h \
* vba/src/tree_sitter/parser.h \
* ../../internal/cbm/vendored/grammars/vba/tree_sitter/
*
* On an upstream bump: re-fetch common/define-grammar.js and vba/grammar.js
* at the new tag, regenerate twice (the two parser.c must be byte-identical),
* and re-run vba_vendored_grammar_* in tests/test_parse_coverage.c.
*/
/**
* @file Tree-sitter grammar for Visual Basic for Applications (VBA) - entry point
* @author harumiWeb
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = require("../common/define-grammar")("vba");
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,54 @@
#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Allow clients to override allocation functions
#ifdef TREE_SITTER_REUSE_ALLOCATOR
extern void *(*ts_current_malloc)(size_t size);
extern void *(*ts_current_calloc)(size_t count, size_t size);
extern void *(*ts_current_realloc)(void *ptr, size_t size);
extern void (*ts_current_free)(void *ptr);
#ifndef ts_malloc
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_current_free
#endif
#else
#ifndef ts_malloc
#define ts_malloc malloc
#endif
#ifndef ts_calloc
#define ts_calloc calloc
#endif
#ifndef ts_realloc
#define ts_realloc realloc
#endif
#ifndef ts_free
#define ts_free free
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ALLOC_H_
@@ -0,0 +1,291 @@
#ifndef TREE_SITTER_ARRAY_H_
#define TREE_SITTER_ARRAY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "./alloc.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4101)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#define Array(T) \
struct { \
T *contents; \
uint32_t size; \
uint32_t capacity; \
}
/// Initialize an array.
#define array_init(self) \
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
/// Create an empty array.
#define array_new() \
{ NULL, 0, 0 }
/// Get a pointer to the element at a given `index` in the array.
#define array_get(self, _index) \
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
/// Get a pointer to the first element in the array.
#define array_front(self) array_get(self, 0)
/// Get a pointer to the last element in the array.
#define array_back(self) array_get(self, (self)->size - 1)
/// Clear the array, setting its size to zero. Note that this does not free any
/// memory allocated for the array's contents.
#define array_clear(self) ((self)->size = 0)
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
/// less than the array's current capacity, this function has no effect.
#define array_reserve(self, new_capacity) \
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
/// Free any memory allocated for this array. Note that this does not free any
/// memory allocated for the array's contents.
#define array_delete(self) _array__delete((Array *)(self))
/// Push a new `element` onto the end of the array.
#define array_push(self, element) \
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
(self)->contents[(self)->size++] = (element))
/// Increase the array's size by `count` elements.
/// New elements are zero-initialized.
#define array_grow_by(self, count) \
do { \
if ((count) == 0) break; \
_array__grow((Array *)(self), count, array_elem_size(self)); \
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
(self)->size += (count); \
} while (0)
/// Append all elements from one array to the end of another.
#define array_push_all(self, other) \
array_extend((self), (other)->size, (other)->contents)
/// Append `count` elements to the end of the array, reading their values from the
/// `contents` pointer.
#define array_extend(self, count, contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), (self)->size, \
0, count, contents \
)
/// Remove `old_count` elements from the array starting at the given `index`. At
/// the same index, insert `new_count` new elements, reading their values from the
/// `new_contents` pointer.
#define array_splice(self, _index, old_count, new_count, new_contents) \
_array__splice( \
(Array *)(self), array_elem_size(self), _index, \
old_count, new_count, new_contents \
)
/// Insert one `element` into the array at the given `index`.
#define array_insert(self, _index, element) \
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
/// Remove one element from the array at the given `index`.
#define array_erase(self, _index) \
_array__erase((Array *)(self), array_elem_size(self), _index)
/// Pop the last element off the array, returning the element by value.
#define array_pop(self) ((self)->contents[--(self)->size])
/// Assign the contents of one array to another, reallocating if necessary.
#define array_assign(self, other) \
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
/// Swap one array with another
#define array_swap(self, other) \
_array__swap((Array *)(self), (Array *)(other))
/// Get the size of the array contents
#define array_elem_size(self) (sizeof *(self)->contents)
/// Search a sorted array for a given `needle` value, using the given `compare`
/// callback to determine the order.
///
/// If an existing element is found to be equal to `needle`, then the `index`
/// out-parameter is set to the existing value's index, and the `exists`
/// out-parameter is set to true. Otherwise, `index` is set to an index where
/// `needle` should be inserted in order to preserve the sorting, and `exists`
/// is set to false.
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
/// Search a sorted array for a given `needle` value, using integer comparisons
/// of a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, _index, _exists) \
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
/// Insert a given `value` into a sorted array, using the given `compare`
/// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
/// Insert a given `value` into a sorted array, using integer comparisons of
/// a given struct field (specified with a leading dot) to determine the order.
///
/// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \
do { \
unsigned _index, _exists; \
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
if (!_exists) array_insert(self, _index, value); \
} while (0)
// Private
typedef Array(void) Array;
/// This is not what you're looking for, see `array_delete`.
static inline void _array__delete(Array *self) {
if (self->contents) {
ts_free(self->contents);
self->contents = NULL;
self->size = 0;
self->capacity = 0;
}
}
/// This is not what you're looking for, see `array_erase`.
static inline void _array__erase(Array *self, size_t element_size,
uint32_t index) {
assert(index < self->size);
char *contents = (char *)self->contents;
memmove(contents + index * element_size, contents + (index + 1) * element_size,
(self->size - index - 1) * element_size);
self->size--;
}
/// This is not what you're looking for, see `array_reserve`.
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
if (new_capacity > self->capacity) {
if (self->contents) {
self->contents = ts_realloc(self->contents, new_capacity * element_size);
} else {
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
}
}
/// This is not what you're looking for, see `array_assign`.
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
_array__reserve(self, element_size, other->size);
self->size = other->size;
memcpy(self->contents, other->contents, self->size * element_size);
}
/// This is not what you're looking for, see `array_swap`.
static inline void _array__swap(Array *self, Array *other) {
Array swap = *other;
*other = *self;
*self = swap;
}
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
uint32_t new_size = self->size + count;
if (new_size > self->capacity) {
uint32_t new_capacity = self->capacity * 2;
if (new_capacity < 8) new_capacity = 8;
if (new_capacity < new_size) new_capacity = new_size;
_array__reserve(self, element_size, new_capacity);
}
}
/// This is not what you're looking for, see `array_splice`.
static inline void _array__splice(Array *self, size_t element_size,
uint32_t index, uint32_t old_count,
uint32_t new_count, const void *elements) {
uint32_t new_size = self->size + new_count - old_count;
uint32_t old_end = index + old_count;
uint32_t new_end = index + new_count;
assert(old_end <= self->size);
_array__reserve(self, element_size, new_size);
char *contents = (char *)self->contents;
if (self->size > old_end) {
memmove(
contents + new_end * element_size,
contents + old_end * element_size,
(self->size - old_end) * element_size
);
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
}
}
self->size += new_count - old_count;
}
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
do { \
*(_index) = start; \
*(_exists) = false; \
uint32_t size = (self)->size - *(_index); \
if (size == 0) break; \
int comparison; \
while (size > 1) { \
uint32_t half_size = size / 2; \
uint32_t mid_index = *(_index) + half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
if (comparison <= 0) *(_index) = mid_index; \
size -= half_size; \
} \
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
if (comparison == 0) *(_exists) = true; \
else if (comparison < 0) *(_index) += 1; \
} while (0)
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
/// parameter by reference in order to work with the generic sorting function above.
#define _compare_int(a, b) ((int)*(a) - (int)(b))
#ifdef _MSC_VER
#pragma warning(pop)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_ARRAY_H_
@@ -0,0 +1,286 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata {
uint8_t major_version;
uint8_t minor_version;
uint8_t patch_version;
} TSLanguageMetadata;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
// Used to index the field and supertype maps.
typedef struct {
uint16_t index;
uint16_t length;
} TSMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
void (*log)(const TSLexer *, const char *, ...);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
uint16_t reserved_word_set_id;
} TSLexerMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage {
uint32_t abi_version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexerMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
const char *name;
const TSSymbol *reserved_words;
uint16_t max_reserved_word_set_size;
uint32_t supertype_count;
const TSSymbol *supertype_symbols;
const TSMapSlice *supertype_map_slices;
const TSSymbol *supertype_map_entries;
TSLanguageMetadata metadata;
};
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
uint32_t index = 0;
uint32_t size = len - index;
while (size > 1) {
uint32_t half_size = size / 2;
uint32_t mid_index = index + half_size;
const TSCharacterRange *range = &ranges[mid_index];
if (lookahead >= range->start && lookahead <= range->end) {
return true;
} else if (lookahead > range->end) {
index = mid_index;
}
size -= half_size;
}
const TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
}
/*
* Lexer Macros
*/
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \
bool result = false; \
bool skip = false; \
UNUSED \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define ADVANCE_MAP(...) \
{ \
static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
if (map[i] == lookahead) { \
state = map[i + 1]; \
goto next_state; \
} \
} \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value) \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = (state_value), \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_name, children, precedence, prod_id) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_name, \
.child_count = children, \
.dynamic_precedence = precedence, \
.production_id = prod_id \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_