Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/cbm/cbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ typedef struct {
// C++ operator). Never fall back to textual resolution.
bool callee_is_locally_bound; // bare call foo() whose callee identifier is bound as a
// parameter of an enclosing function, so it cannot be the
// module-level foo. Python only today. Read by the
// module-level foo. Python: parameters. JS/TS/TSX: also a
// local that names no definition (const f = wrap(...),
// an array-pattern element). Read by the
// weak-local-binding guard. Default false.
bool receiver_is_amd_dependency; // JS/TS member call x.foo() whose receiver root x is a
// parameter the factory of an enclosing AMD
Expand Down
1 change: 1 addition & 0 deletions internal/cbm/extract_unified.c
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,7 @@ void cbm_extract_unified(CBMExtractCtx *ctx) {
state.py_param_stack_count = 0;
state.py_param_tracking_failed = false;
state.usage_start_index = ctx->result->usages.count;
state.call_start_index = ctx->result->calls.count;
state.root_lexical_scope_id = add_lexical_scope(&state, ctx->root, CBM_LEXICAL_SCOPE_MODULE);
/* Base walk-state tuple (previously established by the first
* recompute_state call): module scope, nothing else active. */
Expand Down
7 changes: 7 additions & 0 deletions internal/cbm/extract_unified.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ typedef struct {
uint32_t active_start;
uint32_t active_end;
const char *name;
/* JS/TS only: a parameter, or a local this declaration names for a value
* that is no function it declares (`const save = useCallback(...)`,
* `const [open, setOpen] = useState()`). No definition in the project
* carries that name through this binding, so a bare call of it cannot be
* placed by short name. Sits in the struct's tail padding. */
bool names_no_definition;
} CBMLexicalBinding;

typedef enum {
Expand Down Expand Up @@ -189,6 +195,7 @@ typedef struct {
int lexical_binding_capacity;
int lexical_binding_count;
int usage_start_index;
int call_start_index;
bool lexical_binding_tracking_failed;
CBMPythonDirective *python_directives;
CBMPythonDirective inline_python_directives[INLINE_PYTHON_DIRECTIVES];
Expand Down
144 changes: 142 additions & 2 deletions internal/cbm/extract_usages.c
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,63 @@ static bool js_var_binding(TSNode node) {
return false;
}

static bool js_family_language(CBMLanguage language) {
return language == CBM_LANG_JAVASCRIPT || language == CBM_LANG_TYPESCRIPT ||
language == CBM_LANG_TSX || language == CBM_LANG_ARKTS;
}

/* Does a JS/TS declarator's initializer declare a function or class under the
* declarator's name? Those are graph definitions (`const save = () => {}`), so
* a call of the name has a real target. Type-only wrappers are looked through. */
static bool js_value_declares_callable(TSNode value) {
for (int hops = 0; hops < CBM_SZ_8 && !ts_node_is_null(value); hops++) {
const char *kind = ts_node_type(value);
bool wrapper =
strcmp(kind, "parenthesized_expression") == 0 || strcmp(kind, "as_expression") == 0 ||
strcmp(kind, "satisfies_expression") == 0 || strcmp(kind, "non_null_expression") == 0;
if (!wrapper) {
return strcmp(kind, "arrow_function") == 0 ||
strcmp(kind, "function_expression") == 0 || strcmp(kind, "function") == 0 ||
strcmp(kind, "generator_function") == 0 || strcmp(kind, "class") == 0;
}
value = ts_node_named_child(value, 0);
}
return false;
}

/* True when a JS/TS binding names no definition of the project: a parameter,
* a declarator whose initializer is not a function or class
* (`const save = useCallback(...)`), or an array-pattern element
* (`const [open, setOpen] = useState()`). The declaration chose the name, so a
* project function that merely shares it is unrelated.
*
* Deliberately NOT flagged: a nested function declaration and a function-valued
* declarator (real definitions), a renamed object-pattern value and a
* declarator without initializer (assigned later, unknown). A shorthand
* object-pattern name (`const { removeItem } = useStore()`, a destructured
* prop) is no binding occurrence for this walk at all, so it is never flagged
* either; in a declarator that name is a member some definition does declare. */
static bool js_binding_names_no_definition(TSNode node, bool parameter) {
if (parameter) {
return true;
}
TSNode parent = ts_node_parent(node);
if (ts_node_is_null(parent)) {
return false;
}
const char *kind = ts_node_type(parent);
if (strcmp(kind, "array_pattern") == 0) {
return true;
}
if (strcmp(kind, "variable_declarator") != 0) {
return false;
}
TSNode name = ts_node_child_by_field_name(parent, TS_FIELD("name"));
TSNode value = ts_node_child_by_field_name(parent, TS_FIELD("value"));
return !ts_node_is_null(name) && ts_node_eq(name, node) && !ts_node_is_null(value) &&
!js_value_declares_callable(value);
}

static bool import_kind_matches(TSNode node, const CBMLangSpec *spec) {
return (spec->import_node_types && cbm_kind_in_set(node, spec->import_node_types)) ||
(spec->import_from_types && cbm_kind_in_set(node, spec->import_from_types));
Expand Down Expand Up @@ -2412,8 +2469,7 @@ static void record_lexical_binding(CBMExtractCtx *ctx, WalkState *state, TSNode
} else if (ctx->language == CBM_LANG_POWERSHELL) {
scope_id = lexical_ancestor_of_kind(state, current_id, true, false);
whole_scope = true;
} else if (ctx->language == CBM_LANG_JAVASCRIPT || ctx->language == CBM_LANG_TYPESCRIPT ||
ctx->language == CBM_LANG_TSX || ctx->language == CBM_LANG_ARKTS) {
} else if (js_family_language(ctx->language)) {
bool is_var = js_var_binding(node);
scope_id = lexical_ancestor_of_kind(state, current_id, is_var, !is_var);
if (scope_id == 0) {
Expand Down Expand Up @@ -2449,6 +2505,9 @@ static void record_lexical_binding(CBMExtractCtx *ctx, WalkState *state, TSNode
* signature/body languages extend the scope after parameters are seen. */
binding->active_end = 0;
binding->name = name;
binding->names_no_definition = !import_binding && ts_node_is_null(function_declaration) &&
js_family_language(ctx->language) &&
js_binding_names_no_definition(node, parameter);
}

static int compare_lexical_bindings(const void *left_ptr, const void *right_ptr) {
Expand Down Expand Up @@ -2531,6 +2590,84 @@ static const CBMLexicalScope *lexical_binding_scope_for_usage(const WalkState *s
return NULL;
}

/* Innermost lexical scope holding `position`, 0 when none does. Scopes are
* appended as the walk opens them, so their start bytes never decrease: the
* last one starting at or before the position is the innermost candidate, and
* each one that already closed hands over to its parent. */
static uint32_t lexical_scope_at(const WalkState *state, uint32_t position) {
int low = 0;
int high = state->lexical_scope_count;
while (low < high) {
int middle = low + (high - low) / PAIR_LEN;
if (state->lexical_scopes[middle].start_byte <= position) {
low = middle + 1;
} else {
high = middle;
}
}
uint32_t scope_id = (uint32_t)low;
int remaining = state->lexical_scope_count;
while (scope_id != 0 && remaining-- > 0) {
const CBMLexicalScope *scope = usage_lexical_scope(state, scope_id);
if (!scope || position < scope->end_byte) {
break;
}
scope_id = scope->parent_id;
}
return scope_id;
}

/* JS/TS: flag every bare call `name()` whose nearest binding of `name` is a
* local one that names no definition (js_binding_names_no_definition). The
* resolver then refuses a weak short-name match for it, as it does for a
* Python parameter: `const submit = useCallback(...)` followed by `submit()`
* must not become a CALLS edge to an unrelated `submit` in a file the caller
* never imports.
*
* Runs after the walk because let/const/var bind their whole scope: a handler
* may call a local that is declared further down. A scope that binds the name
* more than once is flagged only when every active binding agrees, so a real
* nested definition always keeps its edge. On an allocation failure nothing is
* flagged, which costs a suppression and never a true edge. */
static void flag_js_locally_bound_callees(CBMExtractCtx *ctx, const WalkState *state) {
for (int i = state->call_start_index; i < ctx->result->calls.count; i++) {
CBMCall *call = &ctx->result->calls.items[i];
const char *name = call->callee_name;
if (!name || !name[0] || strchr(name, '.') || call->site_end_byte == 0) {
continue;
}
uint32_t scope_id = lexical_scope_at(state, call->site_start_byte);
int remaining = state->lexical_scope_count;
while (scope_id != 0 && remaining-- > 0) {
const CBMLexicalScope *scope = usage_lexical_scope(state, scope_id);
if (!scope) {
break;
}
bool bound = false;
bool no_definition = true;
for (int b = lexical_binding_lower_bound(state, scope_id, name);
b < state->lexical_binding_count; b++) {
const CBMLexicalBinding *binding = &state->lexical_bindings[b];
if (binding->scope_id != scope_id || strcmp(binding->name, name) != 0) {
break;
}
uint32_t active_end = binding->active_end ? binding->active_end : scope->end_byte;
if (binding->active_start <= call->site_start_byte &&
call->site_start_byte < active_end) {
bound = true;
no_definition = no_definition && binding->names_no_definition;
}
}
if (bound) {
call->callee_is_locally_bound =
no_definition && scope->kind != CBM_LEXICAL_SCOPE_MODULE;
break;
}
scope_id = scope->lookup_parent_id;
}
}
}

void cbm_finalize_lexical_usages(CBMExtractCtx *ctx, WalkState *state) {
if (!ctx || !state) {
return;
Expand All @@ -2539,6 +2676,9 @@ void cbm_finalize_lexical_usages(CBMExtractCtx *ctx, WalkState *state) {
qsort(state->lexical_bindings, (size_t)state->lexical_binding_count,
sizeof(*state->lexical_bindings), compare_lexical_bindings);
}
if (js_family_language(ctx->language) && !state->lexical_binding_tracking_failed) {
flag_js_locally_bound_callees(ctx, state);
}
for (int i = state->usage_start_index; i < ctx->result->usages.count; i++) {
CBMUsage *usage = &ctx->result->usages.items[i];
if (!usage->ref_name || !usage->ref_name[0]) {
Expand Down
8 changes: 4 additions & 4 deletions src/pipeline/pass_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1346,10 +1346,10 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
lang == CBM_LANG_SWIFT;
/* Bare-call local-binding suppression. A member call has a receiver the
* guard above can reason about; a bare `run()` has none, so that guard
* cannot see this class at all. Python-only today because the extraction
* flag is set only for Python — this gate MUST match pass_parallel.c's
* exactly, for the same divergence reason noted above. */
bool suppress_weak_local_binding = lang == CBM_LANG_PYTHON;
* cannot see this class at all. The language set is the one the extractor
* sets the flag for, and lives in the helper so that this gate and
* pass_parallel.c's cannot diverge. */
bool suppress_weak_local_binding = cbm_weak_local_binding_guard_lang(lang);
/* The member guard's one exemption (Python, self/cls-rooted receiver,
* unique_name, not a builtin type's method) — see
* cbm_weak_member_unique_name_exempt. MUST match pass_parallel.c exactly. */
Expand Down
4 changes: 2 additions & 2 deletions src/pipeline/pass_parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3558,8 +3558,8 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
/* Swift initializer calls only — see pass_calls.c */
lang == CBM_LANG_SWIFT;
/* Bare-call local-binding suppression — see the note in pass_calls.c.
* This gate MUST stay identical to the one there. */
bool suppress_weak_local_binding = lang == CBM_LANG_PYTHON;
* The shared helper keeps this gate identical to the one there. */
bool suppress_weak_local_binding = cbm_weak_local_binding_guard_lang(lang);
/* The member guard's one exemption — MUST match pass_calls.c exactly. */
/* #1355: same guard as pass_calls.c, and joined to drop_plain_call for
* the same reason — a `continue` here would also skip route/HTTP/CONFIG
Expand Down
12 changes: 8 additions & 4 deletions src/pipeline/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,16 @@ bool cbm_weak_member_unique_name_exempt(bool is_python, bool receiver_is_self_at
* from a weak short-name strategy — so the edge is fabricated by construction
* (`def f(run): run()` must not bind an unrelated `SatoriLive.run`). Shares the
* member guard's drop-list, so lsp_* / import / same-module matches are kept.
* Deliberately keyed on the SCOPE FACT, not on the callee's spelling. The
* language set lives at the call sites (pass_calls.c / pass_parallel.c) and must
* be identical in both, or the sequential and parallel resolvers diverge.
* Pure; unit-tested in test_registry.c. */
* Deliberately keyed on the SCOPE FACT, not on the callee's spelling. Both
* call sites (pass_calls.c / pass_parallel.c) take `enabled` from
* cbm_weak_local_binding_guard_lang, so the sequential and parallel resolvers
* cannot diverge. Pure; unit-tested in test_registry.c. */
bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_bound,
const char *strategy);
/* The languages whose extractor sets CBMCall.callee_is_locally_bound: Python
* (a parameter) and JS/TS/TSX/ArkTS (a parameter, or a local that names no
* definition). Pure; unit-tested in test_registry.c. */
bool cbm_weak_local_binding_guard_lang(CBMLanguage lang);
/* #1355: drop a project-wide same-name guess (suffix_match / unique_name /
* field_type_hint / fuzzy) for a BARE call whose name the calling file binds to
* a NON-RELATIVE (package) import that resolved to nothing in the graph —
Expand Down
9 changes: 9 additions & 0 deletions src/pipeline/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,15 @@ bool cbm_weak_member_unique_name_exempt(bool is_python, bool receiver_is_self_at
* same reason as the member guard: the two call sites in pass_calls.c and
* pass_parallel.c MUST enumerate the identical language set, or the sequential
* and parallel resolvers diverge. Pure; unit-tested in test_registry.c. */
/* The languages whose extractor sets CBMCall.callee_is_locally_bound: Python
* for a parameter (#1912), and the JS family for a parameter or a local that
* names no definition (`const submit = useCallback(...)`, an array-pattern
* element; #1355). One list for both resolvers. */
bool cbm_weak_local_binding_guard_lang(CBMLanguage lang) {
return lang == CBM_LANG_PYTHON || lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT ||
lang == CBM_LANG_TSX || lang == CBM_LANG_ARKTS;
}

bool cbm_suppress_weak_local_binding_call(bool enabled, bool callee_is_locally_bound,
const char *strategy) {
if (!enabled || !callee_is_locally_bound) {
Expand Down
Loading
Loading