Skip to content

Commit 4ae1909

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/php-global-use-known-absent
Signed-off-by: Martin Vogel <[email protected]>
2 parents a7f24ea + 27c3303 commit 4ae1909

19 files changed

Lines changed: 1721 additions & 51 deletions

‎internal/cbm/cbm.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,31 @@ void cbm_imports_push(CBMImportArray *arr, CBMArena *a, CBMImport imp) {
402402
arr->items[arr->count++] = imp;
403403
}
404404

405+
bool cbm_imports_local_span_binds_runtime_name(const CBMImportArray *imports, const char *name,
406+
size_t len) {
407+
if (!imports || !name) {
408+
return true;
409+
}
410+
bool seen = false;
411+
for (int i = 0; i < imports->count; i++) {
412+
const CBMImport *imp = &imports->items[i];
413+
if (!imp->local_name || strncmp(imp->local_name, name, len) != 0 ||
414+
imp->local_name[len] != '\0') {
415+
continue;
416+
}
417+
if (cbm_import_binds_runtime_name(imp)) {
418+
return true;
419+
}
420+
seen = true;
421+
}
422+
return !seen;
423+
}
424+
425+
bool cbm_imports_local_binds_runtime_name(const CBMImportArray *imports, const char *local_name) {
426+
return !local_name ||
427+
cbm_imports_local_span_binds_runtime_name(imports, local_name, strlen(local_name));
428+
}
429+
405430
void cbm_usages_push(CBMUsageArray *arr, CBMArena *a, CBMUsage usage) {
406431
GROW_ARRAY(arr, a);
407432
arr->items[arr->count++] = usage;

‎internal/cbm/cbm.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,20 @@ typedef struct {
398398
const char *member; // ES named import: the exported name the local binds (`get` in
399399
// `import { get as navigate } from "axios"`); NULL otherwise
400400
bool is_default; // ES default import (`import X from "Y"`), JS/TS only (#1916)
401+
bool type_checking_only; // Python: inside a module-level `if TYPE_CHECKING:` block --
402+
// binds the name for annotations, never at runtime (#1277)
401403
CBMImportKind kind; // CBM_IMPORT_KIND_DEFAULT unless the syntax names a member kind
402404
} CBMImport;
403405

406+
/* True when an import binds its local name at runtime. A Python import under
407+
* `if TYPE_CHECKING:` exists for annotations only: a call through that name
408+
* cannot happen, so no CALLS import map may bind it (an import_map edge
409+
* through it would be fabricated by construction). THE one rule every CALLS
410+
* import-map builder applies. */
411+
static inline bool cbm_import_binds_runtime_name(const CBMImport *imp) {
412+
return imp && !imp->type_checking_only;
413+
}
414+
404415
/* Content-only ordered Python namespace facts; no resolved target identities. */
405416
#define CBM_PY_NAMESPACE_FACTS_VERSION 1u
406417

@@ -1306,6 +1317,14 @@ void cbm_py_namespace_test_copy_fail_after(int successful_allocations);
13061317
void cbm_defs_push(CBMDefArray *arr, CBMArena *a, CBMDefinition def);
13071318
void cbm_calls_push(CBMCallArray *arr, CBMArena *a, CBMCall call);
13081319
void cbm_imports_push(CBMImportArray *arr, CBMArena *a, CBMImport imp);
1320+
/* True unless every import of `imports` that binds `local_name` is
1321+
* annotation-only (cbm_import_binds_runtime_name), so also true when no import
1322+
* names it: the rule for the builders that key IMPORTS edges by local name. */
1323+
bool cbm_imports_local_binds_runtime_name(const CBMImportArray *imports, const char *local_name);
1324+
/* The same for the `len` bytes at `name` (not NUL-terminated): lets a caller
1325+
* decide before it copies the name. */
1326+
bool cbm_imports_local_span_binds_runtime_name(const CBMImportArray *imports, const char *name,
1327+
size_t len);
13091328
void cbm_usages_push(CBMUsageArray *arr, CBMArena *a, CBMUsage usage);
13101329
void cbm_throws_push(CBMThrowArray *arr, CBMArena *a, CBMThrow thr);
13111330
void cbm_rw_push(CBMRWArray *arr, CBMArena *a, CBMReadWrite rw);

‎internal/cbm/extract_defs.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11493,9 +11493,12 @@ static void push_nested_class_nodes(TSNode body, const CBMLangSpec *spec, wd_sta
1149311493
wd_push(s, child, enclosing_qn);
1149411494
} else {
1149511495
const char *ck = ts_node_type(child);
11496-
/* A nested class inside #if/#else is still nested here. */
11496+
/* A nested class inside #if/#else is still nested here, and so is
11497+
* a decorated one (Python `@dataclass class Meta:` in a class
11498+
* body): the decorated_definition wraps the class_definition. */
1149711499
if (strcmp(ck, "field_declaration") == 0 ||
1149811500
strcmp(ck, "template_declaration") == 0 || strcmp(ck, "declaration") == 0 ||
11501+
(ctx->language == CBM_LANG_PYTHON && strcmp(ck, "decorated_definition") == 0) ||
1149911502
cbm_kind_in_set(child, cbm_conditional_block_types(spec->language))) {
1150011503
ts_nstack_push(&nc_stack, child);
1150111504
}

‎internal/cbm/extract_imports.c‎

Lines changed: 152 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ static const char *python_import_root(CBMArena *a, const char *path) {
123123
return dot ? cbm_arena_strndup(a, path, (size_t)(dot - path)) : path;
124124
}
125125

126+
/* Spell `from <module> import <name>` as one dotted path. A package-relative
127+
* module (`from . import x`, `from .. import x`) is nothing but dots, which
128+
* already end in the separator: joining with another '.' would add a level and
129+
* name the PARENT package ("..x"), so the import resolved one package too high
130+
* and fell to a same-named definition anywhere in the project. */
131+
static const char *python_join_from_import(CBMArena *a, const char *module, const char *name) {
132+
size_t len = strlen(module);
133+
if (len > 0 && module[len - SKIP_ONE] == '.') {
134+
return cbm_arena_sprintf(a, "%s%s", module, name);
135+
}
136+
return cbm_arena_sprintf(a, "%s.%s", module, name);
137+
}
138+
126139
// --- Go imports ---
127140
// import_declaration -> import_spec_list -> import_spec -> (name, path)
128141

@@ -209,7 +222,7 @@ static void emit_py_aliased_import(CBMExtractCtx *ctx, TSNode child, const char
209222
}
210223
const char *local = !ts_node_is_null(alias_node) ? cbm_node_text(a, alias_node, ctx->source)
211224
: path_last(a, name);
212-
const char *full = mod_prefix ? cbm_arena_sprintf(a, "%s.%s", mod_prefix, name) : name;
225+
const char *full = mod_prefix ? python_join_from_import(a, mod_prefix, name) : name;
213226
CBMImport imp = {.local_name = local, .module_path = full};
214227
cbm_imports_push(&ctx->result->imports, a, imp);
215228
}
@@ -267,7 +280,7 @@ static void emit_py_import_from_name(CBMExtractCtx *ctx, TSNode child, const cha
267280
CBMArena *a = ctx->arena;
268281
char *name = cbm_node_text(a, child, ctx->source);
269282
if (name && name[0]) {
270-
const char *full = mod_path ? cbm_arena_sprintf(a, "%s.%s", mod_path, name) : name;
283+
const char *full = mod_path ? python_join_from_import(a, mod_path, name) : name;
271284
CBMImport imp = {.local_name = name, .module_path = full};
272285
cbm_imports_push(&ctx->result->imports, a, imp);
273286
}
@@ -321,6 +334,139 @@ static void process_py_import_from(CBMExtractCtx *ctx, TSNode node) {
321334
}
322335
}
323336

337+
/* `TYPE_CHECKING` or `<module>.TYPE_CHECKING` (typing.TYPE_CHECKING, t.TYPE_CHECKING). */
338+
static bool py_is_type_checking_condition(CBMExtractCtx *ctx, TSNode cond) {
339+
if (ts_node_is_null(cond)) {
340+
return false;
341+
}
342+
TSNode name = cond;
343+
if (strcmp(ts_node_type(cond), "attribute") == 0) {
344+
name = ts_node_child_by_field_name(cond, TS_FIELD("attribute"));
345+
}
346+
if (ts_node_is_null(name) || strcmp(ts_node_type(name), "identifier") != 0) {
347+
return false;
348+
}
349+
const char *text = cbm_node_text(ctx->arena, name, ctx->source);
350+
return text && strcmp(text, "TYPE_CHECKING") == 0;
351+
}
352+
353+
/* One import statement; `type_only` flags every import it records. */
354+
static void process_py_import_node(CBMExtractCtx *ctx, TSNode node, bool type_only) {
355+
const char *kind = ts_node_type(node);
356+
int before = ctx->result->imports.count;
357+
if (strcmp(kind, "import_statement") == 0) {
358+
process_py_import_stmt(ctx, node);
359+
} else if (strcmp(kind, "import_from_statement") == 0 ||
360+
strcmp(kind, "future_import_statement") == 0) {
361+
// `from __future__ import annotations` is a distinct node type in
362+
// tree-sitter-python but has the same shape (module + name list).
363+
process_py_import_from(ctx, node);
364+
}
365+
for (int i = before; type_only && i < ctx->result->imports.count; i++) {
366+
ctx->result->imports.items[i].type_checking_only = true;
367+
}
368+
}
369+
370+
static bool py_is_import_node(TSNode node) {
371+
const char *kind = ts_node_type(node);
372+
return strcmp(kind, "import_statement") == 0 || strcmp(kind, "import_from_statement") == 0 ||
373+
strcmp(kind, "future_import_statement") == 0;
374+
}
375+
376+
static bool py_import_is_wildcard(TSNode import_node) {
377+
uint32_t n = ts_node_child_count(import_node);
378+
for (uint32_t i = 0; i < n; i++) {
379+
if (strcmp(ts_node_type(ts_node_child(import_node, i)), "wildcard_import") == 0) {
380+
return true;
381+
}
382+
}
383+
return false;
384+
}
385+
386+
/* Records, unflagged, every import statement anywhere under `root` (the
387+
* runtime alternative of a TYPE_CHECKING block usually nests them in
388+
* try/except ImportError). An iterative pre-order walk: adversarially deep
389+
* nesting must not exhaust the stack. Returns true when one of them is a star
390+
* import. */
391+
static bool py_record_subtree_imports(CBMExtractCtx *ctx, TSNode root) {
392+
bool star = false;
393+
TSTreeCursor cursor = ts_tree_cursor_new(root);
394+
for (;;) {
395+
TSNode node = ts_tree_cursor_current_node(&cursor);
396+
bool is_import = py_is_import_node(node);
397+
if (is_import) {
398+
process_py_import_node(ctx, node, false);
399+
star = star || py_import_is_wildcard(node);
400+
}
401+
if (!is_import && ts_tree_cursor_goto_first_child(&cursor)) {
402+
continue;
403+
}
404+
while (!ts_tree_cursor_goto_next_sibling(&cursor)) {
405+
if (!ts_tree_cursor_goto_parent(&cursor)) {
406+
ts_tree_cursor_delete(&cursor);
407+
return star;
408+
}
409+
}
410+
}
411+
}
412+
413+
/* Drops the imports [first, mark) whose local name an import at [mark, count)
414+
* binds as well (every one when `star`), then the imports [mark, count). */
415+
static void py_drop_rebound_imports(CBMImportArray *imports, int first, int mark, bool star) {
416+
int keep = first;
417+
for (int i = first; i < mark; i++) {
418+
const CBMImport *imp = &imports->items[i];
419+
bool rebound = star;
420+
for (int j = mark; !rebound && j < imports->count; j++) {
421+
rebound = imp->local_name && imports->items[j].local_name &&
422+
strcmp(imp->local_name, imports->items[j].local_name) == 0;
423+
}
424+
if (!rebound) {
425+
imports->items[keep++] = *imp;
426+
}
427+
}
428+
imports->count = keep;
429+
}
430+
431+
/* A module-level `if TYPE_CHECKING:` holds imports a type checker sees and
432+
* the interpreter never runs (#1277: an annotated field's type is usually
433+
* imported exactly there, to avoid an import cycle). Record the imports of
434+
* its consequence block, flagged type_checking_only. Its elif/else branches
435+
* are runtime alternatives and stay unrecorded, as every other compound
436+
* statement's imports do; a name they bind again (the compat shape: the same
437+
* name imported in `else:` under try/except ImportError) is bound at runtime,
438+
* so its TYPE_CHECKING import is no annotation-only import and is dropped as
439+
* well, and a star import there may bind any name. */
440+
static void process_py_type_checking_block(CBMExtractCtx *ctx, TSNode if_node) {
441+
if (!py_is_type_checking_condition(
442+
ctx, ts_node_child_by_field_name(if_node, TS_FIELD("condition")))) {
443+
return;
444+
}
445+
TSNode body = ts_node_child_by_field_name(if_node, TS_FIELD("consequence"));
446+
if (ts_node_is_null(body)) {
447+
return;
448+
}
449+
int first = ctx->result->imports.count;
450+
TSTreeCursor cursor = ts_tree_cursor_new(body);
451+
if (ts_tree_cursor_goto_first_child(&cursor)) {
452+
do {
453+
process_py_import_node(ctx, ts_tree_cursor_current_node(&cursor), true);
454+
} while (ts_tree_cursor_goto_next_sibling(&cursor));
455+
}
456+
ts_tree_cursor_delete(&cursor);
457+
int mark = ctx->result->imports.count;
458+
bool star = false;
459+
uint32_t n = ts_node_child_count(if_node);
460+
for (uint32_t i = 0; i < n; i++) {
461+
TSNode alt = ts_node_child(if_node, i);
462+
const char *kind = ts_node_type(alt);
463+
if (strcmp(kind, "elif_clause") == 0 || strcmp(kind, "else_clause") == 0) {
464+
star = py_record_subtree_imports(ctx, alt) || star;
465+
}
466+
}
467+
py_drop_rebound_imports(&ctx->result->imports, first, mark, star);
468+
}
469+
324470
static void parse_python_imports(CBMExtractCtx *ctx) {
325471
TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
326472
if (!ts_tree_cursor_goto_first_child(&cursor)) {
@@ -329,15 +475,10 @@ static void parse_python_imports(CBMExtractCtx *ctx) {
329475
}
330476
do {
331477
TSNode node = ts_tree_cursor_current_node(&cursor);
332-
const char *kind = ts_node_type(node);
333-
334-
if (strcmp(kind, "import_statement") == 0) {
335-
process_py_import_stmt(ctx, node);
336-
} else if (strcmp(kind, "import_from_statement") == 0 ||
337-
strcmp(kind, "future_import_statement") == 0) {
338-
// `from __future__ import annotations` is a distinct node type in
339-
// tree-sitter-python but has the same shape (module + name list).
340-
process_py_import_from(ctx, node);
478+
if (strcmp(ts_node_type(node), "if_statement") == 0) {
479+
process_py_type_checking_block(ctx, node);
480+
} else {
481+
process_py_import_node(ctx, node, false);
341482
}
342483
} while (ts_tree_cursor_goto_next_sibling(&cursor));
343484
ts_tree_cursor_delete(&cursor);

‎internal/cbm/lsp/lsp_work.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ extern _Thread_local uint64_t cbm_lsp_work_steps;
1717
#define CBM_LSP_WORK(n) (cbm_lsp_work_steps += (uint64_t)(n))
1818
/* Read and reset the calling thread's counter. */
1919
uint64_t cbm_lsp_work_take(void);
20+
/* Test-only work ceiling (0 = none). A regression test that reverts a bound
21+
* sets it so the reverted, unbounded loop stops deterministically and the test
22+
* reports RED instead of hanging the runner. Seam builds only: a product build
23+
* has no ceiling, so it can never decide graph content. */
24+
extern _Thread_local uint64_t cbm_lsp_work_ceiling;
25+
#define CBM_LSP_WORK_CEILING_HIT() \
26+
(cbm_lsp_work_ceiling != 0 && cbm_lsp_work_steps > cbm_lsp_work_ceiling)
2027
#else
2128
#define CBM_LSP_WORK(n) ((void)0)
29+
#define CBM_LSP_WORK_CEILING_HIT() (0)
2230
#endif
2331

2432
#endif /* CBM_LSP_WORK_H */

‎internal/cbm/lsp/php_lsp.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4372,6 +4372,17 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
43724372
}
43734373
}
43744374

4375+
/* Index the registry (F12). Unindexed, every lookup below was a linear
4376+
* scan over the stdlib plus every definition of the file: O(lookups x
4377+
* definitions), quadratic in the size of one file. First-registered
4378+
* answers keep the result of that scan (the first registration of a
4379+
* duplicate QN wins), so the output does not change. Index memory goes to
4380+
* a scratch arena that dies with this call, as in cbm_run_php_lsp_cross. */
4381+
reg.index_first_registered = true;
4382+
CBMArena idx_arena;
4383+
cbm_arena_init(&idx_arena);
4384+
cbm_registry_finalize_into(&reg, &idx_arena);
4385+
43754386
/* Phase C: run the resolver. */
43764387
PHPLSPContext ctx;
43774388
php_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
@@ -4401,6 +4412,11 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
44014412
ctx.use_count = 0;
44024413
}
44034414

4415+
/* Trait flattening registered methods after the index was built; lookups
4416+
* would tail-scan them. Re-index once, first-registered as above, so the
4417+
* answers equal those of the indexed part followed by the tail scan. */
4418+
cbm_registry_finalize_into(&reg, &idx_arena);
4419+
44044420
php_lsp_process_file(&ctx, root);
44054421

44064422
if (ctx.debug) {
@@ -4414,6 +4430,7 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
44144430
rc->confidence);
44154431
}
44164432
}
4433+
cbm_arena_destroy(&idx_arena);
44174434
}
44184435

44194436
/* ── Cross-file + batch ───────────────────────────────────────── */

0 commit comments

Comments
 (0)